Skip to content

Commit c16acbf

Browse files
dmcppcheckclaude
andcommitted
simplifyUsing: handle class defined with qualified name and out-of-line destructor
Address review comments: - For "class N::C {" the record name is "N :: C", compare the injected-class-name against the last component. - Never replace the name after "~", e.g. in "N::C::~C()". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 84f55fb commit c16acbf

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎lib/tokenize.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2820,11 +2820,20 @@ namespace {
28202820
tok2 = tok2->tokAt(-2);
28212821
}
28222822

2823+
// destructor name, i.e. "~C" in "~C()" or "N::C::~C()"
2824+
if (tok1->strAt(-1) == "~")
2825+
return false;
2826+
28232827
// Inside a class the unqualified class name is the injected-class-name,
28242828
// it hides any type alias with the same name declared outside the class.
28252829
if (qualification.empty() && tok1->strAt(-1) != "::") {
28262830
for (const ScopeInfo3 *s = memberClassScope ? memberClassScope : currentScope; s; s = s->parent) {
2827-
if (s->type != ScopeInfo3::Record || s->name != tok1->str())
2831+
if (s->type != ScopeInfo3::Record)
2832+
continue;
2833+
// the record name is qualified if the class is defined as "class N::C {"
2834+
const std::string::size_type pos = s->name.rfind(" :: ");
2835+
const std::string className = (pos == std::string::npos) ? s->name : s->name.substr(pos + 4);
2836+
if (className != tok1->str())
28282837
continue;
28292838
if (scope != s->fullName && scope.compare(0, s->fullName.size() + 4, s->fullName + " :: ") != 0)
28302839
return false;

‎test/testsimplifyusing.cpp‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class TestSimplifyUsing : public TestFixture {
8181
TEST_CASE(simplifyUsing41);
8282
TEST_CASE(simplifyUsing42);
8383
TEST_CASE(simplifyUsing43);
84+
TEST_CASE(simplifyUsing44);
85+
TEST_CASE(simplifyUsing45);
8486

8587
TEST_CASE(simplifyUsing8970);
8688
TEST_CASE(simplifyUsing8971);
@@ -1017,6 +1019,56 @@ class TestSimplifyUsing : public TestFixture {
10171019
ASSERT_EQUALS("", errout_str());
10181020
}
10191021

1022+
void simplifyUsing44() {
1023+
// injected-class-name must not be replaced, class defined with qualified name
1024+
const char code[] = "namespace N { class C; }\n"
1025+
"class N::C {\n"
1026+
" C(int* p) : p(p) {}\n"
1027+
" ~C() { unref(p); }\n"
1028+
" C& operator=(const C& other);\n"
1029+
" int* p;\n"
1030+
"};\n"
1031+
"using N::C;\n"
1032+
"C c;\n";
1033+
const char expected[] = "namespace N { class C ; } "
1034+
"class N :: C { "
1035+
"C ( int * p ) : p ( p ) { } "
1036+
"~ C ( ) { unref ( p ) ; } "
1037+
"C & operator= ( const C & other ) ; "
1038+
"int * p ; "
1039+
"} ; "
1040+
"N :: C c ;";
1041+
ASSERT_EQUALS(expected, tok(code));
1042+
ASSERT_EQUALS("", errout_str());
1043+
}
1044+
1045+
void simplifyUsing45() {
1046+
// destructor name must not be replaced in out-of-line destructor
1047+
const char code[] = "namespace N {\n"
1048+
"class C {\n"
1049+
"public:\n"
1050+
" C();\n"
1051+
" ~C();\n"
1052+
" int* p;\n"
1053+
"};\n"
1054+
"}\n"
1055+
"using N::C;\n"
1056+
"N::C::C() : p(new int) {}\n"
1057+
"N::C::~C() { delete p; }\n";
1058+
const char expected[] = "namespace N { "
1059+
"class C { "
1060+
"public: "
1061+
"C ( ) ; "
1062+
"~ C ( ) ; "
1063+
"int * p ; "
1064+
"} ; "
1065+
"} "
1066+
"N :: C :: C ( ) : p ( new int ) { } "
1067+
"N :: C :: ~ C ( ) { delete p ; }";
1068+
ASSERT_EQUALS(expected, tok(code));
1069+
ASSERT_EQUALS("", errout_str());
1070+
}
1071+
10201072
void simplifyUsing8970() {
10211073
const char code[] = "using V = std::vector<int>;\n"
10221074
"struct A {\n"

0 commit comments

Comments
 (0)