SymbolDatabase: Fix variable matching

This commit is contained in:
Daniel Marjamäki 2018-09-04 06:39:02 +02:00
parent 93be440f92
commit 5afb755a3c
4 changed files with 21 additions and 4 deletions

View File

@ -206,9 +206,6 @@ static const Token * followVariableExpression(const Token * tok, bool cpp)
if (const Variable * var2 = tok2->variable()) {
if(!var2->scope())
return tok;
// FIXME This is a quick fix. Fix SymbolDatabase so typeStartToken points at the variable declaration and not a case statement
if(Token::simpleMatch(var2->typeStartToken(), "case"))
return tok;
const Token * endToken2 = var2->scope() != tok->scope() ? var2->scope()->bodyEnd : endToken;
if (!var2->isLocal() && !var2->isConst() && !var2->isArgument())
return tok;

View File

@ -3435,6 +3435,13 @@ void Scope::getVariableList(const Settings* settings)
continue;
}
// skip case/default
if (Token::Match(tok, "case|default")) {
while (tok->next() && !Token::Match(tok->next(), "[:;{}]"))
tok = tok->next();
continue;
}
// Search for start of statement..
else if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|public:|protected:|private:"))
continue;

View File

@ -2572,7 +2572,7 @@ void Tokenizer::setVarId()
// Variable declarations can't start with "return" etc.
#define NOTSTART_C "goto", "NOT", "return", "sizeof", "typedef"
#define NOTSTART_C "case", "default", "goto", "NOT", "return", "sizeof", "typedef"
static const std::set<std::string> notstart_c = { NOTSTART_C };
static const std::set<std::string> notstart_cpp = { NOTSTART_C,
"delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template"

View File

@ -139,6 +139,7 @@ private:
TEST_CASE(isVariableDeclarationDoesNotIdentifyCppCast);
TEST_CASE(isVariableDeclarationPointerConst);
TEST_CASE(isVariableDeclarationRValueRef);
TEST_CASE(isVariableDeclarationDoesNotIdentifyCase);
TEST_CASE(isVariableStlType);
TEST_CASE(VariableValueType1);
@ -788,6 +789,18 @@ private:
ASSERT(var.tokens()->tokAt(2)->scope() != 0);
}
void isVariableDeclarationDoesNotIdentifyCase() {
GET_SYMBOL_DB_C("a b;\n"
"void f() {\n"
" switch (c) {\n"
" case b:;\n"
" }"
"}");
const Variable* b = db->getVariableFromVarId(1);
ASSERT_EQUALS("b", b->name());
ASSERT_EQUALS("a", b->typeStartToken()->str());
}
void VariableValueType1() {
GET_SYMBOL_DB("typedef uint8_t u8;\n"
"static u8 x;\n");