SymbolDatabase: Added testcase for variable in if-scope

This commit is contained in:
Daniel Marjamäki 2019-10-09 16:41:07 +02:00
parent 3776604f06
commit 9f4db5018d
2 changed files with 18 additions and 4 deletions

View File

@ -3543,8 +3543,12 @@ void Scope::getVariableList(const Settings* settings)
{ {
const Token *start; const Token *start;
if (type == eIf) bool beforeBody = false;
if (type == eIf) {
beforeBody = true;
start = classDef; start = classDef;
}
else if (bodyStart) else if (bodyStart)
start = bodyStart->next(); start = bodyStart->next();
@ -3565,7 +3569,10 @@ void Scope::getVariableList(const Settings* settings)
// Is it a function? // Is it a function?
else if (tok->str() == "{") { else if (tok->str() == "{") {
tok = tok->link(); if (beforeBody && tok == bodyStart)
beforeBody = false;
else
tok = tok->link();
continue; continue;
} }
@ -3645,7 +3652,9 @@ void Scope::getVariableList(const Settings* settings)
} }
// Search for start of statement.. // Search for start of statement..
else if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|(|public:|protected:|private:")) else if (beforeBody && !Token::simpleMatch(tok->previous(), "("))
continue;
else if (!beforeBody && tok->previous() && !Token::Match(tok->previous(), ";|{|}|public:|protected:|private:"))
continue; continue;
else if (tok->str() == ";") else if (tok->str() == ";")
continue; continue;

View File

@ -835,13 +835,18 @@ private:
void isVariableDeclarationIf() { void isVariableDeclarationIf() {
GET_SYMBOL_DB("void foo() {\n" GET_SYMBOL_DB("void foo() {\n"
" for (auto& elem : items) {\n" " for (auto& elem : items) {\n"
" if (auto x = bar()) {}\n" " if (auto x = bar()) { int y = 3; }\n"
" }\n" " }\n"
"}"); "}");
const Token *x = Token::findsimplematch(tokenizer.tokens(), "x"); const Token *x = Token::findsimplematch(tokenizer.tokens(), "x");
ASSERT(x); ASSERT(x);
ASSERT(x->varId()); ASSERT(x->varId());
ASSERT(x->variable()); ASSERT(x->variable());
const Token *y = Token::findsimplematch(tokenizer.tokens(), "y");
ASSERT(y);
ASSERT(y->varId());
ASSERT(y->variable());
} }
void VariableValueType1() { void VariableValueType1() {