clang API Documentation
00001 //===--- RefactoringCallbacks.cpp - Structural query framework ------------===// 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 // 00011 //===----------------------------------------------------------------------===// 00012 #include "clang/Lex/Lexer.h" 00013 #include "clang/Tooling/RefactoringCallbacks.h" 00014 00015 namespace clang { 00016 namespace tooling { 00017 00018 RefactoringCallback::RefactoringCallback() {} 00019 tooling::Replacements &RefactoringCallback::getReplacements() { 00020 return Replace; 00021 } 00022 00023 static Replacement replaceStmtWithText(SourceManager &Sources, 00024 const Stmt &From, 00025 StringRef Text) { 00026 return tooling::Replacement(Sources, CharSourceRange::getTokenRange( 00027 From.getSourceRange()), Text); 00028 } 00029 static Replacement replaceStmtWithStmt(SourceManager &Sources, 00030 const Stmt &From, 00031 const Stmt &To) { 00032 return replaceStmtWithText(Sources, From, Lexer::getSourceText( 00033 CharSourceRange::getTokenRange(To.getSourceRange()), 00034 Sources, LangOptions())); 00035 } 00036 00037 ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText) 00038 : FromId(FromId), ToText(ToText) {} 00039 00040 void ReplaceStmtWithText::run( 00041 const ast_matchers::MatchFinder::MatchResult &Result) { 00042 if (const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId)) { 00043 Replace.insert(tooling::Replacement( 00044 *Result.SourceManager, 00045 CharSourceRange::getTokenRange(FromMatch->getSourceRange()), 00046 ToText)); 00047 } 00048 } 00049 00050 ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId) 00051 : FromId(FromId), ToId(ToId) {} 00052 00053 void ReplaceStmtWithStmt::run( 00054 const ast_matchers::MatchFinder::MatchResult &Result) { 00055 const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId); 00056 const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId); 00057 if (FromMatch && ToMatch) 00058 Replace.insert(replaceStmtWithStmt( 00059 *Result.SourceManager, *FromMatch, *ToMatch)); 00060 } 00061 00062 ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id, 00063 bool PickTrueBranch) 00064 : Id(Id), PickTrueBranch(PickTrueBranch) {} 00065 00066 void ReplaceIfStmtWithItsBody::run( 00067 const ast_matchers::MatchFinder::MatchResult &Result) { 00068 if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) { 00069 const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse(); 00070 if (Body) { 00071 Replace.insert(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body)); 00072 } else if (!PickTrueBranch) { 00073 // If we want to use the 'else'-branch, but it doesn't exist, delete 00074 // the whole 'if'. 00075 Replace.insert(replaceStmtWithText(*Result.SourceManager, *Node, "")); 00076 } 00077 } 00078 } 00079 00080 } // end namespace tooling 00081 } // end namespace clang