[PATCH] D69517: [clangd] Add a hidden tweak to dump symbol under the curosr.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 28 09:00:09 PDT 2019


hokein created this revision.
hokein added a reviewer: ilya-biryukov.
Herald added subscribers: usaxena95, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69517

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


Index: clang-tools-extra/clangd/unittests/TweakTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -133,6 +133,15 @@
                     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; }");
Index: clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
+++ 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 @@
 };
 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; };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69517.226675.patch
Type: text/x-patch
Size: 2427 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191028/25510d86/attachment.bin>


More information about the cfe-commits mailing list