blob: 8dfccf7aca7041a1f4cd1d9809ec9c6c7d9a3349 [file] [log] [blame]
Haojian Wu357ef992016-09-21 13:18:19 +00001//===-- ClangMove.h - Clang move -----------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Haojian Wu357ef992016-09-21 13:18:19 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H
11
Haojian Wu36265162017-01-03 09:00:51 +000012#include "HelperDeclRefGraph.h"
Haojian Wu357ef992016-09-21 13:18:19 +000013#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Frontend/FrontendAction.h"
15#include "clang/Tooling/Core/Replacement.h"
16#include "clang/Tooling/Tooling.h"
Haojian Wu2930be12016-11-08 19:55:13 +000017#include "llvm/ADT/SmallPtrSet.h"
Haojian Wu48ac3042016-11-23 10:04:19 +000018#include "llvm/ADT/StringMap.h"
Eric Liubce181c2018-10-08 17:22:50 +000019#include "llvm/ADT/StringRef.h"
Haojian Wu357ef992016-09-21 13:18:19 +000020#include <map>
Haojian Wu35ca9462016-11-14 14:15:44 +000021#include <memory>
Haojian Wu357ef992016-09-21 13:18:19 +000022#include <string>
23#include <vector>
24
25namespace clang {
26namespace move {
27
Haojian Wub15c8da2016-11-24 10:17:17 +000028// A reporter which collects and reports declarations in old header.
29class DeclarationReporter {
30public:
31 DeclarationReporter() = default;
32 ~DeclarationReporter() = default;
33
Eric Liubce181c2018-10-08 17:22:50 +000034 void reportDeclaration(llvm::StringRef DeclarationName, llvm::StringRef Type,
35 bool Templated) {
36 DeclarationList.emplace_back(DeclarationName, Type, Templated);
Haojian Wub15c8da2016-11-24 10:17:17 +000037 };
38
Eric Liubce181c2018-10-08 17:22:50 +000039 struct Declaration {
40 Declaration(llvm::StringRef QName, llvm::StringRef Kind, bool Templated)
41 : QualifiedName(QName), Kind(Kind), Templated(Templated) {}
Haojian Wub15c8da2016-11-24 10:17:17 +000042
Eric Liubce181c2018-10-08 17:22:50 +000043 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);
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 const std::vector<Declaration> getDeclarationList() const {
Haojian Wub15c8da2016-11-24 10:17:17 +000053 return DeclarationList;
54 }
55
56private:
Eric Liubce181c2018-10-08 17:22:50 +000057 std::vector<Declaration> DeclarationList;
Haojian Wub15c8da2016-11-24 10:17:17 +000058};
59
60// Specify declarations being moved. It contains all information of the moved
61// declarations.
62struct 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.
68 std::string OldCC;
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.
72 std::string NewCC;
73 // Whether old.h depends on new.h. If true, #include "new.h" will be added
74 // in old.h.
75 bool OldDependOnNew = false;
76 // Whether new.h depends on old.h. If true, #include "old.h" will be added
77 // in new.h.
78 bool NewDependOnOld = false;
79};
80
81// A Context which contains extra options which are used in ClangMoveTool.
82struct 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.
87 //
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;
96};
97
98// This tool is used to move class/function definitions from the given source
Haojian Wu36265162017-01-03 09:00:51 +000099// 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.
101//
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.
108//
109// The remaining helper declarations which are unused by non-moved symbols in
110// old.cc will be removed.
Haojian Wu2930be12016-11-08 19:55:13 +0000111//
Haojian Wub15c8da2016-11-24 10:17:17 +0000112// Note: When all declarations in old header are being moved, all code in
Eric Liu47a42d52016-12-06 10:12:23 +0000113// 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.
Haojian Wu357ef992016-09-21 13:18:19 +0000116class ClangMoveTool : public ast_matchers::MatchFinder::MatchCallback {
117public:
Haojian Wub15c8da2016-11-24 10:17:17 +0000118 ClangMoveTool(ClangMoveContext *const Context,
119 DeclarationReporter *const Reporter);
Haojian Wu357ef992016-09-21 13:18:19 +0000120
121 void registerMatchers(ast_matchers::MatchFinder *Finder);
122
123 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
124
125 void onEndOfTranslationUnit() override;
126
Haojian Wud2a6d7b2016-10-04 09:05:31 +0000127 /// Add #includes from old.h/cc files.
128 ///
129 /// \param IncludeHeader The name of the file being included, as written in
130 /// the source code.
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.
Haojian Wub15c8da2016-11-24 10:17:17 +0000135 /// \param IncludeFilenameRange The source range for the written file name in
136 /// #include (i.e. "old.h" for #include "old.h") in old.cc.
Haojian Wuef445b72016-10-04 10:40:52 +0000137 /// \param SM The SourceManager.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000138 void addIncludes(llvm::StringRef IncludeHeader, bool IsAngled,
139 llvm::StringRef SearchPath, llvm::StringRef FileName,
Haojian Wu2930be12016-11-08 19:55:13 +0000140 clang::CharSourceRange IncludeFilenameRange,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000141 const SourceManager &SM);
Haojian Wu357ef992016-09-21 13:18:19 +0000142
Haojian Wu08e402a2016-12-02 12:39:39 +0000143 std::vector<const NamedDecl *> &getMovedDecls() { return MovedDecls; }
Haojian Wu35ca9462016-11-14 14:15:44 +0000144
Haojian Wu48ac3042016-11-23 10:04:19 +0000145 /// 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.
Haojian Wu08e402a2016-12-02 12:39:39 +0000148 void addRemovedDecl(const NamedDecl *Decl);
Haojian Wu35ca9462016-11-14 14:15:44 +0000149
150 llvm::SmallPtrSet<const NamedDecl *, 8> &getUnremovedDeclsInOldHeader() {
151 return UnremovedDeclsInOldHeader;
152 }
153
Haojian Wu357ef992016-09-21 13:18:19 +0000154private:
Haojian Wu2930be12016-11-08 19:55:13 +0000155 // 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);
158
Haojian Wu08e402a2016-12-02 12:39:39 +0000159 void removeDeclsInOldFiles();
160 void moveDeclsToNewFiles();
Haojian Wu2930be12016-11-08 19:55:13 +0000161 void moveAll(SourceManager& SM, StringRef OldFile, StringRef NewFile);
Haojian Wu357ef992016-09-21 13:18:19 +0000162
Haojian Wu35ca9462016-11-14 14:15:44 +0000163 // Stores all MatchCallbacks created by this tool.
164 std::vector<std::unique_ptr<ast_matchers::MatchFinder::MatchCallback>>
165 MatchCallbacks;
Haojian Wu36265162017-01-03 09:00:51 +0000166 // 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.
Haojian Wu08e402a2016-12-02 12:39:39 +0000170 std::vector<const NamedDecl *> MovedDecls;
Haojian Wu357ef992016-09-21 13:18:19 +0000171 // The declarations that needs to be removed in old.cc/h.
Haojian Wu08e402a2016-12-02 12:39:39 +0000172 std::vector<const NamedDecl *> RemovedDecls;
Haojian Wu357ef992016-09-21 13:18:19 +0000173 // 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;
Haojian Wu36265162017-01-03 09:00:51 +0000177 // 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;
Haojian Wu2930be12016-11-08 19:55:13 +0000181 // 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
185 /// brackets.
Haojian Wufb68ca12018-01-31 12:12:29 +0000186 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
189 /// brackets.
190 clang::CharSourceRange OldHeaderIncludeRangeInHeader;
Haojian Wu48ac3042016-11-23 10:04:19 +0000191 /// Mapping from FilePath to FileID, which can be used in post processes like
192 /// cleanup around replacements.
193 llvm::StringMap<FileID> FilePathToFileID;
Haojian Wub15c8da2016-11-24 10:17:17 +0000194 /// 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;
Haojian Wu36265162017-01-03 09:00:51 +0000198 /// Builder for helper declarations reference graph.
199 HelperDeclRGBuilder RGBuilder;
Haojian Wu357ef992016-09-21 13:18:19 +0000200};
201
202class ClangMoveAction : public clang::ASTFrontendAction {
203public:
Haojian Wub15c8da2016-11-24 10:17:17 +0000204 ClangMoveAction(ClangMoveContext *const Context,
205 DeclarationReporter *const Reporter)
206 : MoveTool(Context, Reporter) {
Haojian Wu357ef992016-09-21 13:18:19 +0000207 MoveTool.registerMatchers(&MatchFinder);
208 }
209
210 ~ClangMoveAction() override = default;
211
212 std::unique_ptr<clang::ASTConsumer>
213 CreateASTConsumer(clang::CompilerInstance &Compiler,
214 llvm::StringRef InFile) override;
215
216private:
217 ast_matchers::MatchFinder MatchFinder;
218 ClangMoveTool MoveTool;
219};
220
221class ClangMoveActionFactory : public tooling::FrontendActionFactory {
222public:
Haojian Wub15c8da2016-11-24 10:17:17 +0000223 ClangMoveActionFactory(ClangMoveContext *const Context,
224 DeclarationReporter *const Reporter = nullptr)
225 : Context(Context), Reporter(Reporter) {}
Haojian Wu357ef992016-09-21 13:18:19 +0000226
Roman Lebedev12b40742018-02-27 15:54:41 +0000227 clang::FrontendAction *create() override {
228 return new ClangMoveAction(Context, Reporter);
Haojian Wu357ef992016-09-21 13:18:19 +0000229 }
230
231private:
Haojian Wub15c8da2016-11-24 10:17:17 +0000232 // Not owned.
233 ClangMoveContext *const Context;
234 DeclarationReporter *const Reporter;
Haojian Wu357ef992016-09-21 13:18:19 +0000235};
236
237} // namespace move
238} // namespace clang
239
240#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H