clang  16.0.0git
ARCMT.cpp
Go to the documentation of this file.
1 //===--- ARCMT.cpp - Migration to ARC mode --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Internals.h"
10 #include "clang/ARCMigrate/ARCMT.h"
11 #include "clang/AST/ASTConsumer.h"
13 #include "clang/Frontend/ASTUnit.h"
17 #include "clang/Frontend/Utils.h"
18 #include "clang/Lex/Preprocessor.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include <utility>
26 using namespace clang;
27 using namespace arcmt;
28 
31  if (range.isInvalid())
32  return false;
33 
34  bool cleared = false;
35  ListTy::iterator I = List.begin();
36  while (I != List.end()) {
37  FullSourceLoc diagLoc = I->getLocation();
38  if ((IDs.empty() || // empty means clear all diagnostics in the range.
39  llvm::is_contained(IDs, I->getID())) &&
40  !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
41  (diagLoc == range.getEnd() ||
42  diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
43  cleared = true;
44  ListTy::iterator eraseS = I++;
45  if (eraseS->getLevel() != DiagnosticsEngine::Note)
46  while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note)
47  ++I;
48  // Clear the diagnostic and any notes following it.
49  I = List.erase(eraseS, I);
50  continue;
51  }
52 
53  ++I;
54  }
55 
56  return cleared;
57 }
58 
60  SourceRange range) const {
61  if (range.isInvalid())
62  return false;
63 
64  ListTy::const_iterator I = List.begin();
65  while (I != List.end()) {
66  FullSourceLoc diagLoc = I->getLocation();
67  if ((IDs.empty() || // empty means any diagnostic in the range.
68  llvm::is_contained(IDs, I->getID())) &&
69  !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) &&
70  (diagLoc == range.getEnd() ||
71  diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) {
72  return true;
73  }
74 
75  ++I;
76  }
77 
78  return false;
79 }
80 
82  for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
83  Diags.Report(*I);
84 }
85 
87  for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I)
88  if (I->getLevel() >= DiagnosticsEngine::Error)
89  return true;
90 
91  return false;
92 }
93 
94 namespace {
95 
96 class CaptureDiagnosticConsumer : public DiagnosticConsumer {
97  DiagnosticsEngine &Diags;
98  DiagnosticConsumer &DiagClient;
99  CapturedDiagList &CapturedDiags;
100  bool HasBegunSourceFile;
101 public:
102  CaptureDiagnosticConsumer(DiagnosticsEngine &diags,
103  DiagnosticConsumer &client,
104  CapturedDiagList &capturedDiags)
105  : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags),
106  HasBegunSourceFile(false) { }
107 
108  void BeginSourceFile(const LangOptions &Opts,
109  const Preprocessor *PP) override {
110  // Pass BeginSourceFile message onto DiagClient on first call.
111  // The corresponding EndSourceFile call will be made from an
112  // explicit call to FinishCapture.
113  if (!HasBegunSourceFile) {
114  DiagClient.BeginSourceFile(Opts, PP);
115  HasBegunSourceFile = true;
116  }
117  }
118 
119  void FinishCapture() {
120  // Call EndSourceFile on DiagClient on completion of capture to
121  // enable VerifyDiagnosticConsumer to check diagnostics *after*
122  // it has received the diagnostic list.
123  if (HasBegunSourceFile) {
124  DiagClient.EndSourceFile();
125  HasBegunSourceFile = false;
126  }
127  }
128 
129  ~CaptureDiagnosticConsumer() override {
130  assert(!HasBegunSourceFile && "FinishCapture not called!");
131  }
132 
133  void HandleDiagnostic(DiagnosticsEngine::Level level,
134  const Diagnostic &Info) override {
136  level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) {
137  if (Info.getLocation().isValid())
138  CapturedDiags.push_back(StoredDiagnostic(level, Info));
139  return;
140  }
141 
142  // Non-ARC warnings are ignored.
143  Diags.setLastDiagnosticIgnored(true);
144  }
145 };
146 
147 } // end anonymous namespace
148 
149 static bool HasARCRuntime(CompilerInvocation &origCI) {
150  // This duplicates some functionality from Darwin::AddDeploymentTarget
151  // but this function is well defined, so keep it decoupled from the driver
152  // and avoid unrelated complications.
153  llvm::Triple triple(origCI.getTargetOpts().Triple);
154 
155  if (triple.isiOS())
156  return triple.getOSMajorVersion() >= 5;
157 
158  if (triple.isWatchOS())
159  return true;
160 
161  if (triple.getOS() == llvm::Triple::Darwin)
162  return triple.getOSMajorVersion() >= 11;
163 
164  if (triple.getOS() == llvm::Triple::MacOSX) {
165  return triple.getOSVersion() >= VersionTuple(10, 7);
166  }
167 
168  return false;
169 }
170 
171 static CompilerInvocation *
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());
183  new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
184  new IgnoringDiagConsumer()));
186  PPOpts.ImplicitPCHInclude, FileMgr, PCHContainerRdr, *Diags);
187  if (!OriginalFile.empty())
188  PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile);
189  PPOpts.ImplicitPCHInclude.clear();
190  }
192  define += '=';
193  CInvok->getPreprocessorOpts().addMacroDef(define);
194  CInvok->getLangOpts()->ObjCAutoRefCount = true;
195  CInvok->getLangOpts()->setGC(LangOptions::NonGC);
196  CInvok->getDiagnosticOpts().ErrorLimit = 0;
197  CInvok->getDiagnosticOpts().PedanticErrors = 0;
198 
199  // Ignore -Werror flags when migrating.
200  std::vector<std::string> WarnOpts;
201  for (std::vector<std::string>::iterator
202  I = CInvok->getDiagnosticOpts().Warnings.begin(),
203  E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
204  if (!StringRef(*I).startswith("error"))
205  WarnOpts.push_back(*I);
206  }
207  WarnOpts.push_back("error=arc-unsafe-retained-assign");
208  CInvok->getDiagnosticOpts().Warnings = std::move(WarnOpts);
209 
210  CInvok->getLangOpts()->ObjCWeakRuntime = HasARCRuntime(origCI);
211  CInvok->getLangOpts()->ObjCWeak = CInvok->getLangOpts()->ObjCWeakRuntime;
212 
213  return CInvok.release();
214 }
215 
216 static void emitPremigrationErrors(const CapturedDiagList &arcDiags,
217  DiagnosticOptions *diagOpts,
218  Preprocessor &PP) {
219  TextDiagnosticPrinter printer(llvm::errs(), diagOpts);
222  new DiagnosticsEngine(DiagID, diagOpts, &printer,
223  /*ShouldOwnClient=*/false));
224  Diags->setSourceManager(&PP.getSourceManager());
225 
226  printer.BeginSourceFile(PP.getLangOpts(), &PP);
227  arcDiags.reportDiagnostics(*Diags);
228  printer.EndSourceFile();
229 }
230 
231 //===----------------------------------------------------------------------===//
232 // checkForManualIssues.
233 //===----------------------------------------------------------------------===//
234 
236  CompilerInvocation &origCI, const FrontendInputFile &Input,
237  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
238  DiagnosticConsumer *DiagClient, bool emitPremigrationARCErrors,
239  StringRef plistOut) {
240  if (!origCI.getLangOpts()->ObjC)
241  return false;
242 
243  LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
244  bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError;
245  bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
246 
247  std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
248  NoFinalizeRemoval);
249  assert(!transforms.empty());
250 
251  std::unique_ptr<CompilerInvocation> CInvok;
252  CInvok.reset(
253  createInvocationForMigration(origCI, PCHContainerOps->getRawReader()));
254  CInvok->getFrontendOpts().Inputs.clear();
255  CInvok->getFrontendOpts().Inputs.push_back(Input);
256 
257  CapturedDiagList capturedDiags;
258 
259  assert(DiagClient);
262  new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
263  DiagClient, /*ShouldOwnClient=*/false));
264 
265  // Filter of all diagnostics.
266  CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
267  Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
268 
269  std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
270  std::move(CInvok), PCHContainerOps, Diags));
271  if (!Unit) {
272  errRec.FinishCapture();
273  return true;
274  }
275 
276  // Don't filter diagnostics anymore.
277  Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
278 
279  ASTContext &Ctx = Unit->getASTContext();
280 
281  if (Diags->hasFatalErrorOccurred()) {
282  Diags->Reset();
283  DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
284  capturedDiags.reportDiagnostics(*Diags);
285  DiagClient->EndSourceFile();
286  errRec.FinishCapture();
287  return true;
288  }
289 
290  if (emitPremigrationARCErrors)
291  emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(),
292  Unit->getPreprocessor());
293  if (!plistOut.empty()) {
296  I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I)
297  arcDiags.push_back(*I);
298  writeARCDiagsToPlist(std::string(plistOut), arcDiags,
299  Ctx.getSourceManager(), Ctx.getLangOpts());
300  }
301 
302  // After parsing of source files ended, we want to reuse the
303  // diagnostics objects to emit further diagnostics.
304  // We call BeginSourceFile because DiagnosticConsumer requires that
305  // diagnostics with source range information are emitted only in between
306  // BeginSourceFile() and EndSourceFile().
307  DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
308 
309  // No macros will be added since we are just checking and we won't modify
310  // source code.
311  std::vector<SourceLocation> ARCMTMacroLocs;
312 
313  TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
314  MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags,
315  ARCMTMacroLocs);
316  pass.setNoFinalizeRemoval(NoFinalizeRemoval);
317  if (!NoNSAllocReallocError)
318  Diags->setSeverity(diag::warn_arcmt_nsalloc_realloc, diag::Severity::Error,
319  SourceLocation());
320 
321  for (unsigned i=0, e = transforms.size(); i != e; ++i)
322  transforms[i](pass);
323 
324  capturedDiags.reportDiagnostics(*Diags);
325 
326  DiagClient->EndSourceFile();
327  errRec.FinishCapture();
328 
329  return capturedDiags.hasErrors() || testAct.hasReportedErrors();
330 }
331 
332 //===----------------------------------------------------------------------===//
333 // applyTransformations.
334 //===----------------------------------------------------------------------===//
335 
336 static bool
338  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
339  DiagnosticConsumer *DiagClient, StringRef outputDir,
340  bool emitPremigrationARCErrors, StringRef plistOut) {
341  if (!origCI.getLangOpts()->ObjC)
342  return false;
343 
344  LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC();
345 
346  // Make sure checking is successful first.
347  CompilerInvocation CInvokForCheck(origCI);
348  if (arcmt::checkForManualIssues(CInvokForCheck, Input, PCHContainerOps,
349  DiagClient, emitPremigrationARCErrors,
350  plistOut))
351  return true;
352 
353  CompilerInvocation CInvok(origCI);
354  CInvok.getFrontendOpts().Inputs.clear();
355  CInvok.getFrontendOpts().Inputs.push_back(Input);
356 
357  MigrationProcess migration(CInvok, PCHContainerOps, DiagClient, outputDir);
358  bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval;
359 
360  std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode,
361  NoFinalizeRemoval);
362  assert(!transforms.empty());
363 
364  for (unsigned i=0, e = transforms.size(); i != e; ++i) {
365  bool err = migration.applyTransform(transforms[i]);
366  if (err) return true;
367  }
368 
371  new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(),
372  DiagClient, /*ShouldOwnClient=*/false));
373 
374  if (outputDir.empty()) {
375  origCI.getLangOpts()->ObjCAutoRefCount = true;
376  return migration.getRemapper().overwriteOriginal(*Diags);
377  } else {
378  return migration.getRemapper().flushToDisk(outputDir, *Diags);
379  }
380 }
381 
383  CompilerInvocation &origCI, const FrontendInputFile &Input,
384  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
385  DiagnosticConsumer *DiagClient) {
386  return applyTransforms(origCI, Input, PCHContainerOps, DiagClient,
387  StringRef(), false, StringRef());
388 }
389 
391  CompilerInvocation &origCI, const FrontendInputFile &Input,
392  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
393  DiagnosticConsumer *DiagClient, StringRef outputDir,
394  bool emitPremigrationARCErrors, StringRef plistOut) {
395  assert(!outputDir.empty() && "Expected output directory path");
396  return applyTransforms(origCI, Input, PCHContainerOps, DiagClient, outputDir,
397  emitPremigrationARCErrors, plistOut);
398 }
399 
400 bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > &
401  remap,
402  StringRef outputDir,
403  DiagnosticConsumer *DiagClient) {
404  assert(!outputDir.empty());
405 
408  new DiagnosticsEngine(DiagID, new DiagnosticOptions,
409  DiagClient, /*ShouldOwnClient=*/false));
410 
411  FileRemapper remapper;
412  bool err = remapper.initFromDisk(outputDir, *Diags,
413  /*ignoreIfFilesChanged=*/true);
414  if (err)
415  return true;
416 
417  remapper.forEachMapping(
418  [&](StringRef From, StringRef To) {
419  remap.push_back(std::make_pair(From.str(), To.str()));
420  },
421  [](StringRef, const llvm::MemoryBufferRef &) {});
422 
423  return false;
424 }
425 
426 
427 //===----------------------------------------------------------------------===//
428 // CollectTransformActions.
429 //===----------------------------------------------------------------------===//
430 
431 namespace {
432 
433 class ARCMTMacroTrackerPPCallbacks : public PPCallbacks {
434  std::vector<SourceLocation> &ARCMTMacroLocs;
435 
436 public:
437  ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
438  : ARCMTMacroLocs(ARCMTMacroLocs) { }
439 
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());
444  }
445 };
446 
447 class ARCMTMacroTrackerAction : public ASTFrontendAction {
448  std::vector<SourceLocation> &ARCMTMacroLocs;
449 
450 public:
451  ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs)
452  : ARCMTMacroLocs(ARCMTMacroLocs) { }
453 
454  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
455  StringRef InFile) override {
457  std::make_unique<ARCMTMacroTrackerPPCallbacks>(ARCMTMacroLocs));
458  return std::make_unique<ASTConsumer>();
459  }
460 };
461 
462 class RewritesApplicator : public TransformActions::RewriteReceiver {
463  Rewriter &rewriter;
465 
466 public:
467  RewritesApplicator(Rewriter &rewriter, ASTContext &ctx,
469  : rewriter(rewriter), Listener(listener) {
470  if (Listener)
471  Listener->start(ctx);
472  }
473  ~RewritesApplicator() override {
474  if (Listener)
475  Listener->finish();
476  }
477 
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);
483  }
484 
485  void remove(CharSourceRange range) override {
486  Rewriter::RewriteOptions removeOpts;
487  removeOpts.IncludeInsertsAtBeginOfRange = false;
488  removeOpts.IncludeInsertsAtEndOfRange = false;
489  removeOpts.RemoveLineIfEmpty = true;
490 
491  bool err = rewriter.RemoveText(range, removeOpts);
492  if (!err && Listener)
493  Listener->remove(range);
494  }
495 
496  void increaseIndentation(CharSourceRange range,
497  SourceLocation parentIndent) override {
498  rewriter.IncreaseIndentation(range, parentIndent);
499  }
500 };
501 
502 } // end anonymous namespace.
503 
504 /// Anchor for VTable.
506 
508  const CompilerInvocation &CI,
509  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
510  DiagnosticConsumer *diagClient, StringRef outputDir)
511  : OrigCI(CI), PCHContainerOps(std::move(PCHContainerOps)),
512  DiagClient(diagClient), HadARCErrors(false) {
513  if (!outputDir.empty()) {
516  new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(),
517  DiagClient, /*ShouldOwnClient=*/false));
518  Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanged=*/true);
519  }
520 }
521 
523  RewriteListener *listener) {
524  std::unique_ptr<CompilerInvocation> CInvok;
525  CInvok.reset(
526  createInvocationForMigration(OrigCI, PCHContainerOps->getRawReader()));
527  CInvok->getDiagnosticOpts().IgnoreWarnings = true;
528 
529  Remapper.applyMappings(CInvok->getPreprocessorOpts());
530 
531  CapturedDiagList capturedDiags;
532  std::vector<SourceLocation> ARCMTMacroLocs;
533 
534  assert(DiagClient);
537  new DiagnosticsEngine(DiagID, new DiagnosticOptions,
538  DiagClient, /*ShouldOwnClient=*/false));
539 
540  // Filter of all diagnostics.
541  CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags);
542  Diags->setClient(&errRec, /*ShouldOwnClient=*/false);
543 
544  std::unique_ptr<ARCMTMacroTrackerAction> ASTAction;
545  ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
546 
547  std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
548  std::move(CInvok), PCHContainerOps, Diags, ASTAction.get()));
549  if (!Unit) {
550  errRec.FinishCapture();
551  return true;
552  }
553  Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that.
554 
555  HadARCErrors = HadARCErrors || capturedDiags.hasErrors();
556 
557  // Don't filter diagnostics anymore.
558  Diags->setClient(DiagClient, /*ShouldOwnClient=*/false);
559 
560  ASTContext &Ctx = Unit->getASTContext();
561 
562  if (Diags->hasFatalErrorOccurred()) {
563  Diags->Reset();
564  DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor());
565  capturedDiags.reportDiagnostics(*Diags);
566  DiagClient->EndSourceFile();
567  errRec.FinishCapture();
568  return true;
569  }
570 
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());
577 
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);
582 
583  trans(pass);
584 
585  {
586  RewritesApplicator applicator(rewriter, Ctx, listener);
587  TA.applyRewrites(applicator);
588  }
589 
590  DiagClient->EndSourceFile();
591  errRec.FinishCapture();
592 
593  if (DiagClient->getNumErrors())
594  return true;
595 
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);
601  assert(file);
602  std::string newFname = std::string(file->getName());
603  newFname += "-trans";
604  SmallString<512> newText;
605  llvm::raw_svector_ostream vecOS(newText);
606  buf.write(vecOS);
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));
613  }
614 
615  return false;
616 }
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:195
clang::arcmt::MigrationProcess::RewriteListener::start
virtual void start(ASTContext &Ctx)
Definition: ARCMT.h:114
clang::comments::tok::text
@ text
Definition: CommentLexer.h:35
clang::CompilerInvocationRefBase::getTargetOpts
TargetOptions & getTargetOpts()
Definition: CompilerInvocation.h:107
clang::diag::Severity::Error
@ Error
Present this diagnostic as an error.
clang::ASTUnit::LoadFromCompilerInvocationAction
static ASTUnit * LoadFromCompilerInvocationAction(std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, FrontendAction *Action=nullptr, ASTUnit *Unit=nullptr, bool Persistent=true, StringRef ResourceFilesPath=StringRef(), bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, unsigned PrecompilePreambleAfterNParses=0, bool CacheCodeCompletionResults=false, bool UserFilesAreVolatile=false, std::unique_ptr< ASTUnit > *ErrAST=nullptr)
Create an ASTUnit from a source file, via a CompilerInvocation object, by invoking the optionally pro...
Definition: ASTUnit.cpp:1500
clang::FullSourceLoc
A SourceLocation and its associated SourceManager.
Definition: SourceLocation.h:368
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:210
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PreprocessorOptions
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Definition: PreprocessorOptions.h:65
clang::arcmt::MigrationPass
Definition: Internals.h:146
clang::FullSourceLoc::isBeforeInTranslationUnitThan
bool isBeforeInTranslationUnitThan(SourceLocation Loc) const
Determines the order of 2 source locations in the translation unit.
Definition: SourceLocation.cpp:255
clang::MacroDefinition
A description of the current definition of a macro.
Definition: MacroInfo.h:587
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
HasARCRuntime
static bool HasARCRuntime(CompilerInvocation &origCI)
Definition: ARCMT.cpp:149
ARCMT.h
clang::arcmt::TransformActions
Definition: Internals.h:49
clang::TextDiagnosticPrinter::EndSourceFile
void EndSourceFile() override
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: TextDiagnosticPrinter.cpp:42
clang::arcmt::CapturedDiagList::iterator
ListTy::const_iterator iterator
Definition: Internals.h:40
clang::DiagnosticConsumer
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1739
clang::Token::getIdentifierInfo
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:180
clang::MacroArgs
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition: MacroArgs.h:29
clang::RewriteBuffer::write
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
Definition: Rewriter.cpp:37
clang::DiagnosticConsumer::getNumErrors
unsigned getNumErrors() const
Definition: Diagnostic.h:1748
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
clang::FrontendInputFile
An input file for the front end.
Definition: FrontendOptions.h:218
clang::arcmt::getARCMTMacroName
static StringRef getARCMTMacroName()
Definition: Internals.h:172
clang::ASTReader::getOriginalSourceFile
StringRef getOriginalSourceFile()
Retrieve the name of the original source file name for the primary module file.
Definition: ASTReader.h:1720
Internals.h
clang::arcmt::FileRemapper::flushToDisk
bool flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag)
Definition: FileRemapper.cpp:109
clang::DiagnosticsEngine::setLastDiagnosticIgnored
void setLastDiagnosticIgnored(bool Ignored)
Pretend that the last diagnostic issued was ignored, so any subsequent notes will be suppressed,...
Definition: Diagnostic.h:756
clang::arcmt::CapturedDiagList::hasDiagnostic
bool hasDiagnostic(ArrayRef< unsigned > IDs, SourceRange range) const
Definition: ARCMT.cpp:59
clang::LangOptions::GCMode
GCMode
Definition: LangOptions.h:86
FrontendAction.h
clang::Rewriter::RemoveText
bool RemoveText(SourceLocation Start, unsigned Length, RewriteOptions opts=RewriteOptions())
RemoveText - Remove the specified text region.
Definition: Rewriter.cpp:309
clang::DiagnosticConsumer::EndSourceFile
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1771
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:721
clang::PreprocessorOptions::Includes
std::vector< std::string > Includes
Definition: PreprocessorOptions.h:68
clang::PCHContainerReader
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
Definition: PCHContainerOperations.h:55
clang::FrontendOptions::Inputs
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
Definition: FrontendOptions.h:426
clang::Preprocessor::getLangOpts
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:1065
clang::arcmt::CapturedDiagList
Definition: Internals.h:26
Preprocessor.h
clang::arcmt::MigrationProcess::MigrationProcess
MigrationProcess(const CompilerInvocation &CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *diagClient, StringRef outputDir=StringRef())
Definition: ARCMT.cpp:507
clang::Rewriter::buffer_end
buffer_iterator buffer_end()
Definition: Rewriter.h:206
clang::arcmt::FileRemapper::forEachMapping
void forEachMapping(llvm::function_ref< void(StringRef, StringRef)> CaptureFile, llvm::function_ref< void(StringRef, const llvm::MemoryBufferRef &)> CaptureBuffer) const
Iterate through all the mappings.
Definition: FileRemapper.cpp:194
clang::Diagnostic
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1561
clang::arcmt::FileRemapper::initFromDisk
bool initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag, bool ignoreIfFilesChanged)
Definition: FileRemapper.cpp:49
clang::Rewriter::RewriteOptions
Definition: Rewriter.h:38
clang::Rewriter::RewriteOptions::IncludeInsertsAtEndOfRange
bool IncludeInsertsAtEndOfRange
Given a source range, true to include previous inserts at the end of the range as part of the range i...
Definition: Rewriter.h:45
clang::Rewriter::RewriteOptions::RemoveLineIfEmpty
bool RemoveLineIfEmpty
If true and removing some text leaves a blank line also remove the empty line (false by default).
Definition: Rewriter.h:60
clang::CompilerInvocationValueBase::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInvocation.h:176
clang::arcmt::MigrationProcess::RewriteListener::insert
virtual void insert(SourceLocation loc, StringRef text)
Definition: ARCMT.h:117
clang::MigratorOptions::NoFinalizeRemoval
unsigned NoFinalizeRemoval
Definition: MigratorOptions.h:22
clang::arcmt::applyTransformations
bool applyTransformations(CompilerInvocation &origCI, const FrontendInputFile &Input, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagClient)
Works similar to checkForManualIssues but instead of checking, it applies automatic modifications to ...
Definition: ARCMT.cpp:382
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::arcmt::FileRemapper
Definition: FileRemapper.h:32
clang::DiagnosticIDs::isARCDiagnostic
static bool isARCDiagnostic(unsigned DiagID)
Return true if a given diagnostic falls into an ARC diagnostic category.
Definition: DiagnosticIDs.cpp:868
clang::arcmt::checkForManualIssues
bool checkForManualIssues(CompilerInvocation &CI, const FrontendInputFile &Input, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagClient, bool emitPremigrationARCErrors=false, StringRef plistOut=StringRef())
Creates an AST with the provided CompilerInvocation but with these changes: -if a PCH/PTH is set,...
Definition: ARCMT.cpp:235
clang::arcmt::MigrationProcess::RewriteListener::~RewriteListener
virtual ~RewriteListener()
Anchor for VTable.
Definition: ARCMT.cpp:505
clang::DiagnosticsEngine::Error
@ Error
Definition: Diagnostic.h:200
clang::arcmt::CapturedDiagList::hasErrors
bool hasErrors() const
Definition: ARCMT.cpp:86
Utils.h
createInvocationForMigration
static CompilerInvocation * createInvocationForMigration(CompilerInvocation &origCI, const PCHContainerReader &PCHContainerRdr)
Definition: ARCMT.cpp:172
clang::arcmt::CapturedDiagList::begin
iterator begin() const
Definition: Internals.h:41
clang::arcmt::CapturedDiagList::clearDiagnostic
bool clearDiagnostic(ArrayRef< unsigned > IDs, SourceRange range)
Definition: ARCMT.cpp:29
clang::arcmt::FileRemapper::overwriteOriginal
bool overwriteOriginal(DiagnosticsEngine &Diag, StringRef outputDir=StringRef())
Definition: FileRemapper.cpp:168
llvm::SmallString< 512 >
clang::arcmt::TransformActions::RewriteReceiver
Definition: Internals.h:111
clang::MigratorOptions::NoNSAllocReallocError
unsigned NoNSAllocReallocError
Definition: MigratorOptions.h:21
clang::CompilerInvocationRefBase::getLangOpts
LangOptions * getLangOpts()
Definition: CompilerInvocation.h:104
clang::arcmt::CapturedDiagList::reportDiagnostics
void reportDiagnostics(DiagnosticsEngine &diags) const
Definition: ARCMT.cpp:81
clang::CompilerInstance::getPreprocessor
Preprocessor & getPreprocessor() const
Return the current preprocessor.
Definition: CompilerInstance.h:442
clang::Rewriter::RewriteOptions::IncludeInsertsAtBeginOfRange
bool IncludeInsertsAtBeginOfRange
Given a source range, true to include previous inserts at the beginning of the range as part of the r...
Definition: Rewriter.h:41
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:366
clang::arcmt::MigrationProcess::RewriteListener::remove
virtual void remove(CharSourceRange range)
Definition: ARCMT.h:118
clang::StoredDiagnostic
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1685
clang::arcmt::writeARCDiagsToPlist
void writeARCDiagsToPlist(const std::string &outPath, ArrayRef< StoredDiagnostic > diags, SourceManager &SM, const LangOptions &LangOpts)
Definition: PlistReporter.cpp:34
clang::Preprocessor::getSourceManager
SourceManager & getSourceManager() const
Definition: Preprocessor.h:1069
clang::arcmt::MigrationProcess::RewriteListener
Definition: ARCMT.h:110
clang::transformer::remove
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: RewriteRule.cpp:163
clang::arcmt::MigrationProcess::applyTransform
bool applyTransform(TransformFn trans, RewriteListener *listener=nullptr)
Definition: ARCMT.cpp:522
clang::arcmt::MigrationProcess
Definition: ARCMT.h:96
clang::RISCV::TA
@ TA
Definition: RISCVVIntrinsicUtils.h:98
clang::DiagnosticConsumer::BeginSourceFile
virtual void BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP=nullptr)
Callback to inform the diagnostic client that processing of a source file is beginning.
Definition: Diagnostic.h:1763
false
#define false
Definition: stdbool.h:22
SemaDiagnostic.h
clang::arcmt::MigrationProcess::getRemapper
FileRemapper & getRemapper()
Definition: ARCMT.h:123
clang::LangOptions::NonGC
@ NonGC
Definition: LangOptions.h:86
clang::Diagnostic::getLocation
const SourceLocation & getLocation() const
Definition: Diagnostic.h:1572
TextDiagnosticPrinter.h
llvm::ArrayRef< unsigned >
clang::arcmt::getFileRemappings
bool getFileRemappings(std::vector< std::pair< std::string, std::string > > &remap, StringRef outputDir, DiagnosticConsumer *DiagClient)
Get the set of file remappings from the outputDir path that migrateWithTemporaryFiles produced.
Definition: ARCMT.cpp:400
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:253
clang::arcmt::FileRemapper::applyMappings
void applyMappings(PreprocessorOptions &PPOpts) const
Definition: FileRemapper.cpp:209
ASTUnit.h
clang::DiagnosticIDs
Used for handling and querying diagnostic IDs.
Definition: DiagnosticIDs.h:166
clang::arcmt::getAllTransformations
std::vector< TransformFn > getAllTransformations(LangOptions::GCMode OrigGCMode, bool NoFinalizeRemoval)
Definition: Transforms.cpp:581
ASTConsumer.h
clang::TextDiagnosticPrinter::BeginSourceFile
void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override
Callback to inform the diagnostic client that processing of a source file is beginning.
Definition: TextDiagnosticPrinter.cpp:36
DiagnosticCategories.h
clang::arcmt::MigrationProcess::RewriteListener::finish
virtual void finish()
Definition: ARCMT.h:115
clang::Token::getLocation
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:125
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:81
clang::Rewriter::InsertText
bool InsertText(SourceLocation Loc, StringRef Str, bool InsertAfter=true, bool indentNewLines=false)
InsertText - Insert the specified string at the specified location in the original buffer.
Definition: Rewriter.cpp:256
clang::arcmt::MigrationProcess::HadARCErrors
bool HadARCErrors
Definition: ARCMT.h:103
clang::Rewriter::buffer_begin
buffer_iterator buffer_begin()
Definition: Rewriter.h:205
std
Definition: Format.h:4477
emitPremigrationErrors
static void emitPremigrationErrors(const CapturedDiagList &arcDiags, DiagnosticOptions *diagOpts, Preprocessor &PP)
Definition: ARCMT.cpp:216
applyTransforms
static bool applyTransforms(CompilerInvocation &origCI, const FrontendInputFile &Input, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagClient, StringRef outputDir, bool emitPremigrationARCErrors, StringRef plistOut)
Definition: ARCMT.cpp:337
clang::IdentifierInfo::getName
StringRef getName() const
Return the actual identifier string.
Definition: IdentifierTable.h:196
clang::arcmt::migrateWithTemporaryFiles
bool migrateWithTemporaryFiles(CompilerInvocation &origCI, const FrontendInputFile &Input, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagClient, StringRef outputDir, bool emitPremigrationARCErrors, StringRef plistOut)
Applies automatic modifications and produces temporary files and metadata into the outputDir path.
Definition: ARCMT.cpp:390
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::Preprocessor::addPPCallbacks
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:1163
clang::RewriteBuffer
RewriteBuffer - As code is rewritten, SourceBuffer's from the original input with modifications get a...
Definition: RewriteBuffer.h:25
clang::transformer::range
RangeSelector range(RangeSelector Begin, RangeSelector End)
DEPRECATED. Use enclose.
Definition: RangeSelector.h:41
clang::Rewriter::buffer_iterator
std::map< FileID, RewriteBuffer >::iterator buffer_iterator
Definition: Rewriter.h:65
ASTReader.h
clang::arcmt::TransformFn
void(* TransformFn)(MigrationPass &pass)
Definition: ARCMT.h:91
clang::TargetOptions::Triple
std::string Triple
The name of the target triple to compile for.
Definition: TargetOptions.h:29
clang::FileEntry::getName
StringRef getName() const
Definition: FileEntry.h:397
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:38
clang::arcmt::MigrationPass::setNoFinalizeRemoval
void setNoFinalizeRemoval(bool val)
Definition: Internals.h:167
clang::CompilerInvocation
Helper class for holding the data necessary to invoke the compiler.
Definition: CompilerInvocation.h:193
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:110
clang::SourceManager::getFileEntryForID
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Definition: SourceManager.h:1043
clang::Rewriter
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:32
clang::IgnoringDiagConsumer
A diagnostic client that ignores all diagnostics.
Definition: Diagnostic.h:1794
clang::TextDiagnosticPrinter
Definition: TextDiagnosticPrinter.h:27
CompilerInstance.h
clang::DiagnosticsEngine::Note
@ Note
Definition: Diagnostic.h:197
clang::arcmt::CapturedDiagList::end
iterator end() const
Definition: Internals.h:42
clang::arcmt::TransformActions::hasReportedErrors
bool hasReportedErrors() const
Definition: Internals.h:107
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
clang::Diagnostic::getID
unsigned getID() const
Definition: Diagnostic.h:1571
Rewriter.h
PreprocessorOptions.h
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:129
clang::Rewriter::IncreaseIndentation
bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent)
Increase indentation for the lines between the given source range.
Definition: Rewriter.cpp:345
clang::CompilerInvocationValueBase::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Definition: CompilerInvocation.h:170
clang::arcmt::FileRemapper::remap
void remap(StringRef filePath, std::unique_ptr< llvm::MemoryBuffer > memBuf)
Definition: FileRemapper.cpp:223
clang::arcmt::CapturedDiagList::push_back
void push_back(const StoredDiagnostic &diag)
Definition: Internals.h:31
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:791
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47
clang::PreprocessorOptions::ImplicitPCHInclude
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
Definition: PreprocessorOptions.h:97
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1537
clang::CompilerInvocationRefBase::getDiagnosticOpts
DiagnosticOptions & getDiagnosticOpts() const
Definition: CompilerInvocation.h:110
clang::CompilerInvocationValueBase::getMigratorOpts
MigratorOptions & getMigratorOpts()
Definition: CompilerInvocation.h:156