[clang-tools-extra] 80b0cdd - [clangd] Add a hidden tweak to dump symbol under the cursor.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 29 05:06:47 PDT 2019


Author: Haojian Wu
Date: 2019-10-29T13:06:34+01:00
New Revision: 80b0cdde0ffc4ca59e99fed3dcb18412ff97568c

URL: https://github.com/llvm/llvm-project/commit/80b0cdde0ffc4ca59e99fed3dcb18412ff97568c
DIFF: https://github.com/llvm/llvm-project/commit/80b0cdde0ffc4ca59e99fed3dcb18412ff97568c.diff

LOG: [clangd] Add a hidden tweak to dump symbol under the cursor.

Summary:
This provides a convenient way to see the SymbolID/USR of the symbol, mainly
for debugging purpose.

Reviewers: ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D69517

Added: 
    

Modified: 
    clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
    clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp b/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
index 9baa8fa4ad6a..ed3725bf7ea1 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
@@ -9,6 +9,7 @@
 // Some of these are fairly clang-specific and hidden (e.g. textual AST dumps).
 // Others are more generally useful (class layout) and are exposed by default.
 //===----------------------------------------------------------------------===//
+#include "XRefs.h"
 #include "refactor/Tweak.h"
 #include "clang/AST/ASTTypeTraits.h"
 #include "clang/AST/Type.h"
@@ -94,6 +95,32 @@ class ShowSelectionTree : public Tweak {
 };
 REGISTER_TWEAK(ShowSelectionTree)
 
+/// Dumps the symbol under the cursor.
+/// Inputs:
+/// void foo();
+///      ^^^
+/// Message:
+///  foo -
+///  {"containerName":null,"id":"CA2EBE44A1D76D2A","name":"foo","usr":"c:@F at foo#"}
+class DumpSymbol : public Tweak {
+  const char *id() const override final;
+  bool prepare(const Selection &Inputs) override { return true; }
+  Expected<Effect> apply(const Selection &Inputs) override {
+    std::string Storage;
+    llvm::raw_string_ostream Out(Storage);
+
+    for (auto &Sym : getSymbolInfo(
+             Inputs.AST,
+             sourceLocToPosition(Inputs.AST.getSourceManager(), Inputs.Cursor)))
+      Out << Sym;
+    return Effect::showMessage(Out.str());
+  }
+  std::string title() const override { return "Dump symbol under the cursor"; }
+  Intent intent() const override { return Info; }
+  bool hidden() const override { return true; }
+};
+REGISTER_TWEAK(DumpSymbol)
+
 /// Shows the layout of the RecordDecl under the cursor.
 /// Input:
 /// struct X { int foo; };

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index f68dff131d9d..f14383b24081 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -133,6 +133,15 @@ TEST_F(DumpASTTest, Test) {
                     HasSubstr("<col:13> 'int' 2")));
 }
 
+TWEAK_TEST(DumpSymbol);
+TEST_F(DumpSymbolTest, Test) {
+  std::string ID = R"("id":"CA2EBE44A1D76D2A")";
+  std::string USR = R"("usr":"c:@F at foo#")";
+  EXPECT_THAT(apply("void f^oo();"),
+              AllOf(StartsWith("message:"), testing::HasSubstr(ID),
+                    testing::HasSubstr(USR)));
+}
+
 TWEAK_TEST(ShowSelectionTree);
 TEST_F(ShowSelectionTreeTest, Test) {
   EXPECT_AVAILABLE("^int f^oo() { re^turn 2 ^+ 2; }");


        


More information about the cfe-commits mailing list