From 4b5dd180bca0968a89d14fc57ac8e7cc57e4ac53 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 21 Mar 2024 15:52:23 +0100 Subject: [PATCH] Fix #12537 FP unusedVariable with local function declaration --- lib/tokenize.cpp | 7 +++++-- test/testvarid.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9d719cfaafa..9415848c082 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4826,7 +4826,8 @@ void Tokenizer::setVarIdPass1() // function declaration inside executable scope? Function declaration is of form: type name "(" args ")" if (scopeStack.top().isExecutable && Token::Match(tok, "%name% [,)[]")) { bool par = false; - const Token *start, *end; + const Token* start; + Token* end; // search begin of function declaration for (start = tok; Token::Match(start, "%name%|*|&|,|("); start = start->previous()) { @@ -4850,9 +4851,11 @@ void Tokenizer::setVarIdPass1() const bool isNotstartKeyword = start->next() && notstart.find(start->next()->str()) != notstart.end(); // now check if it is a function declaration - if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;") && !isNotstartKeyword) + if (Token::Match(start, "[;{}] %type% %name%|*") && par && Token::simpleMatch(end, ") ;") && !isNotstartKeyword) { // function declaration => don't set varid + tok = end; continue; + } } if ((!scopeStack.top().isEnum || !(Token::Match(tok->previous(), "{|,") && Token::Match(tok->next(), ",|=|}"))) && diff --git a/test/testvarid.cpp b/test/testvarid.cpp index f763a0f1207..a6e5e132042 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1507,6 +1507,20 @@ class TestVarID : public TestFixture { "6: }\n"; ASSERT_EQUALS(expected, actual); } + + { + const std::string actual = tokenize("void f(int n) {\n" // #12537 + " int n1;\n" + " void g(int is, int n1);\n" + " n1 = n - 1;\n" + "}\n", "test.cpp"); + const char expected[] = "1: void f ( int n@1 ) {\n" + "2: int n1@2 ;\n" + "3: void g ( int is , int n1 ) ;\n" + "4: n1@2 = n@1 - 1 ;\n" + "5: }\n"; + ASSERT_EQUALS(expected, actual); + } } void varid_sizeof() {