Skip to content

Commit 047b608

Browse files
authored
Fix #12660 (Tokenizer::setVarId: varid is wrongly set for argument name that shadows method name) (#6347)
1 parent 7a3a43b commit 047b608

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &end
9999
return nullptr;
100100
if (tok->str() == "(")
101101
tok = tok->link();
102+
if (tok->str() != ")")
103+
return nullptr;
104+
if (!tok->isCpp() && !Token::Match(tok->link()->previous(), "%name%|)"))
105+
return nullptr;
102106
if (Token::Match(tok, ") ;|{|[")) {
103107
tok = tok->next();
104108
while (tok && tok->str() == "[" && tok->link()) {
@@ -131,7 +135,8 @@ const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &end
131135
tok = tok->next();
132136
if (Token::Match(tok, "= 0|default|delete ;"))
133137
tok = tok->tokAt(2);
134-
138+
if (tok && tok->str() == ":" && !Token::Match(tok->next(), "%name%|::"))
139+
return nullptr;
135140
return (tok && endsWith.find(tok->str()) != std::string::npos) ? tok : nullptr;
136141
}
137142
return nullptr;

test/testvarid.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class TestVarID : public TestFixture {
101101
TEST_CASE(varid67); // #11711 - NOT function pointer
102102
TEST_CASE(varid68); // #11740 - switch (str_chars(&strOut)[0])
103103
TEST_CASE(varid69);
104+
TEST_CASE(varid70); // #12660 - function
104105
TEST_CASE(varid_for_1);
105106
TEST_CASE(varid_for_2);
106107
TEST_CASE(varid_cpp_keywords_in_c_code);
@@ -1270,6 +1271,29 @@ class TestVarID : public TestFixture {
12701271
ASSERT_EQUALS(expected1, tokenize(code1, true));
12711272
}
12721273

1274+
void varid70() {
1275+
// isFunctionHead
1276+
const char code1[] = "int x = 1 ? (1 << 0) : 0;\n"
1277+
"void foo(bool init);\n"
1278+
"void init();\n";
1279+
const char expected1[] = "1: int x@1 ; x@1 = 1 ? ( 1 << 0 ) : 0 ;\n"
1280+
"2: void foo ( bool init@2 ) ;\n"
1281+
"3: void init ( ) ;\n";
1282+
ASSERT_EQUALS(expected1, tokenize(code1, true));
1283+
1284+
const char code2[] = "int x = 1 ? f(1 << 0) : 0;\n"
1285+
"void foo(bool init);\n"
1286+
"void init();\n";
1287+
const char expected2[] = "1: int x@1 ; x@1 = 1 ? f ( 1 << 0 ) : 0 ;\n"
1288+
"2: void foo ( bool init@2 ) ;\n"
1289+
"3: void init ( ) ;\n";
1290+
ASSERT_EQUALS(expected2, tokenize(code2, true));
1291+
1292+
const char code3[] = "extern void (*arr[10])(uint32_t some);\n";
1293+
const char expected3[] = "1: extern void ( * arr@1 [ 10 ] ) ( uint32_t some@2 ) ;\n";
1294+
ASSERT_EQUALS(expected3, tokenize(code3, true));
1295+
}
1296+
12731297
void varid_for_1() {
12741298
const char code[] = "void foo(int a, int b) {\n"
12751299
" for (int a=1,b=2;;) {}\n"

0 commit comments

Comments
 (0)