clang API Documentation
00001 //===--- RefactoringCallbacks.h - Structural query framework ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Provides callbacks to make common kinds of refactorings easy. 00011 // 00012 // The general idea is to construct a matcher expression that describes a 00013 // subtree match on the AST and then replace the corresponding source code 00014 // either by some specific text or some other AST node. 00015 // 00016 // Example: 00017 // int main(int argc, char **argv) { 00018 // ClangTool Tool(argc, argv); 00019 // MatchFinder Finder; 00020 // ReplaceStmtWithText Callback("integer", "42"); 00021 // Finder.AddMatcher(id("integer", expression(integerLiteral())), Callback); 00022 // return Tool.run(newFrontendActionFactory(&Finder)); 00023 // } 00024 // 00025 // This will replace all integer literals with "42". 00026 // 00027 //===----------------------------------------------------------------------===// 00028 00029 #ifndef LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H 00030 #define LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H 00031 00032 #include "clang/ASTMatchers/ASTMatchFinder.h" 00033 #include "clang/Tooling/Refactoring.h" 00034 00035 namespace clang { 00036 namespace tooling { 00037 00038 /// \brief Base class for RefactoringCallbacks. 00039 /// 00040 /// Collects \c tooling::Replacements while running. 00041 class RefactoringCallback : public ast_matchers::MatchFinder::MatchCallback { 00042 public: 00043 RefactoringCallback(); 00044 Replacements &getReplacements(); 00045 00046 protected: 00047 Replacements Replace; 00048 }; 00049 00050 /// \brief Replace the text of the statement bound to \c FromId with the text in 00051 /// \c ToText. 00052 class ReplaceStmtWithText : public RefactoringCallback { 00053 public: 00054 ReplaceStmtWithText(StringRef FromId, StringRef ToText); 00055 void run(const ast_matchers::MatchFinder::MatchResult &Result) override; 00056 00057 private: 00058 std::string FromId; 00059 std::string ToText; 00060 }; 00061 00062 /// \brief Replace the text of the statement bound to \c FromId with the text of 00063 /// the statement bound to \c ToId. 00064 class ReplaceStmtWithStmt : public RefactoringCallback { 00065 public: 00066 ReplaceStmtWithStmt(StringRef FromId, StringRef ToId); 00067 void run(const ast_matchers::MatchFinder::MatchResult &Result) override; 00068 00069 private: 00070 std::string FromId; 00071 std::string ToId; 00072 }; 00073 00074 /// \brief Replace an if-statement bound to \c Id with the outdented text of its 00075 /// body, choosing the consequent or the alternative based on whether 00076 /// \c PickTrueBranch is true. 00077 class ReplaceIfStmtWithItsBody : public RefactoringCallback { 00078 public: 00079 ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch); 00080 void run(const ast_matchers::MatchFinder::MatchResult &Result) override; 00081 00082 private: 00083 std::string Id; 00084 const bool PickTrueBranch; 00085 }; 00086 00087 } // end namespace tooling 00088 } // end namespace clang 00089 00090 #endif