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