From ddb0a8bb0bab0d3b8711db8b167c68e30939c073 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 27 Feb 2022 09:03:24 +0100 Subject: [PATCH] Fix #10059 missing varId with using namespace (#3860) * Fix #10059 missing varId with using namespace * Undo * Fix test * Limit namespace candidates, duplicateBranch * rvalue ref * Undo * Undo * Undo * Format * Fix condition --- lib/tokenize.cpp | 3 ++- test/testnullpointer.cpp | 2 +- test/testsimplifytokens.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2b0a4fc2e..cb5f43b8e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4243,7 +4243,8 @@ void Tokenizer::setVarIdPass2() tok = tok->next(); else if (Token::Match(tok->previous(), "!!:: %name% <") && Token::Match(tok->next()->findClosingBracket(),"> :: ~| %name%")) tok = tok->next()->findClosingBracket()->next(); - else + else if (usingnamespaces.empty() || tok->varId() || !tok->isName() || tok->isStandardType() || tok->tokType() == Token::eKeyword || tok->tokType() == Token::eBoolean || + Token::Match(tok->previous(), ".|namespace|class|struct|&|&&|*|> %name%") || Token::Match(tok->previous(), "%type%| %name% ( %type%|)") || Token::Match(tok, "public:|private:|protected:")) continue; while (Token::Match(tok, ":: ~| %name%")) { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 97bb4c896..348d40448 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -3589,7 +3589,7 @@ private: " stringstream out;\n" " out << ((ip >> 0) & 0xFF);\n" " return out.str();\n" - "}n", true); + "}", true); ASSERT_EQUALS("", errout.str()); // avoid regression from first fix attempt for #5811... check("void deserialize(const std::string &data) {\n" diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 2dc8192c2..00432efb1 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -344,6 +344,7 @@ private: TEST_CASE(simplifyKnownVariablesGlobalVars); TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440 + TEST_CASE(simplifyKnownVariablesNamespace); // #10059 TEST_CASE(simplifyCasts1); TEST_CASE(simplifyCasts2); @@ -6874,6 +6875,31 @@ private: ASSERT_EQUALS(exp, tokenizeAndStringify(code, /*simplify=*/ true)); } + void simplifyKnownVariablesNamespace() { // #10059 + const char code[] = "namespace N {\n" + " const int n = 0;\n" + " namespace M { const int m = 0; }\n" + "}\n" + "using namespace N;\n" + "int i(n);\n" + "int j(M::m);\n" + "using namespace N::M;\n" + "int k(m);\n" + "int l(N::M::m);\n"; + const char exp[] = "\n\n##file 0\n" + "1: namespace N {\n" + "2: const int n@1 = 0 ;\n" + "3: namespace M { const int m@2 = 0 ; }\n" + "4: }\n" + "5: using namespace N ;\n" + "6: int i ; i = n@1 ;\n" + "7: int j ( M :: m@2 ) ;\n" + "8: using namespace N :: M ;\n" + "9: int k ; k = m@2 ;\n" + "10: int l ( N :: M :: m@2 ) ;\n"; + ASSERT_EQUALS(exp, tokenizeDebugListing(code)); + } + void simplifyKnownVariablesClassMember() { // Ticket #2815 {