1 //===--- RefactoringCallbacks.h - Structural query framework ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Provides callbacks to make common kinds of refactorings easy.
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.
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));
25 // This will replace all integer literals with "42".
27 //===----------------------------------------------------------------------===//
29 #ifndef LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
30 #define LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H
32 #include "clang/ASTMatchers/ASTMatchFinder.h"
33 #include "clang/Tooling/Refactoring.h"
38 /// \brief Base class for RefactoringCallbacks.
40 /// Collects \c tooling::Replacements while running.
41 class RefactoringCallback : public ast_matchers::MatchFinder::MatchCallback {
43 RefactoringCallback();
44 Replacements &getReplacements();
50 /// \brief Replace the text of the statement bound to \c FromId with the text in
52 class ReplaceStmtWithText : public RefactoringCallback {
54 ReplaceStmtWithText(StringRef FromId, StringRef ToText);
55 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
62 /// \brief Replace the text of the statement bound to \c FromId with the text of
63 /// the statement bound to \c ToId.
64 class ReplaceStmtWithStmt : public RefactoringCallback {
66 ReplaceStmtWithStmt(StringRef FromId, StringRef ToId);
67 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
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.
77 class ReplaceIfStmtWithItsBody : public RefactoringCallback {
79 ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch);
80 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
84 const bool PickTrueBranch;
87 } // end namespace tooling
88 } // end namespace clang