@@ -40,6 +40,8 @@ namespace clang {
4040namespace clangd {
4141namespace {
4242
43+ using tooling::SymbolName;
44+
4345std::optional<std::string> filePath (const SymbolLocation &Loc,
4446 llvm::StringRef HintFilePath) {
4547 if (!Loc)
@@ -591,11 +593,11 @@ bool isMatchingSelectorName(const syntax::Token &Cur, const syntax::Token &Next,
591593// The search will terminate upon seeing Terminator or a ; at the top level.
592594std::optional<SymbolRange>
593595findAllSelectorPieces (llvm::ArrayRef<syntax::Token> Tokens,
594- const SourceManager &SM, Selector Sel ,
596+ const SourceManager &SM, const SymbolName &Name ,
595597 tok::TokenKind Terminator) {
596598 assert (!Tokens.empty ());
597599
598- unsigned NumArgs = Sel. getNumArgs ();
600+ unsigned NumArgs = Name. getNamePieces (). size ();
599601 llvm::SmallVector<tok::TokenKind, 8 > Closes;
600602 std::vector<Range> SelectorPieces;
601603 for (unsigned Index = 0 , Last = Tokens.size (); Index < Last - 1 ; ++Index) {
@@ -605,12 +607,12 @@ findAllSelectorPieces(llvm::ArrayRef<syntax::Token> Tokens,
605607 auto PieceCount = SelectorPieces.size ();
606608 if (PieceCount < NumArgs &&
607609 isMatchingSelectorName (Tok, Tokens[Index + 1 ], SM,
608- Sel. getNameForSlot (PieceCount) )) {
610+ Name. getNamePieces ()[PieceCount] )) {
609611 // If 'foo:' instead of ':' (empty selector), we need to skip the ':'
610612 // token after the name. We don't currently properly support empty
611613 // selectors since we may lex them improperly due to ternary statements
612614 // as well as don't properly support storing their ranges for edits.
613- if (!Sel. getNameForSlot (PieceCount) .empty ())
615+ if (!Name. getNamePieces ()[PieceCount] .empty ())
614616 ++Index;
615617 SelectorPieces.push_back (
616618 halfOpenToRange (SM, Tok.range (SM).toCharRange (SM)));
@@ -662,16 +664,17 @@ findAllSelectorPieces(llvm::ArrayRef<syntax::Token> Tokens,
662664
663665// / Collects all ranges of the given identifier/selector in the source code.
664666// /
665- // / If a selector is given, this does a full lex of the given source code in
666- // / order to identify all selector fragments (e.g. in method exprs/decls) since
667- // / they are non-contiguous.
668- std::vector<SymbolRange> collectRenameIdentifierRanges (
669- llvm::StringRef Identifier, llvm::StringRef Content,
670- const LangOptions &LangOpts, std::optional<Selector> Selector) {
667+ // / If `Name` is an Objective-C symbol name, this does a full lex of the given
668+ // / source code in order to identify all selector fragments (e.g. in method
669+ // / exprs/decls) since they are non-contiguous.
670+ std::vector<SymbolRange>
671+ collectRenameIdentifierRanges (const tooling::SymbolName &Name,
672+ llvm::StringRef Content,
673+ const LangOptions &LangOpts) {
671674 std::vector<SymbolRange> Ranges;
672- if (!Selector ) {
675+ if (auto SinglePiece = Name. getSinglePiece () ) {
673676 auto IdentifierRanges =
674- collectIdentifierRanges (Identifier , Content, LangOpts);
677+ collectIdentifierRanges (*SinglePiece , Content, LangOpts);
675678 for (const auto &R : IdentifierRanges)
676679 Ranges.emplace_back (R);
677680 return Ranges;
@@ -685,7 +688,7 @@ std::vector<SymbolRange> collectRenameIdentifierRanges(
685688 // parsing a method declaration or definition which isn't at the top level or
686689 // similar looking expressions (e.g. an @selector() expression).
687690 llvm::SmallVector<tok::TokenKind, 8 > Closes;
688- llvm::StringRef FirstSelPiece = Selector-> getNameForSlot ( 0 ) ;
691+ llvm::StringRef FirstSelPiece = Name. getNamePieces ()[ 0 ] ;
689692
690693 auto Tokens = syntax::tokenize (SM.getMainFileID (), SM, LangOpts);
691694 unsigned Last = Tokens.size () - 1 ;
@@ -717,7 +720,7 @@ std::vector<SymbolRange> collectRenameIdentifierRanges(
717720 // Check if we can find all the relevant selector peices starting from
718721 // this token
719722 auto SelectorRanges =
720- findAllSelectorPieces (ArrayRef (Tokens).slice (Index), SM, *Selector ,
723+ findAllSelectorPieces (ArrayRef (Tokens).slice (Index), SM, Name ,
721724 Closes.empty () ? tok::l_brace : Closes.back ());
722725 if (SelectorRanges)
723726 Ranges.emplace_back (std::move (*SelectorRanges));
@@ -764,7 +767,6 @@ renameObjCMethodWithinFile(ParsedAST &AST, const ObjCMethodDecl *MD,
764767 std::vector<SourceLocation> SelectorOccurences) {
765768 const SourceManager &SM = AST.getSourceManager ();
766769 auto Code = SM.getBufferData (SM.getMainFileID ());
767- auto RenameIdentifier = MD->getSelector ().getNameForSlot (0 ).str ();
768770 llvm::SmallVector<llvm::StringRef, 8 > NewNames;
769771 NewName.split (NewNames, " :" );
770772
@@ -774,7 +776,7 @@ renameObjCMethodWithinFile(ParsedAST &AST, const ObjCMethodDecl *MD,
774776 Ranges.push_back (tokenRangeForLoc (AST, Loc, SM, LangOpts));
775777 auto FilePath = AST.tuPath ();
776778 auto RenameRanges = collectRenameIdentifierRanges (
777- RenameIdentifier , Code, LangOpts, MD-> getSelector () );
779+ SymbolName (MD-> getDeclName ()) , Code, LangOpts);
778780 auto RenameEdit = buildRenameEdit (FilePath, Code, RenameRanges, NewNames);
779781 if (!RenameEdit)
780782 return error (" failed to rename in file {0}: {1}" , FilePath,
@@ -926,22 +928,14 @@ renameOutsideFile(const NamedDecl &RenameDecl, llvm::StringRef MainFilePath,
926928 ExpBuffer.getError ().message ());
927929 continue ;
928930 }
929- std::string RenameIdentifier = RenameDecl.getNameAsString ();
930- std::optional<Selector> Selector = std::nullopt ;
931+ SymbolName RenameName (RenameDecl.getDeclName ());
931932 llvm::SmallVector<llvm::StringRef, 8 > NewNames;
932- if (const auto *MD = dyn_cast<ObjCMethodDecl>(&RenameDecl)) {
933- if (MD->getSelector ().getNumArgs () > 1 ) {
934- RenameIdentifier = MD->getSelector ().getNameForSlot (0 ).str ();
935- Selector = MD->getSelector ();
936- }
937- }
938933 NewName.split (NewNames, " :" );
939934
940935 auto AffectedFileCode = (*ExpBuffer)->getBuffer ();
941- auto RenameRanges =
942- adjustRenameRanges (AffectedFileCode, RenameIdentifier,
943- std::move (FileAndOccurrences.second ),
944- RenameDecl.getASTContext ().getLangOpts (), Selector);
936+ auto RenameRanges = adjustRenameRanges (
937+ AffectedFileCode, RenameName, std::move (FileAndOccurrences.second ),
938+ RenameDecl.getASTContext ().getLangOpts ());
945939 if (!RenameRanges) {
946940 // Our heuristics fails to adjust rename ranges to the current state of
947941 // the file, it is most likely the index is stale, so we give up the
@@ -1226,14 +1220,13 @@ llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath,
12261220// were inserted). If such a "near miss" is found, the rename is still
12271221// possible
12281222std::optional<std::vector<SymbolRange>>
1229- adjustRenameRanges (llvm::StringRef DraftCode, llvm::StringRef Identifier,
1230- std::vector<Range> Indexed, const LangOptions &LangOpts,
1231- std::optional<Selector> Selector) {
1223+ adjustRenameRanges (llvm::StringRef DraftCode, const tooling::SymbolName &Name,
1224+ std::vector<Range> Indexed, const LangOptions &LangOpts) {
12321225 trace::Span Tracer (" AdjustRenameRanges" );
12331226 assert (!Indexed.empty ());
12341227 assert (llvm::is_sorted (Indexed));
12351228 std::vector<SymbolRange> Lexed =
1236- collectRenameIdentifierRanges (Identifier , DraftCode, LangOpts, Selector );
1229+ collectRenameIdentifierRanges (Name , DraftCode, LangOpts);
12371230 llvm::sort (Lexed);
12381231 return getMappedRanges (Indexed, Lexed);
12391232}
0 commit comments