From 491721a6ff5e8596a987c25be0bb0d7559e29bc3 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 9 Jan 2023 16:11:26 +0100 Subject: [PATCH] Fix spurious variable declaration with unknown macro (#4694) --- lib/tokenize.cpp | 2 ++ test/testvarid.cpp | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 87c42d9f0..234523c26 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6406,6 +6406,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co varName = varName->next(); else --typelen; + if (isCPP() && Token::Match(varName, "public:|private:|protected:")) + continue; //skip all the pointer part bool isPointerOrRef = false; while (Token::simpleMatch(varName, "*") || Token::Match(varName, "& %name% ,")) { diff --git a/test/testvarid.cpp b/test/testvarid.cpp index c817864ac..189b7ba5d 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -136,6 +136,7 @@ private: TEST_CASE(varid_in_class21); // #7788 TEST_CASE(varid_in_class22); // #10872 TEST_CASE(varid_in_class23); // #11293 + TEST_CASE(varid_in_class24); TEST_CASE(varid_namespace_1); // #7272 TEST_CASE(varid_namespace_2); // #7000 TEST_CASE(varid_namespace_3); // #8627 @@ -1988,6 +1989,22 @@ private: ASSERT_EQUALS(expected, tokenize(code, "test.cpp")); } + void varid_in_class24() { + const char code[] = "class A {\n" + " Q_OBJECT\n" + "public:\n" + " using QPtr = QPointer;\n" + "};\n"; + + const char expected[] = "1: class A {\n" + "2: Q_OBJECT\n" + "3: public:\n" + "4:\n" + "5: } ;\n"; + + ASSERT_EQUALS(expected, tokenize(code, "test.cpp")); + } + void varid_namespace_1() { // #7272 const char code[] = "namespace Blah {\n" " struct foo { int x;};\n"