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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang-tools-extra/clang-move/Move.h