[clang-tools-extra] 9e83d0b - [clangd] Mention when CXXThis is implicit in exposed AST.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 07:58:04 PST 2020


Author: Sam McCall
Date: 2020-11-24T16:57:56+01:00
New Revision: 9e83d0bcdfe86fd13f2817c9f40c5ca0b08f1443

URL: https://github.com/llvm/llvm-project/commit/9e83d0bcdfe86fd13f2817c9f40c5ca0b08f1443
DIFF: https://github.com/llvm/llvm-project/commit/9e83d0bcdfe86fd13f2817c9f40c5ca0b08f1443.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clangd/DumpAST.cpp
    clang-tools-extra/clangd/unittests/DumpASTTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/DumpAST.cpp b/clang-tools-extra/clangd/DumpAST.cpp
index 9ea17e876de7..12698b42ef3e 100644
--- a/clang-tools-extra/clangd/DumpAST.cpp
+++ b/clang-tools-extra/clangd/DumpAST.cpp
@@ -234,6 +234,14 @@ class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
       return UnaryOperator::getOpcodeStr(UO->getOpcode()).str();
     if (const auto *CCO = dyn_cast<CXXConstructExpr>(S))
       return CCO->getConstructor()->getNameAsString();
+    if (const auto *CTE = dyn_cast<CXXThisExpr>(S)) {
+      bool Const = CTE->getType()->getPointeeType().isLocalConstQualified();
+      if (CTE->isImplicit())
+        return Const ? "const, implicit" : "implicit";
+      if (Const)
+        return "const";
+      return "";
+    }
     if (isa<IntegerLiteral>(S) || isa<FloatingLiteral>(S) ||
         isa<FixedPointLiteral>(S) || isa<CharacterLiteral>(S) ||
         isa<ImaginaryLiteral>(S) || isa<CXXBoolLiteralExpr>(S))

diff  --git a/clang-tools-extra/clangd/unittests/DumpASTTests.cpp b/clang-tools-extra/clangd/unittests/DumpASTTests.cpp
index 4b524820cb4c..23c673284f66 100644
--- a/clang-tools-extra/clangd/unittests/DumpASTTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DumpASTTests.cpp
@@ -76,29 +76,32 @@ declaration: Namespace - root
                   type: Record - S
       )"},
       {R"cpp(
-template <typename T> int root() {
-  (void)root<unsigned>();
+namespace root {
+template <typename T> int tmpl() {
+  (void)tmpl<unsigned>();
   return T::value;
+}
 }
       )cpp",
        R"(
-declaration: FunctionTemplate - root
-  declaration: TemplateTypeParm - T
-  declaration: Function - root
-    type: FunctionProto
-      type: Builtin - int
-    statement: Compound
-      expression: CStyleCast - ToVoid
-        type: Builtin - void
-        expression: Call
-          expression: ImplicitCast - FunctionToPointerDecay
-            expression: DeclRef - root
-              template argument: Type
-                type: Builtin - unsigned int
-      statement: Return
-        expression: DependentScopeDeclRef - value
-          specifier: TypeSpec
-            type: TemplateTypeParm - T
+declaration: Namespace - root
+  declaration: FunctionTemplate - tmpl
+    declaration: TemplateTypeParm - T
+    declaration: Function - tmpl
+      type: FunctionProto
+        type: Builtin - int
+      statement: Compound
+        expression: CStyleCast - ToVoid
+          type: Builtin - void
+          expression: Call
+            expression: ImplicitCast - FunctionToPointerDecay
+              expression: DeclRef - tmpl
+                template argument: Type
+                  type: Builtin - unsigned int
+        statement: Return
+          expression: DependentScopeDeclRef - value
+            specifier: TypeSpec
+              type: TemplateTypeParm - T
       )"},
       {R"cpp(
 struct Foo { char operator+(int); };
@@ -116,10 +119,28 @@ declaration: Var - root
           type: Record - Foo
       expression: IntegerLiteral - 42
       )"},
+      {R"cpp(
+struct Bar {
+  int x;
+  int root() const {
+    return x;
+  }
+};
+      )cpp",
+       R"(
+declaration: CXXMethod - root
+  type: FunctionProto
+    type: Builtin - int
+  statement: Compound
+    statement: Return
+      expression: ImplicitCast - LValueToRValue
+        expression: Member - x
+          expression: CXXThis - const, implicit
+      )"},
   };
   for (const auto &Case : Cases) {
     ParsedAST AST = TestTU::withCode(Case.first).build();
-    auto Node = dumpAST(DynTypedNode::create(findDecl(AST, "root")),
+    auto Node = dumpAST(DynTypedNode::create(findUnqualifiedDecl(AST, "root")),
                         AST.getTokens(), AST.getASTContext());
     EXPECT_EQ(llvm::StringRef(Case.second).trim(),
               llvm::StringRef(llvm::to_string(Node)).trim());


        


More information about the cfe-commits mailing list