blob: a4088a34575de06a40ee8cf2b3966bec63dc0073 [file] [log] [blame]
Haojian Wu357ef992016-09-21 13:18:19 +00001//===-- ClangMove.cpp - Implement ClangMove functationalities ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ClangMove.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/Basic/SourceManager.h"
13#include "clang/Format/Format.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Lex/Lexer.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Rewrite/Core/Rewriter.h"
18#include "clang/Tooling/Core/Replacement.h"
Haojian Wud2a6d7b2016-10-04 09:05:31 +000019#include "llvm/Support/Path.h"
Haojian Wu357ef992016-09-21 13:18:19 +000020
21using namespace clang::ast_matchers;
22
23namespace clang {
24namespace move {
25namespace {
26
Haojian Wu7bd492c2016-10-14 10:07:58 +000027// FIXME: Move to ASTMatchers.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000028AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); }
Haojian Wu7bd492c2016-10-14 10:07:58 +000029
Haojian Wue77bcc72016-10-13 10:31:00 +000030AST_MATCHER_P(Decl, hasOutermostEnclosingClass,
31 ast_matchers::internal::Matcher<Decl>, InnerMatcher) {
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000032 const auto *Context = Node.getDeclContext();
33 if (!Context)
34 return false;
Haojian Wue77bcc72016-10-13 10:31:00 +000035 while (const auto *NextContext = Context->getParent()) {
36 if (isa<NamespaceDecl>(NextContext) ||
37 isa<TranslationUnitDecl>(NextContext))
38 break;
39 Context = NextContext;
40 }
41 return InnerMatcher.matches(*Decl::castFromDeclContext(Context), Finder,
42 Builder);
43}
44
45AST_MATCHER_P(CXXMethodDecl, ofOutermostEnclosingClass,
46 ast_matchers::internal::Matcher<CXXRecordDecl>, InnerMatcher) {
47 const CXXRecordDecl *Parent = Node.getParent();
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000048 if (!Parent)
49 return false;
Haojian Wue77bcc72016-10-13 10:31:00 +000050 while (const auto *NextParent =
51 dyn_cast<CXXRecordDecl>(Parent->getParent())) {
52 Parent = NextParent;
53 }
54
55 return InnerMatcher.matches(*Parent, Finder, Builder);
56}
57
Haojian Wud2a6d7b2016-10-04 09:05:31 +000058// Make the Path absolute using the CurrentDir if the Path is not an absolute
59// path. An empty Path will result in an empty string.
60std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) {
61 if (Path.empty())
62 return "";
63 llvm::SmallString<128> InitialDirectory(CurrentDir);
64 llvm::SmallString<128> AbsolutePath(Path);
65 if (std::error_code EC =
66 llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath))
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000067 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
Haojian Wud2a6d7b2016-10-04 09:05:31 +000068 << '\n';
69 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Haojian Wuc6f125e2016-10-04 09:49:20 +000070 llvm::sys::path::native(AbsolutePath);
Haojian Wud2a6d7b2016-10-04 09:05:31 +000071 return AbsolutePath.str();
72}
73
74// Make the Path absolute using the current working directory of the given
75// SourceManager if the Path is not an absolute path.
76//
77// The Path can be a path relative to the build directory, or retrieved from
78// the SourceManager.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000079std::string MakeAbsolutePath(const SourceManager &SM, StringRef Path) {
Haojian Wud2a6d7b2016-10-04 09:05:31 +000080 llvm::SmallString<128> AbsolutePath(Path);
81 if (std::error_code EC =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000082 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
83 AbsolutePath))
84 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
Haojian Wud2a6d7b2016-10-04 09:05:31 +000085 << '\n';
Haojian Wudb726572016-10-12 15:50:30 +000086 // Handle symbolic link path cases.
87 // We are trying to get the real file path of the symlink.
88 const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000089 llvm::sys::path::parent_path(AbsolutePath.str()));
Haojian Wudb726572016-10-12 15:50:30 +000090 if (Dir) {
91 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
92 SmallVector<char, 128> AbsoluteFilename;
93 llvm::sys::path::append(AbsoluteFilename, DirName,
94 llvm::sys::path::filename(AbsolutePath.str()));
95 return llvm::StringRef(AbsoluteFilename.data(), AbsoluteFilename.size())
96 .str();
97 }
Haojian Wud2a6d7b2016-10-04 09:05:31 +000098 return AbsolutePath.str();
99}
100
101// Matches AST nodes that are expanded within the given AbsoluteFilePath.
102AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,
103 AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc),
104 std::string, AbsoluteFilePath) {
105 auto &SourceManager = Finder->getASTContext().getSourceManager();
106 auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart());
107 if (ExpansionLoc.isInvalid())
108 return false;
109 auto FileEntry =
110 SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
111 if (!FileEntry)
112 return false;
113 return MakeAbsolutePath(SourceManager, FileEntry->getName()) ==
114 AbsoluteFilePath;
115}
116
Haojian Wu357ef992016-09-21 13:18:19 +0000117class FindAllIncludes : public clang::PPCallbacks {
118public:
119 explicit FindAllIncludes(SourceManager *SM, ClangMoveTool *const MoveTool)
120 : SM(*SM), MoveTool(MoveTool) {}
121
122 void InclusionDirective(clang::SourceLocation HashLoc,
123 const clang::Token & /*IncludeTok*/,
124 StringRef FileName, bool IsAngled,
Haojian Wu2930be12016-11-08 19:55:13 +0000125 clang::CharSourceRange FilenameRange,
Haojian Wu357ef992016-09-21 13:18:19 +0000126 const clang::FileEntry * /*File*/,
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000127 StringRef SearchPath, StringRef /*RelativePath*/,
Haojian Wu357ef992016-09-21 13:18:19 +0000128 const clang::Module * /*Imported*/) override {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000129 if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000130 MoveTool->addIncludes(FileName, IsAngled, SearchPath,
Haojian Wu2930be12016-11-08 19:55:13 +0000131 FileEntry->getName(), FilenameRange, SM);
Haojian Wu357ef992016-09-21 13:18:19 +0000132 }
133
134private:
135 const SourceManager &SM;
136 ClangMoveTool *const MoveTool;
137};
138
Haojian Wu4543fec2016-11-16 13:05:19 +0000139class FunctionDeclarationMatch : public MatchFinder::MatchCallback {
140public:
141 explicit FunctionDeclarationMatch(ClangMoveTool *MoveTool)
142 : MoveTool(MoveTool) {}
143
144 void run(const MatchFinder::MatchResult &Result) override {
145 const auto *FD = Result.Nodes.getNodeAs<clang::FunctionDecl>("function");
146 assert(FD);
147 const clang::NamedDecl *D = FD;
148 if (const auto *FTD = FD->getDescribedFunctionTemplate())
149 D = FTD;
Haojian Wu08e402a2016-12-02 12:39:39 +0000150 MoveTool->getMovedDecls().push_back(D);
Haojian Wu4543fec2016-11-16 13:05:19 +0000151 MoveTool->getUnremovedDeclsInOldHeader().erase(D);
Haojian Wu08e402a2016-12-02 12:39:39 +0000152 MoveTool->addRemovedDecl(D);
Haojian Wu4543fec2016-11-16 13:05:19 +0000153 }
154
155private:
156 ClangMoveTool *MoveTool;
157};
158
Haojian Wu35ca9462016-11-14 14:15:44 +0000159class ClassDeclarationMatch : public MatchFinder::MatchCallback {
160public:
161 explicit ClassDeclarationMatch(ClangMoveTool *MoveTool)
162 : MoveTool(MoveTool) {}
163 void run(const MatchFinder::MatchResult &Result) override {
164 clang::SourceManager* SM = &Result.Context->getSourceManager();
165 if (const auto *CMD =
166 Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method"))
167 MatchClassMethod(CMD, SM);
168 else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>(
169 "class_static_var_decl"))
170 MatchClassStaticVariable(VD, SM);
171 else if (const auto *CD = Result.Nodes.getNodeAs<clang::CXXRecordDecl>(
172 "moved_class"))
173 MatchClassDeclaration(CD, SM);
174 }
175
176private:
177 void MatchClassMethod(const clang::CXXMethodDecl* CMD,
178 clang::SourceManager* SM) {
179 // Skip inline class methods. isInline() ast matcher doesn't ignore this
180 // case.
181 if (!CMD->isInlined()) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000182 MoveTool->getMovedDecls().push_back(CMD);
183 MoveTool->addRemovedDecl(CMD);
Haojian Wu35ca9462016-11-14 14:15:44 +0000184 // Get template class method from its method declaration as
185 // UnremovedDecls stores template class method.
186 if (const auto *FTD = CMD->getDescribedFunctionTemplate())
187 MoveTool->getUnremovedDeclsInOldHeader().erase(FTD);
188 else
189 MoveTool->getUnremovedDeclsInOldHeader().erase(CMD);
190 }
191 }
192
193 void MatchClassStaticVariable(const clang::NamedDecl *VD,
194 clang::SourceManager* SM) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000195 MoveTool->getMovedDecls().push_back(VD);
196 MoveTool->addRemovedDecl(VD);
Haojian Wu35ca9462016-11-14 14:15:44 +0000197 MoveTool->getUnremovedDeclsInOldHeader().erase(VD);
198 }
199
200 void MatchClassDeclaration(const clang::CXXRecordDecl *CD,
201 clang::SourceManager* SM) {
202 // Get class template from its class declaration as UnremovedDecls stores
203 // class template.
204 if (const auto *TC = CD->getDescribedClassTemplate())
Haojian Wu08e402a2016-12-02 12:39:39 +0000205 MoveTool->getMovedDecls().push_back(TC);
Haojian Wu35ca9462016-11-14 14:15:44 +0000206 else
Haojian Wu08e402a2016-12-02 12:39:39 +0000207 MoveTool->getMovedDecls().push_back(CD);
Haojian Wu48ac3042016-11-23 10:04:19 +0000208 MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
Haojian Wu35ca9462016-11-14 14:15:44 +0000209 MoveTool->getUnremovedDeclsInOldHeader().erase(
Haojian Wu08e402a2016-12-02 12:39:39 +0000210 MoveTool->getMovedDecls().back());
Haojian Wu35ca9462016-11-14 14:15:44 +0000211 }
212
213 ClangMoveTool *MoveTool;
214};
215
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000216// Expand to get the end location of the line where the EndLoc of the given
217// Decl.
218SourceLocation
Haojian Wu08e402a2016-12-02 12:39:39 +0000219getLocForEndOfDecl(const clang::Decl *D,
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000220 const LangOptions &LangOpts = clang::LangOptions()) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000221 const auto &SM = D->getASTContext().getSourceManager();
222 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(D->getLocEnd());
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000223 // Try to load the file buffer.
224 bool InvalidTemp = false;
Haojian Wu08e402a2016-12-02 12:39:39 +0000225 llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000226 if (InvalidTemp)
227 return SourceLocation();
228
229 const char *TokBegin = File.data() + LocInfo.second;
230 // Lex from the start of the given location.
Haojian Wu08e402a2016-12-02 12:39:39 +0000231 Lexer Lex(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000232 TokBegin, File.end());
233
234 llvm::SmallVector<char, 16> Line;
235 // FIXME: this is a bit hacky to get ReadToEndOfLine work.
236 Lex.setParsingPreprocessorDirective(true);
237 Lex.ReadToEndOfLine(&Line);
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000238 SourceLocation EndLoc = D->getLocEnd().getLocWithOffset(Line.size());
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000239 // If we already reach EOF, just return the EOF SourceLocation;
240 // otherwise, move 1 offset ahead to include the trailing newline character
241 // '\n'.
Haojian Wu08e402a2016-12-02 12:39:39 +0000242 return SM.getLocForEndOfFile(LocInfo.first) == EndLoc
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000243 ? EndLoc
244 : EndLoc.getLocWithOffset(1);
245}
246
247// Get full range of a Decl including the comments associated with it.
248clang::CharSourceRange
Haojian Wu08e402a2016-12-02 12:39:39 +0000249getFullRange(const clang::Decl *D,
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000250 const clang::LangOptions &options = clang::LangOptions()) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000251 const auto &SM = D->getASTContext().getSourceManager();
252 clang::SourceRange Full(SM.getExpansionLoc(D->getLocStart()),
253 getLocForEndOfDecl(D));
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000254 // Expand to comments that are associated with the Decl.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000255 if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000256 if (SM.isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd()))
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000257 Full.setEnd(Comment->getLocEnd());
258 // FIXME: Don't delete a preceding comment, if there are no other entities
259 // it could refer to.
Haojian Wu08e402a2016-12-02 12:39:39 +0000260 if (SM.isBeforeInTranslationUnit(Comment->getLocStart(), Full.getBegin()))
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000261 Full.setBegin(Comment->getLocStart());
262 }
263
264 return clang::CharSourceRange::getCharRange(Full);
265}
266
Haojian Wu08e402a2016-12-02 12:39:39 +0000267std::string getDeclarationSourceText(const clang::Decl *D) {
268 const auto &SM = D->getASTContext().getSourceManager();
269 llvm::StringRef SourceText =
270 clang::Lexer::getSourceText(getFullRange(D), SM, clang::LangOptions());
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000271 return SourceText.str();
272}
273
Haojian Wu08e402a2016-12-02 12:39:39 +0000274bool isInHeaderFile(const clang::Decl *D,
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000275 llvm::StringRef OriginalRunningDirectory,
276 llvm::StringRef OldHeader) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000277 const auto &SM = D->getASTContext().getSourceManager();
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000278 if (OldHeader.empty())
Haojian Wu357ef992016-09-21 13:18:19 +0000279 return false;
280 auto ExpansionLoc = SM.getExpansionLoc(D->getLocStart());
281 if (ExpansionLoc.isInvalid())
282 return false;
283
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000284 if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
285 return MakeAbsolutePath(SM, FE->getName()) ==
286 MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
287 }
Haojian Wu357ef992016-09-21 13:18:19 +0000288
289 return false;
290}
291
Haojian Wu08e402a2016-12-02 12:39:39 +0000292std::vector<std::string> getNamespaces(const clang::Decl *D) {
Haojian Wu357ef992016-09-21 13:18:19 +0000293 std::vector<std::string> Namespaces;
294 for (const auto *Context = D->getDeclContext(); Context;
295 Context = Context->getParent()) {
296 if (llvm::isa<clang::TranslationUnitDecl>(Context) ||
297 llvm::isa<clang::LinkageSpecDecl>(Context))
298 break;
299
300 if (const auto *ND = llvm::dyn_cast<clang::NamespaceDecl>(Context))
301 Namespaces.push_back(ND->getName().str());
302 }
303 std::reverse(Namespaces.begin(), Namespaces.end());
304 return Namespaces;
305}
306
Haojian Wu357ef992016-09-21 13:18:19 +0000307clang::tooling::Replacements
308createInsertedReplacements(const std::vector<std::string> &Includes,
Haojian Wu08e402a2016-12-02 12:39:39 +0000309 const std::vector<const NamedDecl *> &Decls,
Haojian Wu48ac3042016-11-23 10:04:19 +0000310 llvm::StringRef FileName, bool IsHeader = false,
311 StringRef OldHeaderInclude = "") {
Haojian Wu53eab1e2016-10-14 13:43:49 +0000312 std::string NewCode;
Haojian Wu220c7552016-10-14 13:01:36 +0000313 std::string GuardName(FileName);
314 if (IsHeader) {
Haojian Wuac97fc32016-10-17 15:26:34 +0000315 for (size_t i = 0; i < GuardName.size(); ++i) {
316 if (!isAlphanumeric(GuardName[i]))
317 GuardName[i] = '_';
318 }
Haojian Wu220c7552016-10-14 13:01:36 +0000319 GuardName = StringRef(GuardName).upper();
Haojian Wu53eab1e2016-10-14 13:43:49 +0000320 NewCode += "#ifndef " + GuardName + "\n";
Haojian Wu53315a72016-11-15 09:06:59 +0000321 NewCode += "#define " + GuardName + "\n\n";
Haojian Wu220c7552016-10-14 13:01:36 +0000322 }
Haojian Wu357ef992016-09-21 13:18:19 +0000323
Haojian Wu48ac3042016-11-23 10:04:19 +0000324 NewCode += OldHeaderInclude;
Haojian Wu357ef992016-09-21 13:18:19 +0000325 // Add #Includes.
Haojian Wu357ef992016-09-21 13:18:19 +0000326 for (const auto &Include : Includes)
Haojian Wu53eab1e2016-10-14 13:43:49 +0000327 NewCode += Include;
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000328
Haojian Wu53eab1e2016-10-14 13:43:49 +0000329 if (!Includes.empty())
330 NewCode += "\n";
Haojian Wu357ef992016-09-21 13:18:19 +0000331
332 // Add moved class definition and its related declarations. All declarations
333 // in same namespace are grouped together.
Haojian Wu53315a72016-11-15 09:06:59 +0000334 //
335 // Record namespaces where the current position is in.
Haojian Wu357ef992016-09-21 13:18:19 +0000336 std::vector<std::string> CurrentNamespaces;
Haojian Wu08e402a2016-12-02 12:39:39 +0000337 for (const auto *MovedDecl : Decls) {
Haojian Wu53315a72016-11-15 09:06:59 +0000338 // The namespaces of the declaration being moved.
Haojian Wu08e402a2016-12-02 12:39:39 +0000339 std::vector<std::string> DeclNamespaces = getNamespaces(MovedDecl);
Haojian Wu357ef992016-09-21 13:18:19 +0000340 auto CurrentIt = CurrentNamespaces.begin();
341 auto DeclIt = DeclNamespaces.begin();
Haojian Wu53315a72016-11-15 09:06:59 +0000342 // Skip the common prefix.
Haojian Wu357ef992016-09-21 13:18:19 +0000343 while (CurrentIt != CurrentNamespaces.end() &&
344 DeclIt != DeclNamespaces.end()) {
345 if (*CurrentIt != *DeclIt)
346 break;
347 ++CurrentIt;
348 ++DeclIt;
349 }
Haojian Wu53315a72016-11-15 09:06:59 +0000350 // Calculate the new namespaces after adding MovedDecl in CurrentNamespace,
351 // which is used for next iteration of this loop.
Haojian Wu357ef992016-09-21 13:18:19 +0000352 std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(),
353 CurrentIt);
354 NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end());
Haojian Wu53315a72016-11-15 09:06:59 +0000355
356
357 // End with CurrentNamespace.
358 bool HasEndCurrentNamespace = false;
Haojian Wu357ef992016-09-21 13:18:19 +0000359 auto RemainingSize = CurrentNamespaces.end() - CurrentIt;
360 for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0;
361 --RemainingSize, ++It) {
362 assert(It < CurrentNamespaces.rend());
Haojian Wu53eab1e2016-10-14 13:43:49 +0000363 NewCode += "} // namespace " + *It + "\n";
Haojian Wu53315a72016-11-15 09:06:59 +0000364 HasEndCurrentNamespace = true;
Haojian Wu357ef992016-09-21 13:18:19 +0000365 }
Haojian Wu53315a72016-11-15 09:06:59 +0000366 // Add trailing '\n' after the nested namespace definition.
367 if (HasEndCurrentNamespace)
368 NewCode += "\n";
369
370 // If the moved declaration is not in CurrentNamespace, add extra namespace
371 // definitions.
372 bool IsInNewNamespace = false;
Haojian Wu357ef992016-09-21 13:18:19 +0000373 while (DeclIt != DeclNamespaces.end()) {
Haojian Wu53eab1e2016-10-14 13:43:49 +0000374 NewCode += "namespace " + *DeclIt + " {\n";
Haojian Wu53315a72016-11-15 09:06:59 +0000375 IsInNewNamespace = true;
Haojian Wu357ef992016-09-21 13:18:19 +0000376 ++DeclIt;
377 }
Haojian Wu53315a72016-11-15 09:06:59 +0000378 // If the moved declaration is in same namespace CurrentNamespace, add
379 // a preceeding `\n' before the moved declaration.
Haojian Wu50a45d92016-11-18 10:51:16 +0000380 // FIXME: Don't add empty lines between using declarations.
Haojian Wu53315a72016-11-15 09:06:59 +0000381 if (!IsInNewNamespace)
382 NewCode += "\n";
Haojian Wu08e402a2016-12-02 12:39:39 +0000383 NewCode += getDeclarationSourceText(MovedDecl);
Haojian Wu357ef992016-09-21 13:18:19 +0000384 CurrentNamespaces = std::move(NextNamespaces);
385 }
386 std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end());
Haojian Wu53eab1e2016-10-14 13:43:49 +0000387 for (const auto &NS : CurrentNamespaces)
388 NewCode += "} // namespace " + NS + "\n";
Haojian Wu220c7552016-10-14 13:01:36 +0000389
Haojian Wu53eab1e2016-10-14 13:43:49 +0000390 if (IsHeader)
Haojian Wu53315a72016-11-15 09:06:59 +0000391 NewCode += "\n#endif // " + GuardName + "\n";
Haojian Wu53eab1e2016-10-14 13:43:49 +0000392 return clang::tooling::Replacements(
393 clang::tooling::Replacement(FileName, 0, 0, NewCode));
Haojian Wu357ef992016-09-21 13:18:19 +0000394}
395
396} // namespace
397
398std::unique_ptr<clang::ASTConsumer>
399ClangMoveAction::CreateASTConsumer(clang::CompilerInstance &Compiler,
400 StringRef /*InFile*/) {
401 Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>(
402 &Compiler.getSourceManager(), &MoveTool));
403 return MatchFinder.newASTConsumer();
404}
405
Haojian Wub15c8da2016-11-24 10:17:17 +0000406ClangMoveTool::ClangMoveTool(ClangMoveContext *const Context,
407 DeclarationReporter *const Reporter)
408 : Context(Context), Reporter(Reporter) {
409 if (!Context->Spec.NewHeader.empty())
410 CCIncludes.push_back("#include \"" + Context->Spec.NewHeader + "\"\n");
Haojian Wu357ef992016-09-21 13:18:19 +0000411}
412
Haojian Wu08e402a2016-12-02 12:39:39 +0000413void ClangMoveTool::addRemovedDecl(const NamedDecl *Decl) {
414 const auto &SM = Decl->getASTContext().getSourceManager();
415 auto Loc = Decl->getLocation();
Haojian Wu48ac3042016-11-23 10:04:19 +0000416 StringRef FilePath = SM.getFilename(Loc);
417 FilePathToFileID[FilePath] = SM.getFileID(Loc);
418 RemovedDecls.push_back(Decl);
419}
420
Haojian Wu357ef992016-09-21 13:18:19 +0000421void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
Haojian Wub15c8da2016-11-24 10:17:17 +0000422 auto InOldHeader =
423 isExpansionInFile(makeAbsolutePath(Context->Spec.OldHeader));
424 auto InOldCC = isExpansionInFile(makeAbsolutePath(Context->Spec.OldCC));
Haojian Wu357ef992016-09-21 13:18:19 +0000425 auto InOldFiles = anyOf(InOldHeader, InOldCC);
Haojian Wu2930be12016-11-08 19:55:13 +0000426 auto ForwardDecls =
427 cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())));
428
429 //============================================================================
430 // Matchers for old header
431 //============================================================================
432 // Match all top-level named declarations (e.g. function, variable, enum) in
433 // old header, exclude forward class declarations and namespace declarations.
434 //
Haojian Wub15c8da2016-11-24 10:17:17 +0000435 // We consider declarations inside a class belongs to the class. So these
436 // declarations will be ignored.
Haojian Wu2930be12016-11-08 19:55:13 +0000437 auto AllDeclsInHeader = namedDecl(
438 unless(ForwardDecls), unless(namespaceDecl()),
Haojian Wub15c8da2016-11-24 10:17:17 +0000439 unless(usingDirectiveDecl()), // using namespace decl.
Haojian Wu2930be12016-11-08 19:55:13 +0000440 unless(classTemplateDecl(has(ForwardDecls))), // template forward decl.
441 InOldHeader,
Haojian Wub15c8da2016-11-24 10:17:17 +0000442 hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))),
443 hasDeclContext(decl(anyOf(namespaceDecl(), translationUnitDecl()))));
Haojian Wu2930be12016-11-08 19:55:13 +0000444 Finder->addMatcher(AllDeclsInHeader.bind("decls_in_header"), this);
Haojian Wub15c8da2016-11-24 10:17:17 +0000445
446 // Don't register other matchers when dumping all declarations in header.
447 if (Context->DumpDeclarations)
448 return;
449
Haojian Wu2930be12016-11-08 19:55:13 +0000450 // Match forward declarations in old header.
451 Finder->addMatcher(namedDecl(ForwardDecls, InOldHeader).bind("fwd_decl"),
452 this);
453
454 //============================================================================
Haojian Wu2930be12016-11-08 19:55:13 +0000455 // Matchers for old cc
456 //============================================================================
Haojian Wu50a45d92016-11-18 10:51:16 +0000457 auto InOldCCNamedOrGlobalNamespace =
458 allOf(hasParent(decl(anyOf(namespaceDecl(unless(isAnonymous())),
459 translationUnitDecl()))),
460 InOldCC);
461 // Matching using decls/type alias decls which are in named namespace or
462 // global namespace. Those in classes, functions and anonymous namespaces are
463 // covered in other matchers.
Haojian Wu357ef992016-09-21 13:18:19 +0000464 Finder->addMatcher(
Haojian Wu50a45d92016-11-18 10:51:16 +0000465 namedDecl(anyOf(usingDecl(InOldCCNamedOrGlobalNamespace),
466 usingDirectiveDecl(InOldCCNamedOrGlobalNamespace),
467 typeAliasDecl( InOldCCNamedOrGlobalNamespace)))
Haojian Wu67bb6512016-10-19 14:13:21 +0000468 .bind("using_decl"),
Haojian Wu357ef992016-09-21 13:18:19 +0000469 this);
470
Haojian Wu67bb6512016-10-19 14:13:21 +0000471 // Match anonymous namespace decl in old cc.
472 Finder->addMatcher(namespaceDecl(isAnonymous(), InOldCC).bind("anonymous_ns"),
473 this);
474
475 // Match static functions/variable definitions which are defined in named
476 // namespaces.
Haojian Wub15c8da2016-11-24 10:17:17 +0000477 Optional<ast_matchers::internal::Matcher<NamedDecl>> HasAnySymbolNames;
478 for (StringRef SymbolName : Context->Spec.Names) {
479 llvm::StringRef GlobalSymbolName = SymbolName.trim().ltrim(':');
480 const auto HasName = hasName(("::" + GlobalSymbolName).str());
481 HasAnySymbolNames =
482 HasAnySymbolNames ? anyOf(*HasAnySymbolNames, HasName) : HasName;
483 }
484
485 if (!HasAnySymbolNames) {
486 llvm::errs() << "No symbols being moved.\n";
487 return;
488 }
489 auto InMovedClass =
490 hasOutermostEnclosingClass(cxxRecordDecl(*HasAnySymbolNames));
Haojian Wu67bb6512016-10-19 14:13:21 +0000491 auto IsOldCCStaticDefinition =
Haojian Wu50a45d92016-11-18 10:51:16 +0000492 allOf(isDefinition(), unless(InMovedClass), InOldCCNamedOrGlobalNamespace,
Haojian Wu67bb6512016-10-19 14:13:21 +0000493 isStaticStorageClass());
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000494 Finder->addMatcher(namedDecl(anyOf(functionDecl(IsOldCCStaticDefinition),
495 varDecl(IsOldCCStaticDefinition)))
496 .bind("static_decls"),
497 this);
Haojian Wu35ca9462016-11-14 14:15:44 +0000498
499 //============================================================================
500 // Matchers for old files, including old.h/old.cc
501 //============================================================================
502 // Create a MatchCallback for class declarations.
503 MatchCallbacks.push_back(llvm::make_unique<ClassDeclarationMatch>(this));
504 // Match moved class declarations.
505 auto MovedClass =
506 cxxRecordDecl(
Haojian Wu4543fec2016-11-16 13:05:19 +0000507 InOldFiles, *HasAnySymbolNames, isDefinition(),
Haojian Wu35ca9462016-11-14 14:15:44 +0000508 hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())))
509 .bind("moved_class");
510 Finder->addMatcher(MovedClass, MatchCallbacks.back().get());
511 // Match moved class methods (static methods included) which are defined
512 // outside moved class declaration.
513 Finder->addMatcher(
Haojian Wu4543fec2016-11-16 13:05:19 +0000514 cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*HasAnySymbolNames),
Haojian Wu35ca9462016-11-14 14:15:44 +0000515 isDefinition())
516 .bind("class_method"),
517 MatchCallbacks.back().get());
518 // Match static member variable definition of the moved class.
519 Finder->addMatcher(
520 varDecl(InMovedClass, InOldFiles, isDefinition(), isStaticDataMember())
521 .bind("class_static_var_decl"),
522 MatchCallbacks.back().get());
523
Haojian Wu4543fec2016-11-16 13:05:19 +0000524 MatchCallbacks.push_back(llvm::make_unique<FunctionDeclarationMatch>(this));
525 Finder->addMatcher(functionDecl(InOldFiles, *HasAnySymbolNames,
526 anyOf(hasDeclContext(namespaceDecl()),
527 hasDeclContext(translationUnitDecl())))
528 .bind("function"),
529 MatchCallbacks.back().get());
Haojian Wu357ef992016-09-21 13:18:19 +0000530}
531
532void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) {
Haojian Wu2930be12016-11-08 19:55:13 +0000533 if (const auto *D =
534 Result.Nodes.getNodeAs<clang::NamedDecl>("decls_in_header")) {
535 UnremovedDeclsInOldHeader.insert(D);
Haojian Wu357ef992016-09-21 13:18:19 +0000536 } else if (const auto *FWD =
537 Result.Nodes.getNodeAs<clang::CXXRecordDecl>("fwd_decl")) {
Haojian Wub15c8da2016-11-24 10:17:17 +0000538 // Skip all forward declarations which appear after moved class declaration.
Haojian Wu29c38f72016-10-21 19:26:43 +0000539 if (RemovedDecls.empty()) {
Haojian Wub53ec462016-11-10 05:33:26 +0000540 if (const auto *DCT = FWD->getDescribedClassTemplate())
Haojian Wu08e402a2016-12-02 12:39:39 +0000541 MovedDecls.push_back(DCT);
Haojian Wub53ec462016-11-10 05:33:26 +0000542 else
Haojian Wu08e402a2016-12-02 12:39:39 +0000543 MovedDecls.push_back(FWD);
Haojian Wu29c38f72016-10-21 19:26:43 +0000544 }
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000545 } else if (const auto *ANS =
546 Result.Nodes.getNodeAs<clang::NamespaceDecl>("anonymous_ns")) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000547 MovedDecls.push_back(ANS);
Haojian Wu357ef992016-09-21 13:18:19 +0000548 } else if (const auto *ND =
549 Result.Nodes.getNodeAs<clang::NamedDecl>("static_decls")) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000550 MovedDecls.push_back(ND);
Haojian Wu67bb6512016-10-19 14:13:21 +0000551 } else if (const auto *UD =
552 Result.Nodes.getNodeAs<clang::NamedDecl>("using_decl")) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000553 MovedDecls.push_back(UD);
Haojian Wu357ef992016-09-21 13:18:19 +0000554 }
555}
556
Haojian Wu2930be12016-11-08 19:55:13 +0000557std::string ClangMoveTool::makeAbsolutePath(StringRef Path) {
Haojian Wub15c8da2016-11-24 10:17:17 +0000558 return MakeAbsolutePath(Context->OriginalRunningDirectory, Path);
Haojian Wu2930be12016-11-08 19:55:13 +0000559}
560
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000561void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader, bool IsAngled,
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000562 llvm::StringRef SearchPath,
563 llvm::StringRef FileName,
Haojian Wu2930be12016-11-08 19:55:13 +0000564 clang::CharSourceRange IncludeFilenameRange,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000565 const SourceManager &SM) {
Haojian Wudb726572016-10-12 15:50:30 +0000566 SmallVector<char, 128> HeaderWithSearchPath;
567 llvm::sys::path::append(HeaderWithSearchPath, SearchPath, IncludeHeader);
Haojian Wub15c8da2016-11-24 10:17:17 +0000568 std::string AbsoluteOldHeader = makeAbsolutePath(Context->Spec.OldHeader);
Haojian Wudaf4cb82016-09-23 13:28:38 +0000569 // FIXME: Add old.h to the new.cc/h when the new target has dependencies on
570 // old.h/c. For instance, when moved class uses another class defined in
571 // old.h, the old.h should be added in new.h.
Haojian Wudb726572016-10-12 15:50:30 +0000572 if (AbsoluteOldHeader ==
573 MakeAbsolutePath(SM, llvm::StringRef(HeaderWithSearchPath.data(),
Haojian Wu2930be12016-11-08 19:55:13 +0000574 HeaderWithSearchPath.size()))) {
575 OldHeaderIncludeRange = IncludeFilenameRange;
Haojian Wudaf4cb82016-09-23 13:28:38 +0000576 return;
Haojian Wu2930be12016-11-08 19:55:13 +0000577 }
Haojian Wudaf4cb82016-09-23 13:28:38 +0000578
579 std::string IncludeLine =
580 IsAngled ? ("#include <" + IncludeHeader + ">\n").str()
581 : ("#include \"" + IncludeHeader + "\"\n").str();
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000582
Haojian Wudb726572016-10-12 15:50:30 +0000583 std::string AbsoluteCurrentFile = MakeAbsolutePath(SM, FileName);
584 if (AbsoluteOldHeader == AbsoluteCurrentFile) {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000585 HeaderIncludes.push_back(IncludeLine);
Haojian Wub15c8da2016-11-24 10:17:17 +0000586 } else if (makeAbsolutePath(Context->Spec.OldCC) == AbsoluteCurrentFile) {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000587 CCIncludes.push_back(IncludeLine);
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000588 }
Haojian Wu357ef992016-09-21 13:18:19 +0000589}
590
Haojian Wu08e402a2016-12-02 12:39:39 +0000591void ClangMoveTool::removeDeclsInOldFiles() {
Haojian Wu48ac3042016-11-23 10:04:19 +0000592 if (RemovedDecls.empty()) return;
Haojian Wu08e402a2016-12-02 12:39:39 +0000593 for (const auto *RemovedDecl : RemovedDecls) {
594 const auto &SM = RemovedDecl->getASTContext().getSourceManager();
595 auto Range = getFullRange(RemovedDecl);
Haojian Wu357ef992016-09-21 13:18:19 +0000596 clang::tooling::Replacement RemoveReplacement(
Haojian Wu48ac3042016-11-23 10:04:19 +0000597 SM,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000598 clang::CharSourceRange::getCharRange(Range.getBegin(), Range.getEnd()),
Haojian Wu357ef992016-09-21 13:18:19 +0000599 "");
600 std::string FilePath = RemoveReplacement.getFilePath().str();
Haojian Wub15c8da2016-11-24 10:17:17 +0000601 auto Err = Context->FileToReplacements[FilePath].add(RemoveReplacement);
Haojian Wu48ac3042016-11-23 10:04:19 +0000602 if (Err)
Haojian Wu53eab1e2016-10-14 13:43:49 +0000603 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Haojian Wu48ac3042016-11-23 10:04:19 +0000604 }
Haojian Wu08e402a2016-12-02 12:39:39 +0000605 const auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
Haojian Wu48ac3042016-11-23 10:04:19 +0000606
607 // Post process of cleanup around all the replacements.
Haojian Wub15c8da2016-11-24 10:17:17 +0000608 for (auto &FileAndReplacements : Context->FileToReplacements) {
Haojian Wu48ac3042016-11-23 10:04:19 +0000609 StringRef FilePath = FileAndReplacements.first;
610 // Add #include of new header to old header.
Haojian Wub15c8da2016-11-24 10:17:17 +0000611 if (Context->Spec.OldDependOnNew &&
Haojian Wu08e402a2016-12-02 12:39:39 +0000612 MakeAbsolutePath(SM, FilePath) ==
Haojian Wub15c8da2016-11-24 10:17:17 +0000613 makeAbsolutePath(Context->Spec.OldHeader)) {
Haojian Wu48ac3042016-11-23 10:04:19 +0000614 // FIXME: Minimize the include path like include-fixer.
Haojian Wub15c8da2016-11-24 10:17:17 +0000615 std::string IncludeNewH =
616 "#include \"" + Context->Spec.NewHeader + "\"\n";
Haojian Wu48ac3042016-11-23 10:04:19 +0000617 // This replacment for inserting header will be cleaned up at the end.
618 auto Err = FileAndReplacements.second.add(
619 tooling::Replacement(FilePath, UINT_MAX, 0, IncludeNewH));
620 if (Err)
621 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Haojian Wu53eab1e2016-10-14 13:43:49 +0000622 }
Haojian Wu253d5962016-10-06 08:29:32 +0000623
Haojian Wu48ac3042016-11-23 10:04:19 +0000624 auto SI = FilePathToFileID.find(FilePath);
625 // Ignore replacements for new.h/cc.
626 if (SI == FilePathToFileID.end()) continue;
Haojian Wu08e402a2016-12-02 12:39:39 +0000627 llvm::StringRef Code = SM.getBufferData(SI->second);
Haojian Wu253d5962016-10-06 08:29:32 +0000628 format::FormatStyle Style =
Haojian Wub15c8da2016-11-24 10:17:17 +0000629 format::getStyle("file", FilePath, Context->FallbackStyle);
Haojian Wu253d5962016-10-06 08:29:32 +0000630 auto CleanReplacements = format::cleanupAroundReplacements(
Haojian Wub15c8da2016-11-24 10:17:17 +0000631 Code, Context->FileToReplacements[FilePath], Style);
Haojian Wu253d5962016-10-06 08:29:32 +0000632
633 if (!CleanReplacements) {
634 llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
635 continue;
636 }
Haojian Wub15c8da2016-11-24 10:17:17 +0000637 Context->FileToReplacements[FilePath] = *CleanReplacements;
Haojian Wu357ef992016-09-21 13:18:19 +0000638 }
639}
640
Haojian Wu08e402a2016-12-02 12:39:39 +0000641void ClangMoveTool::moveDeclsToNewFiles() {
642 std::vector<const NamedDecl *> NewHeaderDecls;
643 std::vector<const NamedDecl *> NewCCDecls;
644 for (const auto *MovedDecl : MovedDecls) {
645 if (isInHeaderFile(MovedDecl, Context->OriginalRunningDirectory,
Haojian Wub15c8da2016-11-24 10:17:17 +0000646 Context->Spec.OldHeader))
Haojian Wu357ef992016-09-21 13:18:19 +0000647 NewHeaderDecls.push_back(MovedDecl);
648 else
649 NewCCDecls.push_back(MovedDecl);
650 }
651
Haojian Wub15c8da2016-11-24 10:17:17 +0000652 if (!Context->Spec.NewHeader.empty()) {
Haojian Wu48ac3042016-11-23 10:04:19 +0000653 std::string OldHeaderInclude =
Haojian Wub15c8da2016-11-24 10:17:17 +0000654 Context->Spec.NewDependOnOld
655 ? "#include \"" + Context->Spec.OldHeader + "\"\n"
656 : "";
657 Context->FileToReplacements[Context->Spec.NewHeader] =
658 createInsertedReplacements(HeaderIncludes, NewHeaderDecls,
659 Context->Spec.NewHeader, /*IsHeader=*/true,
660 OldHeaderInclude);
Haojian Wu48ac3042016-11-23 10:04:19 +0000661 }
Haojian Wub15c8da2016-11-24 10:17:17 +0000662 if (!Context->Spec.NewCC.empty())
663 Context->FileToReplacements[Context->Spec.NewCC] =
664 createInsertedReplacements(CCIncludes, NewCCDecls, Context->Spec.NewCC);
Haojian Wu357ef992016-09-21 13:18:19 +0000665}
666
Haojian Wu2930be12016-11-08 19:55:13 +0000667// Move all contents from OldFile to NewFile.
668void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile,
669 StringRef NewFile) {
670 const FileEntry *FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile));
671 if (!FE) {
672 llvm::errs() << "Failed to get file: " << OldFile << "\n";
673 return;
674 }
675 FileID ID = SM.getOrCreateFileID(FE, SrcMgr::C_User);
676 auto Begin = SM.getLocForStartOfFile(ID);
677 auto End = SM.getLocForEndOfFile(ID);
678 clang::tooling::Replacement RemoveAll (
679 SM, clang::CharSourceRange::getCharRange(Begin, End), "");
680 std::string FilePath = RemoveAll.getFilePath().str();
Haojian Wub15c8da2016-11-24 10:17:17 +0000681 Context->FileToReplacements[FilePath] =
682 clang::tooling::Replacements(RemoveAll);
Haojian Wu2930be12016-11-08 19:55:13 +0000683
684 StringRef Code = SM.getBufferData(ID);
685 if (!NewFile.empty()) {
686 auto AllCode = clang::tooling::Replacements(
687 clang::tooling::Replacement(NewFile, 0, 0, Code));
688 // If we are moving from old.cc, an extra step is required: excluding
689 // the #include of "old.h", instead, we replace it with #include of "new.h".
Haojian Wub15c8da2016-11-24 10:17:17 +0000690 if (Context->Spec.NewCC == NewFile && OldHeaderIncludeRange.isValid()) {
Haojian Wu2930be12016-11-08 19:55:13 +0000691 AllCode = AllCode.merge(
692 clang::tooling::Replacements(clang::tooling::Replacement(
Haojian Wub15c8da2016-11-24 10:17:17 +0000693 SM, OldHeaderIncludeRange, '"' + Context->Spec.NewHeader + '"')));
Haojian Wu2930be12016-11-08 19:55:13 +0000694 }
Haojian Wub15c8da2016-11-24 10:17:17 +0000695 Context->FileToReplacements[NewFile] = std::move(AllCode);
Haojian Wu2930be12016-11-08 19:55:13 +0000696 }
697}
698
Haojian Wu357ef992016-09-21 13:18:19 +0000699void ClangMoveTool::onEndOfTranslationUnit() {
Haojian Wub15c8da2016-11-24 10:17:17 +0000700 if (Context->DumpDeclarations) {
701 assert(Reporter);
702 for (const auto *Decl : UnremovedDeclsInOldHeader) {
703 auto Kind = Decl->getKind();
704 const std::string QualifiedName = Decl->getQualifiedNameAsString();
705 if (Kind == Decl::Kind::Function || Kind == Decl::Kind::FunctionTemplate)
706 Reporter->reportDeclaration(QualifiedName, "Function");
707 else if (Kind == Decl::Kind::ClassTemplate ||
708 Kind == Decl::Kind::CXXRecord)
709 Reporter->reportDeclaration(QualifiedName, "Class");
710 }
711 return;
712 }
713
Haojian Wu357ef992016-09-21 13:18:19 +0000714 if (RemovedDecls.empty())
715 return;
Haojian Wub15c8da2016-11-24 10:17:17 +0000716 if (UnremovedDeclsInOldHeader.empty() && !Context->Spec.OldHeader.empty()) {
Haojian Wu08e402a2016-12-02 12:39:39 +0000717 auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
Haojian Wub15c8da2016-11-24 10:17:17 +0000718 moveAll(SM, Context->Spec.OldHeader, Context->Spec.NewHeader);
719 moveAll(SM, Context->Spec.OldCC, Context->Spec.NewCC);
Haojian Wu2930be12016-11-08 19:55:13 +0000720 return;
721 }
Haojian Wu08e402a2016-12-02 12:39:39 +0000722 removeDeclsInOldFiles();
723 moveDeclsToNewFiles();
Haojian Wu357ef992016-09-21 13:18:19 +0000724}
725
726} // namespace move
727} // namespace clang