ARCMT.cpp revision 9f084a3166b684573ba49df28fc5792bc37d92e1
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 272bool arcmt::applyTransformations(CompilerInvocation &origCI, 273 llvm::StringRef Filename, InputKind Kind, 274 DiagnosticClient *DiagClient) { 275 if (!origCI.getLangOpts().ObjC1) 276 return false; 277 278 // Make sure checking is successful first. 279 CompilerInvocation CInvokForCheck(origCI); 280 if (arcmt::checkForManualIssues(CInvokForCheck, Filename, Kind, DiagClient)) 281 return true; 282 283 CompilerInvocation CInvok(origCI); 284 CInvok.getFrontendOpts().Inputs.clear(); 285 CInvok.getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename)); 286 287 MigrationProcess migration(CInvok, DiagClient); 288 289 std::vector<TransformFn> transforms = arcmt::getAllTransformations(); 290 assert(!transforms.empty()); 291 292 for (unsigned i=0, e = transforms.size(); i != e; ++i) { 293 bool err = migration.applyTransform(transforms[i]); 294 if (err) return true; 295 } 296 297 origCI.getLangOpts().ObjCAutoRefCount = true; 298 299 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 300 llvm::IntrusiveRefCntPtr<Diagnostic> Diags( 301 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); 302 return migration.getRemapper().overwriteOriginal(*Diags); 303} 304 305//===----------------------------------------------------------------------===// 306// CollectTransformActions. 307//===----------------------------------------------------------------------===// 308 309namespace { 310 311class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { 312 std::vector<SourceLocation> &ARCMTMacroLocs; 313 314public: 315 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) 316 : ARCMTMacroLocs(ARCMTMacroLocs) { } 317 318 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI) { 319 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) 320 ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); 321 } 322}; 323 324class ARCMTMacroTrackerAction : public ASTFrontendAction { 325 std::vector<SourceLocation> &ARCMTMacroLocs; 326 327public: 328 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) 329 : ARCMTMacroLocs(ARCMTMacroLocs) { } 330 331 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 332 llvm::StringRef InFile) { 333 CI.getPreprocessor().addPPCallbacks( 334 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); 335 return new ASTConsumer(); 336 } 337}; 338 339class RewritesApplicator : public TransformActions::RewriteReceiver { 340 Rewriter &rewriter; 341 ASTContext &Ctx; 342 MigrationProcess::RewriteListener *Listener; 343 344public: 345 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, 346 MigrationProcess::RewriteListener *listener) 347 : rewriter(rewriter), Ctx(ctx), Listener(listener) { 348 if (Listener) 349 Listener->start(ctx); 350 } 351 ~RewritesApplicator() { 352 if (Listener) 353 Listener->finish(); 354 } 355 356 virtual void insert(SourceLocation loc, llvm::StringRef text) { 357 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, 358 /*indentNewLines=*/true); 359 if (!err && Listener) 360 Listener->insert(loc, text); 361 } 362 363 virtual void remove(CharSourceRange range) { 364 Rewriter::RewriteOptions removeOpts; 365 removeOpts.IncludeInsertsAtBeginOfRange = false; 366 removeOpts.IncludeInsertsAtEndOfRange = false; 367 removeOpts.RemoveLineIfEmpty = true; 368 369 bool err = rewriter.RemoveText(range, removeOpts); 370 if (!err && Listener) 371 Listener->remove(range); 372 } 373 374 virtual void increaseIndentation(CharSourceRange range, 375 SourceLocation parentIndent) { 376 rewriter.IncreaseIndentation(range, parentIndent); 377 } 378}; 379 380} // end anonymous namespace. 381 382/// \brief Anchor for VTable. 383MigrationProcess::RewriteListener::~RewriteListener() { } 384 385bool MigrationProcess::applyTransform(TransformFn trans, 386 RewriteListener *listener) { 387 llvm::OwningPtr<CompilerInvocation> CInvok; 388 CInvok.reset(createInvocationForMigration(OrigCI)); 389 CInvok->getDiagnosticOpts().IgnoreWarnings = true; 390 391 Remapper.applyMappings(*CInvok); 392 393 CapturedDiagList capturedDiags; 394 std::vector<SourceLocation> ARCMTMacroLocs; 395 396 assert(DiagClient); 397 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 398 llvm::IntrusiveRefCntPtr<Diagnostic> Diags( 399 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false)); 400 401 // Filter of all diagnostics. 402 CaptureDiagnosticClient errRec(*Diags, capturedDiags); 403 Diags->setClient(&errRec, /*ShouldOwnClient=*/false); 404 405 llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction; 406 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); 407 408 llvm::OwningPtr<ASTUnit> Unit( 409 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, 410 ASTAction.get())); 411 if (!Unit) 412 return true; 413 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that. 414 415 // Don't filter diagnostics anymore. 416 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); 417 418 ASTContext &Ctx = Unit->getASTContext(); 419 420 if (Diags->hasFatalErrorOccurred()) { 421 Diags->Reset(); 422 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); 423 capturedDiags.reportDiagnostics(*Diags); 424 DiagClient->EndSourceFile(); 425 return true; 426 } 427 428 // After parsing of source files ended, we want to reuse the 429 // diagnostics objects to emit further diagnostics. 430 // We call BeginSourceFile because DiagnosticClient requires that 431 // diagnostics with source range information are emitted only in between 432 // BeginSourceFile() and EndSourceFile(). 433 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); 434 435 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions()); 436 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); 437 MigrationPass pass(Ctx, Unit->getSema(), TA, ARCMTMacroLocs); 438 439 trans(pass); 440 441 { 442 RewritesApplicator applicator(rewriter, Ctx, listener); 443 TA.applyRewrites(applicator); 444 } 445 446 DiagClient->EndSourceFile(); 447 448 if (DiagClient->getNumErrors()) 449 return true; 450 451 for (Rewriter::buffer_iterator 452 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { 453 FileID FID = I->first; 454 RewriteBuffer &buf = I->second; 455 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); 456 assert(file); 457 std::string newFname = file->getName(); 458 newFname += "-trans"; 459 llvm::SmallString<512> newText; 460 llvm::raw_svector_ostream vecOS(newText); 461 buf.write(vecOS); 462 vecOS.flush(); 463 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( 464 llvm::StringRef(newText.data(), newText.size()), newFname); 465 llvm::SmallString<64> filePath(file->getName()); 466 Unit->getFileManager().FixupRelativePath(filePath); 467 Remapper.remap(filePath.str(), memBuf); 468 } 469 470 return false; 471} 472 473//===----------------------------------------------------------------------===// 474// isARCDiagnostic. 475//===----------------------------------------------------------------------===// 476 477bool arcmt::isARCDiagnostic(unsigned diagID, Diagnostic &Diag) { 478 return Diag.getDiagnosticIDs()->getCategoryNumberForDiag(diagID) == 479 diag::DiagCat_Automatic_Reference_Counting_Issue; 480} 481