Skip to content

Commit d24a134

Browse files
authored
Fixed #11711 (Tokenizer: varId not set properly in function call) (cppcheck-opensource#5041)
1 parent e8233ce commit d24a134

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

lib/tokenize.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4095,8 +4095,16 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap
40954095
return false;
40964096
}
40974097
} else if (Token::Match(tok2, "&|&&")) {
4098+
if (c)
4099+
return false;
40984100
ref = !bracket;
4099-
} else if (singleNameCount >= 1 && Token::Match(tok2, "( [*&]") && Token::Match(tok2->link()->next(), "(|[")) {
4101+
} else if (singleNameCount >= 1 && Token::Match(tok2, "( [*&]") && Token::Match(tok2->link(), ") (|[")) {
4102+
for (const Token* tok3 = tok2->tokAt(2); Token::Match(tok3, "!!)"); tok3 = tok3->next()) {
4103+
if (Token::Match(tok3, "(|["))
4104+
tok3 = tok3->link();
4105+
if (tok3->str() == ",")
4106+
return false;
4107+
}
41004108
bracket = true; // Skip: Seems to be valid pointer to array or function pointer
41014109
} else if (singleNameCount >= 1 && Token::Match(tok2, "( * %name% [") && Token::Match(tok2->linkAt(3), "] ) [;,]")) {
41024110
bracket = true;

test/testunusedvar.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4778,17 +4778,6 @@ class TestUnusedVar : public TestFixture {
47784778
" ref[0] = 123;\n"
47794779
"}");
47804780
ASSERT_EQUALS("", errout.str());
4781-
4782-
functionVariableUsage("void foo()\n"
4783-
"{\n"
4784-
" Foo foo;\n"
4785-
" Foo &ref = foo;\n"
4786-
" ref[0] = 123;\n"
4787-
"}",
4788-
"test.c");
4789-
TODO_ASSERT_EQUALS("[test.c:5]: (style) Variable 'foo' is assigned a value that is never used.\n",
4790-
"",
4791-
errout.str());
47924781
}
47934782

47944783
void localvaralias10() { // ticket 2004

test/testvarid.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class TestVarID : public TestFixture {
9898
TEST_CASE(varid64); // #9928 - extern const char (*x[256])
9999
TEST_CASE(varid65); // #10936
100100
TEST_CASE(varid66);
101+
TEST_CASE(varid67); // #11711 - NOT function pointer
101102
TEST_CASE(varid_for_1);
102103
TEST_CASE(varid_for_2);
103104
TEST_CASE(varid_cpp_keywords_in_c_code);
@@ -895,10 +896,14 @@ class TestVarID : public TestFixture {
895896
void varid41() {
896897
const char code1[] = "union evt; void f(const evt & event);";
897898
ASSERT_EQUALS("1: union evt ; void f ( const evt & event@1 ) ;\n",
899+
tokenize(code1));
900+
ASSERT_EQUALS("1: union evt ; void f ( const evt & event ) ;\n",
898901
tokenize(code1, "test.c"));
899902

900903
const char code2[] = "struct evt; void f(const evt & event);";
901904
ASSERT_EQUALS("1: struct evt ; void f ( const evt & event@1 ) ;\n",
905+
tokenize(code2));
906+
ASSERT_EQUALS("1: struct evt ; void f ( const evt & event ) ;\n",
902907
tokenize(code2, "test.c"));
903908
}
904909

@@ -1212,6 +1217,14 @@ class TestVarID : public TestFixture {
12121217
}
12131218
}
12141219

1220+
void varid67() { // #11711
1221+
const char code1[] = "int *x;\n"
1222+
"_Generic(*x, int: foo, default: bar)();";
1223+
const char expected1[] = "1: int * x@1 ;\n"
1224+
"2: _Generic ( * x@1 , int : foo , default : bar ) ( ) ;\n";
1225+
ASSERT_EQUALS(expected1, tokenize(code1, "test.c"));
1226+
}
1227+
12151228
void varid_for_1() {
12161229
const char code[] = "void foo(int a, int b) {\n"
12171230
" for (int a=1,b=2;;) {}\n"

0 commit comments

Comments
 (0)