clang  3.7.0
RefactoringCallbacks.cpp
Go to the documentation of this file.
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"
14 
15 namespace clang {
16 namespace tooling {
17 
20  return Replace;
21 }
22 
24  const Stmt &From,
25  StringRef Text) {
27  From.getSourceRange()), Text);
28 }
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 
42  if (const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId)) {
44  *Result.SourceManager,
45  CharSourceRange::getTokenRange(FromMatch->getSourceRange()),
46  ToText));
47  }
48 }
49 
50 ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
51  : FromId(FromId), ToId(ToId) {}
52 
55  const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId);
56  const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId);
57  if (FromMatch && ToMatch)
59  *Result.SourceManager, *FromMatch, *ToMatch));
60 }
61 
63  bool PickTrueBranch)
64  : Id(Id), PickTrueBranch(PickTrueBranch) {}
65 
68  if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) {
69  const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
70  if (Body) {
71  Replace.insert(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body));
72  } else if (!PickTrueBranch) {
73  // If we want to use the 'else'-branch, but it doesn't exist, delete
74  // the whole 'if'.
75  Replace.insert(replaceStmtWithText(*Result.SourceManager, *Node, ""));
76  }
77  }
78 }
79 
80 } // end namespace tooling
81 } // end namespace clang
const T * getStmtAs(StringRef ID) const
Definition: ASTMatchers.h:86
static Replacement replaceStmtWithText(SourceManager &Sources, const Stmt &From, StringRef Text)
static CharSourceRange getTokenRange(SourceRange R)
std::set< Replacement > Replacements
A set of Replacements. FIXME: Change to a vector and deduplicate in the RefactoringTool.
Definition: Replacement.h:141
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
ReplaceIfStmtWithItsBody(StringRef Id, bool PickTrueBranch)
A text replacement.
Definition: Replacement.h:70
static StringRef getSourceText(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, bool *Invalid=nullptr)
Returns a string for the source that the range encompasses.
Definition: Lexer.cpp:920
Contains all information for a given match.
ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
ReplaceStmtWithText(StringRef FromId, StringRef ToText)
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
const BoundNodes Nodes
Contains the nodes bound on the current match.
ast_type_traits::DynTypedNode Node
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Called on every match by the MatchFinder.
clang::SourceManager *const SourceManager
static Replacement replaceStmtWithStmt(SourceManager &Sources, const Stmt &From, const Stmt &To)
This class handles loading and caching of source files into memory.