14#include "llvm/ADT/DenseSet.h"
28class TransformActionsImpl {
36 Act_Insert, Act_InsertAfterToken,
37 Act_Remove, Act_RemoveStmt,
38 Act_Replace, Act_ReplaceText,
39 Act_IncreaseIndentation,
47 StringRef Text1, Text2;
52 std::vector<ActionData> CachedActions;
54 enum RangeComparison {
69 assert(beginLoc.
isValid() && endLoc.isValid());
70 if (
range.isTokenRange()) {
72 End =
FullSourceLoc(getLocForEndOfToken(endLoc, srcMgr, PP), srcMgr);
80 RangeComparison compareWith(
const CharRange &RHS)
const {
81 if (End.isBeforeInTranslationUnitThan(RHS.Begin))
83 if (RHS.End.isBeforeInTranslationUnitThan(Begin))
85 if (!
Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
86 !RHS.End.isBeforeInTranslationUnitThan(End))
87 return Range_Contained;
88 if (
Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
89 RHS.End.isBeforeInTranslationUnitThan(End))
90 return Range_Contains;
91 if (
Begin.isBeforeInTranslationUnitThan(RHS.Begin))
92 return Range_ExtendsBegin;
94 return Range_ExtendsEnd;
106 typedef std::map<FullSourceLoc, TextsVec, FullSourceLoc::BeforeThanCompare>
111 std::list<CharRange> Removals;
113 llvm::DenseSet<Stmt *> StmtRemovals;
115 std::vector<std::pair<CharRange, SourceLocation> > IndentationRanges;
118 llvm::StringMap<bool> UniqueText;
123 : CapturedDiags(capturedDiags), Ctx(ctx), PP(PP), IsInTransaction(
false) { }
127 void startTransaction();
128 bool commitTransaction();
129 void abortTransaction();
131 bool isInTransaction()
const {
return IsInTransaction; }
136 void removeStmt(
Stmt *S);
139 void replaceStmt(
Stmt *S, StringRef text);
141 StringRef replacementText);
159 void commitRemoveStmt(
Stmt *S);
162 StringRef replacementText);
172 StringRef getUniqueText(StringRef text);
183void TransformActionsImpl::startTransaction() {
184 assert(!IsInTransaction &&
185 "Cannot start a transaction in the middle of another one");
186 IsInTransaction =
true;
189bool TransformActionsImpl::commitTransaction() {
190 assert(IsInTransaction &&
"No transaction started");
192 if (CachedActions.empty()) {
193 IsInTransaction =
false;
198 bool AllActionsPossible =
true;
199 for (
unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
200 ActionData &act = CachedActions[i];
203 if (!canInsert(act.Loc))
204 AllActionsPossible =
false;
206 case Act_InsertAfterToken:
207 if (!canInsertAfterToken(act.Loc))
208 AllActionsPossible =
false;
211 if (!canRemoveRange(act.R1))
212 AllActionsPossible =
false;
216 if (!canRemoveRange(act.S->getSourceRange()))
217 AllActionsPossible =
false;
220 if (!canReplaceRange(act.R1, act.R2))
221 AllActionsPossible =
false;
223 case Act_ReplaceText:
224 if (!canReplaceText(act.Loc, act.Text1))
225 AllActionsPossible =
false;
227 case Act_IncreaseIndentation:
230 case Act_ClearDiagnostic:
234 if (!AllActionsPossible)
238 if (!AllActionsPossible) {
243 for (
unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
244 ActionData &act = CachedActions[i];
247 commitInsert(act.Loc, act.Text1);
249 case Act_InsertAfterToken:
250 commitInsertAfterToken(act.Loc, act.Text1);
253 commitRemove(act.R1);
256 commitRemoveStmt(act.S);
259 commitReplace(act.R1, act.R2);
261 case Act_ReplaceText:
262 commitReplaceText(act.Loc, act.Text1, act.Text2);
264 case Act_IncreaseIndentation:
265 commitIncreaseIndentation(act.R1, act.Loc);
267 case Act_ClearDiagnostic:
268 commitClearDiagnostic(act.DiagIDs, act.R1);
273 CachedActions.clear();
274 IsInTransaction =
false;
278void TransformActionsImpl::abortTransaction() {
279 assert(IsInTransaction &&
"No transaction started");
280 CachedActions.clear();
281 IsInTransaction =
false;
284void TransformActionsImpl::insert(
SourceLocation loc, StringRef text) {
285 assert(IsInTransaction &&
"Actions only allowed during a transaction");
286 text = getUniqueText(text);
288 data.Kind = Act_Insert;
291 CachedActions.push_back(data);
294void TransformActionsImpl::insertAfterToken(
SourceLocation loc, StringRef text) {
295 assert(IsInTransaction &&
"Actions only allowed during a transaction");
296 text = getUniqueText(text);
298 data.Kind = Act_InsertAfterToken;
301 CachedActions.push_back(data);
304void TransformActionsImpl::remove(
SourceRange range) {
305 assert(IsInTransaction &&
"Actions only allowed during a transaction");
307 data.Kind = Act_Remove;
309 CachedActions.push_back(data);
312void TransformActionsImpl::removeStmt(
Stmt *S) {
313 assert(IsInTransaction &&
"Actions only allowed during a transaction");
315 data.Kind = Act_RemoveStmt;
316 if (
auto *
E = dyn_cast<Expr>(S))
319 CachedActions.push_back(data);
322void TransformActionsImpl::replace(
SourceRange range, StringRef text) {
323 assert(IsInTransaction &&
"Actions only allowed during a transaction");
324 text = getUniqueText(text);
326 insert(
range.getBegin(), text);
329void TransformActionsImpl::replace(
SourceRange range,
331 assert(IsInTransaction &&
"Actions only allowed during a transaction");
333 data.Kind = Act_Replace;
335 data.R2 = replacementRange;
336 CachedActions.push_back(data);
339void TransformActionsImpl::replaceText(
SourceLocation loc, StringRef text,
340 StringRef replacementText) {
341 text = getUniqueText(text);
342 replacementText = getUniqueText(replacementText);
344 data.Kind = Act_ReplaceText;
347 data.Text2 = replacementText;
348 CachedActions.push_back(data);
351void TransformActionsImpl::replaceStmt(
Stmt *S, StringRef text) {
352 assert(IsInTransaction &&
"Actions only allowed during a transaction");
353 text = getUniqueText(text);
354 insert(S->getBeginLoc(), text);
358void TransformActionsImpl::increaseIndentation(
SourceRange range,
360 if (
range.isInvalid())
return;
361 assert(IsInTransaction &&
"Actions only allowed during a transaction");
363 data.Kind = Act_IncreaseIndentation;
365 data.Loc = parentIndent;
366 CachedActions.push_back(data);
371 assert(IsInTransaction &&
"Actions only allowed during a transaction");
376 data.Kind = Act_ClearDiagnostic;
378 data.DiagIDs.append(IDs.begin(), IDs.end());
379 CachedActions.push_back(data);
388 if (
SM.isInSystemHeader(
SM.getExpansionLoc(loc)))
396bool TransformActionsImpl::canInsertAfterToken(
SourceLocation loc) {
401 if (
SM.isInSystemHeader(
SM.getExpansionLoc(loc)))
409bool TransformActionsImpl::canRemoveRange(
SourceRange range) {
410 return canInsert(
range.getBegin()) && canInsertAfterToken(
range.getEnd());
413bool TransformActionsImpl::canReplaceRange(
SourceRange range,
415 return canRemoveRange(range) && canRemoveRange(replacementRange);
418bool TransformActionsImpl::canReplaceText(
SourceLocation loc, StringRef text) {
423 loc =
SM.getExpansionLoc(loc);
426 std::pair<FileID, unsigned> locInfo =
SM.getDecomposedLoc(loc);
429 bool invalidTemp =
false;
430 StringRef file =
SM.getBufferData(locInfo.first, &invalidTemp);
434 return file.substr(locInfo.second).starts_with(text);
437void TransformActionsImpl::commitInsert(
SourceLocation loc, StringRef text) {
438 addInsertion(loc, text);
441void TransformActionsImpl::commitInsertAfterToken(
SourceLocation loc,
446void TransformActionsImpl::commitRemove(
SourceRange range) {
450void TransformActionsImpl::commitRemoveStmt(
Stmt *S) {
452 if (StmtRemovals.count(S))
455 if (
Expr *
E = dyn_cast<Expr>(S)) {
459 commitRemove(S->getSourceRange());
461 StmtRemovals.insert(S);
464void TransformActionsImpl::commitReplace(
SourceRange range,
466 RangeComparison comp = CharRange::compare(replacementRange, range,
468 assert(comp == Range_Contained);
469 if (comp != Range_Contained)
476 getLocForEndOfToken(replacementRange.
getEnd(),
482 StringRef replacementText) {
484 loc =
SM.getExpansionLoc(loc);
489 commitInsert(loc, replacementText);
492void TransformActionsImpl::commitIncreaseIndentation(
SourceRange range,
495 IndentationRanges.push_back(
498 SM.getExpansionLoc(parentIndent)));
506void TransformActionsImpl::addInsertion(
SourceLocation loc, StringRef text) {
508 loc =
SM.getExpansionLoc(loc);
509 for (
const CharRange &I : llvm::reverse(Removals)) {
510 if (!
SM.isBeforeInTranslationUnit(loc, I.End))
512 if (I.Begin.isBeforeInTranslationUnitThan(loc))
521 if (newRange.Begin == newRange.End)
524 Inserts.erase(Inserts.upper_bound(newRange.Begin),
525 Inserts.lower_bound(newRange.End));
527 std::list<CharRange>::iterator I = Removals.end();
528 while (I != Removals.begin()) {
529 std::list<CharRange>::iterator RI = I;
531 RangeComparison comp = newRange.compareWith(*RI);
537 Removals.insert(I, newRange);
539 case Range_Contained:
542 RI->End = newRange.End;
544 case Range_ExtendsBegin:
545 newRange.End = RI->End;
548 case Range_ExtendsEnd:
549 RI->End = newRange.End;
554 Removals.insert(Removals.begin(), newRange);
557void TransformActionsImpl::applyRewrites(
559 for (InsertsMap::iterator I = Inserts.begin(),
E = Inserts.end(); I!=
E; ++I) {
561 for (TextsVec::iterator
562 TI = I->second.begin(), TE = I->second.end(); TI != TE; ++TI) {
563 receiver.
insert(loc, *TI);
567 for (std::vector<std::pair<CharRange, SourceLocation> >::iterator
568 I = IndentationRanges.begin(),
E = IndentationRanges.end(); I!=
E; ++I) {
574 for (std::list<CharRange>::iterator
575 I = Removals.begin(),
E = Removals.end(); I !=
E; ++I) {
584StringRef TransformActionsImpl::getUniqueText(StringRef text) {
585 return UniqueText.insert(std::make_pair(text,
false)).first->first();
608 : Diags(diag), CapturedDiags(capturedDiags) {
609 Impl =
new TransformActionsImpl(capturedDiags, ctx, PP);
613 delete static_cast<TransformActionsImpl*
>(Impl);
630 static_cast<TransformActionsImpl*
>(Impl)->
insert(loc, text);
639 static_cast<TransformActionsImpl*
>(Impl)->
remove(range);
643 static_cast<TransformActionsImpl*
>(Impl)->
removeStmt(S);
647 static_cast<TransformActionsImpl*
>(Impl)->
replace(range, text);
652 static_cast<TransformActionsImpl*
>(Impl)->
replace(range, replacementRange);
656 static_cast<TransformActionsImpl*
>(Impl)->
replaceStmt(S, text);
660 StringRef replacementText) {
661 static_cast<TransformActionsImpl*
>(Impl)->
replaceText(loc, text,
673 return static_cast<TransformActionsImpl*
>(Impl)->
clearDiagnostic(IDs, range);
677 static_cast<TransformActionsImpl*
>(Impl)->
applyRewrites(receiver);
682 assert(!
static_cast<TransformActionsImpl *
>(Impl)->isInTransaction() &&
683 "Errors should be emitted out of a transaction");
684 return Diags.
Report(loc, diagId) << range;
689 report(loc, diag::err_mt_message, range) << message;
694 report(loc, diag::warn_mt_message, range) << message;
699 report(loc, diag::note_mt_message, range) << message;
Defines the clang::ASTContext interface.
static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y)
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
Represents a character-granular source range.
static CharSourceRange getCharRange(SourceRange R)
static CharSourceRange getTokenRange(SourceRange R)
SourceLocation getEnd() const
A little helper class used to produce diagnostics.
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
This represents one expression.
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
A SourceLocation and its associated SourceManager.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
bool isAtStartOfMacroExpansion(SourceLocation loc, SourceLocation *MacroBegin=nullptr) const
Returns true if the given MacroID location points at the first token of the macro expansion.
bool isAtEndOfMacroExpansion(SourceLocation loc, SourceLocation *MacroEnd=nullptr) const
Returns true if the given MacroID location points at the last token of the macro expansion.
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
bool clearDiagnostic(ArrayRef< unsigned > IDs, SourceRange range)
bool hasDiagnostic(ArrayRef< unsigned > IDs, SourceRange range) const
static StringRef getARCMTMacroName()
The JSON file list parser is used to communicate input to InstallAPI.