blob: af25fd87b26e760ae909666646761bc9ae7ef0ad [file] [log] [blame]
Daniel Jasper7e222822012-07-16 09:18:17 +00001//===--- 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"
Daniel Jasper1975e032012-07-17 08:03:01 +000013#include "clang/Tooling/RefactoringCallbacks.h"
Daniel Jasper7e222822012-07-16 09:18:17 +000014
15namespace clang {
Daniel Jasper6389dd12012-07-17 08:37:03 +000016namespace tooling {
Daniel Jasper7e222822012-07-16 09:18:17 +000017
18RefactoringCallback::RefactoringCallback() {}
19tooling::Replacements &RefactoringCallback::getReplacements() {
20 return Replace;
21}
22
Daniel Jasper6389dd12012-07-17 08:37:03 +000023static Replacement replaceStmtWithText(SourceManager &Sources,
24 const Stmt &From,
25 StringRef Text) {
Daniel Jasper7e222822012-07-16 09:18:17 +000026 return tooling::Replacement(Sources, CharSourceRange::getTokenRange(
27 From.getSourceRange()), Text);
28}
Daniel Jasper6389dd12012-07-17 08:37:03 +000029static Replacement replaceStmtWithStmt(SourceManager &Sources,
30 const Stmt &From,
31 const Stmt &To) {
Daniel Jasper7e222822012-07-16 09:18:17 +000032 return replaceStmtWithText(Sources, From, Lexer::getSourceText(
33 CharSourceRange::getTokenRange(To.getSourceRange()),
34 Sources, LangOptions()));
35}
36
37ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText)
38 : FromId(FromId), ToText(ToText) {}
39
Daniel Jasper6389dd12012-07-17 08:37:03 +000040void ReplaceStmtWithText::run(
41 const ast_matchers::MatchFinder::MatchResult &Result) {
Daniel Jasper7e222822012-07-16 09:18:17 +000042 if (const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId)) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000043 auto Err = Replace.add(tooling::Replacement(
Daniel Jasper7e222822012-07-16 09:18:17 +000044 *Result.SourceManager,
Eric Liu40ef2fb2016-08-01 10:16:37 +000045 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);
Daniel Jasper7e222822012-07-16 09:18:17 +000051 }
52}
53
54ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
55 : FromId(FromId), ToId(ToId) {}
56
Daniel Jasper6389dd12012-07-17 08:37:03 +000057void ReplaceStmtWithStmt::run(
58 const ast_matchers::MatchFinder::MatchResult &Result) {
Daniel Jasper7e222822012-07-16 09:18:17 +000059 const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId);
60 const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId);
Eric Liu40ef2fb2016-08-01 10:16:37 +000061 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 }
Daniel Jasper7e222822012-07-16 09:18:17 +000070}
71
72ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id,
73 bool PickTrueBranch)
74 : Id(Id), PickTrueBranch(PickTrueBranch) {}
75
Daniel Jasper6389dd12012-07-17 08:37:03 +000076void ReplaceIfStmtWithItsBody::run(
77 const ast_matchers::MatchFinder::MatchResult &Result) {
Daniel Jasper7e222822012-07-16 09:18:17 +000078 if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) {
79 const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
80 if (Body) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000081 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);
Daniel Jasper7e222822012-07-16 09:18:17 +000088 } else if (!PickTrueBranch) {
89 // If we want to use the 'else'-branch, but it doesn't exist, delete
90 // the whole 'if'.
Eric Liu40ef2fb2016-08-01 10:16:37 +000091 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);
Daniel Jasper7e222822012-07-16 09:18:17 +000098 }
99 }
100}
101
Daniel Jasper6389dd12012-07-17 08:37:03 +0000102} // end namespace tooling
Daniel Jasper7e222822012-07-16 09:18:17 +0000103} // end namespace clang