diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 825752e1e..021240b35 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2804,10 +2804,12 @@ std::string Tokenizer::getNameForFunctionParams(const Token *start) } -static bool setVarIdParseDeclaration(const Token **tok, const std::map &variableId) +static bool setVarIdParseDeclaration(const Token **tok, const std::map &variableId, bool executableScope) { const Token *tok2 = *tok; + bool ref = false; + if (!tok2->isName()) return false; @@ -2840,15 +2842,22 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::mapstr() != "*" && tok2->str() != "&" && tok2->str() != "::") { + } else if (tok2->str() == "&") { + ref = true; + } else if (tok2->str() != "*" && tok2->str() != "::") { break; } tok2 = tok2->next(); } - if (tok2) + if (tok2) { *tok = tok2; + // In executable scopes, references must be assigned + if (executableScope && ref && tok2->str() != "=") + return false; + } + return bool(typeCount >= 2 && tok2 && Token::Match(tok2->tokAt(-2), "!!:: %type%")); } @@ -2902,7 +2911,7 @@ void Tokenizer::setVarIdNew() if (tok2->str() == "return") continue; - const bool decl = setVarIdParseDeclaration(&tok2, variableId); + const bool decl = setVarIdParseDeclaration(&tok2, variableId, executableScope.top()); if (decl && Token::Match(tok2->previous(), "%type% [;[=,)]")) { variableId[tok2->previous()->str()] = ++_varId; @@ -2911,7 +2920,7 @@ void Tokenizer::setVarIdNew() else if (decl && Token::Match(tok2->previous(), "%type% ( !!)")) { const Token *tok3 = tok2->next(); - if (!setVarIdParseDeclaration(&tok3,variableId)) { + if (!setVarIdParseDeclaration(&tok3,variableId,executableScope.top())) { variableId[tok2->previous()->str()] = ++_varId; tok = tok2->previous(); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 43ac86217..077c3baa3 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -3213,7 +3213,7 @@ private: const std::string code = "static int const SZ = 22;\n"; ASSERT_EQUALS("\n\n##file 0\n" "1: static int const SZ@1 = 22 ;\n", - tokenizeDebugListing(code)); + tokenizeDebugListing(code, false, "test.c")); } } @@ -3255,7 +3255,7 @@ private: const std::string code("int main(int flag) { if(a & flag) { return 1; } }"); ASSERT_EQUALS("\n\n##file 0\n" "1: int main ( int flag@1 ) { if ( a & flag@1 ) { return 1 ; } }\n", - tokenizeDebugListing(code)); + tokenizeDebugListing(code, false, "test.c")); } void varid44() { @@ -3275,7 +3275,7 @@ private: "2: int x@1 ;\n" "3: x@1 = a ( y * x@1 , 10 ) ;\n" "4: }\n"); - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); + ASSERT_EQUALS(expected, tokenizeDebugListing(code, false, "test.c")); } void varidFunctionCall2() {