From c283bc414d71d4f63c35254f31fa148b9d0e7814 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 25 Feb 2011 07:17:55 -0500 Subject: [PATCH] fix symboldatabase global variable detection --- lib/symboldatabase.cpp | 8 ++--- test/testsymboldatabase.cpp | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index a600a1a3a..7d6ca98a0 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1164,7 +1164,7 @@ void Scope::getVariableList() continue; // Search for start of statement.. - else if (!tok->previous() || !Token::Match(tok->previous(), ";|{|}|public:|protected:|private:")) + else if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|public:|protected:|private:")) continue; else if (Token::Match(tok, ";|{|}")) continue; @@ -1288,7 +1288,7 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const { localVarTok = skipPointers(closeTok->next()); - if (Token::Match(localVarTok, ":: %type% %var% ;")) + if (Token::Match(localVarTok, ":: %type% %var% ;|=")) { localTypeTok = localVarTok->next(); localVarTok = localVarTok->tokAt(2); @@ -1311,7 +1311,7 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const bool Scope::isSimpleVariable(const Token* tok) const { - return Token::Match(tok, "%var% ;"); + return Token::Match(tok, "%var% ;|="); } bool Scope::isArrayVariable(const Token* tok) const @@ -1325,7 +1325,7 @@ bool Scope::findClosingBracket(const Token* tok, const Token*& close) const if (NULL != tok && tok->str() == "<") { unsigned int depth = 0; - for (close = tok; (close != NULL) && (close->str() != ";"); close = close->next()) + for (close = tok; (close != NULL) && (close->str() != ";") && (close->str() != "="); close = close->next()) { if (close->str() == "<") { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index bfcb71b60..d3ba32467 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -93,6 +93,10 @@ private: TEST_CASE(hasInlineClassFunctionReturningFunctionPointer); TEST_CASE(hasMissingInlineClassFunctionReturningFunctionPointer); TEST_CASE(hasClassFunctionReturningFunctionPointer); + + TEST_CASE(hasGlobalVariables1); + TEST_CASE(hasGlobalVariables2); + TEST_CASE(hasGlobalVariables3); } void test_isVariableDeclarationCanHandleNull() @@ -522,6 +526,60 @@ private: ASSERT(function && function->hasBody && !function->isInline && function->retFuncPtr); } } + + void hasGlobalVariables1() + { + GET_SYMBOL_DB("int i;\n") + + ASSERT(db && db->scopeList.size() == 1); + if (db && db->scopeList.size() == 1) + { + std::list::const_iterator it = db->scopeList.begin(); + ASSERT((*it)->varlist.size() == 1); + if ((*it)->varlist.size() == 1) + { + std::list::const_iterator var = (*it)->varlist.begin(); + ASSERT(var->name() == "i"); + ASSERT(var->typeStartToken()->str() == "int"); + } + } + } + + void hasGlobalVariables2() + { + GET_SYMBOL_DB("int array[2][2];\n") + + ASSERT(db && db->scopeList.size() == 1); + if (db && db->scopeList.size() == 1) + { + std::list::const_iterator it = db->scopeList.begin(); + ASSERT((*it)->varlist.size() == 1); + if ((*it)->varlist.size() == 1) + { + std::list::const_iterator var = (*it)->varlist.begin(); + ASSERT(var->name() == "array"); + ASSERT(var->typeStartToken()->str() == "int"); + } + } + } + + void hasGlobalVariables3() + { + GET_SYMBOL_DB("int array[2][2] = { { 0, 0 }, { 0, 0 } };\n") + + ASSERT(db && db->scopeList.size() == 1); + if (db && db->scopeList.size() == 1) + { + std::list::const_iterator it = db->scopeList.begin(); + ASSERT((*it)->varlist.size() == 1); + if ((*it)->varlist.size() == 1) + { + std::list::const_iterator var = (*it)->varlist.begin(); + ASSERT(var->name() == "array"); + ASSERT(var->typeStartToken()->str() == "int"); + } + } + } }; REGISTER_TEST(TestSymbolDatabase)