From f4e0ae3b84e854939af87244dc29a8fbe5757d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 4 Mar 2023 09:27:51 +0100 Subject: [PATCH] Tokenizer: Improved removing of function pointer arguments to avoid false positives --- lib/tokenize.cpp | 15 +++++++++++++-- test/testvarid.cpp | 19 ++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index eb8388944..bae9864fa 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6168,11 +6168,22 @@ void Tokenizer::simplifyFunctionPointers() Token::eraseTokens(tok->link()->linkAt(1), endTok->next()); // remove variable names - for (Token* tok3 = tok->link()->tokAt(2); Token::Match(tok3, "%name%|*|&|[|(|::|,|<"); tok3 = tok3->next()) { + int indent = 0; + for (Token* tok3 = tok->link()->tokAt(2); Token::Match(tok3, "%name%|*|&|[|(|)|::|,|<"); tok3 = tok3->next()) { + if (tok3->str() == ")" && --indent < 0) + break; if (tok3->str() == "<" && tok3->link()) tok3 = tok3->link(); - else if (Token::Match(tok3, "[|(")) + else if (Token::Match(tok3, "[")) tok3 = tok3->link(); + else if (tok3->str() == "(") { + tok3 = tok3->link(); + if (Token::simpleMatch(tok3, ") (")) { + tok3 = tok3->next(); + ++indent; + } else + break; + } if (Token::Match(tok3, "%type%|*|&|> %name% [,)[]")) tok3->deleteNext(); } diff --git a/test/testvarid.cpp b/test/testvarid.cpp index dbc53ed16..829d28564 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -2998,14 +2998,23 @@ private: } void varid_function_pointer_args() { - const char code[] = "void foo() {\n" - " char *text;\n" - " void (*cb)(char* text);\n" - "}\n"; + const char code1[] = "void foo() {\n" + " char *text;\n" + " void (*cb)(char* text);\n" + "}\n"; ASSERT_EQUALS("1: void foo ( ) {\n" "2: char * text@1 ;\n" "3: void ( * cb@2 ) ( char * ) ;\n" - "4: }\n", tokenize(code)); + "4: }\n", tokenize(code1)); + + const char code2[] = "void foo() {\n" + " char *text;\n" + " void (*f)(int (*arg)(char* text));\n" + "}\n"; + ASSERT_EQUALS("1: void foo ( ) {\n" + "2: char * text@1 ;\n" + "3: void ( * f@2 ) ( int ( * arg ) ( char * ) ) ;\n" + "4: }\n", tokenize(code2)); } void varidclass1() {