From d24a1342a6bc45e99b362918e4a741fe30358018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 8 May 2023 12:11:30 +0200 Subject: [PATCH] Fixed #11711 (Tokenizer: varId not set properly in function call) (#5041) --- lib/tokenize.cpp | 10 +++++++++- test/testunusedvar.cpp | 11 ----------- test/testvarid.cpp | 13 +++++++++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 94a583202..7c7a9cb66 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4095,8 +4095,16 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap return false; } } else if (Token::Match(tok2, "&|&&")) { + if (c) + return false; ref = !bracket; - } else if (singleNameCount >= 1 && Token::Match(tok2, "( [*&]") && Token::Match(tok2->link()->next(), "(|[")) { + } else if (singleNameCount >= 1 && Token::Match(tok2, "( [*&]") && Token::Match(tok2->link(), ") (|[")) { + for (const Token* tok3 = tok2->tokAt(2); Token::Match(tok3, "!!)"); tok3 = tok3->next()) { + if (Token::Match(tok3, "(|[")) + tok3 = tok3->link(); + if (tok3->str() == ",") + return false; + } bracket = true; // Skip: Seems to be valid pointer to array or function pointer } else if (singleNameCount >= 1 && Token::Match(tok2, "( * %name% [") && Token::Match(tok2->linkAt(3), "] ) [;,]")) { bracket = true; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index b6952ab32..352e9f426 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -4778,17 +4778,6 @@ private: " ref[0] = 123;\n" "}"); ASSERT_EQUALS("", errout.str()); - - functionVariableUsage("void foo()\n" - "{\n" - " Foo foo;\n" - " Foo &ref = foo;\n" - " ref[0] = 123;\n" - "}", - "test.c"); - TODO_ASSERT_EQUALS("[test.c:5]: (style) Variable 'foo' is assigned a value that is never used.\n", - "", - errout.str()); } void localvaralias10() { // ticket 2004 diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 3827e724f..acd94c1af 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -98,6 +98,7 @@ private: TEST_CASE(varid64); // #9928 - extern const char (*x[256]) TEST_CASE(varid65); // #10936 TEST_CASE(varid66); + TEST_CASE(varid67); // #11711 - NOT function pointer TEST_CASE(varid_for_1); TEST_CASE(varid_for_2); TEST_CASE(varid_cpp_keywords_in_c_code); @@ -895,10 +896,14 @@ private: void varid41() { const char code1[] = "union evt; void f(const evt & event);"; ASSERT_EQUALS("1: union evt ; void f ( const evt & event@1 ) ;\n", + tokenize(code1)); + ASSERT_EQUALS("1: union evt ; void f ( const evt & event ) ;\n", tokenize(code1, "test.c")); const char code2[] = "struct evt; void f(const evt & event);"; ASSERT_EQUALS("1: struct evt ; void f ( const evt & event@1 ) ;\n", + tokenize(code2)); + ASSERT_EQUALS("1: struct evt ; void f ( const evt & event ) ;\n", tokenize(code2, "test.c")); } @@ -1212,6 +1217,14 @@ private: } } + void varid67() { // #11711 + const char code1[] = "int *x;\n" + "_Generic(*x, int: foo, default: bar)();"; + const char expected1[] = "1: int * x@1 ;\n" + "2: _Generic ( * x@1 , int : foo , default : bar ) ( ) ;\n"; + ASSERT_EQUALS(expected1, tokenize(code1, "test.c")); + } + void varid_for_1() { const char code[] = "void foo(int a, int b) {\n" " for (int a=1,b=2;;) {}\n"