blob: fe19fa0065f2535a16ac72abd1a36811bee9a486 [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 Wud2a6d7b2016-10-04 09:05:31 +000027// Make the Path absolute using the CurrentDir if the Path is not an absolute
28// path. An empty Path will result in an empty string.
29std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) {
30 if (Path.empty())
31 return "";
32 llvm::SmallString<128> InitialDirectory(CurrentDir);
33 llvm::SmallString<128> AbsolutePath(Path);
34 if (std::error_code EC =
35 llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath))
36 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
37 << '\n';
38 llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
Haojian Wuc6f125e2016-10-04 09:49:20 +000039 llvm::sys::path::native(AbsolutePath);
Haojian Wud2a6d7b2016-10-04 09:05:31 +000040 return AbsolutePath.str();
41}
42
43// Make the Path absolute using the current working directory of the given
44// SourceManager if the Path is not an absolute path.
45//
46// The Path can be a path relative to the build directory, or retrieved from
47// the SourceManager.
48std::string MakeAbsolutePath(const SourceManager& SM, StringRef Path) {
49 llvm::SmallString<128> AbsolutePath(Path);
50 if (std::error_code EC =
51 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(AbsolutePath))
52 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
53 << '\n';
Haojian Wudb726572016-10-12 15:50:30 +000054 // Handle symbolic link path cases.
55 // We are trying to get the real file path of the symlink.
56 const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
57 llvm::sys::path::parent_path(AbsolutePath.str()));
58 if (Dir) {
59 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
60 SmallVector<char, 128> AbsoluteFilename;
61 llvm::sys::path::append(AbsoluteFilename, DirName,
62 llvm::sys::path::filename(AbsolutePath.str()));
63 return llvm::StringRef(AbsoluteFilename.data(), AbsoluteFilename.size())
64 .str();
65 }
Haojian Wud2a6d7b2016-10-04 09:05:31 +000066 return AbsolutePath.str();
67}
68
69// Matches AST nodes that are expanded within the given AbsoluteFilePath.
70AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,
71 AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc),
72 std::string, AbsoluteFilePath) {
73 auto &SourceManager = Finder->getASTContext().getSourceManager();
74 auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart());
75 if (ExpansionLoc.isInvalid())
76 return false;
77 auto FileEntry =
78 SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
79 if (!FileEntry)
80 return false;
81 return MakeAbsolutePath(SourceManager, FileEntry->getName()) ==
82 AbsoluteFilePath;
83}
84
Haojian Wu357ef992016-09-21 13:18:19 +000085class FindAllIncludes : public clang::PPCallbacks {
86public:
87 explicit FindAllIncludes(SourceManager *SM, ClangMoveTool *const MoveTool)
88 : SM(*SM), MoveTool(MoveTool) {}
89
90 void InclusionDirective(clang::SourceLocation HashLoc,
91 const clang::Token & /*IncludeTok*/,
92 StringRef FileName, bool IsAngled,
93 clang::CharSourceRange /*FilenameRange*/,
94 const clang::FileEntry * /*File*/,
Haojian Wud2a6d7b2016-10-04 09:05:31 +000095 StringRef SearchPath, StringRef /*RelativePath*/,
Haojian Wu357ef992016-09-21 13:18:19 +000096 const clang::Module * /*Imported*/) override {
Haojian Wudaf4cb82016-09-23 13:28:38 +000097 if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
Haojian Wud2a6d7b2016-10-04 09:05:31 +000098 MoveTool->addIncludes(FileName, IsAngled, SearchPath,
99 FileEntry->getName(), SM);
Haojian Wu357ef992016-09-21 13:18:19 +0000100 }
101
102private:
103 const SourceManager &SM;
104 ClangMoveTool *const MoveTool;
105};
106
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000107// Expand to get the end location of the line where the EndLoc of the given
108// Decl.
109SourceLocation
110getLocForEndOfDecl(const clang::Decl *D, const SourceManager *SM,
111 const LangOptions &LangOpts = clang::LangOptions()) {
112 std::pair<FileID, unsigned> LocInfo = SM->getDecomposedLoc(D->getLocEnd());
113 // Try to load the file buffer.
114 bool InvalidTemp = false;
115 llvm::StringRef File = SM->getBufferData(LocInfo.first, &InvalidTemp);
116 if (InvalidTemp)
117 return SourceLocation();
118
119 const char *TokBegin = File.data() + LocInfo.second;
120 // Lex from the start of the given location.
121 Lexer Lex(SM->getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
122 TokBegin, File.end());
123
124 llvm::SmallVector<char, 16> Line;
125 // FIXME: this is a bit hacky to get ReadToEndOfLine work.
126 Lex.setParsingPreprocessorDirective(true);
127 Lex.ReadToEndOfLine(&Line);
128 SourceLocation EndLoc = D->getLocEnd().getLocWithOffset(Line.size());
129 // If we already reach EOF, just return the EOF SourceLocation;
130 // otherwise, move 1 offset ahead to include the trailing newline character
131 // '\n'.
132 return SM->getLocForEndOfFile(LocInfo.first) == EndLoc
133 ? EndLoc
134 : EndLoc.getLocWithOffset(1);
135}
136
137// Get full range of a Decl including the comments associated with it.
138clang::CharSourceRange
139GetFullRange(const clang::SourceManager *SM, const clang::Decl *D,
140 const clang::LangOptions &options = clang::LangOptions()) {
141 clang::SourceRange Full = D->getSourceRange();
142 Full.setEnd(getLocForEndOfDecl(D, SM));
143 // Expand to comments that are associated with the Decl.
144 if (const auto* Comment =
145 D->getASTContext().getRawCommentForDeclNoCache(D)) {
146 if (SM->isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd()))
147 Full.setEnd(Comment->getLocEnd());
148 // FIXME: Don't delete a preceding comment, if there are no other entities
149 // it could refer to.
150 if (SM->isBeforeInTranslationUnit(Comment->getLocStart(),
151 Full.getBegin()))
152 Full.setBegin(Comment->getLocStart());
153 }
154
155 return clang::CharSourceRange::getCharRange(Full);
156}
157
158std::string getDeclarationSourceText(const clang::Decl *D,
159 const clang::SourceManager *SM) {
160 llvm::StringRef SourceText = clang::Lexer::getSourceText(
161 GetFullRange(SM, D), *SM, clang::LangOptions());
162 return SourceText.str();
163}
164
Haojian Wu357ef992016-09-21 13:18:19 +0000165clang::tooling::Replacement
166getReplacementInChangedCode(const clang::tooling::Replacements &Replacements,
167 const clang::tooling::Replacement &Replacement) {
168 unsigned Start = Replacements.getShiftedCodePosition(Replacement.getOffset());
169 unsigned End = Replacements.getShiftedCodePosition(Replacement.getOffset() +
170 Replacement.getLength());
171 return clang::tooling::Replacement(Replacement.getFilePath(), Start,
172 End - Start,
173 Replacement.getReplacementText());
174}
175
176void addOrMergeReplacement(const clang::tooling::Replacement &Replacement,
177 clang::tooling::Replacements *Replacements) {
178 auto Err = Replacements->add(Replacement);
179 if (Err) {
180 llvm::consumeError(std::move(Err));
181 auto Replace = getReplacementInChangedCode(*Replacements, Replacement);
182 *Replacements = Replacements->merge(clang::tooling::Replacements(Replace));
183 }
184}
185
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000186bool isInHeaderFile(const clang::SourceManager &SM, const clang::Decl *D,
187 llvm::StringRef OriginalRunningDirectory,
188 llvm::StringRef OldHeader) {
189 if (OldHeader.empty())
Haojian Wu357ef992016-09-21 13:18:19 +0000190 return false;
191 auto ExpansionLoc = SM.getExpansionLoc(D->getLocStart());
192 if (ExpansionLoc.isInvalid())
193 return false;
194
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000195 if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
196 return MakeAbsolutePath(SM, FE->getName()) ==
197 MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
198 }
Haojian Wu357ef992016-09-21 13:18:19 +0000199
200 return false;
201}
202
203std::vector<std::string> GetNamespaces(const clang::Decl *D) {
204 std::vector<std::string> Namespaces;
205 for (const auto *Context = D->getDeclContext(); Context;
206 Context = Context->getParent()) {
207 if (llvm::isa<clang::TranslationUnitDecl>(Context) ||
208 llvm::isa<clang::LinkageSpecDecl>(Context))
209 break;
210
211 if (const auto *ND = llvm::dyn_cast<clang::NamespaceDecl>(Context))
212 Namespaces.push_back(ND->getName().str());
213 }
214 std::reverse(Namespaces.begin(), Namespaces.end());
215 return Namespaces;
216}
217
Haojian Wu357ef992016-09-21 13:18:19 +0000218clang::tooling::Replacements
219createInsertedReplacements(const std::vector<std::string> &Includes,
220 const std::vector<ClangMoveTool::MovedDecl> &Decls,
221 llvm::StringRef FileName) {
222 clang::tooling::Replacements InsertedReplacements;
223
224 // Add #Includes.
225 std::string AllIncludesString;
Haojian Wudaf4cb82016-09-23 13:28:38 +0000226 // FIXME: Add header guard.
Haojian Wu357ef992016-09-21 13:18:19 +0000227 for (const auto &Include : Includes)
228 AllIncludesString += Include;
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000229
230 if (!AllIncludesString.empty()) {
231 clang::tooling::Replacement InsertInclude(FileName, 0, 0,
232 AllIncludesString + "\n");
233 addOrMergeReplacement(InsertInclude, &InsertedReplacements);
234 }
Haojian Wu357ef992016-09-21 13:18:19 +0000235
236 // Add moved class definition and its related declarations. All declarations
237 // in same namespace are grouped together.
238 std::vector<std::string> CurrentNamespaces;
239 for (const auto &MovedDecl : Decls) {
240 std::vector<std::string> DeclNamespaces = GetNamespaces(MovedDecl.Decl);
241 auto CurrentIt = CurrentNamespaces.begin();
242 auto DeclIt = DeclNamespaces.begin();
243 while (CurrentIt != CurrentNamespaces.end() &&
244 DeclIt != DeclNamespaces.end()) {
245 if (*CurrentIt != *DeclIt)
246 break;
247 ++CurrentIt;
248 ++DeclIt;
249 }
250 std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(),
251 CurrentIt);
252 NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end());
253 auto RemainingSize = CurrentNamespaces.end() - CurrentIt;
254 for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0;
255 --RemainingSize, ++It) {
256 assert(It < CurrentNamespaces.rend());
257 auto code = "} // namespace " + *It + "\n";
258 clang::tooling::Replacement InsertedReplacement(FileName, 0, 0, code);
259 addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
260 }
261 while (DeclIt != DeclNamespaces.end()) {
262 clang::tooling::Replacement InsertedReplacement(
263 FileName, 0, 0, "namespace " + *DeclIt + " {\n");
264 addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
265 ++DeclIt;
266 }
267
Haojian Wu357ef992016-09-21 13:18:19 +0000268 clang::tooling::Replacement InsertedReplacement(
269 FileName, 0, 0, getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM));
270 addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
271
272 CurrentNamespaces = std::move(NextNamespaces);
273 }
274 std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end());
275 for (const auto &NS : CurrentNamespaces) {
276 clang::tooling::Replacement InsertedReplacement(
277 FileName, 0, 0, "} // namespace " + NS + "\n");
278 addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
279 }
280 return InsertedReplacements;
281}
282
283} // namespace
284
285std::unique_ptr<clang::ASTConsumer>
286ClangMoveAction::CreateASTConsumer(clang::CompilerInstance &Compiler,
287 StringRef /*InFile*/) {
288 Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>(
289 &Compiler.getSourceManager(), &MoveTool));
290 return MatchFinder.newASTConsumer();
291}
292
Haojian Wu357ef992016-09-21 13:18:19 +0000293ClangMoveTool::ClangMoveTool(
Haojian Wu253d5962016-10-06 08:29:32 +0000294 const MoveDefinitionSpec &MoveSpec,
295 std::map<std::string, tooling::Replacements> &FileToReplacements,
296 llvm::StringRef OriginalRunningDirectory, llvm::StringRef FallbackStyle)
297 : Spec(MoveSpec), FileToReplacements(FileToReplacements),
298 OriginalRunningDirectory(OriginalRunningDirectory),
299 FallbackStyle(FallbackStyle) {
Renato Golin0dccd1a2016-10-07 13:58:10 +0000300 Spec.Name = llvm::StringRef(Spec.Name).ltrim(':');
Haojian Wudaf4cb82016-09-23 13:28:38 +0000301 if (!Spec.NewHeader.empty())
302 CCIncludes.push_back("#include \"" + Spec.NewHeader + "\"\n");
Haojian Wu357ef992016-09-21 13:18:19 +0000303}
304
305void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
Renato Golin0dccd1a2016-10-07 13:58:10 +0000306 std::string FullyQualifiedName = "::" + Spec.Name;
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000307 auto InOldHeader = isExpansionInFile(
308 MakeAbsolutePath(OriginalRunningDirectory, Spec.OldHeader));
309 auto InOldCC = isExpansionInFile(
310 MakeAbsolutePath(OriginalRunningDirectory, Spec.OldCC));
Haojian Wu357ef992016-09-21 13:18:19 +0000311 auto InOldFiles = anyOf(InOldHeader, InOldCC);
312 auto InMovedClass =
Renato Golin0dccd1a2016-10-07 13:58:10 +0000313 hasDeclContext(cxxRecordDecl(hasName(FullyQualifiedName)));
Haojian Wu357ef992016-09-21 13:18:19 +0000314
315 // Match moved class declarations.
316 auto MovedClass = cxxRecordDecl(
Renato Golin0dccd1a2016-10-07 13:58:10 +0000317 InOldFiles, hasName(FullyQualifiedName), isDefinition(),
Haojian Wu357ef992016-09-21 13:18:19 +0000318 hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())));
319 Finder->addMatcher(MovedClass.bind("moved_class"), this);
320
321 // Match moved class methods (static methods included) which are defined
322 // outside moved class declaration.
323 Finder->addMatcher(cxxMethodDecl(InOldFiles,
Renato Golin0dccd1a2016-10-07 13:58:10 +0000324 ofClass(hasName(FullyQualifiedName)),
Haojian Wu357ef992016-09-21 13:18:19 +0000325 isDefinition())
326 .bind("class_method"),
327 this);
328
329 // Match static member variable definition of the moved class.
330 Finder->addMatcher(varDecl(InMovedClass, InOldCC, isDefinition())
331 .bind("class_static_var_decl"),
332 this);
333
334 auto inAnonymousNamespace = hasParent(namespaceDecl(isAnonymous()));
335 // Match functions/variables definitions which are defined in anonymous
336 // namespace in old cc.
337 Finder->addMatcher(
338 namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
339 inAnonymousNamespace)
340 .bind("decls_in_anonymous_ns"),
341 this);
342
343 // Match static functions/variabale definitions in old cc.
344 Finder->addMatcher(
345 namedDecl(anyOf(functionDecl(isDefinition(), unless(InMovedClass),
Haojian Wuef247cb2016-09-27 08:01:04 +0000346 isStaticStorageClass(), InOldCC),
347 varDecl(isDefinition(), unless(InMovedClass),
348 isStaticStorageClass(), InOldCC)))
Haojian Wu357ef992016-09-21 13:18:19 +0000349 .bind("static_decls"),
350 this);
351
352 // Match forward declarations in old header.
353 Finder->addMatcher(
354 cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())), InOldHeader)
355 .bind("fwd_decl"),
356 this);
357}
358
359void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) {
360 if (const auto *CMD =
361 Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method")) {
362 // Skip inline class methods. isInline() ast matcher doesn't ignore this
363 // case.
364 if (!CMD->isInlined()) {
365 MovedDecls.emplace_back(CMD, &Result.Context->getSourceManager());
366 RemovedDecls.push_back(MovedDecls.back());
367 }
368 } else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>(
369 "class_static_var_decl")) {
370 MovedDecls.emplace_back(VD, &Result.Context->getSourceManager());
371 RemovedDecls.push_back(MovedDecls.back());
372 } else if (const auto *class_decl =
373 Result.Nodes.getNodeAs<clang::CXXRecordDecl>("moved_class")) {
374 MovedDecls.emplace_back(class_decl, &Result.Context->getSourceManager());
375 RemovedDecls.push_back(MovedDecls.back());
376 } else if (const auto *FWD =
377 Result.Nodes.getNodeAs<clang::CXXRecordDecl>("fwd_decl")) {
378 // Skip all forwad declarations which appear after moved class declaration.
379 if (RemovedDecls.empty())
380 MovedDecls.emplace_back(FWD, &Result.Context->getSourceManager());
381 } else if (const auto *FD = Result.Nodes.getNodeAs<clang::NamedDecl>(
382 "decls_in_anonymous_ns")) {
383 MovedDecls.emplace_back(FD, &Result.Context->getSourceManager());
384 } else if (const auto *ND =
385 Result.Nodes.getNodeAs<clang::NamedDecl>("static_decls")) {
386 MovedDecls.emplace_back(ND, &Result.Context->getSourceManager());
387 }
388}
389
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000390void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader,
391 bool IsAngled,
392 llvm::StringRef SearchPath,
393 llvm::StringRef FileName,
394 const SourceManager& SM) {
Haojian Wudb726572016-10-12 15:50:30 +0000395 SmallVector<char, 128> HeaderWithSearchPath;
396 llvm::sys::path::append(HeaderWithSearchPath, SearchPath, IncludeHeader);
397 std::string AbsoluteOldHeader =
398 MakeAbsolutePath(OriginalRunningDirectory, Spec.OldHeader);
Haojian Wudaf4cb82016-09-23 13:28:38 +0000399 // FIXME: Add old.h to the new.cc/h when the new target has dependencies on
400 // old.h/c. For instance, when moved class uses another class defined in
401 // old.h, the old.h should be added in new.h.
Haojian Wudb726572016-10-12 15:50:30 +0000402 if (AbsoluteOldHeader ==
403 MakeAbsolutePath(SM, llvm::StringRef(HeaderWithSearchPath.data(),
404 HeaderWithSearchPath.size())))
Haojian Wudaf4cb82016-09-23 13:28:38 +0000405 return;
406
407 std::string IncludeLine =
408 IsAngled ? ("#include <" + IncludeHeader + ">\n").str()
409 : ("#include \"" + IncludeHeader + "\"\n").str();
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000410
Haojian Wudb726572016-10-12 15:50:30 +0000411 std::string AbsoluteCurrentFile = MakeAbsolutePath(SM, FileName);
412 if (AbsoluteOldHeader == AbsoluteCurrentFile) {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000413 HeaderIncludes.push_back(IncludeLine);
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000414 } else if (MakeAbsolutePath(OriginalRunningDirectory, Spec.OldCC) ==
Haojian Wudb726572016-10-12 15:50:30 +0000415 AbsoluteCurrentFile) {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000416 CCIncludes.push_back(IncludeLine);
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000417 }
Haojian Wu357ef992016-09-21 13:18:19 +0000418}
419
420void ClangMoveTool::removeClassDefinitionInOldFiles() {
421 for (const auto &MovedDecl : RemovedDecls) {
Haojian Wu253d5962016-10-06 08:29:32 +0000422 const auto &SM = *MovedDecl.SM;
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000423 auto Range = GetFullRange(&SM, MovedDecl.Decl);
Haojian Wu357ef992016-09-21 13:18:19 +0000424 clang::tooling::Replacement RemoveReplacement(
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000425 *MovedDecl.SM, clang::CharSourceRange::getCharRange(
426 Range.getBegin(), Range.getEnd()),
Haojian Wu357ef992016-09-21 13:18:19 +0000427 "");
428 std::string FilePath = RemoveReplacement.getFilePath().str();
429 addOrMergeReplacement(RemoveReplacement, &FileToReplacements[FilePath]);
Haojian Wu253d5962016-10-06 08:29:32 +0000430
431 llvm::StringRef Code =
432 SM.getBufferData(SM.getFileID(MovedDecl.Decl->getLocation()));
433 format::FormatStyle Style =
434 format::getStyle("file", FilePath, FallbackStyle);
435 auto CleanReplacements = format::cleanupAroundReplacements(
436 Code, FileToReplacements[FilePath], Style);
437
438 if (!CleanReplacements) {
439 llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
440 continue;
441 }
442 FileToReplacements[FilePath] = *CleanReplacements;
Haojian Wu357ef992016-09-21 13:18:19 +0000443 }
444}
445
446void ClangMoveTool::moveClassDefinitionToNewFiles() {
447 std::vector<MovedDecl> NewHeaderDecls;
448 std::vector<MovedDecl> NewCCDecls;
449 for (const auto &MovedDecl : MovedDecls) {
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000450 if (isInHeaderFile(*MovedDecl.SM, MovedDecl.Decl, OriginalRunningDirectory,
451 Spec.OldHeader))
Haojian Wu357ef992016-09-21 13:18:19 +0000452 NewHeaderDecls.push_back(MovedDecl);
453 else
454 NewCCDecls.push_back(MovedDecl);
455 }
456
457 if (!Spec.NewHeader.empty())
458 FileToReplacements[Spec.NewHeader] = createInsertedReplacements(
459 HeaderIncludes, NewHeaderDecls, Spec.NewHeader);
460 if (!Spec.NewCC.empty())
461 FileToReplacements[Spec.NewCC] =
462 createInsertedReplacements(CCIncludes, NewCCDecls, Spec.NewCC);
463}
464
465void ClangMoveTool::onEndOfTranslationUnit() {
466 if (RemovedDecls.empty())
467 return;
468 removeClassDefinitionInOldFiles();
469 moveClassDefinitionToNewFiles();
470}
471
472} // namespace move
473} // namespace clang