[PATCH] D96612: [clangd] Improve printing of Objective-C categories and methods

David Goldman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 12 08:59:54 PST 2021


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

- Categories will now be printed as `MyClass(Category)` instead of `Category`

- Methods will now be printed as `-selector:` or `+selector:` instead of `selector:` to differentiate between instance and class methods

This is primarily noticable for DocumentSymbols


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96612

Files:
  clang-tools-extra/clangd/AST.cpp
  clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp


Index: clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
+++ clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
@@ -848,6 +848,39 @@
                 SymRange(Main.range("forwardfunc")))));
 }
 
+TEST(DocumentSymbolsTest, ObjCCategoriesAndClassExtensions) {
+  TestTU TU;
+  TU.ExtraArgs = {"-xobjective-c++", "-Wno-objc-root-class"};
+  Annotations Main(R"cpp(
+      $Cat[[@interface Cat
+      @end]]
+      $SneakyCat[[@interface Cat (Sneaky)
+      - (id)sneak:(id)behavior;
+      @end]]
+
+      $MeowCat[[@interface Cat ()
+      - (void)meow;
+      @end]]
+      $PurCat[[@interface Cat ()
+      - (void)pur;
+      @end]]
+    )cpp");
+  TU.Code = Main.code().str();
+  EXPECT_THAT(
+      getSymbols(TU.build()),
+      ElementsAre(
+          AllOf(WithName("Cat"), SymRange(Main.range("Cat"))),
+          AllOf(WithName("Cat(Sneaky)"), SymRange(Main.range("SneakyCat")),
+                Children(
+                    AllOf(WithName("-sneak:"), WithKind(SymbolKind::Method)))),
+          AllOf(
+              WithName("Cat()"), SymRange(Main.range("MeowCat")),
+              Children(AllOf(WithName("-meow"), WithKind(SymbolKind::Method)))),
+          AllOf(WithName("Cat()"), SymRange(Main.range("PurCat")),
+                Children(
+                    AllOf(WithName("-pur"), WithKind(SymbolKind::Method))))));
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/AST.cpp
===================================================================
--- clang-tools-extra/clangd/AST.cpp
+++ clang-tools-extra/clangd/AST.cpp
@@ -221,6 +221,14 @@
     return Out.str();
   }
 
+  if (const ObjCContainerDecl *C = dyn_cast<ObjCContainerDecl>(&ND))
+    return printObjCContainer(*C);
+  if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(&ND)) {
+    Out << (Method->isInstanceMethod() ? '-' : '+');
+    Method->getSelector().print(Out);
+    return Out.str();
+  }
+
   if (isAnonymous(ND.getDeclName())) {
     // Come up with a presentation for an anonymous entity.
     if (isa<NamespaceDecl>(ND))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96612.323349.patch
Type: text/x-patch
Size: 2209 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210212/2ca66436/attachment.bin>


More information about the cfe-commits mailing list