1 //===-- 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 "HelperDeclRefGraph.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Format/Format.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Lex/Lexer.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Rewrite/Core/Rewriter.h"
19 #include "clang/Tooling/Core/Replacement.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Path.h"
22
23 #define DEBUG_TYPE "clang-move"
24
25 using namespace clang::ast_matchers;
26
27 namespace clang {
28 namespace move {
29 namespace {
30
31 // FIXME: Move to ASTMatchers.
AST_MATCHER(VarDecl,isStaticDataMember)32 AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); }
33
AST_MATCHER(NamedDecl,notInMacro)34 AST_MATCHER(NamedDecl, notInMacro) { return !Node.getLocation().isMacroID(); }
35
AST_MATCHER_P(Decl,hasOutermostEnclosingClass,ast_matchers::internal::Matcher<Decl>,InnerMatcher)36 AST_MATCHER_P(Decl, hasOutermostEnclosingClass,
37 ast_matchers::internal::Matcher<Decl>, InnerMatcher) {
38 const auto *Context = Node.getDeclContext();
39 if (!Context)
40 return false;
41 while (const auto *NextContext = Context->getParent()) {
42 if (isa<NamespaceDecl>(NextContext) ||
43 isa<TranslationUnitDecl>(NextContext))
44 break;
45 Context = NextContext;
46 }
47 return InnerMatcher.matches(*Decl::castFromDeclContext(Context), Finder,
48 Builder);
49 }
50
AST_MATCHER_P(CXXMethodDecl,ofOutermostEnclosingClass,ast_matchers::internal::Matcher<CXXRecordDecl>,InnerMatcher)51 AST_MATCHER_P(CXXMethodDecl, ofOutermostEnclosingClass,
52 ast_matchers::internal::Matcher<CXXRecordDecl>, InnerMatcher) {
53 const CXXRecordDecl *Parent = Node.getParent();
54 if (!Parent)
55 return false;
56 while (const auto *NextParent =
57 dyn_cast<CXXRecordDecl>(Parent->getParent())) {
58 Parent = NextParent;
59 }
60
61 return InnerMatcher.matches(*Parent, Finder, Builder);
62 }
63
CleanPath(StringRef PathRef)64 std::string CleanPath(StringRef PathRef) {
65 llvm::SmallString<128> Path(PathRef);
66 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
67 // FIXME: figure out why this is necessary.
68 llvm::sys::path::native(Path);
69 return Path.str();
70 }
71
72 // Make the Path absolute using the CurrentDir if the Path is not an absolute
73 // path. An empty Path will result in an empty string.
MakeAbsolutePath(StringRef CurrentDir,StringRef Path)74 std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) {
75 if (Path.empty())
76 return "";
77 llvm::SmallString<128> InitialDirectory(CurrentDir);
78 llvm::SmallString<128> AbsolutePath(Path);
79 llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath);
80 return CleanPath(std::move(AbsolutePath));
81 }
82
83 // Make the Path absolute using the current working directory of the given
84 // SourceManager if the Path is not an absolute path.
85 //
86 // The Path can be a path relative to the build directory, or retrieved from
87 // the SourceManager.
MakeAbsolutePath(const SourceManager & SM,StringRef Path)88 std::string MakeAbsolutePath(const SourceManager &SM, StringRef Path) {
89 llvm::SmallString<128> AbsolutePath(Path);
90 if (std::error_code EC =
91 SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
92 AbsolutePath))
93 llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
94 << '\n';
95 // Handle symbolic link path cases.
96 // We are trying to get the real file path of the symlink.
97 const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
98 llvm::sys::path::parent_path(AbsolutePath.str()));
99 if (Dir) {
100 StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
101 // FIXME: getCanonicalName might fail to get real path on VFS.
102 if (llvm::sys::path::is_absolute(DirName)) {
103 SmallString<128> AbsoluteFilename;
104 llvm::sys::path::append(AbsoluteFilename, DirName,
105 llvm::sys::path::filename(AbsolutePath.str()));
106 return CleanPath(AbsoluteFilename);
107 }
108 }
109 return CleanPath(AbsolutePath);
110 }
111
112 // Matches AST nodes that are expanded within the given AbsoluteFilePath.
AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,AST_POLYMORPHIC_SUPPORTED_TYPES (Decl,Stmt,TypeLoc),std::string,AbsoluteFilePath)113 AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,
114 AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc),
115 std::string, AbsoluteFilePath) {
116 auto &SourceManager = Finder->getASTContext().getSourceManager();
117 auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
118 if (ExpansionLoc.isInvalid())
119 return false;
120 auto FileEntry =
121 SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
122 if (!FileEntry)
123 return false;
124 return MakeAbsolutePath(SourceManager, FileEntry->getName()) ==
125 AbsoluteFilePath;
126 }
127
128 class FindAllIncludes : public PPCallbacks {
129 public:
FindAllIncludes(SourceManager * SM,ClangMoveTool * const MoveTool)130 explicit FindAllIncludes(SourceManager *SM, ClangMoveTool *const MoveTool)
131 : SM(*SM), MoveTool(MoveTool) {}
132
InclusionDirective(SourceLocation HashLoc,const Token &,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,const FileEntry *,StringRef SearchPath,StringRef,const Module *,SrcMgr::CharacteristicKind)133 void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/,
134 StringRef FileName, bool IsAngled,
135 CharSourceRange FilenameRange,
136 const FileEntry * /*File*/, StringRef SearchPath,
137 StringRef /*RelativePath*/,
138 const Module * /*Imported*/,
139 SrcMgr::CharacteristicKind /*FileType*/) override {
140 if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
141 MoveTool->addIncludes(FileName, IsAngled, SearchPath,
142 FileEntry->getName(), FilenameRange, SM);
143 }
144
145 private:
146 const SourceManager &SM;
147 ClangMoveTool *const MoveTool;
148 };
149
150 /// Add a declatration being moved to new.h/cc. Note that the declaration will
151 /// also be deleted in old.h/cc.
MoveDeclFromOldFileToNewFile(ClangMoveTool * MoveTool,const NamedDecl * D)152 void MoveDeclFromOldFileToNewFile(ClangMoveTool *MoveTool, const NamedDecl *D) {
153 MoveTool->getMovedDecls().push_back(D);
154 MoveTool->addRemovedDecl(D);
155 MoveTool->getUnremovedDeclsInOldHeader().erase(D);
156 }
157
158 class FunctionDeclarationMatch : public MatchFinder::MatchCallback {
159 public:
FunctionDeclarationMatch(ClangMoveTool * MoveTool)160 explicit FunctionDeclarationMatch(ClangMoveTool *MoveTool)
161 : MoveTool(MoveTool) {}
162
run(const MatchFinder::MatchResult & Result)163 void run(const MatchFinder::MatchResult &Result) override {
164 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("function");
165 assert(FD);
166 const NamedDecl *D = FD;
167 if (const auto *FTD = FD->getDescribedFunctionTemplate())
168 D = FTD;
169 MoveDeclFromOldFileToNewFile(MoveTool, D);
170 }
171
172 private:
173 ClangMoveTool *MoveTool;
174 };
175
176 class VarDeclarationMatch : public MatchFinder::MatchCallback {
177 public:
VarDeclarationMatch(ClangMoveTool * MoveTool)178 explicit VarDeclarationMatch(ClangMoveTool *MoveTool)
179 : MoveTool(MoveTool) {}
180
run(const MatchFinder::MatchResult & Result)181 void run(const MatchFinder::MatchResult &Result) override {
182 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var");
183 assert(VD);
184 MoveDeclFromOldFileToNewFile(MoveTool, VD);
185 }
186
187 private:
188 ClangMoveTool *MoveTool;
189 };
190
191 class TypeAliasMatch : public MatchFinder::MatchCallback {
192 public:
TypeAliasMatch(ClangMoveTool * MoveTool)193 explicit TypeAliasMatch(ClangMoveTool *MoveTool)
194 : MoveTool(MoveTool) {}
195
run(const MatchFinder::MatchResult & Result)196 void run(const MatchFinder::MatchResult &Result) override {
197 if (const auto *TD = Result.Nodes.getNodeAs<TypedefDecl>("typedef"))
198 MoveDeclFromOldFileToNewFile(MoveTool, TD);
199 else if (const auto *TAD =
200 Result.Nodes.getNodeAs<TypeAliasDecl>("type_alias")) {
201 const NamedDecl * D = TAD;
202 if (const auto * TD = TAD->getDescribedAliasTemplate())
203 D = TD;
204 MoveDeclFromOldFileToNewFile(MoveTool, D);
205 }
206 }
207
208 private:
209 ClangMoveTool *MoveTool;
210 };
211
212 class EnumDeclarationMatch : public MatchFinder::MatchCallback {
213 public:
EnumDeclarationMatch(ClangMoveTool * MoveTool)214 explicit EnumDeclarationMatch(ClangMoveTool *MoveTool)
215 : MoveTool(MoveTool) {}
216
run(const MatchFinder::MatchResult & Result)217 void run(const MatchFinder::MatchResult &Result) override {
218 const auto *ED = Result.Nodes.getNodeAs<EnumDecl>("enum");
219 assert(ED);
220 MoveDeclFromOldFileToNewFile(MoveTool, ED);
221 }
222
223 private:
224 ClangMoveTool *MoveTool;
225 };
226
227 class ClassDeclarationMatch : public MatchFinder::MatchCallback {
228 public:
ClassDeclarationMatch(ClangMoveTool * MoveTool)229 explicit ClassDeclarationMatch(ClangMoveTool *MoveTool)
230 : MoveTool(MoveTool) {}
run(const MatchFinder::MatchResult & Result)231 void run(const MatchFinder::MatchResult &Result) override {
232 SourceManager *SM = &Result.Context->getSourceManager();
233 if (const auto *CMD = Result.Nodes.getNodeAs<CXXMethodDecl>("class_method"))
234 MatchClassMethod(CMD, SM);
235 else if (const auto *VD =
236 Result.Nodes.getNodeAs<VarDecl>("class_static_var_decl"))
237 MatchClassStaticVariable(VD, SM);
238 else if (const auto *CD =
239 Result.Nodes.getNodeAs<CXXRecordDecl>("moved_class"))
240 MatchClassDeclaration(CD, SM);
241 }
242
243 private:
MatchClassMethod(const CXXMethodDecl * CMD,SourceManager * SM)244 void MatchClassMethod(const CXXMethodDecl *CMD, SourceManager *SM) {
245 // Skip inline class methods. isInline() ast matcher doesn't ignore this
246 // case.
247 if (!CMD->isInlined()) {
248 MoveTool->getMovedDecls().push_back(CMD);
249 MoveTool->addRemovedDecl(CMD);
250 // Get template class method from its method declaration as
251 // UnremovedDecls stores template class method.
252 if (const auto *FTD = CMD->getDescribedFunctionTemplate())
253 MoveTool->getUnremovedDeclsInOldHeader().erase(FTD);
254 else
255 MoveTool->getUnremovedDeclsInOldHeader().erase(CMD);
256 }
257 }
258
MatchClassStaticVariable(const NamedDecl * VD,SourceManager * SM)259 void MatchClassStaticVariable(const NamedDecl *VD, SourceManager *SM) {
260 MoveDeclFromOldFileToNewFile(MoveTool, VD);
261 }
262
MatchClassDeclaration(const CXXRecordDecl * CD,SourceManager * SM)263 void MatchClassDeclaration(const CXXRecordDecl *CD, SourceManager *SM) {
264 // Get class template from its class declaration as UnremovedDecls stores
265 // class template.
266 if (const auto *TC = CD->getDescribedClassTemplate())
267 MoveTool->getMovedDecls().push_back(TC);
268 else
269 MoveTool->getMovedDecls().push_back(CD);
270 MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
271 MoveTool->getUnremovedDeclsInOldHeader().erase(
272 MoveTool->getMovedDecls().back());
273 }
274
275 ClangMoveTool *MoveTool;
276 };
277
278 // Expand to get the end location of the line where the EndLoc of the given
279 // Decl.
getLocForEndOfDecl(const Decl * D,const LangOptions & LangOpts=LangOptions ())280 SourceLocation getLocForEndOfDecl(const Decl *D,
281 const LangOptions &LangOpts = LangOptions()) {
282 const auto &SM = D->getASTContext().getSourceManager();
283 // If the expansion range is a character range, this is the location of
284 // the first character past the end. Otherwise it's the location of the
285 // first character in the final token in the range.
286 auto EndExpansionLoc = SM.getExpansionRange(D->getEndLoc()).getEnd();
287 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(EndExpansionLoc);
288 // Try to load the file buffer.
289 bool InvalidTemp = false;
290 llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
291 if (InvalidTemp)
292 return SourceLocation();
293
294 const char *TokBegin = File.data() + LocInfo.second;
295 // Lex from the start of the given location.
296 Lexer Lex(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
297 TokBegin, File.end());
298
299 llvm::SmallVector<char, 16> Line;
300 // FIXME: this is a bit hacky to get ReadToEndOfLine work.
301 Lex.setParsingPreprocessorDirective(true);
302 Lex.ReadToEndOfLine(&Line);
303 SourceLocation EndLoc = EndExpansionLoc.getLocWithOffset(Line.size());
304 // If we already reach EOF, just return the EOF SourceLocation;
305 // otherwise, move 1 offset ahead to include the trailing newline character
306 // '\n'.
307 return SM.getLocForEndOfFile(LocInfo.first) == EndLoc
308 ? EndLoc
309 : EndLoc.getLocWithOffset(1);
310 }
311
312 // Get full range of a Decl including the comments associated with it.
getFullRange(const Decl * D,const LangOptions & options=LangOptions ())313 CharSourceRange getFullRange(const Decl *D,
314 const LangOptions &options = LangOptions()) {
315 const auto &SM = D->getASTContext().getSourceManager();
316 SourceRange Full(SM.getExpansionLoc(D->getBeginLoc()), getLocForEndOfDecl(D));
317 // Expand to comments that are associated with the Decl.
318 if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) {
319 if (SM.isBeforeInTranslationUnit(Full.getEnd(), Comment->getEndLoc()))
320 Full.setEnd(Comment->getEndLoc());
321 // FIXME: Don't delete a preceding comment, if there are no other entities
322 // it could refer to.
323 if (SM.isBeforeInTranslationUnit(Comment->getBeginLoc(), Full.getBegin()))
324 Full.setBegin(Comment->getBeginLoc());
325 }
326
327 return CharSourceRange::getCharRange(Full);
328 }
329
getDeclarationSourceText(const Decl * D)330 std::string getDeclarationSourceText(const Decl *D) {
331 const auto &SM = D->getASTContext().getSourceManager();
332 llvm::StringRef SourceText =
333 Lexer::getSourceText(getFullRange(D), SM, LangOptions());
334 return SourceText.str();
335 }
336
isInHeaderFile(const Decl * D,llvm::StringRef OriginalRunningDirectory,llvm::StringRef OldHeader)337 bool isInHeaderFile(const Decl *D, llvm::StringRef OriginalRunningDirectory,
338 llvm::StringRef OldHeader) {
339 const auto &SM = D->getASTContext().getSourceManager();
340 if (OldHeader.empty())
341 return false;
342 auto ExpansionLoc = SM.getExpansionLoc(D->getBeginLoc());
343 if (ExpansionLoc.isInvalid())
344 return false;
345
346 if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
347 return MakeAbsolutePath(SM, FE->getName()) ==
348 MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
349 }
350
351 return false;
352 }
353
getNamespaces(const Decl * D)354 std::vector<std::string> getNamespaces(const Decl *D) {
355 std::vector<std::string> Namespaces;
356 for (const auto *Context = D->getDeclContext(); Context;
357 Context = Context->getParent()) {
358 if (llvm::isa<TranslationUnitDecl>(Context) ||
359 llvm::isa<LinkageSpecDecl>(Context))
360 break;
361
362 if (const auto *ND = llvm::dyn_cast<NamespaceDecl>(Context))
363 Namespaces.push_back(ND->getName().str());
364 }
365 std::reverse(Namespaces.begin(), Namespaces.end());
366 return Namespaces;
367 }
368
369 tooling::Replacements
createInsertedReplacements(const std::vector<std::string> & Includes,const std::vector<const NamedDecl * > & Decls,llvm::StringRef FileName,bool IsHeader=false,StringRef OldHeaderInclude="")370 createInsertedReplacements(const std::vector<std::string> &Includes,
371 const std::vector<const NamedDecl *> &Decls,
372 llvm::StringRef FileName, bool IsHeader = false,
373 StringRef OldHeaderInclude = "") {
374 std::string NewCode;
375 std::string GuardName(FileName);
376 if (IsHeader) {
377 for (size_t i = 0; i < GuardName.size(); ++i) {
378 if (!isAlphanumeric(GuardName[i]))
379 GuardName[i] = '_';
380 }
381 GuardName = StringRef(GuardName).upper();
382 NewCode += "#ifndef " + GuardName + "\n";
383 NewCode += "#define " + GuardName + "\n\n";
384 }
385
386 NewCode += OldHeaderInclude;
387 // Add #Includes.
388 for (const auto &Include : Includes)
389 NewCode += Include;
390
391 if (!Includes.empty())
392 NewCode += "\n";
393
394 // Add moved class definition and its related declarations. All declarations
395 // in same namespace are grouped together.
396 //
397 // Record namespaces where the current position is in.
398 std::vector<std::string> CurrentNamespaces;
399 for (const auto *MovedDecl : Decls) {
400 // The namespaces of the declaration being moved.
401 std::vector<std::string> DeclNamespaces = getNamespaces(MovedDecl);
402 auto CurrentIt = CurrentNamespaces.begin();
403 auto DeclIt = DeclNamespaces.begin();
404 // Skip the common prefix.
405 while (CurrentIt != CurrentNamespaces.end() &&
406 DeclIt != DeclNamespaces.end()) {
407 if (*CurrentIt != *DeclIt)
408 break;
409 ++CurrentIt;
410 ++DeclIt;
411 }
412 // Calculate the new namespaces after adding MovedDecl in CurrentNamespace,
413 // which is used for next iteration of this loop.
414 std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(),
415 CurrentIt);
416 NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end());
417
418
419 // End with CurrentNamespace.
420 bool HasEndCurrentNamespace = false;
421 auto RemainingSize = CurrentNamespaces.end() - CurrentIt;
422 for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0;
423 --RemainingSize, ++It) {
424 assert(It < CurrentNamespaces.rend());
425 NewCode += "} // namespace " + *It + "\n";
426 HasEndCurrentNamespace = true;
427 }
428 // Add trailing '\n' after the nested namespace definition.
429 if (HasEndCurrentNamespace)
430 NewCode += "\n";
431
432 // If the moved declaration is not in CurrentNamespace, add extra namespace
433 // definitions.
434 bool IsInNewNamespace = false;
435 while (DeclIt != DeclNamespaces.end()) {
436 NewCode += "namespace " + *DeclIt + " {\n";
437 IsInNewNamespace = true;
438 ++DeclIt;
439 }
440 // If the moved declaration is in same namespace CurrentNamespace, add
441 // a preceeding `\n' before the moved declaration.
442 // FIXME: Don't add empty lines between using declarations.
443 if (!IsInNewNamespace)
444 NewCode += "\n";
445 NewCode += getDeclarationSourceText(MovedDecl);
446 CurrentNamespaces = std::move(NextNamespaces);
447 }
448 std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end());
449 for (const auto &NS : CurrentNamespaces)
450 NewCode += "} // namespace " + NS + "\n";
451
452 if (IsHeader)
453 NewCode += "\n#endif // " + GuardName + "\n";
454 return tooling::Replacements(tooling::Replacement(FileName, 0, 0, NewCode));
455 }
456
457 // Return a set of all decls which are used/referenced by the given Decls.
458 // Specically, given a class member declaration, this method will return all
459 // decls which are used by the whole class.
460 llvm::DenseSet<const Decl *>
getUsedDecls(const HelperDeclRefGraph * RG,const std::vector<const NamedDecl * > & Decls)461 getUsedDecls(const HelperDeclRefGraph *RG,
462 const std::vector<const NamedDecl *> &Decls) {
463 assert(RG);
464 llvm::DenseSet<const CallGraphNode *> Nodes;
465 for (const auto *D : Decls) {
466 auto Result = RG->getReachableNodes(
467 HelperDeclRGBuilder::getOutmostClassOrFunDecl(D));
468 Nodes.insert(Result.begin(), Result.end());
469 }
470 llvm::DenseSet<const Decl *> Results;
471 for (const auto *Node : Nodes)
472 Results.insert(Node->getDecl());
473 return Results;
474 }
475
476 } // namespace
477
478 std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & Compiler,StringRef)479 ClangMoveAction::CreateASTConsumer(CompilerInstance &Compiler,
480 StringRef /*InFile*/) {
481 Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>(
482 &Compiler.getSourceManager(), &MoveTool));
483 return MatchFinder.newASTConsumer();
484 }
485
ClangMoveTool(ClangMoveContext * const Context,DeclarationReporter * const Reporter)486 ClangMoveTool::ClangMoveTool(ClangMoveContext *const Context,
487 DeclarationReporter *const Reporter)
488 : Context(Context), Reporter(Reporter) {
489 if (!Context->Spec.NewHeader.empty())
490 CCIncludes.push_back("#include \"" + Context->Spec.NewHeader + "\"\n");
491 }
492
addRemovedDecl(const NamedDecl * Decl)493 void ClangMoveTool::addRemovedDecl(const NamedDecl *Decl) {
494 const auto &SM = Decl->getASTContext().getSourceManager();
495 auto Loc = Decl->getLocation();
496 StringRef FilePath = SM.getFilename(Loc);
497 FilePathToFileID[FilePath] = SM.getFileID(Loc);
498 RemovedDecls.push_back(Decl);
499 }
500
registerMatchers(ast_matchers::MatchFinder * Finder)501 void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
502 auto InOldHeader =
503 isExpansionInFile(makeAbsolutePath(Context->Spec.OldHeader));
504 auto InOldCC = isExpansionInFile(makeAbsolutePath(Context->Spec.OldCC));
505 auto InOldFiles = anyOf(InOldHeader, InOldCC);
506 auto classTemplateForwardDecls =
507 classTemplateDecl(unless(has(cxxRecordDecl(isDefinition()))));
508 auto ForwardClassDecls = namedDecl(
509 anyOf(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition()))),
510 classTemplateForwardDecls));
511 auto TopLevelDecl =
512 hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl()));
513
514 //============================================================================
515 // Matchers for old header
516 //============================================================================
517 // Match all top-level named declarations (e.g. function, variable, enum) in
518 // old header, exclude forward class declarations and namespace declarations.
519 //
520 // We consider declarations inside a class belongs to the class. So these
521 // declarations will be ignored.
522 auto AllDeclsInHeader = namedDecl(
523 unless(ForwardClassDecls), unless(namespaceDecl()),
524 unless(usingDirectiveDecl()), // using namespace decl.
525 notInMacro(),
526 InOldHeader,
527 hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))),
528 hasDeclContext(decl(anyOf(namespaceDecl(), translationUnitDecl()))));
529 Finder->addMatcher(AllDeclsInHeader.bind("decls_in_header"), this);
530
531 // Don't register other matchers when dumping all declarations in header.
532 if (Context->DumpDeclarations)
533 return;
534
535 // Match forward declarations in old header.
536 Finder->addMatcher(namedDecl(ForwardClassDecls, InOldHeader).bind("fwd_decl"),
537 this);
538
539 //============================================================================
540 // Matchers for old cc
541 //============================================================================
542 auto IsOldCCTopLevelDecl = allOf(
543 hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))), InOldCC);
544 // Matching using decls/type alias decls which are in named/anonymous/global
545 // namespace, these decls are always copied to new.h/cc. Those in classes,
546 // functions are covered in other matchers.
547 Finder->addMatcher(namedDecl(anyOf(usingDecl(IsOldCCTopLevelDecl),
548 usingDirectiveDecl(unless(isImplicit()),
549 IsOldCCTopLevelDecl),
550 typeAliasDecl(IsOldCCTopLevelDecl)),
551 notInMacro())
552 .bind("using_decl"),
553 this);
554
555 // Match static functions/variable definitions which are defined in named
556 // namespaces.
557 Optional<ast_matchers::internal::Matcher<NamedDecl>> HasAnySymbolNames;
558 for (StringRef SymbolName : Context->Spec.Names) {
559 llvm::StringRef GlobalSymbolName = SymbolName.trim().ltrim(':');
560 const auto HasName = hasName(("::" + GlobalSymbolName).str());
561 HasAnySymbolNames =
562 HasAnySymbolNames ? anyOf(*HasAnySymbolNames, HasName) : HasName;
563 }
564
565 if (!HasAnySymbolNames) {
566 llvm::errs() << "No symbols being moved.\n";
567 return;
568 }
569 auto InMovedClass =
570 hasOutermostEnclosingClass(cxxRecordDecl(*HasAnySymbolNames));
571
572 // Matchers for helper declarations in old.cc.
573 auto InAnonymousNS = hasParent(namespaceDecl(isAnonymous()));
574 auto NotInMovedClass= allOf(unless(InMovedClass), InOldCC);
575 auto IsOldCCHelper =
576 allOf(NotInMovedClass, anyOf(isStaticStorageClass(), InAnonymousNS));
577 // Match helper classes separately with helper functions/variables since we
578 // want to reuse these matchers in finding helpers usage below.
579 //
580 // There could be forward declarations usage for helpers, especially for
581 // classes and functions. We need include these forward declarations.
582 //
583 // Forward declarations for variable helpers will be excluded as these
584 // declarations (with "extern") are not supposed in cpp file.
585 auto HelperFuncOrVar =
586 namedDecl(notInMacro(), anyOf(functionDecl(IsOldCCHelper),
587 varDecl(isDefinition(), IsOldCCHelper)));
588 auto HelperClasses =
589 cxxRecordDecl(notInMacro(), NotInMovedClass, InAnonymousNS);
590 // Save all helper declarations in old.cc.
591 Finder->addMatcher(
592 namedDecl(anyOf(HelperFuncOrVar, HelperClasses)).bind("helper_decls"),
593 this);
594
595 // Construct an AST-based call graph of helper declarations in old.cc.
596 // In the following matcheres, "dc" is a caller while "helper_decls" and
597 // "used_class" is a callee, so a new edge starting from caller to callee will
598 // be add in the graph.
599 //
600 // Find helper function/variable usages.
601 Finder->addMatcher(
602 declRefExpr(to(HelperFuncOrVar), hasAncestor(decl().bind("dc")))
603 .bind("func_ref"),
604 &RGBuilder);
605 // Find helper class usages.
606 Finder->addMatcher(
607 typeLoc(loc(recordType(hasDeclaration(HelperClasses.bind("used_class")))),
608 hasAncestor(decl().bind("dc"))),
609 &RGBuilder);
610
611 //============================================================================
612 // Matchers for old files, including old.h/old.cc
613 //============================================================================
614 // Create a MatchCallback for class declarations.
615 MatchCallbacks.push_back(llvm::make_unique<ClassDeclarationMatch>(this));
616 // Match moved class declarations.
617 auto MovedClass = cxxRecordDecl(InOldFiles, *HasAnySymbolNames,
618 isDefinition(), TopLevelDecl)
619 .bind("moved_class");
620 Finder->addMatcher(MovedClass, MatchCallbacks.back().get());
621 // Match moved class methods (static methods included) which are defined
622 // outside moved class declaration.
623 Finder->addMatcher(
624 cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*HasAnySymbolNames),
625 isDefinition())
626 .bind("class_method"),
627 MatchCallbacks.back().get());
628 // Match static member variable definition of the moved class.
629 Finder->addMatcher(
630 varDecl(InMovedClass, InOldFiles, isDefinition(), isStaticDataMember())
631 .bind("class_static_var_decl"),
632 MatchCallbacks.back().get());
633
634 MatchCallbacks.push_back(llvm::make_unique<FunctionDeclarationMatch>(this));
635 Finder->addMatcher(functionDecl(InOldFiles, *HasAnySymbolNames, TopLevelDecl)
636 .bind("function"),
637 MatchCallbacks.back().get());
638
639 MatchCallbacks.push_back(llvm::make_unique<VarDeclarationMatch>(this));
640 Finder->addMatcher(
641 varDecl(InOldFiles, *HasAnySymbolNames, TopLevelDecl).bind("var"),
642 MatchCallbacks.back().get());
643
644 // Match enum definition in old.h. Enum helpers (which are defined in old.cc)
645 // will not be moved for now no matter whether they are used or not.
646 MatchCallbacks.push_back(llvm::make_unique<EnumDeclarationMatch>(this));
647 Finder->addMatcher(
648 enumDecl(InOldHeader, *HasAnySymbolNames, isDefinition(), TopLevelDecl)
649 .bind("enum"),
650 MatchCallbacks.back().get());
651
652 // Match type alias in old.h, this includes "typedef" and "using" type alias
653 // declarations. Type alias helpers (which are defined in old.cc) will not be
654 // moved for now no matter whether they are used or not.
655 MatchCallbacks.push_back(llvm::make_unique<TypeAliasMatch>(this));
656 Finder->addMatcher(namedDecl(anyOf(typedefDecl().bind("typedef"),
657 typeAliasDecl().bind("type_alias")),
658 InOldHeader, *HasAnySymbolNames, TopLevelDecl),
659 MatchCallbacks.back().get());
660 }
661
run(const ast_matchers::MatchFinder::MatchResult & Result)662 void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) {
663 if (const auto *D = Result.Nodes.getNodeAs<NamedDecl>("decls_in_header")) {
664 UnremovedDeclsInOldHeader.insert(D);
665 } else if (const auto *FWD =
666 Result.Nodes.getNodeAs<CXXRecordDecl>("fwd_decl")) {
667 // Skip all forward declarations which appear after moved class declaration.
668 if (RemovedDecls.empty()) {
669 if (const auto *DCT = FWD->getDescribedClassTemplate())
670 MovedDecls.push_back(DCT);
671 else
672 MovedDecls.push_back(FWD);
673 }
674 } else if (const auto *ND =
675 Result.Nodes.getNodeAs<NamedDecl>("helper_decls")) {
676 MovedDecls.push_back(ND);
677 HelperDeclarations.push_back(ND);
678 LLVM_DEBUG(llvm::dbgs() << "Add helper : " << ND->getNameAsString() << " ("
679 << ND << ")\n");
680 } else if (const auto *UD = Result.Nodes.getNodeAs<NamedDecl>("using_decl")) {
681 MovedDecls.push_back(UD);
682 }
683 }
684
makeAbsolutePath(StringRef Path)685 std::string ClangMoveTool::makeAbsolutePath(StringRef Path) {
686 return MakeAbsolutePath(Context->OriginalRunningDirectory, Path);
687 }
688
addIncludes(llvm::StringRef IncludeHeader,bool IsAngled,llvm::StringRef SearchPath,llvm::StringRef FileName,CharSourceRange IncludeFilenameRange,const SourceManager & SM)689 void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader, bool IsAngled,
690 llvm::StringRef SearchPath,
691 llvm::StringRef FileName,
692 CharSourceRange IncludeFilenameRange,
693 const SourceManager &SM) {
694 SmallVector<char, 128> HeaderWithSearchPath;
695 llvm::sys::path::append(HeaderWithSearchPath, SearchPath, IncludeHeader);
696 std::string AbsoluteIncludeHeader =
697 MakeAbsolutePath(SM, llvm::StringRef(HeaderWithSearchPath.data(),
698 HeaderWithSearchPath.size()));
699 std::string IncludeLine =
700 IsAngled ? ("#include <" + IncludeHeader + ">\n").str()
701 : ("#include \"" + IncludeHeader + "\"\n").str();
702
703 std::string AbsoluteOldHeader = makeAbsolutePath(Context->Spec.OldHeader);
704 std::string AbsoluteCurrentFile = MakeAbsolutePath(SM, FileName);
705 if (AbsoluteOldHeader == AbsoluteCurrentFile) {
706 // Find old.h includes "old.h".
707 if (AbsoluteOldHeader == AbsoluteIncludeHeader) {
708 OldHeaderIncludeRangeInHeader = IncludeFilenameRange;
709 return;
710 }
711 HeaderIncludes.push_back(IncludeLine);
712 } else if (makeAbsolutePath(Context->Spec.OldCC) == AbsoluteCurrentFile) {
713 // Find old.cc includes "old.h".
714 if (AbsoluteOldHeader == AbsoluteIncludeHeader) {
715 OldHeaderIncludeRangeInCC = IncludeFilenameRange;
716 return;
717 }
718 CCIncludes.push_back(IncludeLine);
719 }
720 }
721
removeDeclsInOldFiles()722 void ClangMoveTool::removeDeclsInOldFiles() {
723 if (RemovedDecls.empty()) return;
724
725 // If old_header is not specified (only move declarations from old.cc), remain
726 // all the helper function declarations in old.cc as UnremovedDeclsInOldHeader
727 // is empty in this case, there is no way to verify unused/used helpers.
728 if (!Context->Spec.OldHeader.empty()) {
729 std::vector<const NamedDecl *> UnremovedDecls;
730 for (const auto *D : UnremovedDeclsInOldHeader)
731 UnremovedDecls.push_back(D);
732
733 auto UsedDecls = getUsedDecls(RGBuilder.getGraph(), UnremovedDecls);
734
735 // We remove the helper declarations which are not used in the old.cc after
736 // moving the given declarations.
737 for (const auto *D : HelperDeclarations) {
738 LLVM_DEBUG(llvm::dbgs() << "Check helper is used: "
739 << D->getNameAsString() << " (" << D << ")\n");
740 if (!UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl(
741 D->getCanonicalDecl()))) {
742 LLVM_DEBUG(llvm::dbgs() << "Helper removed in old.cc: "
743 << D->getNameAsString() << " (" << D << ")\n");
744 RemovedDecls.push_back(D);
745 }
746 }
747 }
748
749 for (const auto *RemovedDecl : RemovedDecls) {
750 const auto &SM = RemovedDecl->getASTContext().getSourceManager();
751 auto Range = getFullRange(RemovedDecl);
752 tooling::Replacement RemoveReplacement(
753 SM, CharSourceRange::getCharRange(Range.getBegin(), Range.getEnd()),
754 "");
755 std::string FilePath = RemoveReplacement.getFilePath().str();
756 auto Err = Context->FileToReplacements[FilePath].add(RemoveReplacement);
757 if (Err)
758 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
759 }
760 const auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
761
762 // Post process of cleanup around all the replacements.
763 for (auto &FileAndReplacements : Context->FileToReplacements) {
764 StringRef FilePath = FileAndReplacements.first;
765 // Add #include of new header to old header.
766 if (Context->Spec.OldDependOnNew &&
767 MakeAbsolutePath(SM, FilePath) ==
768 makeAbsolutePath(Context->Spec.OldHeader)) {
769 // FIXME: Minimize the include path like include-fixer.
770 std::string IncludeNewH =
771 "#include \"" + Context->Spec.NewHeader + "\"\n";
772 // This replacment for inserting header will be cleaned up at the end.
773 auto Err = FileAndReplacements.second.add(
774 tooling::Replacement(FilePath, UINT_MAX, 0, IncludeNewH));
775 if (Err)
776 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
777 }
778
779 auto SI = FilePathToFileID.find(FilePath);
780 // Ignore replacements for new.h/cc.
781 if (SI == FilePathToFileID.end()) continue;
782 llvm::StringRef Code = SM.getBufferData(SI->second);
783 auto Style = format::getStyle(format::DefaultFormatStyle, FilePath,
784 Context->FallbackStyle);
785 if (!Style) {
786 llvm::errs() << llvm::toString(Style.takeError()) << "\n";
787 continue;
788 }
789 auto CleanReplacements = format::cleanupAroundReplacements(
790 Code, Context->FileToReplacements[FilePath], *Style);
791
792 if (!CleanReplacements) {
793 llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
794 continue;
795 }
796 Context->FileToReplacements[FilePath] = *CleanReplacements;
797 }
798 }
799
moveDeclsToNewFiles()800 void ClangMoveTool::moveDeclsToNewFiles() {
801 std::vector<const NamedDecl *> NewHeaderDecls;
802 std::vector<const NamedDecl *> NewCCDecls;
803 for (const auto *MovedDecl : MovedDecls) {
804 if (isInHeaderFile(MovedDecl, Context->OriginalRunningDirectory,
805 Context->Spec.OldHeader))
806 NewHeaderDecls.push_back(MovedDecl);
807 else
808 NewCCDecls.push_back(MovedDecl);
809 }
810
811 auto UsedDecls = getUsedDecls(RGBuilder.getGraph(), RemovedDecls);
812 std::vector<const NamedDecl *> ActualNewCCDecls;
813
814 // Filter out all unused helpers in NewCCDecls.
815 // We only move the used helpers (including transively used helpers) and the
816 // given symbols being moved.
817 for (const auto *D : NewCCDecls) {
818 if (llvm::is_contained(HelperDeclarations, D) &&
819 !UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl(
820 D->getCanonicalDecl())))
821 continue;
822
823 LLVM_DEBUG(llvm::dbgs() << "Helper used in new.cc: " << D->getNameAsString()
824 << " " << D << "\n");
825 ActualNewCCDecls.push_back(D);
826 }
827
828 if (!Context->Spec.NewHeader.empty()) {
829 std::string OldHeaderInclude =
830 Context->Spec.NewDependOnOld
831 ? "#include \"" + Context->Spec.OldHeader + "\"\n"
832 : "";
833 Context->FileToReplacements[Context->Spec.NewHeader] =
834 createInsertedReplacements(HeaderIncludes, NewHeaderDecls,
835 Context->Spec.NewHeader, /*IsHeader=*/true,
836 OldHeaderInclude);
837 }
838 if (!Context->Spec.NewCC.empty())
839 Context->FileToReplacements[Context->Spec.NewCC] =
840 createInsertedReplacements(CCIncludes, ActualNewCCDecls,
841 Context->Spec.NewCC);
842 }
843
844 // Move all contents from OldFile to NewFile.
moveAll(SourceManager & SM,StringRef OldFile,StringRef NewFile)845 void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile,
846 StringRef NewFile) {
847 const FileEntry *FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile));
848 if (!FE) {
849 llvm::errs() << "Failed to get file: " << OldFile << "\n";
850 return;
851 }
852 FileID ID = SM.getOrCreateFileID(FE, SrcMgr::C_User);
853 auto Begin = SM.getLocForStartOfFile(ID);
854 auto End = SM.getLocForEndOfFile(ID);
855 tooling::Replacement RemoveAll(SM, CharSourceRange::getCharRange(Begin, End),
856 "");
857 std::string FilePath = RemoveAll.getFilePath().str();
858 Context->FileToReplacements[FilePath] = tooling::Replacements(RemoveAll);
859
860 StringRef Code = SM.getBufferData(ID);
861 if (!NewFile.empty()) {
862 auto AllCode =
863 tooling::Replacements(tooling::Replacement(NewFile, 0, 0, Code));
864 auto ReplaceOldInclude = [&](CharSourceRange OldHeaderIncludeRange) {
865 AllCode = AllCode.merge(tooling::Replacements(tooling::Replacement(
866 SM, OldHeaderIncludeRange, '"' + Context->Spec.NewHeader + '"')));
867 };
868 // Fix the case where old.h/old.cc includes "old.h", we replace the
869 // `#include "old.h"` with `#include "new.h"`.
870 if (Context->Spec.NewCC == NewFile && OldHeaderIncludeRangeInCC.isValid())
871 ReplaceOldInclude(OldHeaderIncludeRangeInCC);
872 else if (Context->Spec.NewHeader == NewFile &&
873 OldHeaderIncludeRangeInHeader.isValid())
874 ReplaceOldInclude(OldHeaderIncludeRangeInHeader);
875 Context->FileToReplacements[NewFile] = std::move(AllCode);
876 }
877 }
878
onEndOfTranslationUnit()879 void ClangMoveTool::onEndOfTranslationUnit() {
880 if (Context->DumpDeclarations) {
881 assert(Reporter);
882 for (const auto *Decl : UnremovedDeclsInOldHeader) {
883 auto Kind = Decl->getKind();
884 bool Templated = Decl->isTemplated();
885 const std::string QualifiedName = Decl->getQualifiedNameAsString();
886 if (Kind == Decl::Kind::Var)
887 Reporter->reportDeclaration(QualifiedName, "Variable", Templated);
888 else if (Kind == Decl::Kind::Function ||
889 Kind == Decl::Kind::FunctionTemplate)
890 Reporter->reportDeclaration(QualifiedName, "Function", Templated);
891 else if (Kind == Decl::Kind::ClassTemplate ||
892 Kind == Decl::Kind::CXXRecord)
893 Reporter->reportDeclaration(QualifiedName, "Class", Templated);
894 else if (Kind == Decl::Kind::Enum)
895 Reporter->reportDeclaration(QualifiedName, "Enum", Templated);
896 else if (Kind == Decl::Kind::Typedef || Kind == Decl::Kind::TypeAlias ||
897 Kind == Decl::Kind::TypeAliasTemplate)
898 Reporter->reportDeclaration(QualifiedName, "TypeAlias", Templated);
899 }
900 return;
901 }
902
903 if (RemovedDecls.empty())
904 return;
905 // Ignore symbols that are not supported when checking if there is unremoved
906 // symbol in old header. This makes sure that we always move old files to new
907 // files when all symbols produced from dump_decls are moved.
908 auto IsSupportedKind = [](const NamedDecl *Decl) {
909 switch (Decl->getKind()) {
910 case Decl::Kind::Function:
911 case Decl::Kind::FunctionTemplate:
912 case Decl::Kind::ClassTemplate:
913 case Decl::Kind::CXXRecord:
914 case Decl::Kind::Enum:
915 case Decl::Kind::Typedef:
916 case Decl::Kind::TypeAlias:
917 case Decl::Kind::TypeAliasTemplate:
918 case Decl::Kind::Var:
919 return true;
920 default:
921 return false;
922 }
923 };
924 if (std::none_of(UnremovedDeclsInOldHeader.begin(),
925 UnremovedDeclsInOldHeader.end(), IsSupportedKind) &&
926 !Context->Spec.OldHeader.empty()) {
927 auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
928 moveAll(SM, Context->Spec.OldHeader, Context->Spec.NewHeader);
929 moveAll(SM, Context->Spec.OldCC, Context->Spec.NewCC);
930 return;
931 }
932 LLVM_DEBUG(RGBuilder.getGraph()->dump());
933 moveDeclsToNewFiles();
934 removeDeclsInOldFiles();
935 }
936
937 } // namespace move
938 } // namespace clang
939