SymbolDatabase: Add variable in if condition 'if (auto x = bar())'

This commit is contained in:
Daniel Marjamäki 2019-10-09 15:55:38 +02:00
parent 7e8ee31678
commit 3776604f06
2 changed files with 18 additions and 2 deletions

View File

@ -3543,7 +3543,10 @@ void Scope::getVariableList(const Settings* settings)
{
const Token *start;
if (bodyStart)
if (type == eIf)
start = classDef;
else if (bodyStart)
start = bodyStart->next();
// global scope
@ -3642,7 +3645,7 @@ void Scope::getVariableList(const Settings* settings)
}
// 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 (tok->str() == ";")
continue;

View File

@ -141,6 +141,7 @@ private:
TEST_CASE(isVariableDeclarationPointerConst);
TEST_CASE(isVariableDeclarationRValueRef);
TEST_CASE(isVariableDeclarationDoesNotIdentifyCase);
TEST_CASE(isVariableDeclarationIf);
TEST_CASE(isVariableStlType);
TEST_CASE(isVariablePointerToConstPointer);
TEST_CASE(isVariablePointerToVolatilePointer);
@ -831,6 +832,18 @@ private:
ASSERT_EQUALS("a", b->typeStartToken()->str());
}
void isVariableDeclarationIf() {
GET_SYMBOL_DB("void foo() {\n"
" for (auto& elem : items) {\n"
" if (auto x = bar()) {}\n"
" }\n"
"}");
const Token *x = Token::findsimplematch(tokenizer.tokens(), "x");
ASSERT(x);
ASSERT(x->varId());
ASSERT(x->variable());
}
void VariableValueType1() {
GET_SYMBOL_DB("typedef uint8_t u8;\n"
"static u8 x;\n");