From 84f55fb13b8b5ef3cafb3273006555341b6a04ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 3 Dec 2026 08:51:55 +0100 Subject: [PATCH 1/4] Fix #15304 (crash in exprDependsOnThis: using-declaration replaces injected-class-name inside class definition) `using N::C;` caused simplifyUsing to replace `C` inside the definition of class N::C, e.g. `~C()` became `~N::C()`. The destructor was then not recognized and its body was parsed as class members, creating bogus function declarations whose arguments have no scope. exprDependsOnThis dereferenced that null scope. Seen when checking quickshell-0.3.2/src/services/polkit/listener.cpp. Co-Authored-By: Claude Opus 5.5 --- lib/astutils.cpp | 2 +- lib/tokenize.cpp | 12 ++++++++++++ test/testsimplifyusing.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) 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..7c2ed501f64 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2820,6 +2820,18 @@ namespace { tok2 = tok2->tokAt(-2); } + // 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 || s->name != 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..3229ce22ee7 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -80,6 +80,7 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing40); TEST_CASE(simplifyUsing41); TEST_CASE(simplifyUsing42); + TEST_CASE(simplifyUsing43); TEST_CASE(simplifyUsing8970); TEST_CASE(simplifyUsing8971); @@ -991,6 +992,31 @@ 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 simplifyUsing8970() { const char code[] = "using V = std::vector;\n" "struct A {\n" From c16acbf0ea58dfe1857a1735b2c5ed5494bc878e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 3 Dec 2026 13:44:42 +0100 Subject: [PATCH 2/4] 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 --- lib/tokenize.cpp | 11 +++++++- test/testsimplifyusing.cpp | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7c2ed501f64..f51eed51135 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2820,11 +2820,20 @@ namespace { tok2 = tok2->tokAt(-2); } + // destructor name, i.e. "~C" in "~C()" or "N::C::~C()" + if (tok1->strAt(-1) == "~") + 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 || s->name != tok1->str()) + 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; diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 3229ce22ee7..8d2fd6a8fe7 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -81,6 +81,8 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing41); TEST_CASE(simplifyUsing42); TEST_CASE(simplifyUsing43); + TEST_CASE(simplifyUsing44); + TEST_CASE(simplifyUsing45); TEST_CASE(simplifyUsing8970); TEST_CASE(simplifyUsing8971); @@ -1017,6 +1019,56 @@ class TestSimplifyUsing : public TestFixture { 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 simplifyUsing8970() { const char code[] = "using V = std::vector;\n" "struct A {\n" From e129ca0376a47b23b803bc07477d23f2f092c226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 3 Dec 2026 16:33:30 +0100 Subject: [PATCH 3/4] simplifyUsing: only skip destructor names, not unary bitwise not The name after "~" was never replaced, which also affected expressions like "~U(0)" where U is a type alias. Limit the check to "~ %name% ( )". Co-Authored-By: Claude Opus 5.5 --- lib/tokenize.cpp | 2 +- test/testsimplifyusing.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f51eed51135..418184012fd 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2821,7 +2821,7 @@ namespace { } // destructor name, i.e. "~C" in "~C()" or "N::C::~C()" - if (tok1->strAt(-1) == "~") + if (Token::Match(tok1->previous(), "~ %name% ( )")) return false; // Inside a class the unqualified class name is the injected-class-name, diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 8d2fd6a8fe7..41f3d13db49 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -83,6 +83,7 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing43); TEST_CASE(simplifyUsing44); TEST_CASE(simplifyUsing45); + TEST_CASE(simplifyUsing46); TEST_CASE(simplifyUsing8970); TEST_CASE(simplifyUsing8971); @@ -1069,6 +1070,15 @@ class TestSimplifyUsing : public TestFixture { 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}; }\n"; + const char expected[] = "unsigned int f ( ) { return ~ unsigned char ( 0 ) & ~ unsigned char { 1 } ; }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout_str()); + } + void simplifyUsing8970() { const char code[] = "using V = std::vector;\n" "struct A {\n" From 7112cdf62ccf8f4ff66e8e4058a289bd93778559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 3 Dec 2026 19:06:00 +0100 Subject: [PATCH 4/4] review --- lib/tokenize.cpp | 5 +++-- test/testsimplifyusing.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 418184012fd..c97466a7fe3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2820,8 +2820,9 @@ namespace { tok2 = tok2->tokAt(-2); } - // destructor name, i.e. "~C" in "~C()" or "N::C::~C()" - if (Token::Match(tok1->previous(), "~ %name% ( )")) + // 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, diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 41f3d13db49..200371ace5c 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -1073,8 +1073,8 @@ class TestSimplifyUsing : public TestFixture { 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}; }\n"; - const char expected[] = "unsigned int f ( ) { return ~ unsigned char ( 0 ) & ~ unsigned char { 1 } ; }"; + "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()); }