| Haojian Wu | 488c509 | 2019-05-31 14:38:16 +0000 | [diff] [blame] | 1 | //===--- Rename.cpp - Symbol-rename refactorings -----------------*- C++-*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| Sam McCall | c094912 | 2019-05-07 07:11:56 +0000 | [diff] [blame] | 9 | #include "refactor/Rename.h" |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 10 | #include "AST.h" |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 11 | #include "FindTarget.h" |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 12 | #include "Logger.h" |
| Sam McCall | 915f978 | 2019-09-04 09:46:06 +0000 | [diff] [blame] | 13 | #include "ParsedAST.h" |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 14 | #include "Selection.h" |
| Sam McCall | 19cefc2 | 2019-09-03 15:34:47 +0000 | [diff] [blame] | 15 | #include "SourceCode.h" |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 16 | #include "Trace.h" |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 17 | #include "index/SymbolCollector.h" |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclTemplate.h" |
| 20 | #include "clang/Basic/SourceLocation.h" |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 21 | #include "clang/Tooling/Refactoring/Rename/USRFindingAction.h" |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 22 | #include "clang/Tooling/Syntax/Tokens.h" |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 23 | #include "llvm/ADT/None.h" |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 25 | #include "llvm/Support/Casting.h" |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 26 | #include "llvm/Support/Error.h" |
| Haojian Wu | 8805316 | 2019-11-19 15:23:36 +0100 | [diff] [blame] | 27 | #include "llvm/Support/FormatVariadic.h" |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 28 | #include <algorithm> |
| Sam McCall | c094912 | 2019-05-07 07:11:56 +0000 | [diff] [blame] | 29 | |
| 30 | namespace clang { |
| 31 | namespace clangd { |
| 32 | namespace { |
| 33 | |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 34 | llvm::Optional<std::string> filePath(const SymbolLocation &Loc, |
| 35 | llvm::StringRef HintFilePath) { |
| 36 | if (!Loc) |
| 37 | return None; |
| Haojian Wu | f821ab8 | 2019-10-29 10:49:27 +0100 | [diff] [blame] | 38 | auto Path = URI::resolve(Loc.FileURI, HintFilePath); |
| 39 | if (!Path) { |
| 40 | elog("Could not resolve URI {0}: {1}", Loc.FileURI, Path.takeError()); |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 41 | return None; |
| 42 | } |
| Haojian Wu | f821ab8 | 2019-10-29 10:49:27 +0100 | [diff] [blame] | 43 | |
| 44 | return *Path; |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 47 | // Returns true if the given location is expanded from any macro body. |
| 48 | bool isInMacroBody(const SourceManager &SM, SourceLocation Loc) { |
| 49 | while (Loc.isMacroID()) { |
| 50 | if (SM.isMacroBodyExpansion(Loc)) |
| 51 | return true; |
| 52 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
| 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 58 | // Query the index to find some other files where the Decl is referenced. |
| Haojian Wu | 93a825c | 2019-06-27 13:24:10 +0000 | [diff] [blame] | 59 | llvm::Optional<std::string> getOtherRefFile(const Decl &D, StringRef MainFile, |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 60 | const SymbolIndex &Index) { |
| 61 | RefsRequest Req; |
| 62 | // We limit the number of results, this is a correctness/performance |
| 63 | // tradeoff. We expect the number of symbol references in the current file |
| 64 | // is smaller than the limit. |
| 65 | Req.Limit = 100; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 66 | Req.IDs.insert(*getSymbolID(&D)); |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 67 | llvm::Optional<std::string> OtherFile; |
| 68 | Index.refs(Req, [&](const Ref &R) { |
| 69 | if (OtherFile) |
| 70 | return; |
| 71 | if (auto RefFilePath = filePath(R.Location, /*HintFilePath=*/MainFile)) { |
| 72 | if (*RefFilePath != MainFile) |
| 73 | OtherFile = *RefFilePath; |
| 74 | } |
| 75 | }); |
| 76 | return OtherFile; |
| 77 | } |
| 78 | |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 79 | llvm::DenseSet<const NamedDecl *> locateDeclAt(ParsedAST &AST, |
| 80 | SourceLocation TokenStartLoc) { |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 81 | unsigned Offset = |
| 82 | AST.getSourceManager().getDecomposedSpellingLoc(TokenStartLoc).second; |
| 83 | |
| Sam McCall | be6d07c | 2020-02-23 20:03:00 +0100 | [diff] [blame] | 84 | SelectionTree Selection = SelectionTree::createRight( |
| 85 | AST.getASTContext(), AST.getTokens(), Offset, Offset); |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 86 | const SelectionTree::Node *SelectedNode = Selection.commonAncestor(); |
| 87 | if (!SelectedNode) |
| 88 | return {}; |
| 89 | |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 90 | llvm::DenseSet<const NamedDecl *> Result; |
| Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 91 | for (const NamedDecl *D : |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 92 | targetDecl(SelectedNode->ASTNode, |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 93 | DeclRelation::Alias | DeclRelation::TemplatePattern)) { |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 94 | // Get to CXXRecordDecl from constructor or destructor. |
| Sam McCall | f06f439 | 2020-01-03 17:26:33 +0100 | [diff] [blame] | 95 | D = tooling::getCanonicalSymbolDeclaration(D); |
| 96 | Result.insert(D); |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 97 | } |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 98 | return Result; |
| 99 | } |
| 100 | |
| Haojian Wu | d6da8a1 | 2020-02-05 12:31:11 +0100 | [diff] [blame] | 101 | // By default, we blacklist C++ standard symbols and protobuf symbols as rename |
| 102 | // these symbols would change system/generated files which are unlikely to be |
| 103 | // modified. |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 104 | bool isBlacklisted(const NamedDecl &RenameDecl) { |
| Haojian Wu | d6da8a1 | 2020-02-05 12:31:11 +0100 | [diff] [blame] | 105 | if (isProtoFile(RenameDecl.getLocation(), |
| 106 | RenameDecl.getASTContext().getSourceManager())) |
| 107 | return true; |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 108 | static const auto *StdSymbols = new llvm::DenseSet<llvm::StringRef>({ |
| 109 | #define SYMBOL(Name, NameSpace, Header) {#NameSpace #Name}, |
| 110 | #include "StdSymbolMap.inc" |
| 111 | #undef SYMBOL |
| 112 | }); |
| Haojian Wu | 02323a3 | 2020-02-26 15:22:56 +0100 | [diff] [blame] | 113 | return StdSymbols->count(printQualifiedName(RenameDecl)); |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 116 | enum ReasonToReject { |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 117 | NoSymbolFound, |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 118 | NoIndexProvided, |
| 119 | NonIndexable, |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 120 | UsedOutsideFile, // for within-file rename only. |
| Haojian Wu | 442a120 | 2019-06-26 08:10:26 +0000 | [diff] [blame] | 121 | UnsupportedSymbol, |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 122 | AmbiguousSymbol, |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 125 | llvm::Optional<ReasonToReject> renameable(const NamedDecl &RenameDecl, |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 126 | StringRef MainFilePath, |
| 127 | const SymbolIndex *Index, |
| 128 | bool CrossFile) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 129 | trace::Span Tracer("Renameable"); |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 130 | // Filter out symbols that are unsupported in both rename modes. |
| Haojian Wu | 93a825c | 2019-06-27 13:24:10 +0000 | [diff] [blame] | 131 | if (llvm::isa<NamespaceDecl>(&RenameDecl)) |
| Haojian Wu | 442a120 | 2019-06-26 08:10:26 +0000 | [diff] [blame] | 132 | return ReasonToReject::UnsupportedSymbol; |
| Haojian Wu | af28bb6 | 2019-09-16 10:16:56 +0000 | [diff] [blame] | 133 | if (const auto *FD = llvm::dyn_cast<FunctionDecl>(&RenameDecl)) { |
| 134 | if (FD->isOverloadedOperator()) |
| 135 | return ReasonToReject::UnsupportedSymbol; |
| 136 | } |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 137 | // function-local symbols is safe to rename. |
| Haojian Wu | 93a825c | 2019-06-27 13:24:10 +0000 | [diff] [blame] | 138 | if (RenameDecl.getParentFunctionOrMethod()) |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 139 | return None; |
| 140 | |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 141 | if (isBlacklisted(RenameDecl)) |
| 142 | return ReasonToReject::UnsupportedSymbol; |
| 143 | |
| Haojian Wu | 902dc6c | 2019-11-29 14:58:44 +0100 | [diff] [blame] | 144 | // Check whether the symbol being rename is indexable. |
| 145 | auto &ASTCtx = RenameDecl.getASTContext(); |
| 146 | bool MainFileIsHeader = isHeaderFile(MainFilePath, ASTCtx.getLangOpts()); |
| 147 | bool DeclaredInMainFile = |
| 148 | isInsideMainFile(RenameDecl.getBeginLoc(), ASTCtx.getSourceManager()); |
| 149 | bool IsMainFileOnly = true; |
| 150 | if (MainFileIsHeader) |
| 151 | // main file is a header, the symbol can't be main file only. |
| 152 | IsMainFileOnly = false; |
| 153 | else if (!DeclaredInMainFile) |
| 154 | IsMainFileOnly = false; |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 155 | // If the symbol is not indexable, we disallow rename. |
| 156 | if (!SymbolCollector::shouldCollectSymbol( |
| 157 | RenameDecl, RenameDecl.getASTContext(), SymbolCollector::Options(), |
| 158 | IsMainFileOnly)) |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 159 | return ReasonToReject::NonIndexable; |
| 160 | |
| 161 | if (!CrossFile) { |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 162 | if (!DeclaredInMainFile) |
| 163 | // We are sure the symbol is used externally, bail out early. |
| 164 | return ReasonToReject::UsedOutsideFile; |
| 165 | |
| 166 | // If the symbol is declared in the main file (which is not a header), we |
| 167 | // rename it. |
| 168 | if (!MainFileIsHeader) |
| 169 | return None; |
| 170 | |
| 171 | if (!Index) |
| 172 | return ReasonToReject::NoIndexProvided; |
| 173 | |
| 174 | auto OtherFile = getOtherRefFile(RenameDecl, MainFilePath, *Index); |
| 175 | // If the symbol is indexable and has no refs from other files in the index, |
| 176 | // we rename it. |
| 177 | if (!OtherFile) |
| 178 | return None; |
| 179 | // If the symbol is indexable and has refs from other files in the index, |
| 180 | // we disallow rename. |
| 181 | return ReasonToReject::UsedOutsideFile; |
| 182 | } |
| 183 | |
| 184 | assert(CrossFile); |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 185 | if (!Index) |
| 186 | return ReasonToReject::NoIndexProvided; |
| 187 | |
| Kirill Bobyrev | 9091f06 | 2019-12-03 10:12:17 +0100 | [diff] [blame] | 188 | // FIXME: Renaming virtual methods requires to rename all overridens in |
| 189 | // subclasses, our index doesn't have this information. |
| 190 | // Note: Within-file rename does support this through the AST. |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 191 | if (const auto *S = llvm::dyn_cast<CXXMethodDecl>(&RenameDecl)) { |
| 192 | if (S->isVirtual()) |
| 193 | return ReasonToReject::UnsupportedSymbol; |
| 194 | } |
| 195 | return None; |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| Haojian Wu | 9d34f45 | 2019-07-01 09:26:48 +0000 | [diff] [blame] | 198 | llvm::Error makeError(ReasonToReject Reason) { |
| 199 | auto Message = [](ReasonToReject Reason) { |
| 200 | switch (Reason) { |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 201 | case ReasonToReject::NoSymbolFound: |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 202 | return "there is no symbol at the given location"; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 203 | case ReasonToReject::NoIndexProvided: |
| Haojian Wu | 08cce03 | 2019-11-28 11:39:48 +0100 | [diff] [blame] | 204 | return "no index provided"; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 205 | case ReasonToReject::UsedOutsideFile: |
| Haojian Wu | 9d34f45 | 2019-07-01 09:26:48 +0000 | [diff] [blame] | 206 | return "the symbol is used outside main file"; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 207 | case ReasonToReject::NonIndexable: |
| Haojian Wu | 9d34f45 | 2019-07-01 09:26:48 +0000 | [diff] [blame] | 208 | return "symbol may be used in other files (not eligible for indexing)"; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 209 | case ReasonToReject::UnsupportedSymbol: |
| Haojian Wu | 9d34f45 | 2019-07-01 09:26:48 +0000 | [diff] [blame] | 210 | return "symbol is not a supported kind (e.g. namespace, macro)"; |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 211 | case AmbiguousSymbol: |
| 212 | return "there are multiple symbols at the given location"; |
| Haojian Wu | 9d34f45 | 2019-07-01 09:26:48 +0000 | [diff] [blame] | 213 | } |
| 214 | llvm_unreachable("unhandled reason kind"); |
| 215 | }; |
| 216 | return llvm::make_error<llvm::StringError>( |
| 217 | llvm::formatv("Cannot rename symbol: {0}", Message(Reason)), |
| 218 | llvm::inconvertibleErrorCode()); |
| 219 | } |
| 220 | |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 221 | // Return all rename occurrences in the main file. |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 222 | std::vector<SourceLocation> findOccurrencesWithinFile(ParsedAST &AST, |
| 223 | const NamedDecl &ND) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 224 | trace::Span Tracer("FindOccurrenceeWithinFile"); |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 225 | // If the cursor is at the underlying CXXRecordDecl of the |
| 226 | // ClassTemplateDecl, ND will be the CXXRecordDecl. In this case, we need to |
| Kazuaki Ishizaki | dd5571d | 2020-04-05 15:28:11 +0900 | [diff] [blame^] | 227 | // get the primary template manually. |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 228 | // getUSRsForDeclaration will find other related symbols, e.g. virtual and its |
| 229 | // overriddens, primary template and all explicit specializations. |
| Kirill Bobyrev | 9091f06 | 2019-12-03 10:12:17 +0100 | [diff] [blame] | 230 | // FIXME: Get rid of the remaining tooling APIs. |
| Haojian Wu | 3de9a5d | 2020-01-20 14:14:52 +0100 | [diff] [blame] | 231 | const auto *RenameDecl = |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 232 | ND.getDescribedTemplate() ? ND.getDescribedTemplate() : &ND; |
| 233 | std::vector<std::string> RenameUSRs = |
| 234 | tooling::getUSRsForDeclaration(RenameDecl, AST.getASTContext()); |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 235 | llvm::DenseSet<SymbolID> TargetIDs; |
| 236 | for (auto &USR : RenameUSRs) |
| 237 | TargetIDs.insert(SymbolID(USR)); |
| 238 | |
| 239 | std::vector<SourceLocation> Results; |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 240 | for (Decl *TopLevelDecl : AST.getLocalTopLevelDecls()) { |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 241 | findExplicitReferences(TopLevelDecl, [&](ReferenceLoc Ref) { |
| 242 | if (Ref.Targets.empty()) |
| 243 | return; |
| 244 | for (const auto *Target : Ref.Targets) { |
| 245 | auto ID = getSymbolID(Target); |
| 246 | if (!ID || TargetIDs.find(*ID) == TargetIDs.end()) |
| 247 | return; |
| 248 | } |
| 249 | Results.push_back(Ref.NameLoc); |
| 250 | }); |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 251 | } |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 252 | |
| 253 | return Results; |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 256 | // AST-based rename, it renames all occurrences in the main file. |
| Sam McCall | c094912 | 2019-05-07 07:11:56 +0000 | [diff] [blame] | 257 | llvm::Expected<tooling::Replacements> |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 258 | renameWithinFile(ParsedAST &AST, const NamedDecl &RenameDecl, |
| 259 | llvm::StringRef NewName) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 260 | trace::Span Tracer("RenameWithinFile"); |
| Sam McCall | b2a984c0 | 2019-09-04 10:15:27 +0000 | [diff] [blame] | 261 | const SourceManager &SM = AST.getSourceManager(); |
| Haojian Wu | 7276a44 | 2019-06-25 08:43:17 +0000 | [diff] [blame] | 262 | |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 263 | tooling::Replacements FilteredChanges; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 264 | for (SourceLocation Loc : findOccurrencesWithinFile(AST, RenameDecl)) { |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 265 | SourceLocation RenameLoc = Loc; |
| 266 | // We don't rename in any macro bodies, but we allow rename the symbol |
| 267 | // spelled in a top-level macro argument in the main file. |
| 268 | if (RenameLoc.isMacroID()) { |
| 269 | if (isInMacroBody(SM, RenameLoc)) |
| 270 | continue; |
| 271 | RenameLoc = SM.getSpellingLoc(Loc); |
| 272 | } |
| 273 | // Filter out locations not from main file. |
| 274 | // We traverse only main file decls, but locations could come from an |
| 275 | // non-preamble #include file e.g. |
| 276 | // void test() { |
| 277 | // int f^oo; |
| 278 | // #include "use_foo.inc" |
| 279 | // } |
| 280 | if (!isInsideMainFile(RenameLoc, SM)) |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 281 | continue; |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 282 | if (auto Err = FilteredChanges.add(tooling::Replacement( |
| Haojian Wu | 7db1230 | 2019-11-19 10:10:43 +0100 | [diff] [blame] | 283 | SM, CharSourceRange::getTokenRange(RenameLoc), NewName))) |
| Haojian Wu | 8b49173 | 2019-08-09 10:55:22 +0000 | [diff] [blame] | 284 | return std::move(Err); |
| Sam McCall | c094912 | 2019-05-07 07:11:56 +0000 | [diff] [blame] | 285 | } |
| 286 | return FilteredChanges; |
| 287 | } |
| 288 | |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 289 | Range toRange(const SymbolLocation &L) { |
| 290 | Range R; |
| 291 | R.start.line = L.Start.line(); |
| 292 | R.start.character = L.Start.column(); |
| 293 | R.end.line = L.End.line(); |
| 294 | R.end.character = L.End.column(); |
| 295 | return R; |
| Bill Wendling | 936de1c | 2019-12-02 14:05:28 -0800 | [diff] [blame] | 296 | } |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 297 | |
| Kirill Bobyrev | 9091f06 | 2019-12-03 10:12:17 +0100 | [diff] [blame] | 298 | // Return all rename occurrences (using the index) outside of the main file, |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 299 | // grouped by the absolute file path. |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 300 | llvm::Expected<llvm::StringMap<std::vector<Range>>> |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 301 | findOccurrencesOutsideFile(const NamedDecl &RenameDecl, |
| Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 302 | llvm::StringRef MainFile, const SymbolIndex &Index, |
| 303 | size_t MaxLimitFiles) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 304 | trace::Span Tracer("FindOccurrencesOutsideFile"); |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 305 | RefsRequest RQuest; |
| 306 | RQuest.IDs.insert(*getSymbolID(&RenameDecl)); |
| 307 | |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 308 | // Absolute file path => rename occurrences in that file. |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 309 | llvm::StringMap<std::vector<Range>> AffectedFiles; |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 310 | bool HasMore = Index.refs(RQuest, [&](const Ref &R) { |
| Haojian Wu | d83ade4 | 2020-03-11 16:07:44 +0100 | [diff] [blame] | 311 | if (AffectedFiles.size() >= MaxLimitFiles) |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 312 | return; |
| Kirill Bobyrev | 10540e4 | 2020-02-06 10:28:47 +0100 | [diff] [blame] | 313 | if ((R.Kind & RefKind::Spelled) == RefKind::Unknown) |
| 314 | return; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 315 | if (auto RefFilePath = filePath(R.Location, /*HintFilePath=*/MainFile)) { |
| 316 | if (*RefFilePath != MainFile) |
| 317 | AffectedFiles[*RefFilePath].push_back(toRange(R.Location)); |
| 318 | } |
| 319 | }); |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 320 | |
| Haojian Wu | d83ade4 | 2020-03-11 16:07:44 +0100 | [diff] [blame] | 321 | if (AffectedFiles.size() >= MaxLimitFiles) |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 322 | return llvm::make_error<llvm::StringError>( |
| 323 | llvm::formatv("The number of affected files exceeds the max limit {0}", |
| 324 | MaxLimitFiles), |
| 325 | llvm::inconvertibleErrorCode()); |
| 326 | if (HasMore) { |
| 327 | return llvm::make_error<llvm::StringError>( |
| 328 | llvm::formatv("The symbol {0} has too many occurrences", |
| 329 | RenameDecl.getQualifiedNameAsString()), |
| 330 | llvm::inconvertibleErrorCode()); |
| 331 | } |
| Haojian Wu | f0004aa | 2019-12-10 22:15:29 +0100 | [diff] [blame] | 332 | // Sort and deduplicate the results, in case that index returns duplications. |
| 333 | for (auto &FileAndOccurrences : AffectedFiles) { |
| 334 | auto &Ranges = FileAndOccurrences.getValue(); |
| 335 | llvm::sort(Ranges); |
| 336 | Ranges.erase(std::unique(Ranges.begin(), Ranges.end()), Ranges.end()); |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 337 | |
| 338 | SPAN_ATTACH(Tracer, FileAndOccurrences.first(), |
| 339 | static_cast<int64_t>(Ranges.size())); |
| Haojian Wu | f0004aa | 2019-12-10 22:15:29 +0100 | [diff] [blame] | 340 | } |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 341 | return AffectedFiles; |
| 342 | } |
| 343 | |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 344 | // Index-based rename, it renames all occurrences outside of the main file. |
| 345 | // |
| 346 | // The cross-file rename is purely based on the index, as we don't want to |
| 347 | // build all ASTs for affected files, which may cause a performance hit. |
| 348 | // We choose to trade off some correctness for performance and scalability. |
| 349 | // |
| 350 | // Clangd builds a dynamic index for all opened files on top of the static |
| 351 | // index of the whole codebase. Dynamic index is up-to-date (respects dirty |
| 352 | // buffers) as long as clangd finishes processing opened files, while static |
| 353 | // index (background index) is relatively stale. We choose the dirty buffers |
| 354 | // as the file content we rename on, and fallback to file content on disk if |
| 355 | // there is no dirty buffer. |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 356 | llvm::Expected<FileEdits> renameOutsideFile( |
| 357 | const NamedDecl &RenameDecl, llvm::StringRef MainFilePath, |
| Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 358 | llvm::StringRef NewName, const SymbolIndex &Index, size_t MaxLimitFiles, |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 359 | llvm::function_ref<llvm::Expected<std::string>(PathRef)> GetFileContent) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 360 | trace::Span Tracer("RenameOutsideFile"); |
| Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 361 | auto AffectedFiles = findOccurrencesOutsideFile(RenameDecl, MainFilePath, |
| 362 | Index, MaxLimitFiles); |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 363 | if (!AffectedFiles) |
| 364 | return AffectedFiles.takeError(); |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 365 | FileEdits Results; |
| Haojian Wu | 3c3aca2 | 2019-11-28 12:47:32 +0100 | [diff] [blame] | 366 | for (auto &FileAndOccurrences : *AffectedFiles) { |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 367 | llvm::StringRef FilePath = FileAndOccurrences.first(); |
| 368 | |
| 369 | auto AffectedFileCode = GetFileContent(FilePath); |
| 370 | if (!AffectedFileCode) { |
| 371 | elog("Fail to read file content: {0}", AffectedFileCode.takeError()); |
| 372 | continue; |
| 373 | } |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 374 | auto RenameRanges = |
| 375 | adjustRenameRanges(*AffectedFileCode, RenameDecl.getNameAsString(), |
| 376 | std::move(FileAndOccurrences.second), |
| 377 | RenameDecl.getASTContext().getLangOpts()); |
| 378 | if (!RenameRanges) { |
| Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame] | 379 | // Our heuristics fails to adjust rename ranges to the current state of |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 380 | // the file, it is most likely the index is stale, so we give up the |
| 381 | // entire rename. |
| 382 | return llvm::make_error<llvm::StringError>( |
| 383 | llvm::formatv("Index results don't match the content of file {0} " |
| 384 | "(the index may be stale)", |
| 385 | FilePath), |
| 386 | llvm::inconvertibleErrorCode()); |
| 387 | } |
| Haojian Wu | 66ab932 | 2019-11-28 16:48:49 +0100 | [diff] [blame] | 388 | auto RenameEdit = |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 389 | buildRenameEdit(FilePath, *AffectedFileCode, *RenameRanges, NewName); |
| Haojian Wu | 8805316 | 2019-11-19 15:23:36 +0100 | [diff] [blame] | 390 | if (!RenameEdit) { |
| 391 | return llvm::make_error<llvm::StringError>( |
| 392 | llvm::formatv("fail to build rename edit for file {0}: {1}", FilePath, |
| 393 | llvm::toString(RenameEdit.takeError())), |
| 394 | llvm::inconvertibleErrorCode()); |
| 395 | } |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 396 | if (!RenameEdit->Replacements.empty()) |
| 397 | Results.insert({FilePath, std::move(*RenameEdit)}); |
| 398 | } |
| 399 | return Results; |
| 400 | } |
| 401 | |
| Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame] | 402 | // A simple edit is either changing line or column, but not both. |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 403 | bool impliesSimpleEdit(const Position &LHS, const Position &RHS) { |
| 404 | return LHS.line == RHS.line || LHS.character == RHS.character; |
| 405 | } |
| 406 | |
| 407 | // Performs a DFS to enumerate all possible near-miss matches. |
| 408 | // It finds the locations where the indexed occurrences are now spelled in |
| 409 | // Lexed occurrences, a near miss is defined as: |
| 410 | // - a near miss maps all of the **name** occurrences from the index onto a |
| 411 | // *subset* of lexed occurrences (we allow a single name refers to more |
| 412 | // than one symbol) |
| 413 | // - all indexed occurrences must be mapped, and Result must be distinct and |
| Kazuaki Ishizaki | dd5571d | 2020-04-05 15:28:11 +0900 | [diff] [blame^] | 414 | // preserve order (only support detecting simple edits to ensure a |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 415 | // robust mapping) |
| 416 | // - each indexed -> lexed occurrences mapping correspondence may change the |
| 417 | // *line* or *column*, but not both (increases chance of a robust mapping) |
| 418 | void findNearMiss( |
| 419 | std::vector<size_t> &PartialMatch, ArrayRef<Range> IndexedRest, |
| 420 | ArrayRef<Range> LexedRest, int LexedIndex, int &Fuel, |
| 421 | llvm::function_ref<void(const std::vector<size_t> &)> MatchedCB) { |
| 422 | if (--Fuel < 0) |
| 423 | return; |
| 424 | if (IndexedRest.size() > LexedRest.size()) |
| 425 | return; |
| 426 | if (IndexedRest.empty()) { |
| 427 | MatchedCB(PartialMatch); |
| 428 | return; |
| 429 | } |
| 430 | if (impliesSimpleEdit(IndexedRest.front().start, LexedRest.front().start)) { |
| 431 | PartialMatch.push_back(LexedIndex); |
| 432 | findNearMiss(PartialMatch, IndexedRest.drop_front(), LexedRest.drop_front(), |
| 433 | LexedIndex + 1, Fuel, MatchedCB); |
| 434 | PartialMatch.pop_back(); |
| 435 | } |
| 436 | findNearMiss(PartialMatch, IndexedRest, LexedRest.drop_front(), |
| 437 | LexedIndex + 1, Fuel, MatchedCB); |
| 438 | } |
| 439 | |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 440 | } // namespace |
| 441 | |
| 442 | llvm::Expected<FileEdits> rename(const RenameInputs &RInputs) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 443 | trace::Span Tracer("Rename flow"); |
| Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 444 | const auto &Opts = RInputs.Opts; |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 445 | ParsedAST &AST = RInputs.AST; |
| 446 | const SourceManager &SM = AST.getSourceManager(); |
| 447 | llvm::StringRef MainFileCode = SM.getBufferData(SM.getMainFileID()); |
| 448 | auto GetFileContent = [&RInputs, |
| 449 | &SM](PathRef AbsPath) -> llvm::Expected<std::string> { |
| 450 | llvm::Optional<std::string> DirtyBuffer; |
| 451 | if (RInputs.GetDirtyBuffer && |
| 452 | (DirtyBuffer = RInputs.GetDirtyBuffer(AbsPath))) |
| 453 | return std::move(*DirtyBuffer); |
| 454 | |
| 455 | auto Content = |
| 456 | SM.getFileManager().getVirtualFileSystem().getBufferForFile(AbsPath); |
| 457 | if (!Content) |
| 458 | return llvm::createStringError( |
| 459 | llvm::inconvertibleErrorCode(), |
| 460 | llvm::formatv("Fail to open file {0}: {1}", AbsPath, |
| 461 | Content.getError().message())); |
| 462 | if (!*Content) |
| 463 | return llvm::createStringError( |
| 464 | llvm::inconvertibleErrorCode(), |
| 465 | llvm::formatv("Got no buffer for file {0}", AbsPath)); |
| 466 | |
| 467 | return (*Content)->getBuffer().str(); |
| 468 | }; |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 469 | // Try to find the tokens adjacent to the cursor position. |
| 470 | auto Loc = sourceLocationInMainFile(SM, RInputs.Pos); |
| 471 | if (!Loc) |
| 472 | return Loc.takeError(); |
| 473 | const syntax::Token *IdentifierToken = |
| 474 | spelledIdentifierTouching(*Loc, AST.getTokens()); |
| 475 | // Renames should only triggered on identifiers. |
| 476 | if (!IdentifierToken) |
| 477 | return makeError(ReasonToReject::NoSymbolFound); |
| Kirill Bobyrev | 9091f06 | 2019-12-03 10:12:17 +0100 | [diff] [blame] | 478 | // FIXME: Renaming macros is not supported yet, the macro-handling code should |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 479 | // be moved to rename tooling library. |
| Kadir Cetinkaya | 3ae2fc7 | 2020-02-28 09:25:40 +0100 | [diff] [blame] | 480 | if (locateMacroAt(*IdentifierToken, AST.getPreprocessor())) |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 481 | return makeError(ReasonToReject::UnsupportedSymbol); |
| 482 | |
| Kirill Bobyrev | ec61882 | 2019-12-12 13:10:59 +0100 | [diff] [blame] | 483 | auto DeclsUnderCursor = locateDeclAt(AST, IdentifierToken->location()); |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 484 | if (DeclsUnderCursor.empty()) |
| 485 | return makeError(ReasonToReject::NoSymbolFound); |
| 486 | if (DeclsUnderCursor.size() > 1) |
| 487 | return makeError(ReasonToReject::AmbiguousSymbol); |
| 488 | |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 489 | const auto &RenameDecl = |
| 490 | llvm::cast<NamedDecl>(*(*DeclsUnderCursor.begin())->getCanonicalDecl()); |
| 491 | auto Reject = renameable(RenameDecl, RInputs.MainFilePath, RInputs.Index, |
| Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 492 | Opts.AllowCrossFile); |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 493 | if (Reject) |
| 494 | return makeError(*Reject); |
| 495 | |
| Kirill Bobyrev | 9091f06 | 2019-12-03 10:12:17 +0100 | [diff] [blame] | 496 | // We have two implementations of the rename: |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 497 | // - AST-based rename: used for renaming local symbols, e.g. variables |
| 498 | // defined in a function body; |
| 499 | // - index-based rename: used for renaming non-local symbols, and not |
| 500 | // feasible for local symbols (as by design our index don't index these |
| 501 | // symbols by design; |
| 502 | // To make cross-file rename work for local symbol, we use a hybrid solution: |
| 503 | // - run AST-based rename on the main file; |
| 504 | // - run index-based rename on other affected files; |
| Haojian Wu | 0d893fd | 2020-01-27 10:39:55 +0100 | [diff] [blame] | 505 | auto MainFileRenameEdit = renameWithinFile(AST, RenameDecl, RInputs.NewName); |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 506 | if (!MainFileRenameEdit) |
| 507 | return MainFileRenameEdit.takeError(); |
| 508 | |
| Haojian Wu | aa324c5 | 2020-02-27 14:46:01 +0100 | [diff] [blame] | 509 | // return the main file edit if this is a within-file rename or the symbol |
| 510 | // being renamed is function local. |
| 511 | if (!Opts.AllowCrossFile || RenameDecl.getParentFunctionOrMethod()) { |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 512 | return FileEdits( |
| 513 | {std::make_pair(RInputs.MainFilePath, |
| 514 | Edit{MainFileCode, std::move(*MainFileRenameEdit)})}); |
| 515 | } |
| 516 | |
| 517 | FileEdits Results; |
| Kirill Bobyrev | 9091f06 | 2019-12-03 10:12:17 +0100 | [diff] [blame] | 518 | // Renameable safely guards us that at this point we are renaming a local |
| 519 | // symbol if we don't have index. |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 520 | if (RInputs.Index) { |
| Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 521 | auto OtherFilesEdits = renameOutsideFile( |
| 522 | RenameDecl, RInputs.MainFilePath, RInputs.NewName, *RInputs.Index, |
| 523 | Opts.LimitFiles == 0 ? std::numeric_limits<size_t>::max() |
| Haojian Wu | d83ade4 | 2020-03-11 16:07:44 +0100 | [diff] [blame] | 524 | : Opts.LimitFiles, |
| Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 525 | GetFileContent); |
| Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 526 | if (!OtherFilesEdits) |
| 527 | return OtherFilesEdits.takeError(); |
| 528 | Results = std::move(*OtherFilesEdits); |
| 529 | } |
| 530 | // Attach the rename edits for the main file. |
| 531 | Results.try_emplace(RInputs.MainFilePath, MainFileCode, |
| 532 | std::move(*MainFileRenameEdit)); |
| 533 | return Results; |
| 534 | } |
| 535 | |
| Haojian Wu | 66ab932 | 2019-11-28 16:48:49 +0100 | [diff] [blame] | 536 | llvm::Expected<Edit> buildRenameEdit(llvm::StringRef AbsFilePath, |
| 537 | llvm::StringRef InitialCode, |
| Haojian Wu | 8805316 | 2019-11-19 15:23:36 +0100 | [diff] [blame] | 538 | std::vector<Range> Occurrences, |
| 539 | llvm::StringRef NewName) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 540 | trace::Span Tracer("BuildRenameEdit"); |
| 541 | SPAN_ATTACH(Tracer, "file_path", AbsFilePath); |
| 542 | SPAN_ATTACH(Tracer, "rename_occurrences", |
| 543 | static_cast<int64_t>(Occurrences.size())); |
| 544 | |
| Haojian Wu | f0004aa | 2019-12-10 22:15:29 +0100 | [diff] [blame] | 545 | assert(std::is_sorted(Occurrences.begin(), Occurrences.end())); |
| 546 | assert(std::unique(Occurrences.begin(), Occurrences.end()) == |
| 547 | Occurrences.end() && |
| 548 | "Occurrences must be unique"); |
| 549 | |
| Haojian Wu | 8805316 | 2019-11-19 15:23:36 +0100 | [diff] [blame] | 550 | // These two always correspond to the same position. |
| 551 | Position LastPos{0, 0}; |
| 552 | size_t LastOffset = 0; |
| 553 | |
| 554 | auto Offset = [&](const Position &P) -> llvm::Expected<size_t> { |
| 555 | assert(LastPos <= P && "malformed input"); |
| 556 | Position Shifted = { |
| 557 | P.line - LastPos.line, |
| 558 | P.line > LastPos.line ? P.character : P.character - LastPos.character}; |
| 559 | auto ShiftedOffset = |
| 560 | positionToOffset(InitialCode.substr(LastOffset), Shifted); |
| 561 | if (!ShiftedOffset) |
| 562 | return llvm::make_error<llvm::StringError>( |
| 563 | llvm::formatv("fail to convert the position {0} to offset ({1})", P, |
| 564 | llvm::toString(ShiftedOffset.takeError())), |
| 565 | llvm::inconvertibleErrorCode()); |
| 566 | LastPos = P; |
| 567 | LastOffset += *ShiftedOffset; |
| 568 | return LastOffset; |
| 569 | }; |
| 570 | |
| 571 | std::vector<std::pair</*start*/ size_t, /*end*/ size_t>> OccurrencesOffsets; |
| 572 | for (const auto &R : Occurrences) { |
| 573 | auto StartOffset = Offset(R.start); |
| 574 | if (!StartOffset) |
| 575 | return StartOffset.takeError(); |
| 576 | auto EndOffset = Offset(R.end); |
| 577 | if (!EndOffset) |
| 578 | return EndOffset.takeError(); |
| 579 | OccurrencesOffsets.push_back({*StartOffset, *EndOffset}); |
| 580 | } |
| 581 | |
| 582 | tooling::Replacements RenameEdit; |
| 583 | for (const auto &R : OccurrencesOffsets) { |
| 584 | auto ByteLength = R.second - R.first; |
| 585 | if (auto Err = RenameEdit.add( |
| Haojian Wu | 66ab932 | 2019-11-28 16:48:49 +0100 | [diff] [blame] | 586 | tooling::Replacement(AbsFilePath, R.first, ByteLength, NewName))) |
| Haojian Wu | 8805316 | 2019-11-19 15:23:36 +0100 | [diff] [blame] | 587 | return std::move(Err); |
| 588 | } |
| 589 | return Edit(InitialCode, std::move(RenameEdit)); |
| 590 | } |
| 591 | |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 592 | // Details: |
| 593 | // - lex the draft code to get all rename candidates, this yields a superset |
| 594 | // of candidates. |
| 595 | // - apply range patching heuristics to generate "authoritative" occurrences, |
| 596 | // cases we consider: |
| 597 | // (a) index returns a subset of candidates, we use the indexed results. |
| 598 | // - fully equal, we are sure the index is up-to-date |
| 599 | // - proper subset, index is correct in most cases? there may be false |
| 600 | // positives (e.g. candidates got appended), but rename is still safe |
| 601 | // (b) index returns non-candidate results, we attempt to map the indexed |
| 602 | // ranges onto candidates in a plausible way (e.g. guess that lines |
| 603 | // were inserted). If such a "near miss" is found, the rename is still |
| 604 | // possible |
| 605 | llvm::Optional<std::vector<Range>> |
| 606 | adjustRenameRanges(llvm::StringRef DraftCode, llvm::StringRef Identifier, |
| 607 | std::vector<Range> Indexed, const LangOptions &LangOpts) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 608 | trace::Span Tracer("AdjustRenameRanges"); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 609 | assert(!Indexed.empty()); |
| Haojian Wu | f0004aa | 2019-12-10 22:15:29 +0100 | [diff] [blame] | 610 | assert(std::is_sorted(Indexed.begin(), Indexed.end())); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 611 | std::vector<Range> Lexed = |
| 612 | collectIdentifierRanges(Identifier, DraftCode, LangOpts); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 613 | llvm::sort(Lexed); |
| 614 | return getMappedRanges(Indexed, Lexed); |
| 615 | } |
| 616 | |
| 617 | llvm::Optional<std::vector<Range>> getMappedRanges(ArrayRef<Range> Indexed, |
| 618 | ArrayRef<Range> Lexed) { |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 619 | trace::Span Tracer("GetMappedRanges"); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 620 | assert(!Indexed.empty()); |
| 621 | assert(std::is_sorted(Indexed.begin(), Indexed.end())); |
| 622 | assert(std::is_sorted(Lexed.begin(), Lexed.end())); |
| 623 | |
| 624 | if (Indexed.size() > Lexed.size()) { |
| 625 | vlog("The number of lexed occurrences is less than indexed occurrences"); |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 626 | SPAN_ATTACH( |
| 627 | Tracer, "error", |
| 628 | "The number of lexed occurrences is less than indexed occurrences"); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 629 | return llvm::None; |
| 630 | } |
| 631 | // Fast check for the special subset case. |
| 632 | if (std::includes(Indexed.begin(), Indexed.end(), Lexed.begin(), Lexed.end())) |
| 633 | return Indexed.vec(); |
| 634 | |
| 635 | std::vector<size_t> Best; |
| 636 | size_t BestCost = std::numeric_limits<size_t>::max(); |
| 637 | bool HasMultiple = 0; |
| 638 | std::vector<size_t> ResultStorage; |
| 639 | int Fuel = 10000; |
| 640 | findNearMiss(ResultStorage, Indexed, Lexed, 0, Fuel, |
| 641 | [&](const std::vector<size_t> &Matched) { |
| 642 | size_t MCost = |
| 643 | renameRangeAdjustmentCost(Indexed, Lexed, Matched); |
| 644 | if (MCost < BestCost) { |
| 645 | BestCost = MCost; |
| 646 | Best = std::move(Matched); |
| 647 | HasMultiple = false; // reset |
| 648 | return; |
| 649 | } |
| 650 | if (MCost == BestCost) |
| 651 | HasMultiple = true; |
| 652 | }); |
| 653 | if (HasMultiple) { |
| 654 | vlog("The best near miss is not unique."); |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 655 | SPAN_ATTACH(Tracer, "error", "The best near miss is not unique"); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 656 | return llvm::None; |
| 657 | } |
| 658 | if (Best.empty()) { |
| 659 | vlog("Didn't find a near miss."); |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 660 | SPAN_ATTACH(Tracer, "error", "Didn't find a near miss"); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 661 | return llvm::None; |
| 662 | } |
| 663 | std::vector<Range> Mapped; |
| 664 | for (auto I : Best) |
| 665 | Mapped.push_back(Lexed[I]); |
| Haojian Wu | 74c97ca | 2020-02-11 12:37:09 +0100 | [diff] [blame] | 666 | SPAN_ATTACH(Tracer, "mapped_ranges", static_cast<int64_t>(Mapped.size())); |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 667 | return Mapped; |
| 668 | } |
| 669 | |
| 670 | // The cost is the sum of the implied edit sizes between successive diffs, only |
| 671 | // simple edits are considered: |
| 672 | // - insert/remove a line (change line offset) |
| 673 | // - insert/remove a character on an existing line (change column offset) |
| 674 | // |
| 675 | // Example I, total result is 1 + 1 = 2. |
| 676 | // diff[0]: line + 1 <- insert a line before edit 0. |
| 677 | // diff[1]: line + 1 |
| 678 | // diff[2]: line + 1 |
| 679 | // diff[3]: line + 2 <- insert a line before edits 2 and 3. |
| 680 | // |
| 681 | // Example II, total result is 1 + 1 + 1 = 3. |
| 682 | // diff[0]: line + 1 <- insert a line before edit 0. |
| 683 | // diff[1]: column + 1 <- remove a line between edits 0 and 1, and insert a |
| 684 | // character on edit 1. |
| 685 | size_t renameRangeAdjustmentCost(ArrayRef<Range> Indexed, ArrayRef<Range> Lexed, |
| 686 | ArrayRef<size_t> MappedIndex) { |
| 687 | assert(Indexed.size() == MappedIndex.size()); |
| 688 | assert(std::is_sorted(Indexed.begin(), Indexed.end())); |
| 689 | assert(std::is_sorted(Lexed.begin(), Lexed.end())); |
| 690 | |
| 691 | int LastLine = -1; |
| 692 | int LastDLine = 0, LastDColumn = 0; |
| 693 | int Cost = 0; |
| 694 | for (size_t I = 0; I < Indexed.size(); ++I) { |
| 695 | int DLine = Indexed[I].start.line - Lexed[MappedIndex[I]].start.line; |
| 696 | int DColumn = |
| 697 | Indexed[I].start.character - Lexed[MappedIndex[I]].start.character; |
| 698 | int Line = Indexed[I].start.line; |
| 699 | if (Line != LastLine) |
| Kazuaki Ishizaki | b7ecf1c | 2020-01-04 10:28:41 -0500 | [diff] [blame] | 700 | LastDColumn = 0; // column offsets don't carry cross lines. |
| Haojian Wu | 891f822 | 2019-12-09 17:00:51 +0100 | [diff] [blame] | 701 | Cost += abs(DLine - LastDLine) + abs(DColumn - LastDColumn); |
| 702 | std::tie(LastLine, LastDLine, LastDColumn) = std::tie(Line, DLine, DColumn); |
| 703 | } |
| 704 | return Cost; |
| 705 | } |
| 706 | |
| Sam McCall | c094912 | 2019-05-07 07:11:56 +0000 | [diff] [blame] | 707 | } // namespace clangd |
| 708 | } // namespace clang |