fix symboldatabase global variable detection

This commit is contained in:
Robert Reif 2011-02-25 07:17:55 -05:00
parent 74c85b6b70
commit c283bc414d
2 changed files with 62 additions and 4 deletions

View File

@ -1164,7 +1164,7 @@ void Scope::getVariableList()
continue; continue;
// Search for start of statement.. // 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; continue;
else if (Token::Match(tok, ";|{|}")) else if (Token::Match(tok, ";|{|}"))
continue; continue;
@ -1288,7 +1288,7 @@ bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const
{ {
localVarTok = skipPointers(closeTok->next()); localVarTok = skipPointers(closeTok->next());
if (Token::Match(localVarTok, ":: %type% %var% ;")) if (Token::Match(localVarTok, ":: %type% %var% ;|="))
{ {
localTypeTok = localVarTok->next(); localTypeTok = localVarTok->next();
localVarTok = localVarTok->tokAt(2); 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 bool Scope::isSimpleVariable(const Token* tok) const
{ {
return Token::Match(tok, "%var% ;"); return Token::Match(tok, "%var% ;|=");
} }
bool Scope::isArrayVariable(const Token* tok) const 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() == "<") if (NULL != tok && tok->str() == "<")
{ {
unsigned int depth = 0; 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() == "<") if (close->str() == "<")
{ {

View File

@ -93,6 +93,10 @@ private:
TEST_CASE(hasInlineClassFunctionReturningFunctionPointer); TEST_CASE(hasInlineClassFunctionReturningFunctionPointer);
TEST_CASE(hasMissingInlineClassFunctionReturningFunctionPointer); TEST_CASE(hasMissingInlineClassFunctionReturningFunctionPointer);
TEST_CASE(hasClassFunctionReturningFunctionPointer); TEST_CASE(hasClassFunctionReturningFunctionPointer);
TEST_CASE(hasGlobalVariables1);
TEST_CASE(hasGlobalVariables2);
TEST_CASE(hasGlobalVariables3);
} }
void test_isVariableDeclarationCanHandleNull() void test_isVariableDeclarationCanHandleNull()
@ -522,6 +526,60 @@ private:
ASSERT(function && function->hasBody && !function->isInline && function->retFuncPtr); 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<Scope *>::const_iterator it = db->scopeList.begin();
ASSERT((*it)->varlist.size() == 1);
if ((*it)->varlist.size() == 1)
{
std::list<Variable>::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<Scope *>::const_iterator it = db->scopeList.begin();
ASSERT((*it)->varlist.size() == 1);
if ((*it)->varlist.size() == 1)
{
std::list<Variable>::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<Scope *>::const_iterator it = db->scopeList.begin();
ASSERT((*it)->varlist.size() == 1);
if ((*it)->varlist.size() == 1)
{
std::list<Variable>::const_iterator var = (*it)->varlist.begin();
ASSERT(var->name() == "array");
ASSERT(var->typeStartToken()->str() == "int");
}
}
}
}; };
REGISTER_TEST(TestSymbolDatabase) REGISTER_TEST(TestSymbolDatabase)