ARCMT.cpp revision 1f6b2b5c82b2d2d3935b0db76352a04e9877b73f
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/FrontendAction.h"
14#include "clang/Frontend/TextDiagnosticPrinter.h"
15#include "clang/Frontend/Utils.h"
16#include "clang/AST/ASTConsumer.h"
17#include "clang/Rewrite/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"
23using namespace clang;
24using namespace arcmt;
25
26bool CapturedDiagList::clearDiagnostic(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() == DiagnosticsEngine::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(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(DiagnosticsEngine &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() >= DiagnosticsEngine::Error)
85      return true;
86
87  return false;
88}
89
90namespace {
91
92class CaptureDiagnosticConsumer : public DiagnosticConsumer {
93  DiagnosticsEngine &Diags;
94  CapturedDiagList &CapturedDiags;
95public:
96  CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
97                           CapturedDiagList &capturedDiags)
98    : Diags(diags), CapturedDiags(capturedDiags) { }
99
100  virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
101                                const Diagnostic &Info) {
102    if (DiagnosticIDs::isARCDiagnostic(Info.getID()) ||
103        level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
104      CapturedDiags.push_back(StoredDiagnostic(level, Info));
105      return;
106    }
107
108    // Non-ARC warnings are ignored.
109    Diags.setLastDiagnosticIgnored();
110  }
111
112  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
113    // Just drop any diagnostics that come from cloned consumers; they'll
114    // have different source managers anyway.
115    return new IgnoringDiagConsumer();
116  }
117};
118
119} // end anonymous namespace
120
121static inline StringRef SimulatorVersionDefineName() {
122  return "__IPHONE_OS_VERSION_MIN_REQUIRED=";
123}
124
125/// \brief Parse the simulator version define:
126/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
127// and return the grouped values as integers, e.g:
128//   __IPHONE_OS_VERSION_MIN_REQUIRED=40201
129// will return Major=4, Minor=2, Micro=1.
130static bool GetVersionFromSimulatorDefine(StringRef define,
131                                          unsigned &Major, unsigned &Minor,
132                                          unsigned &Micro) {
133  assert(define.startswith(SimulatorVersionDefineName()));
134  StringRef name, version;
135  llvm::tie(name, version) = define.split('=');
136  if (version.empty())
137    return false;
138  std::string verstr = version.str();
139  char *end;
140  unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
141  if (*end != '\0')
142    return false;
143  Major = num / 10000;
144  num = num % 10000;
145  Minor = num / 100;
146  Micro = num % 100;
147  return true;
148}
149
150static 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
155  for (unsigned i = 0, e = origCI.getPreprocessorOpts().Macros.size();
156         i != e; ++i) {
157    StringRef define = origCI.getPreprocessorOpts().Macros[i].first;
158    bool isUndef = origCI.getPreprocessorOpts().Macros[i].second;
159    if (isUndef)
160      continue;
161    if (!define.startswith(SimulatorVersionDefineName()))
162      continue;
163    unsigned Major = 0, Minor = 0, Micro = 0;
164    if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
165        Major < 10 && Minor < 100 && Micro < 100)
166      return Major >= 5;
167  }
168
169  llvm::Triple triple(origCI.getTargetOpts().Triple);
170
171  if (triple.getOS() == llvm::Triple::IOS)
172    return triple.getOSMajorVersion() >= 5;
173
174  if (triple.getOS() == llvm::Triple::Darwin)
175    return triple.getOSMajorVersion() >= 11;
176
177  if (triple.getOS() == llvm::Triple::MacOSX) {
178    unsigned Major, Minor, Micro;
179    triple.getOSVersion(Major, Minor, Micro);
180    return Major > 10 || (Major == 10 && Minor >= 7);
181  }
182
183  return false;
184}
185
186static CompilerInvocation *
187createInvocationForMigration(CompilerInvocation &origCI) {
188  llvm::OwningPtr<CompilerInvocation> CInvok;
189  CInvok.reset(new CompilerInvocation(origCI));
190  CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string();
191  CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string();
192  std::string define = getARCMTMacroName();
193  define += '=';
194  CInvok->getPreprocessorOpts().addMacroDef(define);
195  CInvok->getLangOpts()->ObjCAutoRefCount = true;
196  CInvok->getLangOpts()->setGC(LangOptions::NonGC);
197  CInvok->getDiagnosticOpts().ErrorLimit = 0;
198  CInvok->getDiagnosticOpts().Warnings.push_back(
199                                            "error=arc-unsafe-retained-assign");
200  CInvok->getLangOpts()->ObjCRuntimeHasWeak = HasARCRuntime(origCI);
201
202  return CInvok.take();
203}
204
205static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
206                                   const DiagnosticOptions &diagOpts,
207                                   Preprocessor &PP) {
208  TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
209  llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
210  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
211      new DiagnosticsEngine(DiagID, &printer, /*ShouldOwnClient=*/false));
212  Diags->setSourceManager(&PP.getSourceManager());
213
214  printer.BeginSourceFile(PP.getLangOptions(), &PP);
215  arcDiags.reportDiagnostics(*Diags);
216  printer.EndSourceFile();
217}
218
219//===----------------------------------------------------------------------===//
220// checkForManualIssues.
221//===----------------------------------------------------------------------===//
222
223bool arcmt::checkForManualIssues(CompilerInvocation &origCI,
224                                 const FrontendInputFile &Input,
225                                 DiagnosticConsumer *DiagClient,
226                                 bool emitPremigrationARCErrors,
227                                 StringRef plistOut) {
228  if (!origCI.getLangOpts()->ObjC1)
229    return false;
230
231  LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
232
233  std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode);
234  assert(!transforms.empty());
235
236  llvm::OwningPtr<CompilerInvocation> CInvok;
237  CInvok.reset(createInvocationForMigration(origCI));
238  CInvok->getFrontendOpts().Inputs.clear();
239  CInvok->getFrontendOpts().Inputs.push_back(Input);
240
241  CapturedDiagList capturedDiags;
242
243  assert(DiagClient);
244  llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
245  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
246      new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false));
247
248  // Filter of all diagnostics.
249  CaptureDiagnosticConsumer errRec(*Diags, capturedDiags);
250  Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
251
252  llvm::OwningPtr<ASTUnit> Unit(
253      ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags));
254  if (!Unit)
255    return true;
256
257  // Don't filter diagnostics anymore.
258  Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
259
260  ASTContext &Ctx = Unit->getASTContext();
261
262  if (Diags->hasFatalErrorOccurred()) {
263    Diags->Reset();
264    DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
265    capturedDiags.reportDiagnostics(*Diags);
266    DiagClient->EndSourceFile();
267    return true;
268  }
269
270  if (emitPremigrationARCErrors)
271    emitPremigrationErrors(capturedDiags, origCI.getDiagnosticOpts(),
272                           Unit->getPreprocessor());
273  if (!plistOut.empty()) {
274    SmallVector<StoredDiagnostic, 8> arcDiags;
275    for (CapturedDiagList::iterator
276           I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
277      arcDiags.push_back(*I);
278    writeARCDiagsToPlist(plistOut, arcDiags,
279                         Ctx.getSourceManager(), Ctx.getLangOptions());
280  }
281
282  // After parsing of source files ended, we want to reuse the
283  // diagnostics objects to emit further diagnostics.
284  // We call BeginSourceFile because DiagnosticConsumer requires that
285  // diagnostics with source range information are emitted only in between
286  // BeginSourceFile() and EndSourceFile().
287  DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
288
289  // No macros will be added since we are just checking and we won't modify
290  // source code.
291  std::vector<SourceLocation> ARCMTMacroLocs;
292
293  TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
294  MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, ARCMTMacroLocs);
295
296  for (unsigned i=0, e = transforms.size(); i != e; ++i)
297    transforms[i](pass);
298
299  capturedDiags.reportDiagnostics(*Diags);
300
301  DiagClient->EndSourceFile();
302
303  // If we are migrating code that gets the '-fobjc-arc' flag, make sure
304  // to remove it so that we don't get errors from normal compilation.
305  origCI.getLangOpts()->ObjCAutoRefCount = false;
306
307  return capturedDiags.hasErrors() || testAct.hasReportedErrors();
308}
309
310//===----------------------------------------------------------------------===//
311// applyTransformations.
312//===----------------------------------------------------------------------===//
313
314static bool applyTransforms(CompilerInvocation &origCI,
315                            const FrontendInputFile &Input,
316                            DiagnosticConsumer *DiagClient,
317                            StringRef outputDir,
318                            bool emitPremigrationARCErrors,
319                            StringRef plistOut) {
320  if (!origCI.getLangOpts()->ObjC1)
321    return false;
322
323  LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
324
325  // Make sure checking is successful first.
326  CompilerInvocation CInvokForCheck(origCI);
327  if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient,
328                                  emitPremigrationARCErrors, plistOut))
329    return true;
330
331  CompilerInvocation CInvok(origCI);
332  CInvok.getFrontendOpts().Inputs.clear();
333  CInvok.getFrontendOpts().Inputs.push_back(Input);
334
335  MigrationProcess migration(CInvok, DiagClient, outputDir);
336
337  std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode);
338  assert(!transforms.empty());
339
340  for (unsigned i=0, e = transforms.size(); i != e; ++i) {
341    bool err = migration.applyTransform(transforms[i]);
342    if (err) return true;
343  }
344
345  llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
346  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
347      new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false));
348
349  if (outputDir.empty()) {
350    origCI.getLangOpts()->ObjCAutoRefCount = true;
351    return migration.getRemapper().overwriteOriginal(*Diags);
352  } else {
353    // If we are migrating code that gets the '-fobjc-arc' flag, make sure
354    // to remove it so that we don't get errors from normal compilation.
355    origCI.getLangOpts()->ObjCAutoRefCount = false;
356    return migration.getRemapper().flushToDisk(outputDir, *Diags);
357  }
358}
359
360bool arcmt::applyTransformations(CompilerInvocation &origCI,
361                                 const FrontendInputFile &Input,
362                                 DiagnosticConsumer *DiagClient) {
363  return applyTransforms(origCI, Input, DiagClient,
364                         StringRef(), false, StringRef());
365}
366
367bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI,
368                                      const FrontendInputFile &Input,
369                                      DiagnosticConsumer *DiagClient,
370                                      StringRef outputDir,
371                                      bool emitPremigrationARCErrors,
372                                      StringRef plistOut) {
373  assert(!outputDir.empty() && "Expected output directory path");
374  return applyTransforms(origCI, Input, DiagClient,
375                         outputDir, emitPremigrationARCErrors, plistOut);
376}
377
378bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
379                                  remap,
380                              StringRef outputDir,
381                              DiagnosticConsumer *DiagClient) {
382  assert(!outputDir.empty());
383
384  llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
385  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
386      new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false));
387
388  FileRemapper remapper;
389  bool err = remapper.initFromDisk(outputDir, *Diags,
390                                   /*ignoreIfFilesChanged=*/true);
391  if (err)
392    return true;
393
394  CompilerInvocation CI;
395  remapper.applyMappings(CI);
396  remap = CI.getPreprocessorOpts().RemappedFiles;
397
398  return false;
399}
400
401//===----------------------------------------------------------------------===//
402// CollectTransformActions.
403//===----------------------------------------------------------------------===//
404
405namespace {
406
407class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
408  std::vector<SourceLocation> &ARCMTMacroLocs;
409
410public:
411  ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
412    : ARCMTMacroLocs(ARCMTMacroLocs) { }
413
414  virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI,
415                            SourceRange Range) {
416    if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
417      ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
418  }
419};
420
421class ARCMTMacroTrackerAction : public ASTFrontendAction {
422  std::vector<SourceLocation> &ARCMTMacroLocs;
423
424public:
425  ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
426    : ARCMTMacroLocs(ARCMTMacroLocs) { }
427
428  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
429                                         StringRef InFile) {
430    CI.getPreprocessor().addPPCallbacks(
431                              new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs));
432    return new ASTConsumer();
433  }
434};
435
436class RewritesApplicator : public TransformActions::RewriteReceiver {
437  Rewriter &rewriter;
438  ASTContext &Ctx;
439  MigrationProcess::RewriteListener *Listener;
440
441public:
442  RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
443                     MigrationProcess::RewriteListener *listener)
444    : rewriter(rewriter), Ctx(ctx), Listener(listener) {
445    if (Listener)
446      Listener->start(ctx);
447  }
448  ~RewritesApplicator() {
449    if (Listener)
450      Listener->finish();
451  }
452
453  virtual void insert(SourceLocation loc, StringRef text) {
454    bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true,
455                                   /*indentNewLines=*/true);
456    if (!err && Listener)
457      Listener->insert(loc, text);
458  }
459
460  virtual void remove(CharSourceRange range) {
461    Rewriter::RewriteOptions removeOpts;
462    removeOpts.IncludeInsertsAtBeginOfRange = false;
463    removeOpts.IncludeInsertsAtEndOfRange = false;
464    removeOpts.RemoveLineIfEmpty = true;
465
466    bool err = rewriter.RemoveText(range, removeOpts);
467    if (!err && Listener)
468      Listener->remove(range);
469  }
470
471  virtual void increaseIndentation(CharSourceRange range,
472                                    SourceLocation parentIndent) {
473    rewriter.IncreaseIndentation(range, parentIndent);
474  }
475};
476
477} // end anonymous namespace.
478
479/// \brief Anchor for VTable.
480MigrationProcess::RewriteListener::~RewriteListener() { }
481
482MigrationProcess::MigrationProcess(const CompilerInvocation &CI,
483                                   DiagnosticConsumer *diagClient,
484                                   StringRef outputDir)
485  : OrigCI(CI), DiagClient(diagClient) {
486  if (!outputDir.empty()) {
487    llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
488    llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
489      new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false));
490    Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true);
491  }
492}
493
494bool MigrationProcess::applyTransform(TransformFn trans,
495                                      RewriteListener *listener) {
496  llvm::OwningPtr<CompilerInvocation> CInvok;
497  CInvok.reset(createInvocationForMigration(OrigCI));
498  CInvok->getDiagnosticOpts().IgnoreWarnings = true;
499
500  Remapper.applyMappings(*CInvok);
501
502  CapturedDiagList capturedDiags;
503  std::vector<SourceLocation> ARCMTMacroLocs;
504
505  assert(DiagClient);
506  llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
507  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
508      new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false));
509
510  // Filter of all diagnostics.
511  CaptureDiagnosticConsumer errRec(*Diags, capturedDiags);
512  Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
513
514  llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction;
515  ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
516
517  llvm::OwningPtr<ASTUnit> Unit(
518      ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags,
519                                                ASTAction.get()));
520  if (!Unit)
521    return true;
522  Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
523
524  // Don't filter diagnostics anymore.
525  Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
526
527  ASTContext &Ctx = Unit->getASTContext();
528
529  if (Diags->hasFatalErrorOccurred()) {
530    Diags->Reset();
531    DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
532    capturedDiags.reportDiagnostics(*Diags);
533    DiagClient->EndSourceFile();
534    return true;
535  }
536
537  // After parsing of source files ended, we want to reuse the
538  // diagnostics objects to emit further diagnostics.
539  // We call BeginSourceFile because DiagnosticConsumer requires that
540  // diagnostics with source range information are emitted only in between
541  // BeginSourceFile() and EndSourceFile().
542  DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor());
543
544  Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions());
545  TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
546  MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(),
547                     Unit->getSema(), TA, ARCMTMacroLocs);
548
549  trans(pass);
550
551  {
552    RewritesApplicator applicator(rewriter, Ctx, listener);
553    TA.applyRewrites(applicator);
554  }
555
556  DiagClient->EndSourceFile();
557
558  if (DiagClient->getNumErrors())
559    return true;
560
561  for (Rewriter::buffer_iterator
562        I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
563    FileID FID = I->first;
564    RewriteBuffer &buf = I->second;
565    const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
566    assert(file);
567    std::string newFname = file->getName();
568    newFname += "-trans";
569    llvm::SmallString<512> newText;
570    llvm::raw_svector_ostream vecOS(newText);
571    buf.write(vecOS);
572    vecOS.flush();
573    llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
574                   StringRef(newText.data(), newText.size()), newFname);
575    llvm::SmallString<64> filePath(file->getName());
576    Unit->getFileManager().FixupRelativePath(filePath);
577    Remapper.remap(filePath.str(), memBuf);
578  }
579
580  return false;
581}
582