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
This commit is contained in:
chrchr-github 2022-02-27 09:03:24 +01:00 committed by GitHub
parent d79d6e60db
commit ddb0a8bb0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

View File

@ -4243,7 +4243,8 @@ void Tokenizer::setVarIdPass2()
tok = tok->next(); tok = tok->next();
else if (Token::Match(tok->previous(), "!!:: %name% <") && Token::Match(tok->next()->findClosingBracket(),"> :: ~| %name%")) else if (Token::Match(tok->previous(), "!!:: %name% <") && Token::Match(tok->next()->findClosingBracket(),"> :: ~| %name%"))
tok = tok->next()->findClosingBracket()->next(); 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; continue;
while (Token::Match(tok, ":: ~| %name%")) { while (Token::Match(tok, ":: ~| %name%")) {

View File

@ -3589,7 +3589,7 @@ private:
" stringstream out;\n" " stringstream out;\n"
" out << ((ip >> 0) & 0xFF);\n" " out << ((ip >> 0) & 0xFF);\n"
" return out.str();\n" " return out.str();\n"
"}n", true); "}", true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// avoid regression from first fix attempt for #5811... // avoid regression from first fix attempt for #5811...
check("void deserialize(const std::string &data) {\n" check("void deserialize(const std::string &data) {\n"

View File

@ -344,6 +344,7 @@ private:
TEST_CASE(simplifyKnownVariablesGlobalVars); TEST_CASE(simplifyKnownVariablesGlobalVars);
TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return
TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440 TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440
TEST_CASE(simplifyKnownVariablesNamespace); // #10059
TEST_CASE(simplifyCasts1); TEST_CASE(simplifyCasts1);
TEST_CASE(simplifyCasts2); TEST_CASE(simplifyCasts2);
@ -6874,6 +6875,31 @@ private:
ASSERT_EQUALS(exp, tokenizeAndStringify(code, /*simplify=*/ true)); 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() { void simplifyKnownVariablesClassMember() {
// Ticket #2815 // Ticket #2815
{ {