xref: /minix3/external/bsd/llvm/dist/clang/lib/ARCMigrate/TransformActions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ARCMT.cpp - Migration to ARC mode --------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "Internals.h"
11f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
12f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
13f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
14f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/DenseSet.h"
16f4a2713aSLionel Sambuc #include <map>
17f4a2713aSLionel Sambuc using namespace clang;
18f4a2713aSLionel Sambuc using namespace arcmt;
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc namespace {
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc /// \brief Collects transformations and merges them before applying them with
23f4a2713aSLionel Sambuc /// with applyRewrites(). E.g. if the same source range
24f4a2713aSLionel Sambuc /// is requested to be removed twice, only one rewriter remove will be invoked.
25f4a2713aSLionel Sambuc /// Rewrites happen in "transactions"; if one rewrite in the transaction cannot
26f4a2713aSLionel Sambuc /// be done (e.g. it resides in a macro) all rewrites in the transaction are
27f4a2713aSLionel Sambuc /// aborted.
28f4a2713aSLionel Sambuc /// FIXME: "Transactional" rewrites support should be baked in the Rewriter.
29f4a2713aSLionel Sambuc class TransformActionsImpl {
30f4a2713aSLionel Sambuc   CapturedDiagList &CapturedDiags;
31f4a2713aSLionel Sambuc   ASTContext &Ctx;
32f4a2713aSLionel Sambuc   Preprocessor &PP;
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc   bool IsInTransaction;
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc   enum ActionKind {
37f4a2713aSLionel Sambuc     Act_Insert, Act_InsertAfterToken,
38f4a2713aSLionel Sambuc     Act_Remove, Act_RemoveStmt,
39f4a2713aSLionel Sambuc     Act_Replace, Act_ReplaceText,
40f4a2713aSLionel Sambuc     Act_IncreaseIndentation,
41f4a2713aSLionel Sambuc     Act_ClearDiagnostic
42f4a2713aSLionel Sambuc   };
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   struct ActionData {
45f4a2713aSLionel Sambuc     ActionKind Kind;
46f4a2713aSLionel Sambuc     SourceLocation Loc;
47f4a2713aSLionel Sambuc     SourceRange R1, R2;
48f4a2713aSLionel Sambuc     StringRef Text1, Text2;
49f4a2713aSLionel Sambuc     Stmt *S;
50f4a2713aSLionel Sambuc     SmallVector<unsigned, 2> DiagIDs;
51f4a2713aSLionel Sambuc   };
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc   std::vector<ActionData> CachedActions;
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc   enum RangeComparison {
56f4a2713aSLionel Sambuc     Range_Before,
57f4a2713aSLionel Sambuc     Range_After,
58f4a2713aSLionel Sambuc     Range_Contains,
59f4a2713aSLionel Sambuc     Range_Contained,
60f4a2713aSLionel Sambuc     Range_ExtendsBegin,
61f4a2713aSLionel Sambuc     Range_ExtendsEnd
62f4a2713aSLionel Sambuc   };
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   /// \brief A range to remove. It is a character range.
65f4a2713aSLionel Sambuc   struct CharRange {
66f4a2713aSLionel Sambuc     FullSourceLoc Begin, End;
67f4a2713aSLionel Sambuc 
CharRange__anon365d55880111::TransformActionsImpl::CharRange68f4a2713aSLionel Sambuc     CharRange(CharSourceRange range, SourceManager &srcMgr, Preprocessor &PP) {
69f4a2713aSLionel Sambuc       SourceLocation beginLoc = range.getBegin(), endLoc = range.getEnd();
70f4a2713aSLionel Sambuc       assert(beginLoc.isValid() && endLoc.isValid());
71f4a2713aSLionel Sambuc       if (range.isTokenRange()) {
72f4a2713aSLionel Sambuc         Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr);
73f4a2713aSLionel Sambuc         End = FullSourceLoc(getLocForEndOfToken(endLoc, srcMgr, PP), srcMgr);
74f4a2713aSLionel Sambuc       } else {
75f4a2713aSLionel Sambuc         Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr);
76f4a2713aSLionel Sambuc         End = FullSourceLoc(srcMgr.getExpansionLoc(endLoc), srcMgr);
77f4a2713aSLionel Sambuc       }
78f4a2713aSLionel Sambuc       assert(Begin.isValid() && End.isValid());
79f4a2713aSLionel Sambuc     }
80f4a2713aSLionel Sambuc 
compareWith__anon365d55880111::TransformActionsImpl::CharRange81f4a2713aSLionel Sambuc     RangeComparison compareWith(const CharRange &RHS) const {
82f4a2713aSLionel Sambuc       if (End.isBeforeInTranslationUnitThan(RHS.Begin))
83f4a2713aSLionel Sambuc         return Range_Before;
84f4a2713aSLionel Sambuc       if (RHS.End.isBeforeInTranslationUnitThan(Begin))
85f4a2713aSLionel Sambuc         return Range_After;
86f4a2713aSLionel Sambuc       if (!Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
87f4a2713aSLionel Sambuc           !RHS.End.isBeforeInTranslationUnitThan(End))
88f4a2713aSLionel Sambuc         return Range_Contained;
89f4a2713aSLionel Sambuc       if (Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
90f4a2713aSLionel Sambuc           RHS.End.isBeforeInTranslationUnitThan(End))
91f4a2713aSLionel Sambuc         return Range_Contains;
92f4a2713aSLionel Sambuc       if (Begin.isBeforeInTranslationUnitThan(RHS.Begin))
93f4a2713aSLionel Sambuc         return Range_ExtendsBegin;
94f4a2713aSLionel Sambuc       else
95f4a2713aSLionel Sambuc         return Range_ExtendsEnd;
96f4a2713aSLionel Sambuc     }
97f4a2713aSLionel Sambuc 
compare__anon365d55880111::TransformActionsImpl::CharRange98f4a2713aSLionel Sambuc     static RangeComparison compare(SourceRange LHS, SourceRange RHS,
99f4a2713aSLionel Sambuc                                    SourceManager &SrcMgr, Preprocessor &PP) {
100f4a2713aSLionel Sambuc       return CharRange(CharSourceRange::getTokenRange(LHS), SrcMgr, PP)
101f4a2713aSLionel Sambuc                   .compareWith(CharRange(CharSourceRange::getTokenRange(RHS),
102f4a2713aSLionel Sambuc                                             SrcMgr, PP));
103f4a2713aSLionel Sambuc     }
104f4a2713aSLionel Sambuc   };
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   typedef SmallVector<StringRef, 2> TextsVec;
107f4a2713aSLionel Sambuc   typedef std::map<FullSourceLoc, TextsVec, FullSourceLoc::BeforeThanCompare>
108f4a2713aSLionel Sambuc       InsertsMap;
109f4a2713aSLionel Sambuc   InsertsMap Inserts;
110f4a2713aSLionel Sambuc   /// \brief A list of ranges to remove. They are always sorted and they never
111f4a2713aSLionel Sambuc   /// intersect with each other.
112f4a2713aSLionel Sambuc   std::list<CharRange> Removals;
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   llvm::DenseSet<Stmt *> StmtRemovals;
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc   std::vector<std::pair<CharRange, SourceLocation> > IndentationRanges;
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc   /// \brief Keeps text passed to transformation methods.
119f4a2713aSLionel Sambuc   llvm::StringMap<bool> UniqueText;
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc public:
TransformActionsImpl(CapturedDiagList & capturedDiags,ASTContext & ctx,Preprocessor & PP)122f4a2713aSLionel Sambuc   TransformActionsImpl(CapturedDiagList &capturedDiags,
123f4a2713aSLionel Sambuc                        ASTContext &ctx, Preprocessor &PP)
124f4a2713aSLionel Sambuc     : CapturedDiags(capturedDiags), Ctx(ctx), PP(PP), IsInTransaction(false) { }
125f4a2713aSLionel Sambuc 
getASTContext()126f4a2713aSLionel Sambuc   ASTContext &getASTContext() { return Ctx; }
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc   void startTransaction();
129f4a2713aSLionel Sambuc   bool commitTransaction();
130f4a2713aSLionel Sambuc   void abortTransaction();
131f4a2713aSLionel Sambuc 
isInTransaction() const132f4a2713aSLionel Sambuc   bool isInTransaction() const { return IsInTransaction; }
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   void insert(SourceLocation loc, StringRef text);
135f4a2713aSLionel Sambuc   void insertAfterToken(SourceLocation loc, StringRef text);
136f4a2713aSLionel Sambuc   void remove(SourceRange range);
137f4a2713aSLionel Sambuc   void removeStmt(Stmt *S);
138f4a2713aSLionel Sambuc   void replace(SourceRange range, StringRef text);
139f4a2713aSLionel Sambuc   void replace(SourceRange range, SourceRange replacementRange);
140f4a2713aSLionel Sambuc   void replaceStmt(Stmt *S, StringRef text);
141f4a2713aSLionel Sambuc   void replaceText(SourceLocation loc, StringRef text,
142f4a2713aSLionel Sambuc                    StringRef replacementText);
143f4a2713aSLionel Sambuc   void increaseIndentation(SourceRange range,
144f4a2713aSLionel Sambuc                            SourceLocation parentIndent);
145f4a2713aSLionel Sambuc 
146f4a2713aSLionel Sambuc   bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc   void applyRewrites(TransformActions::RewriteReceiver &receiver);
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc private:
151f4a2713aSLionel Sambuc   bool canInsert(SourceLocation loc);
152f4a2713aSLionel Sambuc   bool canInsertAfterToken(SourceLocation loc);
153f4a2713aSLionel Sambuc   bool canRemoveRange(SourceRange range);
154f4a2713aSLionel Sambuc   bool canReplaceRange(SourceRange range, SourceRange replacementRange);
155f4a2713aSLionel Sambuc   bool canReplaceText(SourceLocation loc, StringRef text);
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   void commitInsert(SourceLocation loc, StringRef text);
158f4a2713aSLionel Sambuc   void commitInsertAfterToken(SourceLocation loc, StringRef text);
159f4a2713aSLionel Sambuc   void commitRemove(SourceRange range);
160f4a2713aSLionel Sambuc   void commitRemoveStmt(Stmt *S);
161f4a2713aSLionel Sambuc   void commitReplace(SourceRange range, SourceRange replacementRange);
162f4a2713aSLionel Sambuc   void commitReplaceText(SourceLocation loc, StringRef text,
163f4a2713aSLionel Sambuc                          StringRef replacementText);
164f4a2713aSLionel Sambuc   void commitIncreaseIndentation(SourceRange range,SourceLocation parentIndent);
165f4a2713aSLionel Sambuc   void commitClearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc   void addRemoval(CharSourceRange range);
168f4a2713aSLionel Sambuc   void addInsertion(SourceLocation loc, StringRef text);
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc   /// \brief Stores text passed to the transformation methods to keep the string
171f4a2713aSLionel Sambuc   /// "alive". Since the vast majority of text will be the same, we also unique
172f4a2713aSLionel Sambuc   /// the strings using a StringMap.
173f4a2713aSLionel Sambuc   StringRef getUniqueText(StringRef text);
174f4a2713aSLionel Sambuc 
175f4a2713aSLionel Sambuc   /// \brief Computes the source location just past the end of the token at
176f4a2713aSLionel Sambuc   /// the given source location. If the location points at a macro, the whole
177f4a2713aSLionel Sambuc   /// macro expansion is skipped.
178f4a2713aSLionel Sambuc   static SourceLocation getLocForEndOfToken(SourceLocation loc,
179f4a2713aSLionel Sambuc                                             SourceManager &SM,Preprocessor &PP);
180f4a2713aSLionel Sambuc };
181f4a2713aSLionel Sambuc 
182f4a2713aSLionel Sambuc } // anonymous namespace
183f4a2713aSLionel Sambuc 
startTransaction()184f4a2713aSLionel Sambuc void TransformActionsImpl::startTransaction() {
185f4a2713aSLionel Sambuc   assert(!IsInTransaction &&
186f4a2713aSLionel Sambuc          "Cannot start a transaction in the middle of another one");
187f4a2713aSLionel Sambuc   IsInTransaction = true;
188f4a2713aSLionel Sambuc }
189f4a2713aSLionel Sambuc 
commitTransaction()190f4a2713aSLionel Sambuc bool TransformActionsImpl::commitTransaction() {
191f4a2713aSLionel Sambuc   assert(IsInTransaction && "No transaction started");
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc   if (CachedActions.empty()) {
194f4a2713aSLionel Sambuc     IsInTransaction = false;
195f4a2713aSLionel Sambuc     return false;
196f4a2713aSLionel Sambuc   }
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   // Verify that all actions are possible otherwise abort the whole transaction.
199f4a2713aSLionel Sambuc   bool AllActionsPossible = true;
200f4a2713aSLionel Sambuc   for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
201f4a2713aSLionel Sambuc     ActionData &act = CachedActions[i];
202f4a2713aSLionel Sambuc     switch (act.Kind) {
203f4a2713aSLionel Sambuc     case Act_Insert:
204f4a2713aSLionel Sambuc       if (!canInsert(act.Loc))
205f4a2713aSLionel Sambuc         AllActionsPossible = false;
206f4a2713aSLionel Sambuc       break;
207f4a2713aSLionel Sambuc     case Act_InsertAfterToken:
208f4a2713aSLionel Sambuc       if (!canInsertAfterToken(act.Loc))
209f4a2713aSLionel Sambuc         AllActionsPossible = false;
210f4a2713aSLionel Sambuc       break;
211f4a2713aSLionel Sambuc     case Act_Remove:
212f4a2713aSLionel Sambuc       if (!canRemoveRange(act.R1))
213f4a2713aSLionel Sambuc         AllActionsPossible = false;
214f4a2713aSLionel Sambuc       break;
215f4a2713aSLionel Sambuc     case Act_RemoveStmt:
216f4a2713aSLionel Sambuc       assert(act.S);
217f4a2713aSLionel Sambuc       if (!canRemoveRange(act.S->getSourceRange()))
218f4a2713aSLionel Sambuc         AllActionsPossible = false;
219f4a2713aSLionel Sambuc       break;
220f4a2713aSLionel Sambuc     case Act_Replace:
221f4a2713aSLionel Sambuc       if (!canReplaceRange(act.R1, act.R2))
222f4a2713aSLionel Sambuc         AllActionsPossible = false;
223f4a2713aSLionel Sambuc       break;
224f4a2713aSLionel Sambuc     case Act_ReplaceText:
225f4a2713aSLionel Sambuc       if (!canReplaceText(act.Loc, act.Text1))
226f4a2713aSLionel Sambuc         AllActionsPossible = false;
227f4a2713aSLionel Sambuc       break;
228f4a2713aSLionel Sambuc     case Act_IncreaseIndentation:
229f4a2713aSLionel Sambuc       // This is not important, we don't care if it will fail.
230f4a2713aSLionel Sambuc       break;
231f4a2713aSLionel Sambuc     case Act_ClearDiagnostic:
232f4a2713aSLionel Sambuc       // We are just checking source rewrites.
233f4a2713aSLionel Sambuc       break;
234f4a2713aSLionel Sambuc     }
235f4a2713aSLionel Sambuc     if (!AllActionsPossible)
236f4a2713aSLionel Sambuc       break;
237f4a2713aSLionel Sambuc   }
238f4a2713aSLionel Sambuc 
239f4a2713aSLionel Sambuc   if (!AllActionsPossible) {
240f4a2713aSLionel Sambuc     abortTransaction();
241f4a2713aSLionel Sambuc     return true;
242f4a2713aSLionel Sambuc   }
243f4a2713aSLionel Sambuc 
244f4a2713aSLionel Sambuc   for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
245f4a2713aSLionel Sambuc     ActionData &act = CachedActions[i];
246f4a2713aSLionel Sambuc     switch (act.Kind) {
247f4a2713aSLionel Sambuc     case Act_Insert:
248f4a2713aSLionel Sambuc       commitInsert(act.Loc, act.Text1);
249f4a2713aSLionel Sambuc       break;
250f4a2713aSLionel Sambuc     case Act_InsertAfterToken:
251f4a2713aSLionel Sambuc       commitInsertAfterToken(act.Loc, act.Text1);
252f4a2713aSLionel Sambuc       break;
253f4a2713aSLionel Sambuc     case Act_Remove:
254f4a2713aSLionel Sambuc       commitRemove(act.R1);
255f4a2713aSLionel Sambuc       break;
256f4a2713aSLionel Sambuc     case Act_RemoveStmt:
257f4a2713aSLionel Sambuc       commitRemoveStmt(act.S);
258f4a2713aSLionel Sambuc       break;
259f4a2713aSLionel Sambuc     case Act_Replace:
260f4a2713aSLionel Sambuc       commitReplace(act.R1, act.R2);
261f4a2713aSLionel Sambuc       break;
262f4a2713aSLionel Sambuc     case Act_ReplaceText:
263f4a2713aSLionel Sambuc       commitReplaceText(act.Loc, act.Text1, act.Text2);
264f4a2713aSLionel Sambuc       break;
265f4a2713aSLionel Sambuc     case Act_IncreaseIndentation:
266f4a2713aSLionel Sambuc       commitIncreaseIndentation(act.R1, act.Loc);
267f4a2713aSLionel Sambuc       break;
268f4a2713aSLionel Sambuc     case Act_ClearDiagnostic:
269f4a2713aSLionel Sambuc       commitClearDiagnostic(act.DiagIDs, act.R1);
270f4a2713aSLionel Sambuc       break;
271f4a2713aSLionel Sambuc     }
272f4a2713aSLionel Sambuc   }
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc   CachedActions.clear();
275f4a2713aSLionel Sambuc   IsInTransaction = false;
276f4a2713aSLionel Sambuc   return false;
277f4a2713aSLionel Sambuc }
278f4a2713aSLionel Sambuc 
abortTransaction()279f4a2713aSLionel Sambuc void TransformActionsImpl::abortTransaction() {
280f4a2713aSLionel Sambuc   assert(IsInTransaction && "No transaction started");
281f4a2713aSLionel Sambuc   CachedActions.clear();
282f4a2713aSLionel Sambuc   IsInTransaction = false;
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc 
insert(SourceLocation loc,StringRef text)285f4a2713aSLionel Sambuc void TransformActionsImpl::insert(SourceLocation loc, StringRef text) {
286f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
287f4a2713aSLionel Sambuc   text = getUniqueText(text);
288f4a2713aSLionel Sambuc   ActionData data;
289f4a2713aSLionel Sambuc   data.Kind = Act_Insert;
290f4a2713aSLionel Sambuc   data.Loc = loc;
291f4a2713aSLionel Sambuc   data.Text1 = text;
292f4a2713aSLionel Sambuc   CachedActions.push_back(data);
293f4a2713aSLionel Sambuc }
294f4a2713aSLionel Sambuc 
insertAfterToken(SourceLocation loc,StringRef text)295f4a2713aSLionel Sambuc void TransformActionsImpl::insertAfterToken(SourceLocation loc, StringRef text) {
296f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
297f4a2713aSLionel Sambuc   text = getUniqueText(text);
298f4a2713aSLionel Sambuc   ActionData data;
299f4a2713aSLionel Sambuc   data.Kind = Act_InsertAfterToken;
300f4a2713aSLionel Sambuc   data.Loc = loc;
301f4a2713aSLionel Sambuc   data.Text1 = text;
302f4a2713aSLionel Sambuc   CachedActions.push_back(data);
303f4a2713aSLionel Sambuc }
304f4a2713aSLionel Sambuc 
remove(SourceRange range)305f4a2713aSLionel Sambuc void TransformActionsImpl::remove(SourceRange range) {
306f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
307f4a2713aSLionel Sambuc   ActionData data;
308f4a2713aSLionel Sambuc   data.Kind = Act_Remove;
309f4a2713aSLionel Sambuc   data.R1 = range;
310f4a2713aSLionel Sambuc   CachedActions.push_back(data);
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc 
removeStmt(Stmt * S)313f4a2713aSLionel Sambuc void TransformActionsImpl::removeStmt(Stmt *S) {
314f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
315f4a2713aSLionel Sambuc   ActionData data;
316f4a2713aSLionel Sambuc   data.Kind = Act_RemoveStmt;
317f4a2713aSLionel Sambuc   data.S = S->IgnoreImplicit(); // important for uniquing
318f4a2713aSLionel Sambuc   CachedActions.push_back(data);
319f4a2713aSLionel Sambuc }
320f4a2713aSLionel Sambuc 
replace(SourceRange range,StringRef text)321f4a2713aSLionel Sambuc void TransformActionsImpl::replace(SourceRange range, StringRef text) {
322f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
323f4a2713aSLionel Sambuc   text = getUniqueText(text);
324f4a2713aSLionel Sambuc   remove(range);
325f4a2713aSLionel Sambuc   insert(range.getBegin(), text);
326f4a2713aSLionel Sambuc }
327f4a2713aSLionel Sambuc 
replace(SourceRange range,SourceRange replacementRange)328f4a2713aSLionel Sambuc void TransformActionsImpl::replace(SourceRange range,
329f4a2713aSLionel Sambuc                                    SourceRange replacementRange) {
330f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
331f4a2713aSLionel Sambuc   ActionData data;
332f4a2713aSLionel Sambuc   data.Kind = Act_Replace;
333f4a2713aSLionel Sambuc   data.R1 = range;
334f4a2713aSLionel Sambuc   data.R2 = replacementRange;
335f4a2713aSLionel Sambuc   CachedActions.push_back(data);
336f4a2713aSLionel Sambuc }
337f4a2713aSLionel Sambuc 
replaceText(SourceLocation loc,StringRef text,StringRef replacementText)338f4a2713aSLionel Sambuc void TransformActionsImpl::replaceText(SourceLocation loc, StringRef text,
339f4a2713aSLionel Sambuc                                        StringRef replacementText) {
340f4a2713aSLionel Sambuc   text = getUniqueText(text);
341f4a2713aSLionel Sambuc   replacementText = getUniqueText(replacementText);
342f4a2713aSLionel Sambuc   ActionData data;
343f4a2713aSLionel Sambuc   data.Kind = Act_ReplaceText;
344f4a2713aSLionel Sambuc   data.Loc = loc;
345f4a2713aSLionel Sambuc   data.Text1 = text;
346f4a2713aSLionel Sambuc   data.Text2 = replacementText;
347f4a2713aSLionel Sambuc   CachedActions.push_back(data);
348f4a2713aSLionel Sambuc }
349f4a2713aSLionel Sambuc 
replaceStmt(Stmt * S,StringRef text)350f4a2713aSLionel Sambuc void TransformActionsImpl::replaceStmt(Stmt *S, StringRef text) {
351f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
352f4a2713aSLionel Sambuc   text = getUniqueText(text);
353f4a2713aSLionel Sambuc   insert(S->getLocStart(), text);
354f4a2713aSLionel Sambuc   removeStmt(S);
355f4a2713aSLionel Sambuc }
356f4a2713aSLionel Sambuc 
increaseIndentation(SourceRange range,SourceLocation parentIndent)357f4a2713aSLionel Sambuc void TransformActionsImpl::increaseIndentation(SourceRange range,
358f4a2713aSLionel Sambuc                                                SourceLocation parentIndent) {
359f4a2713aSLionel Sambuc   if (range.isInvalid()) return;
360f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
361f4a2713aSLionel Sambuc   ActionData data;
362f4a2713aSLionel Sambuc   data.Kind = Act_IncreaseIndentation;
363f4a2713aSLionel Sambuc   data.R1 = range;
364f4a2713aSLionel Sambuc   data.Loc = parentIndent;
365f4a2713aSLionel Sambuc   CachedActions.push_back(data);
366f4a2713aSLionel Sambuc }
367f4a2713aSLionel Sambuc 
clearDiagnostic(ArrayRef<unsigned> IDs,SourceRange range)368f4a2713aSLionel Sambuc bool TransformActionsImpl::clearDiagnostic(ArrayRef<unsigned> IDs,
369f4a2713aSLionel Sambuc                                            SourceRange range) {
370f4a2713aSLionel Sambuc   assert(IsInTransaction && "Actions only allowed during a transaction");
371f4a2713aSLionel Sambuc   if (!CapturedDiags.hasDiagnostic(IDs, range))
372f4a2713aSLionel Sambuc     return false;
373f4a2713aSLionel Sambuc 
374f4a2713aSLionel Sambuc   ActionData data;
375f4a2713aSLionel Sambuc   data.Kind = Act_ClearDiagnostic;
376f4a2713aSLionel Sambuc   data.R1 = range;
377f4a2713aSLionel Sambuc   data.DiagIDs.append(IDs.begin(), IDs.end());
378f4a2713aSLionel Sambuc   CachedActions.push_back(data);
379f4a2713aSLionel Sambuc   return true;
380f4a2713aSLionel Sambuc }
381f4a2713aSLionel Sambuc 
canInsert(SourceLocation loc)382f4a2713aSLionel Sambuc bool TransformActionsImpl::canInsert(SourceLocation loc) {
383f4a2713aSLionel Sambuc   if (loc.isInvalid())
384f4a2713aSLionel Sambuc     return false;
385f4a2713aSLionel Sambuc 
386f4a2713aSLionel Sambuc   SourceManager &SM = Ctx.getSourceManager();
387f4a2713aSLionel Sambuc   if (SM.isInSystemHeader(SM.getExpansionLoc(loc)))
388f4a2713aSLionel Sambuc     return false;
389f4a2713aSLionel Sambuc 
390f4a2713aSLionel Sambuc   if (loc.isFileID())
391f4a2713aSLionel Sambuc     return true;
392f4a2713aSLionel Sambuc   return PP.isAtStartOfMacroExpansion(loc);
393f4a2713aSLionel Sambuc }
394f4a2713aSLionel Sambuc 
canInsertAfterToken(SourceLocation loc)395f4a2713aSLionel Sambuc bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) {
396f4a2713aSLionel Sambuc   if (loc.isInvalid())
397f4a2713aSLionel Sambuc     return false;
398f4a2713aSLionel Sambuc 
399f4a2713aSLionel Sambuc   SourceManager &SM = Ctx.getSourceManager();
400f4a2713aSLionel Sambuc   if (SM.isInSystemHeader(SM.getExpansionLoc(loc)))
401f4a2713aSLionel Sambuc     return false;
402f4a2713aSLionel Sambuc 
403f4a2713aSLionel Sambuc   if (loc.isFileID())
404f4a2713aSLionel Sambuc     return true;
405f4a2713aSLionel Sambuc   return PP.isAtEndOfMacroExpansion(loc);
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc 
canRemoveRange(SourceRange range)408f4a2713aSLionel Sambuc bool TransformActionsImpl::canRemoveRange(SourceRange range) {
409f4a2713aSLionel Sambuc   return canInsert(range.getBegin()) && canInsertAfterToken(range.getEnd());
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc 
canReplaceRange(SourceRange range,SourceRange replacementRange)412f4a2713aSLionel Sambuc bool TransformActionsImpl::canReplaceRange(SourceRange range,
413f4a2713aSLionel Sambuc                                            SourceRange replacementRange) {
414f4a2713aSLionel Sambuc   return canRemoveRange(range) && canRemoveRange(replacementRange);
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc 
canReplaceText(SourceLocation loc,StringRef text)417f4a2713aSLionel Sambuc bool TransformActionsImpl::canReplaceText(SourceLocation loc, StringRef text) {
418f4a2713aSLionel Sambuc   if (!canInsert(loc))
419f4a2713aSLionel Sambuc     return false;
420f4a2713aSLionel Sambuc 
421f4a2713aSLionel Sambuc   SourceManager &SM = Ctx.getSourceManager();
422f4a2713aSLionel Sambuc   loc = SM.getExpansionLoc(loc);
423f4a2713aSLionel Sambuc 
424f4a2713aSLionel Sambuc   // Break down the source location.
425f4a2713aSLionel Sambuc   std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
426f4a2713aSLionel Sambuc 
427f4a2713aSLionel Sambuc   // Try to load the file buffer.
428f4a2713aSLionel Sambuc   bool invalidTemp = false;
429f4a2713aSLionel Sambuc   StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
430f4a2713aSLionel Sambuc   if (invalidTemp)
431f4a2713aSLionel Sambuc     return false;
432f4a2713aSLionel Sambuc 
433f4a2713aSLionel Sambuc   return file.substr(locInfo.second).startswith(text);
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc 
commitInsert(SourceLocation loc,StringRef text)436f4a2713aSLionel Sambuc void TransformActionsImpl::commitInsert(SourceLocation loc, StringRef text) {
437f4a2713aSLionel Sambuc   addInsertion(loc, text);
438f4a2713aSLionel Sambuc }
439f4a2713aSLionel Sambuc 
commitInsertAfterToken(SourceLocation loc,StringRef text)440f4a2713aSLionel Sambuc void TransformActionsImpl::commitInsertAfterToken(SourceLocation loc,
441f4a2713aSLionel Sambuc                                                   StringRef text) {
442f4a2713aSLionel Sambuc   addInsertion(getLocForEndOfToken(loc, Ctx.getSourceManager(), PP), text);
443f4a2713aSLionel Sambuc }
444f4a2713aSLionel Sambuc 
commitRemove(SourceRange range)445f4a2713aSLionel Sambuc void TransformActionsImpl::commitRemove(SourceRange range) {
446f4a2713aSLionel Sambuc   addRemoval(CharSourceRange::getTokenRange(range));
447f4a2713aSLionel Sambuc }
448f4a2713aSLionel Sambuc 
commitRemoveStmt(Stmt * S)449f4a2713aSLionel Sambuc void TransformActionsImpl::commitRemoveStmt(Stmt *S) {
450f4a2713aSLionel Sambuc   assert(S);
451f4a2713aSLionel Sambuc   if (StmtRemovals.count(S))
452f4a2713aSLionel Sambuc     return; // already removed.
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc   if (Expr *E = dyn_cast<Expr>(S)) {
455f4a2713aSLionel Sambuc     commitRemove(E->getSourceRange());
456f4a2713aSLionel Sambuc     commitInsert(E->getSourceRange().getBegin(), getARCMTMacroName());
457f4a2713aSLionel Sambuc   } else
458f4a2713aSLionel Sambuc     commitRemove(S->getSourceRange());
459f4a2713aSLionel Sambuc 
460f4a2713aSLionel Sambuc   StmtRemovals.insert(S);
461f4a2713aSLionel Sambuc }
462f4a2713aSLionel Sambuc 
commitReplace(SourceRange range,SourceRange replacementRange)463f4a2713aSLionel Sambuc void TransformActionsImpl::commitReplace(SourceRange range,
464f4a2713aSLionel Sambuc                                          SourceRange replacementRange) {
465f4a2713aSLionel Sambuc   RangeComparison comp = CharRange::compare(replacementRange, range,
466f4a2713aSLionel Sambuc                                                Ctx.getSourceManager(), PP);
467f4a2713aSLionel Sambuc   assert(comp == Range_Contained);
468f4a2713aSLionel Sambuc   if (comp != Range_Contained)
469f4a2713aSLionel Sambuc     return; // Although we asserted, be extra safe for release build.
470f4a2713aSLionel Sambuc   if (range.getBegin() != replacementRange.getBegin())
471f4a2713aSLionel Sambuc     addRemoval(CharSourceRange::getCharRange(range.getBegin(),
472f4a2713aSLionel Sambuc                                              replacementRange.getBegin()));
473f4a2713aSLionel Sambuc   if (replacementRange.getEnd() != range.getEnd())
474f4a2713aSLionel Sambuc     addRemoval(CharSourceRange::getTokenRange(
475f4a2713aSLionel Sambuc                                   getLocForEndOfToken(replacementRange.getEnd(),
476f4a2713aSLionel Sambuc                                                       Ctx.getSourceManager(), PP),
477f4a2713aSLionel Sambuc                                   range.getEnd()));
478f4a2713aSLionel Sambuc }
commitReplaceText(SourceLocation loc,StringRef text,StringRef replacementText)479f4a2713aSLionel Sambuc void TransformActionsImpl::commitReplaceText(SourceLocation loc,
480f4a2713aSLionel Sambuc                                              StringRef text,
481f4a2713aSLionel Sambuc                                              StringRef replacementText) {
482f4a2713aSLionel Sambuc   SourceManager &SM = Ctx.getSourceManager();
483f4a2713aSLionel Sambuc   loc = SM.getExpansionLoc(loc);
484f4a2713aSLionel Sambuc   // canReplaceText already checked if loc points at text.
485f4a2713aSLionel Sambuc   SourceLocation afterText = loc.getLocWithOffset(text.size());
486f4a2713aSLionel Sambuc 
487f4a2713aSLionel Sambuc   addRemoval(CharSourceRange::getCharRange(loc, afterText));
488f4a2713aSLionel Sambuc   commitInsert(loc, replacementText);
489f4a2713aSLionel Sambuc }
490f4a2713aSLionel Sambuc 
commitIncreaseIndentation(SourceRange range,SourceLocation parentIndent)491f4a2713aSLionel Sambuc void TransformActionsImpl::commitIncreaseIndentation(SourceRange range,
492f4a2713aSLionel Sambuc                                                   SourceLocation parentIndent) {
493f4a2713aSLionel Sambuc   SourceManager &SM = Ctx.getSourceManager();
494f4a2713aSLionel Sambuc   IndentationRanges.push_back(
495f4a2713aSLionel Sambuc                  std::make_pair(CharRange(CharSourceRange::getTokenRange(range),
496f4a2713aSLionel Sambuc                                           SM, PP),
497f4a2713aSLionel Sambuc                                 SM.getExpansionLoc(parentIndent)));
498f4a2713aSLionel Sambuc }
499f4a2713aSLionel Sambuc 
commitClearDiagnostic(ArrayRef<unsigned> IDs,SourceRange range)500f4a2713aSLionel Sambuc void TransformActionsImpl::commitClearDiagnostic(ArrayRef<unsigned> IDs,
501f4a2713aSLionel Sambuc                                                  SourceRange range) {
502f4a2713aSLionel Sambuc   CapturedDiags.clearDiagnostic(IDs, range);
503f4a2713aSLionel Sambuc }
504f4a2713aSLionel Sambuc 
addInsertion(SourceLocation loc,StringRef text)505f4a2713aSLionel Sambuc void TransformActionsImpl::addInsertion(SourceLocation loc, StringRef text) {
506f4a2713aSLionel Sambuc   SourceManager &SM = Ctx.getSourceManager();
507f4a2713aSLionel Sambuc   loc = SM.getExpansionLoc(loc);
508f4a2713aSLionel Sambuc   for (std::list<CharRange>::reverse_iterator
509f4a2713aSLionel Sambuc          I = Removals.rbegin(), E = Removals.rend(); I != E; ++I) {
510f4a2713aSLionel Sambuc     if (!SM.isBeforeInTranslationUnit(loc, I->End))
511f4a2713aSLionel Sambuc       break;
512f4a2713aSLionel Sambuc     if (I->Begin.isBeforeInTranslationUnitThan(loc))
513f4a2713aSLionel Sambuc       return;
514f4a2713aSLionel Sambuc   }
515f4a2713aSLionel Sambuc 
516f4a2713aSLionel Sambuc   Inserts[FullSourceLoc(loc, SM)].push_back(text);
517f4a2713aSLionel Sambuc }
518f4a2713aSLionel Sambuc 
addRemoval(CharSourceRange range)519f4a2713aSLionel Sambuc void TransformActionsImpl::addRemoval(CharSourceRange range) {
520f4a2713aSLionel Sambuc   CharRange newRange(range, Ctx.getSourceManager(), PP);
521f4a2713aSLionel Sambuc   if (newRange.Begin == newRange.End)
522f4a2713aSLionel Sambuc     return;
523f4a2713aSLionel Sambuc 
524f4a2713aSLionel Sambuc   Inserts.erase(Inserts.upper_bound(newRange.Begin),
525f4a2713aSLionel Sambuc                 Inserts.lower_bound(newRange.End));
526f4a2713aSLionel Sambuc 
527f4a2713aSLionel Sambuc   std::list<CharRange>::iterator I = Removals.end();
528f4a2713aSLionel Sambuc   while (I != Removals.begin()) {
529f4a2713aSLionel Sambuc     std::list<CharRange>::iterator RI = I;
530f4a2713aSLionel Sambuc     --RI;
531f4a2713aSLionel Sambuc     RangeComparison comp = newRange.compareWith(*RI);
532f4a2713aSLionel Sambuc     switch (comp) {
533f4a2713aSLionel Sambuc     case Range_Before:
534f4a2713aSLionel Sambuc       --I;
535f4a2713aSLionel Sambuc       break;
536f4a2713aSLionel Sambuc     case Range_After:
537f4a2713aSLionel Sambuc       Removals.insert(I, newRange);
538f4a2713aSLionel Sambuc       return;
539f4a2713aSLionel Sambuc     case Range_Contained:
540f4a2713aSLionel Sambuc       return;
541f4a2713aSLionel Sambuc     case Range_Contains:
542f4a2713aSLionel Sambuc       RI->End = newRange.End;
543f4a2713aSLionel Sambuc     case Range_ExtendsBegin:
544f4a2713aSLionel Sambuc       newRange.End = RI->End;
545f4a2713aSLionel Sambuc       Removals.erase(RI);
546f4a2713aSLionel Sambuc       break;
547f4a2713aSLionel Sambuc     case Range_ExtendsEnd:
548f4a2713aSLionel Sambuc       RI->End = newRange.End;
549f4a2713aSLionel Sambuc       return;
550f4a2713aSLionel Sambuc     }
551f4a2713aSLionel Sambuc   }
552f4a2713aSLionel Sambuc 
553f4a2713aSLionel Sambuc   Removals.insert(Removals.begin(), newRange);
554f4a2713aSLionel Sambuc }
555f4a2713aSLionel Sambuc 
applyRewrites(TransformActions::RewriteReceiver & receiver)556f4a2713aSLionel Sambuc void TransformActionsImpl::applyRewrites(
557f4a2713aSLionel Sambuc                                   TransformActions::RewriteReceiver &receiver) {
558f4a2713aSLionel Sambuc   for (InsertsMap::iterator I = Inserts.begin(), E = Inserts.end(); I!=E; ++I) {
559f4a2713aSLionel Sambuc     SourceLocation loc = I->first;
560f4a2713aSLionel Sambuc     for (TextsVec::iterator
561f4a2713aSLionel Sambuc            TI = I->second.begin(), TE = I->second.end(); TI != TE; ++TI) {
562f4a2713aSLionel Sambuc       receiver.insert(loc, *TI);
563f4a2713aSLionel Sambuc     }
564f4a2713aSLionel Sambuc   }
565f4a2713aSLionel Sambuc 
566f4a2713aSLionel Sambuc   for (std::vector<std::pair<CharRange, SourceLocation> >::iterator
567f4a2713aSLionel Sambuc        I = IndentationRanges.begin(), E = IndentationRanges.end(); I!=E; ++I) {
568f4a2713aSLionel Sambuc     CharSourceRange range = CharSourceRange::getCharRange(I->first.Begin,
569f4a2713aSLionel Sambuc                                                           I->first.End);
570f4a2713aSLionel Sambuc     receiver.increaseIndentation(range, I->second);
571f4a2713aSLionel Sambuc   }
572f4a2713aSLionel Sambuc 
573f4a2713aSLionel Sambuc   for (std::list<CharRange>::iterator
574f4a2713aSLionel Sambuc          I = Removals.begin(), E = Removals.end(); I != E; ++I) {
575f4a2713aSLionel Sambuc     CharSourceRange range = CharSourceRange::getCharRange(I->Begin, I->End);
576f4a2713aSLionel Sambuc     receiver.remove(range);
577f4a2713aSLionel Sambuc   }
578f4a2713aSLionel Sambuc }
579f4a2713aSLionel Sambuc 
580f4a2713aSLionel Sambuc /// \brief Stores text passed to the transformation methods to keep the string
581f4a2713aSLionel Sambuc /// "alive". Since the vast majority of text will be the same, we also unique
582f4a2713aSLionel Sambuc /// the strings using a StringMap.
getUniqueText(StringRef text)583f4a2713aSLionel Sambuc StringRef TransformActionsImpl::getUniqueText(StringRef text) {
584*0a6a1f1dSLionel Sambuc   return UniqueText.insert(std::make_pair(text, false)).first->first();
585f4a2713aSLionel Sambuc }
586f4a2713aSLionel Sambuc 
587f4a2713aSLionel Sambuc /// \brief Computes the source location just past the end of the token at
588f4a2713aSLionel Sambuc /// the given source location. If the location points at a macro, the whole
589f4a2713aSLionel Sambuc /// macro expansion is skipped.
getLocForEndOfToken(SourceLocation loc,SourceManager & SM,Preprocessor & PP)590f4a2713aSLionel Sambuc SourceLocation TransformActionsImpl::getLocForEndOfToken(SourceLocation loc,
591f4a2713aSLionel Sambuc                                                          SourceManager &SM,
592f4a2713aSLionel Sambuc                                                          Preprocessor &PP) {
593f4a2713aSLionel Sambuc   if (loc.isMacroID())
594f4a2713aSLionel Sambuc     loc = SM.getExpansionRange(loc).second;
595f4a2713aSLionel Sambuc   return PP.getLocForEndOfToken(loc);
596f4a2713aSLionel Sambuc }
597f4a2713aSLionel Sambuc 
~RewriteReceiver()598f4a2713aSLionel Sambuc TransformActions::RewriteReceiver::~RewriteReceiver() { }
599f4a2713aSLionel Sambuc 
TransformActions(DiagnosticsEngine & diag,CapturedDiagList & capturedDiags,ASTContext & ctx,Preprocessor & PP)600f4a2713aSLionel Sambuc TransformActions::TransformActions(DiagnosticsEngine &diag,
601f4a2713aSLionel Sambuc                                    CapturedDiagList &capturedDiags,
602f4a2713aSLionel Sambuc                                    ASTContext &ctx, Preprocessor &PP)
603*0a6a1f1dSLionel Sambuc     : Diags(diag), CapturedDiags(capturedDiags) {
604f4a2713aSLionel Sambuc   Impl = new TransformActionsImpl(capturedDiags, ctx, PP);
605f4a2713aSLionel Sambuc }
606f4a2713aSLionel Sambuc 
~TransformActions()607f4a2713aSLionel Sambuc TransformActions::~TransformActions() {
608f4a2713aSLionel Sambuc   delete static_cast<TransformActionsImpl*>(Impl);
609f4a2713aSLionel Sambuc }
610f4a2713aSLionel Sambuc 
startTransaction()611f4a2713aSLionel Sambuc void TransformActions::startTransaction() {
612f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->startTransaction();
613f4a2713aSLionel Sambuc }
614f4a2713aSLionel Sambuc 
commitTransaction()615f4a2713aSLionel Sambuc bool TransformActions::commitTransaction() {
616f4a2713aSLionel Sambuc   return static_cast<TransformActionsImpl*>(Impl)->commitTransaction();
617f4a2713aSLionel Sambuc }
618f4a2713aSLionel Sambuc 
abortTransaction()619f4a2713aSLionel Sambuc void TransformActions::abortTransaction() {
620f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->abortTransaction();
621f4a2713aSLionel Sambuc }
622f4a2713aSLionel Sambuc 
623f4a2713aSLionel Sambuc 
insert(SourceLocation loc,StringRef text)624f4a2713aSLionel Sambuc void TransformActions::insert(SourceLocation loc, StringRef text) {
625f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->insert(loc, text);
626f4a2713aSLionel Sambuc }
627f4a2713aSLionel Sambuc 
insertAfterToken(SourceLocation loc,StringRef text)628f4a2713aSLionel Sambuc void TransformActions::insertAfterToken(SourceLocation loc,
629f4a2713aSLionel Sambuc                                         StringRef text) {
630f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->insertAfterToken(loc, text);
631f4a2713aSLionel Sambuc }
632f4a2713aSLionel Sambuc 
remove(SourceRange range)633f4a2713aSLionel Sambuc void TransformActions::remove(SourceRange range) {
634f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->remove(range);
635f4a2713aSLionel Sambuc }
636f4a2713aSLionel Sambuc 
removeStmt(Stmt * S)637f4a2713aSLionel Sambuc void TransformActions::removeStmt(Stmt *S) {
638f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->removeStmt(S);
639f4a2713aSLionel Sambuc }
640f4a2713aSLionel Sambuc 
replace(SourceRange range,StringRef text)641f4a2713aSLionel Sambuc void TransformActions::replace(SourceRange range, StringRef text) {
642f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->replace(range, text);
643f4a2713aSLionel Sambuc }
644f4a2713aSLionel Sambuc 
replace(SourceRange range,SourceRange replacementRange)645f4a2713aSLionel Sambuc void TransformActions::replace(SourceRange range,
646f4a2713aSLionel Sambuc                                SourceRange replacementRange) {
647f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->replace(range, replacementRange);
648f4a2713aSLionel Sambuc }
649f4a2713aSLionel Sambuc 
replaceStmt(Stmt * S,StringRef text)650f4a2713aSLionel Sambuc void TransformActions::replaceStmt(Stmt *S, StringRef text) {
651f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->replaceStmt(S, text);
652f4a2713aSLionel Sambuc }
653f4a2713aSLionel Sambuc 
replaceText(SourceLocation loc,StringRef text,StringRef replacementText)654f4a2713aSLionel Sambuc void TransformActions::replaceText(SourceLocation loc, StringRef text,
655f4a2713aSLionel Sambuc                                    StringRef replacementText) {
656f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->replaceText(loc, text,
657f4a2713aSLionel Sambuc                                                         replacementText);
658f4a2713aSLionel Sambuc }
659f4a2713aSLionel Sambuc 
increaseIndentation(SourceRange range,SourceLocation parentIndent)660f4a2713aSLionel Sambuc void TransformActions::increaseIndentation(SourceRange range,
661f4a2713aSLionel Sambuc                                            SourceLocation parentIndent) {
662f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->increaseIndentation(range,
663f4a2713aSLionel Sambuc                                                                 parentIndent);
664f4a2713aSLionel Sambuc }
665f4a2713aSLionel Sambuc 
clearDiagnostic(ArrayRef<unsigned> IDs,SourceRange range)666f4a2713aSLionel Sambuc bool TransformActions::clearDiagnostic(ArrayRef<unsigned> IDs,
667f4a2713aSLionel Sambuc                                        SourceRange range) {
668f4a2713aSLionel Sambuc   return static_cast<TransformActionsImpl*>(Impl)->clearDiagnostic(IDs, range);
669f4a2713aSLionel Sambuc }
670f4a2713aSLionel Sambuc 
applyRewrites(RewriteReceiver & receiver)671f4a2713aSLionel Sambuc void TransformActions::applyRewrites(RewriteReceiver &receiver) {
672f4a2713aSLionel Sambuc   static_cast<TransformActionsImpl*>(Impl)->applyRewrites(receiver);
673f4a2713aSLionel Sambuc }
674f4a2713aSLionel Sambuc 
report(SourceLocation loc,unsigned diagId,SourceRange range)675*0a6a1f1dSLionel Sambuc DiagnosticBuilder TransformActions::report(SourceLocation loc, unsigned diagId,
676f4a2713aSLionel Sambuc                                            SourceRange range) {
677f4a2713aSLionel Sambuc   assert(!static_cast<TransformActionsImpl *>(Impl)->isInTransaction() &&
678f4a2713aSLionel Sambuc          "Errors should be emitted out of a transaction");
679*0a6a1f1dSLionel Sambuc   return Diags.Report(loc, diagId) << range;
680f4a2713aSLionel Sambuc }
681f4a2713aSLionel Sambuc 
reportError(StringRef message,SourceLocation loc,SourceRange range)682*0a6a1f1dSLionel Sambuc void TransformActions::reportError(StringRef message, SourceLocation loc,
683f4a2713aSLionel Sambuc                                    SourceRange range) {
684*0a6a1f1dSLionel Sambuc   report(loc, diag::err_mt_message, range) << message;
685f4a2713aSLionel Sambuc }
686f4a2713aSLionel Sambuc 
reportWarning(StringRef message,SourceLocation loc,SourceRange range)687*0a6a1f1dSLionel Sambuc void TransformActions::reportWarning(StringRef message, SourceLocation loc,
688f4a2713aSLionel Sambuc                                      SourceRange range) {
689*0a6a1f1dSLionel Sambuc   report(loc, diag::warn_mt_message, range) << message;
690*0a6a1f1dSLionel Sambuc }
691f4a2713aSLionel Sambuc 
reportNote(StringRef message,SourceLocation loc,SourceRange range)692*0a6a1f1dSLionel Sambuc void TransformActions::reportNote(StringRef message, SourceLocation loc,
693*0a6a1f1dSLionel Sambuc                                   SourceRange range) {
694*0a6a1f1dSLionel Sambuc   report(loc, diag::note_mt_message, range) << message;
695f4a2713aSLionel Sambuc }
696