Skip to content

Commit 9e83d0b

Browse files
committed
[clangd] Mention when CXXThis is implicit in exposed AST.
Seeing an implicit this in the AST is pretty confusing I think. While here, also mention when `this` is const. Differential Revision: https://reviews.llvm.org/D91868
1 parent 5ce85e6 commit 9e83d0b

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

clang-tools-extra/clangd/DumpAST.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
234234
return UnaryOperator::getOpcodeStr(UO->getOpcode()).str();
235235
if (const auto *CCO = dyn_cast<CXXConstructExpr>(S))
236236
return CCO->getConstructor()->getNameAsString();
237+
if (const auto *CTE = dyn_cast<CXXThisExpr>(S)) {
238+
bool Const = CTE->getType()->getPointeeType().isLocalConstQualified();
239+
if (CTE->isImplicit())
240+
return Const ? "const, implicit" : "implicit";
241+
if (Const)
242+
return "const";
243+
return "";
244+
}
237245
if (isa<IntegerLiteral>(S) || isa<FloatingLiteral>(S) ||
238246
isa<FixedPointLiteral>(S) || isa<CharacterLiteral>(S) ||
239247
isa<ImaginaryLiteral>(S) || isa<CXXBoolLiteralExpr>(S))

clang-tools-extra/clangd/unittests/DumpASTTests.cpp

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,32 @@ declaration: Namespace - root
7676
type: Record - S
7777
)"},
7878
{R"cpp(
79-
template <typename T> int root() {
80-
(void)root<unsigned>();
79+
namespace root {
80+
template <typename T> int tmpl() {
81+
(void)tmpl<unsigned>();
8182
return T::value;
83+
}
8284
}
8385
)cpp",
8486
R"(
85-
declaration: FunctionTemplate - root
86-
declaration: TemplateTypeParm - T
87-
declaration: Function - root
88-
type: FunctionProto
89-
type: Builtin - int
90-
statement: Compound
91-
expression: CStyleCast - ToVoid
92-
type: Builtin - void
93-
expression: Call
94-
expression: ImplicitCast - FunctionToPointerDecay
95-
expression: DeclRef - root
96-
template argument: Type
97-
type: Builtin - unsigned int
98-
statement: Return
99-
expression: DependentScopeDeclRef - value
100-
specifier: TypeSpec
101-
type: TemplateTypeParm - T
87+
declaration: Namespace - root
88+
declaration: FunctionTemplate - tmpl
89+
declaration: TemplateTypeParm - T
90+
declaration: Function - tmpl
91+
type: FunctionProto
92+
type: Builtin - int
93+
statement: Compound
94+
expression: CStyleCast - ToVoid
95+
type: Builtin - void
96+
expression: Call
97+
expression: ImplicitCast - FunctionToPointerDecay
98+
expression: DeclRef - tmpl
99+
template argument: Type
100+
type: Builtin - unsigned int
101+
statement: Return
102+
expression: DependentScopeDeclRef - value
103+
specifier: TypeSpec
104+
type: TemplateTypeParm - T
102105
)"},
103106
{R"cpp(
104107
struct Foo { char operator+(int); };
@@ -116,10 +119,28 @@ declaration: Var - root
116119
type: Record - Foo
117120
expression: IntegerLiteral - 42
118121
)"},
122+
{R"cpp(
123+
struct Bar {
124+
int x;
125+
int root() const {
126+
return x;
127+
}
128+
};
129+
)cpp",
130+
R"(
131+
declaration: CXXMethod - root
132+
type: FunctionProto
133+
type: Builtin - int
134+
statement: Compound
135+
statement: Return
136+
expression: ImplicitCast - LValueToRValue
137+
expression: Member - x
138+
expression: CXXThis - const, implicit
139+
)"},
119140
};
120141
for (const auto &Case : Cases) {
121142
ParsedAST AST = TestTU::withCode(Case.first).build();
122-
auto Node = dumpAST(DynTypedNode::create(findDecl(AST, "root")),
143+
auto Node = dumpAST(DynTypedNode::create(findUnqualifiedDecl(AST, "root")),
123144
AST.getTokens(), AST.getASTContext());
124145
EXPECT_EQ(llvm::StringRef(Case.second).trim(),
125146
llvm::StringRef(llvm::to_string(Node)).trim());

0 commit comments

Comments
 (0)