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