1139969Simp//===--- RefactoringCallbacks.h - Structural query framework ----*- C++ -*-===// 21556Srgrimes// 31556Srgrimes// The LLVM Compiler Infrastructure 41556Srgrimes// 51556Srgrimes// This file is distributed under the University of Illinois Open Source 61556Srgrimes// License. See LICENSE.TXT for details. 71556Srgrimes// 81556Srgrimes//===----------------------------------------------------------------------===// 91556Srgrimes// 101556Srgrimes// Provides callbacks to make common kinds of refactorings easy. 111556Srgrimes// 121556Srgrimes// The general idea is to construct a matcher expression that describes a 131556Srgrimes// subtree match on the AST and then replace the corresponding source code 141556Srgrimes// either by some specific text or some other AST node. 151556Srgrimes// 161556Srgrimes// Example: 171556Srgrimes// int main(int argc, char **argv) { 181556Srgrimes// ClangTool Tool(argc, argv); 191556Srgrimes// MatchFinder Finder; 201556Srgrimes// ReplaceStmtWithText Callback("integer", "42"); 211556Srgrimes// Finder.AddMatcher(id("integer", expression(integerLiteral())), Callback); 221556Srgrimes// return Tool.run(newFrontendActionFactory(&Finder)); 231556Srgrimes// } 241556Srgrimes// 251556Srgrimes// This will replace all integer literals with "42". 261556Srgrimes// 271556Srgrimes//===----------------------------------------------------------------------===// 281556Srgrimes 291556Srgrimes#ifndef LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H 301556Srgrimes#define LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H 3120413Ssteve 321556Srgrimes#include "clang/ASTMatchers/ASTMatchFinder.h" 331556Srgrimes#include "clang/Tooling/Refactoring.h" 341556Srgrimes 351556Srgrimesnamespace clang { 36110390Scharniernamespace tooling { 371556Srgrimes 3836006Scharnier/// \brief Base class for RefactoringCallbacks. 39110390Scharnier/// 4035773Scharnier/// Collects \c tooling::Replacements while running. 41110390Scharnierclass RefactoringCallback : public ast_matchers::MatchFinder::MatchCallback { 4299109Sobrienpublic: 4399109Sobrien RefactoringCallback(); 441556Srgrimes Replacements &getReplacements(); 4536006Scharnier 461556Srgrimesprotected: 47283258Sdelphij Replacements Replace; 481556Srgrimes}; 491556Srgrimes 501556Srgrimes/// \brief Replace the text of the statement bound to \c FromId with the text in 5191079Smarkm/// \c ToText. 521556Srgrimesclass ReplaceStmtWithText : public RefactoringCallback { 531556Srgrimespublic: 5439925Salex ReplaceStmtWithText(StringRef FromId, StringRef ToText); 551556Srgrimes virtual void run(const ast_matchers::MatchFinder::MatchResult &Result); 561556Srgrimes 57202193Sedprivate: 581556Srgrimes std::string FromId; 591556Srgrimes std::string ToText; 6027874Sbrian}; 611556Srgrimes 6255225Ssheldonh/// \brief Replace the text of the statement bound to \c FromId with the text of 6355225Ssheldonh/// the statement bound to \c ToId. 6455225Ssheldonhclass ReplaceStmtWithStmt : public RefactoringCallback { 6555225Ssheldonhpublic: 66105396Smarkm ReplaceStmtWithStmt(StringRef FromId, StringRef ToId); 6747129Sjmg virtual void run(const ast_matchers::MatchFinder::MatchResult &Result); 681556Srgrimes 6990108Simpprivate: 7090108Simp std::string FromId; 7190108Simp std::string ToId; 721556Srgrimes}; 73265265Sdumbbell 74265265Sdumbbell/// \brief Replace an if-statement bound to \c Id with the outdented text of its 751556Srgrimes/// body, choosing the consequent or the alternative based on whether 7690108Simp/// \c PickTrueBranch is true. 771556Srgrimesclass ReplaceIfStmtWithItsBody : public RefactoringCallback { 781556Srgrimespublic: 7930073Sdanny ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch); 80265265Sdumbbell virtual void run(const ast_matchers::MatchFinder::MatchResult &Result); 8191079Smarkm 8291079Smarkmprivate: 8328037Sbrian std::string Id; 8485615Sdillon const bool PickTrueBranch; 855233Sbde}; 8627874Sbrian 8727874Sbrian} // end namespace tooling 88323291Smarius} // end namespace clang 89283258Sdelphij 901556Srgrimes#endif // LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H 9127874Sbrian