Nico Weber | 71ebc9e | 2019-03-22 16:34:39 +0000 | [diff] [blame^] | 1 | //===-- Move.h - Clang move ----------------------------------------------===// |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H |
| 11 | |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 12 | #include "HelperDeclRefGraph.h" |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 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" |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 20 | #include <map> |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 21 | #include <memory> |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace move { |
| 27 | |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 28 | // A reporter which collects and reports declarations in old header. |
| 29 | class DeclarationReporter { |
| 30 | public: |
| 31 | DeclarationReporter() = default; |
| 32 | ~DeclarationReporter() = default; |
| 33 | |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 34 | void reportDeclaration(llvm::StringRef DeclarationName, llvm::StringRef Type, |
| 35 | bool Templated) { |
| 36 | DeclarationList.emplace_back(DeclarationName, Type, Templated); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 39 | struct Declaration { |
| 40 | Declaration(llvm::StringRef QName, llvm::StringRef Kind, bool Templated) |
| 41 | : QualifiedName(QName), Kind(Kind), Templated(Templated) {} |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 42 | |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 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); |
| 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 Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 53 | return DeclarationList; |
| 54 | } |
| 55 | |
| 56 | private: |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 57 | std::vector<Declaration> DeclarationList; |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // Specify declarations being moved. It contains all information of the moved |
| 61 | // declarations. |
| 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. |
| 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. |
| 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. |
| 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 Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 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. |
| 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 Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 111 | // |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 112 | // Note: When all declarations in old header are being moved, all code in |
Eric Liu | 47a42d5 | 2016-12-06 10:12:23 +0000 | [diff] [blame] | 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. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 116 | class ClangMoveTool : public ast_matchers::MatchFinder::MatchCallback { |
| 117 | public: |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 118 | ClangMoveTool(ClangMoveContext *const Context, |
| 119 | DeclarationReporter *const Reporter); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 120 | |
| 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 Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 127 | /// 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 Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 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. |
Haojian Wu | ef445b7 | 2016-10-04 10:40:52 +0000 | [diff] [blame] | 137 | /// \param SM The SourceManager. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 138 | void addIncludes(llvm::StringRef IncludeHeader, bool IsAngled, |
| 139 | llvm::StringRef SearchPath, llvm::StringRef FileName, |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 140 | clang::CharSourceRange IncludeFilenameRange, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 141 | const SourceManager &SM); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 142 | |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 143 | std::vector<const NamedDecl *> &getMovedDecls() { return MovedDecls; } |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 144 | |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 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. |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 148 | void addRemovedDecl(const NamedDecl *Decl); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 149 | |
| 150 | llvm::SmallPtrSet<const NamedDecl *, 8> &getUnremovedDeclsInOldHeader() { |
| 151 | return UnremovedDeclsInOldHeader; |
| 152 | } |
| 153 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 154 | private: |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 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); |
| 158 | |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 159 | void removeDeclsInOldFiles(); |
| 160 | void moveDeclsToNewFiles(); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 161 | void moveAll(SourceManager& SM, StringRef OldFile, StringRef NewFile); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 162 | |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 163 | // Stores all MatchCallbacks created by this tool. |
| 164 | std::vector<std::unique_ptr<ast_matchers::MatchFinder::MatchCallback>> |
| 165 | MatchCallbacks; |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 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. |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 170 | std::vector<const NamedDecl *> MovedDecls; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 171 | // The declarations that needs to be removed in old.cc/h. |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 172 | std::vector<const NamedDecl *> RemovedDecls; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 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; |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 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; |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 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 |
| 185 | /// brackets. |
Haojian Wu | fb68ca1 | 2018-01-31 12:12:29 +0000 | [diff] [blame] | 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 |
| 189 | /// brackets. |
| 190 | clang::CharSourceRange OldHeaderIncludeRangeInHeader; |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 191 | /// Mapping from FilePath to FileID, which can be used in post processes like |
| 192 | /// cleanup around replacements. |
| 193 | llvm::StringMap<FileID> FilePathToFileID; |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 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; |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 198 | /// Builder for helper declarations reference graph. |
| 199 | HelperDeclRGBuilder RGBuilder; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | class ClangMoveAction : public clang::ASTFrontendAction { |
| 203 | public: |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 204 | ClangMoveAction(ClangMoveContext *const Context, |
| 205 | DeclarationReporter *const Reporter) |
| 206 | : MoveTool(Context, Reporter) { |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 207 | 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 | |
| 216 | private: |
| 217 | ast_matchers::MatchFinder MatchFinder; |
| 218 | ClangMoveTool MoveTool; |
| 219 | }; |
| 220 | |
| 221 | class ClangMoveActionFactory : public tooling::FrontendActionFactory { |
| 222 | public: |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 223 | ClangMoveActionFactory(ClangMoveContext *const Context, |
| 224 | DeclarationReporter *const Reporter = nullptr) |
| 225 | : Context(Context), Reporter(Reporter) {} |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 226 | |
Roman Lebedev | 12b4074 | 2018-02-27 15:54:41 +0000 | [diff] [blame] | 227 | clang::FrontendAction *create() override { |
| 228 | return new ClangMoveAction(Context, Reporter); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | private: |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 232 | // Not owned. |
| 233 | ClangMoveContext *const Context; |
| 234 | DeclarationReporter *const Reporter; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | } // namespace move |
| 238 | } // namespace clang |
| 239 | |
| 240 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H |