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" |
Benjamin Kramer | 478851c | 2012-07-04 17:04:04 +0000 | [diff] [blame^] | 11 | #include "clang/AST/ASTContext.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 12 | #include "clang/AST/Expr.h" |
| 13 | #include "clang/Lex/Preprocessor.h" |
| 14 | #include "clang/Basic/SourceManager.h" |
| 15 | #include "llvm/ADT/DenseSet.h" |
| 16 | #include <map> |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | using namespace arcmt; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | /// \brief 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; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 48 | StringRef Text1, Text2; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 49 | Stmt *S; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 50 | SmallVector<unsigned, 2> DiagIDs; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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 | /// \brief 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()) { |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 72 | Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 73 | End = FullSourceLoc(getLocForEndOfToken(endLoc, srcMgr, PP), srcMgr); |
| 74 | } else { |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 75 | Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr); |
| 76 | End = FullSourceLoc(srcMgr.getExpansionLoc(endLoc), srcMgr); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 106 | typedef SmallVector<StringRef, 2> TextsVec; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 107 | typedef std::map<FullSourceLoc, TextsVec, FullSourceLoc::BeforeThanCompare> |
| 108 | InsertsMap; |
| 109 | InsertsMap Inserts; |
| 110 | /// \brief 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 | /// \brief 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 | |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame] | 126 | ASTContext &getASTContext() { return Ctx; } |
| 127 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 128 | void startTransaction(); |
| 129 | bool commitTransaction(); |
| 130 | void abortTransaction(); |
| 131 | |
| 132 | bool isInTransaction() const { return IsInTransaction; } |
| 133 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 134 | void insert(SourceLocation loc, StringRef text); |
| 135 | void insertAfterToken(SourceLocation loc, StringRef text); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 136 | void remove(SourceRange range); |
| 137 | void removeStmt(Stmt *S); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 138 | void replace(SourceRange range, StringRef text); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 139 | void replace(SourceRange range, SourceRange replacementRange); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 140 | void replaceStmt(Stmt *S, StringRef text); |
| 141 | void replaceText(SourceLocation loc, StringRef text, |
| 142 | StringRef replacementText); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 143 | void increaseIndentation(SourceRange range, |
| 144 | SourceLocation parentIndent); |
| 145 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 146 | bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 155 | bool canReplaceText(SourceLocation loc, StringRef text); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 162 | void commitReplaceText(SourceLocation loc, StringRef text, |
| 163 | StringRef replacementText); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 164 | void commitIncreaseIndentation(SourceRange range,SourceLocation parentIndent); |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 165 | void commitClearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 166 | |
| 167 | void addRemoval(CharSourceRange range); |
| 168 | void addInsertion(SourceLocation loc, StringRef text); |
| 169 | |
| 170 | /// \brief 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 | /// \brief 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 |
Chandler Carruth | 711474a | 2011-07-15 00:04:31 +0000 | [diff] [blame] | 177 | /// macro expansion is skipped. |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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; |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 317 | data.S = S->IgnoreImplicit(); // important for uniquing |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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->getLocStart(), 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 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 368 | bool TransformActionsImpl::clearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 387 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 388 | return false; |
| 389 | |
| 390 | if (loc.isFileID()) |
| 391 | return true; |
Chandler Carruth | 433db06 | 2011-07-14 08:20:40 +0000 | [diff] [blame] | 392 | return PP.isAtStartOfMacroExpansion(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) { |
| 396 | if (loc.isInvalid()) |
| 397 | return false; |
| 398 | |
| 399 | SourceManager &SM = Ctx.getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 400 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 401 | return false; |
| 402 | |
| 403 | if (loc.isFileID()) |
| 404 | return true; |
Chandler Carruth | 433db06 | 2011-07-14 08:20:40 +0000 | [diff] [blame] | 405 | return PP.isAtEndOfMacroExpansion(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 422 | loc = SM.getExpansionLoc(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 429 | StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 483 | loc = SM.getExpansionLoc(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 484 | // canReplaceText already checked if loc points at text. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 485 | SourceLocation afterText = loc.getLocWithOffset(text.size()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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), |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 497 | SM.getExpansionLoc(parentIndent))); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 500 | void TransformActionsImpl::commitClearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 501 | SourceRange range) { |
| 502 | CapturedDiags.clearDiagnostic(IDs, range); |
| 503 | } |
| 504 | |
| 505 | void TransformActionsImpl::addInsertion(SourceLocation loc, StringRef text) { |
| 506 | SourceManager &SM = Ctx.getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 507 | loc = SM.getExpansionLoc(loc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 508 | for (std::list<CharRange>::reverse_iterator |
| 509 | I = Removals.rbegin(), E = Removals.rend(); I != E; ++I) { |
| 510 | if (!SM.isBeforeInTranslationUnit(loc, I->End)) |
| 511 | break; |
| 512 | if (I->Begin.isBeforeInTranslationUnitThan(loc)) |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | Inserts[FullSourceLoc(loc, SM)].push_back(text); |
| 517 | } |
| 518 | |
| 519 | void TransformActionsImpl::addRemoval(CharSourceRange range) { |
| 520 | CharRange newRange(range, Ctx.getSourceManager(), PP); |
| 521 | if (newRange.Begin == newRange.End) |
| 522 | return; |
| 523 | |
| 524 | Inserts.erase(Inserts.upper_bound(newRange.Begin), |
| 525 | Inserts.lower_bound(newRange.End)); |
| 526 | |
| 527 | std::list<CharRange>::iterator I = Removals.end(); |
| 528 | while (I != Removals.begin()) { |
| 529 | std::list<CharRange>::iterator RI = I; |
| 530 | --RI; |
| 531 | RangeComparison comp = newRange.compareWith(*RI); |
| 532 | switch (comp) { |
| 533 | case Range_Before: |
| 534 | --I; |
| 535 | break; |
| 536 | case Range_After: |
| 537 | Removals.insert(I, newRange); |
| 538 | return; |
| 539 | case Range_Contained: |
| 540 | return; |
| 541 | case Range_Contains: |
| 542 | RI->End = newRange.End; |
| 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 | /// \brief 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 | llvm::StringMapEntry<bool> &entry = UniqueText.GetOrCreateValue(text); |
| 585 | return entry.getKey(); |
| 586 | } |
| 587 | |
| 588 | /// \brief 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 |
Chandler Carruth | 711474a | 2011-07-15 00:04:31 +0000 | [diff] [blame] | 590 | /// macro expansion is skipped. |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 591 | SourceLocation TransformActionsImpl::getLocForEndOfToken(SourceLocation loc, |
| 592 | SourceManager &SM, |
| 593 | Preprocessor &PP) { |
| 594 | if (loc.isMacroID()) |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 595 | loc = SM.getExpansionRange(loc).second; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 596 | return PP.getLocForEndOfToken(loc); |
| 597 | } |
| 598 | |
| 599 | TransformActions::RewriteReceiver::~RewriteReceiver() { } |
| 600 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 601 | TransformActions::TransformActions(DiagnosticsEngine &diag, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 602 | CapturedDiagList &capturedDiags, |
| 603 | ASTContext &ctx, Preprocessor &PP) |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 604 | : Diags(diag), CapturedDiags(capturedDiags), ReportedErrors(false) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 605 | Impl = new TransformActionsImpl(capturedDiags, ctx, PP); |
| 606 | } |
| 607 | |
| 608 | TransformActions::~TransformActions() { |
| 609 | delete static_cast<TransformActionsImpl*>(Impl); |
| 610 | } |
| 611 | |
| 612 | void TransformActions::startTransaction() { |
| 613 | static_cast<TransformActionsImpl*>(Impl)->startTransaction(); |
| 614 | } |
| 615 | |
| 616 | bool TransformActions::commitTransaction() { |
| 617 | return static_cast<TransformActionsImpl*>(Impl)->commitTransaction(); |
| 618 | } |
| 619 | |
| 620 | void TransformActions::abortTransaction() { |
| 621 | static_cast<TransformActionsImpl*>(Impl)->abortTransaction(); |
| 622 | } |
| 623 | |
| 624 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 625 | void TransformActions::insert(SourceLocation loc, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 626 | static_cast<TransformActionsImpl*>(Impl)->insert(loc, text); |
| 627 | } |
| 628 | |
| 629 | void TransformActions::insertAfterToken(SourceLocation loc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 630 | StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 631 | static_cast<TransformActionsImpl*>(Impl)->insertAfterToken(loc, text); |
| 632 | } |
| 633 | |
| 634 | void TransformActions::remove(SourceRange range) { |
| 635 | static_cast<TransformActionsImpl*>(Impl)->remove(range); |
| 636 | } |
| 637 | |
| 638 | void TransformActions::removeStmt(Stmt *S) { |
| 639 | static_cast<TransformActionsImpl*>(Impl)->removeStmt(S); |
| 640 | } |
| 641 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 642 | void TransformActions::replace(SourceRange range, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 643 | static_cast<TransformActionsImpl*>(Impl)->replace(range, text); |
| 644 | } |
| 645 | |
| 646 | void TransformActions::replace(SourceRange range, |
| 647 | SourceRange replacementRange) { |
| 648 | static_cast<TransformActionsImpl*>(Impl)->replace(range, replacementRange); |
| 649 | } |
| 650 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 651 | void TransformActions::replaceStmt(Stmt *S, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 652 | static_cast<TransformActionsImpl*>(Impl)->replaceStmt(S, text); |
| 653 | } |
| 654 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 655 | void TransformActions::replaceText(SourceLocation loc, StringRef text, |
| 656 | StringRef replacementText) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 657 | static_cast<TransformActionsImpl*>(Impl)->replaceText(loc, text, |
| 658 | replacementText); |
| 659 | } |
| 660 | |
| 661 | void TransformActions::increaseIndentation(SourceRange range, |
| 662 | SourceLocation parentIndent) { |
| 663 | static_cast<TransformActionsImpl*>(Impl)->increaseIndentation(range, |
| 664 | parentIndent); |
| 665 | } |
| 666 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 667 | bool TransformActions::clearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 668 | SourceRange range) { |
| 669 | return static_cast<TransformActionsImpl*>(Impl)->clearDiagnostic(IDs, range); |
| 670 | } |
| 671 | |
| 672 | void TransformActions::applyRewrites(RewriteReceiver &receiver) { |
| 673 | static_cast<TransformActionsImpl*>(Impl)->applyRewrites(receiver); |
| 674 | } |
| 675 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 676 | void TransformActions::reportError(StringRef error, SourceLocation loc, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 677 | SourceRange range) { |
| 678 | assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() && |
| 679 | "Errors should be emitted out of a transaction"); |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame] | 680 | |
| 681 | SourceManager &SM = static_cast<TransformActionsImpl*>(Impl)-> |
| 682 | getASTContext().getSourceManager(); |
| 683 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
| 684 | return; |
| 685 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 686 | // FIXME: Use a custom category name to distinguish rewriter errors. |
| 687 | std::string rewriteErr = "[rewriter] "; |
| 688 | rewriteErr += error; |
| 689 | unsigned diagID |
| 690 | = Diags.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error, |
| 691 | rewriteErr); |
| 692 | Diags.Report(loc, diagID) << range; |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 693 | ReportedErrors = true; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Fariborz Jahanian | b5c6bab | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 696 | void TransformActions::reportWarning(StringRef warning, SourceLocation loc, |
| 697 | SourceRange range) { |
| 698 | assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() && |
| 699 | "Warning should be emitted out of a transaction"); |
| 700 | |
| 701 | SourceManager &SM = static_cast<TransformActionsImpl*>(Impl)-> |
| 702 | getASTContext().getSourceManager(); |
| 703 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
| 704 | return; |
| 705 | |
| 706 | // FIXME: Use a custom category name to distinguish rewriter errors. |
| 707 | std::string rewriterWarn = "[rewriter] "; |
| 708 | rewriterWarn += warning; |
| 709 | unsigned diagID |
| 710 | = Diags.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Warning, |
| 711 | rewriterWarn); |
| 712 | Diags.Report(loc, diagID) << range; |
| 713 | } |
| 714 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 715 | void TransformActions::reportNote(StringRef note, SourceLocation loc, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 716 | SourceRange range) { |
| 717 | assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() && |
| 718 | "Errors should be emitted out of a transaction"); |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame] | 719 | |
| 720 | SourceManager &SM = static_cast<TransformActionsImpl*>(Impl)-> |
| 721 | getASTContext().getSourceManager(); |
| 722 | if (SM.isInSystemHeader(SM.getExpansionLoc(loc))) |
| 723 | return; |
| 724 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 725 | // FIXME: Use a custom category name to distinguish rewriter errors. |
| 726 | std::string rewriteNote = "[rewriter] "; |
| 727 | rewriteNote += note; |
| 728 | unsigned diagID |
| 729 | = Diags.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Note, |
| 730 | rewriteNote); |
| 731 | Diags.Report(loc, diagID) << range; |
| 732 | } |