From 0b62c73ff06e693d3a2c1db604baa91137e5b16b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 18 Jan 2023 17:11:41 +0100 Subject: [PATCH] Fix spurious variable declaration with unknown macro and using (#4698) --- lib/tokenize.cpp | 2 +- test/testvarid.cpp | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index bb1407aa3..1858f0745 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6406,7 +6406,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co varName = varName->next(); else --typelen; - if (isCPP() && Token::Match(varName, "public:|private:|protected:")) + if (isCPP() && Token::Match(varName, "public:|private:|protected:|using")) continue; //skip all the pointer part bool isPointerOrRef = false; diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 189b7ba5d..587166ac1 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1990,18 +1990,28 @@ private: } void varid_in_class24() { - const char code[] = "class A {\n" - " Q_OBJECT\n" - "public:\n" - " using QPtr = QPointer;\n" - "};\n"; + const char *code{}, *expected{}; - const char expected[] = "1: class A {\n" - "2: Q_OBJECT\n" - "3: public:\n" - "4:\n" - "5: } ;\n"; + code = "class A {\n" + " Q_OBJECT\n" + "public:\n" + " using QPtr = QPointer;\n" + "};\n"; + expected = "1: class A {\n" + "2: Q_OBJECT\n" + "3: public:\n" + "4:\n" + "5: } ;\n"; + ASSERT_EQUALS(expected, tokenize(code, "test.cpp")); + code = "class A {\n" + " Q_OBJECT\n" + " using QPtr = QPointer;\n" + "};\n"; + expected = "1: class A {\n" + "2: Q_OBJECT\n" + "3:\n" + "4: } ;\n"; ASSERT_EQUALS(expected, tokenize(code, "test.cpp")); }