1 //===--- ARCMT.cpp - Migration to ARC mode --------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
24 using namespace clang;
25 using namespace arcmt;
27 bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
29 if (range.isInvalid())
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()))) {
42 ListTy::iterator eraseS = I++;
43 if (eraseS->getLevel() != DiagnosticsEngine::Note)
44 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
46 // Clear the diagnostic and any notes following it.
47 I = List.erase(eraseS, I);
57 bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
58 SourceRange range) const {
59 if (range.isInvalid())
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()))) {
79 void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
80 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
84 bool CapturedDiagList::hasErrors() const {
85 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
86 if (I->getLevel() >= DiagnosticsEngine::Error)
94 class CaptureDiagnosticConsumer : public DiagnosticConsumer {
95 DiagnosticsEngine &Diags;
96 DiagnosticConsumer &DiagClient;
97 CapturedDiagList &CapturedDiags;
98 bool HasBegunSourceFile;
100 CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
101 DiagnosticConsumer &client,
102 CapturedDiagList &capturedDiags)
103 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
104 HasBegunSourceFile(false) { }
106 void BeginSourceFile(const LangOptions &Opts,
107 const Preprocessor *PP) override {
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;
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;
127 ~CaptureDiagnosticConsumer() override {
128 assert(!HasBegunSourceFile && "FinishCapture not called!");
131 void HandleDiagnostic(DiagnosticsEngine::Level level,
132 const Diagnostic &Info) override {
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));
140 // Non-ARC warnings are ignored.
141 Diags.setLastDiagnosticIgnored();
145 } // end anonymous namespace
147 static 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);
154 return triple.getOSMajorVersion() >= 5;
156 if (triple.isWatchOS())
159 if (triple.getOS() == llvm::Triple::Darwin)
160 return triple.getOSMajorVersion() >= 11;
162 if (triple.getOS() == llvm::Triple::MacOSX) {
163 unsigned Major, Minor, Micro;
164 triple.getOSVersion(Major, Minor, Micro);
165 return Major > 10 || (Major == 10 && Minor >= 7);
171 static CompilerInvocation *
172 createInvocationForMigration(CompilerInvocation &origCI,
173 const PCHContainerReader &PCHContainerRdr) {
174 std::unique_ptr<CompilerInvocation> CInvok;
175 CInvok.reset(new CompilerInvocation(origCI));
176 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
177 if (!PPOpts.ImplicitPCHInclude.empty()) {
178 // We can't use a PCH because it was likely built in non-ARC mode and we
179 // want to parse in ARC. Include the original header.
180 FileManager FileMgr(origCI.getFileSystemOpts());
181 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
182 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
183 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
184 new IgnoringDiagConsumer()));
185 std::string OriginalFile = ASTReader::getOriginalSourceFile(
186 PPOpts.ImplicitPCHInclude, FileMgr, PCHContainerRdr, *Diags);
187 if (!OriginalFile.empty())
188 PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
189 PPOpts.ImplicitPCHInclude.clear();
191 // FIXME: Get the original header of a PTH as well.
192 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
193 std::string define = getARCMTMacroName();
195 CInvok->getPreprocessorOpts().addMacroDef(define);
196 CInvok->getLangOpts()->ObjCAutoRefCount = true;
197 CInvok->getLangOpts()->setGC(LangOptions::NonGC);
198 CInvok->getDiagnosticOpts().ErrorLimit = 0;
199 CInvok->getDiagnosticOpts().PedanticErrors = 0;
201 // Ignore -Werror flags when migrating.
202 std::vector<std::string> WarnOpts;
203 for (std::vector<std::string>::iterator
204 I = CInvok->getDiagnosticOpts().Warnings.begin(),
205 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
206 if (!StringRef(*I).startswith("error"))
207 WarnOpts.push_back(*I);
209 WarnOpts.push_back("error=arc-unsafe-retained-assign");
210 CInvok->getDiagnosticOpts().Warnings = std::move(WarnOpts);
212 CInvok->getLangOpts()->ObjCWeakRuntime = HasARCRuntime(origCI);
213 CInvok->getLangOpts()->ObjCWeak = CInvok->getLangOpts()->ObjCWeakRuntime;
215 return CInvok.release();
218 static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
219 DiagnosticOptions *diagOpts,
221 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
222 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
223 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
224 new DiagnosticsEngine(DiagID, diagOpts, &printer,
225 /*ShouldOwnClient=*/false));
226 Diags->setSourceManager(&PP.getSourceManager());
228 printer.BeginSourceFile(PP.getLangOpts(), &PP);
229 arcDiags.reportDiagnostics(*Diags);
230 printer.EndSourceFile();
233 //===----------------------------------------------------------------------===//
234 // checkForManualIssues.
235 //===----------------------------------------------------------------------===//
237 bool arcmt::checkForManualIssues(
238 CompilerInvocation &origCI, const FrontendInputFile &Input,
239 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
240 DiagnosticConsumer *DiagClient, bool emitPremigrationARCErrors,
241 StringRef plistOut) {
242 if (!origCI.getLangOpts()->ObjC1)
245 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
246 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
247 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
249 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
251 assert(!transforms.empty());
253 std::unique_ptr<CompilerInvocation> CInvok;
255 createInvocationForMigration(origCI, PCHContainerOps->getRawReader()));
256 CInvok->getFrontendOpts().Inputs.clear();
257 CInvok->getFrontendOpts().Inputs.push_back(Input);
259 CapturedDiagList capturedDiags;
262 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
263 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
264 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
265 DiagClient, /*ShouldOwnClient=*/false));
267 // Filter of all diagnostics.
268 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
269 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
271 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
272 CInvok.release(), PCHContainerOps, Diags));
274 errRec.FinishCapture();
278 // Don't filter diagnostics anymore.
279 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
281 ASTContext &Ctx = Unit->getASTContext();
283 if (Diags->hasFatalErrorOccurred()) {
285 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
286 capturedDiags.reportDiagnostics(*Diags);
287 DiagClient->EndSourceFile();
288 errRec.FinishCapture();
292 if (emitPremigrationARCErrors)
293 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
294 Unit->getPreprocessor());
295 if (!plistOut.empty()) {
296 SmallVector<StoredDiagnostic, 8> arcDiags;
297 for (CapturedDiagList::iterator
298 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
299 arcDiags.push_back(*I);
300 writeARCDiagsToPlist(plistOut, arcDiags,
301 Ctx.getSourceManager(), Ctx.getLangOpts());
304 // After parsing of source files ended, we want to reuse the
305 // diagnostics objects to emit further diagnostics.
306 // We call BeginSourceFile because DiagnosticConsumer requires that
307 // diagnostics with source range information are emitted only in between
308 // BeginSourceFile() and EndSourceFile().
309 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
311 // No macros will be added since we are just checking and we won't modify
313 std::vector<SourceLocation> ARCMTMacroLocs;
315 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
316 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
318 pass.setNoFinalizeRemoval(NoFinalizeRemoval);
319 if (!NoNSAllocReallocError)
320 Diags->setSeverity(diag::warn_arcmt_nsalloc_realloc, diag::Severity::Error,
323 for (unsigned i=0, e = transforms.size(); i != e; ++i)
326 capturedDiags.reportDiagnostics(*Diags);
328 DiagClient->EndSourceFile();
329 errRec.FinishCapture();
331 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
334 //===----------------------------------------------------------------------===//
335 // applyTransformations.
336 //===----------------------------------------------------------------------===//
339 applyTransforms(CompilerInvocation &origCI, const FrontendInputFile &Input,
340 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
341 DiagnosticConsumer *DiagClient, StringRef outputDir,
342 bool emitPremigrationARCErrors, StringRef plistOut) {
343 if (!origCI.getLangOpts()->ObjC1)
346 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
348 // Make sure checking is successful first.
349 CompilerInvocation CInvokForCheck(origCI);
350 if (arcmt::checkForManualIssues(CInvokForCheck, Input, PCHContainerOps,
351 DiagClient, emitPremigrationARCErrors,
355 CompilerInvocation CInvok(origCI);
356 CInvok.getFrontendOpts().Inputs.clear();
357 CInvok.getFrontendOpts().Inputs.push_back(Input);
359 MigrationProcess migration(CInvok, PCHContainerOps, DiagClient, outputDir);
360 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
362 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
364 assert(!transforms.empty());
366 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
367 bool err = migration.applyTransform(transforms[i]);
368 if (err) return true;
371 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
372 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
373 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
374 DiagClient, /*ShouldOwnClient=*/false));
376 if (outputDir.empty()) {
377 origCI.getLangOpts()->ObjCAutoRefCount = true;
378 return migration.getRemapper().overwriteOriginal(*Diags);
380 return migration.getRemapper().flushToDisk(outputDir, *Diags);
384 bool arcmt::applyTransformations(
385 CompilerInvocation &origCI, const FrontendInputFile &Input,
386 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
387 DiagnosticConsumer *DiagClient) {
388 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient,
389 StringRef(), false, StringRef());
392 bool arcmt::migrateWithTemporaryFiles(
393 CompilerInvocation &origCI, const FrontendInputFile &Input,
394 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
395 DiagnosticConsumer *DiagClient, StringRef outputDir,
396 bool emitPremigrationARCErrors, StringRef plistOut) {
397 assert(!outputDir.empty() && "Expected output directory path");
398 return applyTransforms(origCI, Input, PCHContainerOps, DiagClient, outputDir,
399 emitPremigrationARCErrors, plistOut);
402 bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
405 DiagnosticConsumer *DiagClient) {
406 assert(!outputDir.empty());
408 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
409 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
410 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
411 DiagClient, /*ShouldOwnClient=*/false));
413 FileRemapper remapper;
414 bool err = remapper.initFromDisk(outputDir, *Diags,
415 /*ignoreIfFilesChanged=*/true);
419 PreprocessorOptions PPOpts;
420 remapper.applyMappings(PPOpts);
421 remap = PPOpts.RemappedFiles;
427 //===----------------------------------------------------------------------===//
428 // CollectTransformActions.
429 //===----------------------------------------------------------------------===//
433 class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
434 std::vector<SourceLocation> &ARCMTMacroLocs;
437 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
438 : ARCMTMacroLocs(ARCMTMacroLocs) { }
440 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
441 SourceRange Range, const MacroArgs *Args) override {
442 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
443 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
447 class ARCMTMacroTrackerAction : public ASTFrontendAction {
448 std::vector<SourceLocation> &ARCMTMacroLocs;
451 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
452 : ARCMTMacroLocs(ARCMTMacroLocs) { }
454 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
455 StringRef InFile) override {
456 CI.getPreprocessor().addPPCallbacks(
457 llvm::make_unique<ARCMTMacroTrackerPPCallbacks>(ARCMTMacroLocs));
458 return llvm::make_unique<ASTConsumer>();
462 class RewritesApplicator : public TransformActions::RewriteReceiver {
464 MigrationProcess::RewriteListener *Listener;
467 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
468 MigrationProcess::RewriteListener *listener)
469 : rewriter(rewriter), Listener(listener) {
471 Listener->start(ctx);
473 ~RewritesApplicator() override {
478 void insert(SourceLocation loc, StringRef text) override {
479 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
480 /*indentNewLines=*/true);
481 if (!err && Listener)
482 Listener->insert(loc, text);
485 void remove(CharSourceRange range) override {
486 Rewriter::RewriteOptions removeOpts;
487 removeOpts.IncludeInsertsAtBeginOfRange = false;
488 removeOpts.IncludeInsertsAtEndOfRange = false;
489 removeOpts.RemoveLineIfEmpty = true;
491 bool err = rewriter.RemoveText(range, removeOpts);
492 if (!err && Listener)
493 Listener->remove(range);
496 void increaseIndentation(CharSourceRange range,
497 SourceLocation parentIndent) override {
498 rewriter.IncreaseIndentation(range, parentIndent);
502 } // end anonymous namespace.
504 /// \brief Anchor for VTable.
505 MigrationProcess::RewriteListener::~RewriteListener() { }
507 MigrationProcess::MigrationProcess(
508 const CompilerInvocation &CI,
509 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
510 DiagnosticConsumer *diagClient, StringRef outputDir)
511 : OrigCI(CI), PCHContainerOps(PCHContainerOps), DiagClient(diagClient),
512 HadARCErrors(false) {
513 if (!outputDir.empty()) {
514 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
515 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
516 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
517 DiagClient, /*ShouldOwnClient=*/false));
518 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
522 bool MigrationProcess::applyTransform(TransformFn trans,
523 RewriteListener *listener) {
524 std::unique_ptr<CompilerInvocation> CInvok;
526 createInvocationForMigration(OrigCI, PCHContainerOps->getRawReader()));
527 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
529 Remapper.applyMappings(CInvok->getPreprocessorOpts());
531 CapturedDiagList capturedDiags;
532 std::vector<SourceLocation> ARCMTMacroLocs;
535 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
536 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
537 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
538 DiagClient, /*ShouldOwnClient=*/false));
540 // Filter of all diagnostics.
541 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
542 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
544 std::unique_ptr<ARCMTMacroTrackerAction> ASTAction;
545 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
547 std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
548 CInvok.release(), PCHContainerOps, Diags, ASTAction.get()));
550 errRec.FinishCapture();
553 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
555 HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
557 // Don't filter diagnostics anymore.
558 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
560 ASTContext &Ctx = Unit->getASTContext();
562 if (Diags->hasFatalErrorOccurred()) {
564 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
565 capturedDiags.reportDiagnostics(*Diags);
566 DiagClient->EndSourceFile();
567 errRec.FinishCapture();
571 // After parsing of source files ended, we want to reuse the
572 // diagnostics objects to emit further diagnostics.
573 // We call BeginSourceFile because DiagnosticConsumer requires that
574 // diagnostics with source range information are emitted only in between
575 // BeginSourceFile() and EndSourceFile().
576 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
578 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
579 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
580 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
581 Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs);
586 RewritesApplicator applicator(rewriter, Ctx, listener);
587 TA.applyRewrites(applicator);
590 DiagClient->EndSourceFile();
591 errRec.FinishCapture();
593 if (DiagClient->getNumErrors())
596 for (Rewriter::buffer_iterator
597 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
598 FileID FID = I->first;
599 RewriteBuffer &buf = I->second;
600 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
602 std::string newFname = file->getName();
603 newFname += "-trans";
604 SmallString<512> newText;
605 llvm::raw_svector_ostream vecOS(newText);
607 std::unique_ptr<llvm::MemoryBuffer> memBuf(
608 llvm::MemoryBuffer::getMemBufferCopy(
609 StringRef(newText.data(), newText.size()), newFname));
610 SmallString<64> filePath(file->getName());
611 Unit->getFileManager().FixupRelativePath(filePath);
612 Remapper.remap(filePath.str(), std::move(memBuf));