clang 8.0.0

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