ARCMT.cpp revision 69325d5b7cfecf1b3128745efc33612aedf1b8b4
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 22using namespace clang; 23using namespace arcmt; 24using llvm::StringRef; 25 26bool 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 55bool CapturedDiagList::hasDiagnostic(llvm::ArrayRef<unsigned> IDs, 56 SourceRange range) const { 57 if (range.isInvalid()) 58 return false; 59 60 ListTy::const_iterator I = List.begin(); 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 77void CapturedDiagList::reportDiagnostics(Diagnostic &Diags) const { 78 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I) 79 Diags.Report(*I); 80} 81 82bool 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 90namespace { 91 92class CaptureDiagnosticClient : public DiagnosticClient { 93 Diagnostic &Diags; 94 CapturedDiagList &CapturedDiags; 95public: 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 115static 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. 124static 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 144static 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 180CompilerInvocation *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; 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 197//===----------------------------------------------------------------------===// 198// checkForManualIssues. 199//===----------------------------------------------------------------------===// 200 201bool 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 265 return capturedDiags.hasErrors(); 266} 267 268//===----------------------------------------------------------------------===// 269// applyTransformations. 270//===----------------------------------------------------------------------===// 271 272static bool applyTransforms(CompilerInvocation &origCI, 273 llvm::StringRef Filename, InputKind Kind, 274 DiagnosticClient *DiagClient, 275 llvm::StringRef outputDir) { 276 if (!origCI.getLangOpts().ObjC1) 277 return false; 278 279 // Make sure checking is successful first. 280 CompilerInvocation CInvokForCheck(origCI); 281 if (arcmt::checkForManualIssues(CInvokForCheck, Filename, Kind, DiagClient)) 282 return true; 283 284 CompilerInvocation CInvok(origCI); 285 CInvok.getFrontendOpts().Inputs.clear(); 286 CInvok.getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename)); 287 288 MigrationProcess migration(CInvok, DiagClient, outputDir); 289 290 std::vector<TransformFn> transforms = arcmt::getAllTransformations(); 291 assert(!transforms.empty()); 292 293 for (unsigned i=0, e = transforms.size(); i != e; ++i) { 294 bool err = migration.applyTransform(transforms[i]); 295 if (err) return true; 296 } 297 298 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 299 llvm::IntrusiveRefCntPtr<Diagnostic> Diags( 300 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); 301 302 if (outputDir.empty()) { 303 origCI.getLangOpts().ObjCAutoRefCount = true; 304 return migration.getRemapper().overwriteOriginal(*Diags); 305 } else 306 return migration.getRemapper().flushToDisk(outputDir, *Diags); 307} 308 309bool arcmt::applyTransformations(CompilerInvocation &origCI, 310 llvm::StringRef Filename, InputKind Kind, 311 DiagnosticClient *DiagClient) { 312 return applyTransforms(origCI, Filename, Kind, DiagClient, llvm::StringRef()); 313} 314 315bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI, 316 llvm::StringRef Filename, InputKind Kind, 317 DiagnosticClient *DiagClient, 318 llvm::StringRef outputDir) { 319 assert(!outputDir.empty() && "Expected output directory path"); 320 return applyTransforms(origCI, Filename, Kind, DiagClient, outputDir); 321} 322 323bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > & 324 remap, 325 llvm::StringRef outputDir, 326 DiagnosticClient *DiagClient) { 327 assert(!outputDir.empty()); 328 329 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 330 llvm::IntrusiveRefCntPtr<Diagnostic> Diags( 331 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); 332 333 FileRemapper remapper; 334 bool err = remapper.initFromDisk(outputDir, *Diags, 335 /*ignoreIfFilesChanged=*/true); 336 if (err) 337 return true; 338 339 CompilerInvocation CI; 340 remapper.applyMappings(CI); 341 remap = CI.getPreprocessorOpts().RemappedFiles; 342 343 return false; 344} 345 346//===----------------------------------------------------------------------===// 347// CollectTransformActions. 348//===----------------------------------------------------------------------===// 349 350namespace { 351 352class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { 353 std::vector<SourceLocation> &ARCMTMacroLocs; 354 355public: 356 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) 357 : ARCMTMacroLocs(ARCMTMacroLocs) { } 358 359 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI) { 360 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) 361 ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); 362 } 363}; 364 365class ARCMTMacroTrackerAction : public ASTFrontendAction { 366 std::vector<SourceLocation> &ARCMTMacroLocs; 367 368public: 369 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) 370 : ARCMTMacroLocs(ARCMTMacroLocs) { } 371 372 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 373 llvm::StringRef InFile) { 374 CI.getPreprocessor().addPPCallbacks( 375 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); 376 return new ASTConsumer(); 377 } 378}; 379 380class RewritesApplicator : public TransformActions::RewriteReceiver { 381 Rewriter &rewriter; 382 ASTContext &Ctx; 383 MigrationProcess::RewriteListener *Listener; 384 385public: 386 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, 387 MigrationProcess::RewriteListener *listener) 388 : rewriter(rewriter), Ctx(ctx), Listener(listener) { 389 if (Listener) 390 Listener->start(ctx); 391 } 392 ~RewritesApplicator() { 393 if (Listener) 394 Listener->finish(); 395 } 396 397 virtual void insert(SourceLocation loc, llvm::StringRef text) { 398 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, 399 /*indentNewLines=*/true); 400 if (!err && Listener) 401 Listener->insert(loc, text); 402 } 403 404 virtual void remove(CharSourceRange range) { 405 Rewriter::RewriteOptions removeOpts; 406 removeOpts.IncludeInsertsAtBeginOfRange = false; 407 removeOpts.IncludeInsertsAtEndOfRange = false; 408 removeOpts.RemoveLineIfEmpty = true; 409 410 bool err = rewriter.RemoveText(range, removeOpts); 411 if (!err && Listener) 412 Listener->remove(range); 413 } 414 415 virtual void increaseIndentation(CharSourceRange range, 416 SourceLocation parentIndent) { 417 rewriter.IncreaseIndentation(range, parentIndent); 418 } 419}; 420 421} // end anonymous namespace. 422 423/// \brief Anchor for VTable. 424MigrationProcess::RewriteListener::~RewriteListener() { } 425 426MigrationProcess::MigrationProcess(const CompilerInvocation &CI, 427 DiagnosticClient *diagClient, 428 llvm::StringRef outputDir) 429 : OrigCI(CI), DiagClient(diagClient) { 430 if (!outputDir.empty()) { 431 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 432 llvm::IntrusiveRefCntPtr<Diagnostic> Diags( 433 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); 434 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true); 435 } 436} 437 438bool MigrationProcess::applyTransform(TransformFn trans, 439 RewriteListener *listener) { 440 llvm::OwningPtr<CompilerInvocation> CInvok; 441 CInvok.reset(createInvocationForMigration(OrigCI)); 442 CInvok->getDiagnosticOpts().IgnoreWarnings = true; 443 444 Remapper.applyMappings(*CInvok); 445 446 CapturedDiagList capturedDiags; 447 std::vector<SourceLocation> ARCMTMacroLocs; 448 449 assert(DiagClient); 450 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 451 llvm::IntrusiveRefCntPtr<Diagnostic> Diags( 452 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); 453 454 // Filter of all diagnostics. 455 CaptureDiagnosticClient errRec(*Diags, capturedDiags); 456 Diags->setClient(&errRec, /*ShouldOwnClient=*/false); 457 458 llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction; 459 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); 460 461 llvm::OwningPtr<ASTUnit> Unit( 462 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, 463 ASTAction.get())); 464 if (!Unit) 465 return true; 466 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that. 467 468 // Don't filter diagnostics anymore. 469 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); 470 471 ASTContext &Ctx = Unit->getASTContext(); 472 473 if (Diags->hasFatalErrorOccurred()) { 474 Diags->Reset(); 475 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); 476 capturedDiags.reportDiagnostics(*Diags); 477 DiagClient->EndSourceFile(); 478 return true; 479 } 480 481 // After parsing of source files ended, we want to reuse the 482 // diagnostics objects to emit further diagnostics. 483 // We call BeginSourceFile because DiagnosticClient requires that 484 // diagnostics with source range information are emitted only in between 485 // BeginSourceFile() and EndSourceFile(). 486 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); 487 488 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions()); 489 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); 490 MigrationPass pass(Ctx, Unit->getSema(), TA, ARCMTMacroLocs); 491 492 trans(pass); 493 494 { 495 RewritesApplicator applicator(rewriter, Ctx, listener); 496 TA.applyRewrites(applicator); 497 } 498 499 DiagClient->EndSourceFile(); 500 501 if (DiagClient->getNumErrors()) 502 return true; 503 504 for (Rewriter::buffer_iterator 505 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { 506 FileID FID = I->first; 507 RewriteBuffer &buf = I->second; 508 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); 509 assert(file); 510 std::string newFname = file->getName(); 511 newFname += "-trans"; 512 llvm::SmallString<512> newText; 513 llvm::raw_svector_ostream vecOS(newText); 514 buf.write(vecOS); 515 vecOS.flush(); 516 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( 517 llvm::StringRef(newText.data(), newText.size()), newFname); 518 llvm::SmallString<64> filePath(file->getName()); 519 Unit->getFileManager().FixupRelativePath(filePath); 520 Remapper.remap(filePath.str(), memBuf); 521 } 522 523 return false; 524} 525 526//===----------------------------------------------------------------------===// 527// isARCDiagnostic. 528//===----------------------------------------------------------------------===// 529 530bool arcmt::isARCDiagnostic(unsigned diagID, Diagnostic &Diag) { 531 return Diag.getDiagnosticIDs()->getCategoryNumberForDiag(diagID) == 532 diag::DiagCat_Automatic_Reference_Counting_Issue; 533} 534