blob: e900c23e4f64e738613eaee4b9634c39ca11ef68 [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) {
Alexander Kornienko7cdc7052016-12-13 16:19:34 +000042 if (const Stmt *FromMatch = Result.Nodes.getNodeAs<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.
Piotr Padlewski1ec383c2016-12-23 11:40:44 +000048 if (Err) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000049 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Piotr Padlewski1ec383c2016-12-23 11:40:44 +000050 assert(false);
51 }
Daniel Jasper7e222822012-07-16 09:18:17 +000052 }
53}
54
55ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
56 : FromId(FromId), ToId(ToId) {}
57
Daniel Jasper6389dd12012-07-17 08:37:03 +000058void ReplaceStmtWithStmt::run(
59 const ast_matchers::MatchFinder::MatchResult &Result) {
Alexander Kornienko7cdc7052016-12-13 16:19:34 +000060 const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId);
61 const Stmt *ToMatch = Result.Nodes.getNodeAs<Stmt>(ToId);
Eric Liu40ef2fb2016-08-01 10:16:37 +000062 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.
Piotr Padlewski1ec383c2016-12-23 11:40:44 +000067 if (Err) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000068 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Piotr Padlewski1ec383c2016-12-23 11:40:44 +000069 assert(false);
70 }
Eric Liu40ef2fb2016-08-01 10:16:37 +000071 }
Daniel Jasper7e222822012-07-16 09:18:17 +000072}
73
74ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id,
75 bool PickTrueBranch)
76 : Id(Id), PickTrueBranch(PickTrueBranch) {}
77
Daniel Jasper6389dd12012-07-17 08:37:03 +000078void ReplaceIfStmtWithItsBody::run(
79 const ast_matchers::MatchFinder::MatchResult &Result) {
Alexander Kornienko7cdc7052016-12-13 16:19:34 +000080 if (const IfStmt *Node = Result.Nodes.getNodeAs<IfStmt>(Id)) {
Daniel Jasper7e222822012-07-16 09:18:17 +000081 const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
82 if (Body) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000083 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.
Piotr Padlewski1ec383c2016-12-23 11:40:44 +000087 if (Err) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000088 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Piotr Padlewski1ec383c2016-12-23 11:40:44 +000089 assert(false);
90 }
Daniel Jasper7e222822012-07-16 09:18:17 +000091 } else if (!PickTrueBranch) {
92 // If we want to use the 'else'-branch, but it doesn't exist, delete
93 // the whole 'if'.
Eric Liu40ef2fb2016-08-01 10:16:37 +000094 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.
Piotr Padlewski1ec383c2016-12-23 11:40:44 +000098 if (Err) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000099 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Piotr Padlewski1ec383c2016-12-23 11:40:44 +0000100 assert(false);
101 }
Daniel Jasper7e222822012-07-16 09:18:17 +0000102 }
103 }
104}
105
Daniel Jasper6389dd12012-07-17 08:37:03 +0000106} // end namespace tooling
Daniel Jasper7e222822012-07-16 09:18:17 +0000107} // end namespace clang