diff --git a/lib/astutils.cpp b/lib/astutils.cpp index a08d86a2f86..691e8c5596c 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1160,7 +1160,7 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth) } if (expr->isOperatorKeyword() && !Token::simpleMatch(expr->next()->astParent(), ".")) return true; - if (expr->variable() && expr->variable()->isArgument() && !expr->variable()->type() && expr->variable()->scope()->function && expr->variable()->scope()->function->templateDef) + if (expr->variable() && expr->variable()->isArgument() && !expr->variable()->type() && expr->variable()->scope() && expr->variable()->scope()->function && expr->variable()->scope()->function->templateDef) return true; } if (onVar && expr->variable()) { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 72873ac2475..c97466a7fe3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2820,6 +2820,28 @@ namespace { tok2 = tok2->tokAt(-2); } + // qualified destructor name, i.e. "~C" in "C::~C()" or "N::C::~C()" + // (the unqualified "~C()" inside the class is handled below) + if (Token::Match(tok1->tokAt(-3), "%name% :: ~ %name% (") && tok1->strAt(-3) == tok1->str()) + return false; + + // Inside a class the unqualified class name is the injected-class-name, + // it hides any type alias with the same name declared outside the class. + if (qualification.empty() && tok1->strAt(-1) != "::") { + for (const ScopeInfo3 *s = memberClassScope ? memberClassScope : currentScope; s; s = s->parent) { + if (s->type != ScopeInfo3::Record) + continue; + // the record name is qualified if the class is defined as "class N::C {" + const std::string::size_type pos = s->name.rfind(" :: "); + const std::string className = (pos == std::string::npos) ? s->name : s->name.substr(pos + 4); + if (className != tok1->str()) + continue; + if (scope != s->fullName && scope.compare(0, s->fullName.size() + 4, s->fullName + " :: ") != 0) + return false; + break; + } + } + std::string fullScope1 = scope1; if (!scope1.empty() && !qualification.empty()) fullScope1 += " :: "; diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 239407f5c2f..200371ace5c 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -80,6 +80,10 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing40); TEST_CASE(simplifyUsing41); TEST_CASE(simplifyUsing42); + TEST_CASE(simplifyUsing43); + TEST_CASE(simplifyUsing44); + TEST_CASE(simplifyUsing45); + TEST_CASE(simplifyUsing46); TEST_CASE(simplifyUsing8970); TEST_CASE(simplifyUsing8971); @@ -991,6 +995,90 @@ class TestSimplifyUsing : public TestFixture { } } + void simplifyUsing43() { + // injected-class-name must not be replaced + const char code[] = "namespace N {\n" + "class C {\n" + " C(int* p) : p(p) {}\n" + " ~C() { unref(p); }\n" + " C& operator=(const C& other);\n" + " int* p;\n" + "};\n" + "}\n" + "using N::C;\n" + "C c;\n"; + const char expected[] = "namespace N { " + "class C { " + "C ( int * p ) : p ( p ) { } " + "~ C ( ) { unref ( p ) ; } " + "C & operator= ( const C & other ) ; " + "int * p ; " + "} ; " + "} " + "N :: C c ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout_str()); + } + + void simplifyUsing44() { + // injected-class-name must not be replaced, class defined with qualified name + const char code[] = "namespace N { class C; }\n" + "class N::C {\n" + " C(int* p) : p(p) {}\n" + " ~C() { unref(p); }\n" + " C& operator=(const C& other);\n" + " int* p;\n" + "};\n" + "using N::C;\n" + "C c;\n"; + const char expected[] = "namespace N { class C ; } " + "class N :: C { " + "C ( int * p ) : p ( p ) { } " + "~ C ( ) { unref ( p ) ; } " + "C & operator= ( const C & other ) ; " + "int * p ; " + "} ; " + "N :: C c ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout_str()); + } + + void simplifyUsing45() { + // destructor name must not be replaced in out-of-line destructor + const char code[] = "namespace N {\n" + "class C {\n" + "public:\n" + " C();\n" + " ~C();\n" + " int* p;\n" + "};\n" + "}\n" + "using N::C;\n" + "N::C::C() : p(new int) {}\n" + "N::C::~C() { delete p; }\n"; + const char expected[] = "namespace N { " + "class C { " + "public: " + "C ( ) ; " + "~ C ( ) ; " + "int * p ; " + "} ; " + "} " + "N :: C :: C ( ) : p ( new int ) { } " + "N :: C :: ~ C ( ) { delete p ; }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout_str()); + } + + void simplifyUsing46() { + // alias after unary bitwise not must be replaced + const char code[] = "using U = unsigned char;\n" + "unsigned f() { return ~U(0) & ~U{1} & ~U(); }\n"; + const char expected[] = "unsigned int f ( ) { return ~ unsigned char ( 0 ) & ~ unsigned char { 1 } & ~ unsigned char ( ) ; }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout_str()); + } + void simplifyUsing8970() { const char code[] = "using V = std::vector;\n" "struct A {\n"