From ba57e15cb277e8bc673f0dcbf27b7af36897d33e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 21 May 2023 13:59:49 +0200 Subject: [PATCH] Fix #11717 Warn when C++ code is scanned as C (prevent FPs) (#5064) * Fix #11717 Warn when C++ code is scanned as C (prevent FPs) * Don't throw for operators * Detect reference variables * Fix tests * Fix compilation * Fix test * Fix another test * Use strAt() --- lib/tokenize.cpp | 4 ++-- test/testleakautovar.cpp | 8 ++++---- test/testunusedvar.cpp | 9 --------- test/testvarid.cpp | 6 ++---- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index efef99e4a..6b1f62e25 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4139,8 +4139,6 @@ 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(), ") (|[")) { for (const Token* tok3 = tok2->tokAt(2); Token::Match(tok3, "!!)"); tok3 = tok3->next()) { @@ -4667,6 +4665,8 @@ void Tokenizer::setVarIdPass1() decl = false; if (decl) { + if (isC() && Token::Match(prev2->previous(), "&|&&")) + syntaxErrorC(prev2, prev2->strAt(-2) + prev2->strAt(-1) + " " + prev2->str()); variableMap.addVariable(prev2->str(), scopeStack.size() <= 1); if (Token::simpleMatch(tok->previous(), "for (") && Token::Match(prev2, "%name% [=,]")) { diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 49959c057..37cd60bee 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -368,8 +368,8 @@ private: " char * &ref = p;\n" " p = malloc(10);\n" " free(ref);\n" - "}"); - TODO_ASSERT_EQUALS("", "[test.c:6]: (error) Memory leak: p\n", errout.str()); + "}", /*cpp*/ true); + TODO_ASSERT_EQUALS("", "[test.cpp:6]: (error) Memory leak: p\n", errout.str()); } void assign14() { @@ -2329,7 +2329,7 @@ private: void test1() { // 3809 check("void f(double*&p) {\n" " p = malloc(0x100);\n" - "}"); + "}", /*cpp*/ true); ASSERT_EQUALS("", errout.str()); } @@ -2345,7 +2345,7 @@ private: check("void f() {\n" " char *&p = x();\n" " p = malloc(10);\n" - "};"); + "};", /*cpp*/ true); ASSERT_EQUALS("", errout.str()); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 352e9f426..eea73f2ba 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -4788,15 +4788,6 @@ private: " *x = 0;\n" "}"); ASSERT_EQUALS("", errout.str()); - - functionVariableUsage("void foo(Foo &foo)\n" - "{\n" - " Foo &ref = foo;\n" - " int *x = &ref.x;\n" - " *x = 0;\n" - "}", - "test.c"); - ASSERT_EQUALS("", errout.str()); } void localvaralias11() { // #4423 - iterator diff --git a/test/testvarid.cpp b/test/testvarid.cpp index acd94c1af..e908d1d6e 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -897,14 +897,12 @@ private: 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")); + ASSERT_THROW(tokenize(code1, "test.c"), InternalError); 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")); + ASSERT_THROW(tokenize(code2, "test.c"), InternalError); } void varid42() {