1*f4a2713aSLionel Sambuc //===--- RefactoringCallbacks.cpp - Structural query framework ------------===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc //
10*f4a2713aSLionel Sambuc //
11*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
12*f4a2713aSLionel Sambuc #include "clang/Lex/Lexer.h"
13*f4a2713aSLionel Sambuc #include "clang/Tooling/RefactoringCallbacks.h"
14*f4a2713aSLionel Sambuc
15*f4a2713aSLionel Sambuc namespace clang {
16*f4a2713aSLionel Sambuc namespace tooling {
17*f4a2713aSLionel Sambuc
RefactoringCallback()18*f4a2713aSLionel Sambuc RefactoringCallback::RefactoringCallback() {}
getReplacements()19*f4a2713aSLionel Sambuc tooling::Replacements &RefactoringCallback::getReplacements() {
20*f4a2713aSLionel Sambuc return Replace;
21*f4a2713aSLionel Sambuc }
22*f4a2713aSLionel Sambuc
replaceStmtWithText(SourceManager & Sources,const Stmt & From,StringRef Text)23*f4a2713aSLionel Sambuc static Replacement replaceStmtWithText(SourceManager &Sources,
24*f4a2713aSLionel Sambuc const Stmt &From,
25*f4a2713aSLionel Sambuc StringRef Text) {
26*f4a2713aSLionel Sambuc return tooling::Replacement(Sources, CharSourceRange::getTokenRange(
27*f4a2713aSLionel Sambuc From.getSourceRange()), Text);
28*f4a2713aSLionel Sambuc }
replaceStmtWithStmt(SourceManager & Sources,const Stmt & From,const Stmt & To)29*f4a2713aSLionel Sambuc static Replacement replaceStmtWithStmt(SourceManager &Sources,
30*f4a2713aSLionel Sambuc const Stmt &From,
31*f4a2713aSLionel Sambuc const Stmt &To) {
32*f4a2713aSLionel Sambuc return replaceStmtWithText(Sources, From, Lexer::getSourceText(
33*f4a2713aSLionel Sambuc CharSourceRange::getTokenRange(To.getSourceRange()),
34*f4a2713aSLionel Sambuc Sources, LangOptions()));
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc
ReplaceStmtWithText(StringRef FromId,StringRef ToText)37*f4a2713aSLionel Sambuc ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText)
38*f4a2713aSLionel Sambuc : FromId(FromId), ToText(ToText) {}
39*f4a2713aSLionel Sambuc
run(const ast_matchers::MatchFinder::MatchResult & Result)40*f4a2713aSLionel Sambuc void ReplaceStmtWithText::run(
41*f4a2713aSLionel Sambuc const ast_matchers::MatchFinder::MatchResult &Result) {
42*f4a2713aSLionel Sambuc if (const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId)) {
43*f4a2713aSLionel Sambuc Replace.insert(tooling::Replacement(
44*f4a2713aSLionel Sambuc *Result.SourceManager,
45*f4a2713aSLionel Sambuc CharSourceRange::getTokenRange(FromMatch->getSourceRange()),
46*f4a2713aSLionel Sambuc ToText));
47*f4a2713aSLionel Sambuc }
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc
ReplaceStmtWithStmt(StringRef FromId,StringRef ToId)50*f4a2713aSLionel Sambuc ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
51*f4a2713aSLionel Sambuc : FromId(FromId), ToId(ToId) {}
52*f4a2713aSLionel Sambuc
run(const ast_matchers::MatchFinder::MatchResult & Result)53*f4a2713aSLionel Sambuc void ReplaceStmtWithStmt::run(
54*f4a2713aSLionel Sambuc const ast_matchers::MatchFinder::MatchResult &Result) {
55*f4a2713aSLionel Sambuc const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId);
56*f4a2713aSLionel Sambuc const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId);
57*f4a2713aSLionel Sambuc if (FromMatch && ToMatch)
58*f4a2713aSLionel Sambuc Replace.insert(replaceStmtWithStmt(
59*f4a2713aSLionel Sambuc *Result.SourceManager, *FromMatch, *ToMatch));
60*f4a2713aSLionel Sambuc }
61*f4a2713aSLionel Sambuc
ReplaceIfStmtWithItsBody(StringRef Id,bool PickTrueBranch)62*f4a2713aSLionel Sambuc ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id,
63*f4a2713aSLionel Sambuc bool PickTrueBranch)
64*f4a2713aSLionel Sambuc : Id(Id), PickTrueBranch(PickTrueBranch) {}
65*f4a2713aSLionel Sambuc
run(const ast_matchers::MatchFinder::MatchResult & Result)66*f4a2713aSLionel Sambuc void ReplaceIfStmtWithItsBody::run(
67*f4a2713aSLionel Sambuc const ast_matchers::MatchFinder::MatchResult &Result) {
68*f4a2713aSLionel Sambuc if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) {
69*f4a2713aSLionel Sambuc const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
70*f4a2713aSLionel Sambuc if (Body) {
71*f4a2713aSLionel Sambuc Replace.insert(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body));
72*f4a2713aSLionel Sambuc } else if (!PickTrueBranch) {
73*f4a2713aSLionel Sambuc // If we want to use the 'else'-branch, but it doesn't exist, delete
74*f4a2713aSLionel Sambuc // the whole 'if'.
75*f4a2713aSLionel Sambuc Replace.insert(replaceStmtWithText(*Result.SourceManager, *Node, ""));
76*f4a2713aSLionel Sambuc }
77*f4a2713aSLionel Sambuc }
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc
80*f4a2713aSLionel Sambuc } // end namespace tooling
81*f4a2713aSLionel Sambuc } // end namespace clang
82