1 //===-- Move.h - Clang move ----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H
12 #include "HelperDeclRefGraph.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Frontend/FrontendAction.h"
15 #include "clang/Tooling/Core/Replacement.h"
16 #include "clang/Tooling/Tooling.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
28 // A reporter which collects and reports declarations in old header.
29 class DeclarationReporter
{
31 DeclarationReporter() = default;
32 ~DeclarationReporter() = default;
34 void reportDeclaration(llvm::StringRef DeclarationName
, llvm::StringRef Type
,
36 DeclarationList
.emplace_back(DeclarationName
, Type
, Templated
);
40 Declaration(llvm::StringRef QName
, llvm::StringRef Kind
, bool Templated
)
41 : QualifiedName(QName
), Kind(Kind
), Templated(Templated
) {}
43 friend bool operator==(const Declaration
&LHS
, const Declaration
&RHS
) {
44 return std::tie(LHS
.QualifiedName
, LHS
.Kind
, LHS
.Templated
) ==
45 std::tie(RHS
.QualifiedName
, RHS
.Kind
, RHS
.Templated
);
47 std::string QualifiedName
; // E.g. A::B::Foo.
48 std::string Kind
; // E.g. Function, Class
49 bool Templated
= false; // Whether the declaration is templated.
52 const std::vector
<Declaration
> getDeclarationList() const {
53 return DeclarationList
;
57 std::vector
<Declaration
> DeclarationList
;
60 // Specify declarations being moved. It contains all information of the moved
62 struct MoveDefinitionSpec
{
63 // The list of fully qualified names, e.g. Foo, a::Foo, b::Foo.
64 SmallVector
<std::string
, 4> Names
;
65 // The file path of old header, can be relative path and absolute path.
66 std::string OldHeader
;
67 // The file path of old cc, can be relative path and absolute path.
69 // The file path of new header, can be relative path and absolute path.
70 std::string NewHeader
;
71 // The file path of new cc, can be relative path and absolute path.
73 // Whether old.h depends on new.h. If true, #include "new.h" will be added
75 bool OldDependOnNew
= false;
76 // Whether new.h depends on old.h. If true, #include "old.h" will be added
78 bool NewDependOnOld
= false;
81 // A Context which contains extra options which are used in ClangMoveTool.
82 struct ClangMoveContext
{
83 MoveDefinitionSpec Spec
;
84 // The Key is file path, value is the replacements being applied to the file.
85 std::map
<std::string
, tooling::Replacements
> &FileToReplacements
;
86 // The original working directory where the local clang-move binary runs.
88 // clang-move will change its current working directory to the build
89 // directory when analyzing the source file. We save the original working
90 // directory in order to get the absolute file path for the fields in Spec.
91 std::string OriginalRunningDirectory
;
92 // The name of a predefined code style.
93 std::string FallbackStyle
;
94 // Whether dump all declarations in old header.
95 bool DumpDeclarations
;
98 // This tool is used to move class/function definitions from the given source
99 // files (old.h/cc) to new files (new.h/cc).
100 // The goal of this tool is to make the new/old files as compilable as possible.
102 // When moving a symbol,all used helper declarations (e.g. static
103 // functions/variables definitions in global/named namespace,
104 // functions/variables/classes definitions in anonymous namespace) used by the
105 // moved symbol in old.cc are moved to the new.cc. In addition, all
106 // using-declarations in old.cc are also moved to new.cc; forward class
107 // declarations in old.h are also moved to new.h.
109 // The remaining helper declarations which are unused by non-moved symbols in
110 // old.cc will be removed.
112 // Note: When all declarations in old header are being moved, all code in
113 // old.h/cc will be moved, which means old.h/cc are empty. This ignores symbols
114 // that are not supported (e.g. typedef and enum) so that we always move old
115 // files to new files when all symbols produced from dump_decls are moved.
116 class ClangMoveTool
: public ast_matchers::MatchFinder::MatchCallback
{
118 ClangMoveTool(ClangMoveContext
*const Context
,
119 DeclarationReporter
*const Reporter
);
121 void registerMatchers(ast_matchers::MatchFinder
*Finder
);
123 void run(const ast_matchers::MatchFinder::MatchResult
&Result
) override
;
125 void onEndOfTranslationUnit() override
;
127 /// Add #includes from old.h/cc files.
129 /// \param IncludeHeader The name of the file being included, as written in
131 /// \param IsAngled Whether the file name was enclosed in angle brackets.
132 /// \param SearchPath The search path which was used to find the IncludeHeader
133 /// in the file system. It can be a relative path or an absolute path.
134 /// \param FileName The name of file where the IncludeHeader comes from.
135 /// \param IncludeFilenameRange The source range for the written file name in
136 /// #include (i.e. "old.h" for #include "old.h") in old.cc.
137 /// \param SM The SourceManager.
138 void addIncludes(llvm::StringRef IncludeHeader
, bool IsAngled
,
139 llvm::StringRef SearchPath
, llvm::StringRef FileName
,
140 clang::CharSourceRange IncludeFilenameRange
,
141 const SourceManager
&SM
);
143 std::vector
<const NamedDecl
*> &getMovedDecls() { return MovedDecls
; }
145 /// Add declarations being removed from old.h/cc. For each declarations, the
146 /// method also records the mapping relationship between the corresponding
147 /// FilePath and its FileID.
148 void addRemovedDecl(const NamedDecl
*Decl
);
150 llvm::SmallPtrSet
<const NamedDecl
*, 8> &getUnremovedDeclsInOldHeader() {
151 return UnremovedDeclsInOldHeader
;
155 // Make the Path absolute using the OrignalRunningDirectory if the Path is not
156 // an absolute path. An empty Path will result in an empty string.
157 std::string
makeAbsolutePath(StringRef Path
);
159 void removeDeclsInOldFiles();
160 void moveDeclsToNewFiles();
161 void moveAll(SourceManager
& SM
, StringRef OldFile
, StringRef NewFile
);
163 // Stores all MatchCallbacks created by this tool.
164 std::vector
<std::unique_ptr
<ast_matchers::MatchFinder::MatchCallback
>>
166 // Store all potential declarations (decls being moved, forward decls) that
167 // might need to move to new.h/cc. It includes all helper declarations
168 // (include unused ones) by default. The unused ones will be filtered out in
169 // the last stage. Saving in an AST-visited order.
170 std::vector
<const NamedDecl
*> MovedDecls
;
171 // The declarations that needs to be removed in old.cc/h.
172 std::vector
<const NamedDecl
*> RemovedDecls
;
173 // The #includes in old_header.h.
174 std::vector
<std::string
> HeaderIncludes
;
175 // The #includes in old_cc.cc.
176 std::vector
<std::string
> CCIncludes
;
177 // Records all helper declarations (function/variable/class definitions in
178 // anonymous namespaces, static function/variable definitions in global/named
179 // namespaces) in old.cc. saving in an AST-visited order.
180 std::vector
<const NamedDecl
*> HelperDeclarations
;
181 // The unmoved named declarations in old header.
182 llvm::SmallPtrSet
<const NamedDecl
*, 8> UnremovedDeclsInOldHeader
;
183 /// The source range for the written file name in #include (i.e. "old.h" for
184 /// #include "old.h") in old.cc, including the enclosing quotes or angle
186 clang::CharSourceRange OldHeaderIncludeRangeInCC
;
187 /// The source range for the written file name in #include (i.e. "old.h" for
188 /// #include "old.h") in old.h, including the enclosing quotes or angle
190 clang::CharSourceRange OldHeaderIncludeRangeInHeader
;
191 /// Mapping from FilePath to FileID, which can be used in post processes like
192 /// cleanup around replacements.
193 llvm::StringMap
<FileID
> FilePathToFileID
;
194 /// A context contains all running options. It is not owned.
195 ClangMoveContext
*const Context
;
196 /// A reporter to report all declarations from old header. It is not owned.
197 DeclarationReporter
*const Reporter
;
198 /// Builder for helper declarations reference graph.
199 HelperDeclRGBuilder RGBuilder
;
202 class ClangMoveAction
: public clang::ASTFrontendAction
{
204 ClangMoveAction(ClangMoveContext
*const Context
,
205 DeclarationReporter
*const Reporter
)
206 : MoveTool(Context
, Reporter
) {
207 MoveTool
.registerMatchers(&MatchFinder
);
210 ~ClangMoveAction() override
= default;
212 std::unique_ptr
<clang::ASTConsumer
>
213 CreateASTConsumer(clang::CompilerInstance
&Compiler
,
214 llvm::StringRef InFile
) override
;
217 ast_matchers::MatchFinder MatchFinder
;
218 ClangMoveTool MoveTool
;
221 class ClangMoveActionFactory
: public tooling::FrontendActionFactory
{
223 ClangMoveActionFactory(ClangMoveContext
*const Context
,
224 DeclarationReporter
*const Reporter
= nullptr)
225 : Context(Context
), Reporter(Reporter
) {}
227 std::unique_ptr
<clang::FrontendAction
> create() override
{
228 return std::make_unique
<ClangMoveAction
>(Context
, Reporter
);
233 ClangMoveContext
*const Context
;
234 DeclarationReporter
*const Reporter
;
240 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H