[PATCH] D93029: [clangd] Find relations in Dex exploration tool.

Utkarsh Saxena via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 10 05:27:04 PST 2020


usaxena95 created this revision.
usaxena95 added a reviewer: hokein.
Herald added subscribers: kadircet, arphaman.
usaxena95 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93029

Files:
  clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp


Index: clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
===================================================================
--- clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
+++ clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
@@ -11,6 +11,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "index/Index.h"
+#include "index/Relation.h"
 #include "index/Serialization.h"
 #include "index/dex/Dex.h"
 #include "index/remote/Client.h"
@@ -267,6 +269,43 @@
   }
 };
 
+class Relations : public Command {
+  llvm::cl::opt<std::string> ID{
+      "id",
+      llvm::cl::Positional,
+      llvm::cl::desc("Symbol ID of the symbol being queried (hex)."),
+  };
+  llvm::cl::opt<RelationKind> Relation{
+      "relation",
+      llvm::cl::desc("Relation kind for the predicate."),
+      values(clEnumValN(RelationKind::BaseOf, "base_of",
+                        "Find subclasses of a class."),
+             clEnumValN(RelationKind::OverriddenBy, "overridden_by",
+                        "Find methods that overrides a virtual method.")),
+  };
+
+  void run() override {
+    if (ID.getNumOccurrences() == 0 || Relation.getNumOccurrences() == 0) {
+      llvm::errs()
+          << "Missing required argument: please provide id or -name.\n";
+      return;
+    }
+    RelationsRequest Req;
+    if (ID.getNumOccurrences()) {
+      auto SID = SymbolID::fromStr(ID);
+      if (!SID) {
+        llvm::errs() << llvm::toString(SID.takeError()) << "\n";
+        return;
+      }
+      Req.Subjects.insert(*SID);
+    }
+    Req.Predicate = Relation.getValue();
+    Index->relations(Req, [](const SymbolID &SID, const Symbol &S) {
+      llvm::outs() << S << " defined at: " << S.Definition << "\n";
+    });
+  }
+};
+
 class Export : public Command {
   llvm::cl::opt<IndexFileFormat> Format{
       "format",
@@ -326,6 +365,8 @@
     {"lookup", "Dump symbol details by ID or qualified name",
      std::make_unique<Lookup>},
     {"refs", "Find references by ID or qualified name", std::make_unique<Refs>},
+    {"relations", "Find relations by ID and relation kind",
+     std::make_unique<Relations>},
     {"export", "Export index", std::make_unique<Export>},
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93029.310859.patch
Type: text/x-patch
Size: 2222 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201210/8d12577d/attachment.bin>


More information about the cfe-commits mailing list