clang-tools 19.0.0git
Move.h
Go to the documentation of this file.
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.
30public:
33
34 void reportDeclaration(llvm::StringRef DeclarationName, llvm::StringRef Type,
35 bool Templated) {
36 DeclarationList.emplace_back(DeclarationName, Type, Templated);
37 };
38
39 struct Declaration {
40 Declaration(llvm::StringRef QName, llvm::StringRef Kind, bool Templated)
42
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 {
53 return DeclarationList;
54 }
55
56private:
57 std::vector<Declaration> DeclarationList;
58};
59
60// Specify declarations being moved. It contains all information of the moved
61// declarations.
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.
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.
92 // The name of a predefined code style.
93 std::string FallbackStyle;
94 // Whether dump all declarations in old header.
96};
97
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.
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.
111//
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.
116class ClangMoveTool : public ast_matchers::MatchFinder::MatchCallback {
117public:
118 ClangMoveTool(ClangMoveContext *const Context,
119 DeclarationReporter *const Reporter);
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
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.
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);
142
143 std::vector<const NamedDecl *> &getMovedDecls() { return MovedDecls; }
144
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);
149
150 llvm::SmallPtrSet<const NamedDecl *, 8> &getUnremovedDeclsInOldHeader() {
151 return UnremovedDeclsInOldHeader;
152 }
153
154private:
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
159 void removeDeclsInOldFiles();
160 void moveDeclsToNewFiles();
161 void moveAll(SourceManager& SM, StringRef OldFile, StringRef NewFile);
162
163 // Stores all MatchCallbacks created by this tool.
164 std::vector<std::unique_ptr<ast_matchers::MatchFinder::MatchCallback>>
165 MatchCallbacks;
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
185 /// brackets.
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;
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;
200};
201
202class ClangMoveAction : public clang::ASTFrontendAction {
203public:
205 DeclarationReporter *const Reporter)
206 : MoveTool(Context, Reporter) {
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
216private:
217 ast_matchers::MatchFinder MatchFinder;
218 ClangMoveTool MoveTool;
219};
220
221class ClangMoveActionFactory : public tooling::FrontendActionFactory {
222public:
224 DeclarationReporter *const Reporter = nullptr)
225 : Context(Context), Reporter(Reporter) {}
226
227 std::unique_ptr<clang::FrontendAction> create() override {
228 return std::make_unique<ClangMoveAction>(Context, Reporter);
229 }
230
231private:
232 // Not owned.
233 ClangMoveContext *const Context;
234 DeclarationReporter *const Reporter;
235};
236
237} // namespace move
238} // namespace clang
239
240#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_CLANGMOVE_H
const FunctionDecl * Decl
NodeType Type
bool IsAngled
true if this was an include with angle brackets
StringRef FileName
std::vector< HeaderHandle > Path
ClangMoveActionFactory(ClangMoveContext *const Context, DeclarationReporter *const Reporter=nullptr)
Definition: Move.h:223
std::unique_ptr< clang::FrontendAction > create() override
Definition: Move.h:227
~ClangMoveAction() override=default
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) override
Definition: Move.cpp:478
ClangMoveAction(ClangMoveContext *const Context, DeclarationReporter *const Reporter)
Definition: Move.h:204
void addRemovedDecl(const NamedDecl *Decl)
Add declarations being removed from old.h/cc.
Definition: Move.cpp:492
void addIncludes(llvm::StringRef IncludeHeader, bool IsAngled, llvm::StringRef SearchPath, llvm::StringRef FileName, clang::CharSourceRange IncludeFilenameRange, const SourceManager &SM)
Add #includes from old.h/cc files.
Definition: Move.cpp:690
void onEndOfTranslationUnit() override
Definition: Move.cpp:879
std::vector< const NamedDecl * > & getMovedDecls()
Definition: Move.h:143
void registerMatchers(ast_matchers::MatchFinder *Finder)
Definition: Move.cpp:500
llvm::SmallPtrSet< const NamedDecl *, 8 > & getUnremovedDeclsInOldHeader()
Definition: Move.h:150
void reportDeclaration(llvm::StringRef DeclarationName, llvm::StringRef Type, bool Templated)
Definition: Move.h:34
const std::vector< Declaration > getDeclarationList() const
Definition: Move.h:52
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
-clang-tidy
std::map< std::string, tooling::Replacements > & FileToReplacements
Definition: Move.h:85
std::string FallbackStyle
Definition: Move.h:93
MoveDefinitionSpec Spec
Definition: Move.h:83
std::string OriginalRunningDirectory
Definition: Move.h:91
Declaration(llvm::StringRef QName, llvm::StringRef Kind, bool Templated)
Definition: Move.h:40
friend bool operator==(const Declaration &LHS, const Declaration &RHS)
Definition: Move.h:43
SmallVector< std::string, 4 > Names
Definition: Move.h:64