blob: 94172181e93d458b0e8010bce211febdd2c48854 [file] [log] [blame]
Haojian Wu357ef992016-09-21 13:18:19 +00001//===-- ClangMove.h - Clang move -----------------------------------------===//
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#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H
12
Haojian Wu36265162017-01-03 09:00:51 +000013#include "HelperDeclRefGraph.h"
Haojian Wu357ef992016-09-21 13:18:19 +000014#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/Frontend/FrontendAction.h"
16#include "clang/Tooling/Core/Replacement.h"
17#include "clang/Tooling/Tooling.h"
Haojian Wu2930be12016-11-08 19:55:13 +000018#include "llvm/ADT/SmallPtrSet.h"
Haojian Wu48ac3042016-11-23 10:04:19 +000019#include "llvm/ADT/StringMap.h"
Eric Liubce181c2018-10-08 17:22:50 +000020#include "llvm/ADT/StringRef.h"
Haojian Wu357ef992016-09-21 13:18:19 +000021#include <map>
Haojian Wu35ca9462016-11-14 14:15:44 +000022#include <memory>
Haojian Wu357ef992016-09-21 13:18:19 +000023#include <string>
24#include <vector>
25
26namespace clang {
27namespace move {
28
Haojian Wub15c8da2016-11-24 10:17:17 +000029// A reporter which collects and reports declarations in old header.
30class DeclarationReporter {
31public:
32 DeclarationReporter() = default;
33 ~DeclarationReporter() = default;
34
Eric Liubce181c2018-10-08 17:22:50 +000035 void reportDeclaration(llvm::StringRef DeclarationName, llvm::StringRef Type,
36 bool Templated) {
37 DeclarationList.emplace_back(DeclarationName, Type, Templated);
Haojian Wub15c8da2016-11-24 10:17:17 +000038 };
39
Eric Liubce181c2018-10-08 17:22:50 +000040 struct Declaration {
41 Declaration(llvm::StringRef QName, llvm::StringRef Kind, bool Templated)
42 : QualifiedName(QName), Kind(Kind), Templated(Templated) {}
Haojian Wub15c8da2016-11-24 10:17:17 +000043
Eric Liubce181c2018-10-08 17:22:50 +000044 friend bool operator==(const Declaration &LHS, const Declaration &RHS) {
45 return std::tie(LHS.QualifiedName, LHS.Kind, LHS.Templated) ==
46 std::tie(RHS.QualifiedName, RHS.Kind, RHS.Templated);
47 }
48 std::string QualifiedName; // E.g. A::B::Foo.
49 std::string Kind; // E.g. Function, Class
50 bool Templated = false; // Whether the declaration is templated.
51 };
52
53 const std::vector<Declaration> getDeclarationList() const {
Haojian Wub15c8da2016-11-24 10:17:17 +000054 return DeclarationList;
55 }
56
57private:
Eric Liubce181c2018-10-08 17:22:50 +000058 std::vector<Declaration> DeclarationList;
Haojian Wub15c8da2016-11-24 10:17:17 +000059};
60
61// Specify declarations being moved. It contains all information of the moved
62// declarations.
63struct MoveDefinitionSpec {
64 // The list of fully qualified names, e.g. Foo, a::Foo, b::Foo.
65 SmallVector<std::string, 4> Names;
66 // The file path of old header, can be relative path and absolute path.
67 std::string OldHeader;
68 // The file path of old cc, can be relative path and absolute path.
69 std::string OldCC;
70 // The file path of new header, can be relative path and absolute path.
71 std::string NewHeader;
72 // The file path of new cc, can be relative path and absolute path.
73 std::string NewCC;
74 // Whether old.h depends on new.h. If true, #include "new.h" will be added
75 // in old.h.
76 bool OldDependOnNew = false;
77 // Whether new.h depends on old.h. If true, #include "old.h" will be added
78 // in new.h.
79 bool NewDependOnOld = false;
80};
81
82// A Context which contains extra options which are used in ClangMoveTool.
83struct ClangMoveContext {
84 MoveDefinitionSpec Spec;
85 // The Key is file path, value is the replacements being applied to the file.
86 std::map<std::string, tooling::Replacements> &FileToReplacements;
87 // The original working directory where the local clang-move binary runs.
88 //
89 // clang-move will change its current working directory to the build
90 // directory when analyzing the source file. We save the original working
91 // directory in order to get the absolute file path for the fields in Spec.
92 std::string OriginalRunningDirectory;
93 // The name of a predefined code style.
94 std::string FallbackStyle;
95 // Whether dump all declarations in old header.
96 bool DumpDeclarations;
97};
98
99// This tool is used to move class/function definitions from the given source
Haojian Wu36265162017-01-03 09:00:51 +0000100// files (old.h/cc) to new files (new.h/cc).
101// The goal of this tool is to make the new/old files as compilable as possible.
102//
103// When moving a symbol,all used helper declarations (e.g. static
104// functions/variables definitions in global/named namespace,
105// functions/variables/classes definitions in anonymous namespace) used by the
106// moved symbol in old.cc are moved to the new.cc. In addition, all
107// using-declarations in old.cc are also moved to new.cc; forward class
108// declarations in old.h are also moved to new.h.
109//
110// The remaining helper declarations which are unused by non-moved symbols in
111// old.cc will be removed.
Haojian Wu2930be12016-11-08 19:55:13 +0000112//
Haojian Wub15c8da2016-11-24 10:17:17 +0000113// Note: When all declarations in old header are being moved, all code in
Eric Liu47a42d52016-12-06 10:12:23 +0000114// old.h/cc will be moved, which means old.h/cc are empty. This ignores symbols
115// that are not supported (e.g. typedef and enum) so that we always move old
116// files to new files when all symbols produced from dump_decls are moved.
Haojian Wu357ef992016-09-21 13:18:19 +0000117class ClangMoveTool : public ast_matchers::MatchFinder::MatchCallback {
118public:
Haojian Wub15c8da2016-11-24 10:17:17 +0000119 ClangMoveTool(ClangMoveContext *const Context,
120 DeclarationReporter *const Reporter);
Haojian Wu357ef992016-09-21 13:18:19 +0000121
122 void registerMatchers(ast_matchers::MatchFinder *Finder);
123
124 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
125
126 void onEndOfTranslationUnit() override;
127
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000128 /// Add #includes from old.h/cc files.
129 ///
130 /// \param IncludeHeader The name of the file being included, as written in
131 /// the source code.
132 /// \param IsAngled Whether the file name was enclosed in angle brackets.
133 /// \param SearchPath The search path which was used to find the IncludeHeader
134 /// in the file system. It can be a relative path or an absolute path.
135 /// \param FileName The name of file where the IncludeHeader comes from.
Haojian Wub15c8da2016-11-24 10:17:17 +0000136 /// \param IncludeFilenameRange The source range for the written file name in
137 /// #include (i.e. "old.h" for #include "old.h") in old.cc.
Haojian Wuef445b72016-10-04 10:40:52 +0000138 /// \param SM The SourceManager.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000139 void addIncludes(llvm::StringRef IncludeHeader, bool IsAngled,
140 llvm::StringRef SearchPath, llvm::StringRef FileName,
Haojian Wu2930be12016-11-08 19:55:13 +0000141 clang::CharSourceRange IncludeFilenameRange,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000142 const SourceManager &SM);
Haojian Wu357ef992016-09-21 13:18:19 +0000143
Haojian Wu08e402a2016-12-02 12:39:39 +0000144 std::vector<const NamedDecl *> &getMovedDecls() { return MovedDecls; }
Haojian Wu35ca9462016-11-14 14:15:44 +0000145
Haojian Wu48ac3042016-11-23 10:04:19 +0000146 /// Add declarations being removed from old.h/cc. For each declarations, the
147 /// method also records the mapping relationship between the corresponding
148 /// FilePath and its FileID.
Haojian Wu08e402a2016-12-02 12:39:39 +0000149 void addRemovedDecl(const NamedDecl *Decl);
Haojian Wu35ca9462016-11-14 14:15:44 +0000150
151 llvm::SmallPtrSet<const NamedDecl *, 8> &getUnremovedDeclsInOldHeader() {
152 return UnremovedDeclsInOldHeader;
153 }
154
Haojian Wu357ef992016-09-21 13:18:19 +0000155private:
Haojian Wu2930be12016-11-08 19:55:13 +0000156 // Make the Path absolute using the OrignalRunningDirectory if the Path is not
157 // an absolute path. An empty Path will result in an empty string.
158 std::string makeAbsolutePath(StringRef Path);
159
Haojian Wu08e402a2016-12-02 12:39:39 +0000160 void removeDeclsInOldFiles();
161 void moveDeclsToNewFiles();
Haojian Wu2930be12016-11-08 19:55:13 +0000162 void moveAll(SourceManager& SM, StringRef OldFile, StringRef NewFile);
Haojian Wu357ef992016-09-21 13:18:19 +0000163
Haojian Wu35ca9462016-11-14 14:15:44 +0000164 // Stores all MatchCallbacks created by this tool.
165 std::vector<std::unique_ptr<ast_matchers::MatchFinder::MatchCallback>>
166 MatchCallbacks;
Haojian Wu36265162017-01-03 09:00:51 +0000167 // Store all potential declarations (decls being moved, forward decls) that
168 // might need to move to new.h/cc. It includes all helper declarations
169 // (include unused ones) by default. The unused ones will be filtered out in
170 // the last stage. Saving in an AST-visited order.
Haojian Wu08e402a2016-12-02 12:39:39 +0000171 std::vector<const NamedDecl *> MovedDecls;
Haojian Wu357ef992016-09-21 13:18:19 +0000172 // The declarations that needs to be removed in old.cc/h.
Haojian Wu08e402a2016-12-02 12:39:39 +0000173 std::vector<const NamedDecl *> RemovedDecls;
Haojian Wu357ef992016-09-21 13:18:19 +0000174 // The #includes in old_header.h.
175 std::vector<std::string> HeaderIncludes;
176 // The #includes in old_cc.cc.
177 std::vector<std::string> CCIncludes;
Haojian Wu36265162017-01-03 09:00:51 +0000178 // Records all helper declarations (function/variable/class definitions in
179 // anonymous namespaces, static function/variable definitions in global/named
180 // namespaces) in old.cc. saving in an AST-visited order.
181 std::vector<const NamedDecl *> HelperDeclarations;
Haojian Wu2930be12016-11-08 19:55:13 +0000182 // The unmoved named declarations in old header.
183 llvm::SmallPtrSet<const NamedDecl*, 8> UnremovedDeclsInOldHeader;
184 /// The source range for the written file name in #include (i.e. "old.h" for
185 /// #include "old.h") in old.cc, including the enclosing quotes or angle
186 /// brackets.
Haojian Wufb68ca12018-01-31 12:12:29 +0000187 clang::CharSourceRange OldHeaderIncludeRangeInCC;
188 /// The source range for the written file name in #include (i.e. "old.h" for
189 /// #include "old.h") in old.h, including the enclosing quotes or angle
190 /// brackets.
191 clang::CharSourceRange OldHeaderIncludeRangeInHeader;
Haojian Wu48ac3042016-11-23 10:04:19 +0000192 /// Mapping from FilePath to FileID, which can be used in post processes like
193 /// cleanup around replacements.
194 llvm::StringMap<FileID> FilePathToFileID;
Haojian Wub15c8da2016-11-24 10:17:17 +0000195 /// A context contains all running options. It is not owned.
196 ClangMoveContext *const Context;
197 /// A reporter to report all declarations from old header. It is not owned.
198 DeclarationReporter *const Reporter;
Haojian Wu36265162017-01-03 09:00:51 +0000199 /// Builder for helper declarations reference graph.
200 HelperDeclRGBuilder RGBuilder;
Haojian Wu357ef992016-09-21 13:18:19 +0000201};
202
203class ClangMoveAction : public clang::ASTFrontendAction {
204public:
Haojian Wub15c8da2016-11-24 10:17:17 +0000205 ClangMoveAction(ClangMoveContext *const Context,
206 DeclarationReporter *const Reporter)
207 : MoveTool(Context, Reporter) {
Haojian Wu357ef992016-09-21 13:18:19 +0000208 MoveTool.registerMatchers(&MatchFinder);
209 }
210
211 ~ClangMoveAction() override = default;
212
213 std::unique_ptr<clang::ASTConsumer>
214 CreateASTConsumer(clang::CompilerInstance &Compiler,
215 llvm::StringRef InFile) override;
216
217private:
218 ast_matchers::MatchFinder MatchFinder;
219 ClangMoveTool MoveTool;
220};
221
222class ClangMoveActionFactory : public tooling::FrontendActionFactory {
223public:
Haojian Wub15c8da2016-11-24 10:17:17 +0000224 ClangMoveActionFactory(ClangMoveContext *const Context,
225 DeclarationReporter *const Reporter = nullptr)
226 : Context(Context), Reporter(Reporter) {}
Haojian Wu357ef992016-09-21 13:18:19 +0000227
Roman Lebedev12b40742018-02-27 15:54:41 +0000228 clang::FrontendAction *create() override {
229 return new ClangMoveAction(Context, Reporter);
Haojian Wu357ef992016-09-21 13:18:19 +0000230 }
231
232private:
Haojian Wub15c8da2016-11-24 10:17:17 +0000233 // Not owned.
234 ClangMoveContext *const Context;
235 DeclarationReporter *const Reporter;
Haojian Wu357ef992016-09-21 13:18:19 +0000236};
237
238} // namespace move
239} // namespace clang
240
241#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H