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/ASTConsumer.h" 12#include "clang/Basic/DiagnosticCategories.h" 13#include "clang/Frontend/ASTUnit.h" 14#include "clang/Frontend/CompilerInstance.h" 15#include "clang/Frontend/FrontendAction.h" 16#include "clang/Frontend/TextDiagnosticPrinter.h" 17#include "clang/Frontend/Utils.h" 18#include "clang/Lex/Preprocessor.h" 19#include "clang/Rewrite/Core/Rewriter.h" 20#include "clang/Sema/SemaDiagnostic.h" 21#include "clang/Serialization/ASTReader.h" 22#include "llvm/ADT/Triple.h" 23#include "llvm/Support/MemoryBuffer.h" 24using namespace clang; 25using namespace arcmt; 26 27bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs, 28 SourceRange range) { 29 if (range.isInvalid()) 30 return false; 31 32 bool cleared = false; 33 ListTy::iterator I = List.begin(); 34 while (I != List.end()) { 35 FullSourceLoc diagLoc = I->getLocation(); 36 if ((IDs.empty() || // empty means clear all diagnostics in the range. 37 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && 38 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && 39 (diagLoc == range.getEnd() || 40 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { 41 cleared = true; 42 ListTy::iterator eraseS = I++; 43 if (eraseS->getLevel() != DiagnosticsEngine::Note) 44 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note) 45 ++I; 46 // Clear the diagnostic and any notes following it. 47 I = List.erase(eraseS, I); 48 continue; 49 } 50 51 ++I; 52 } 53 54 return cleared; 55} 56 57bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs, 58 SourceRange range) const { 59 if (range.isInvalid()) 60 return false; 61 62 ListTy::const_iterator I = List.begin(); 63 while (I != List.end()) { 64 FullSourceLoc diagLoc = I->getLocation(); 65 if ((IDs.empty() || // empty means any diagnostic in the range. 66 std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && 67 !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && 68 (diagLoc == range.getEnd() || 69 diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { 70 return true; 71 } 72 73 ++I; 74 } 75 76 return false; 77} 78 79void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const { 80 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I) 81 Diags.Report(*I); 82} 83 84bool CapturedDiagList::hasErrors() const { 85 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I) 86 if (I->getLevel() >= DiagnosticsEngine::Error) 87 return true; 88 89 return false; 90} 91 92namespace { 93 94class CaptureDiagnosticConsumer : public DiagnosticConsumer { 95 DiagnosticsEngine &Diags; 96 DiagnosticConsumer &DiagClient; 97 CapturedDiagList &CapturedDiags; 98 bool HasBegunSourceFile; 99public: 100 CaptureDiagnosticConsumer(DiagnosticsEngine &diags, 101 DiagnosticConsumer &client, 102 CapturedDiagList &capturedDiags) 103 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags), 104 HasBegunSourceFile(false) { } 105 106 virtual void BeginSourceFile(const LangOptions &Opts, 107 const Preprocessor *PP) { 108 // Pass BeginSourceFile message onto DiagClient on first call. 109 // The corresponding EndSourceFile call will be made from an 110 // explicit call to FinishCapture. 111 if (!HasBegunSourceFile) { 112 DiagClient.BeginSourceFile(Opts, PP); 113 HasBegunSourceFile = true; 114 } 115 } 116 117 void FinishCapture() { 118 // Call EndSourceFile on DiagClient on completion of capture to 119 // enable VerifyDiagnosticConsumer to check diagnostics *after* 120 // it has received the diagnostic list. 121 if (HasBegunSourceFile) { 122 DiagClient.EndSourceFile(); 123 HasBegunSourceFile = false; 124 } 125 } 126 127 virtual ~CaptureDiagnosticConsumer() { 128 assert(!HasBegunSourceFile && "FinishCapture not called!"); 129 } 130 131 virtual void HandleDiagnostic(DiagnosticsEngine::Level level, 132 const Diagnostic &Info) { 133 if (DiagnosticIDs::isARCDiagnostic(Info.getID()) || 134 level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) { 135 if (Info.getLocation().isValid()) 136 CapturedDiags.push_back(StoredDiagnostic(level, Info)); 137 return; 138 } 139 140 // Non-ARC warnings are ignored. 141 Diags.setLastDiagnosticIgnored(); 142 } 143}; 144 145} // end anonymous namespace 146 147static bool HasARCRuntime(CompilerInvocation &origCI) { 148 // This duplicates some functionality from Darwin::AddDeploymentTarget 149 // but this function is well defined, so keep it decoupled from the driver 150 // and avoid unrelated complications. 151 llvm::Triple triple(origCI.getTargetOpts().Triple); 152 153 if (triple.isiOS()) 154 return triple.getOSMajorVersion() >= 5; 155 156 if (triple.getOS() == llvm::Triple::Darwin) 157 return triple.getOSMajorVersion() >= 11; 158 159 if (triple.getOS() == llvm::Triple::MacOSX) { 160 unsigned Major, Minor, Micro; 161 triple.getOSVersion(Major, Minor, Micro); 162 return Major > 10 || (Major == 10 && Minor >= 7); 163 } 164 165 return false; 166} 167 168static CompilerInvocation * 169createInvocationForMigration(CompilerInvocation &origCI) { 170 OwningPtr<CompilerInvocation> CInvok; 171 CInvok.reset(new CompilerInvocation(origCI)); 172 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); 173 if (!PPOpts.ImplicitPCHInclude.empty()) { 174 // We can't use a PCH because it was likely built in non-ARC mode and we 175 // want to parse in ARC. Include the original header. 176 FileManager FileMgr(origCI.getFileSystemOpts()); 177 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 178 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 179 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), 180 new IgnoringDiagConsumer())); 181 std::string OriginalFile = 182 ASTReader::getOriginalSourceFile(PPOpts.ImplicitPCHInclude, 183 FileMgr, *Diags); 184 if (!OriginalFile.empty()) 185 PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile); 186 PPOpts.ImplicitPCHInclude.clear(); 187 } 188 // FIXME: Get the original header of a PTH as well. 189 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear(); 190 std::string define = getARCMTMacroName(); 191 define += '='; 192 CInvok->getPreprocessorOpts().addMacroDef(define); 193 CInvok->getLangOpts()->ObjCAutoRefCount = true; 194 CInvok->getLangOpts()->setGC(LangOptions::NonGC); 195 CInvok->getDiagnosticOpts().ErrorLimit = 0; 196 CInvok->getDiagnosticOpts().PedanticErrors = 0; 197 198 // Ignore -Werror flags when migrating. 199 std::vector<std::string> WarnOpts; 200 for (std::vector<std::string>::iterator 201 I = CInvok->getDiagnosticOpts().Warnings.begin(), 202 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) { 203 if (!StringRef(*I).startswith("error")) 204 WarnOpts.push_back(*I); 205 } 206 WarnOpts.push_back("error=arc-unsafe-retained-assign"); 207 CInvok->getDiagnosticOpts().Warnings = llvm_move(WarnOpts); 208 209 CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI); 210 211 return CInvok.take(); 212} 213 214static void emitPremigrationErrors(const CapturedDiagList &arcDiags, 215 DiagnosticOptions *diagOpts, 216 Preprocessor &PP) { 217 TextDiagnosticPrinter printer(llvm::errs(), diagOpts); 218 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 219 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 220 new DiagnosticsEngine(DiagID, diagOpts, &printer, 221 /*ShouldOwnClient=*/false)); 222 Diags->setSourceManager(&PP.getSourceManager()); 223 224 printer.BeginSourceFile(PP.getLangOpts(), &PP); 225 arcDiags.reportDiagnostics(*Diags); 226 printer.EndSourceFile(); 227} 228 229//===----------------------------------------------------------------------===// 230// checkForManualIssues. 231//===----------------------------------------------------------------------===// 232 233bool arcmt::checkForManualIssues(CompilerInvocation &origCI, 234 const FrontendInputFile &Input, 235 DiagnosticConsumer *DiagClient, 236 bool emitPremigrationARCErrors, 237 StringRef plistOut) { 238 if (!origCI.getLangOpts()->ObjC1) 239 return false; 240 241 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); 242 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError; 243 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; 244 245 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode, 246 NoFinalizeRemoval); 247 assert(!transforms.empty()); 248 249 OwningPtr<CompilerInvocation> CInvok; 250 CInvok.reset(createInvocationForMigration(origCI)); 251 CInvok->getFrontendOpts().Inputs.clear(); 252 CInvok->getFrontendOpts().Inputs.push_back(Input); 253 254 CapturedDiagList capturedDiags; 255 256 assert(DiagClient); 257 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 258 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 259 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), 260 DiagClient, /*ShouldOwnClient=*/false)); 261 262 // Filter of all diagnostics. 263 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags); 264 Diags->setClient(&errRec, /*ShouldOwnClient=*/false); 265 266 OwningPtr<ASTUnit> Unit( 267 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags)); 268 if (!Unit) { 269 errRec.FinishCapture(); 270 return true; 271 } 272 273 // Don't filter diagnostics anymore. 274 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); 275 276 ASTContext &Ctx = Unit->getASTContext(); 277 278 if (Diags->hasFatalErrorOccurred()) { 279 Diags->Reset(); 280 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); 281 capturedDiags.reportDiagnostics(*Diags); 282 DiagClient->EndSourceFile(); 283 errRec.FinishCapture(); 284 return true; 285 } 286 287 if (emitPremigrationARCErrors) 288 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(), 289 Unit->getPreprocessor()); 290 if (!plistOut.empty()) { 291 SmallVector<StoredDiagnostic, 8> arcDiags; 292 for (CapturedDiagList::iterator 293 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I) 294 arcDiags.push_back(*I); 295 writeARCDiagsToPlist(plistOut, arcDiags, 296 Ctx.getSourceManager(), Ctx.getLangOpts()); 297 } 298 299 // After parsing of source files ended, we want to reuse the 300 // diagnostics objects to emit further diagnostics. 301 // We call BeginSourceFile because DiagnosticConsumer requires that 302 // diagnostics with source range information are emitted only in between 303 // BeginSourceFile() and EndSourceFile(). 304 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); 305 306 // No macros will be added since we are just checking and we won't modify 307 // source code. 308 std::vector<SourceLocation> ARCMTMacroLocs; 309 310 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); 311 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags, 312 ARCMTMacroLocs); 313 pass.setNSAllocReallocError(NoNSAllocReallocError); 314 pass.setNoFinalizeRemoval(NoFinalizeRemoval); 315 316 for (unsigned i=0, e = transforms.size(); i != e; ++i) 317 transforms[i](pass); 318 319 capturedDiags.reportDiagnostics(*Diags); 320 321 DiagClient->EndSourceFile(); 322 errRec.FinishCapture(); 323 324 return capturedDiags.hasErrors() || testAct.hasReportedErrors(); 325} 326 327//===----------------------------------------------------------------------===// 328// applyTransformations. 329//===----------------------------------------------------------------------===// 330 331static bool applyTransforms(CompilerInvocation &origCI, 332 const FrontendInputFile &Input, 333 DiagnosticConsumer *DiagClient, 334 StringRef outputDir, 335 bool emitPremigrationARCErrors, 336 StringRef plistOut) { 337 if (!origCI.getLangOpts()->ObjC1) 338 return false; 339 340 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); 341 342 // Make sure checking is successful first. 343 CompilerInvocation CInvokForCheck(origCI); 344 if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient, 345 emitPremigrationARCErrors, plistOut)) 346 return true; 347 348 CompilerInvocation CInvok(origCI); 349 CInvok.getFrontendOpts().Inputs.clear(); 350 CInvok.getFrontendOpts().Inputs.push_back(Input); 351 352 MigrationProcess migration(CInvok, DiagClient, outputDir); 353 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; 354 355 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode, 356 NoFinalizeRemoval); 357 assert(!transforms.empty()); 358 359 for (unsigned i=0, e = transforms.size(); i != e; ++i) { 360 bool err = migration.applyTransform(transforms[i]); 361 if (err) return true; 362 } 363 364 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 365 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 366 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), 367 DiagClient, /*ShouldOwnClient=*/false)); 368 369 if (outputDir.empty()) { 370 origCI.getLangOpts()->ObjCAutoRefCount = true; 371 return migration.getRemapper().overwriteOriginal(*Diags); 372 } else { 373 return migration.getRemapper().flushToDisk(outputDir, *Diags); 374 } 375} 376 377bool arcmt::applyTransformations(CompilerInvocation &origCI, 378 const FrontendInputFile &Input, 379 DiagnosticConsumer *DiagClient) { 380 return applyTransforms(origCI, Input, DiagClient, 381 StringRef(), false, StringRef()); 382} 383 384bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI, 385 const FrontendInputFile &Input, 386 DiagnosticConsumer *DiagClient, 387 StringRef outputDir, 388 bool emitPremigrationARCErrors, 389 StringRef plistOut) { 390 assert(!outputDir.empty() && "Expected output directory path"); 391 return applyTransforms(origCI, Input, DiagClient, 392 outputDir, emitPremigrationARCErrors, plistOut); 393} 394 395bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > & 396 remap, 397 StringRef outputDir, 398 DiagnosticConsumer *DiagClient) { 399 assert(!outputDir.empty()); 400 401 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 402 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 403 new DiagnosticsEngine(DiagID, new DiagnosticOptions, 404 DiagClient, /*ShouldOwnClient=*/false)); 405 406 FileRemapper remapper; 407 bool err = remapper.initFromDisk(outputDir, *Diags, 408 /*ignoreIfFilesChanged=*/true); 409 if (err) 410 return true; 411 412 PreprocessorOptions PPOpts; 413 remapper.applyMappings(PPOpts); 414 remap = PPOpts.RemappedFiles; 415 416 return false; 417} 418 419bool arcmt::getFileRemappingsFromFileList( 420 std::vector<std::pair<std::string,std::string> > &remap, 421 ArrayRef<StringRef> remapFiles, 422 DiagnosticConsumer *DiagClient) { 423 bool hasErrorOccurred = false; 424 llvm::StringMap<bool> Uniquer; 425 426 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 427 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 428 new DiagnosticsEngine(DiagID, new DiagnosticOptions, 429 DiagClient, /*ShouldOwnClient=*/false)); 430 431 for (ArrayRef<StringRef>::iterator 432 I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) { 433 StringRef file = *I; 434 435 FileRemapper remapper; 436 bool err = remapper.initFromFile(file, *Diags, 437 /*ignoreIfFilesChanged=*/true); 438 hasErrorOccurred = hasErrorOccurred || err; 439 if (err) 440 continue; 441 442 PreprocessorOptions PPOpts; 443 remapper.applyMappings(PPOpts); 444 for (PreprocessorOptions::remapped_file_iterator 445 RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end(); 446 RI != RE; ++RI) { 447 bool &inserted = Uniquer[RI->first]; 448 if (inserted) 449 continue; 450 inserted = true; 451 remap.push_back(*RI); 452 } 453 } 454 455 return hasErrorOccurred; 456} 457 458//===----------------------------------------------------------------------===// 459// CollectTransformActions. 460//===----------------------------------------------------------------------===// 461 462namespace { 463 464class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { 465 std::vector<SourceLocation> &ARCMTMacroLocs; 466 467public: 468 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) 469 : ARCMTMacroLocs(ARCMTMacroLocs) { } 470 471 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, 472 SourceRange Range, const MacroArgs *Args) { 473 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) 474 ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); 475 } 476}; 477 478class ARCMTMacroTrackerAction : public ASTFrontendAction { 479 std::vector<SourceLocation> &ARCMTMacroLocs; 480 481public: 482 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) 483 : ARCMTMacroLocs(ARCMTMacroLocs) { } 484 485 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 486 StringRef InFile) { 487 CI.getPreprocessor().addPPCallbacks( 488 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); 489 return new ASTConsumer(); 490 } 491}; 492 493class RewritesApplicator : public TransformActions::RewriteReceiver { 494 Rewriter &rewriter; 495 MigrationProcess::RewriteListener *Listener; 496 497public: 498 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, 499 MigrationProcess::RewriteListener *listener) 500 : rewriter(rewriter), Listener(listener) { 501 if (Listener) 502 Listener->start(ctx); 503 } 504 ~RewritesApplicator() { 505 if (Listener) 506 Listener->finish(); 507 } 508 509 virtual void insert(SourceLocation loc, StringRef text) { 510 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, 511 /*indentNewLines=*/true); 512 if (!err && Listener) 513 Listener->insert(loc, text); 514 } 515 516 virtual void remove(CharSourceRange range) { 517 Rewriter::RewriteOptions removeOpts; 518 removeOpts.IncludeInsertsAtBeginOfRange = false; 519 removeOpts.IncludeInsertsAtEndOfRange = false; 520 removeOpts.RemoveLineIfEmpty = true; 521 522 bool err = rewriter.RemoveText(range, removeOpts); 523 if (!err && Listener) 524 Listener->remove(range); 525 } 526 527 virtual void increaseIndentation(CharSourceRange range, 528 SourceLocation parentIndent) { 529 rewriter.IncreaseIndentation(range, parentIndent); 530 } 531}; 532 533} // end anonymous namespace. 534 535/// \brief Anchor for VTable. 536MigrationProcess::RewriteListener::~RewriteListener() { } 537 538MigrationProcess::MigrationProcess(const CompilerInvocation &CI, 539 DiagnosticConsumer *diagClient, 540 StringRef outputDir) 541 : OrigCI(CI), DiagClient(diagClient), HadARCErrors(false) { 542 if (!outputDir.empty()) { 543 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 544 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 545 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), 546 DiagClient, /*ShouldOwnClient=*/false)); 547 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true); 548 } 549} 550 551bool MigrationProcess::applyTransform(TransformFn trans, 552 RewriteListener *listener) { 553 OwningPtr<CompilerInvocation> CInvok; 554 CInvok.reset(createInvocationForMigration(OrigCI)); 555 CInvok->getDiagnosticOpts().IgnoreWarnings = true; 556 557 Remapper.applyMappings(CInvok->getPreprocessorOpts()); 558 559 CapturedDiagList capturedDiags; 560 std::vector<SourceLocation> ARCMTMacroLocs; 561 562 assert(DiagClient); 563 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 564 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 565 new DiagnosticsEngine(DiagID, new DiagnosticOptions, 566 DiagClient, /*ShouldOwnClient=*/false)); 567 568 // Filter of all diagnostics. 569 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags); 570 Diags->setClient(&errRec, /*ShouldOwnClient=*/false); 571 572 OwningPtr<ARCMTMacroTrackerAction> ASTAction; 573 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); 574 575 OwningPtr<ASTUnit> Unit( 576 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, 577 ASTAction.get())); 578 if (!Unit) { 579 errRec.FinishCapture(); 580 return true; 581 } 582 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that. 583 584 HadARCErrors = HadARCErrors || capturedDiags.hasErrors(); 585 586 // Don't filter diagnostics anymore. 587 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); 588 589 ASTContext &Ctx = Unit->getASTContext(); 590 591 if (Diags->hasFatalErrorOccurred()) { 592 Diags->Reset(); 593 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); 594 capturedDiags.reportDiagnostics(*Diags); 595 DiagClient->EndSourceFile(); 596 errRec.FinishCapture(); 597 return true; 598 } 599 600 // After parsing of source files ended, we want to reuse the 601 // diagnostics objects to emit further diagnostics. 602 // We call BeginSourceFile because DiagnosticConsumer requires that 603 // diagnostics with source range information are emitted only in between 604 // BeginSourceFile() and EndSourceFile(). 605 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); 606 607 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); 608 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); 609 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(), 610 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs); 611 612 trans(pass); 613 614 { 615 RewritesApplicator applicator(rewriter, Ctx, listener); 616 TA.applyRewrites(applicator); 617 } 618 619 DiagClient->EndSourceFile(); 620 errRec.FinishCapture(); 621 622 if (DiagClient->getNumErrors()) 623 return true; 624 625 for (Rewriter::buffer_iterator 626 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { 627 FileID FID = I->first; 628 RewriteBuffer &buf = I->second; 629 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); 630 assert(file); 631 std::string newFname = file->getName(); 632 newFname += "-trans"; 633 SmallString<512> newText; 634 llvm::raw_svector_ostream vecOS(newText); 635 buf.write(vecOS); 636 vecOS.flush(); 637 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( 638 StringRef(newText.data(), newText.size()), newFname); 639 SmallString<64> filePath(file->getName()); 640 Unit->getFileManager().FixupRelativePath(filePath); 641 Remapper.remap(filePath.str(), memBuf); 642 } 643 644 return false; 645} 646