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/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"
22 using namespace clang;
23 using namespace arcmt;
24 using llvm::StringRef;
26 bool CapturedDiagList::clearDiagnostic(llvm::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() == Diagnostic::Note)
44 // Clear the diagnostic and any notes following it.
45 List.erase(eraseS, I);
55 bool CapturedDiagList::hasDiagnostic(llvm::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(Diagnostic &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() >= Diagnostic::Error)
92 class CaptureDiagnosticClient : public DiagnosticClient {
94 CapturedDiagList &CapturedDiags;
96 CaptureDiagnosticClient(Diagnostic &diags,
97 CapturedDiagList &capturedDiags)
98 : Diags(diags), CapturedDiags(capturedDiags) { }
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));
108 // Non-ARC warnings are ignored.
109 Diags.setLastDiagnosticIgnored();
113 } // end anonymous namespace
115 static inline llvm::StringRef SimulatorVersionDefineName() {
116 return "__IPHONE_OS_VERSION_MIN_REQUIRED=";
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.
124 static bool GetVersionFromSimulatorDefine(llvm::StringRef define,
125 unsigned &Major, unsigned &Minor,
127 assert(define.startswith(SimulatorVersionDefineName()));
128 llvm::StringRef name, version;
129 llvm::tie(name, version) = define.split('=');
132 std::string verstr = version.str();
134 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
144 static 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.
149 for (unsigned i = 0, e = origCI.getPreprocessorOpts().Macros.size();
151 StringRef define = origCI.getPreprocessorOpts().Macros[i].first;
152 bool isUndef = origCI.getPreprocessorOpts().Macros[i].second;
155 if (!define.startswith(SimulatorVersionDefineName()))
157 unsigned Major, Minor, Micro;
158 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
159 Major < 10 && Minor < 100 && Micro < 100)
163 llvm::Triple triple(origCI.getTargetOpts().Triple);
165 if (triple.getOS() == llvm::Triple::IOS)
166 return triple.getOSMajorVersion() >= 5;
168 if (triple.getOS() == llvm::Triple::Darwin)
169 return triple.getOSMajorVersion() >= 11;
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);
180 CompilerInvocation *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();
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);
194 return CInvok.take();
197 //===----------------------------------------------------------------------===//
198 // checkForManualIssues.
199 //===----------------------------------------------------------------------===//
201 bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
202 llvm::StringRef Filename, InputKind Kind,
203 DiagnosticClient *DiagClient) {
204 if (!origCI.getLangOpts().ObjC1)
207 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
208 assert(!transforms.empty());
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));
215 CapturedDiagList capturedDiags;
218 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
219 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
220 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
222 // Filter of all diagnostics.
223 CaptureDiagnosticClient errRec(*Diags, capturedDiags);
224 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
226 llvm::OwningPtr<ASTUnit> Unit(
227 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
231 // Don't filter diagnostics anymore.
232 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
234 ASTContext &Ctx = Unit->getASTContext();
236 if (Diags->hasFatalErrorOccurred()) {
238 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
239 capturedDiags.reportDiagnostics(*Diags);
240 DiagClient->EndSourceFile();
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());
251 // No macros will be added since we are just checking and we won't modify
253 std::vector<SourceLocation> ARCMTMacroLocs;
255 TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
256 MigrationPass pass(Ctx, Unit->getSema(), testAct, ARCMTMacroLocs);
258 for (unsigned i=0, e = transforms.size(); i != e; ++i)
261 capturedDiags.reportDiagnostics(*Diags);
263 DiagClient->EndSourceFile();
265 // If we are migrating code that gets the '-fobjc-arc' flag, make sure
266 // to remove it so that we don't get errors from normal compilation.
267 origCI.getLangOpts().ObjCAutoRefCount = false;
269 return capturedDiags.hasErrors();
272 //===----------------------------------------------------------------------===//
273 // applyTransformations.
274 //===----------------------------------------------------------------------===//
276 static bool applyTransforms(CompilerInvocation &origCI,
277 llvm::StringRef Filename, InputKind Kind,
278 DiagnosticClient *DiagClient,
279 llvm::StringRef outputDir) {
280 if (!origCI.getLangOpts().ObjC1)
283 // Make sure checking is successful first.
284 CompilerInvocation CInvokForCheck(origCI);
285 if (arcmt::checkForManualIssues(CInvokForCheck, Filename, Kind, DiagClient))
288 CompilerInvocation CInvok(origCI);
289 CInvok.getFrontendOpts().Inputs.clear();
290 CInvok.getFrontendOpts().Inputs.push_back(std::make_pair(Kind, Filename));
292 MigrationProcess migration(CInvok, DiagClient, outputDir);
294 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
295 assert(!transforms.empty());
297 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
298 bool err = migration.applyTransform(transforms[i]);
299 if (err) return true;
302 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
303 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
304 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
306 if (outputDir.empty()) {
307 origCI.getLangOpts().ObjCAutoRefCount = true;
308 return migration.getRemapper().overwriteOriginal(*Diags);
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;
313 return migration.getRemapper().flushToDisk(outputDir, *Diags);
317 bool arcmt::applyTransformations(CompilerInvocation &origCI,
318 llvm::StringRef Filename, InputKind Kind,
319 DiagnosticClient *DiagClient) {
320 return applyTransforms(origCI, Filename, Kind, DiagClient, llvm::StringRef());
323 bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
324 llvm::StringRef Filename, InputKind Kind,
325 DiagnosticClient *DiagClient,
326 llvm::StringRef outputDir) {
327 assert(!outputDir.empty() && "Expected output directory path");
328 return applyTransforms(origCI, Filename, Kind, DiagClient, outputDir);
331 bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
333 llvm::StringRef outputDir,
334 DiagnosticClient *DiagClient) {
335 assert(!outputDir.empty());
337 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
338 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
339 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
341 FileRemapper remapper;
342 bool err = remapper.initFromDisk(outputDir, *Diags,
343 /*ignoreIfFilesChanged=*/true);
347 CompilerInvocation CI;
348 remapper.applyMappings(CI);
349 remap = CI.getPreprocessorOpts().RemappedFiles;
354 //===----------------------------------------------------------------------===//
355 // CollectTransformActions.
356 //===----------------------------------------------------------------------===//
360 class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
361 std::vector<SourceLocation> &ARCMTMacroLocs;
364 ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
365 : ARCMTMacroLocs(ARCMTMacroLocs) { }
367 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI) {
368 if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
369 ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
373 class ARCMTMacroTrackerAction : public ASTFrontendAction {
374 std::vector<SourceLocation> &ARCMTMacroLocs;
377 ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
378 : ARCMTMacroLocs(ARCMTMacroLocs) { }
380 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
381 llvm::StringRef InFile) {
382 CI.getPreprocessor().addPPCallbacks(
383 new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
384 return new ASTConsumer();
388 class RewritesApplicator : public TransformActions::RewriteReceiver {
391 MigrationProcess::RewriteListener *Listener;
394 RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
395 MigrationProcess::RewriteListener *listener)
396 : rewriter(rewriter), Ctx(ctx), Listener(listener) {
398 Listener->start(ctx);
400 ~RewritesApplicator() {
405 virtual void insert(SourceLocation loc, llvm::StringRef text) {
406 bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
407 /*indentNewLines=*/true);
408 if (!err && Listener)
409 Listener->insert(loc, text);
412 virtual void remove(CharSourceRange range) {
413 Rewriter::RewriteOptions removeOpts;
414 removeOpts.IncludeInsertsAtBeginOfRange = false;
415 removeOpts.IncludeInsertsAtEndOfRange = false;
416 removeOpts.RemoveLineIfEmpty = true;
418 bool err = rewriter.RemoveText(range, removeOpts);
419 if (!err && Listener)
420 Listener->remove(range);
423 virtual void increaseIndentation(CharSourceRange range,
424 SourceLocation parentIndent) {
425 rewriter.IncreaseIndentation(range, parentIndent);
429 } // end anonymous namespace.
431 /// \brief Anchor for VTable.
432 MigrationProcess::RewriteListener::~RewriteListener() { }
434 MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
435 DiagnosticClient *diagClient,
436 llvm::StringRef outputDir)
437 : OrigCI(CI), DiagClient(diagClient) {
438 if (!outputDir.empty()) {
439 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
440 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
441 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
442 Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
446 bool MigrationProcess::applyTransform(TransformFn trans,
447 RewriteListener *listener) {
448 llvm::OwningPtr<CompilerInvocation> CInvok;
449 CInvok.reset(createInvocationForMigration(OrigCI));
450 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
452 Remapper.applyMappings(*CInvok);
454 CapturedDiagList capturedDiags;
455 std::vector<SourceLocation> ARCMTMacroLocs;
458 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
459 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(
460 new Diagnostic(DiagID, DiagClient, /*ShouldOwnClient=*/false));
462 // Filter of all diagnostics.
463 CaptureDiagnosticClient errRec(*Diags, capturedDiags);
464 Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
466 llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction;
467 ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
469 llvm::OwningPtr<ASTUnit> Unit(
470 ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
474 Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
476 // Don't filter diagnostics anymore.
477 Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
479 ASTContext &Ctx = Unit->getASTContext();
481 if (Diags->hasFatalErrorOccurred()) {
483 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
484 capturedDiags.reportDiagnostics(*Diags);
485 DiagClient->EndSourceFile();
489 // After parsing of source files ended, we want to reuse the
490 // diagnostics objects to emit further diagnostics.
491 // We call BeginSourceFile because DiagnosticClient requires that
492 // diagnostics with source range information are emitted only in between
493 // BeginSourceFile() and EndSourceFile().
494 DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
496 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions());
497 TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
498 MigrationPass pass(Ctx, Unit->getSema(), TA, ARCMTMacroLocs);
503 RewritesApplicator applicator(rewriter, Ctx, listener);
504 TA.applyRewrites(applicator);
507 DiagClient->EndSourceFile();
509 if (DiagClient->getNumErrors())
512 for (Rewriter::buffer_iterator
513 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
514 FileID FID = I->first;
515 RewriteBuffer &buf = I->second;
516 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
518 std::string newFname = file->getName();
519 newFname += "-trans";
520 llvm::SmallString<512> newText;
521 llvm::raw_svector_ostream vecOS(newText);
524 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
525 llvm::StringRef(newText.data(), newText.size()), newFname);
526 llvm::SmallString<64> filePath(file->getName());
527 Unit->getFileManager().FixupRelativePath(filePath);
528 Remapper.remap(filePath.str(), memBuf);
534 //===----------------------------------------------------------------------===//
536 //===----------------------------------------------------------------------===//
538 bool arcmt::isARCDiagnostic(unsigned diagID, Diagnostic &Diag) {
539 return Diag.getDiagnosticIDs()->getCategoryNumberForDiag(diagID) ==
540 diag::DiagCat_Automatic_Reference_Counting_Issue;