1 //===-- Move.cpp - Implement ClangMove functationalities --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "HelperDeclRefGraph.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"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/Path.h"
22 #define DEBUG_TYPE "clang-move"
24 using namespace clang::ast_matchers
;
30 // FIXME: Move to ASTMatchers.
31 AST_MATCHER(VarDecl
, isStaticDataMember
) { return Node
.isStaticDataMember(); }
33 AST_MATCHER(NamedDecl
, notInMacro
) { return !Node
.getLocation().isMacroID(); }
35 AST_MATCHER_P(Decl
, hasOutermostEnclosingClass
,
36 ast_matchers::internal::Matcher
<Decl
>, InnerMatcher
) {
37 const auto *Context
= Node
.getDeclContext();
40 while (const auto *NextContext
= Context
->getParent()) {
41 if (isa
<NamespaceDecl
>(NextContext
) ||
42 isa
<TranslationUnitDecl
>(NextContext
))
44 Context
= NextContext
;
46 return InnerMatcher
.matches(*Decl::castFromDeclContext(Context
), Finder
,
50 AST_MATCHER_P(CXXMethodDecl
, ofOutermostEnclosingClass
,
51 ast_matchers::internal::Matcher
<CXXRecordDecl
>, InnerMatcher
) {
52 const CXXRecordDecl
*Parent
= Node
.getParent();
55 while (const auto *NextParent
=
56 dyn_cast
<CXXRecordDecl
>(Parent
->getParent())) {
60 return InnerMatcher
.matches(*Parent
, Finder
, Builder
);
63 std::string
CleanPath(StringRef PathRef
) {
64 llvm::SmallString
<128> Path(PathRef
);
65 llvm::sys::path::remove_dots(Path
, /*remove_dot_dot=*/true);
66 // FIXME: figure out why this is necessary.
67 llvm::sys::path::native(Path
);
68 return std::string(Path
);
71 // Make the Path absolute using the CurrentDir if the Path is not an absolute
72 // path. An empty Path will result in an empty string.
73 std::string
MakeAbsolutePath(StringRef CurrentDir
, StringRef Path
) {
76 llvm::SmallString
<128> InitialDirectory(CurrentDir
);
77 llvm::SmallString
<128> AbsolutePath(Path
);
78 llvm::sys::fs::make_absolute(InitialDirectory
, AbsolutePath
);
79 return CleanPath(std::move(AbsolutePath
));
82 // Make the Path absolute using the current working directory of the given
83 // SourceManager if the Path is not an absolute path.
85 // The Path can be a path relative to the build directory, or retrieved from
87 std::string
MakeAbsolutePath(const SourceManager
&SM
, StringRef Path
) {
88 llvm::SmallString
<128> AbsolutePath(Path
);
89 if (std::error_code EC
=
90 SM
.getFileManager().getVirtualFileSystem().makeAbsolute(AbsolutePath
))
91 llvm::errs() << "Warning: could not make absolute file: '" << EC
.message()
93 // Handle symbolic link path cases.
94 // We are trying to get the real file path of the symlink.
95 auto Dir
= SM
.getFileManager().getOptionalDirectoryRef(
96 llvm::sys::path::parent_path(AbsolutePath
.str()));
98 StringRef DirName
= SM
.getFileManager().getCanonicalName(*Dir
);
99 // FIXME: getCanonicalName might fail to get real path on VFS.
100 if (llvm::sys::path::is_absolute(DirName
)) {
101 SmallString
<128> AbsoluteFilename
;
102 llvm::sys::path::append(AbsoluteFilename
, DirName
,
103 llvm::sys::path::filename(AbsolutePath
.str()));
104 return CleanPath(AbsoluteFilename
);
107 return CleanPath(AbsolutePath
);
110 // Matches AST nodes that are expanded within the given AbsoluteFilePath.
111 AST_POLYMORPHIC_MATCHER_P(isExpansionInFile
,
112 AST_POLYMORPHIC_SUPPORTED_TYPES(Decl
, Stmt
, TypeLoc
),
113 std::string
, AbsoluteFilePath
) {
114 auto &SourceManager
= Finder
->getASTContext().getSourceManager();
115 auto ExpansionLoc
= SourceManager
.getExpansionLoc(Node
.getBeginLoc());
116 if (ExpansionLoc
.isInvalid())
119 SourceManager
.getFileEntryRefForID(SourceManager
.getFileID(ExpansionLoc
));
122 return MakeAbsolutePath(SourceManager
, FileEntry
->getName()) ==
126 class FindAllIncludes
: public PPCallbacks
{
128 explicit FindAllIncludes(SourceManager
*SM
, ClangMoveTool
*const MoveTool
)
129 : SM(*SM
), MoveTool(MoveTool
) {}
131 void InclusionDirective(SourceLocation HashLoc
, const Token
& /*IncludeTok*/,
132 StringRef FileName
, bool IsAngled
,
133 CharSourceRange FilenameRange
,
134 OptionalFileEntryRef
/*File*/, StringRef SearchPath
,
135 StringRef
/*RelativePath*/,
136 const Module
* /*SuggestedModule*/,
137 bool /*ModuleImported*/,
138 SrcMgr::CharacteristicKind
/*FileType*/) override
{
139 if (auto FileEntry
= SM
.getFileEntryRefForID(SM
.getFileID(HashLoc
)))
140 MoveTool
->addIncludes(FileName
, IsAngled
, SearchPath
,
141 FileEntry
->getName(), FilenameRange
, SM
);
145 const SourceManager
&SM
;
146 ClangMoveTool
*const MoveTool
;
149 /// Add a declaration being moved to new.h/cc. Note that the declaration will
150 /// also be deleted in old.h/cc.
151 void MoveDeclFromOldFileToNewFile(ClangMoveTool
*MoveTool
, const NamedDecl
*D
) {
152 MoveTool
->getMovedDecls().push_back(D
);
153 MoveTool
->addRemovedDecl(D
);
154 MoveTool
->getUnremovedDeclsInOldHeader().erase(D
);
157 class FunctionDeclarationMatch
: public MatchFinder::MatchCallback
{
159 explicit FunctionDeclarationMatch(ClangMoveTool
*MoveTool
)
160 : MoveTool(MoveTool
) {}
162 void run(const MatchFinder::MatchResult
&Result
) override
{
163 const auto *FD
= Result
.Nodes
.getNodeAs
<FunctionDecl
>("function");
165 const NamedDecl
*D
= FD
;
166 if (const auto *FTD
= FD
->getDescribedFunctionTemplate())
168 MoveDeclFromOldFileToNewFile(MoveTool
, D
);
172 ClangMoveTool
*MoveTool
;
175 class VarDeclarationMatch
: public MatchFinder::MatchCallback
{
177 explicit VarDeclarationMatch(ClangMoveTool
*MoveTool
)
178 : MoveTool(MoveTool
) {}
180 void run(const MatchFinder::MatchResult
&Result
) override
{
181 const auto *VD
= Result
.Nodes
.getNodeAs
<VarDecl
>("var");
183 MoveDeclFromOldFileToNewFile(MoveTool
, VD
);
187 ClangMoveTool
*MoveTool
;
190 class TypeAliasMatch
: public MatchFinder::MatchCallback
{
192 explicit TypeAliasMatch(ClangMoveTool
*MoveTool
)
193 : MoveTool(MoveTool
) {}
195 void run(const MatchFinder::MatchResult
&Result
) override
{
196 if (const auto *TD
= Result
.Nodes
.getNodeAs
<TypedefDecl
>("typedef"))
197 MoveDeclFromOldFileToNewFile(MoveTool
, TD
);
198 else if (const auto *TAD
=
199 Result
.Nodes
.getNodeAs
<TypeAliasDecl
>("type_alias")) {
200 const NamedDecl
* D
= TAD
;
201 if (const auto * TD
= TAD
->getDescribedAliasTemplate())
203 MoveDeclFromOldFileToNewFile(MoveTool
, D
);
208 ClangMoveTool
*MoveTool
;
211 class EnumDeclarationMatch
: public MatchFinder::MatchCallback
{
213 explicit EnumDeclarationMatch(ClangMoveTool
*MoveTool
)
214 : MoveTool(MoveTool
) {}
216 void run(const MatchFinder::MatchResult
&Result
) override
{
217 const auto *ED
= Result
.Nodes
.getNodeAs
<EnumDecl
>("enum");
219 MoveDeclFromOldFileToNewFile(MoveTool
, ED
);
223 ClangMoveTool
*MoveTool
;
226 class ClassDeclarationMatch
: public MatchFinder::MatchCallback
{
228 explicit ClassDeclarationMatch(ClangMoveTool
*MoveTool
)
229 : MoveTool(MoveTool
) {}
230 void run(const MatchFinder::MatchResult
&Result
) override
{
231 SourceManager
*SM
= &Result
.Context
->getSourceManager();
232 if (const auto *CMD
= Result
.Nodes
.getNodeAs
<CXXMethodDecl
>("class_method"))
233 MatchClassMethod(CMD
, SM
);
234 else if (const auto *VD
=
235 Result
.Nodes
.getNodeAs
<VarDecl
>("class_static_var_decl"))
236 MatchClassStaticVariable(VD
, SM
);
237 else if (const auto *CD
=
238 Result
.Nodes
.getNodeAs
<CXXRecordDecl
>("moved_class"))
239 MatchClassDeclaration(CD
, SM
);
243 void MatchClassMethod(const CXXMethodDecl
*CMD
, SourceManager
*SM
) {
244 // Skip inline class methods. isInline() ast matcher doesn't ignore this
246 if (!CMD
->isInlined()) {
247 MoveTool
->getMovedDecls().push_back(CMD
);
248 MoveTool
->addRemovedDecl(CMD
);
249 // Get template class method from its method declaration as
250 // UnremovedDecls stores template class method.
251 if (const auto *FTD
= CMD
->getDescribedFunctionTemplate())
252 MoveTool
->getUnremovedDeclsInOldHeader().erase(FTD
);
254 MoveTool
->getUnremovedDeclsInOldHeader().erase(CMD
);
258 void MatchClassStaticVariable(const NamedDecl
*VD
, SourceManager
*SM
) {
259 MoveDeclFromOldFileToNewFile(MoveTool
, VD
);
262 void MatchClassDeclaration(const CXXRecordDecl
*CD
, SourceManager
*SM
) {
263 // Get class template from its class declaration as UnremovedDecls stores
265 if (const auto *TC
= CD
->getDescribedClassTemplate())
266 MoveTool
->getMovedDecls().push_back(TC
);
268 MoveTool
->getMovedDecls().push_back(CD
);
269 MoveTool
->addRemovedDecl(MoveTool
->getMovedDecls().back());
270 MoveTool
->getUnremovedDeclsInOldHeader().erase(
271 MoveTool
->getMovedDecls().back());
274 ClangMoveTool
*MoveTool
;
277 // Expand to get the end location of the line where the EndLoc of the given
279 SourceLocation
getLocForEndOfDecl(const Decl
*D
,
280 const LangOptions
&LangOpts
= LangOptions()) {
281 const auto &SM
= D
->getASTContext().getSourceManager();
282 // If the expansion range is a character range, this is the location of
283 // the first character past the end. Otherwise it's the location of the
284 // first character in the final token in the range.
285 auto EndExpansionLoc
= SM
.getExpansionRange(D
->getEndLoc()).getEnd();
286 std::pair
<FileID
, unsigned> LocInfo
= SM
.getDecomposedLoc(EndExpansionLoc
);
287 // Try to load the file buffer.
288 bool InvalidTemp
= false;
289 llvm::StringRef File
= SM
.getBufferData(LocInfo
.first
, &InvalidTemp
);
291 return SourceLocation();
293 const char *TokBegin
= File
.data() + LocInfo
.second
;
294 // Lex from the start of the given location.
295 Lexer
Lex(SM
.getLocForStartOfFile(LocInfo
.first
), LangOpts
, File
.begin(),
296 TokBegin
, File
.end());
298 llvm::SmallVector
<char, 16> Line
;
299 // FIXME: this is a bit hacky to get ReadToEndOfLine work.
300 Lex
.setParsingPreprocessorDirective(true);
301 Lex
.ReadToEndOfLine(&Line
);
302 SourceLocation EndLoc
= EndExpansionLoc
.getLocWithOffset(Line
.size());
303 // If we already reach EOF, just return the EOF SourceLocation;
304 // otherwise, move 1 offset ahead to include the trailing newline character
306 return SM
.getLocForEndOfFile(LocInfo
.first
) == EndLoc
308 : EndLoc
.getLocWithOffset(1);
311 // Get full range of a Decl including the comments associated with it.
312 CharSourceRange
getFullRange(const Decl
*D
,
313 const LangOptions
&options
= LangOptions()) {
314 const auto &SM
= D
->getASTContext().getSourceManager();
315 SourceRange
Full(SM
.getExpansionLoc(D
->getBeginLoc()), getLocForEndOfDecl(D
));
316 // Expand to comments that are associated with the Decl.
317 if (const auto *Comment
= D
->getASTContext().getRawCommentForDeclNoCache(D
)) {
318 if (SM
.isBeforeInTranslationUnit(Full
.getEnd(), Comment
->getEndLoc()))
319 Full
.setEnd(Comment
->getEndLoc());
320 // FIXME: Don't delete a preceding comment, if there are no other entities
321 // it could refer to.
322 if (SM
.isBeforeInTranslationUnit(Comment
->getBeginLoc(), Full
.getBegin()))
323 Full
.setBegin(Comment
->getBeginLoc());
326 return CharSourceRange::getCharRange(Full
);
329 std::string
getDeclarationSourceText(const Decl
*D
) {
330 const auto &SM
= D
->getASTContext().getSourceManager();
331 llvm::StringRef SourceText
=
332 Lexer::getSourceText(getFullRange(D
), SM
, LangOptions());
333 return SourceText
.str();
336 bool isInHeaderFile(const Decl
*D
, llvm::StringRef OriginalRunningDirectory
,
337 llvm::StringRef OldHeader
) {
338 const auto &SM
= D
->getASTContext().getSourceManager();
339 if (OldHeader
.empty())
341 auto ExpansionLoc
= SM
.getExpansionLoc(D
->getBeginLoc());
342 if (ExpansionLoc
.isInvalid())
345 if (auto FE
= SM
.getFileEntryRefForID(SM
.getFileID(ExpansionLoc
))) {
346 return MakeAbsolutePath(SM
, FE
->getName()) ==
347 MakeAbsolutePath(OriginalRunningDirectory
, OldHeader
);
353 std::vector
<std::string
> getNamespaces(const Decl
*D
) {
354 std::vector
<std::string
> Namespaces
;
355 for (const auto *Context
= D
->getDeclContext(); Context
;
356 Context
= Context
->getParent()) {
357 if (llvm::isa
<TranslationUnitDecl
>(Context
) ||
358 llvm::isa
<LinkageSpecDecl
>(Context
))
361 if (const auto *ND
= llvm::dyn_cast
<NamespaceDecl
>(Context
))
362 Namespaces
.push_back(ND
->getName().str());
364 std::reverse(Namespaces
.begin(), Namespaces
.end());
368 tooling::Replacements
369 createInsertedReplacements(const std::vector
<std::string
> &Includes
,
370 const std::vector
<const NamedDecl
*> &Decls
,
371 llvm::StringRef FileName
, bool IsHeader
= false,
372 StringRef OldHeaderInclude
= "") {
374 std::string
GuardName(FileName
);
376 for (size_t i
= 0; i
< GuardName
.size(); ++i
) {
377 if (!isAlphanumeric(GuardName
[i
]))
380 GuardName
= StringRef(GuardName
).upper();
381 NewCode
+= "#ifndef " + GuardName
+ "\n";
382 NewCode
+= "#define " + GuardName
+ "\n\n";
385 NewCode
+= OldHeaderInclude
;
387 for (const auto &Include
: Includes
)
390 if (!Includes
.empty())
393 // Add moved class definition and its related declarations. All declarations
394 // in same namespace are grouped together.
396 // Record namespaces where the current position is in.
397 std::vector
<std::string
> CurrentNamespaces
;
398 for (const auto *MovedDecl
: Decls
) {
399 // The namespaces of the declaration being moved.
400 std::vector
<std::string
> DeclNamespaces
= getNamespaces(MovedDecl
);
401 auto CurrentIt
= CurrentNamespaces
.begin();
402 auto DeclIt
= DeclNamespaces
.begin();
403 // Skip the common prefix.
404 while (CurrentIt
!= CurrentNamespaces
.end() &&
405 DeclIt
!= DeclNamespaces
.end()) {
406 if (*CurrentIt
!= *DeclIt
)
411 // Calculate the new namespaces after adding MovedDecl in CurrentNamespace,
412 // which is used for next iteration of this loop.
413 std::vector
<std::string
> NextNamespaces(CurrentNamespaces
.begin(),
415 NextNamespaces
.insert(NextNamespaces
.end(), DeclIt
, DeclNamespaces
.end());
418 // End with CurrentNamespace.
419 bool HasEndCurrentNamespace
= false;
420 auto RemainingSize
= CurrentNamespaces
.end() - CurrentIt
;
421 for (auto It
= CurrentNamespaces
.rbegin(); RemainingSize
> 0;
422 --RemainingSize
, ++It
) {
423 assert(It
< CurrentNamespaces
.rend());
424 NewCode
+= "} // namespace " + *It
+ "\n";
425 HasEndCurrentNamespace
= true;
427 // Add trailing '\n' after the nested namespace definition.
428 if (HasEndCurrentNamespace
)
431 // If the moved declaration is not in CurrentNamespace, add extra namespace
433 bool IsInNewNamespace
= false;
434 while (DeclIt
!= DeclNamespaces
.end()) {
435 NewCode
+= "namespace " + *DeclIt
+ " {\n";
436 IsInNewNamespace
= true;
439 // If the moved declaration is in same namespace CurrentNamespace, add
440 // a preceeding `\n' before the moved declaration.
441 // FIXME: Don't add empty lines between using declarations.
442 if (!IsInNewNamespace
)
444 NewCode
+= getDeclarationSourceText(MovedDecl
);
445 CurrentNamespaces
= std::move(NextNamespaces
);
447 std::reverse(CurrentNamespaces
.begin(), CurrentNamespaces
.end());
448 for (const auto &NS
: CurrentNamespaces
)
449 NewCode
+= "} // namespace " + NS
+ "\n";
452 NewCode
+= "\n#endif // " + GuardName
+ "\n";
453 return tooling::Replacements(tooling::Replacement(FileName
, 0, 0, NewCode
));
456 // Return a set of all decls which are used/referenced by the given Decls.
457 // Specifically, given a class member declaration, this method will return all
458 // decls which are used by the whole class.
459 llvm::DenseSet
<const Decl
*>
460 getUsedDecls(const HelperDeclRefGraph
*RG
,
461 const std::vector
<const NamedDecl
*> &Decls
) {
463 llvm::DenseSet
<const CallGraphNode
*> Nodes
;
464 for (const auto *D
: Decls
) {
465 auto Result
= RG
->getReachableNodes(
466 HelperDeclRGBuilder::getOutmostClassOrFunDecl(D
));
467 Nodes
.insert(Result
.begin(), Result
.end());
469 llvm::DenseSet
<const Decl
*> Results
;
470 for (const auto *Node
: Nodes
)
471 Results
.insert(Node
->getDecl());
477 std::unique_ptr
<ASTConsumer
>
478 ClangMoveAction::CreateASTConsumer(CompilerInstance
&Compiler
,
479 StringRef
/*InFile*/) {
480 Compiler
.getPreprocessor().addPPCallbacks(std::make_unique
<FindAllIncludes
>(
481 &Compiler
.getSourceManager(), &MoveTool
));
482 return MatchFinder
.newASTConsumer();
485 ClangMoveTool::ClangMoveTool(ClangMoveContext
*const Context
,
486 DeclarationReporter
*const Reporter
)
487 : Context(Context
), Reporter(Reporter
) {
488 if (!Context
->Spec
.NewHeader
.empty())
489 CCIncludes
.push_back("#include \"" + Context
->Spec
.NewHeader
+ "\"\n");
492 void ClangMoveTool::addRemovedDecl(const NamedDecl
*Decl
) {
493 const auto &SM
= Decl
->getASTContext().getSourceManager();
494 auto Loc
= Decl
->getLocation();
495 StringRef FilePath
= SM
.getFilename(Loc
);
496 FilePathToFileID
[FilePath
] = SM
.getFileID(Loc
);
497 RemovedDecls
.push_back(Decl
);
500 void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder
*Finder
) {
502 isExpansionInFile(makeAbsolutePath(Context
->Spec
.OldHeader
));
503 auto InOldCC
= isExpansionInFile(makeAbsolutePath(Context
->Spec
.OldCC
));
504 auto InOldFiles
= anyOf(InOldHeader
, InOldCC
);
505 auto classTemplateForwardDecls
=
506 classTemplateDecl(unless(has(cxxRecordDecl(isDefinition()))));
507 auto ForwardClassDecls
= namedDecl(
508 anyOf(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition()))),
509 classTemplateForwardDecls
));
511 hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl()));
513 //============================================================================
514 // Matchers for old header
515 //============================================================================
516 // Match all top-level named declarations (e.g. function, variable, enum) in
517 // old header, exclude forward class declarations and namespace declarations.
519 // We consider declarations inside a class belongs to the class. So these
520 // declarations will be ignored.
521 auto AllDeclsInHeader
= namedDecl(
522 unless(ForwardClassDecls
), unless(namespaceDecl()),
523 unless(usingDirectiveDecl()), // using namespace decl.
526 hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))),
527 hasDeclContext(decl(anyOf(namespaceDecl(), translationUnitDecl()))));
528 Finder
->addMatcher(AllDeclsInHeader
.bind("decls_in_header"), this);
530 // Don't register other matchers when dumping all declarations in header.
531 if (Context
->DumpDeclarations
)
534 // Match forward declarations in old header.
535 Finder
->addMatcher(namedDecl(ForwardClassDecls
, InOldHeader
).bind("fwd_decl"),
538 //============================================================================
539 // Matchers for old cc
540 //============================================================================
541 auto IsOldCCTopLevelDecl
= allOf(
542 hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))), InOldCC
);
543 // Matching using decls/type alias decls which are in named/anonymous/global
544 // namespace, these decls are always copied to new.h/cc. Those in classes,
545 // functions are covered in other matchers.
546 Finder
->addMatcher(namedDecl(anyOf(usingDecl(IsOldCCTopLevelDecl
),
547 usingDirectiveDecl(unless(isImplicit()),
548 IsOldCCTopLevelDecl
),
549 typeAliasDecl(IsOldCCTopLevelDecl
)),
554 // Match static functions/variable definitions which are defined in named
556 SmallVector
<std::string
, 4> QualNames
;
557 QualNames
.reserve(Context
->Spec
.Names
.size());
558 for (StringRef SymbolName
: Context
->Spec
.Names
) {
559 QualNames
.push_back(("::" + SymbolName
.trim().ltrim(':')).str());
562 if (QualNames
.empty()) {
563 llvm::errs() << "No symbols being moved.\n";
567 ast_matchers::internal::Matcher
<NamedDecl
> HasAnySymbolNames
=
568 hasAnyName(SmallVector
<StringRef
, 4>(QualNames
.begin(), QualNames
.end()));
571 hasOutermostEnclosingClass(cxxRecordDecl(HasAnySymbolNames
));
573 // Matchers for helper declarations in old.cc.
574 auto InAnonymousNS
= hasParent(namespaceDecl(isAnonymous()));
575 auto NotInMovedClass
= allOf(unless(InMovedClass
), InOldCC
);
577 allOf(NotInMovedClass
, anyOf(isStaticStorageClass(), InAnonymousNS
));
578 // Match helper classes separately with helper functions/variables since we
579 // want to reuse these matchers in finding helpers usage below.
581 // There could be forward declarations usage for helpers, especially for
582 // classes and functions. We need include these forward declarations.
584 // Forward declarations for variable helpers will be excluded as these
585 // declarations (with "extern") are not supposed in cpp file.
586 auto HelperFuncOrVar
=
587 namedDecl(notInMacro(), anyOf(functionDecl(IsOldCCHelper
),
588 varDecl(isDefinition(), IsOldCCHelper
)));
590 cxxRecordDecl(notInMacro(), NotInMovedClass
, InAnonymousNS
);
591 // Save all helper declarations in old.cc.
593 namedDecl(anyOf(HelperFuncOrVar
, HelperClasses
)).bind("helper_decls"),
596 // Construct an AST-based call graph of helper declarations in old.cc.
597 // In the following matcheres, "dc" is a caller while "helper_decls" and
598 // "used_class" is a callee, so a new edge starting from caller to callee will
599 // be add in the graph.
601 // Find helper function/variable usages.
603 declRefExpr(to(HelperFuncOrVar
), hasAncestor(decl().bind("dc")))
606 // Find helper class usages.
608 typeLoc(loc(recordType(hasDeclaration(HelperClasses
.bind("used_class")))),
609 hasAncestor(decl().bind("dc"))),
612 //============================================================================
613 // Matchers for old files, including old.h/old.cc
614 //============================================================================
615 // Create a MatchCallback for class declarations.
616 MatchCallbacks
.push_back(std::make_unique
<ClassDeclarationMatch
>(this));
617 // Match moved class declarations.
619 cxxRecordDecl(InOldFiles
, HasAnySymbolNames
, isDefinition(), TopLevelDecl
)
620 .bind("moved_class");
621 Finder
->addMatcher(MovedClass
, MatchCallbacks
.back().get());
622 // Match moved class methods (static methods included) which are defined
623 // outside moved class declaration.
624 Finder
->addMatcher(cxxMethodDecl(InOldFiles
,
625 ofOutermostEnclosingClass(HasAnySymbolNames
),
627 .bind("class_method"),
628 MatchCallbacks
.back().get());
629 // Match static member variable definition of the moved class.
631 varDecl(InMovedClass
, InOldFiles
, isDefinition(), isStaticDataMember())
632 .bind("class_static_var_decl"),
633 MatchCallbacks
.back().get());
635 MatchCallbacks
.push_back(std::make_unique
<FunctionDeclarationMatch
>(this));
636 Finder
->addMatcher(functionDecl(InOldFiles
, HasAnySymbolNames
, TopLevelDecl
)
638 MatchCallbacks
.back().get());
640 MatchCallbacks
.push_back(std::make_unique
<VarDeclarationMatch
>(this));
642 varDecl(InOldFiles
, HasAnySymbolNames
, TopLevelDecl
).bind("var"),
643 MatchCallbacks
.back().get());
645 // Match enum definition in old.h. Enum helpers (which are defined in old.cc)
646 // will not be moved for now no matter whether they are used or not.
647 MatchCallbacks
.push_back(std::make_unique
<EnumDeclarationMatch
>(this));
649 enumDecl(InOldHeader
, HasAnySymbolNames
, isDefinition(), TopLevelDecl
)
651 MatchCallbacks
.back().get());
653 // Match type alias in old.h, this includes "typedef" and "using" type alias
654 // declarations. Type alias helpers (which are defined in old.cc) will not be
655 // moved for now no matter whether they are used or not.
656 MatchCallbacks
.push_back(std::make_unique
<TypeAliasMatch
>(this));
657 Finder
->addMatcher(namedDecl(anyOf(typedefDecl().bind("typedef"),
658 typeAliasDecl().bind("type_alias")),
659 InOldHeader
, HasAnySymbolNames
, TopLevelDecl
),
660 MatchCallbacks
.back().get());
663 void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult
&Result
) {
664 if (const auto *D
= Result
.Nodes
.getNodeAs
<NamedDecl
>("decls_in_header")) {
665 UnremovedDeclsInOldHeader
.insert(D
);
666 } else if (const auto *FWD
=
667 Result
.Nodes
.getNodeAs
<CXXRecordDecl
>("fwd_decl")) {
668 // Skip all forward declarations which appear after moved class declaration.
669 if (RemovedDecls
.empty()) {
670 if (const auto *DCT
= FWD
->getDescribedClassTemplate())
671 MovedDecls
.push_back(DCT
);
673 MovedDecls
.push_back(FWD
);
675 } else if (const auto *ND
=
676 Result
.Nodes
.getNodeAs
<NamedDecl
>("helper_decls")) {
677 MovedDecls
.push_back(ND
);
678 HelperDeclarations
.push_back(ND
);
679 LLVM_DEBUG(llvm::dbgs()
680 << "Add helper : " << ND
->getDeclName() << " (" << ND
<< ")\n");
681 } else if (const auto *UD
= Result
.Nodes
.getNodeAs
<NamedDecl
>("using_decl")) {
682 MovedDecls
.push_back(UD
);
686 std::string
ClangMoveTool::makeAbsolutePath(StringRef Path
) {
687 return MakeAbsolutePath(Context
->OriginalRunningDirectory
, Path
);
690 void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader
, bool IsAngled
,
691 llvm::StringRef SearchPath
,
692 llvm::StringRef FileName
,
693 CharSourceRange IncludeFilenameRange
,
694 const SourceManager
&SM
) {
695 SmallString
<128> HeaderWithSearchPath
;
696 llvm::sys::path::append(HeaderWithSearchPath
, SearchPath
, IncludeHeader
);
697 std::string AbsoluteIncludeHeader
=
698 MakeAbsolutePath(SM
, HeaderWithSearchPath
);
699 std::string IncludeLine
=
700 IsAngled
? ("#include <" + IncludeHeader
+ ">\n").str()
701 : ("#include \"" + IncludeHeader
+ "\"\n").str();
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
;
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
;
718 CCIncludes
.push_back(IncludeLine
);
722 void ClangMoveTool::removeDeclsInOldFiles() {
723 if (RemovedDecls
.empty()) return;
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
);
733 auto UsedDecls
= getUsedDecls(RGBuilder
.getGraph(), UnremovedDecls
);
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: " << D
->getDeclName()
739 << " (" << D
<< ")\n");
740 if (!UsedDecls
.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl(
741 D
->getCanonicalDecl()))) {
742 LLVM_DEBUG(llvm::dbgs() << "Helper removed in old.cc: "
743 << D
->getDeclName() << " (" << D
<< ")\n");
744 RemovedDecls
.push_back(D
);
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()),
755 std::string FilePath
= RemoveReplacement
.getFilePath().str();
756 auto Err
= Context
->FileToReplacements
[FilePath
].add(RemoveReplacement
);
758 llvm::errs() << llvm::toString(std::move(Err
)) << "\n";
760 const auto &SM
= RemovedDecls
[0]->getASTContext().getSourceManager();
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 clang-include-fixer.
770 std::string IncludeNewH
=
771 "#include \"" + Context
->Spec
.NewHeader
+ "\"\n";
772 // This replacement for inserting header will be cleaned up at the end.
773 auto Err
= FileAndReplacements
.second
.add(
774 tooling::Replacement(FilePath
, UINT_MAX
, 0, IncludeNewH
));
776 llvm::errs() << llvm::toString(std::move(Err
)) << "\n";
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
);
786 llvm::errs() << llvm::toString(Style
.takeError()) << "\n";
789 auto CleanReplacements
= format::cleanupAroundReplacements(
790 Code
, Context
->FileToReplacements
[std::string(FilePath
)], *Style
);
792 if (!CleanReplacements
) {
793 llvm::errs() << llvm::toString(CleanReplacements
.takeError()) << "\n";
796 Context
->FileToReplacements
[std::string(FilePath
)] = *CleanReplacements
;
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
);
808 NewCCDecls
.push_back(MovedDecl
);
811 auto UsedDecls
= getUsedDecls(RGBuilder
.getGraph(), RemovedDecls
);
812 std::vector
<const NamedDecl
*> ActualNewCCDecls
;
814 // Filter out all unused helpers in NewCCDecls.
815 // We only move the used helpers (including transitively 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())))
823 LLVM_DEBUG(llvm::dbgs() << "Helper used in new.cc: " << D
->getDeclName()
824 << " " << D
<< "\n");
825 ActualNewCCDecls
.push_back(D
);
828 if (!Context
->Spec
.NewHeader
.empty()) {
829 std::string OldHeaderInclude
=
830 Context
->Spec
.NewDependOnOld
831 ? "#include \"" + Context
->Spec
.OldHeader
+ "\"\n"
833 Context
->FileToReplacements
[Context
->Spec
.NewHeader
] =
834 createInsertedReplacements(HeaderIncludes
, NewHeaderDecls
,
835 Context
->Spec
.NewHeader
, /*IsHeader=*/true,
838 if (!Context
->Spec
.NewCC
.empty())
839 Context
->FileToReplacements
[Context
->Spec
.NewCC
] =
840 createInsertedReplacements(CCIncludes
, ActualNewCCDecls
,
841 Context
->Spec
.NewCC
);
844 // Move all contents from OldFile to NewFile.
845 void ClangMoveTool::moveAll(SourceManager
&SM
, StringRef OldFile
,
847 auto FE
= SM
.getFileManager().getOptionalFileRef(makeAbsolutePath(OldFile
));
849 llvm::errs() << "Failed to get file: " << OldFile
<< "\n";
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
),
857 std::string FilePath
= RemoveAll
.getFilePath().str();
858 Context
->FileToReplacements
[FilePath
] = tooling::Replacements(RemoveAll
);
860 StringRef Code
= SM
.getBufferData(ID
);
861 if (!NewFile
.empty()) {
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
+ '"')));
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
[std::string(NewFile
)] = std::move(AllCode
);
879 void ClangMoveTool::onEndOfTranslationUnit() {
880 if (Context
->DumpDeclarations
) {
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
);
903 if (RemovedDecls
.empty())
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
:
924 if (llvm::none_of(UnremovedDeclsInOldHeader
, IsSupportedKind
) &&
925 !Context
->Spec
.OldHeader
.empty()) {
926 auto &SM
= RemovedDecls
[0]->getASTContext().getSourceManager();
927 moveAll(SM
, Context
->Spec
.OldHeader
, Context
->Spec
.NewHeader
);
928 moveAll(SM
, Context
->Spec
.OldCC
, Context
->Spec
.NewCC
);
931 LLVM_DEBUG(RGBuilder
.getGraph()->dump());
932 moveDeclsToNewFiles();
933 removeDeclsInOldFiles();