================ @@ -543,6 +550,45 @@ std::optional<InvalidName> checkName(const NamedDecl &RenameDecl, return Result; } +clangd::Range tokenRangeForLoc(ParsedAST &AST, SourceLocation TokLoc, + const SourceManager &SM, + const LangOptions &LangOpts) { + const auto *Token = AST.getTokens().spelledTokenAt(TokLoc); + assert(Token && "got inclusion at wrong offset"); + clangd::Range Result; + Result.start = sourceLocToPosition(SM, Token->location()); + Result.end = sourceLocToPosition(SM, Token->endLocation()); + return Result; +} + +// AST-based ObjC method rename, it renames all occurrences in the main file +// even for selectors which may have multiple tokens. +llvm::Expected<tooling::Replacements> +renameObjCMethodWithinFile(ParsedAST &AST, const ObjCMethodDecl *MD, + llvm::StringRef NewName, + std::vector<SourceLocation> Locs) { + const SourceManager &SM = AST.getSourceManager(); + auto Code = SM.getBufferData(SM.getMainFileID()); + auto RenameIdentifier = MD->getSelector().getNameForSlot(0).str(); + llvm::SmallVector<llvm::StringRef, 8> NewNames; + NewName.split(NewNames, ":"); + if (NewNames.empty()) + NewNames.push_back(NewName); + + std::vector<Range> Ranges; + const auto &LangOpts = MD->getASTContext().getLangOpts(); + for (const auto &Loc : Locs) + Ranges.push_back(tokenRangeForLoc(AST, Loc, SM, LangOpts)); + auto FilePath = AST.tuPath(); + auto RenameRanges = collectRenameIdentifierRanges( ---------------- DavidGoldman wrote:
Hmm, what do you think? we could call adjustRenameRanges to make sure what we've lexed matches. It's a bit more complex to guide lexing since we need to differentiate between method decls/exprs but maybe that could work. Just not sure it's worth adding much on top of this logic - we originally had a purely AST-visitor based approach but I'd prefer not to add more complexity here. https://github.com/llvm/llvm-project/pull/76466 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits