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