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.getStmtAs<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(!Err); 51 } 52 } 53 54 ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId) 55 : FromId(FromId), ToId(ToId) {} 56 57 void ReplaceStmtWithStmt::run( 58 const ast_matchers::MatchFinder::MatchResult &Result) { 59 const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId); 60 const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId); 61 if (FromMatch && ToMatch) { 62 auto Err = Replace.add( 63 replaceStmtWithStmt(*Result.SourceManager, *FromMatch, *ToMatch)); 64 // FIXME: better error handling. For now, just print error message in the 65 // release version. 66 if (Err) 67 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 68 assert(!Err); 69 } 70 } 71 72 ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id, 73 bool PickTrueBranch) 74 : Id(Id), PickTrueBranch(PickTrueBranch) {} 75 76 void ReplaceIfStmtWithItsBody::run( 77 const ast_matchers::MatchFinder::MatchResult &Result) { 78 if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) { 79 const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse(); 80 if (Body) { 81 auto Err = 82 Replace.add(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body)); 83 // FIXME: better error handling. For now, just print error message in the 84 // release version. 85 if (Err) 86 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 87 assert(!Err); 88 } else if (!PickTrueBranch) { 89 // If we want to use the 'else'-branch, but it doesn't exist, delete 90 // the whole 'if'. 91 auto Err = 92 Replace.add(replaceStmtWithText(*Result.SourceManager, *Node, "")); 93 // FIXME: better error handling. For now, just print error message in the 94 // release version. 95 if (Err) 96 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 97 assert(!Err); 98 } 99 } 100 } 101 102 } // end namespace tooling 103 } // end namespace clang 104