blob: d4d0af83e3798bbe235499ba04d53b43790d2625 [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;
150 MoveTool->getMovedDecls().emplace_back(D,
151 &Result.Context->getSourceManager());
152 MoveTool->getUnremovedDeclsInOldHeader().erase(D);
Haojian Wu48ac3042016-11-23 10:04:19 +0000153 MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
Haojian Wu4543fec2016-11-16 13:05:19 +0000154 }
155
156private:
157 ClangMoveTool *MoveTool;
158};
159
Haojian Wu35ca9462016-11-14 14:15:44 +0000160class ClassDeclarationMatch : public MatchFinder::MatchCallback {
161public:
162 explicit ClassDeclarationMatch(ClangMoveTool *MoveTool)
163 : MoveTool(MoveTool) {}
164 void run(const MatchFinder::MatchResult &Result) override {
165 clang::SourceManager* SM = &Result.Context->getSourceManager();
166 if (const auto *CMD =
167 Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method"))
168 MatchClassMethod(CMD, SM);
169 else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>(
170 "class_static_var_decl"))
171 MatchClassStaticVariable(VD, SM);
172 else if (const auto *CD = Result.Nodes.getNodeAs<clang::CXXRecordDecl>(
173 "moved_class"))
174 MatchClassDeclaration(CD, SM);
175 }
176
177private:
178 void MatchClassMethod(const clang::CXXMethodDecl* CMD,
179 clang::SourceManager* SM) {
180 // Skip inline class methods. isInline() ast matcher doesn't ignore this
181 // case.
182 if (!CMD->isInlined()) {
183 MoveTool->getMovedDecls().emplace_back(CMD, SM);
Haojian Wu48ac3042016-11-23 10:04:19 +0000184 MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
Haojian Wu35ca9462016-11-14 14:15:44 +0000185 // Get template class method from its method declaration as
186 // UnremovedDecls stores template class method.
187 if (const auto *FTD = CMD->getDescribedFunctionTemplate())
188 MoveTool->getUnremovedDeclsInOldHeader().erase(FTD);
189 else
190 MoveTool->getUnremovedDeclsInOldHeader().erase(CMD);
191 }
192 }
193
194 void MatchClassStaticVariable(const clang::NamedDecl *VD,
195 clang::SourceManager* SM) {
196 MoveTool->getMovedDecls().emplace_back(VD, SM);
Haojian Wu48ac3042016-11-23 10:04:19 +0000197 MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
Haojian Wu35ca9462016-11-14 14:15:44 +0000198 MoveTool->getUnremovedDeclsInOldHeader().erase(VD);
199 }
200
201 void MatchClassDeclaration(const clang::CXXRecordDecl *CD,
202 clang::SourceManager* SM) {
203 // Get class template from its class declaration as UnremovedDecls stores
204 // class template.
205 if (const auto *TC = CD->getDescribedClassTemplate())
206 MoveTool->getMovedDecls().emplace_back(TC, SM);
207 else
208 MoveTool->getMovedDecls().emplace_back(CD, SM);
Haojian Wu48ac3042016-11-23 10:04:19 +0000209 MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
Haojian Wu35ca9462016-11-14 14:15:44 +0000210 MoveTool->getUnremovedDeclsInOldHeader().erase(
211 MoveTool->getMovedDecls().back().Decl);
212 }
213
214 ClangMoveTool *MoveTool;
215};
216
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000217// Expand to get the end location of the line where the EndLoc of the given
218// Decl.
219SourceLocation
220getLocForEndOfDecl(const clang::Decl *D, const SourceManager *SM,
221 const LangOptions &LangOpts = clang::LangOptions()) {
222 std::pair<FileID, unsigned> LocInfo = SM->getDecomposedLoc(D->getLocEnd());
223 // Try to load the file buffer.
224 bool InvalidTemp = false;
225 llvm::StringRef File = SM->getBufferData(LocInfo.first, &InvalidTemp);
226 if (InvalidTemp)
227 return SourceLocation();
228
229 const char *TokBegin = File.data() + LocInfo.second;
230 // Lex from the start of the given location.
231 Lexer Lex(SM->getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
232 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'.
242 return SM->getLocForEndOfFile(LocInfo.first) == EndLoc
243 ? EndLoc
244 : EndLoc.getLocWithOffset(1);
245}
246
247// Get full range of a Decl including the comments associated with it.
248clang::CharSourceRange
249GetFullRange(const clang::SourceManager *SM, const clang::Decl *D,
250 const clang::LangOptions &options = clang::LangOptions()) {
Haojian Wu24675392016-11-14 14:46:48 +0000251 clang::SourceRange Full(SM->getExpansionLoc(D->getLocStart()),
252 getLocForEndOfDecl(D, SM));
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000253 // Expand to comments that are associated with the Decl.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000254 if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) {
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000255 if (SM->isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd()))
256 Full.setEnd(Comment->getLocEnd());
257 // FIXME: Don't delete a preceding comment, if there are no other entities
258 // it could refer to.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000259 if (SM->isBeforeInTranslationUnit(Comment->getLocStart(), Full.getBegin()))
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000260 Full.setBegin(Comment->getLocStart());
261 }
262
263 return clang::CharSourceRange::getCharRange(Full);
264}
265
266std::string getDeclarationSourceText(const clang::Decl *D,
267 const clang::SourceManager *SM) {
268 llvm::StringRef SourceText = clang::Lexer::getSourceText(
269 GetFullRange(SM, D), *SM, clang::LangOptions());
270 return SourceText.str();
271}
272
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000273bool isInHeaderFile(const clang::SourceManager &SM, const clang::Decl *D,
274 llvm::StringRef OriginalRunningDirectory,
275 llvm::StringRef OldHeader) {
276 if (OldHeader.empty())
Haojian Wu357ef992016-09-21 13:18:19 +0000277 return false;
278 auto ExpansionLoc = SM.getExpansionLoc(D->getLocStart());
279 if (ExpansionLoc.isInvalid())
280 return false;
281
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000282 if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
283 return MakeAbsolutePath(SM, FE->getName()) ==
284 MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
285 }
Haojian Wu357ef992016-09-21 13:18:19 +0000286
287 return false;
288}
289
290std::vector<std::string> GetNamespaces(const clang::Decl *D) {
291 std::vector<std::string> Namespaces;
292 for (const auto *Context = D->getDeclContext(); Context;
293 Context = Context->getParent()) {
294 if (llvm::isa<clang::TranslationUnitDecl>(Context) ||
295 llvm::isa<clang::LinkageSpecDecl>(Context))
296 break;
297
298 if (const auto *ND = llvm::dyn_cast<clang::NamespaceDecl>(Context))
299 Namespaces.push_back(ND->getName().str());
300 }
301 std::reverse(Namespaces.begin(), Namespaces.end());
302 return Namespaces;
303}
304
Haojian Wu357ef992016-09-21 13:18:19 +0000305clang::tooling::Replacements
306createInsertedReplacements(const std::vector<std::string> &Includes,
307 const std::vector<ClangMoveTool::MovedDecl> &Decls,
Haojian Wu48ac3042016-11-23 10:04:19 +0000308 llvm::StringRef FileName, bool IsHeader = false,
309 StringRef OldHeaderInclude = "") {
Haojian Wu53eab1e2016-10-14 13:43:49 +0000310 std::string NewCode;
Haojian Wu220c7552016-10-14 13:01:36 +0000311 std::string GuardName(FileName);
312 if (IsHeader) {
Haojian Wuac97fc32016-10-17 15:26:34 +0000313 for (size_t i = 0; i < GuardName.size(); ++i) {
314 if (!isAlphanumeric(GuardName[i]))
315 GuardName[i] = '_';
316 }
Haojian Wu220c7552016-10-14 13:01:36 +0000317 GuardName = StringRef(GuardName).upper();
Haojian Wu53eab1e2016-10-14 13:43:49 +0000318 NewCode += "#ifndef " + GuardName + "\n";
Haojian Wu53315a72016-11-15 09:06:59 +0000319 NewCode += "#define " + GuardName + "\n\n";
Haojian Wu220c7552016-10-14 13:01:36 +0000320 }
Haojian Wu357ef992016-09-21 13:18:19 +0000321
Haojian Wu48ac3042016-11-23 10:04:19 +0000322 NewCode += OldHeaderInclude;
Haojian Wu357ef992016-09-21 13:18:19 +0000323 // Add #Includes.
Haojian Wu357ef992016-09-21 13:18:19 +0000324 for (const auto &Include : Includes)
Haojian Wu53eab1e2016-10-14 13:43:49 +0000325 NewCode += Include;
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000326
Haojian Wu53eab1e2016-10-14 13:43:49 +0000327 if (!Includes.empty())
328 NewCode += "\n";
Haojian Wu357ef992016-09-21 13:18:19 +0000329
330 // Add moved class definition and its related declarations. All declarations
331 // in same namespace are grouped together.
Haojian Wu53315a72016-11-15 09:06:59 +0000332 //
333 // Record namespaces where the current position is in.
Haojian Wu357ef992016-09-21 13:18:19 +0000334 std::vector<std::string> CurrentNamespaces;
335 for (const auto &MovedDecl : Decls) {
Haojian Wu53315a72016-11-15 09:06:59 +0000336 // The namespaces of the declaration being moved.
Haojian Wu357ef992016-09-21 13:18:19 +0000337 std::vector<std::string> DeclNamespaces = GetNamespaces(MovedDecl.Decl);
338 auto CurrentIt = CurrentNamespaces.begin();
339 auto DeclIt = DeclNamespaces.begin();
Haojian Wu53315a72016-11-15 09:06:59 +0000340 // Skip the common prefix.
Haojian Wu357ef992016-09-21 13:18:19 +0000341 while (CurrentIt != CurrentNamespaces.end() &&
342 DeclIt != DeclNamespaces.end()) {
343 if (*CurrentIt != *DeclIt)
344 break;
345 ++CurrentIt;
346 ++DeclIt;
347 }
Haojian Wu53315a72016-11-15 09:06:59 +0000348 // Calculate the new namespaces after adding MovedDecl in CurrentNamespace,
349 // which is used for next iteration of this loop.
Haojian Wu357ef992016-09-21 13:18:19 +0000350 std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(),
351 CurrentIt);
352 NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end());
Haojian Wu53315a72016-11-15 09:06:59 +0000353
354
355 // End with CurrentNamespace.
356 bool HasEndCurrentNamespace = false;
Haojian Wu357ef992016-09-21 13:18:19 +0000357 auto RemainingSize = CurrentNamespaces.end() - CurrentIt;
358 for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0;
359 --RemainingSize, ++It) {
360 assert(It < CurrentNamespaces.rend());
Haojian Wu53eab1e2016-10-14 13:43:49 +0000361 NewCode += "} // namespace " + *It + "\n";
Haojian Wu53315a72016-11-15 09:06:59 +0000362 HasEndCurrentNamespace = true;
Haojian Wu357ef992016-09-21 13:18:19 +0000363 }
Haojian Wu53315a72016-11-15 09:06:59 +0000364 // Add trailing '\n' after the nested namespace definition.
365 if (HasEndCurrentNamespace)
366 NewCode += "\n";
367
368 // If the moved declaration is not in CurrentNamespace, add extra namespace
369 // definitions.
370 bool IsInNewNamespace = false;
Haojian Wu357ef992016-09-21 13:18:19 +0000371 while (DeclIt != DeclNamespaces.end()) {
Haojian Wu53eab1e2016-10-14 13:43:49 +0000372 NewCode += "namespace " + *DeclIt + " {\n";
Haojian Wu53315a72016-11-15 09:06:59 +0000373 IsInNewNamespace = true;
Haojian Wu357ef992016-09-21 13:18:19 +0000374 ++DeclIt;
375 }
Haojian Wu53315a72016-11-15 09:06:59 +0000376 // If the moved declaration is in same namespace CurrentNamespace, add
377 // a preceeding `\n' before the moved declaration.
Haojian Wu50a45d92016-11-18 10:51:16 +0000378 // FIXME: Don't add empty lines between using declarations.
Haojian Wu53315a72016-11-15 09:06:59 +0000379 if (!IsInNewNamespace)
380 NewCode += "\n";
Haojian Wu53eab1e2016-10-14 13:43:49 +0000381 NewCode += getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM);
Haojian Wu357ef992016-09-21 13:18:19 +0000382 CurrentNamespaces = std::move(NextNamespaces);
383 }
384 std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end());
Haojian Wu53eab1e2016-10-14 13:43:49 +0000385 for (const auto &NS : CurrentNamespaces)
386 NewCode += "} // namespace " + NS + "\n";
Haojian Wu220c7552016-10-14 13:01:36 +0000387
Haojian Wu53eab1e2016-10-14 13:43:49 +0000388 if (IsHeader)
Haojian Wu53315a72016-11-15 09:06:59 +0000389 NewCode += "\n#endif // " + GuardName + "\n";
Haojian Wu53eab1e2016-10-14 13:43:49 +0000390 return clang::tooling::Replacements(
391 clang::tooling::Replacement(FileName, 0, 0, NewCode));
Haojian Wu357ef992016-09-21 13:18:19 +0000392}
393
394} // namespace
395
396std::unique_ptr<clang::ASTConsumer>
397ClangMoveAction::CreateASTConsumer(clang::CompilerInstance &Compiler,
398 StringRef /*InFile*/) {
399 Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>(
400 &Compiler.getSourceManager(), &MoveTool));
401 return MatchFinder.newASTConsumer();
402}
403
Haojian Wu357ef992016-09-21 13:18:19 +0000404ClangMoveTool::ClangMoveTool(
Haojian Wu253d5962016-10-06 08:29:32 +0000405 const MoveDefinitionSpec &MoveSpec,
406 std::map<std::string, tooling::Replacements> &FileToReplacements,
407 llvm::StringRef OriginalRunningDirectory, llvm::StringRef FallbackStyle)
408 : Spec(MoveSpec), FileToReplacements(FileToReplacements),
409 OriginalRunningDirectory(OriginalRunningDirectory),
410 FallbackStyle(FallbackStyle) {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000411 if (!Spec.NewHeader.empty())
412 CCIncludes.push_back("#include \"" + Spec.NewHeader + "\"\n");
Haojian Wu357ef992016-09-21 13:18:19 +0000413}
414
Haojian Wu48ac3042016-11-23 10:04:19 +0000415void ClangMoveTool::addRemovedDecl(const MovedDecl &Decl) {
416 const auto &SM = *Decl.SM;
417 auto Loc = Decl.Decl->getLocation();
418 StringRef FilePath = SM.getFilename(Loc);
419 FilePathToFileID[FilePath] = SM.getFileID(Loc);
420 RemovedDecls.push_back(Decl);
421}
422
Haojian Wu357ef992016-09-21 13:18:19 +0000423void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
Haojian Wu4543fec2016-11-16 13:05:19 +0000424 Optional<ast_matchers::internal::Matcher<NamedDecl>> HasAnySymbolNames;
425 for (StringRef SymbolName: Spec.Names) {
426 llvm::StringRef GlobalSymbolName = SymbolName.trim().ltrim(':');
427 const auto HasName = hasName(("::" + GlobalSymbolName).str());
428 HasAnySymbolNames =
429 HasAnySymbolNames ? anyOf(*HasAnySymbolNames, HasName) : HasName;
Haojian Wu9df3ac12016-10-13 08:48:42 +0000430 }
Haojian Wu4543fec2016-11-16 13:05:19 +0000431 if (!HasAnySymbolNames) {
432 llvm::errs() << "No symbols being moved.\n";
Haojian Wu9df3ac12016-10-13 08:48:42 +0000433 return;
434 }
435
Haojian Wu2930be12016-11-08 19:55:13 +0000436 auto InOldHeader = isExpansionInFile(makeAbsolutePath(Spec.OldHeader));
437 auto InOldCC = isExpansionInFile(makeAbsolutePath(Spec.OldCC));
Haojian Wu357ef992016-09-21 13:18:19 +0000438 auto InOldFiles = anyOf(InOldHeader, InOldCC);
439 auto InMovedClass =
Haojian Wu4543fec2016-11-16 13:05:19 +0000440 hasOutermostEnclosingClass(cxxRecordDecl(*HasAnySymbolNames));
Haojian Wu357ef992016-09-21 13:18:19 +0000441
Haojian Wu2930be12016-11-08 19:55:13 +0000442 auto ForwardDecls =
443 cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())));
444
445 //============================================================================
446 // Matchers for old header
447 //============================================================================
448 // Match all top-level named declarations (e.g. function, variable, enum) in
449 // old header, exclude forward class declarations and namespace declarations.
450 //
451 // The old header which contains only one declaration being moved and forward
452 // declarations is considered to be moved totally.
453 auto AllDeclsInHeader = namedDecl(
454 unless(ForwardDecls), unless(namespaceDecl()),
455 unless(usingDirectiveDecl()), // using namespace decl.
456 unless(classTemplateDecl(has(ForwardDecls))), // template forward decl.
457 InOldHeader,
458 hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))));
459 Finder->addMatcher(AllDeclsInHeader.bind("decls_in_header"), this);
460 // Match forward declarations in old header.
461 Finder->addMatcher(namedDecl(ForwardDecls, InOldHeader).bind("fwd_decl"),
462 this);
463
464 //============================================================================
Haojian Wu2930be12016-11-08 19:55:13 +0000465 // Matchers for old cc
466 //============================================================================
Haojian Wu50a45d92016-11-18 10:51:16 +0000467 auto InOldCCNamedOrGlobalNamespace =
468 allOf(hasParent(decl(anyOf(namespaceDecl(unless(isAnonymous())),
469 translationUnitDecl()))),
470 InOldCC);
471 // Matching using decls/type alias decls which are in named namespace or
472 // global namespace. Those in classes, functions and anonymous namespaces are
473 // covered in other matchers.
Haojian Wu357ef992016-09-21 13:18:19 +0000474 Finder->addMatcher(
Haojian Wu50a45d92016-11-18 10:51:16 +0000475 namedDecl(anyOf(usingDecl(InOldCCNamedOrGlobalNamespace),
476 usingDirectiveDecl(InOldCCNamedOrGlobalNamespace),
477 typeAliasDecl( InOldCCNamedOrGlobalNamespace)))
Haojian Wu67bb6512016-10-19 14:13:21 +0000478 .bind("using_decl"),
Haojian Wu357ef992016-09-21 13:18:19 +0000479 this);
480
Haojian Wu67bb6512016-10-19 14:13:21 +0000481 // Match anonymous namespace decl in old cc.
482 Finder->addMatcher(namespaceDecl(isAnonymous(), InOldCC).bind("anonymous_ns"),
483 this);
484
485 // Match static functions/variable definitions which are defined in named
486 // namespaces.
487 auto IsOldCCStaticDefinition =
Haojian Wu50a45d92016-11-18 10:51:16 +0000488 allOf(isDefinition(), unless(InMovedClass), InOldCCNamedOrGlobalNamespace,
Haojian Wu67bb6512016-10-19 14:13:21 +0000489 isStaticStorageClass());
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000490 Finder->addMatcher(namedDecl(anyOf(functionDecl(IsOldCCStaticDefinition),
491 varDecl(IsOldCCStaticDefinition)))
492 .bind("static_decls"),
493 this);
Haojian Wu35ca9462016-11-14 14:15:44 +0000494
495 //============================================================================
496 // Matchers for old files, including old.h/old.cc
497 //============================================================================
498 // Create a MatchCallback for class declarations.
499 MatchCallbacks.push_back(llvm::make_unique<ClassDeclarationMatch>(this));
500 // Match moved class declarations.
501 auto MovedClass =
502 cxxRecordDecl(
Haojian Wu4543fec2016-11-16 13:05:19 +0000503 InOldFiles, *HasAnySymbolNames, isDefinition(),
Haojian Wu35ca9462016-11-14 14:15:44 +0000504 hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())))
505 .bind("moved_class");
506 Finder->addMatcher(MovedClass, MatchCallbacks.back().get());
507 // Match moved class methods (static methods included) which are defined
508 // outside moved class declaration.
509 Finder->addMatcher(
Haojian Wu4543fec2016-11-16 13:05:19 +0000510 cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*HasAnySymbolNames),
Haojian Wu35ca9462016-11-14 14:15:44 +0000511 isDefinition())
512 .bind("class_method"),
513 MatchCallbacks.back().get());
514 // Match static member variable definition of the moved class.
515 Finder->addMatcher(
516 varDecl(InMovedClass, InOldFiles, isDefinition(), isStaticDataMember())
517 .bind("class_static_var_decl"),
518 MatchCallbacks.back().get());
519
Haojian Wu4543fec2016-11-16 13:05:19 +0000520 MatchCallbacks.push_back(llvm::make_unique<FunctionDeclarationMatch>(this));
521 Finder->addMatcher(functionDecl(InOldFiles, *HasAnySymbolNames,
522 anyOf(hasDeclContext(namespaceDecl()),
523 hasDeclContext(translationUnitDecl())))
524 .bind("function"),
525 MatchCallbacks.back().get());
Haojian Wu357ef992016-09-21 13:18:19 +0000526}
527
528void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) {
Haojian Wu2930be12016-11-08 19:55:13 +0000529 if (const auto *D =
530 Result.Nodes.getNodeAs<clang::NamedDecl>("decls_in_header")) {
531 UnremovedDeclsInOldHeader.insert(D);
Haojian Wu357ef992016-09-21 13:18:19 +0000532 } else if (const auto *FWD =
533 Result.Nodes.getNodeAs<clang::CXXRecordDecl>("fwd_decl")) {
534 // Skip all forwad declarations which appear after moved class declaration.
Haojian Wu29c38f72016-10-21 19:26:43 +0000535 if (RemovedDecls.empty()) {
Haojian Wub53ec462016-11-10 05:33:26 +0000536 if (const auto *DCT = FWD->getDescribedClassTemplate())
Haojian Wu29c38f72016-10-21 19:26:43 +0000537 MovedDecls.emplace_back(DCT, &Result.Context->getSourceManager());
Haojian Wub53ec462016-11-10 05:33:26 +0000538 else
Haojian Wu29c38f72016-10-21 19:26:43 +0000539 MovedDecls.emplace_back(FWD, &Result.Context->getSourceManager());
Haojian Wu29c38f72016-10-21 19:26:43 +0000540 }
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000541 } else if (const auto *ANS =
542 Result.Nodes.getNodeAs<clang::NamespaceDecl>("anonymous_ns")) {
Haojian Wu67bb6512016-10-19 14:13:21 +0000543 MovedDecls.emplace_back(ANS, &Result.Context->getSourceManager());
Haojian Wu357ef992016-09-21 13:18:19 +0000544 } else if (const auto *ND =
545 Result.Nodes.getNodeAs<clang::NamedDecl>("static_decls")) {
546 MovedDecls.emplace_back(ND, &Result.Context->getSourceManager());
Haojian Wu67bb6512016-10-19 14:13:21 +0000547 } else if (const auto *UD =
548 Result.Nodes.getNodeAs<clang::NamedDecl>("using_decl")) {
549 MovedDecls.emplace_back(UD, &Result.Context->getSourceManager());
Haojian Wu357ef992016-09-21 13:18:19 +0000550 }
551}
552
Haojian Wu2930be12016-11-08 19:55:13 +0000553std::string ClangMoveTool::makeAbsolutePath(StringRef Path) {
554 return MakeAbsolutePath(OriginalRunningDirectory, Path);
555}
556
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000557void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader, bool IsAngled,
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000558 llvm::StringRef SearchPath,
559 llvm::StringRef FileName,
Haojian Wu2930be12016-11-08 19:55:13 +0000560 clang::CharSourceRange IncludeFilenameRange,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000561 const SourceManager &SM) {
Haojian Wudb726572016-10-12 15:50:30 +0000562 SmallVector<char, 128> HeaderWithSearchPath;
563 llvm::sys::path::append(HeaderWithSearchPath, SearchPath, IncludeHeader);
Haojian Wu2930be12016-11-08 19:55:13 +0000564 std::string AbsoluteOldHeader = makeAbsolutePath(Spec.OldHeader);
Haojian Wudaf4cb82016-09-23 13:28:38 +0000565 // FIXME: Add old.h to the new.cc/h when the new target has dependencies on
566 // old.h/c. For instance, when moved class uses another class defined in
567 // old.h, the old.h should be added in new.h.
Haojian Wudb726572016-10-12 15:50:30 +0000568 if (AbsoluteOldHeader ==
569 MakeAbsolutePath(SM, llvm::StringRef(HeaderWithSearchPath.data(),
Haojian Wu2930be12016-11-08 19:55:13 +0000570 HeaderWithSearchPath.size()))) {
571 OldHeaderIncludeRange = IncludeFilenameRange;
Haojian Wudaf4cb82016-09-23 13:28:38 +0000572 return;
Haojian Wu2930be12016-11-08 19:55:13 +0000573 }
Haojian Wudaf4cb82016-09-23 13:28:38 +0000574
575 std::string IncludeLine =
576 IsAngled ? ("#include <" + IncludeHeader + ">\n").str()
577 : ("#include \"" + IncludeHeader + "\"\n").str();
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000578
Haojian Wudb726572016-10-12 15:50:30 +0000579 std::string AbsoluteCurrentFile = MakeAbsolutePath(SM, FileName);
580 if (AbsoluteOldHeader == AbsoluteCurrentFile) {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000581 HeaderIncludes.push_back(IncludeLine);
Haojian Wu2930be12016-11-08 19:55:13 +0000582 } else if (makeAbsolutePath(Spec.OldCC) == AbsoluteCurrentFile) {
Haojian Wudaf4cb82016-09-23 13:28:38 +0000583 CCIncludes.push_back(IncludeLine);
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000584 }
Haojian Wu357ef992016-09-21 13:18:19 +0000585}
586
587void ClangMoveTool::removeClassDefinitionInOldFiles() {
Haojian Wu48ac3042016-11-23 10:04:19 +0000588 if (RemovedDecls.empty()) return;
Haojian Wu357ef992016-09-21 13:18:19 +0000589 for (const auto &MovedDecl : RemovedDecls) {
Haojian Wu253d5962016-10-06 08:29:32 +0000590 const auto &SM = *MovedDecl.SM;
Haojian Wu9abbeaa2016-10-06 08:59:24 +0000591 auto Range = GetFullRange(&SM, MovedDecl.Decl);
Haojian Wu357ef992016-09-21 13:18:19 +0000592 clang::tooling::Replacement RemoveReplacement(
Haojian Wu48ac3042016-11-23 10:04:19 +0000593 SM,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000594 clang::CharSourceRange::getCharRange(Range.getBegin(), Range.getEnd()),
Haojian Wu357ef992016-09-21 13:18:19 +0000595 "");
596 std::string FilePath = RemoveReplacement.getFilePath().str();
Haojian Wu53eab1e2016-10-14 13:43:49 +0000597 auto Err = FileToReplacements[FilePath].add(RemoveReplacement);
Haojian Wu48ac3042016-11-23 10:04:19 +0000598 if (Err)
Haojian Wu53eab1e2016-10-14 13:43:49 +0000599 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Haojian Wu48ac3042016-11-23 10:04:19 +0000600 }
601 const SourceManager* SM = RemovedDecls[0].SM;
602
603 // Post process of cleanup around all the replacements.
604 for (auto& FileAndReplacements: FileToReplacements) {
605 StringRef FilePath = FileAndReplacements.first;
606 // Add #include of new header to old header.
607 if (Spec.OldDependOnNew &&
608 MakeAbsolutePath(*SM, FilePath) == makeAbsolutePath(Spec.OldHeader)) {
609 // FIXME: Minimize the include path like include-fixer.
610 std::string IncludeNewH = "#include \"" + Spec.NewHeader + "\"\n";
611 // This replacment for inserting header will be cleaned up at the end.
612 auto Err = FileAndReplacements.second.add(
613 tooling::Replacement(FilePath, UINT_MAX, 0, IncludeNewH));
614 if (Err)
615 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Haojian Wu53eab1e2016-10-14 13:43:49 +0000616 }
Haojian Wu253d5962016-10-06 08:29:32 +0000617
Haojian Wu48ac3042016-11-23 10:04:19 +0000618 auto SI = FilePathToFileID.find(FilePath);
619 // Ignore replacements for new.h/cc.
620 if (SI == FilePathToFileID.end()) continue;
621 llvm::StringRef Code = SM->getBufferData(SI->second);
Haojian Wu253d5962016-10-06 08:29:32 +0000622 format::FormatStyle Style =
623 format::getStyle("file", FilePath, FallbackStyle);
624 auto CleanReplacements = format::cleanupAroundReplacements(
625 Code, FileToReplacements[FilePath], Style);
626
627 if (!CleanReplacements) {
628 llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
629 continue;
630 }
631 FileToReplacements[FilePath] = *CleanReplacements;
Haojian Wu357ef992016-09-21 13:18:19 +0000632 }
633}
634
635void ClangMoveTool::moveClassDefinitionToNewFiles() {
636 std::vector<MovedDecl> NewHeaderDecls;
637 std::vector<MovedDecl> NewCCDecls;
638 for (const auto &MovedDecl : MovedDecls) {
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000639 if (isInHeaderFile(*MovedDecl.SM, MovedDecl.Decl, OriginalRunningDirectory,
640 Spec.OldHeader))
Haojian Wu357ef992016-09-21 13:18:19 +0000641 NewHeaderDecls.push_back(MovedDecl);
642 else
643 NewCCDecls.push_back(MovedDecl);
644 }
645
Haojian Wu48ac3042016-11-23 10:04:19 +0000646 if (!Spec.NewHeader.empty()) {
647 std::string OldHeaderInclude =
648 Spec.NewDependOnOld ? "#include \"" + Spec.OldHeader + "\"\n" : "";
Haojian Wu357ef992016-09-21 13:18:19 +0000649 FileToReplacements[Spec.NewHeader] = createInsertedReplacements(
Haojian Wu48ac3042016-11-23 10:04:19 +0000650 HeaderIncludes, NewHeaderDecls, Spec.NewHeader, /*IsHeader=*/true,
651 OldHeaderInclude);
652 }
Haojian Wu357ef992016-09-21 13:18:19 +0000653 if (!Spec.NewCC.empty())
654 FileToReplacements[Spec.NewCC] =
655 createInsertedReplacements(CCIncludes, NewCCDecls, Spec.NewCC);
656}
657
Haojian Wu2930be12016-11-08 19:55:13 +0000658// Move all contents from OldFile to NewFile.
659void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile,
660 StringRef NewFile) {
661 const FileEntry *FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile));
662 if (!FE) {
663 llvm::errs() << "Failed to get file: " << OldFile << "\n";
664 return;
665 }
666 FileID ID = SM.getOrCreateFileID(FE, SrcMgr::C_User);
667 auto Begin = SM.getLocForStartOfFile(ID);
668 auto End = SM.getLocForEndOfFile(ID);
669 clang::tooling::Replacement RemoveAll (
670 SM, clang::CharSourceRange::getCharRange(Begin, End), "");
671 std::string FilePath = RemoveAll.getFilePath().str();
672 FileToReplacements[FilePath] = clang::tooling::Replacements(RemoveAll);
673
674 StringRef Code = SM.getBufferData(ID);
675 if (!NewFile.empty()) {
676 auto AllCode = clang::tooling::Replacements(
677 clang::tooling::Replacement(NewFile, 0, 0, Code));
678 // If we are moving from old.cc, an extra step is required: excluding
679 // the #include of "old.h", instead, we replace it with #include of "new.h".
680 if (Spec.NewCC == NewFile && OldHeaderIncludeRange.isValid()) {
681 AllCode = AllCode.merge(
682 clang::tooling::Replacements(clang::tooling::Replacement(
683 SM, OldHeaderIncludeRange, '"' + Spec.NewHeader + '"')));
684 }
685 FileToReplacements[NewFile] = std::move(AllCode);
686 }
687}
688
Haojian Wu357ef992016-09-21 13:18:19 +0000689void ClangMoveTool::onEndOfTranslationUnit() {
690 if (RemovedDecls.empty())
691 return;
Haojian Wu2930be12016-11-08 19:55:13 +0000692 if (UnremovedDeclsInOldHeader.empty() && !Spec.OldHeader.empty()) {
693 auto &SM = *RemovedDecls[0].SM;
694 moveAll(SM, Spec.OldHeader, Spec.NewHeader);
695 moveAll(SM, Spec.OldCC, Spec.NewCC);
696 return;
697 }
Haojian Wu357ef992016-09-21 13:18:19 +0000698 removeClassDefinitionInOldFiles();
699 moveClassDefinitionToNewFiles();
700}
701
702} // namespace move
703} // namespace clang