clang  3.7.0
RefactoringCallbacks.h
Go to the documentation of this file.
1 //===--- RefactoringCallbacks.h - Structural query framework ----*- C++ -*-===//
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 // Provides callbacks to make common kinds of refactorings easy.
11 //
12 // The general idea is to construct a matcher expression that describes a
13 // subtree match on the AST and then replace the corresponding source code
14 // either by some specific text or some other AST node.
15 //
16 // Example:
17 // int main(int argc, char **argv) {
18 // ClangTool Tool(argc, argv);
19 // MatchFinder Finder;
20 // ReplaceStmtWithText Callback("integer", "42");
21 // Finder.AddMatcher(id("integer", expression(integerLiteral())), Callback);
22 // return Tool.run(newFrontendActionFactory(&Finder));
23 // }
24 //
25 // This will replace all integer literals with "42".
26 //
27 //===----------------------------------------------------------------------===//
28 
29 #ifndef LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
30 #define LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
31 
34 
35 namespace clang {
36 namespace tooling {
37 
38 /// \brief Base class for RefactoringCallbacks.
39 ///
40 /// Collects \c tooling::Replacements while running.
42 public:
45 
46 protected:
48 };
49 
50 /// \brief Replace the text of the statement bound to \c FromId with the text in
51 /// \c ToText.
53 public:
54  ReplaceStmtWithText(StringRef FromId, StringRef ToText);
56 
57 private:
58  std::string FromId;
59  std::string ToText;
60 };
61 
62 /// \brief Replace the text of the statement bound to \c FromId with the text of
63 /// the statement bound to \c ToId.
65 public:
66  ReplaceStmtWithStmt(StringRef FromId, StringRef ToId);
68 
69 private:
70  std::string FromId;
71  std::string ToId;
72 };
73 
74 /// \brief Replace an if-statement bound to \c Id with the outdented text of its
75 /// body, choosing the consequent or the alternative based on whether
76 /// \c PickTrueBranch is true.
78 public:
79  ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch);
81 
82 private:
83  std::string Id;
84  const bool PickTrueBranch;
85 };
86 
87 } // end namespace tooling
88 } // end namespace clang
89 
90 #endif
Replace an if-statement bound to Id with the outdented text of its body, choosing the consequent or t...
std::set< Replacement > Replacements
A set of Replacements. FIXME: Change to a vector and deduplicate in the RefactoringTool.
Definition: Replacement.h:141
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch)
Base class for RefactoringCallbacks.
Contains all information for a given match.
The result type of a method or function.
ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
ReplaceStmtWithText(StringRef FromId, StringRef ToText)
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
Replace the text of the statement bound to FromId with the text in ToText.
Replace the text of the statement bound to FromId with the text of the statement bound to ToId...
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
Called when the Match registered for it was successfully found in the AST.