Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
22 changes: 22 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) != "::") {
Comment thread
danmar marked this conversation as resolved.
Resolved
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 += " :: ";
Expand Down
88 changes: 88 additions & 0 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<int>;\n"
"struct A {\n"
Expand Down
Loading