From 0c1e979af7cb55e8439a5c02d20d085bd0ba33bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 9 Oct 2019 22:27:48 +0200 Subject: [PATCH] SymbolDatabase: Refactor handling of variable declarations in if condition --- lib/symboldatabase.cpp | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index bdcb2a6dc..d79f9e66c 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3543,14 +3543,7 @@ void Scope::getVariableList(const Settings* settings) { const Token *start; - bool beforeBody = false; - - if (type == eIf) { - beforeBody = true; - start = classDef; - } - - else if (bodyStart) + if (bodyStart) start = bodyStart->next(); // global scope @@ -3561,6 +3554,11 @@ void Scope::getVariableList(const Settings* settings) else return; + // Variable declared in condition: if (auto x = bar()) + if (Token::Match(classDef, "if|while ( %type%") && Token::simpleMatch(classDef->next()->astOperand2(), "=")) { + checkVariable(classDef->tokAt(2), defaultAccess(), settings); + } + AccessControl varaccess = defaultAccess(); for (const Token *tok = start; tok && tok != bodyEnd; tok = tok->next()) { // syntax error? @@ -3569,9 +3567,7 @@ void Scope::getVariableList(const Settings* settings) // Is it a function? else if (tok->str() == "{") { - if (beforeBody && tok == bodyStart) - beforeBody = false; - else + tok = tok->link(); continue; } @@ -3652,9 +3648,7 @@ void Scope::getVariableList(const Settings* settings) } // Search for start of statement.. - else if (beforeBody && !Token::simpleMatch(tok->previous(), "(")) - continue; - else if (!beforeBody && 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;