John McCall | d70fb98 | 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/Frontend/ASTUnit.h" |
| 12 | #include "clang/Frontend/CompilerInstance.h" |
| 13 | #include "clang/Frontend/Utils.h" |
| 14 | #include "clang/AST/ASTConsumer.h" |
| 15 | #include "clang/Rewrite/Rewriter.h" |
| 16 | #include "clang/Sema/SemaDiagnostic.h" |
| 17 | #include "clang/Basic/DiagnosticCategories.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "llvm/ADT/Triple.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace arcmt; |
| 24 | using llvm::StringRef; |
| 25 | |
| 26 | bool CapturedDiagList::clearDiagnostic(llvm::ArrayRef<unsigned> IDs, |
| 27 | SourceRange range) { |
| 28 | if (range.isInvalid()) |
| 29 | return false; |
| 30 | |
| 31 | bool cleared = false; |
| 32 | ListTy::iterator I = List.begin(); |
| 33 | while (I != List.end()) { |
| 34 | FullSourceLoc diagLoc = I->getLocation(); |
| 35 | if ((IDs.empty() || // empty means clear all diagnostics in the range. |
| 36 | std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && |
| 37 | !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && |
| 38 | (diagLoc == range.getEnd() || |
| 39 | diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { |
| 40 | cleared = true; |
| 41 | ListTy::iterator eraseS = I++; |
| 42 | while (I != List.end() && I->getLevel() == Diagnostic::Note) |
| 43 | ++I; |
| 44 | // Clear the diagnostic and any notes following it. |
| 45 | List.erase(eraseS, I); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | ++I; |
| 50 | } |
| 51 | |
| 52 | return cleared; |
| 53 | } |
| 54 | |
| 55 | bool CapturedDiagList::hasDiagnostic(llvm::ArrayRef<unsigned> IDs, |
Argyrios Kyrtzidis | 0f3f9f7 | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 56 | SourceRange range) const { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 57 | if (range.isInvalid()) |
| 58 | return false; |
| 59 | |
Argyrios Kyrtzidis | 0f3f9f7 | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 60 | ListTy::const_iterator I = List.begin(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 61 | while (I != List.end()) { |
| 62 | FullSourceLoc diagLoc = I->getLocation(); |
| 63 | if ((IDs.empty() || // empty means any diagnostic in the range. |
| 64 | std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && |
| 65 | !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && |
| 66 | (diagLoc == range.getEnd() || |
| 67 | diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | ++I; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
Argyrios Kyrtzidis | 0f3f9f7 | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 77 | void CapturedDiagList::reportDiagnostics(Diagnostic &Diags) const { |
| 78 | for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 79 | Diags.Report(*I); |
| 80 | } |
| 81 | |
Argyrios Kyrtzidis | 90b6a2a | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 82 | bool CapturedDiagList::hasErrors() const { |
| 83 | for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I) |
| 84 | if (I->getLevel() >= Diagnostic::Error) |
| 85 | return true; |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 90 | namespace { |
| 91 | |
| 92 | class CaptureDiagnosticClient : public DiagnosticClient { |
| 93 | Diagnostic &Diags; |
| 94 | CapturedDiagList &CapturedDiags; |
| 95 | public: |
| 96 | CaptureDiagnosticClient(Diagnostic &diags, |
| 97 | CapturedDiagList &capturedDiags) |
| 98 | : Diags(diags), CapturedDiags(capturedDiags) { } |
| 99 | |
| 100 | virtual void HandleDiagnostic(Diagnostic::Level level, |
| 101 | const DiagnosticInfo &Info) { |
| 102 | if (arcmt::isARCDiagnostic(Info.getID(), Diags) || |
| 103 | level >= Diagnostic::Error || level == Diagnostic::Note) { |
| 104 | CapturedDiags.push_back(StoredDiagnostic(level, Info)); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Non-ARC warnings are ignored. |
| 109 | Diags.setLastDiagnosticIgnored(); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | } // end anonymous namespace |
| 114 | |
Argyrios Kyrtzidis | 81a3590 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 115 | static inline llvm::StringRef SimulatorVersionDefineName() { |
| 116 | return "__IPHONE_OS_VERSION_MIN_REQUIRED="; |
| 117 | } |
| 118 | |
| 119 | /// \brief Parse the simulator version define: |
| 120 | /// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9]) |
| 121 | // and return the grouped values as integers, e.g: |
| 122 | // __IPHONE_OS_VERSION_MIN_REQUIRED=40201 |
| 123 | // will return Major=4, Minor=2, Micro=1. |
| 124 | static bool GetVersionFromSimulatorDefine(llvm::StringRef define, |
| 125 | unsigned &Major, unsigned &Minor, |
| 126 | unsigned &Micro) { |
| 127 | assert(define.startswith(SimulatorVersionDefineName())); |
| 128 | llvm::StringRef name, version; |
| 129 | llvm::tie(name, version) = define.split('='); |
| 130 | if (version.empty()) |
| 131 | return false; |
| 132 | std::string verstr = version.str(); |
| 133 | char *end; |
| 134 | unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10); |
| 135 | if (*end != '\0') |
| 136 | return false; |
| 137 | Major = num / 10000; |
| 138 | num = num % 10000; |
| 139 | Minor = num / 100; |
| 140 | Micro = num % 100; |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | static bool HasARCRuntime(CompilerInvocation &origCI) { |
| 145 | // This duplicates some functionality from Darwin::AddDeploymentTarget |
| 146 | // but this function is well defined, so keep it decoupled from the driver |
| 147 | // and avoid unrelated complications. |
| 148 | |
| 149 | for (unsigned i = 0, e = origCI.getPreprocessorOpts().Macros.size(); |
| 150 | i != e; ++i) { |
| 151 | StringRef define = origCI.getPreprocessorOpts().Macros[i].first; |
| 152 | bool isUndef = origCI.getPreprocessorOpts().Macros[i].second; |
| 153 | if (isUndef) |
| 154 | continue; |
| 155 | if (!define.startswith(SimulatorVersionDefineName())) |
| 156 | continue; |
| 157 | unsigned Major, Minor, Micro; |
| 158 | if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) && |
| 159 | Major < 10 && Minor < 100 && Micro < 100) |
| 160 | return Major >= 5; |
| 161 | } |
| 162 | |
| 163 | llvm::Triple triple(origCI.getTargetOpts().Triple); |
| 164 | |
| 165 | if (triple.getOS() == llvm::Triple::IOS) |
| 166 | return triple.getOSMajorVersion() >= 5; |
| 167 | |
| 168 | if (triple.getOS() == llvm::Triple::Darwin) |
| 169 | return triple.getOSMajorVersion() >= 11; |
| 170 | |
| 171 | if (triple.getOS() == llvm::Triple::MacOSX) { |
| 172 | unsigned Major, Minor, Micro; |
| 173 | triple.getOSVersion(Major, Minor, Micro); |
| 174 | return Major > 10 || (Major == 10 && Minor >= 7); |
| 175 | } |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 180 | CompilerInvocation *createInvocationForMigration(CompilerInvocation &origCI) { |
| 181 | llvm::OwningPtr<CompilerInvocation> CInvok; |
| 182 | CInvok.reset(new CompilerInvocation(origCI)); |
| 183 | CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string(); |
| 184 | CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string(); |
| 185 | std::string define = getARCMTMacroName(); |
| 186 | define += '='; |
| 187 | CInvok->getPreprocessorOpts().addMacroDef(define); |
| 188 | CInvok->getLangOpts().ObjCAutoRefCount = true; |
| 189 | CInvok->getDiagnosticOpts().ErrorLimit = 0; |
Argyrios Kyrtzidis | 59b5f35 | 2011-06-22 18:03:59 +0000 | [diff] [blame] | 190 | CInvok->getDiagnosticOpts().Warnings.push_back( |
| 191 | "error=arc-unsafe-retained-assign"); |
John McCall | 24fc0de | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 192 | CInvok->getLangOpts().ObjCRuntimeHasWeak = HasARCRuntime(origCI); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 193 | |
| 194 | return CInvok.take(); |
| 195 | } |
| 196 | |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | // checkForManualIssues. |
| 199 | //===----------------------------------------------------------------------===// |
| 200 | |
| 201 | bool arcmt::checkForManualIssues(CompilerInvocation &origCI, |
| 202 | llvm::StringRef Filename, InputKind Kind, |
| 203 | DiagnosticClient *DiagClient) { |
| 204 | if (!origCI.getLangOpts().ObjC1) |
| 205 | return false; |
| 206 | |
| 207 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(); |
| 208 | assert(!transforms.empty()); |
| 209 | |
| 210 | llvm::OwningPtr<CompilerInvocation> CInvok; |
| 211 | CInvok.reset(createInvocationForMigration(origCI)); |
| 212 | CInvok->getFrontendOpts().Inputs.clear(); |
| 213 | CInvok->getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename)); |
| 214 | |
| 215 | CapturedDiagList capturedDiags; |
| 216 | |
| 217 | assert(DiagClient); |
| 218 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 219 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags( |
| 220 | new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
| 221 | |
| 222 | // Filter of all diagnostics. |
| 223 | CaptureDiagnosticClient errRec(*Diags, capturedDiags); |
| 224 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 225 | |
| 226 | llvm::OwningPtr<ASTUnit> Unit( |
| 227 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags)); |
| 228 | if (!Unit) |
| 229 | return true; |
| 230 | |
| 231 | // Don't filter diagnostics anymore. |
| 232 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 233 | |
| 234 | ASTContext &Ctx = Unit->getASTContext(); |
| 235 | |
| 236 | if (Diags->hasFatalErrorOccurred()) { |
| 237 | Diags->Reset(); |
| 238 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 239 | capturedDiags.reportDiagnostics(*Diags); |
| 240 | DiagClient->EndSourceFile(); |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | // After parsing of source files ended, we want to reuse the |
| 245 | // diagnostics objects to emit further diagnostics. |
| 246 | // We call BeginSourceFile because DiagnosticClient requires that |
| 247 | // diagnostics with source range information are emitted only in between |
| 248 | // BeginSourceFile() and EndSourceFile(). |
| 249 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 250 | |
| 251 | // No macros will be added since we are just checking and we won't modify |
| 252 | // source code. |
| 253 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 254 | |
| 255 | TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
| 256 | MigrationPass pass(Ctx, Unit->getSema(), testAct, ARCMTMacroLocs); |
| 257 | |
| 258 | for (unsigned i=0, e = transforms.size(); i != e; ++i) |
| 259 | transforms[i](pass); |
| 260 | |
| 261 | capturedDiags.reportDiagnostics(*Diags); |
| 262 | |
| 263 | DiagClient->EndSourceFile(); |
| 264 | |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame^] | 265 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 266 | // to remove it so that we don't get errors from normal compilation. |
| 267 | origCI.getLangOpts().ObjCAutoRefCount = false; |
| 268 | |
Argyrios Kyrtzidis | 90b6a2a | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 269 | return capturedDiags.hasErrors(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | // applyTransformations. |
| 274 | //===----------------------------------------------------------------------===// |
| 275 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 276 | static bool applyTransforms(CompilerInvocation &origCI, |
| 277 | llvm::StringRef Filename, InputKind Kind, |
| 278 | DiagnosticClient *DiagClient, |
| 279 | llvm::StringRef outputDir) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 280 | if (!origCI.getLangOpts().ObjC1) |
| 281 | return false; |
| 282 | |
| 283 | // Make sure checking is successful first. |
| 284 | CompilerInvocation CInvokForCheck(origCI); |
| 285 | if (arcmt::checkForManualIssues(CInvokForCheck, Filename, Kind, DiagClient)) |
| 286 | return true; |
| 287 | |
| 288 | CompilerInvocation CInvok(origCI); |
| 289 | CInvok.getFrontendOpts().Inputs.clear(); |
| 290 | CInvok.getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename)); |
| 291 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 292 | MigrationProcess migration(CInvok, DiagClient, outputDir); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 293 | |
| 294 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(); |
| 295 | assert(!transforms.empty()); |
| 296 | |
| 297 | for (unsigned i=0, e = transforms.size(); i != e; ++i) { |
| 298 | bool err = migration.applyTransform(transforms[i]); |
| 299 | if (err) return true; |
| 300 | } |
| 301 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 302 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 303 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags( |
| 304 | new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
| 305 | |
| 306 | if (outputDir.empty()) { |
| 307 | origCI.getLangOpts().ObjCAutoRefCount = true; |
| 308 | return migration.getRemapper().overwriteOriginal(*Diags); |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame^] | 309 | } else { |
| 310 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 311 | // to remove it so that we don't get errors from normal compilation. |
| 312 | origCI.getLangOpts().ObjCAutoRefCount = false; |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 313 | return migration.getRemapper().flushToDisk(outputDir, *Diags); |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame^] | 314 | } |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | bool arcmt::applyTransformations(CompilerInvocation &origCI, |
| 318 | llvm::StringRef Filename, InputKind Kind, |
| 319 | DiagnosticClient *DiagClient) { |
| 320 | return applyTransforms(origCI, Filename, Kind, DiagClient, llvm::StringRef()); |
| 321 | } |
| 322 | |
| 323 | bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI, |
| 324 | llvm::StringRef Filename, InputKind Kind, |
| 325 | DiagnosticClient *DiagClient, |
| 326 | llvm::StringRef outputDir) { |
| 327 | assert(!outputDir.empty() && "Expected output directory path"); |
| 328 | return applyTransforms(origCI, Filename, Kind, DiagClient, outputDir); |
| 329 | } |
| 330 | |
| 331 | bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > & |
| 332 | remap, |
| 333 | llvm::StringRef outputDir, |
| 334 | DiagnosticClient *DiagClient) { |
| 335 | assert(!outputDir.empty()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 336 | |
| 337 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 338 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags( |
| 339 | new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 340 | |
| 341 | FileRemapper remapper; |
| 342 | bool err = remapper.initFromDisk(outputDir, *Diags, |
| 343 | /*ignoreIfFilesChanged=*/true); |
| 344 | if (err) |
| 345 | return true; |
| 346 | |
| 347 | CompilerInvocation CI; |
| 348 | remapper.applyMappings(CI); |
| 349 | remap = CI.getPreprocessorOpts().RemappedFiles; |
| 350 | |
| 351 | return false; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | //===----------------------------------------------------------------------===// |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 355 | // CollectTransformActions. |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | |
| 358 | namespace { |
| 359 | |
| 360 | class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { |
| 361 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 362 | |
| 363 | public: |
| 364 | ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 365 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 366 | |
| 367 | virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI) { |
| 368 | if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) |
| 369 | ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | class ARCMTMacroTrackerAction : public ASTFrontendAction { |
| 374 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 375 | |
| 376 | public: |
| 377 | ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 378 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 379 | |
| 380 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 381 | llvm::StringRef InFile) { |
| 382 | CI.getPreprocessor().addPPCallbacks( |
| 383 | new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); |
| 384 | return new ASTConsumer(); |
| 385 | } |
| 386 | }; |
| 387 | |
| 388 | class RewritesApplicator : public TransformActions::RewriteReceiver { |
| 389 | Rewriter &rewriter; |
| 390 | ASTContext &Ctx; |
| 391 | MigrationProcess::RewriteListener *Listener; |
| 392 | |
| 393 | public: |
| 394 | RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, |
| 395 | MigrationProcess::RewriteListener *listener) |
| 396 | : rewriter(rewriter), Ctx(ctx), Listener(listener) { |
| 397 | if (Listener) |
| 398 | Listener->start(ctx); |
| 399 | } |
| 400 | ~RewritesApplicator() { |
| 401 | if (Listener) |
| 402 | Listener->finish(); |
| 403 | } |
| 404 | |
| 405 | virtual void insert(SourceLocation loc, llvm::StringRef text) { |
| 406 | bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, |
| 407 | /*indentNewLines=*/true); |
| 408 | if (!err && Listener) |
| 409 | Listener->insert(loc, text); |
| 410 | } |
| 411 | |
| 412 | virtual void remove(CharSourceRange range) { |
| 413 | Rewriter::RewriteOptions removeOpts; |
| 414 | removeOpts.IncludeInsertsAtBeginOfRange = false; |
| 415 | removeOpts.IncludeInsertsAtEndOfRange = false; |
| 416 | removeOpts.RemoveLineIfEmpty = true; |
| 417 | |
| 418 | bool err = rewriter.RemoveText(range, removeOpts); |
| 419 | if (!err && Listener) |
| 420 | Listener->remove(range); |
| 421 | } |
| 422 | |
| 423 | virtual void increaseIndentation(CharSourceRange range, |
| 424 | SourceLocation parentIndent) { |
| 425 | rewriter.IncreaseIndentation(range, parentIndent); |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | } // end anonymous namespace. |
| 430 | |
| 431 | /// \brief Anchor for VTable. |
| 432 | MigrationProcess::RewriteListener::~RewriteListener() { } |
| 433 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 434 | MigrationProcess::MigrationProcess(const CompilerInvocation &CI, |
| 435 | DiagnosticClient *diagClient, |
| 436 | llvm::StringRef outputDir) |
| 437 | : OrigCI(CI), DiagClient(diagClient) { |
| 438 | if (!outputDir.empty()) { |
| 439 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 440 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags( |
| 441 | new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
| 442 | Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true); |
| 443 | } |
| 444 | } |
| 445 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 446 | bool MigrationProcess::applyTransform(TransformFn trans, |
| 447 | RewriteListener *listener) { |
| 448 | llvm::OwningPtr<CompilerInvocation> CInvok; |
| 449 | CInvok.reset(createInvocationForMigration(OrigCI)); |
| 450 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 451 | |
| 452 | Remapper.applyMappings(*CInvok); |
| 453 | |
| 454 | CapturedDiagList capturedDiags; |
| 455 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 456 | |
| 457 | assert(DiagClient); |
| 458 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 459 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags( |
| 460 | new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
| 461 | |
| 462 | // Filter of all diagnostics. |
| 463 | CaptureDiagnosticClient errRec(*Diags, capturedDiags); |
| 464 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 465 | |
| 466 | llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction; |
| 467 | ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); |
| 468 | |
| 469 | llvm::OwningPtr<ASTUnit> Unit( |
| 470 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, |
| 471 | ASTAction.get())); |
| 472 | if (!Unit) |
| 473 | return true; |
| 474 | Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that. |
| 475 | |
| 476 | // Don't filter diagnostics anymore. |
| 477 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 478 | |
| 479 | ASTContext &Ctx = Unit->getASTContext(); |
| 480 | |
| 481 | if (Diags->hasFatalErrorOccurred()) { |
| 482 | Diags->Reset(); |
| 483 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 484 | capturedDiags.reportDiagnostics(*Diags); |
| 485 | DiagClient->EndSourceFile(); |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | // After parsing of source files ended, we want to reuse the |
| 490 | // diagnostics objects to emit further diagnostics. |
| 491 | // We call BeginSourceFile because DiagnosticClient requires that |
| 492 | // diagnostics with source range information are emitted only in between |
| 493 | // BeginSourceFile() and EndSourceFile(). |
| 494 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 495 | |
| 496 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions()); |
| 497 | TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
| 498 | MigrationPass pass(Ctx, Unit->getSema(), TA, ARCMTMacroLocs); |
| 499 | |
| 500 | trans(pass); |
| 501 | |
| 502 | { |
| 503 | RewritesApplicator applicator(rewriter, Ctx, listener); |
| 504 | TA.applyRewrites(applicator); |
| 505 | } |
| 506 | |
| 507 | DiagClient->EndSourceFile(); |
| 508 | |
| 509 | if (DiagClient->getNumErrors()) |
| 510 | return true; |
| 511 | |
| 512 | for (Rewriter::buffer_iterator |
| 513 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 514 | FileID FID = I->first; |
| 515 | RewriteBuffer &buf = I->second; |
| 516 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 517 | assert(file); |
| 518 | std::string newFname = file->getName(); |
| 519 | newFname += "-trans"; |
| 520 | llvm::SmallString<512> newText; |
| 521 | llvm::raw_svector_ostream vecOS(newText); |
| 522 | buf.write(vecOS); |
| 523 | vecOS.flush(); |
| 524 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
| 525 | llvm::StringRef(newText.data(), newText.size()), newFname); |
| 526 | llvm::SmallString<64> filePath(file->getName()); |
| 527 | Unit->getFileManager().FixupRelativePath(filePath); |
| 528 | Remapper.remap(filePath.str(), memBuf); |
| 529 | } |
| 530 | |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | //===----------------------------------------------------------------------===// |
| 535 | // isARCDiagnostic. |
| 536 | //===----------------------------------------------------------------------===// |
| 537 | |
| 538 | bool arcmt::isARCDiagnostic(unsigned diagID, Diagnostic &Diag) { |
| 539 | return Diag.getDiagnosticIDs()->getCategoryNumberForDiag(diagID) == |
| 540 | diag::DiagCat_Automatic_Reference_Counting_Issue; |
| 541 | } |