[clang-tools-extra] [clang] [clangd] Add support to rename Objective-C selectors (PR #78872)

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 20 18:03:01 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff b9a1e2ab8dead4863834f70cdae56104ec92d041 048d6f062e8a683af8ebcecf85b5c50bfb02454c -- clang/lib/Tooling/Refactoring/SymbolName.cpp clang-tools-extra/clangd/ClangdLSPServer.cpp clang-tools-extra/clangd/ClangdLSPServer.h clang-tools-extra/clangd/ClangdServer.cpp clang-tools-extra/clangd/Protocol.cpp clang-tools-extra/clangd/Protocol.h clang-tools-extra/clangd/SourceCode.cpp clang-tools-extra/clangd/SourceCode.h clang-tools-extra/clangd/index/SymbolCollector.cpp clang-tools-extra/clangd/refactor/Rename.cpp clang-tools-extra/clangd/refactor/Rename.h clang-tools-extra/clangd/unittests/RenameTests.cpp clang-tools-extra/clangd/unittests/SourceCodeTests.cpp clang/include/clang/Tooling/Refactoring/Rename/RenamingAction.h clang/include/clang/Tooling/Refactoring/Rename/SymbolName.h clang/include/clang/Tooling/Syntax/Tokens.h clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp clang/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp clang/lib/Tooling/Syntax/Tokens.cpp clang/unittests/Tooling/RefactoringActionRulesTest.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang-tools-extra/clangd/unittests/RenameTests.cpp b/clang-tools-extra/clangd/unittests/RenameTests.cpp
index ff64e17645..9bf3b94120 100644
--- a/clang-tools-extra/clangd/unittests/RenameTests.cpp
+++ b/clang-tools-extra/clangd/unittests/RenameTests.cpp
@@ -887,11 +887,10 @@ TEST(RenameTest, ObjCWithinFileRename) {
     /// fail.
     std::optional<llvm::StringRef> Expected;
   };
-  TestCase Tests[] = {
-    // Simple rename
-    {
-      // Input
-      R"cpp(
+  TestCase Tests[] = {// Simple rename
+                      {
+                          // Input
+                          R"cpp(
         @interface Foo
         - (int)performA^ction:(int)action w^ith:(int)value;
         @end
@@ -901,10 +900,10 @@ TEST(RenameTest, ObjCWithinFileRename) {
         }
         @end
       )cpp",
-      // New name
-      "performNewAction:by:",
-      // Expected
-      R"cpp(
+                          // New name
+                          "performNewAction:by:",
+                          // Expected
+                          R"cpp(
         @interface Foo
         - (int)performNewAction:(int)action by:(int)value;
         @end
@@ -914,11 +913,11 @@ TEST(RenameTest, ObjCWithinFileRename) {
         }
         @end
       )cpp",
-    },
-    // Rename selector with macro
-    {
-      // Input
-      R"cpp(
+                      },
+                      // Rename selector with macro
+                      {
+                          // Input
+                          R"cpp(
         #define mySelector - (int)performAction:(int)action with:(int)value
         @interface Foo
         ^mySelector;
@@ -929,15 +928,15 @@ TEST(RenameTest, ObjCWithinFileRename) {
         }
         @end
       )cpp",
-      // New name
-      "performNewAction:by:",
-      // Expected error
-      std::nullopt,
-    },
-    // Rename selector in macro definition
-    {
-      // Input
-      R"cpp(
+                          // New name
+                          "performNewAction:by:",
+                          // Expected error
+                          std::nullopt,
+                      },
+                      // Rename selector in macro definition
+                      {
+                          // Input
+                          R"cpp(
         #define mySelector - (int)perform^Action:(int)action with:(int)value
         @interface Foo
         mySelector;
@@ -948,18 +947,20 @@ TEST(RenameTest, ObjCWithinFileRename) {
         }
         @end
       )cpp",
-      // New name
-      "performNewAction:by:",
-      // Expected error
-      std::nullopt,
-    },
-    // Don't rename `@selector`
-    // `@selector` is not tied to a single selector. Eg. there might be multiple
-    // classes in the codebase that implement that selector. It's thus more like
-    // a string literal and we shouldn't rename it.
-    {
-      // Input
-      R"cpp(
+                          // New name
+                          "performNewAction:by:",
+                          // Expected error
+                          std::nullopt,
+                      },
+                      // Don't rename `@selector`
+                      // `@selector` is not tied to a single selector. Eg. there
+                      // might be multiple
+                      // classes in the codebase that implement that selector.
+                      // It's thus more like
+                      // a string literal and we shouldn't rename it.
+                      {
+                          // Input
+                          R"cpp(
         @interface Foo
         - (void)performA^ction:(int)action with:(int)value;
         @end
@@ -969,10 +970,10 @@ TEST(RenameTest, ObjCWithinFileRename) {
         }
         @end
       )cpp",
-      // New name
-      "performNewAction:by:",
-      // Expected
-      R"cpp(
+                          // New name
+                          "performNewAction:by:",
+                          // Expected
+                          R"cpp(
         @interface Foo
         - (void)performNewAction:(int)action by:(int)value;
         @end
@@ -982,11 +983,11 @@ TEST(RenameTest, ObjCWithinFileRename) {
         }
         @end
       )cpp",
-    },
-    // Fail if rename initiated inside @selector
-    {
-      // Input
-      R"cpp(
+                      },
+                      // Fail if rename initiated inside @selector
+                      {
+                          // Input
+                          R"cpp(
         @interface Foo
         - (void)performAction:(int)action with:(int)value;
         @end
@@ -996,12 +997,11 @@ TEST(RenameTest, ObjCWithinFileRename) {
         }
         @end
       )cpp",
-      // New name
-      "performNewAction:by:",
-      // Expected
-      std::nullopt,
-    }
-  };
+                          // New name
+                          "performNewAction:by:",
+                          // Expected
+                          std::nullopt,
+                      }};
   for (TestCase T : Tests) {
     SCOPED_TRACE(T.Input);
     Annotations Code(T.Input);
@@ -1017,8 +1017,8 @@ TEST(RenameTest, ObjCWithinFileRename) {
         ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError();
         ASSERT_EQ(1u, RenameResult->GlobalChanges.size());
         EXPECT_EQ(
-                  applyEdits(std::move(RenameResult->GlobalChanges)).front().second,
-                  *Expected);
+            applyEdits(std::move(RenameResult->GlobalChanges)).front().second,
+            *Expected);
       } else {
         ASSERT_FALSE(bool(RenameResult));
         consumeError(RenameResult.takeError());
@@ -1954,101 +1954,99 @@ TEST(CrossFileRenameTests, ObjC) {
     llvm::StringRef ExpectedFooH;
     llvm::StringRef ExpectedFooM;
   };
-  Case Cases[] = {
-    // --- Zero arg selector
-    {
-      // Input
-      R"cpp(
+  Case Cases[] = {// --- Zero arg selector
+                  {
+                      // Input
+                      R"cpp(
         @interface Foo
         - (int)performA^ction;
         @end
       )cpp",
-      R"cpp(
+                      R"cpp(
         @implementation Foo
         - (int)performAction {
           [self performAction];
         }
         @end
       )cpp",
-      // New name
-      "performNewAction",
-      // Expected
-      R"cpp(
+                      // New name
+                      "performNewAction",
+                      // Expected
+                      R"cpp(
         @interface Foo
         - (int)performNewAction;
         @end
       )cpp",
-      R"cpp(
+                      R"cpp(
         @implementation Foo
         - (int)performNewAction {
           [self performNewAction];
         }
         @end
       )cpp",
-    },
-    // --- Single arg selector
-    {
-      // Input
-      R"cpp(
+                  },
+                  // --- Single arg selector
+                  {
+                      // Input
+                      R"cpp(
         @interface Foo
         - (int)performA^ction:(int)action;
         @end
       )cpp",
-      R"cpp(
+                      R"cpp(
         @implementation Foo
         - (int)performAction:(int)action {
           [self performAction:action];
         }
         @end
       )cpp",
-      // New name
-      "performNewAction:",
-      // Expected
-      R"cpp(
+                      // New name
+                      "performNewAction:",
+                      // Expected
+                      R"cpp(
         @interface Foo
         - (int)performNewAction:(int)action;
         @end
       )cpp",
-      R"cpp(
+                      R"cpp(
         @implementation Foo
         - (int)performNewAction:(int)action {
           [self performNewAction:action];
         }
         @end
       )cpp",
-    },
-    // --- Multi arg selector
-    {
-      // Input
-      R"cpp(
+                  },
+                  // --- Multi arg selector
+                  {
+                      // Input
+                      R"cpp(
         @interface Foo
         - (int)performA^ction:(int)action with:(int)value;
         @end
       )cpp",
-      R"cpp(
+                      R"cpp(
         @implementation Foo
         - (int)performAction:(int)action with:(int)value {
           [self performAction:action with:value];
         }
         @end
       )cpp",
-      // New name
-      "performNewAction:by:",
-      // Expected
-      R"cpp(
+                      // New name
+                      "performNewAction:by:",
+                      // Expected
+                      R"cpp(
         @interface Foo
         - (int)performNewAction:(int)action by:(int)value;
         @end
       )cpp",
-      R"cpp(
+                      R"cpp(
         @implementation Foo
         - (int)performNewAction:(int)action by:(int)value {
           [self performNewAction:action by:value];
         }
         @end
       )cpp",
-    }
-  };
+                  }};
 
   trace::TestTracer Tracer;
   for (const auto &T : Cases) {
diff --git a/clang/include/clang/Tooling/Refactoring/Rename/RenamingAction.h b/clang/include/clang/Tooling/Refactoring/Rename/RenamingAction.h
index a1f0a7c03e..e6b38c40c8 100644
--- a/clang/include/clang/Tooling/Refactoring/Rename/RenamingAction.h
+++ b/clang/include/clang/Tooling/Refactoring/Rename/RenamingAction.h
@@ -120,7 +120,7 @@ private:
 enum class ObjCSymbolSelectorKind {
   /// The rename location is an Objective-C method call, eg. `[self add: 1]`.
   MessageSend,
-  
+
   /// The rename location is an Objective-C method definition, eg.
   /// ` - (void)add:(int)theValue`
   MethodDecl,

``````````

</details>


https://github.com/llvm/llvm-project/pull/78872


More information about the cfe-commits mailing list