[clang-tools-extra] Add support for renaming objc methods, even those with multiple selector pieces (PR #76466)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 24 05:04:07 PST 2024


================
@@ -778,12 +868,44 @@ llvm::Expected<RenameResult> rename(const RenameInputs &RInputs) {
     return makeError(ReasonToReject::NoSymbolFound);
   if (DeclsUnderCursor.size() > 1)
     return makeError(ReasonToReject::AmbiguousSymbol);
+  std::string Placeholder;
+  // We expect the token under the cursor to be changed unless the user is
+  // renaming an Objective-C selector with multiple pieces and only renames
+  // some of the selector piece(s).
+  bool RenamingCurToken = true;
   const auto &RenameDecl = **DeclsUnderCursor.begin();
-  const auto *ID = RenameDecl.getIdentifier();
-  if (!ID)
-    return makeError(ReasonToReject::UnsupportedSymbol);
-  if (ID->getName() == RInputs.NewName)
-    return makeError(ReasonToReject::SameName);
+  if (const auto *MD = dyn_cast<ObjCMethodDecl>(&RenameDecl)) {
+    const auto Sel = MD->getSelector();
+    if (Sel.getAsString() == RInputs.NewName)
+      return makeError(ReasonToReject::SameName);
+    if (Sel.getNumArgs() != RInputs.NewName.count(':') &&
+        RInputs.NewName != "__clangd_rename_placeholder")
+      return makeError(
+          InvalidName{InvalidName::BadIdentifier, RInputs.NewName.str()});
+    if (Sel.getNumArgs() > 1)
+      Placeholder = Sel.getAsString();
+
+    // See if the token under the cursor should actually be renamed.
+    if (RInputs.NewName != "__clangd_rename_placeholder") {
+      llvm::StringRef NewName = RInputs.NewName;
+      llvm::SmallVector<llvm::StringRef, 8> NewNames;
+      NewName.split(NewNames, ":");
+
+      unsigned NumSelectorLocs = MD->getNumSelectorLocs();
+      for (unsigned I = 0; I < NumSelectorLocs; ++I) {
+        if (MD->getSelectorLoc(I) == IdentifierToken->location()) {
+          RenamingCurToken = Sel.getNameForSlot(I) != NewNames[I];
----------------
kadircet wrote:

i don't think it's worth all this trouble to leave things in a semi-working state. we might rely on `CurrentIdentifier` or `IdentifierRange` in other places, and until we change this logic to actually amend the identifer, it's easier to assume that it'll always be invalid for objc.

let's leave a TODO to figure out the relevant selector range using `ObjCMessageExpr` under the cursor and change the safety check to not happen for ObjCMethodDecls for now?

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


More information about the cfe-commits mailing list