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/Frontend/ASTUnit.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Frontend/FrontendAction.h"
14 #include "clang/Frontend/TextDiagnosticPrinter.h"
15 #include "clang/Frontend/Utils.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/Rewrite/Core/Rewriter.h"
18 #include "clang/Sema/SemaDiagnostic.h"
19 #include "clang/Basic/DiagnosticCategories.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/ADT/Triple.h"
23 using namespace clang;
24 using namespace arcmt;
26 bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs,
28 if (range.isInvalid())
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()))) {
41 ListTy::iterator eraseS = I++;
42 while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
44 // Clear the diagnostic and any notes following it.
45 I = List.erase(eraseS, I);
55 bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs,
56 SourceRange range) const {
57 if (range.isInvalid())
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()))) {
77 void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const {
78 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
82 bool CapturedDiagList::hasErrors() const {
83 for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
84 if (I->getLevel() >= DiagnosticsEngine::Error)
92 class CaptureDiagnosticConsumer : public DiagnosticConsumer {
93 DiagnosticsEngine &Diags;
94 DiagnosticConsumer &DiagClient;
95 CapturedDiagList &CapturedDiags;
96 bool HasBegunSourceFile;
98 CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
99 DiagnosticConsumer &client,
100 CapturedDiagList &capturedDiags)
101 : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
102 HasBegunSourceFile(false) { }
104 virtual void BeginSourceFile(const LangOptions &Opts,
105 const Preprocessor *PP) {
106 // Pass BeginSourceFile message onto DiagClient on first call.
107 // The corresponding EndSourceFile call will be made from an
108 // explicit call to FinishCapture.
109 if (!HasBegunSourceFile) {
110 DiagClient.BeginSourceFile(Opts, PP);
111 HasBegunSourceFile = true;
115 void FinishCapture() {
116 // Call EndSourceFile on DiagClient on completion of capture to
117 // enable VerifyDiagnosticConsumer to check diagnostics *after*
118 // it has received the diagnostic list.
119 if (HasBegunSourceFile) {
120 DiagClient.EndSourceFile();
121 HasBegunSourceFile = false;
125 virtual ~CaptureDiagnosticConsumer() {
126 assert(!HasBegunSourceFile && "FinishCapture not called!");
129 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
130 const Diagnostic &Info) {
131 if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
132 level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
133 CapturedDiags.push_back(StoredDiagnostic(level, Info));
137 // Non-ARC warnings are ignored.
138 Diags.setLastDiagnosticIgnored();
141 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
142 // Just drop any diagnostics that come from cloned consumers; they'll
143 // have different source managers anyway.
144 return new IgnoringDiagConsumer();
148 } // end anonymous namespace
150 static bool HasARCRuntime(CompilerInvocation &origCI) {
151 // This duplicates some functionality from Darwin::AddDeploymentTarget
152 // but this function is well defined, so keep it decoupled from the driver
153 // and avoid unrelated complications.
154 llvm::Triple triple(origCI.getTargetOpts().Triple);
156 if (triple.getOS() == llvm::Triple::IOS)
157 return triple.getOSMajorVersion() >= 5;
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 OwningPtr<CompilerInvocation> CInvok;
174 CInvok.reset(new CompilerInvocation(origCI));
175 CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string();
176 CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string();
177 std::string define = getARCMTMacroName();
179 CInvok->getPreprocessorOpts().addMacroDef(define);
180 CInvok->getLangOpts()->ObjCAutoRefCount = true;
181 CInvok->getLangOpts()->setGC(LangOptions::NonGC);
182 CInvok->getDiagnosticOpts().ErrorLimit = 0;
183 CInvok->getDiagnosticOpts().PedanticErrors = 0;
185 // Ignore -Werror flags when migrating.
186 std::vector<std::string> WarnOpts;
187 for (std::vector<std::string>::iterator
188 I = CInvok->getDiagnosticOpts().Warnings.begin(),
189 E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
190 if (!StringRef(*I).startswith("error"))
191 WarnOpts.push_back(*I);
193 WarnOpts.push_back("error=arc-unsafe-retained-assign");
194 CInvok->getDiagnosticOpts().Warnings = llvm_move(WarnOpts);
196 CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI);
198 return CInvok.take();
201 static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
202 DiagnosticOptions *diagOpts,
204 TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
205 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
206 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
207 new DiagnosticsEngine(DiagID, diagOpts, &printer,
208 /*ShouldOwnClient=*/false));
209 Diags->setSourceManager(&PP.getSourceManager());
211 printer.BeginSourceFile(PP.getLangOpts(), &PP);
212 arcDiags.reportDiagnostics(*Diags);
213 printer.EndSourceFile();
216 //===----------------------------------------------------------------------===//
217 // checkForManualIssues.
218 //===----------------------------------------------------------------------===//
220 bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
221 const FrontendInputFile &Input,
222 DiagnosticConsumer *DiagClient,
223 bool emitPremigrationARCErrors,
224 StringRef plistOut) {
225 if (!origCI.getLangOpts()->ObjC1)
228 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
229 bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
230 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
232 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
234 assert(!transforms.empty());
236 OwningPtr<CompilerInvocation> CInvok;
237 CInvok.reset(createInvocationForMigration(origCI));
238 CInvok->getFrontendOpts().Inputs.clear();
239 CInvok->getFrontendOpts().Inputs.push_back(Input);
241 CapturedDiagList capturedDiags;
244 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
245 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
246 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
247 DiagClient, /*ShouldOwnClient=*/false));
249 // Filter of all diagnostics.
250 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
251 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
253 OwningPtr<ASTUnit> Unit(
254 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
256 errRec.FinishCapture();
260 // Don't filter diagnostics anymore.
261 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
263 ASTContext &Ctx = Unit->getASTContext();
265 if (Diags->hasFatalErrorOccurred()) {
267 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
268 capturedDiags.reportDiagnostics(*Diags);
269 DiagClient->EndSourceFile();
270 errRec.FinishCapture();
274 if (emitPremigrationARCErrors)
275 emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
276 Unit->getPreprocessor());
277 if (!plistOut.empty()) {
278 SmallVector<StoredDiagnostic, 8> arcDiags;
279 for (CapturedDiagList::iterator
280 I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
281 arcDiags.push_back(*I);
282 writeARCDiagsToPlist(plistOut, arcDiags,
283 Ctx.getSourceManager(), Ctx.getLangOpts());
286 // After parsing of source files ended, we want to reuse the
287 // diagnostics objects to emit further diagnostics.
288 // We call BeginSourceFile because DiagnosticConsumer requires that
289 // diagnostics with source range information are emitted only in between
290 // BeginSourceFile() and EndSourceFile().
291 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
293 // No macros will be added since we are just checking and we won't modify
295 std::vector<SourceLocation> ARCMTMacroLocs;
297 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
298 MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, ARCMTMacroLocs);
299 pass.setNSAllocReallocError(NoNSAllocReallocError);
300 pass.setNoFinalizeRemoval(NoFinalizeRemoval);
302 for (unsigned i=0, e = transforms.size(); i != e; ++i)
305 capturedDiags.reportDiagnostics(*Diags);
307 DiagClient->EndSourceFile();
308 errRec.FinishCapture();
310 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
311 // to remove it so that we don't get errors from normal compilation.
312 origCI.getLangOpts()->ObjCAutoRefCount = false;
314 return capturedDiags.hasErrors() || testAct.hasReportedErrors();
317 //===----------------------------------------------------------------------===//
318 // applyTransformations.
319 //===----------------------------------------------------------------------===//
321 static bool applyTransforms(CompilerInvocation &origCI,
322 const FrontendInputFile &Input,
323 DiagnosticConsumer *DiagClient,
325 bool emitPremigrationARCErrors,
326 StringRef plistOut) {
327 if (!origCI.getLangOpts()->ObjC1)
330 LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
332 // Make sure checking is successful first.
333 CompilerInvocation CInvokForCheck(origCI);
334 if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient,
335 emitPremigrationARCErrors, plistOut))
338 CompilerInvocation CInvok(origCI);
339 CInvok.getFrontendOpts().Inputs.clear();
340 CInvok.getFrontendOpts().Inputs.push_back(Input);
342 MigrationProcess migration(CInvok, DiagClient, outputDir);
343 bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
345 std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
347 assert(!transforms.empty());
349 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
350 bool err = migration.applyTransform(transforms[i]);
351 if (err) return true;
354 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
355 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
356 new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
357 DiagClient, /*ShouldOwnClient=*/false));
359 if (outputDir.empty()) {
360 origCI.getLangOpts()->ObjCAutoRefCount = true;
361 return migration.getRemapper().overwriteOriginal(*Diags);
363 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
364 // to remove it so that we don't get errors from normal compilation.
365 origCI.getLangOpts()->ObjCAutoRefCount = false;
366 return migration.getRemapper().flushToDisk(outputDir, *Diags);
370 bool arcmt::applyTransformations(CompilerInvocation &origCI,
371 const FrontendInputFile &Input,
372 DiagnosticConsumer *DiagClient) {
373 return applyTransforms(origCI, Input, DiagClient,
374 StringRef(), false, StringRef());
377 bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
378 const FrontendInputFile &Input,
379 DiagnosticConsumer *DiagClient,
381 bool emitPremigrationARCErrors,
382 StringRef plistOut) {
383 assert(!outputDir.empty() && "Expected output directory path");
384 return applyTransforms(origCI, Input, DiagClient,
385 outputDir, emitPremigrationARCErrors, plistOut);
388 bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
391 DiagnosticConsumer *DiagClient) {
392 assert(!outputDir.empty());
394 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
395 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
396 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
397 DiagClient, /*ShouldOwnClient=*/false));
399 FileRemapper remapper;
400 bool err = remapper.initFromDisk(outputDir, *Diags,
401 /*ignoreIfFilesChanged=*/true);
405 PreprocessorOptions PPOpts;
406 remapper.applyMappings(PPOpts);
407 remap = PPOpts.RemappedFiles;
412 bool arcmt::getFileRemappingsFromFileList(
413 std::vector<std::pair<std::string,std::string> > &remap,
414 ArrayRef<StringRef> remapFiles,
415 DiagnosticConsumer *DiagClient) {
416 bool hasErrorOccurred = false;
417 llvm::StringMap<bool> Uniquer;
419 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
420 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
421 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
422 DiagClient, /*ShouldOwnClient=*/false));
424 for (ArrayRef<StringRef>::iterator
425 I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) {
428 FileRemapper remapper;
429 bool err = remapper.initFromFile(file, *Diags,
430 /*ignoreIfFilesChanged=*/true);
431 hasErrorOccurred = hasErrorOccurred || err;
435 PreprocessorOptions PPOpts;
436 remapper.applyMappings(PPOpts);
437 for (PreprocessorOptions::remapped_file_iterator
438 RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end();
440 bool &inserted = Uniquer[RI->first];
444 remap.push_back(*RI);
448 return hasErrorOccurred;
451 //===----------------------------------------------------------------------===//
452 // CollectTransformActions.
453 //===----------------------------------------------------------------------===//
457 class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
458 std::vector<SourceLocation> &ARCMTMacroLocs;
461 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
462 : ARCMTMacroLocs(ARCMTMacroLocs) { }
464 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI,
466 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
467 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
471 class ARCMTMacroTrackerAction : public ASTFrontendAction {
472 std::vector<SourceLocation> &ARCMTMacroLocs;
475 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
476 : ARCMTMacroLocs(ARCMTMacroLocs) { }
478 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
480 CI.getPreprocessor().addPPCallbacks(
481 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
482 return new ASTConsumer();
486 class RewritesApplicator : public TransformActions::RewriteReceiver {
488 MigrationProcess::RewriteListener *Listener;
491 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
492 MigrationProcess::RewriteListener *listener)
493 : rewriter(rewriter), Listener(listener) {
495 Listener->start(ctx);
497 ~RewritesApplicator() {
502 virtual void insert(SourceLocation loc, StringRef text) {
503 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
504 /*indentNewLines=*/true);
505 if (!err && Listener)
506 Listener->insert(loc, text);
509 virtual void remove(CharSourceRange range) {
510 Rewriter::RewriteOptions removeOpts;
511 removeOpts.IncludeInsertsAtBeginOfRange = false;
512 removeOpts.IncludeInsertsAtEndOfRange = false;
513 removeOpts.RemoveLineIfEmpty = true;
515 bool err = rewriter.RemoveText(range, removeOpts);
516 if (!err && Listener)
517 Listener->remove(range);
520 virtual void increaseIndentation(CharSourceRange range,
521 SourceLocation parentIndent) {
522 rewriter.IncreaseIndentation(range, parentIndent);
526 } // end anonymous namespace.
528 /// \brief Anchor for VTable.
529 MigrationProcess::RewriteListener::~RewriteListener() { }
531 MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
532 DiagnosticConsumer *diagClient,
534 : OrigCI(CI), DiagClient(diagClient) {
535 if (!outputDir.empty()) {
536 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
537 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
538 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
539 DiagClient, /*ShouldOwnClient=*/false));
540 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
544 bool MigrationProcess::applyTransform(TransformFn trans,
545 RewriteListener *listener) {
546 OwningPtr<CompilerInvocation> CInvok;
547 CInvok.reset(createInvocationForMigration(OrigCI));
548 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
550 Remapper.applyMappings(CInvok->getPreprocessorOpts());
552 CapturedDiagList capturedDiags;
553 std::vector<SourceLocation> ARCMTMacroLocs;
556 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
557 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
558 new DiagnosticsEngine(DiagID, new DiagnosticOptions,
559 DiagClient, /*ShouldOwnClient=*/false));
561 // Filter of all diagnostics.
562 CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
563 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
565 OwningPtr<ARCMTMacroTrackerAction> ASTAction;
566 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
568 OwningPtr<ASTUnit> Unit(
569 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
572 errRec.FinishCapture();
575 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
577 // Don't filter diagnostics anymore.
578 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
580 ASTContext &Ctx = Unit->getASTContext();
582 if (Diags->hasFatalErrorOccurred()) {
584 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
585 capturedDiags.reportDiagnostics(*Diags);
586 DiagClient->EndSourceFile();
587 errRec.FinishCapture();
591 // After parsing of source files ended, we want to reuse the
592 // diagnostics objects to emit further diagnostics.
593 // We call BeginSourceFile because DiagnosticConsumer requires that
594 // diagnostics with source range information are emitted only in between
595 // BeginSourceFile() and EndSourceFile().
596 DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
598 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
599 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
600 MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
601 Unit->getSema(), TA, ARCMTMacroLocs);
606 RewritesApplicator applicator(rewriter, Ctx, listener);
607 TA.applyRewrites(applicator);
610 DiagClient->EndSourceFile();
611 errRec.FinishCapture();
613 if (DiagClient->getNumErrors())
616 for (Rewriter::buffer_iterator
617 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
618 FileID FID = I->first;
619 RewriteBuffer &buf = I->second;
620 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
622 std::string newFname = file->getName();
623 newFname += "-trans";
624 SmallString<512> newText;
625 llvm::raw_svector_ostream vecOS(newText);
628 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
629 StringRef(newText.data(), newText.size()), newFname);
630 SmallString<64> filePath(file->getName());
631 Unit->getFileManager().FixupRelativePath(filePath);
632 Remapper.remap(filePath.str(), memBuf);