clang  13.0.0git
RefactoringCallbacks.h
Go to the documentation of this file.
1 //===--- RefactoringCallbacks.h - Structural query framework ----*- C++ -*-===//
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 // Provides callbacks to make common kinds of refactorings easy.
10 //
11 // The general idea is to construct a matcher expression that describes a
12 // subtree match on the AST and then replace the corresponding source code
13 // either by some specific text or some other AST node.
14 //
15 // Example:
16 // int main(int argc, char **argv) {
17 // ClangTool Tool(argc, argv);
18 // MatchFinder Finder;
19 // ReplaceStmtWithText Callback("integer", "42");
20 // Finder.AddMatcher(id("integer", expression(integerLiteral())), Callback);
21 // return Tool.run(newFrontendActionFactory(&Finder));
22 // }
23 //
24 // This will replace all integer literals with "42".
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #ifndef LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
29 #define LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
30 
33 
34 namespace clang {
35 namespace tooling {
36 
37 /// Base class for RefactoringCallbacks.
38 ///
39 /// Collects \c tooling::Replacements while running.
41 public:
44 
45 protected:
47 };
48 
49 /// Adaptor between \c ast_matchers::MatchFinder and \c
50 /// tooling::RefactoringTool.
51 ///
52 /// Runs AST matchers and stores the \c tooling::Replacements in a map.
54 public:
55  explicit ASTMatchRefactorer(
56  std::map<std::string, Replacements> &FileToReplaces);
57 
58  template <typename T>
59  void addMatcher(const T &Matcher, RefactoringCallback *Callback) {
60  MatchFinder.addMatcher(Matcher, Callback);
61  Callbacks.push_back(Callback);
62  }
63 
64  void addDynamicMatcher(const ast_matchers::internal::DynTypedMatcher &Matcher,
65  RefactoringCallback *Callback);
66 
67  std::unique_ptr<ASTConsumer> newASTConsumer();
68 
69 private:
70  friend class RefactoringASTConsumer;
71  std::vector<RefactoringCallback *> Callbacks;
73  std::map<std::string, Replacements> &FileToReplaces;
74 };
75 
76 /// Replace the text of the statement bound to \c FromId with the text in
77 /// \c ToText.
79 public:
80  ReplaceStmtWithText(StringRef FromId, StringRef ToText);
81  void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
82 
83 private:
84  std::string FromId;
85  std::string ToText;
86 };
87 
88 /// Replace the text of an AST node bound to \c FromId with the result of
89 /// evaluating the template in \c ToTemplate.
90 ///
91 /// Expressions of the form ${NodeName} in \c ToTemplate will be
92 /// replaced by the text of the node bound to ${NodeName}. The string
93 /// "$$" will be replaced by "$".
95 public:
97  create(StringRef FromId, StringRef ToTemplate);
98  void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
99 
100 private:
101  struct TemplateElement {
102  enum { Literal, Identifier } Type;
104  };
105  ReplaceNodeWithTemplate(llvm::StringRef FromId,
106  std::vector<TemplateElement> Template);
107  std::string FromId;
108  std::vector<TemplateElement> Template;
109 };
110 
111 /// Replace the text of the statement bound to \c FromId with the text of
112 /// the statement bound to \c ToId.
114 public:
115  ReplaceStmtWithStmt(StringRef FromId, StringRef ToId);
116  void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
117 
118 private:
119  std::string FromId;
120  std::string ToId;
121 };
122 
123 /// Replace an if-statement bound to \c Id with the outdented text of its
124 /// body, choosing the consequent or the alternative based on whether
125 /// \c PickTrueBranch is true.
127 public:
128  ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch);
129  void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
130 
131 private:
132  std::string Id;
133  const bool PickTrueBranch;
134 };
135 
136 } // end namespace tooling
137 } // end namespace clang
138 
139 #endif
clang::tooling::ReplaceStmtWithStmt::ReplaceStmtWithStmt
ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
Definition: RefactoringCallbacks.cpp:103
clang::tooling::RefactoringCallback::RefactoringCallback
RefactoringCallback()
Definition: RefactoringCallbacks.cpp:22
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ast_matchers::MatchFinder
A class to allow finding matches over the Clang AST.
Definition: ASTMatchFinder.h:68
clang::tooling::Replacements
Maintains a set of replacements that are conflict-free.
Definition: Replacement.h:209
clang::tooling::RefactoringASTConsumer
Definition: RefactoringCallbacks.cpp:38
llvm::Expected
Definition: LLVM.h:41
ASTMatchFinder.h
Identifier
StringRef Identifier
Definition: Format.cpp:2170
clang::tooling::ASTMatchRefactorer::addMatcher
void addMatcher(const T &Matcher, RefactoringCallback *Callback)
Definition: RefactoringCallbacks.h:59
clang::tooling::ReplaceStmtWithText
Replace the text of the statement bound to FromId with the text in ToText.
Definition: RefactoringCallbacks.h:78
clang::tooling::ReplaceIfStmtWithItsBody
Replace an if-statement bound to Id with the outdented text of its body, choosing the consequent or t...
Definition: RefactoringCallbacks.h:126
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1478
clang::tooling::ReplaceIfStmtWithItsBody::run
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
Definition: RefactoringCallbacks.cpp:126
clang::tooling::ReplaceStmtWithStmt
Replace the text of the statement bound to FromId with the text of the statement bound to ToId.
Definition: RefactoringCallbacks.h:113
clang::tooling::ASTMatchRefactorer
Adaptor between ast_matchers::MatchFinder and tooling::RefactoringTool.
Definition: RefactoringCallbacks.h:53
Id
int Id
Definition: ASTDiff.cpp:191
clang::tooling::ReplaceStmtWithText::ReplaceStmtWithText
ReplaceStmtWithText(StringRef FromId, StringRef ToText)
Definition: RefactoringCallbacks.cpp:85
Refactoring.h
clang::tooling::ReplaceStmtWithText::run
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
Definition: RefactoringCallbacks.cpp:88
clang::tooling::ASTMatchRefactorer::newASTConsumer
std::unique_ptr< ASTConsumer > newASTConsumer()
Definition: RefactoringCallbacks.cpp:68
clang::tooling::ASTMatchRefactorer::addDynamicMatcher
void addDynamicMatcher(const ast_matchers::internal::DynTypedMatcher &Matcher, RefactoringCallback *Callback)
Definition: RefactoringCallbacks.cpp:31
clang::ast_matchers::MatchFinder::addMatcher
void addMatcher(const DeclarationMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
Definition: ASTMatchFinder.cpp:1337
Value
Value
Definition: UninitializedValues.cpp:102
clang::tooling::ReplaceNodeWithTemplate
Replace the text of an AST node bound to FromId with the result of evaluating the template in ToTempl...
Definition: RefactoringCallbacks.h:94
clang::ast_matchers::MatchFinder::MatchResult
Contains all information for a given match.
Definition: ASTMatchFinder.h:74
clang::tooling::ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody
ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch)
Definition: RefactoringCallbacks.cpp:122
clang::tooling::RefactoringCallback
Base class for RefactoringCallbacks.
Definition: RefactoringCallbacks.h:40
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::tooling::ReplaceStmtWithStmt::run
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
Definition: RefactoringCallbacks.cpp:106
clang::tooling::ASTMatchRefactorer::ASTMatchRefactorer
ASTMatchRefactorer(std::map< std::string, Replacements > &FileToReplaces)
Definition: RefactoringCallbacks.cpp:27
clang::tooling::ReplaceNodeWithTemplate::create
static llvm::Expected< std::unique_ptr< ReplaceNodeWithTemplate > > create(StringRef FromId, StringRef ToTemplate)
Definition: RefactoringCallbacks.cpp:159
clang::tooling::RefactoringCallback::Replace
Replacements Replace
Definition: RefactoringCallbacks.h:46
clang::tooling::RefactoringCallback::getReplacements
Replacements & getReplacements()
Definition: RefactoringCallbacks.cpp:23
clang::tooling::ReplaceNodeWithTemplate::run
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
Definition: RefactoringCallbacks.cpp:198
clang::ast_matchers::MatchFinder::MatchCallback
Called when the Match registered for it was successfully found in the AST.
Definition: ASTMatchFinder.h:91