John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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/Expr.h" |
| 12 | #include "clang/Lex/Preprocessor.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
| 14 | #include "llvm/ADT/DenseSet.h" |
| 15 | #include <map> |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 16 | using namespace clang; |
| 17 | using namespace arcmt; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | /// \brief 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; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 47 | StringRef Text1, Text2; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 48 | Stmt *S; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 49 | SmallVector<unsigned, 2> DiagIDs; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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 | /// \brief A range to remove. It is a character range. |
| 64 | struct CharRange { |
| 65 | FullSourceLoc Begin, End; |
| 66 | |
| 67 | CharRange(CharSourceRange range, SourceManager &srcMgr, Preprocessor &PP) { |
| 68 | SourceLocation beginLoc = range.getBegin(), endLoc = range.getEnd(); |
| 69 | assert(beginLoc.isValid() && endLoc.isValid()); |
| 70 | if (range.isTokenRange()) { |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 71 | Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 72 | End = FullSourceLoc(getLocForEndOfToken(endLoc, srcMgr, PP), srcMgr); |
| 73 | } else { |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 74 | Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr); |
| 75 | End = FullSourceLoc(srcMgr.getExpansionLoc(endLoc), srcMgr); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 76 | } |
| 77 | assert(Begin.isValid() && End.isValid()); |
| 78 | } |
| 79 | |
| 80 | RangeComparison compareWith(const CharRange &RHS) const { |
| 81 | if (End.isBeforeInTranslationUnitThan(RHS.Begin)) |
| 82 | return Range_Before; |
| 83 | if (RHS.End.isBeforeInTranslationUnitThan(Begin)) |
| 84 | return Range_After; |
| 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; |
| 93 | else |
| 94 | return Range_ExtendsEnd; |
| 95 | } |
| 96 | |
| 97 | static RangeComparison compare(SourceRange LHS, SourceRange RHS, |
| 98 | SourceManager &SrcMgr, Preprocessor &PP) { |
| 99 | return CharRange(CharSourceRange::getTokenRange(LHS), SrcMgr, PP) |
| 100 | .compareWith(CharRange(CharSourceRange::getTokenRange(RHS), |
| 101 | SrcMgr, PP)); |
| 102 | } |
| 103 | }; |
| 104 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 105 | typedef SmallVector<StringRef, 2> TextsVec; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 106 | typedef std::map<FullSourceLoc, TextsVec, FullSourceLoc::BeforeThanCompare> |
| 107 | InsertsMap; |
| 108 | InsertsMap Inserts; |
| 109 | /// \brief 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 | /// \brief Keeps text passed to transformation methods. |
| 118 | llvm::StringMap<bool> UniqueText; |
| 119 | |
| 120 | public: |
| 121 | TransformActionsImpl(CapturedDiagList &capturedDiags, |
| 122 | ASTContext &ctx, Preprocessor &PP) |
| 123 | : CapturedDiags(capturedDiags), Ctx(ctx), PP(PP), IsInTransaction(false) { } |
| 124 | |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame^] | 125 | ASTContext &getASTContext() { return Ctx; } |
| 126 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 127 | void startTransaction(); |
| 128 | bool commitTransaction(); |
| 129 | void abortTransaction(); |
| 130 | |
| 131 | bool isInTransaction() const { return IsInTransaction; } |
| 132 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | void insert(SourceLocation loc, StringRef text); |
| 134 | void insertAfterToken(SourceLocation loc, StringRef text); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 135 | void remove(SourceRange range); |
| 136 | void removeStmt(Stmt *S); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 137 | void replace(SourceRange range, StringRef text); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 138 | void replace(SourceRange range, SourceRange replacementRange); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 139 | void replaceStmt(Stmt *S, StringRef text); |
| 140 | void replaceText(SourceLocation loc, StringRef text, |
| 141 | StringRef replacementText); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 142 | void increaseIndentation(SourceRange range, |
| 143 | SourceLocation parentIndent); |
| 144 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 145 | bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 154 | bool canReplaceText(SourceLocation loc, StringRef text); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 161 | void commitReplaceText(SourceLocation loc, StringRef text, |
| 162 | StringRef replacementText); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 163 | void commitIncreaseIndentation(SourceRange range,SourceLocation parentIndent); |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 164 | void commitClearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 165 | |
| 166 | void addRemoval(CharSourceRange range); |
| 167 | void addInsertion(SourceLocation loc, StringRef text); |
| 168 | |
| 169 | /// \brief 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 | /// \brief 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 |
Chandler Carruth | 711474a | 2011-07-15 00:04:31 +0000 | [diff] [blame] | 176 | /// macro expansion is skipped. |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 177 | static SourceLocation getLocForEndOfToken(SourceLocation loc, |
| 178 | SourceManager &SM,Preprocessor &PP); |
| 179 | }; |
| 180 | |
| 181 | } // anonymous namespace |
| 182 | |
| 183 | void TransformActionsImpl::startTransaction() { |
| 184 | assert(!IsInTransaction && |
| 185 | "Cannot start a transaction in the middle of another one"); |
| 186 | IsInTransaction = true; |
| 187 | } |
| 188 | |
| 189 | bool TransformActionsImpl::commitTransaction() { |
| 190 | assert(IsInTransaction && "No transaction started"); |
| 191 | |
| 192 | if (CachedActions.empty()) { |
| 193 | IsInTransaction = false; |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | // Verify that all actions are possible otherwise abort the whole transaction. |
| 198 | bool AllActionsPossible = true; |
| 199 | for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) { |
| 200 | ActionData &act = CachedActions[i]; |
| 201 | switch (act.Kind) { |
| 202 | case Act_Insert: |
| 203 | if (!canInsert(act.Loc)) |
| 204 | AllActionsPossible = false; |
| 205 | break; |
| 206 | case Act_InsertAfterToken: |
| 207 | if (!canInsertAfterToken(act.Loc)) |
| 208 | AllActionsPossible = false; |
| 209 | break; |
| 210 | case Act_Remove: |
| 211 | if (!canRemoveRange(act.R1)) |
| 212 | AllActionsPossible = false; |
| 213 | break; |
| 214 | case Act_RemoveStmt: |
| 215 | assert(act.S); |
| 216 | if (!canRemoveRange(act.S->getSourceRange())) |
| 217 | AllActionsPossible = false; |
| 218 | break; |
| 219 | case Act_Replace: |
| 220 | if (!canReplaceRange(act.R1, act.R2)) |
| 221 | AllActionsPossible = false; |
| 222 | break; |
| 223 | case Act_ReplaceText: |
| 224 | if (!canReplaceText(act.Loc, act.Text1)) |
| 225 | AllActionsPossible = false; |
| 226 | break; |
| 227 | case Act_IncreaseIndentation: |
| 228 | // This is not important, we don't care if it will fail. |
| 229 | break; |
| 230 | case Act_ClearDiagnostic: |
| 231 | // We are just checking source rewrites. |
| 232 | break; |
| 233 | } |
| 234 | if (!AllActionsPossible) |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | if (!AllActionsPossible) { |
| 239 | abortTransaction(); |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) { |
| 244 | ActionData &act = CachedActions[i]; |
| 245 | switch (act.Kind) { |
| 246 | case Act_Insert: |
| 247 | commitInsert(act.Loc, act.Text1); |
| 248 | break; |
| 249 | case Act_InsertAfterToken: |
| 250 | commitInsertAfterToken(act.Loc, act.Text1); |
| 251 | break; |
| 252 | case Act_Remove: |
| 253 | commitRemove(act.R1); |
| 254 | break; |
| 255 | case Act_RemoveStmt: |
| 256 | commitRemoveStmt(act.S); |
| 257 | break; |
| 258 | case Act_Replace: |
| 259 | commitReplace(act.R1, act.R2); |
| 260 | break; |
| 261 | case Act_ReplaceText: |
| 262 | commitReplaceText(act.Loc, act.Text1, act.Text2); |
| 263 | break; |
| 264 | case Act_IncreaseIndentation: |
| 265 | commitIncreaseIndentation(act.R1, act.Loc); |
| 266 | break; |
| 267 | case Act_ClearDiagnostic: |
| 268 | commitClearDiagnostic(act.DiagIDs, act.R1); |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | CachedActions.clear(); |
| 274 | IsInTransaction = false; |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | void TransformActionsImpl::abortTransaction() { |
| 279 | assert(IsInTransaction && "No transaction started"); |
| 280 | CachedActions.clear(); |
| 281 | IsInTransaction = false; |
| 282 | } |
| 283 | |
| 284 | void TransformActionsImpl::insert(SourceLocation loc, StringRef text) { |
| 285 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 286 | text = getUniqueText(text); |
| 287 | ActionData data; |
| 288 | data.Kind = Act_Insert; |
| 289 | data.Loc = loc; |
| 290 | data.Text1 = text; |
| 291 | CachedActions.push_back(data); |
| 292 | } |
| 293 | |
| 294 | void TransformActionsImpl::insertAfterToken(SourceLocation loc, StringRef text) { |
| 295 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 296 | text = getUniqueText(text); |
| 297 | ActionData data; |
| 298 | data.Kind = Act_InsertAfterToken; |
| 299 | data.Loc = loc; |
| 300 | data.Text1 = text; |
| 301 | CachedActions.push_back(data); |
| 302 | } |
| 303 | |
| 304 | void TransformActionsImpl::remove(SourceRange range) { |
| 305 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 306 | ActionData data; |
| 307 | data.Kind = Act_Remove; |
| 308 | data.R1 = range; |
| 309 | CachedActions.push_back(data); |
| 310 | } |
| 311 | |
| 312 | void TransformActionsImpl::removeStmt(Stmt *S) { |
| 313 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 314 | ActionData data; |
| 315 | data.Kind = Act_RemoveStmt; |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 316 | data.S = S->IgnoreImplicit(); // important for uniquing |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 317 | CachedActions.push_back(data); |
| 318 | } |
| 319 | |
| 320 | void TransformActionsImpl::replace(SourceRange range, StringRef text) { |
| 321 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 322 | text = getUniqueText(text); |
| 323 | remove(range); |
| 324 | insert(range.getBegin(), text); |
| 325 | } |
| 326 | |
| 327 | void TransformActionsImpl::replace(SourceRange range, |
| 328 | SourceRange replacementRange) { |
| 329 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 330 | ActionData data; |
| 331 | data.Kind = Act_Replace; |
| 332 | data.R1 = range; |
| 333 | data.R2 = replacementRange; |
| 334 | CachedActions.push_back(data); |
| 335 | } |
| 336 | |
| 337 | void TransformActionsImpl::replaceText(SourceLocation loc, StringRef text, |
| 338 | StringRef replacementText) { |
| 339 | text = getUniqueText(text); |
| 340 | replacementText = getUniqueText(replacementText); |
| 341 | ActionData data; |
| 342 | data.Kind = Act_ReplaceText; |
| 343 | data.Loc = loc; |
| 344 | data.Text1 = text; |
| 345 | data.Text2 = replacementText; |
| 346 | CachedActions.push_back(data); |
| 347 | } |
| 348 | |
| 349 | void TransformActionsImpl::replaceStmt(Stmt *S, StringRef text) { |
| 350 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 351 | text = getUniqueText(text); |
| 352 | insert(S->getLocStart(), text); |
| 353 | removeStmt(S); |
| 354 | } |
| 355 | |
| 356 | void TransformActionsImpl::increaseIndentation(SourceRange range, |
| 357 | SourceLocation parentIndent) { |
| 358 | if (range.isInvalid()) return; |
| 359 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 360 | ActionData data; |
| 361 | data.Kind = Act_IncreaseIndentation; |
| 362 | data.R1 = range; |
| 363 | data.Loc = parentIndent; |
| 364 | CachedActions.push_back(data); |
| 365 | } |
| 366 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 367 | bool TransformActionsImpl::clearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 368 | SourceRange range) { |
| 369 | assert(IsInTransaction && "Actions only allowed during a transaction"); |
| 370 | if (!CapturedDiags.hasDiagnostic(IDs, range)) |
| 371 | return false; |
| 372 | |
| 373 | ActionData data; |
| 374 | data.Kind = Act_ClearDiagnostic; |
| 375 | data.R1 = range; |
| 376 | data.DiagIDs.append(IDs.begin(), IDs.end()); |
| 377 | CachedActions.push_back(data); |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool TransformActionsImpl::canInsert(SourceLocation loc) { |
| 382 | if (loc.isInvalid()) |
| 383 | return false; |
| 384 | |
| 385 | SourceManager &SM = Ctx.getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 386 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 387 | return false; |
| 388 | |
| 389 | if (loc.isFileID()) |
| 390 | return true; |
Chandler Carruth | 433db06 | 2011-07-14 08:20:40 +0000 | [diff] [blame] | 391 | return PP.isAtStartOfMacroExpansion(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) { |
| 395 | if (loc.isInvalid()) |
| 396 | return false; |
| 397 | |
| 398 | SourceManager &SM = Ctx.getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 399 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 400 | return false; |
| 401 | |
| 402 | if (loc.isFileID()) |
| 403 | return true; |
Chandler Carruth | 433db06 | 2011-07-14 08:20:40 +0000 | [diff] [blame] | 404 | return PP.isAtEndOfMacroExpansion(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | bool TransformActionsImpl::canRemoveRange(SourceRange range) { |
| 408 | return canInsert(range.getBegin()) && canInsertAfterToken(range.getEnd()); |
| 409 | } |
| 410 | |
| 411 | bool TransformActionsImpl::canReplaceRange(SourceRange range, |
| 412 | SourceRange replacementRange) { |
| 413 | return canRemoveRange(range) && canRemoveRange(replacementRange); |
| 414 | } |
| 415 | |
| 416 | bool TransformActionsImpl::canReplaceText(SourceLocation loc, StringRef text) { |
| 417 | if (!canInsert(loc)) |
| 418 | return false; |
| 419 | |
| 420 | SourceManager &SM = Ctx.getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 421 | loc = SM.getExpansionLoc(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 422 | |
| 423 | // Break down the source location. |
| 424 | std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); |
| 425 | |
| 426 | // Try to load the file buffer. |
| 427 | bool invalidTemp = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 428 | StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 429 | if (invalidTemp) |
| 430 | return false; |
| 431 | |
| 432 | return file.substr(locInfo.second).startswith(text); |
| 433 | } |
| 434 | |
| 435 | void TransformActionsImpl::commitInsert(SourceLocation loc, StringRef text) { |
| 436 | addInsertion(loc, text); |
| 437 | } |
| 438 | |
| 439 | void TransformActionsImpl::commitInsertAfterToken(SourceLocation loc, |
| 440 | StringRef text) { |
| 441 | addInsertion(getLocForEndOfToken(loc, Ctx.getSourceManager(), PP), text); |
| 442 | } |
| 443 | |
| 444 | void TransformActionsImpl::commitRemove(SourceRange range) { |
| 445 | addRemoval(CharSourceRange::getTokenRange(range)); |
| 446 | } |
| 447 | |
| 448 | void TransformActionsImpl::commitRemoveStmt(Stmt *S) { |
| 449 | assert(S); |
| 450 | if (StmtRemovals.count(S)) |
| 451 | return; // already removed. |
| 452 | |
| 453 | if (Expr *E = dyn_cast<Expr>(S)) { |
| 454 | commitRemove(E->getSourceRange()); |
| 455 | commitInsert(E->getSourceRange().getBegin(), getARCMTMacroName()); |
| 456 | } else |
| 457 | commitRemove(S->getSourceRange()); |
| 458 | |
| 459 | StmtRemovals.insert(S); |
| 460 | } |
| 461 | |
| 462 | void TransformActionsImpl::commitReplace(SourceRange range, |
| 463 | SourceRange replacementRange) { |
| 464 | RangeComparison comp = CharRange::compare(replacementRange, range, |
| 465 | Ctx.getSourceManager(), PP); |
| 466 | assert(comp == Range_Contained); |
| 467 | if (comp != Range_Contained) |
| 468 | return; // Although we asserted, be extra safe for release build. |
| 469 | if (range.getBegin() != replacementRange.getBegin()) |
| 470 | addRemoval(CharSourceRange::getCharRange(range.getBegin(), |
| 471 | replacementRange.getBegin())); |
| 472 | if (replacementRange.getEnd() != range.getEnd()) |
| 473 | addRemoval(CharSourceRange::getTokenRange( |
| 474 | getLocForEndOfToken(replacementRange.getEnd(), |
| 475 | Ctx.getSourceManager(), PP), |
| 476 | range.getEnd())); |
| 477 | } |
| 478 | void TransformActionsImpl::commitReplaceText(SourceLocation loc, |
| 479 | StringRef text, |
| 480 | StringRef replacementText) { |
| 481 | SourceManager &SM = Ctx.getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 482 | loc = SM.getExpansionLoc(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 483 | // canReplaceText already checked if loc points at text. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 484 | SourceLocation afterText = loc.getLocWithOffset(text.size()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 485 | |
| 486 | addRemoval(CharSourceRange::getCharRange(loc, afterText)); |
| 487 | commitInsert(loc, replacementText); |
| 488 | } |
| 489 | |
| 490 | void TransformActionsImpl::commitIncreaseIndentation(SourceRange range, |
| 491 | SourceLocation parentIndent) { |
| 492 | SourceManager &SM = Ctx.getSourceManager(); |
| 493 | IndentationRanges.push_back( |
| 494 | std::make_pair(CharRange(CharSourceRange::getTokenRange(range), |
| 495 | SM, PP), |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 496 | SM.getExpansionLoc(parentIndent))); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 499 | void TransformActionsImpl::commitClearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 500 | SourceRange range) { |
| 501 | CapturedDiags.clearDiagnostic(IDs, range); |
| 502 | } |
| 503 | |
| 504 | void TransformActionsImpl::addInsertion(SourceLocation loc, StringRef text) { |
| 505 | SourceManager &SM = Ctx.getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 506 | loc = SM.getExpansionLoc(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 507 | for (std::list<CharRange>::reverse_iterator |
| 508 | I = Removals.rbegin(), E = Removals.rend(); I != E; ++I) { |
| 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 | case Range_ExtendsBegin: |
| 543 | newRange.End = RI->End; |
| 544 | Removals.erase(RI); |
| 545 | break; |
| 546 | case Range_ExtendsEnd: |
| 547 | RI->End = newRange.End; |
| 548 | return; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | Removals.insert(Removals.begin(), newRange); |
| 553 | } |
| 554 | |
| 555 | void TransformActionsImpl::applyRewrites( |
| 556 | TransformActions::RewriteReceiver &receiver) { |
| 557 | for (InsertsMap::iterator I = Inserts.begin(), E = Inserts.end(); I!=E; ++I) { |
| 558 | SourceLocation loc = I->first; |
| 559 | for (TextsVec::iterator |
| 560 | TI = I->second.begin(), TE = I->second.end(); TI != TE; ++TI) { |
| 561 | receiver.insert(loc, *TI); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | for (std::vector<std::pair<CharRange, SourceLocation> >::iterator |
| 566 | I = IndentationRanges.begin(), E = IndentationRanges.end(); I!=E; ++I) { |
| 567 | CharSourceRange range = CharSourceRange::getCharRange(I->first.Begin, |
| 568 | I->first.End); |
| 569 | receiver.increaseIndentation(range, I->second); |
| 570 | } |
| 571 | |
| 572 | for (std::list<CharRange>::iterator |
| 573 | I = Removals.begin(), E = Removals.end(); I != E; ++I) { |
| 574 | CharSourceRange range = CharSourceRange::getCharRange(I->Begin, I->End); |
| 575 | receiver.remove(range); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /// \brief Stores text passed to the transformation methods to keep the string |
| 580 | /// "alive". Since the vast majority of text will be the same, we also unique |
| 581 | /// the strings using a StringMap. |
| 582 | StringRef TransformActionsImpl::getUniqueText(StringRef text) { |
| 583 | llvm::StringMapEntry<bool> &entry = UniqueText.GetOrCreateValue(text); |
| 584 | return entry.getKey(); |
| 585 | } |
| 586 | |
| 587 | /// \brief 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 |
Chandler Carruth | 711474a | 2011-07-15 00:04:31 +0000 | [diff] [blame] | 589 | /// macro expansion is skipped. |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 590 | SourceLocation TransformActionsImpl::getLocForEndOfToken(SourceLocation loc, |
| 591 | SourceManager &SM, |
| 592 | Preprocessor &PP) { |
| 593 | if (loc.isMacroID()) |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 594 | loc = SM.getExpansionRange(loc).second; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 595 | return PP.getLocForEndOfToken(loc); |
| 596 | } |
| 597 | |
| 598 | TransformActions::RewriteReceiver::~RewriteReceiver() { } |
| 599 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 600 | TransformActions::TransformActions(DiagnosticsEngine &diag, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 601 | CapturedDiagList &capturedDiags, |
| 602 | ASTContext &ctx, Preprocessor &PP) |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 603 | : Diags(diag), CapturedDiags(capturedDiags), ReportedErrors(false) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 604 | Impl = new TransformActionsImpl(capturedDiags, ctx, PP); |
| 605 | } |
| 606 | |
| 607 | TransformActions::~TransformActions() { |
| 608 | delete static_cast<TransformActionsImpl*>(Impl); |
| 609 | } |
| 610 | |
| 611 | void TransformActions::startTransaction() { |
| 612 | static_cast<TransformActionsImpl*>(Impl)->startTransaction(); |
| 613 | } |
| 614 | |
| 615 | bool TransformActions::commitTransaction() { |
| 616 | return static_cast<TransformActionsImpl*>(Impl)->commitTransaction(); |
| 617 | } |
| 618 | |
| 619 | void TransformActions::abortTransaction() { |
| 620 | static_cast<TransformActionsImpl*>(Impl)->abortTransaction(); |
| 621 | } |
| 622 | |
| 623 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 624 | void TransformActions::insert(SourceLocation loc, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 625 | static_cast<TransformActionsImpl*>(Impl)->insert(loc, text); |
| 626 | } |
| 627 | |
| 628 | void TransformActions::insertAfterToken(SourceLocation loc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 629 | StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 630 | static_cast<TransformActionsImpl*>(Impl)->insertAfterToken(loc, text); |
| 631 | } |
| 632 | |
| 633 | void TransformActions::remove(SourceRange range) { |
| 634 | static_cast<TransformActionsImpl*>(Impl)->remove(range); |
| 635 | } |
| 636 | |
| 637 | void TransformActions::removeStmt(Stmt *S) { |
| 638 | static_cast<TransformActionsImpl*>(Impl)->removeStmt(S); |
| 639 | } |
| 640 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 641 | void TransformActions::replace(SourceRange range, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 642 | static_cast<TransformActionsImpl*>(Impl)->replace(range, text); |
| 643 | } |
| 644 | |
| 645 | void TransformActions::replace(SourceRange range, |
| 646 | SourceRange replacementRange) { |
| 647 | static_cast<TransformActionsImpl*>(Impl)->replace(range, replacementRange); |
| 648 | } |
| 649 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 650 | void TransformActions::replaceStmt(Stmt *S, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 651 | static_cast<TransformActionsImpl*>(Impl)->replaceStmt(S, text); |
| 652 | } |
| 653 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 654 | void TransformActions::replaceText(SourceLocation loc, StringRef text, |
| 655 | StringRef replacementText) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 656 | static_cast<TransformActionsImpl*>(Impl)->replaceText(loc, text, |
| 657 | replacementText); |
| 658 | } |
| 659 | |
| 660 | void TransformActions::increaseIndentation(SourceRange range, |
| 661 | SourceLocation parentIndent) { |
| 662 | static_cast<TransformActionsImpl*>(Impl)->increaseIndentation(range, |
| 663 | parentIndent); |
| 664 | } |
| 665 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 666 | bool TransformActions::clearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 667 | SourceRange range) { |
| 668 | return static_cast<TransformActionsImpl*>(Impl)->clearDiagnostic(IDs, range); |
| 669 | } |
| 670 | |
| 671 | void TransformActions::applyRewrites(RewriteReceiver &receiver) { |
| 672 | static_cast<TransformActionsImpl*>(Impl)->applyRewrites(receiver); |
| 673 | } |
| 674 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 675 | void TransformActions::reportError(StringRef error, SourceLocation loc, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 676 | SourceRange range) { |
| 677 | assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() && |
| 678 | "Errors should be emitted out of a transaction"); |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame^] | 679 | |
| 680 | SourceManager &SM = static_cast<TransformActionsImpl*>(Impl)-> |
| 681 | getASTContext().getSourceManager(); |
| 682 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
| 683 | return; |
| 684 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 685 | // FIXME: Use a custom category name to distinguish rewriter errors. |
| 686 | std::string rewriteErr = "[rewriter] "; |
| 687 | rewriteErr += error; |
| 688 | unsigned diagID |
| 689 | = Diags.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error, |
| 690 | rewriteErr); |
| 691 | Diags.Report(loc, diagID) << range; |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 692 | ReportedErrors = true; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 695 | void TransformActions::reportNote(StringRef note, SourceLocation loc, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 696 | SourceRange range) { |
| 697 | assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() && |
| 698 | "Errors should be emitted out of a transaction"); |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame^] | 699 | |
| 700 | SourceManager &SM = static_cast<TransformActionsImpl*>(Impl)-> |
| 701 | getASTContext().getSourceManager(); |
| 702 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
| 703 | return; |
| 704 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 705 | // FIXME: Use a custom category name to distinguish rewriter errors. |
| 706 | std::string rewriteNote = "[rewriter] "; |
| 707 | rewriteNote += note; |
| 708 | unsigned diagID |
| 709 | = Diags.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Note, |
| 710 | rewriteNote); |
| 711 | Diags.Report(loc, diagID) << range; |
| 712 | } |