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