Tokenizer: Improved removing of function pointer arguments to avoid false positives

This commit is contained in:
Daniel Marjamäki 2023-03-04 09:27:51 +01:00
parent 7fd4118d60
commit f4e0ae3b84
2 changed files with 27 additions and 7 deletions

View File

@ -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();
}

View File

@ -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() {