Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 1 | //===-- ChangeNamespace.cpp - Change namespace implementation -------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 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 |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | #include "ChangeNamespace.h" |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 9 | #include "clang/AST/ASTContext.h" |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 10 | #include "clang/Format/Format.h" |
| 11 | #include "clang/Lex/Lexer.h" |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 12 | #include "llvm/Support/Casting.h" |
Eric Liu | ff51f01 | 2016-11-16 16:54:53 | [diff] [blame] | 13 | #include "llvm/Support/ErrorHandling.h" |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace change_namespace { |
| 19 | |
| 20 | namespace { |
| 21 | |
Nathan James | 6a3b10e | 2020-07-09 10:29:49 | [diff] [blame] | 22 | inline std::string joinNamespaces(ArrayRef<StringRef> Namespaces) { |
| 23 | return llvm::join(Namespaces, "::"); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 24 | } |
| 25 | |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 26 | // Given "a::b::c", returns {"a", "b", "c"}. |
| 27 | llvm::SmallVector<llvm::StringRef, 4> splitSymbolName(llvm::StringRef Name) { |
| 28 | llvm::SmallVector<llvm::StringRef, 4> Splitted; |
| 29 | Name.split(Splitted, "::", /*MaxSplit=*/-1, |
| 30 | /*KeepEmpty=*/false); |
| 31 | return Splitted; |
| 32 | } |
| 33 | |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 34 | SourceLocation startLocationForType(TypeLoc TLoc) { |
| 35 | // For elaborated types (e.g. `struct a::A`) we want the portion after the |
| 36 | // `struct` but including the namespace qualifier, `a::`. |
| 37 | if (TLoc.getTypeLocClass() == TypeLoc::Elaborated) { |
| 38 | NestedNameSpecifierLoc NestedNameSpecifier = |
| 39 | TLoc.castAs<ElaboratedTypeLoc>().getQualifierLoc(); |
| 40 | if (NestedNameSpecifier.getNestedNameSpecifier()) |
| 41 | return NestedNameSpecifier.getBeginLoc(); |
| 42 | TLoc = TLoc.getNextTypeLoc(); |
| 43 | } |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 | [diff] [blame] | 44 | return TLoc.getBeginLoc(); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 45 | } |
| 46 | |
Eric Liu | c265b02 | 2016-12-01 17:25:55 | [diff] [blame] | 47 | SourceLocation endLocationForType(TypeLoc TLoc) { |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 48 | // Dig past any namespace or keyword qualifications. |
| 49 | while (TLoc.getTypeLocClass() == TypeLoc::Elaborated || |
| 50 | TLoc.getTypeLocClass() == TypeLoc::Qualified) |
| 51 | TLoc = TLoc.getNextTypeLoc(); |
| 52 | |
| 53 | // The location for template specializations (e.g. Foo<int>) includes the |
| 54 | // templated types in its location range. We want to restrict this to just |
| 55 | // before the `<` character. |
| 56 | if (TLoc.getTypeLocClass() == TypeLoc::TemplateSpecialization) |
| 57 | return TLoc.castAs<TemplateSpecializationTypeLoc>() |
| 58 | .getLAngleLoc() |
| 59 | .getLocWithOffset(-1); |
| 60 | return TLoc.getEndLoc(); |
| 61 | } |
| 62 | |
| 63 | // Returns the containing namespace of `InnerNs` by skipping `PartialNsName`. |
Eric Liu | 6aa9416 | 2016-11-10 18:29:01 | [diff] [blame] | 64 | // If the `InnerNs` does not have `PartialNsName` as suffix, or `PartialNsName` |
| 65 | // is empty, nullptr is returned. |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 66 | // For example, if `InnerNs` is "a::b::c" and `PartialNsName` is "b::c", then |
| 67 | // the NamespaceDecl of namespace "a" will be returned. |
| 68 | const NamespaceDecl *getOuterNamespace(const NamespaceDecl *InnerNs, |
| 69 | llvm::StringRef PartialNsName) { |
Eric Liu | 6aa9416 | 2016-11-10 18:29:01 | [diff] [blame] | 70 | if (!InnerNs || PartialNsName.empty()) |
| 71 | return nullptr; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 72 | const auto *CurrentContext = llvm::cast<DeclContext>(InnerNs); |
| 73 | const auto *CurrentNs = InnerNs; |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 74 | auto PartialNsNameSplitted = splitSymbolName(PartialNsName); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 75 | while (!PartialNsNameSplitted.empty()) { |
| 76 | // Get the inner-most namespace in CurrentContext. |
| 77 | while (CurrentContext && !llvm::isa<NamespaceDecl>(CurrentContext)) |
| 78 | CurrentContext = CurrentContext->getParent(); |
| 79 | if (!CurrentContext) |
| 80 | return nullptr; |
| 81 | CurrentNs = llvm::cast<NamespaceDecl>(CurrentContext); |
| 82 | if (PartialNsNameSplitted.back() != CurrentNs->getNameAsString()) |
| 83 | return nullptr; |
| 84 | PartialNsNameSplitted.pop_back(); |
| 85 | CurrentContext = CurrentContext->getParent(); |
| 86 | } |
| 87 | return CurrentNs; |
| 88 | } |
| 89 | |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 90 | static std::unique_ptr<Lexer> |
| 91 | getLexerStartingFromLoc(SourceLocation Loc, const SourceManager &SM, |
| 92 | const LangOptions &LangOpts) { |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 93 | if (Loc.isMacroID() && |
| 94 | !Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 95 | return nullptr; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 96 | // Break down the source location. |
| 97 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 98 | // Try to load the file buffer. |
| 99 | bool InvalidTemp = false; |
| 100 | llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp); |
| 101 | if (InvalidTemp) |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 102 | return nullptr; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 103 | |
| 104 | const char *TokBegin = File.data() + LocInfo.second; |
| 105 | // Lex from the start of the given location. |
Jonas Devlieghere | 1c705d9 | 2019-08-14 23:52:23 | [diff] [blame] | 106 | return std::make_unique<Lexer>(SM.getLocForStartOfFile(LocInfo.first), |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 107 | LangOpts, File.begin(), TokBegin, File.end()); |
| 108 | } |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 109 | |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 110 | // FIXME: get rid of this helper function if this is supported in clang-refactor |
| 111 | // library. |
| 112 | static SourceLocation getStartOfNextLine(SourceLocation Loc, |
| 113 | const SourceManager &SM, |
| 114 | const LangOptions &LangOpts) { |
| 115 | std::unique_ptr<Lexer> Lex = getLexerStartingFromLoc(Loc, SM, LangOpts); |
| 116 | if (!Lex.get()) |
| 117 | return SourceLocation(); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 118 | llvm::SmallVector<char, 16> Line; |
| 119 | // FIXME: this is a bit hacky to get ReadToEndOfLine work. |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 120 | Lex->setParsingPreprocessorDirective(true); |
| 121 | Lex->ReadToEndOfLine(&Line); |
Haojian Wu | ef8a6dc | 2016-10-04 10:35:53 | [diff] [blame] | 122 | auto End = Loc.getLocWithOffset(Line.size()); |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 123 | return SM.getLocForEndOfFile(SM.getDecomposedLoc(Loc).first) == End |
| 124 | ? End |
| 125 | : End.getLocWithOffset(1); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Returns `R` with new range that refers to code after `Replaces` being |
| 129 | // applied. |
| 130 | tooling::Replacement |
| 131 | getReplacementInChangedCode(const tooling::Replacements &Replaces, |
| 132 | const tooling::Replacement &R) { |
| 133 | unsigned NewStart = Replaces.getShiftedCodePosition(R.getOffset()); |
| 134 | unsigned NewEnd = |
| 135 | Replaces.getShiftedCodePosition(R.getOffset() + R.getLength()); |
| 136 | return tooling::Replacement(R.getFilePath(), NewStart, NewEnd - NewStart, |
| 137 | R.getReplacementText()); |
| 138 | } |
| 139 | |
| 140 | // Adds a replacement `R` into `Replaces` or merges it into `Replaces` by |
| 141 | // applying all existing Replaces first if there is conflict. |
| 142 | void addOrMergeReplacement(const tooling::Replacement &R, |
| 143 | tooling::Replacements *Replaces) { |
| 144 | auto Err = Replaces->add(R); |
| 145 | if (Err) { |
| 146 | llvm::consumeError(std::move(Err)); |
| 147 | auto Replace = getReplacementInChangedCode(*Replaces, R); |
| 148 | *Replaces = Replaces->merge(tooling::Replacements(Replace)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | tooling::Replacement createReplacement(SourceLocation Start, SourceLocation End, |
| 153 | llvm::StringRef ReplacementText, |
| 154 | const SourceManager &SM) { |
| 155 | if (!Start.isValid() || !End.isValid()) { |
| 156 | llvm::errs() << "start or end location were invalid\n"; |
| 157 | return tooling::Replacement(); |
| 158 | } |
| 159 | if (SM.getDecomposedLoc(Start).first != SM.getDecomposedLoc(End).first) { |
| 160 | llvm::errs() |
| 161 | << "start or end location were in different macro expansions\n"; |
| 162 | return tooling::Replacement(); |
| 163 | } |
| 164 | Start = SM.getSpellingLoc(Start); |
| 165 | End = SM.getSpellingLoc(End); |
| 166 | if (SM.getFileID(Start) != SM.getFileID(End)) { |
| 167 | llvm::errs() << "start or end location were in different files\n"; |
| 168 | return tooling::Replacement(); |
| 169 | } |
| 170 | return tooling::Replacement( |
| 171 | SM, CharSourceRange::getTokenRange(SM.getSpellingLoc(Start), |
| 172 | SM.getSpellingLoc(End)), |
| 173 | ReplacementText); |
| 174 | } |
| 175 | |
Eric Liu | 4fe99e1 | 2016-12-14 17:01:52 | [diff] [blame] | 176 | void addReplacementOrDie( |
| 177 | SourceLocation Start, SourceLocation End, llvm::StringRef ReplacementText, |
| 178 | const SourceManager &SM, |
| 179 | std::map<std::string, tooling::Replacements> *FileToReplacements) { |
| 180 | const auto R = createReplacement(Start, End, ReplacementText, SM); |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 181 | auto Err = (*FileToReplacements)[std::string(R.getFilePath())].add(R); |
Eric Liu | 4fe99e1 | 2016-12-14 17:01:52 | [diff] [blame] | 182 | if (Err) |
| 183 | llvm_unreachable(llvm::toString(std::move(Err)).c_str()); |
| 184 | } |
| 185 | |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 186 | tooling::Replacement createInsertion(SourceLocation Loc, |
| 187 | llvm::StringRef InsertText, |
| 188 | const SourceManager &SM) { |
| 189 | if (Loc.isInvalid()) { |
| 190 | llvm::errs() << "insert Location is invalid.\n"; |
| 191 | return tooling::Replacement(); |
| 192 | } |
| 193 | Loc = SM.getSpellingLoc(Loc); |
| 194 | return tooling::Replacement(SM, Loc, 0, InsertText); |
| 195 | } |
| 196 | |
| 197 | // Returns the shortest qualified name for declaration `DeclName` in the |
| 198 | // namespace `NsName`. For example, if `DeclName` is "a::b::X" and `NsName` |
| 199 | // is "a::c::d", then "b::X" will be returned. |
Eric Liu | bc71550 | 2017-01-26 15:08:44 | [diff] [blame] | 200 | // Note that if `DeclName` is `::b::X` and `NsName` is `::a::b`, this returns |
| 201 | // "::b::X" instead of "b::X" since there will be a name conflict otherwise. |
Eric Liu | 447164d | 2016-10-05 15:52:39 | [diff] [blame] | 202 | // \param DeclName A fully qualified name, "::a::b::X" or "a::b::X". |
| 203 | // \param NsName A fully qualified name, "::a::b" or "a::b". Global namespace |
| 204 | // will have empty name. |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 205 | std::string getShortestQualifiedNameInNamespace(llvm::StringRef DeclName, |
| 206 | llvm::StringRef NsName) { |
Eric Liu | 447164d | 2016-10-05 15:52:39 | [diff] [blame] | 207 | DeclName = DeclName.ltrim(':'); |
| 208 | NsName = NsName.ltrim(':'); |
Kazu Hirata | 20f0f15 | 2022-08-29 06:29:02 | [diff] [blame^] | 209 | if (!DeclName.contains(':')) |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 210 | return std::string(DeclName); |
Eric Liu | 447164d | 2016-10-05 15:52:39 | [diff] [blame] | 211 | |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 212 | auto NsNameSplitted = splitSymbolName(NsName); |
| 213 | auto DeclNsSplitted = splitSymbolName(DeclName); |
Eric Liu | bc71550 | 2017-01-26 15:08:44 | [diff] [blame] | 214 | llvm::StringRef UnqualifiedDeclName = DeclNsSplitted.pop_back_val(); |
| 215 | // If the Decl is in global namespace, there is no need to shorten it. |
| 216 | if (DeclNsSplitted.empty()) |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 217 | return std::string(UnqualifiedDeclName); |
Eric Liu | bc71550 | 2017-01-26 15:08:44 | [diff] [blame] | 218 | // If NsName is the global namespace, we can simply use the DeclName sans |
| 219 | // leading "::". |
| 220 | if (NsNameSplitted.empty()) |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 221 | return std::string(DeclName); |
Eric Liu | bc71550 | 2017-01-26 15:08:44 | [diff] [blame] | 222 | |
| 223 | if (NsNameSplitted.front() != DeclNsSplitted.front()) { |
| 224 | // The DeclName must be fully-qualified, but we still need to decide if a |
| 225 | // leading "::" is necessary. For example, if `NsName` is "a::b::c" and the |
| 226 | // `DeclName` is "b::X", then the reference must be qualified as "::b::X" |
| 227 | // to avoid conflict. |
| 228 | if (llvm::is_contained(NsNameSplitted, DeclNsSplitted.front())) |
| 229 | return ("::" + DeclName).str(); |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 230 | return std::string(DeclName); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 231 | } |
Eric Liu | bc71550 | 2017-01-26 15:08:44 | [diff] [blame] | 232 | // Since there is already an overlap namespace, we know that `DeclName` can be |
| 233 | // shortened, so we reduce the longest common prefix. |
| 234 | auto DeclI = DeclNsSplitted.begin(); |
| 235 | auto DeclE = DeclNsSplitted.end(); |
| 236 | auto NsI = NsNameSplitted.begin(); |
| 237 | auto NsE = NsNameSplitted.end(); |
| 238 | for (; DeclI != DeclE && NsI != NsE && *DeclI == *NsI; ++DeclI, ++NsI) { |
| 239 | } |
| 240 | return (DeclI == DeclE) |
| 241 | ? UnqualifiedDeclName.str() |
| 242 | : (llvm::join(DeclI, DeclE, "::") + "::" + UnqualifiedDeclName) |
| 243 | .str(); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | std::string wrapCodeInNamespace(StringRef NestedNs, std::string Code) { |
| 247 | if (Code.back() != '\n') |
| 248 | Code += "\n"; |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 249 | auto NsSplitted = splitSymbolName(NestedNs); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 250 | while (!NsSplitted.empty()) { |
| 251 | // FIXME: consider code style for comments. |
| 252 | Code = ("namespace " + NsSplitted.back() + " {\n" + Code + |
| 253 | "} // namespace " + NsSplitted.back() + "\n") |
| 254 | .str(); |
| 255 | NsSplitted.pop_back(); |
| 256 | } |
| 257 | return Code; |
| 258 | } |
| 259 | |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 260 | // Returns true if \p D is a nested DeclContext in \p Context |
| 261 | bool isNestedDeclContext(const DeclContext *D, const DeclContext *Context) { |
| 262 | while (D) { |
| 263 | if (D == Context) |
| 264 | return true; |
| 265 | D = D->getParent(); |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | // Returns true if \p D is visible at \p Loc with DeclContext \p DeclCtx. |
| 271 | bool isDeclVisibleAtLocation(const SourceManager &SM, const Decl *D, |
| 272 | const DeclContext *DeclCtx, SourceLocation Loc) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 | [diff] [blame] | 273 | SourceLocation DeclLoc = SM.getSpellingLoc(D->getBeginLoc()); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 274 | Loc = SM.getSpellingLoc(Loc); |
| 275 | return SM.isBeforeInTranslationUnit(DeclLoc, Loc) && |
| 276 | (SM.getFileID(DeclLoc) == SM.getFileID(Loc) && |
| 277 | isNestedDeclContext(DeclCtx, D->getDeclContext())); |
| 278 | } |
| 279 | |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 280 | // Given a qualified symbol name, returns true if the symbol will be |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 281 | // incorrectly qualified without leading "::". For example, a symbol |
| 282 | // "nx::ny::Foo" in namespace "na::nx::ny" without leading "::"; a symbol |
| 283 | // "util::X" in namespace "na" can potentially conflict with "na::util" (if this |
| 284 | // exists). |
| 285 | bool conflictInNamespace(const ASTContext &AST, llvm::StringRef QualifiedSymbol, |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 286 | llvm::StringRef Namespace) { |
| 287 | auto SymbolSplitted = splitSymbolName(QualifiedSymbol.trim(":")); |
| 288 | assert(!SymbolSplitted.empty()); |
| 289 | SymbolSplitted.pop_back(); // We are only interested in namespaces. |
| 290 | |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 291 | if (SymbolSplitted.size() >= 1 && !Namespace.empty()) { |
| 292 | auto SymbolTopNs = SymbolSplitted.front(); |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 293 | auto NsSplitted = splitSymbolName(Namespace.trim(":")); |
| 294 | assert(!NsSplitted.empty()); |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 295 | |
| 296 | auto LookupDecl = [&AST](const Decl &Scope, |
| 297 | llvm::StringRef Name) -> const NamedDecl * { |
| 298 | const auto *DC = llvm::dyn_cast<DeclContext>(&Scope); |
| 299 | if (!DC) |
| 300 | return nullptr; |
| 301 | auto LookupRes = DC->lookup(DeclarationName(&AST.Idents.get(Name))); |
| 302 | if (LookupRes.empty()) |
| 303 | return nullptr; |
| 304 | return LookupRes.front(); |
| 305 | }; |
| 306 | // We do not check the outermost namespace since it would not be a |
| 307 | // conflict if it equals to the symbol's outermost namespace and the |
| 308 | // symbol name would have been shortened. |
| 309 | const NamedDecl *Scope = |
| 310 | LookupDecl(*AST.getTranslationUnitDecl(), NsSplitted.front()); |
Kazu Hirata | 246bf08 | 2022-07-16 06:24:59 | [diff] [blame] | 311 | for (const auto &I : llvm::drop_begin(NsSplitted)) { |
| 312 | if (I == SymbolTopNs) // Handles "::ny" in "::nx::ny" case. |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 313 | return true; |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 314 | // Handles "::util" and "::nx::util" conflicts. |
| 315 | if (Scope) { |
| 316 | if (LookupDecl(*Scope, SymbolTopNs)) |
| 317 | return true; |
Kazu Hirata | 246bf08 | 2022-07-16 06:24:59 | [diff] [blame] | 318 | Scope = LookupDecl(*Scope, I); |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 319 | } |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 320 | } |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 321 | if (Scope && LookupDecl(*Scope, SymbolTopNs)) |
| 322 | return true; |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | |
Eric Liu | 284b97c | 2017-03-17 14:05:39 | [diff] [blame] | 327 | bool isTemplateParameter(TypeLoc Type) { |
| 328 | while (!Type.isNull()) { |
| 329 | if (Type.getTypeLocClass() == TypeLoc::SubstTemplateTypeParm) |
| 330 | return true; |
| 331 | Type = Type.getNextTypeLoc(); |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 336 | } // anonymous namespace |
| 337 | |
| 338 | ChangeNamespaceTool::ChangeNamespaceTool( |
| 339 | llvm::StringRef OldNs, llvm::StringRef NewNs, llvm::StringRef FilePattern, |
Eric Christopher | 25ed42f | 2020-06-20 06:01:42 | [diff] [blame] | 340 | llvm::ArrayRef<std::string> AllowedSymbolPatterns, |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 341 | std::map<std::string, tooling::Replacements> *FileToReplacements, |
| 342 | llvm::StringRef FallbackStyle) |
| 343 | : FallbackStyle(FallbackStyle), FileToReplacements(*FileToReplacements), |
| 344 | OldNamespace(OldNs.ltrim(':')), NewNamespace(NewNs.ltrim(':')), |
Eric Liu | c265b02 | 2016-12-01 17:25:55 | [diff] [blame] | 345 | FilePattern(FilePattern), FilePatternRE(FilePattern) { |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 346 | FileToReplacements->clear(); |
Eric Liu | 8bc2416 | 2017-03-21 12:41:59 | [diff] [blame] | 347 | auto OldNsSplitted = splitSymbolName(OldNamespace); |
| 348 | auto NewNsSplitted = splitSymbolName(NewNamespace); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 349 | // Calculates `DiffOldNamespace` and `DiffNewNamespace`. |
| 350 | while (!OldNsSplitted.empty() && !NewNsSplitted.empty() && |
| 351 | OldNsSplitted.front() == NewNsSplitted.front()) { |
| 352 | OldNsSplitted.erase(OldNsSplitted.begin()); |
| 353 | NewNsSplitted.erase(NewNsSplitted.begin()); |
| 354 | } |
| 355 | DiffOldNamespace = joinNamespaces(OldNsSplitted); |
| 356 | DiffNewNamespace = joinNamespaces(NewNsSplitted); |
Eric Liu | 7fccc99 | 2017-02-24 11:54:45 | [diff] [blame] | 357 | |
Eric Christopher | 25ed42f | 2020-06-20 06:01:42 | [diff] [blame] | 358 | for (const auto &Pattern : AllowedSymbolPatterns) |
| 359 | AllowedSymbolRegexes.emplace_back(Pattern); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 360 | } |
| 361 | |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 362 | void ChangeNamespaceTool::registerMatchers(ast_matchers::MatchFinder *Finder) { |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 363 | std::string FullOldNs = "::" + OldNamespace; |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 364 | // Prefix is the outer-most namespace in DiffOldNamespace. For example, if the |
| 365 | // OldNamespace is "a::b::c" and DiffOldNamespace is "b::c", then Prefix will |
| 366 | // be "a::b". Declarations in this namespace will not be visible in the new |
| 367 | // namespace. If DiffOldNamespace is empty, Prefix will be a invalid name "-". |
| 368 | llvm::SmallVector<llvm::StringRef, 4> DiffOldNsSplitted; |
Eric Liu | 2dd0e1b | 2016-12-05 11:17:04 | [diff] [blame] | 369 | llvm::StringRef(DiffOldNamespace) |
| 370 | .split(DiffOldNsSplitted, "::", /*MaxSplit=*/-1, |
| 371 | /*KeepEmpty=*/false); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 372 | std::string Prefix = "-"; |
| 373 | if (!DiffOldNsSplitted.empty()) |
| 374 | Prefix = (StringRef(FullOldNs).drop_back(DiffOldNamespace.size()) + |
| 375 | DiffOldNsSplitted.front()) |
| 376 | .str(); |
| 377 | auto IsInMovedNs = |
| 378 | allOf(hasAncestor(namespaceDecl(hasName(FullOldNs)).bind("ns_decl")), |
| 379 | isExpansionInFileMatching(FilePattern)); |
| 380 | auto IsVisibleInNewNs = anyOf( |
| 381 | IsInMovedNs, unless(hasAncestor(namespaceDecl(hasName(Prefix))))); |
| 382 | // Match using declarations. |
| 383 | Finder->addMatcher( |
| 384 | usingDecl(isExpansionInFileMatching(FilePattern), IsVisibleInNewNs) |
| 385 | .bind("using"), |
| 386 | this); |
| 387 | // Match using namespace declarations. |
| 388 | Finder->addMatcher(usingDirectiveDecl(isExpansionInFileMatching(FilePattern), |
| 389 | IsVisibleInNewNs) |
| 390 | .bind("using_namespace"), |
| 391 | this); |
Eric Liu | 180dac6 | 2016-12-23 10:47:09 | [diff] [blame] | 392 | // Match namespace alias declarations. |
| 393 | Finder->addMatcher(namespaceAliasDecl(isExpansionInFileMatching(FilePattern), |
| 394 | IsVisibleInNewNs) |
| 395 | .bind("namespace_alias"), |
| 396 | this); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 397 | |
| 398 | // Match old namespace blocks. |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 399 | Finder->addMatcher( |
| 400 | namespaceDecl(hasName(FullOldNs), isExpansionInFileMatching(FilePattern)) |
| 401 | .bind("old_ns"), |
| 402 | this); |
| 403 | |
Eric Liu | 41552d6 | 2016-12-07 14:20:52 | [diff] [blame] | 404 | // Match class forward-declarations in the old namespace. |
| 405 | // Note that forward-declarations in classes are not matched. |
| 406 | Finder->addMatcher(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())), |
| 407 | IsInMovedNs, hasParent(namespaceDecl())) |
| 408 | .bind("class_fwd_decl"), |
| 409 | this); |
| 410 | |
| 411 | // Match template class forward-declarations in the old namespace. |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 412 | Finder->addMatcher( |
Eric Liu | 41552d6 | 2016-12-07 14:20:52 | [diff] [blame] | 413 | classTemplateDecl(unless(hasDescendant(cxxRecordDecl(isDefinition()))), |
| 414 | IsInMovedNs, hasParent(namespaceDecl())) |
| 415 | .bind("template_class_fwd_decl"), |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 416 | this); |
| 417 | |
| 418 | // Match references to types that are not defined in the old namespace. |
| 419 | // Forward-declarations in the old namespace are also matched since they will |
| 420 | // be moved back to the old namespace. |
| 421 | auto DeclMatcher = namedDecl( |
| 422 | hasAncestor(namespaceDecl()), |
| 423 | unless(anyOf( |
Eric Liu | 912d039 | 2016-09-27 12:54:48 | [diff] [blame] | 424 | isImplicit(), hasAncestor(namespaceDecl(isAnonymous())), |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 425 | hasAncestor(cxxRecordDecl()), |
| 426 | allOf(IsInMovedNs, unless(cxxRecordDecl(unless(isDefinition()))))))); |
Eric Liu | 912d039 | 2016-09-27 12:54:48 | [diff] [blame] | 427 | |
Eric Liu | 8685c76 | 2016-12-07 17:04:07 | [diff] [blame] | 428 | // Using shadow declarations in classes always refers to base class, which |
| 429 | // does not need to be qualified since it can be inferred from inheritance. |
| 430 | // Note that this does not match using alias declarations. |
| 431 | auto UsingShadowDeclInClass = |
| 432 | usingDecl(hasAnyUsingShadowDecl(decl()), hasParent(cxxRecordDecl())); |
| 433 | |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 434 | // Match TypeLocs on the declaration. Carefully match only the outermost |
Eric Liu | 8393cb0 | 2016-10-31 08:28:29 | [diff] [blame] | 435 | // TypeLoc and template specialization arguments (which are not outermost) |
| 436 | // that are directly linked to types matching `DeclMatcher`. Nested name |
| 437 | // specifier locs are handled separately below. |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 438 | Finder->addMatcher( |
| 439 | typeLoc(IsInMovedNs, |
| 440 | loc(qualType(hasDeclaration(DeclMatcher.bind("from_decl")))), |
Eric Liu | 8393cb0 | 2016-10-31 08:28:29 | [diff] [blame] | 441 | unless(anyOf(hasParent(typeLoc(loc(qualType( |
Alexander Kornienko | 976e0c0 | 2018-11-25 02:41:01 | [diff] [blame] | 442 | hasDeclaration(DeclMatcher), |
| 443 | unless(templateSpecializationType()))))), |
Eric Liu | 8685c76 | 2016-12-07 17:04:07 | [diff] [blame] | 444 | hasParent(nestedNameSpecifierLoc()), |
Sam McCall | c8f1482 | 2021-08-06 20:28:31 | [diff] [blame] | 445 | hasAncestor(decl(isImplicit())), |
Eric Liu | b583a7e | 2017-10-16 08:20:10 | [diff] [blame] | 446 | hasAncestor(UsingShadowDeclInClass), |
| 447 | hasAncestor(functionDecl(isDefaulted())))), |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 448 | hasAncestor(decl().bind("dc"))) |
| 449 | .bind("type"), |
| 450 | this); |
Eric Liu | 912d039 | 2016-09-27 12:54:48 | [diff] [blame] | 451 | |
Eric Liu | 68765a8 | 2016-09-21 15:06:12 | [diff] [blame] | 452 | // Types in `UsingShadowDecl` is not matched by `typeLoc` above, so we need to |
| 453 | // special case it. |
Eric Liu | 8685c76 | 2016-12-07 17:04:07 | [diff] [blame] | 454 | // Since using declarations inside classes must have the base class in the |
| 455 | // nested name specifier, we leave it to the nested name specifier matcher. |
| 456 | Finder->addMatcher(usingDecl(IsInMovedNs, hasAnyUsingShadowDecl(decl()), |
| 457 | unless(UsingShadowDeclInClass)) |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 458 | .bind("using_with_shadow"), |
| 459 | this); |
Eric Liu | 912d039 | 2016-09-27 12:54:48 | [diff] [blame] | 460 | |
Eric Liu | ff51f01 | 2016-11-16 16:54:53 | [diff] [blame] | 461 | // Handle types in nested name specifier. Specifiers that are in a TypeLoc |
| 462 | // matched above are not matched, e.g. "A::" in "A::A" is not matched since |
| 463 | // "A::A" would have already been fixed. |
Eric Liu | 8685c76 | 2016-12-07 17:04:07 | [diff] [blame] | 464 | Finder->addMatcher( |
| 465 | nestedNameSpecifierLoc( |
| 466 | hasAncestor(decl(IsInMovedNs).bind("dc")), |
| 467 | loc(nestedNameSpecifier( |
| 468 | specifiesType(hasDeclaration(DeclMatcher.bind("from_decl"))))), |
Sam McCall | c8f1482 | 2021-08-06 20:28:31 | [diff] [blame] | 469 | unless(anyOf(hasAncestor(decl(isImplicit())), |
Eric Liu | 8685c76 | 2016-12-07 17:04:07 | [diff] [blame] | 470 | hasAncestor(UsingShadowDeclInClass), |
Eric Liu | b583a7e | 2017-10-16 08:20:10 | [diff] [blame] | 471 | hasAncestor(functionDecl(isDefaulted())), |
Eric Liu | 8685c76 | 2016-12-07 17:04:07 | [diff] [blame] | 472 | hasAncestor(typeLoc(loc(qualType(hasDeclaration( |
| 473 | decl(equalsBoundNode("from_decl")))))))))) |
| 474 | .bind("nested_specifier_loc"), |
| 475 | this); |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 476 | |
Eric Liu | ff51f01 | 2016-11-16 16:54:53 | [diff] [blame] | 477 | // Matches base class initializers in constructors. TypeLocs of base class |
| 478 | // initializers do not need to be fixed. For example, |
| 479 | // class X : public a::b::Y { |
| 480 | // public: |
| 481 | // X() : Y::Y() {} // Y::Y do not need namespace specifier. |
| 482 | // }; |
| 483 | Finder->addMatcher( |
| 484 | cxxCtorInitializer(isBaseInitializer()).bind("base_initializer"), this); |
| 485 | |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 486 | // Handle function. |
Eric Liu | 912d039 | 2016-09-27 12:54:48 | [diff] [blame] | 487 | // Only handle functions that are defined in a namespace excluding member |
| 488 | // function, static methods (qualified by nested specifier), and functions |
| 489 | // defined in the global namespace. |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 490 | // Note that the matcher does not exclude calls to out-of-line static method |
| 491 | // definitions, so we need to exclude them in the callback handler. |
Eric Liu | 912d039 | 2016-09-27 12:54:48 | [diff] [blame] | 492 | auto FuncMatcher = |
| 493 | functionDecl(unless(anyOf(cxxMethodDecl(), IsInMovedNs, |
| 494 | hasAncestor(namespaceDecl(isAnonymous())), |
| 495 | hasAncestor(cxxRecordDecl()))), |
| 496 | hasParent(namespaceDecl())); |
Alexander Kornienko | 976e0c0 | 2018-11-25 02:41:01 | [diff] [blame] | 497 | Finder->addMatcher(expr(hasAncestor(decl().bind("dc")), IsInMovedNs, |
Sam McCall | c8f1482 | 2021-08-06 20:28:31 | [diff] [blame] | 498 | unless(hasAncestor(decl(isImplicit()))), |
Alexander Kornienko | 976e0c0 | 2018-11-25 02:41:01 | [diff] [blame] | 499 | anyOf(callExpr(callee(FuncMatcher)).bind("call"), |
| 500 | declRefExpr(to(FuncMatcher.bind("func_decl"))) |
| 501 | .bind("func_ref"))), |
| 502 | this); |
Eric Liu | 159f013 | 2016-09-30 04:32:39 | [diff] [blame] | 503 | |
| 504 | auto GlobalVarMatcher = varDecl( |
| 505 | hasGlobalStorage(), hasParent(namespaceDecl()), |
| 506 | unless(anyOf(IsInMovedNs, hasAncestor(namespaceDecl(isAnonymous()))))); |
| 507 | Finder->addMatcher(declRefExpr(IsInMovedNs, hasAncestor(decl().bind("dc")), |
| 508 | to(GlobalVarMatcher.bind("var_decl"))) |
| 509 | .bind("var_ref"), |
| 510 | this); |
Eric Liu | 0325a77 | 2017-02-02 17:40:38 | [diff] [blame] | 511 | |
| 512 | // Handle unscoped enum constant. |
| 513 | auto UnscopedEnumMatcher = enumConstantDecl(hasParent(enumDecl( |
| 514 | hasParent(namespaceDecl()), |
| 515 | unless(anyOf(isScoped(), IsInMovedNs, hasAncestor(cxxRecordDecl()), |
| 516 | hasAncestor(namespaceDecl(isAnonymous()))))))); |
| 517 | Finder->addMatcher( |
| 518 | declRefExpr(IsInMovedNs, hasAncestor(decl().bind("dc")), |
| 519 | to(UnscopedEnumMatcher.bind("enum_const_decl"))) |
| 520 | .bind("enum_const_ref"), |
| 521 | this); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void ChangeNamespaceTool::run( |
| 525 | const ast_matchers::MatchFinder::MatchResult &Result) { |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 526 | if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) { |
| 527 | UsingDecls.insert(Using); |
| 528 | } else if (const auto *UsingNamespace = |
| 529 | Result.Nodes.getNodeAs<UsingDirectiveDecl>( |
| 530 | "using_namespace")) { |
| 531 | UsingNamespaceDecls.insert(UsingNamespace); |
Eric Liu | 180dac6 | 2016-12-23 10:47:09 | [diff] [blame] | 532 | } else if (const auto *NamespaceAlias = |
| 533 | Result.Nodes.getNodeAs<NamespaceAliasDecl>( |
| 534 | "namespace_alias")) { |
| 535 | NamespaceAliasDecls.insert(NamespaceAlias); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 536 | } else if (const auto *NsDecl = |
| 537 | Result.Nodes.getNodeAs<NamespaceDecl>("old_ns")) { |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 538 | moveOldNamespace(Result, NsDecl); |
| 539 | } else if (const auto *FwdDecl = |
Eric Liu | 41552d6 | 2016-12-07 14:20:52 | [diff] [blame] | 540 | Result.Nodes.getNodeAs<CXXRecordDecl>("class_fwd_decl")) { |
| 541 | moveClassForwardDeclaration(Result, cast<NamedDecl>(FwdDecl)); |
| 542 | } else if (const auto *TemplateFwdDecl = |
| 543 | Result.Nodes.getNodeAs<ClassTemplateDecl>( |
| 544 | "template_class_fwd_decl")) { |
| 545 | moveClassForwardDeclaration(Result, cast<NamedDecl>(TemplateFwdDecl)); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 546 | } else if (const auto *UsingWithShadow = |
| 547 | Result.Nodes.getNodeAs<UsingDecl>("using_with_shadow")) { |
| 548 | fixUsingShadowDecl(Result, UsingWithShadow); |
Eric Liu | 68765a8 | 2016-09-21 15:06:12 | [diff] [blame] | 549 | } else if (const auto *Specifier = |
| 550 | Result.Nodes.getNodeAs<NestedNameSpecifierLoc>( |
| 551 | "nested_specifier_loc")) { |
| 552 | SourceLocation Start = Specifier->getBeginLoc(); |
Eric Liu | c265b02 | 2016-12-01 17:25:55 | [diff] [blame] | 553 | SourceLocation End = endLocationForType(Specifier->getTypeLoc()); |
Eric Liu | 68765a8 | 2016-09-21 15:06:12 | [diff] [blame] | 554 | fixTypeLoc(Result, Start, End, Specifier->getTypeLoc()); |
Eric Liu | ff51f01 | 2016-11-16 16:54:53 | [diff] [blame] | 555 | } else if (const auto *BaseInitializer = |
| 556 | Result.Nodes.getNodeAs<CXXCtorInitializer>( |
| 557 | "base_initializer")) { |
| 558 | BaseCtorInitializerTypeLocs.push_back( |
| 559 | BaseInitializer->getTypeSourceInfo()->getTypeLoc()); |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 560 | } else if (const auto *TLoc = Result.Nodes.getNodeAs<TypeLoc>("type")) { |
Eric Liu | 26cf68a | 2016-12-15 10:42:35 | [diff] [blame] | 561 | // This avoids fixing types with record types as qualifier, which is not |
| 562 | // filtered by matchers in some cases, e.g. the type is templated. We should |
| 563 | // handle the record type qualifier instead. |
Eric Liu | 0c0aea0 | 2016-12-15 13:02:41 | [diff] [blame] | 564 | TypeLoc Loc = *TLoc; |
| 565 | while (Loc.getTypeLocClass() == TypeLoc::Qualified) |
| 566 | Loc = Loc.getNextTypeLoc(); |
| 567 | if (Loc.getTypeLocClass() == TypeLoc::Elaborated) { |
Eric Liu | 26cf68a | 2016-12-15 10:42:35 | [diff] [blame] | 568 | NestedNameSpecifierLoc NestedNameSpecifier = |
Eric Liu | 0c0aea0 | 2016-12-15 13:02:41 | [diff] [blame] | 569 | Loc.castAs<ElaboratedTypeLoc>().getQualifierLoc(); |
Matheus Izvekov | 15f3cd6 | 2021-10-11 16:15:36 | [diff] [blame] | 570 | // FIXME: avoid changing injected class names. |
| 571 | if (auto *NNS = NestedNameSpecifier.getNestedNameSpecifier()) { |
| 572 | const Type *SpecifierType = NNS->getAsType(); |
| 573 | if (SpecifierType && SpecifierType->isRecordType()) |
| 574 | return; |
| 575 | } |
Eric Liu | 26cf68a | 2016-12-15 10:42:35 | [diff] [blame] | 576 | } |
Eric Liu | 0c0aea0 | 2016-12-15 13:02:41 | [diff] [blame] | 577 | fixTypeLoc(Result, startLocationForType(Loc), endLocationForType(Loc), Loc); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 | [diff] [blame] | 578 | } else if (const auto *VarRef = |
| 579 | Result.Nodes.getNodeAs<DeclRefExpr>("var_ref")) { |
Eric Liu | 159f013 | 2016-09-30 04:32:39 | [diff] [blame] | 580 | const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var_decl"); |
| 581 | assert(Var); |
| 582 | if (Var->getCanonicalDecl()->isStaticDataMember()) |
| 583 | return; |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 584 | const auto *Context = Result.Nodes.getNodeAs<Decl>("dc"); |
Eric Liu | 159f013 | 2016-09-30 04:32:39 | [diff] [blame] | 585 | assert(Context && "Empty decl context."); |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 586 | fixDeclRefExpr(Result, Context->getDeclContext(), |
| 587 | llvm::cast<NamedDecl>(Var), VarRef); |
Eric Liu | 0325a77 | 2017-02-02 17:40:38 | [diff] [blame] | 588 | } else if (const auto *EnumConstRef = |
| 589 | Result.Nodes.getNodeAs<DeclRefExpr>("enum_const_ref")) { |
| 590 | // Do not rename the reference if it is already scoped by the EnumDecl name. |
| 591 | if (EnumConstRef->hasQualifier() && |
Eric Liu | 28c30ce | 2017-02-02 19:46:12 | [diff] [blame] | 592 | EnumConstRef->getQualifier()->getKind() == |
| 593 | NestedNameSpecifier::SpecifierKind::TypeSpec && |
Eric Liu | 0325a77 | 2017-02-02 17:40:38 | [diff] [blame] | 594 | EnumConstRef->getQualifier()->getAsType()->isEnumeralType()) |
| 595 | return; |
| 596 | const auto *EnumConstDecl = |
| 597 | Result.Nodes.getNodeAs<EnumConstantDecl>("enum_const_decl"); |
| 598 | assert(EnumConstDecl); |
| 599 | const auto *Context = Result.Nodes.getNodeAs<Decl>("dc"); |
| 600 | assert(Context && "Empty decl context."); |
| 601 | // FIXME: this would qualify "ns::VALUE" as "ns::EnumValue::VALUE". Fix it |
| 602 | // if it turns out to be an issue. |
| 603 | fixDeclRefExpr(Result, Context->getDeclContext(), |
| 604 | llvm::cast<NamedDecl>(EnumConstDecl), EnumConstRef); |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 605 | } else if (const auto *FuncRef = |
| 606 | Result.Nodes.getNodeAs<DeclRefExpr>("func_ref")) { |
Eric Liu | e3f35e4 | 2016-12-20 14:39:04 | [diff] [blame] | 607 | // If this reference has been processed as a function call, we do not |
| 608 | // process it again. |
| 609 | if (ProcessedFuncRefs.count(FuncRef)) |
| 610 | return; |
| 611 | ProcessedFuncRefs.insert(FuncRef); |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 612 | const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func_decl"); |
| 613 | assert(Func); |
| 614 | const auto *Context = Result.Nodes.getNodeAs<Decl>("dc"); |
| 615 | assert(Context && "Empty decl context."); |
| 616 | fixDeclRefExpr(Result, Context->getDeclContext(), |
| 617 | llvm::cast<NamedDecl>(Func), FuncRef); |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 618 | } else { |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 619 | const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call"); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 | [diff] [blame] | 620 | assert(Call != nullptr && "Expecting callback for CallExpr."); |
Eric Liu | e3f35e4 | 2016-12-20 14:39:04 | [diff] [blame] | 621 | const auto *CalleeFuncRef = |
| 622 | llvm::cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit()); |
| 623 | ProcessedFuncRefs.insert(CalleeFuncRef); |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 624 | const FunctionDecl *Func = Call->getDirectCallee(); |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 625 | assert(Func != nullptr); |
Eric Liu | e3f35e4 | 2016-12-20 14:39:04 | [diff] [blame] | 626 | // FIXME: ignore overloaded operators. This would miss cases where operators |
| 627 | // are called by qualified names (i.e. "ns::operator <"). Ignore such |
| 628 | // cases for now. |
| 629 | if (Func->isOverloadedOperator()) |
| 630 | return; |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 631 | // Ignore out-of-line static methods since they will be handled by nested |
| 632 | // name specifiers. |
Thorsten Schütt | 2fd11e0 | 2021-01-04 22:17:45 | [diff] [blame] | 633 | if (Func->getCanonicalDecl()->getStorageClass() == |
| 634 | StorageClass::SC_Static && |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 635 | Func->isOutOfLine()) |
| 636 | return; |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 637 | const auto *Context = Result.Nodes.getNodeAs<Decl>("dc"); |
Eric Liu | 12068d8 | 2016-09-22 11:54:00 | [diff] [blame] | 638 | assert(Context && "Empty decl context."); |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 639 | SourceRange CalleeRange = Call->getCallee()->getSourceRange(); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 640 | replaceQualifiedSymbolInDeclContext( |
| 641 | Result, Context->getDeclContext(), CalleeRange.getBegin(), |
Eric Liu | 231c655 | 2016-11-10 18:15:34 | [diff] [blame] | 642 | CalleeRange.getEnd(), llvm::cast<NamedDecl>(Func)); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 646 | static SourceLocation getLocAfterNamespaceLBrace(const NamespaceDecl *NsDecl, |
| 647 | const SourceManager &SM, |
| 648 | const LangOptions &LangOpts) { |
| 649 | std::unique_ptr<Lexer> Lex = |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 | [diff] [blame] | 650 | getLexerStartingFromLoc(NsDecl->getBeginLoc(), SM, LangOpts); |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 651 | assert(Lex.get() && |
| 652 | "Failed to create lexer from the beginning of namespace."); |
| 653 | if (!Lex.get()) |
| 654 | return SourceLocation(); |
| 655 | Token Tok; |
| 656 | while (!Lex->LexFromRawLexer(Tok) && Tok.isNot(tok::TokenKind::l_brace)) { |
| 657 | } |
| 658 | return Tok.isNot(tok::TokenKind::l_brace) |
| 659 | ? SourceLocation() |
| 660 | : Tok.getEndLoc().getLocWithOffset(1); |
| 661 | } |
| 662 | |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 663 | // Stores information about a moved namespace in `MoveNamespaces` and leaves |
| 664 | // the actual movement to `onEndOfTranslationUnit()`. |
| 665 | void ChangeNamespaceTool::moveOldNamespace( |
| 666 | const ast_matchers::MatchFinder::MatchResult &Result, |
| 667 | const NamespaceDecl *NsDecl) { |
| 668 | // If the namespace is empty, do nothing. |
| 669 | if (Decl::castToDeclContext(NsDecl)->decls_empty()) |
| 670 | return; |
| 671 | |
Eric Liu | ee5104b | 2017-01-04 14:49:08 | [diff] [blame] | 672 | const SourceManager &SM = *Result.SourceManager; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 673 | // Get the range of the code in the old namespace. |
Eric Liu | ee5104b | 2017-01-04 14:49:08 | [diff] [blame] | 674 | SourceLocation Start = |
| 675 | getLocAfterNamespaceLBrace(NsDecl, SM, Result.Context->getLangOpts()); |
Eric Liu | 73f49fd | 2016-10-12 12:34:18 | [diff] [blame] | 676 | assert(Start.isValid() && "Can't find l_brace for namespace."); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 677 | MoveNamespace MoveNs; |
Eric Liu | ee5104b | 2017-01-04 14:49:08 | [diff] [blame] | 678 | MoveNs.Offset = SM.getFileOffset(Start); |
| 679 | // The range of the moved namespace is from the location just past the left |
| 680 | // brace to the location right before the right brace. |
| 681 | MoveNs.Length = SM.getFileOffset(NsDecl->getRBraceLoc()) - MoveNs.Offset; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 682 | |
| 683 | // Insert the new namespace after `DiffOldNamespace`. For example, if |
| 684 | // `OldNamespace` is "a::b::c" and `NewNamespace` is `a::x::y`, then |
| 685 | // "x::y" will be inserted inside the existing namespace "a" and after "a::b". |
| 686 | // `OuterNs` is the first namespace in `DiffOldNamespace`, e.g. "namespace b" |
| 687 | // in the above example. |
Eric Liu | 6aa9416 | 2016-11-10 18:29:01 | [diff] [blame] | 688 | // If there is no outer namespace (i.e. DiffOldNamespace is empty), the new |
| 689 | // namespace will be a nested namespace in the old namespace. |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 690 | const NamespaceDecl *OuterNs = getOuterNamespace(NsDecl, DiffOldNamespace); |
Eric Liu | 6aa9416 | 2016-11-10 18:29:01 | [diff] [blame] | 691 | SourceLocation InsertionLoc = Start; |
| 692 | if (OuterNs) { |
Eric Liu | ee5104b | 2017-01-04 14:49:08 | [diff] [blame] | 693 | SourceLocation LocAfterNs = getStartOfNextLine( |
| 694 | OuterNs->getRBraceLoc(), SM, Result.Context->getLangOpts()); |
Eric Liu | 6aa9416 | 2016-11-10 18:29:01 | [diff] [blame] | 695 | assert(LocAfterNs.isValid() && |
| 696 | "Failed to get location after DiffOldNamespace"); |
| 697 | InsertionLoc = LocAfterNs; |
| 698 | } |
Eric Liu | ee5104b | 2017-01-04 14:49:08 | [diff] [blame] | 699 | MoveNs.InsertionOffset = SM.getFileOffset(SM.getSpellingLoc(InsertionLoc)); |
| 700 | MoveNs.FID = SM.getFileID(Start); |
Eric Liu | cc83c66 | 2016-09-19 17:58:59 | [diff] [blame] | 701 | MoveNs.SourceMgr = Result.SourceManager; |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 702 | MoveNamespaces[std::string(SM.getFilename(Start))].push_back(MoveNs); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // Removes a class forward declaration from the code in the moved namespace and |
| 706 | // creates an `InsertForwardDeclaration` to insert the forward declaration back |
| 707 | // into the old namespace after moving code from the old namespace to the new |
| 708 | // namespace. |
| 709 | // For example, changing "a" to "x": |
| 710 | // Old code: |
| 711 | // namespace a { |
| 712 | // class FWD; |
| 713 | // class A { FWD *fwd; } |
| 714 | // } // a |
| 715 | // New code: |
| 716 | // namespace a { |
| 717 | // class FWD; |
| 718 | // } // a |
| 719 | // namespace x { |
| 720 | // class A { a::FWD *fwd; } |
| 721 | // } // x |
| 722 | void ChangeNamespaceTool::moveClassForwardDeclaration( |
| 723 | const ast_matchers::MatchFinder::MatchResult &Result, |
Eric Liu | 41552d6 | 2016-12-07 14:20:52 | [diff] [blame] | 724 | const NamedDecl *FwdDecl) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 | [diff] [blame] | 725 | SourceLocation Start = FwdDecl->getBeginLoc(); |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 | [diff] [blame] | 726 | SourceLocation End = FwdDecl->getEndLoc(); |
Eric Liu | 4136715 | 2017-03-01 10:29:39 | [diff] [blame] | 727 | const SourceManager &SM = *Result.SourceManager; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 728 | SourceLocation AfterSemi = Lexer::findLocationAfterToken( |
Eric Liu | 4136715 | 2017-03-01 10:29:39 | [diff] [blame] | 729 | End, tok::semi, SM, Result.Context->getLangOpts(), |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 730 | /*SkipTrailingWhitespaceAndNewLine=*/true); |
| 731 | if (AfterSemi.isValid()) |
| 732 | End = AfterSemi.getLocWithOffset(-1); |
| 733 | // Delete the forward declaration from the code to be moved. |
Eric Liu | 4136715 | 2017-03-01 10:29:39 | [diff] [blame] | 734 | addReplacementOrDie(Start, End, "", SM, &FileToReplacements); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 735 | llvm::StringRef Code = Lexer::getSourceText( |
Eric Liu | 4136715 | 2017-03-01 10:29:39 | [diff] [blame] | 736 | CharSourceRange::getTokenRange(SM.getSpellingLoc(Start), |
| 737 | SM.getSpellingLoc(End)), |
| 738 | SM, Result.Context->getLangOpts()); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 739 | // Insert the forward declaration back into the old namespace after moving the |
| 740 | // code from old namespace to new namespace. |
| 741 | // Insertion information is stored in `InsertFwdDecls` and actual |
| 742 | // insertion will be performed in `onEndOfTranslationUnit`. |
| 743 | // Get the (old) namespace that contains the forward declaration. |
| 744 | const auto *NsDecl = Result.Nodes.getNodeAs<NamespaceDecl>("ns_decl"); |
| 745 | // The namespace contains the forward declaration, so it must not be empty. |
| 746 | assert(!NsDecl->decls_empty()); |
Eric Liu | 4136715 | 2017-03-01 10:29:39 | [diff] [blame] | 747 | const auto Insertion = createInsertion( |
| 748 | getLocAfterNamespaceLBrace(NsDecl, SM, Result.Context->getLangOpts()), |
| 749 | Code, SM); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 750 | InsertForwardDeclaration InsertFwd; |
| 751 | InsertFwd.InsertionOffset = Insertion.getOffset(); |
| 752 | InsertFwd.ForwardDeclText = Insertion.getReplacementText().str(); |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 753 | InsertFwdDecls[std::string(Insertion.getFilePath())].push_back(InsertFwd); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 754 | } |
| 755 | |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 756 | // Replaces a qualified symbol (in \p DeclCtx) that refers to a declaration \p |
| 757 | // FromDecl with the shortest qualified name possible when the reference is in |
| 758 | // `NewNamespace`. |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 759 | void ChangeNamespaceTool::replaceQualifiedSymbolInDeclContext( |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 760 | const ast_matchers::MatchFinder::MatchResult &Result, |
| 761 | const DeclContext *DeclCtx, SourceLocation Start, SourceLocation End, |
| 762 | const NamedDecl *FromDecl) { |
| 763 | const auto *NsDeclContext = DeclCtx->getEnclosingNamespaceContext(); |
Eric Liu | 4fe99e1 | 2016-12-14 17:01:52 | [diff] [blame] | 764 | if (llvm::isa<TranslationUnitDecl>(NsDeclContext)) { |
| 765 | // This should not happen in usual unless the TypeLoc is in function type |
| 766 | // parameters, e.g `std::function<void(T)>`. In this case, DeclContext of |
| 767 | // `T` will be the translation unit. We simply use fully-qualified name |
| 768 | // here. |
| 769 | // Note that `FromDecl` must not be defined in the old namespace (according |
| 770 | // to `DeclMatcher`), so its fully-qualified name will not change after |
| 771 | // changing the namespace. |
| 772 | addReplacementOrDie(Start, End, FromDecl->getQualifiedNameAsString(), |
| 773 | *Result.SourceManager, &FileToReplacements); |
| 774 | return; |
| 775 | } |
Eric Liu | 231c655 | 2016-11-10 18:15:34 | [diff] [blame] | 776 | const auto *NsDecl = llvm::cast<NamespaceDecl>(NsDeclContext); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 777 | // Calculate the name of the `NsDecl` after it is moved to new namespace. |
| 778 | std::string OldNs = NsDecl->getQualifiedNameAsString(); |
| 779 | llvm::StringRef Postfix = OldNs; |
| 780 | bool Consumed = Postfix.consume_front(OldNamespace); |
| 781 | assert(Consumed && "Expect OldNS to start with OldNamespace."); |
| 782 | (void)Consumed; |
| 783 | const std::string NewNs = (NewNamespace + Postfix).str(); |
| 784 | |
| 785 | llvm::StringRef NestedName = Lexer::getSourceText( |
| 786 | CharSourceRange::getTokenRange( |
| 787 | Result.SourceManager->getSpellingLoc(Start), |
| 788 | Result.SourceManager->getSpellingLoc(End)), |
| 789 | *Result.SourceManager, Result.Context->getLangOpts()); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 790 | std::string FromDeclName = FromDecl->getQualifiedNameAsString(); |
Eric Christopher | 25ed42f | 2020-06-20 06:01:42 | [diff] [blame] | 791 | for (llvm::Regex &RE : AllowedSymbolRegexes) |
Eric Liu | 7fccc99 | 2017-02-24 11:54:45 | [diff] [blame] | 792 | if (RE.match(FromDeclName)) |
| 793 | return; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 794 | std::string ReplaceName = |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 795 | getShortestQualifiedNameInNamespace(FromDeclName, NewNs); |
| 796 | // Checks if there is any using namespace declarations that can shorten the |
| 797 | // qualified name. |
| 798 | for (const auto *UsingNamespace : UsingNamespaceDecls) { |
| 799 | if (!isDeclVisibleAtLocation(*Result.SourceManager, UsingNamespace, DeclCtx, |
| 800 | Start)) |
| 801 | continue; |
| 802 | StringRef FromDeclNameRef = FromDeclName; |
| 803 | if (FromDeclNameRef.consume_front(UsingNamespace->getNominatedNamespace() |
| 804 | ->getQualifiedNameAsString())) { |
| 805 | FromDeclNameRef = FromDeclNameRef.drop_front(2); |
| 806 | if (FromDeclNameRef.size() < ReplaceName.size()) |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 807 | ReplaceName = std::string(FromDeclNameRef); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 808 | } |
| 809 | } |
Eric Liu | 180dac6 | 2016-12-23 10:47:09 | [diff] [blame] | 810 | // Checks if there is any namespace alias declarations that can shorten the |
| 811 | // qualified name. |
| 812 | for (const auto *NamespaceAlias : NamespaceAliasDecls) { |
| 813 | if (!isDeclVisibleAtLocation(*Result.SourceManager, NamespaceAlias, DeclCtx, |
| 814 | Start)) |
| 815 | continue; |
| 816 | StringRef FromDeclNameRef = FromDeclName; |
| 817 | if (FromDeclNameRef.consume_front( |
| 818 | NamespaceAlias->getNamespace()->getQualifiedNameAsString() + |
| 819 | "::")) { |
| 820 | std::string AliasName = NamespaceAlias->getNameAsString(); |
| 821 | std::string AliasQualifiedName = |
| 822 | NamespaceAlias->getQualifiedNameAsString(); |
Kazuaki Ishizaki | dd5571d | 2020-04-05 06:28:11 | [diff] [blame] | 823 | // We only consider namespace aliases define in the global namespace or |
Eric Liu | 180dac6 | 2016-12-23 10:47:09 | [diff] [blame] | 824 | // in namespaces that are directly visible from the reference, i.e. |
| 825 | // ancestor of the `OldNs`. Note that declarations in ancestor namespaces |
| 826 | // but not visible in the new namespace is filtered out by |
| 827 | // "IsVisibleInNewNs" matcher. |
| 828 | if (AliasQualifiedName != AliasName) { |
| 829 | // The alias is defined in some namespace. |
| 830 | assert(StringRef(AliasQualifiedName).endswith("::" + AliasName)); |
| 831 | llvm::StringRef AliasNs = |
| 832 | StringRef(AliasQualifiedName).drop_back(AliasName.size() + 2); |
| 833 | if (!llvm::StringRef(OldNs).startswith(AliasNs)) |
| 834 | continue; |
| 835 | } |
| 836 | std::string NameWithAliasNamespace = |
| 837 | (AliasName + "::" + FromDeclNameRef).str(); |
| 838 | if (NameWithAliasNamespace.size() < ReplaceName.size()) |
| 839 | ReplaceName = NameWithAliasNamespace; |
| 840 | } |
| 841 | } |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 842 | // Checks if there is any using shadow declarations that can shorten the |
| 843 | // qualified name. |
| 844 | bool Matched = false; |
| 845 | for (const UsingDecl *Using : UsingDecls) { |
| 846 | if (Matched) |
| 847 | break; |
| 848 | if (isDeclVisibleAtLocation(*Result.SourceManager, Using, DeclCtx, Start)) { |
| 849 | for (const auto *UsingShadow : Using->shadows()) { |
| 850 | const auto *TargetDecl = UsingShadow->getTargetDecl(); |
Eric Liu | ae7de71 | 2017-02-02 15:29:54 | [diff] [blame] | 851 | if (TargetDecl->getQualifiedNameAsString() == |
| 852 | FromDecl->getQualifiedNameAsString()) { |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 853 | ReplaceName = FromDecl->getNameAsString(); |
| 854 | Matched = true; |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 860 | bool Conflict = conflictInNamespace(DeclCtx->getParentASTContext(), |
| 861 | ReplaceName, NewNamespace); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 862 | // If the new nested name in the new namespace is the same as it was in the |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 863 | // old namespace, we don't create replacement unless there can be ambiguity. |
| 864 | if ((NestedName == ReplaceName && !Conflict) || |
Eric Liu | 9122916 | 2017-01-26 16:31:32 | [diff] [blame] | 865 | (NestedName.startswith("::") && NestedName.drop_front(2) == ReplaceName)) |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 866 | return; |
Eric Liu | 97f87ad | 2016-12-07 20:08:02 | [diff] [blame] | 867 | // If the reference need to be fully-qualified, add a leading "::" unless |
| 868 | // NewNamespace is the global namespace. |
Eric Liu | c6c894b | 2018-10-22 12:48:49 | [diff] [blame] | 869 | if (ReplaceName == FromDeclName && !NewNamespace.empty() && Conflict) |
Eric Liu | 97f87ad | 2016-12-07 20:08:02 | [diff] [blame] | 870 | ReplaceName = "::" + ReplaceName; |
Eric Liu | 4fe99e1 | 2016-12-14 17:01:52 | [diff] [blame] | 871 | addReplacementOrDie(Start, End, ReplaceName, *Result.SourceManager, |
| 872 | &FileToReplacements); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | // Replace the [Start, End] of `Type` with the shortest qualified name when the |
| 876 | // `Type` is in `NewNamespace`. |
| 877 | void ChangeNamespaceTool::fixTypeLoc( |
| 878 | const ast_matchers::MatchFinder::MatchResult &Result, SourceLocation Start, |
| 879 | SourceLocation End, TypeLoc Type) { |
| 880 | // FIXME: do not rename template parameter. |
| 881 | if (Start.isInvalid() || End.isInvalid()) |
| 882 | return; |
Eric Liu | ff51f01 | 2016-11-16 16:54:53 | [diff] [blame] | 883 | // Types of CXXCtorInitializers do not need to be fixed. |
| 884 | if (llvm::is_contained(BaseCtorInitializerTypeLocs, Type)) |
| 885 | return; |
Eric Liu | 284b97c | 2017-03-17 14:05:39 | [diff] [blame] | 886 | if (isTemplateParameter(Type)) |
| 887 | return; |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 888 | // The declaration which this TypeLoc refers to. |
| 889 | const auto *FromDecl = Result.Nodes.getNodeAs<NamedDecl>("from_decl"); |
| 890 | // `hasDeclaration` gives underlying declaration, but if the type is |
| 891 | // a typedef type, we need to use the typedef type instead. |
Eric Liu | 26cf68a | 2016-12-15 10:42:35 | [diff] [blame] | 892 | auto IsInMovedNs = [&](const NamedDecl *D) { |
| 893 | if (!llvm::StringRef(D->getQualifiedNameAsString()) |
| 894 | .startswith(OldNamespace + "::")) |
| 895 | return false; |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 | [diff] [blame] | 896 | auto ExpansionLoc = Result.SourceManager->getExpansionLoc(D->getBeginLoc()); |
Eric Liu | 26cf68a | 2016-12-15 10:42:35 | [diff] [blame] | 897 | if (ExpansionLoc.isInvalid()) |
| 898 | return false; |
| 899 | llvm::StringRef Filename = Result.SourceManager->getFilename(ExpansionLoc); |
| 900 | return FilePatternRE.match(Filename); |
| 901 | }; |
| 902 | // Make `FromDecl` the immediate declaration that `Type` refers to, i.e. if |
| 903 | // `Type` is an alias type, we make `FromDecl` the type alias declaration. |
| 904 | // Also, don't fix the \p Type if it refers to a type alias decl in the moved |
| 905 | // namespace since the alias decl will be moved along with the type reference. |
Eric Liu | 3215886 | 2016-11-14 19:37:55 | [diff] [blame] | 906 | if (auto *Typedef = Type.getType()->getAs<TypedefType>()) { |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 907 | FromDecl = Typedef->getDecl(); |
Eric Liu | 3215886 | 2016-11-14 19:37:55 | [diff] [blame] | 908 | if (IsInMovedNs(FromDecl)) |
| 909 | return; |
Eric Liu | 26cf68a | 2016-12-15 10:42:35 | [diff] [blame] | 910 | } else if (auto *TemplateType = |
| 911 | Type.getType()->getAs<TemplateSpecializationType>()) { |
| 912 | if (TemplateType->isTypeAlias()) { |
| 913 | FromDecl = TemplateType->getTemplateName().getAsTemplateDecl(); |
| 914 | if (IsInMovedNs(FromDecl)) |
| 915 | return; |
| 916 | } |
Eric Liu | 3215886 | 2016-11-14 19:37:55 | [diff] [blame] | 917 | } |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 | [diff] [blame] | 918 | const auto *DeclCtx = Result.Nodes.getNodeAs<Decl>("dc"); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 919 | assert(DeclCtx && "Empty decl context."); |
Eric Liu | b9bf1b5 | 2016-11-08 22:44:17 | [diff] [blame] | 920 | replaceQualifiedSymbolInDeclContext(Result, DeclCtx->getDeclContext(), Start, |
| 921 | End, FromDecl); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 922 | } |
| 923 | |
Eric Liu | 68765a8 | 2016-09-21 15:06:12 | [diff] [blame] | 924 | void ChangeNamespaceTool::fixUsingShadowDecl( |
| 925 | const ast_matchers::MatchFinder::MatchResult &Result, |
| 926 | const UsingDecl *UsingDeclaration) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 | [diff] [blame] | 927 | SourceLocation Start = UsingDeclaration->getBeginLoc(); |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 | [diff] [blame] | 928 | SourceLocation End = UsingDeclaration->getEndLoc(); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 | [diff] [blame] | 929 | if (Start.isInvalid() || End.isInvalid()) |
| 930 | return; |
Eric Liu | 68765a8 | 2016-09-21 15:06:12 | [diff] [blame] | 931 | |
| 932 | assert(UsingDeclaration->shadow_size() > 0); |
| 933 | // FIXME: it might not be always accurate to use the first using-decl. |
| 934 | const NamedDecl *TargetDecl = |
| 935 | UsingDeclaration->shadow_begin()->getTargetDecl(); |
| 936 | std::string TargetDeclName = TargetDecl->getQualifiedNameAsString(); |
| 937 | // FIXME: check if target_decl_name is in moved ns, which doesn't make much |
| 938 | // sense. If this happens, we need to use name with the new namespace. |
| 939 | // Use fully qualified name in UsingDecl for now. |
Eric Liu | 4fe99e1 | 2016-12-14 17:01:52 | [diff] [blame] | 940 | addReplacementOrDie(Start, End, "using ::" + TargetDeclName, |
| 941 | *Result.SourceManager, &FileToReplacements); |
Eric Liu | 68765a8 | 2016-09-21 15:06:12 | [diff] [blame] | 942 | } |
| 943 | |
Eric Liu | da22b3c | 2016-11-29 14:15:14 | [diff] [blame] | 944 | void ChangeNamespaceTool::fixDeclRefExpr( |
| 945 | const ast_matchers::MatchFinder::MatchResult &Result, |
| 946 | const DeclContext *UseContext, const NamedDecl *From, |
| 947 | const DeclRefExpr *Ref) { |
| 948 | SourceRange RefRange = Ref->getSourceRange(); |
| 949 | replaceQualifiedSymbolInDeclContext(Result, UseContext, RefRange.getBegin(), |
| 950 | RefRange.getEnd(), From); |
| 951 | } |
| 952 | |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 953 | void ChangeNamespaceTool::onEndOfTranslationUnit() { |
| 954 | // Move namespace blocks and insert forward declaration to old namespace. |
| 955 | for (const auto &FileAndNsMoves : MoveNamespaces) { |
| 956 | auto &NsMoves = FileAndNsMoves.second; |
| 957 | if (NsMoves.empty()) |
| 958 | continue; |
| 959 | const std::string &FilePath = FileAndNsMoves.first; |
| 960 | auto &Replaces = FileToReplacements[FilePath]; |
Eric Liu | cc83c66 | 2016-09-19 17:58:59 | [diff] [blame] | 961 | auto &SM = *NsMoves.begin()->SourceMgr; |
| 962 | llvm::StringRef Code = SM.getBufferData(NsMoves.begin()->FID); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 963 | auto ChangedCode = tooling::applyAllReplacements(Code, Replaces); |
| 964 | if (!ChangedCode) { |
| 965 | llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; |
| 966 | continue; |
| 967 | } |
| 968 | // Replacements on the changed code for moving namespaces and inserting |
| 969 | // forward declarations to old namespaces. |
| 970 | tooling::Replacements NewReplacements; |
| 971 | // Cut the changed code from the old namespace and paste the code in the new |
| 972 | // namespace. |
| 973 | for (const auto &NsMove : NsMoves) { |
| 974 | // Calculate the range of the old namespace block in the changed |
| 975 | // code. |
| 976 | const unsigned NewOffset = Replaces.getShiftedCodePosition(NsMove.Offset); |
| 977 | const unsigned NewLength = |
| 978 | Replaces.getShiftedCodePosition(NsMove.Offset + NsMove.Length) - |
| 979 | NewOffset; |
| 980 | tooling::Replacement Deletion(FilePath, NewOffset, NewLength, ""); |
| 981 | std::string MovedCode = ChangedCode->substr(NewOffset, NewLength); |
| 982 | std::string MovedCodeWrappedInNewNs = |
| 983 | wrapCodeInNamespace(DiffNewNamespace, MovedCode); |
| 984 | // Calculate the new offset at which the code will be inserted in the |
| 985 | // changed code. |
| 986 | unsigned NewInsertionOffset = |
| 987 | Replaces.getShiftedCodePosition(NsMove.InsertionOffset); |
| 988 | tooling::Replacement Insertion(FilePath, NewInsertionOffset, 0, |
| 989 | MovedCodeWrappedInNewNs); |
| 990 | addOrMergeReplacement(Deletion, &NewReplacements); |
| 991 | addOrMergeReplacement(Insertion, &NewReplacements); |
| 992 | } |
| 993 | // After moving namespaces, insert forward declarations back to old |
| 994 | // namespaces. |
| 995 | const auto &FwdDeclInsertions = InsertFwdDecls[FilePath]; |
| 996 | for (const auto &FwdDeclInsertion : FwdDeclInsertions) { |
| 997 | unsigned NewInsertionOffset = |
| 998 | Replaces.getShiftedCodePosition(FwdDeclInsertion.InsertionOffset); |
| 999 | tooling::Replacement Insertion(FilePath, NewInsertionOffset, 0, |
| 1000 | FwdDeclInsertion.ForwardDeclText); |
| 1001 | addOrMergeReplacement(Insertion, &NewReplacements); |
| 1002 | } |
| 1003 | // Add replacements referring to the changed code to existing replacements, |
| 1004 | // which refers to the original code. |
| 1005 | Replaces = Replaces.merge(NewReplacements); |
Eric Liu | fa63e98 | 2018-08-02 10:30:56 | [diff] [blame] | 1006 | auto Style = |
| 1007 | format::getStyle(format::DefaultFormatStyle, FilePath, FallbackStyle); |
Antonio Maiorano | 0d7d9c2 | 2017-01-17 00:13:32 | [diff] [blame] | 1008 | if (!Style) { |
| 1009 | llvm::errs() << llvm::toString(Style.takeError()) << "\n"; |
| 1010 | continue; |
| 1011 | } |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 1012 | // Clean up old namespaces if there is nothing in it after moving. |
| 1013 | auto CleanReplacements = |
Antonio Maiorano | 0d7d9c2 | 2017-01-17 00:13:32 | [diff] [blame] | 1014 | format::cleanupAroundReplacements(Code, Replaces, *Style); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 1015 | if (!CleanReplacements) { |
| 1016 | llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n"; |
| 1017 | continue; |
| 1018 | } |
| 1019 | FileToReplacements[FilePath] = *CleanReplacements; |
| 1020 | } |
Eric Liu | c265b02 | 2016-12-01 17:25:55 | [diff] [blame] | 1021 | |
| 1022 | // Make sure we don't generate replacements for files that do not match |
| 1023 | // FilePattern. |
| 1024 | for (auto &Entry : FileToReplacements) |
| 1025 | if (!FilePatternRE.match(Entry.first)) |
| 1026 | Entry.second.clear(); |
Eric Liu | 495b211 | 2016-09-19 17:40:32 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | } // namespace change_namespace |
| 1030 | } // namespace clang |