Skip to content

Commit 0e33195

Browse files
authored
[clang] fix regression printing constructor/destructor names (#155688)
This makes the type printer not qualify constructor and destructor names. These are represented as canonical types and the type printer is used, but unlike canonical types which we normally print as fully qualified, the expected behaviour for declaration names is for them to be unqualified. Note that this restores the behaviour pre #147835, but that is still broken for the constructor names of class templates, since in that case the injected class name type is used, but here the type printer is configured to also print the implicit template arguments. No release notes since this regression was never released. Fixes #155537
1 parent 7454e1f commit 0e33195

File tree

10 files changed

+37
-20
lines changed

10 files changed

+37
-20
lines changed

clang/lib/AST/DeclarationName.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static void printCXXConstructorDestructorName(QualType ClassType,
113113
PrintingPolicy Policy) {
114114
// We know we're printing C++ here. Ensure we print types properly.
115115
Policy.adjustForCPlusPlus();
116+
Policy.SuppressScope = true;
116117

117118
if (const RecordType *ClassRec = ClassType->getAsCanonical<RecordType>()) {
118119
ClassRec->getOriginalDecl()->printName(OS, Policy);

clang/test/AST/HLSL/StructuredBuffers-AST.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ RESOURCE<float> Buffer;
9191

9292
// Default constructor
9393

94-
// CHECK: CXXConstructorDecl {{.*}} hlsl::[[RESOURCE]]<element_type> 'void ()' inline
94+
// CHECK: CXXConstructorDecl {{.*}} [[RESOURCE]]<element_type> 'void ()' inline
9595
// CHECK-NEXT: CompoundStmt
9696
// CHECK-NEXT: BinaryOperator {{.*}} '='
9797
// CHECK-NEXT: MemberExpr {{.*}} lvalue .__handle
@@ -105,7 +105,7 @@ RESOURCE<float> Buffer;
105105

106106
// Constructor from binding
107107

108-
// CHECK: CXXConstructorDecl {{.*}} hlsl::[[RESOURCE]]<element_type> 'void (unsigned int, unsigned int, int, unsigned int, const char *)' inline
108+
// CHECK: CXXConstructorDecl {{.*}} [[RESOURCE]]<element_type> 'void (unsigned int, unsigned int, int, unsigned int, const char *)' inline
109109
// CHECK-NEXT: ParmVarDecl {{.*}} registerNo 'unsigned int'
110110
// CHECK-NEXT: ParmVarDecl {{.*}} spaceNo 'unsigned int'
111111
// CHECK-NEXT: ParmVarDecl {{.*}} range 'int'
@@ -129,7 +129,7 @@ RESOURCE<float> Buffer;
129129

130130
// Constructor from implicit binding
131131

132-
// CHECK: CXXConstructorDecl {{.*}} hlsl::[[RESOURCE]]<element_type> 'void (unsigned int, int, unsigned int, unsigned int, const char *)' inline
132+
// CHECK: CXXConstructorDecl {{.*}} [[RESOURCE]]<element_type> 'void (unsigned int, int, unsigned int, unsigned int, const char *)' inline
133133
// CHECK-NEXT: ParmVarDecl {{.*}} spaceNo 'unsigned int'
134134
// CHECK-NEXT: ParmVarDecl {{.*}} range 'int'
135135
// CHECK-NEXT: ParmVarDecl {{.*}} index 'unsigned int'

clang/test/AST/HLSL/TypedBuffers-AST.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ RESOURCE<float> Buffer;
6666

6767
// Default constructor
6868

69-
// CHECK: CXXConstructorDecl {{.*}} hlsl::[[RESOURCE]]<element_type> 'void ()' inline
69+
// CHECK: CXXConstructorDecl {{.*}} [[RESOURCE]]<element_type> 'void ()' inline
7070
// CHECK-NEXT: CompoundStmt
7171
// CHECK-NEXT: BinaryOperator {{.*}} '='
7272
// CHECK-NEXT: MemberExpr {{.*}} lvalue .__handle
@@ -80,7 +80,7 @@ RESOURCE<float> Buffer;
8080

8181
// Constructor from binding
8282

83-
// CHECK: CXXConstructorDecl {{.*}} hlsl::[[RESOURCE]]<element_type> 'void (unsigned int, unsigned int, int, unsigned int, const char *)' inline
83+
// CHECK: CXXConstructorDecl {{.*}} [[RESOURCE]]<element_type> 'void (unsigned int, unsigned int, int, unsigned int, const char *)' inline
8484
// CHECK-NEXT: ParmVarDecl {{.*}} registerNo 'unsigned int'
8585
// CHECK-NEXT: ParmVarDecl {{.*}} spaceNo 'unsigned int'
8686
// CHECK-NEXT: ParmVarDecl {{.*}} range 'int'
@@ -104,7 +104,7 @@ RESOURCE<float> Buffer;
104104

105105
// Constructor from implicit binding
106106

107-
// CHECK: CXXConstructorDecl {{.*}} hlsl::[[RESOURCE]]<element_type> 'void (unsigned int, int, unsigned int, unsigned int, const char *)' inline
107+
// CHECK: CXXConstructorDecl {{.*}} [[RESOURCE]]<element_type> 'void (unsigned int, int, unsigned int, unsigned int, const char *)' inline
108108
// CHECK-NEXT: ParmVarDecl {{.*}} spaceNo 'unsigned int'
109109
// CHECK-NEXT: ParmVarDecl {{.*}} range 'int'
110110
// CHECK-NEXT: ParmVarDecl {{.*}} index 'unsigned int'

clang/test/AST/ast-dump-decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ namespace testClassTemplateDecl {
330330
// CHECK-NEXT: | | `-Destructor irrelevant non_trivial user_declared{{$}}
331331
// CHECK-NEXT: | |-CXXRecordDecl 0x{{.+}} <col:24, col:30> col:30 implicit referenced class TestClassTemplate{{$}}
332332
// CHECK-NEXT: | |-AccessSpecDecl 0x{{.+}} <line:[[@LINE-50]]:3, col:9> col:3 public{{$}}
333-
// CHECK-NEXT: | |-CXXConstructorDecl 0x[[#%x,TEMPLATE_CONSTRUCTOR_DECL:]] <line:[[@LINE-50]]:5, col:23> col:5 testClassTemplateDecl::TestClassTemplate<T> 'void ()'{{$}}
334-
// CHECK-NEXT: | |-CXXDestructorDecl 0x[[#%x,TEMPLATE_DESTRUCTOR_DECL:]] <line:[[@LINE-50]]:5, col:24> col:5 ~testClassTemplateDecl::TestClassTemplate<T> 'void ()' not_selected{{$}}
333+
// CHECK-NEXT: | |-CXXConstructorDecl 0x[[#%x,TEMPLATE_CONSTRUCTOR_DECL:]] <line:[[@LINE-50]]:5, col:23> col:5 TestClassTemplate<T> 'void ()'{{$}}
334+
// CHECK-NEXT: | |-CXXDestructorDecl 0x[[#%x,TEMPLATE_DESTRUCTOR_DECL:]] <line:[[@LINE-50]]:5, col:24> col:5 ~TestClassTemplate<T> 'void ()' not_selected{{$}}
335335
// CHECK-NEXT: | |-CXXMethodDecl 0x[[#%x,TEMPLATE_METHOD_DECL:]] <line:[[@LINE-50]]:5, col:11> col:9 j 'int ()'{{$}}
336336
// CHECK-NEXT: | `-FieldDecl 0x{{.+}} <line:[[@LINE-50]]:5, col:9> col:9 i 'int'{{$}}
337337
// CHECK-NEXT: |-ClassTemplateSpecializationDecl 0x{{.+}} <line:[[@LINE-56]]:3, line:[[@LINE-50]]:3> line:[[@LINE-56]]:30 class TestClassTemplate definition implicit_instantiation{{$}}

clang/test/AST/ast-dump-templates.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8170,7 +8170,7 @@ namespace GH153540 {
81708170
// JSON-NEXT: "tokLen": 1
81718171
// JSON-NEXT: }
81728172
// JSON-NEXT: },
8173-
// JSON-NEXT: "name": "GH153540::N::S<T>",
8173+
// JSON-NEXT: "name": "S<T>",
81748174
// JSON-NEXT: "type": {
81758175
// JSON-NEXT: "qualType": "void (T)"
81768176
// JSON-NEXT: },

clang/test/CXX/drs/cwg6xx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ namespace cwg635 { // cwg635: 2.7
383383
template<typename T> template<typename U> D<T>::D() {}
384384
template<typename T> D<T>::D<T>() {} // #cwg635-D-T
385385
// expected-error@#cwg635-D-T {{out-of-line constructor for 'D' cannot have template arguments}}
386-
// expected-error@#cwg635-D-T {{redefinition of 'cwg635::D<T>'}}
386+
// expected-error@#cwg635-D-T {{redefinition of 'D<T>'}}
387387
// expected-note@#cwg635-D {{previous definition is here}}
388388
} // namespace cwg635
389389

clang/test/Index/recursive-cxx-member-calls.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -823,18 +823,18 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo * Name) {
823823
// CHECK-tokens: Punctuation: ";" [85:18 - 85:19] ClassTemplate=StringSwitch:83:47 (Definition)
824824
// CHECK-tokens: Keyword: "public" [86:1 - 86:7] CXXAccessSpecifier=:86:1 (Definition)
825825
// CHECK-tokens: Punctuation: ":" [86:7 - 86:8] CXXAccessSpecifier=:86:1 (Definition)
826-
// CHECK-tokens: Keyword: "explicit" [87:3 - 87:11] CXXConstructor=llvm::StringSwitch<T, R>:87:12 (Definition)
827-
// CHECK-tokens: Identifier: "StringSwitch" [87:12 - 87:24] CXXConstructor=llvm::StringSwitch<T, R>:87:12 (Definition) (explicit)
828-
// CHECK-tokens: Punctuation: "(" [87:24 - 87:25] CXXConstructor=llvm::StringSwitch<T, R>:87:12 (Definition)
826+
// CHECK-tokens: Keyword: "explicit" [87:3 - 87:11] CXXConstructor=StringSwitch<T, R>:87:12 (Definition)
827+
// CHECK-tokens: Identifier: "StringSwitch" [87:12 - 87:24] CXXConstructor=StringSwitch<T, R>:87:12 (Definition) (explicit)
828+
// CHECK-tokens: Punctuation: "(" [87:24 - 87:25] CXXConstructor=StringSwitch<T, R>:87:12 (Definition)
829829
// CHECK-tokens: Identifier: "StringRef" [87:25 - 87:34] TypeRef=class llvm::StringRef:38:7
830830
// CHECK-tokens: Identifier: "Str" [87:35 - 87:38] ParmDecl=Str:87:35 (Definition)
831-
// CHECK-tokens: Punctuation: ")" [87:38 - 87:39] CXXConstructor=llvm::StringSwitch<T, R>:87:12 (Definition)
832-
// CHECK-tokens: Punctuation: ":" [87:40 - 87:41] CXXConstructor=llvm::StringSwitch<T, R>:87:12 (Definition)
831+
// CHECK-tokens: Punctuation: ")" [87:38 - 87:39] CXXConstructor=StringSwitch<T, R>:87:12 (Definition)
832+
// CHECK-tokens: Punctuation: ":" [87:40 - 87:41] CXXConstructor=StringSwitch<T, R>:87:12 (Definition)
833833
// CHECK-tokens: Identifier: "Str" [87:42 - 87:45] MemberRef=Str:84:13
834834
// CHECK-tokens: Punctuation: "(" [87:45 - 87:46] CallExpr=StringRef:38:7
835835
// CHECK-tokens: Identifier: "Str" [87:46 - 87:49] DeclRefExpr=Str:87:35
836836
// CHECK-tokens: Punctuation: ")" [87:49 - 87:50] CallExpr=StringRef:38:7
837-
// CHECK-tokens: Punctuation: "," [87:50 - 87:51] CXXConstructor=llvm::StringSwitch<T, R>:87:12 (Definition)
837+
// CHECK-tokens: Punctuation: "," [87:50 - 87:51] CXXConstructor=StringSwitch<T, R>:87:12 (Definition)
838838
// CHECK-tokens: Identifier: "Result" [87:52 - 87:58] MemberRef=Result:85:12
839839
// CHECK-tokens: Punctuation: "(" [87:58 - 87:59] UnexposedExpr=
840840
// CHECK-tokens: Literal: "0" [87:59 - 87:60] IntegerLiteral=
@@ -1839,7 +1839,7 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo * Name) {
18391839
// CHECK: 84:3: TypeRef=class llvm::StringRef:38:7 Extent=[84:3 - 84:12]
18401840
// CHECK: 85:12: FieldDecl=Result:85:12 (Definition) Extent=[85:3 - 85:18]
18411841
// CHECK: 86:1: CXXAccessSpecifier=:86:1 (Definition) Extent=[86:1 - 86:8]
1842-
// CHECK: 87:12: CXXConstructor=llvm::StringSwitch<T, R>:87:12 (Definition) (explicit) Extent=[87:3 - 87:64]
1842+
// CHECK: 87:12: CXXConstructor=StringSwitch<T, R>:87:12 (Definition) (explicit) Extent=[87:3 - 87:64]
18431843
// CHECK: 87:35: ParmDecl=Str:87:35 (Definition) Extent=[87:25 - 87:38]
18441844
// CHECK: 87:25: TypeRef=class llvm::StringRef:38:7 Extent=[87:25 - 87:34]
18451845
// CHECK: 87:42: MemberRef=Str:84:13 Extent=[87:42 - 87:45]

clang/test/PCH/cxx-explicit-specifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ B<false> b_false;
8585
//expected-note@-8+ {{explicit conversion function is not a candidate (explicit specifier}}
8686
//expected-note@-11 {{explicit constructor is not a candidate (explicit specifier}}
8787

88-
//CHECK: explicit(b){{ +}}templ::A<b>(B<b>)
88+
//CHECK: explicit(b){{ +}}A
8989
//CHECK: explicit(b{{ +}}^{{ +}}T::value){{ +}}operator
9090

9191
A a = { b_true }; //expected-error {{class template argument deduction}}

clang/test/SemaCXX/return.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ namespace ctor_returns_void {
115115
};
116116

117117
template <typename T> struct ST {
118-
ST() { return f(); } // expected-error {{constructor 'ctor_returns_void::ST<T>' must not return void expression}}
118+
ST() { return f(); } // expected-error {{constructor 'ST<T>' must not return void expression}}
119119
// expected-error@-1 {{constructor 'ST' must not return void expression}}
120-
~ST() { return f(); } // expected-error {{destructor '~ctor_returns_void::ST<T>' must not return void expression}}
120+
~ST() { return f(); } // expected-error {{destructor '~ST<T>' must not return void expression}}
121121
// expected-error@-1 {{destructor '~ST' must not return void expression}}
122122
};
123123

clang/unittests/AST/DeclTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,19 @@ void instantiate_template() {
570570
EXPECT_EQ(GetNameInfoRange(Matches[1]), "<input.cc:6:14, col:15>");
571571
EXPECT_EQ(GetNameInfoRange(Matches[2]), "<input.cc:6:14, col:15>");
572572
}
573+
574+
TEST(Decl, getQualifiedNameAsString) {
575+
llvm::Annotations Code(R"cpp(
576+
namespace x::y {
577+
template <class T> class Foo { Foo() {} };
578+
}
579+
)cpp");
580+
581+
auto AST = tooling::buildASTFromCode(Code.code());
582+
ASTContext &Ctx = AST->getASTContext();
583+
584+
auto const *FD = selectFirst<CXXConstructorDecl>(
585+
"ctor", match(cxxConstructorDecl().bind("ctor"), Ctx));
586+
ASSERT_NE(FD, nullptr);
587+
ASSERT_EQ(FD->getQualifiedNameAsString(), "x::y::Foo::Foo<T>");
588+
}

0 commit comments

Comments
 (0)