Skip to content

Commit 457e8cf

Browse files
authored
Fix #15304 (crash in exprDependsOnThis: using-declaration replaces injected-class-name inside class definition) (#9108)
1 parent 83ec29f commit 457e8cf

3 files changed

Lines changed: 111 additions & 1 deletion

File tree

‎lib/astutils.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth)
11601160
}
11611161
if (expr->isOperatorKeyword() && !Token::simpleMatch(expr->next()->astParent(), "."))
11621162
return true;
1163-
if (expr->variable() && expr->variable()->isArgument() && !expr->variable()->type() && expr->variable()->scope()->function && expr->variable()->scope()->function->templateDef)
1163+
if (expr->variable() && expr->variable()->isArgument() && !expr->variable()->type() && expr->variable()->scope() && expr->variable()->scope()->function && expr->variable()->scope()->function->templateDef)
11641164
return true;
11651165
}
11661166
if (onVar && expr->variable()) {

‎lib/tokenize.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,28 @@ namespace {
28202820
tok2 = tok2->tokAt(-2);
28212821
}
28222822

2823+
// qualified destructor name, i.e. "~C" in "C::~C()" or "N::C::~C()"
2824+
// (the unqualified "~C()" inside the class is handled below)
2825+
if (Token::Match(tok1->tokAt(-3), "%name% :: ~ %name% (") && tok1->strAt(-3) == tok1->str())
2826+
return false;
2827+
2828+
// Inside a class the unqualified class name is the injected-class-name,
2829+
// it hides any type alias with the same name declared outside the class.
2830+
if (qualification.empty() && tok1->strAt(-1) != "::") {
2831+
for (const ScopeInfo3 *s = memberClassScope ? memberClassScope : currentScope; s; s = s->parent) {
2832+
if (s->type != ScopeInfo3::Record)
2833+
continue;
2834+
// the record name is qualified if the class is defined as "class N::C {"
2835+
const std::string::size_type pos = s->name.rfind(" :: ");
2836+
const std::string className = (pos == std::string::npos) ? s->name : s->name.substr(pos + 4);
2837+
if (className != tok1->str())
2838+
continue;
2839+
if (scope != s->fullName && scope.compare(0, s->fullName.size() + 4, s->fullName + " :: ") != 0)
2840+
return false;
2841+
break;
2842+
}
2843+
}
2844+
28232845
std::string fullScope1 = scope1;
28242846
if (!scope1.empty() && !qualification.empty())
28252847
fullScope1 += " :: ";

‎test/testsimplifyusing.cpp‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class TestSimplifyUsing : public TestFixture {
8080
TEST_CASE(simplifyUsing40);
8181
TEST_CASE(simplifyUsing41);
8282
TEST_CASE(simplifyUsing42);
83+
TEST_CASE(simplifyUsing43);
84+
TEST_CASE(simplifyUsing44);
85+
TEST_CASE(simplifyUsing45);
86+
TEST_CASE(simplifyUsing46);
8387

8488
TEST_CASE(simplifyUsing8970);
8589
TEST_CASE(simplifyUsing8971);
@@ -991,6 +995,90 @@ class TestSimplifyUsing : public TestFixture {
991995
}
992996
}
993997

998+
void simplifyUsing43() {
999+
// injected-class-name must not be replaced
1000+
const char code[] = "namespace N {\n"
1001+
"class C {\n"
1002+
" C(int* p) : p(p) {}\n"
1003+
" ~C() { unref(p); }\n"
1004+
" C& operator=(const C& other);\n"
1005+
" int* p;\n"
1006+
"};\n"
1007+
"}\n"
1008+
"using N::C;\n"
1009+
"C c;\n";
1010+
const char expected[] = "namespace N { "
1011+
"class C { "
1012+
"C ( int * p ) : p ( p ) { } "
1013+
"~ C ( ) { unref ( p ) ; } "
1014+
"C & operator= ( const C & other ) ; "
1015+
"int * p ; "
1016+
"} ; "
1017+
"} "
1018+
"N :: C c ;";
1019+
ASSERT_EQUALS(expected, tok(code));
1020+
ASSERT_EQUALS("", errout_str());
1021+
}
1022+
1023+
void simplifyUsing44() {
1024+
// injected-class-name must not be replaced, class defined with qualified name
1025+
const char code[] = "namespace N { class C; }\n"
1026+
"class N::C {\n"
1027+
" C(int* p) : p(p) {}\n"
1028+
" ~C() { unref(p); }\n"
1029+
" C& operator=(const C& other);\n"
1030+
" int* p;\n"
1031+
"};\n"
1032+
"using N::C;\n"
1033+
"C c;\n";
1034+
const char expected[] = "namespace N { class C ; } "
1035+
"class N :: C { "
1036+
"C ( int * p ) : p ( p ) { } "
1037+
"~ C ( ) { unref ( p ) ; } "
1038+
"C & operator= ( const C & other ) ; "
1039+
"int * p ; "
1040+
"} ; "
1041+
"N :: C c ;";
1042+
ASSERT_EQUALS(expected, tok(code));
1043+
ASSERT_EQUALS("", errout_str());
1044+
}
1045+
1046+
void simplifyUsing45() {
1047+
// destructor name must not be replaced in out-of-line destructor
1048+
const char code[] = "namespace N {\n"
1049+
"class C {\n"
1050+
"public:\n"
1051+
" C();\n"
1052+
" ~C();\n"
1053+
" int* p;\n"
1054+
"};\n"
1055+
"}\n"
1056+
"using N::C;\n"
1057+
"N::C::C() : p(new int) {}\n"
1058+
"N::C::~C() { delete p; }\n";
1059+
const char expected[] = "namespace N { "
1060+
"class C { "
1061+
"public: "
1062+
"C ( ) ; "
1063+
"~ C ( ) ; "
1064+
"int * p ; "
1065+
"} ; "
1066+
"} "
1067+
"N :: C :: C ( ) : p ( new int ) { } "
1068+
"N :: C :: ~ C ( ) { delete p ; }";
1069+
ASSERT_EQUALS(expected, tok(code));
1070+
ASSERT_EQUALS("", errout_str());
1071+
}
1072+
1073+
void simplifyUsing46() {
1074+
// alias after unary bitwise not must be replaced
1075+
const char code[] = "using U = unsigned char;\n"
1076+
"unsigned f() { return ~U(0) & ~U{1} & ~U(); }\n";
1077+
const char expected[] = "unsigned int f ( ) { return ~ unsigned char ( 0 ) & ~ unsigned char { 1 } & ~ unsigned char ( ) ; }";
1078+
ASSERT_EQUALS(expected, tok(code));
1079+
ASSERT_EQUALS("", errout_str());
1080+
}
1081+
9941082
void simplifyUsing8970() {
9951083
const char code[] = "using V = std::vector<int>;\n"
9961084
"struct A {\n"

0 commit comments

Comments
 (0)