1 //===--- RefactoringCallbacks.cpp - Structural query framework ------------===// 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 // 11 //===----------------------------------------------------------------------===// 12 #include "clang/Lex/Lexer.h" 13 #include "clang/Tooling/RefactoringCallbacks.h" 14 15 namespace clang { 16 namespace tooling { 17 18 RefactoringCallback::RefactoringCallback() {} 19 tooling::Replacements &RefactoringCallback::getReplacements() { 20 return Replace; 21 } 22 23 static Replacement replaceStmtWithText(SourceManager &Sources, 24 const Stmt &From, 25 StringRef Text) { 26 return tooling::Replacement(Sources, CharSourceRange::getTokenRange( 27 From.getSourceRange()), Text); 28 } 29 static Replacement replaceStmtWithStmt(SourceManager &Sources, 30 const Stmt &From, 31 const Stmt &To) { 32 return replaceStmtWithText(Sources, From, Lexer::getSourceText( 33 CharSourceRange::getTokenRange(To.getSourceRange()), 34 Sources, LangOptions())); 35 } 36 37 ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText) 38 : FromId(FromId), ToText(ToText) {} 39 40 void ReplaceStmtWithText::run( 41 const ast_matchers::MatchFinder::MatchResult &Result) { 42 if (const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId)) { 43 auto Err = Replace.add(tooling::Replacement( 44 *Result.SourceManager, 45 CharSourceRange::getTokenRange(FromMatch->getSourceRange()), ToText)); 46 // FIXME: better error handling. For now, just print error message in the 47 // release version. 48 if (Err) { 49 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 50 assert(false); 51 } 52 } 53 } 54 55 ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId) 56 : FromId(FromId), ToId(ToId) {} 57 58 void ReplaceStmtWithStmt::run( 59 const ast_matchers::MatchFinder::MatchResult &Result) { 60 const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId); 61 const Stmt *ToMatch = Result.Nodes.getNodeAs<Stmt>(ToId); 62 if (FromMatch && ToMatch) { 63 auto Err = Replace.add( 64 replaceStmtWithStmt(*Result.SourceManager, *FromMatch, *ToMatch)); 65 // FIXME: better error handling. For now, just print error message in the 66 // release version. 67 if (Err) { 68 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 69 assert(false); 70 } 71 } 72 } 73 74 ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id, 75 bool PickTrueBranch) 76 : Id(Id), PickTrueBranch(PickTrueBranch) {} 77 78 void ReplaceIfStmtWithItsBody::run( 79 const ast_matchers::MatchFinder::MatchResult &Result) { 80 if (const IfStmt *Node = Result.Nodes.getNodeAs<IfStmt>(Id)) { 81 const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse(); 82 if (Body) { 83 auto Err = 84 Replace.add(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body)); 85 // FIXME: better error handling. For now, just print error message in the 86 // release version. 87 if (Err) { 88 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 89 assert(false); 90 } 91 } else if (!PickTrueBranch) { 92 // If we want to use the 'else'-branch, but it doesn't exist, delete 93 // the whole 'if'. 94 auto Err = 95 Replace.add(replaceStmtWithText(*Result.SourceManager, *Node, "")); 96 // FIXME: better error handling. For now, just print error message in the 97 // release version. 98 if (Err) { 99 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 100 assert(false); 101 } 102 } 103 } 104 } 105 106 } // end namespace tooling 107 } // end namespace clang 108