clang  10.0.0svn
CompilerInstance.cpp
Go to the documentation of this file.
1 //===--- CompilerInstance.cpp ---------------------------------------------===//
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 
10 #include "clang/AST/ASTConsumer.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/Basic/CharInfo.h"
14 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/Stack.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "clang/Basic/Version.h"
20 #include "clang/Config/config.h"
28 #include "clang/Frontend/Utils.h"
30 #include "clang/Lex/HeaderSearch.h"
31 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Sema/Sema.h"
38 #include "llvm/ADT/Statistic.h"
39 #include "llvm/Support/BuryPointer.h"
40 #include "llvm/Support/CrashRecoveryContext.h"
41 #include "llvm/Support/Errc.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/Host.h"
44 #include "llvm/Support/LockFileManager.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/Path.h"
47 #include "llvm/Support/Program.h"
48 #include "llvm/Support/Signals.h"
49 #include "llvm/Support/TimeProfiler.h"
50 #include "llvm/Support/Timer.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include <sys/stat.h>
53 #include <system_error>
54 #include <time.h>
55 #include <utility>
56 
57 using namespace clang;
58 
59 CompilerInstance::CompilerInstance(
60  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
61  InMemoryModuleCache *SharedModuleCache)
62  : ModuleLoader(/* BuildingModule = */ SharedModuleCache),
63  Invocation(new CompilerInvocation()),
64  ModuleCache(SharedModuleCache ? SharedModuleCache
65  : new InMemoryModuleCache),
66  ThePCHContainerOperations(std::move(PCHContainerOps)) {}
67 
69  assert(OutputFiles.empty() && "Still output files in flight?");
70 }
71 
73  std::shared_ptr<CompilerInvocation> Value) {
74  Invocation = std::move(Value);
75 }
76 
78  return (BuildGlobalModuleIndex ||
79  (ModuleManager && ModuleManager->isGlobalIndexUnavailable() &&
81  !ModuleBuildFailed;
82 }
83 
85  Diagnostics = Value;
86 }
87 
90 
92  FileMgr = Value;
93 }
94 
96  SourceMgr = Value;
97 }
98 
99 void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) {
100  PP = std::move(Value);
101 }
102 
104  Context = Value;
105 
106  if (Context && Consumer)
108 }
109 
111  TheSema.reset(S);
112 }
113 
114 void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
115  Consumer = std::move(Value);
116 
117  if (Context && Consumer)
119 }
120 
122  CompletionConsumer.reset(Value);
123 }
124 
125 std::unique_ptr<Sema> CompilerInstance::takeSema() {
126  return std::move(TheSema);
127 }
128 
130  return ModuleManager;
131 }
133  assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() &&
134  "Expected ASTReader to use the same PCM cache");
135  ModuleManager = std::move(Reader);
136 }
137 
138 std::shared_ptr<ModuleDependencyCollector>
140  return ModuleDepCollector;
141 }
142 
144  std::shared_ptr<ModuleDependencyCollector> Collector) {
145  ModuleDepCollector = std::move(Collector);
146 }
147 
148 static void collectHeaderMaps(const HeaderSearch &HS,
149  std::shared_ptr<ModuleDependencyCollector> MDC) {
150  SmallVector<std::string, 4> HeaderMapFileNames;
151  HS.getHeaderMapFileNames(HeaderMapFileNames);
152  for (auto &Name : HeaderMapFileNames)
153  MDC->addFile(Name);
154 }
155 
157  std::shared_ptr<ModuleDependencyCollector> MDC) {
158  const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
159  if (PPOpts.ImplicitPCHInclude.empty())
160  return;
161 
162  StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
163  FileManager &FileMgr = CI.getFileManager();
164  const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude);
165  if (!PCHDir) {
166  MDC->addFile(PCHInclude);
167  return;
168  }
169 
170  std::error_code EC;
171  SmallString<128> DirNative;
172  llvm::sys::path::native(PCHDir->getName(), DirNative);
173  llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
175  for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
176  Dir != DirEnd && !EC; Dir.increment(EC)) {
177  // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not
178  // used here since we're not interested in validating the PCH at this time,
179  // but only to check whether this is a file containing an AST.
181  Dir->path(), FileMgr, CI.getPCHContainerReader(),
182  /*FindModuleFileExtensions=*/false, Validator,
183  /*ValidateDiagnosticOptions=*/false))
184  MDC->addFile(Dir->path());
185  }
186 }
187 
189  std::shared_ptr<ModuleDependencyCollector> MDC) {
190  if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty())
191  return;
192 
193  // Collect all VFS found.
195  for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) {
196  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
197  llvm::MemoryBuffer::getFile(VFSFile);
198  if (!Buffer)
199  return;
200  llvm::vfs::collectVFSFromYAML(std::move(Buffer.get()),
201  /*DiagHandler*/ nullptr, VFSFile, VFSEntries);
202  }
203 
204  for (auto &E : VFSEntries)
205  MDC->addFile(E.VPath, E.RPath);
206 }
207 
208 // Diagnostics
209 static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
210  const CodeGenOptions *CodeGenOpts,
211  DiagnosticsEngine &Diags) {
212  std::error_code EC;
213  std::unique_ptr<raw_ostream> StreamOwner;
214  raw_ostream *OS = &llvm::errs();
215  if (DiagOpts->DiagnosticLogFile != "-") {
216  // Create the output stream.
217  auto FileOS = llvm::make_unique<llvm::raw_fd_ostream>(
218  DiagOpts->DiagnosticLogFile, EC,
219  llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
220  if (EC) {
221  Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
222  << DiagOpts->DiagnosticLogFile << EC.message();
223  } else {
224  FileOS->SetUnbuffered();
225  OS = FileOS.get();
226  StreamOwner = std::move(FileOS);
227  }
228  }
229 
230  // Chain in the diagnostic client which will log the diagnostics.
231  auto Logger = llvm::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
232  std::move(StreamOwner));
233  if (CodeGenOpts)
234  Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
235  if (Diags.ownsClient()) {
236  Diags.setClient(
237  new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
238  } else {
239  Diags.setClient(
240  new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger)));
241  }
242 }
243 
245  DiagnosticsEngine &Diags,
246  StringRef OutputFile) {
247  auto SerializedConsumer =
248  clang::serialized_diags::create(OutputFile, DiagOpts);
249 
250  if (Diags.ownsClient()) {
252  Diags.takeClient(), std::move(SerializedConsumer)));
253  } else {
255  Diags.getClient(), std::move(SerializedConsumer)));
256  }
257 }
258 
260  bool ShouldOwnClient) {
261  Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
262  ShouldOwnClient, &getCodeGenOpts());
263 }
264 
267  DiagnosticConsumer *Client,
268  bool ShouldOwnClient,
269  const CodeGenOptions *CodeGenOpts) {
272  Diags(new DiagnosticsEngine(DiagID, Opts));
273 
274  // Create the diagnostic client for reporting errors or for
275  // implementing -verify.
276  if (Client) {
277  Diags->setClient(Client, ShouldOwnClient);
278  } else
279  Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
280 
281  // Chain in -verify checker, if requested.
282  if (Opts->VerifyDiagnostics)
283  Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
284 
285  // Chain in -diagnostic-log-file dumper, if requested.
286  if (!Opts->DiagnosticLogFile.empty())
287  SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
288 
289  if (!Opts->DiagnosticSerializationFile.empty())
290  SetupSerializedDiagnostics(Opts, *Diags,
292 
293  // Configure our handling of diagnostics.
294  ProcessWarningOptions(*Diags, *Opts);
295 
296  return Diags;
297 }
298 
299 // File Manager
300 
303  if (!VFS)
304  VFS = FileMgr ? &FileMgr->getVirtualFileSystem()
306  getDiagnostics());
307  assert(VFS && "FileManager has no VFS?");
308  FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS));
309  return FileMgr.get();
310 }
311 
312 // Source Manager
313 
315  SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
316 }
317 
318 // Initialize the remapping of files to alternative contents, e.g.,
319 // those specified through other files.
321  SourceManager &SourceMgr,
322  FileManager &FileMgr,
323  const PreprocessorOptions &InitOpts) {
324  // Remap files in the source manager (with buffers).
325  for (const auto &RB : InitOpts.RemappedFileBuffers) {
326  // Create the file entry for the file that we're mapping from.
327  const FileEntry *FromFile =
328  FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0);
329  if (!FromFile) {
330  Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first;
331  if (!InitOpts.RetainRemappedFileBuffers)
332  delete RB.second;
333  continue;
334  }
335 
336  // Override the contents of the "from" file with the contents of
337  // the "to" file.
338  SourceMgr.overrideFileContents(FromFile, RB.second,
339  InitOpts.RetainRemappedFileBuffers);
340  }
341 
342  // Remap files in the source manager (with other files).
343  for (const auto &RF : InitOpts.RemappedFiles) {
344  // Find the file that we're mapping to.
345  const FileEntry *ToFile = FileMgr.getFile(RF.second);
346  if (!ToFile) {
347  Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
348  continue;
349  }
350 
351  // Create the file entry for the file that we're mapping from.
352  const FileEntry *FromFile =
353  FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0);
354  if (!FromFile) {
355  Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first;
356  continue;
357  }
358 
359  // Override the contents of the "from" file with the contents of
360  // the "to" file.
361  SourceMgr.overrideFileContents(FromFile, ToFile);
362  }
363 
366 }
367 
368 // Preprocessor
369 
371  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
372 
373  // The module manager holds a reference to the old preprocessor (if any).
374  ModuleManager.reset();
375 
376  // Create the Preprocessor.
377  HeaderSearch *HeaderInfo =
380  PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(),
382  getSourceManager(), *HeaderInfo, *this,
383  /*IdentifierInfoLookup=*/nullptr,
384  /*OwnsHeaderSearch=*/true, TUKind);
386  PP->Initialize(getTarget(), getAuxTarget());
387 
388  if (PPOpts.DetailedRecord)
389  PP->createPreprocessingRecord();
390 
391  // Apply remappings to the source manager.
392  InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
393  PP->getFileManager(), PPOpts);
394 
395  // Predefine macros and configure the preprocessor.
397  getFrontendOpts());
398 
399  // Initialize the header search object. In CUDA compilations, we use the aux
400  // triple (the host triple) to initialize our header search, since we need to
401  // find the host headers in order to compile the CUDA code.
402  const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
403  if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
404  PP->getAuxTargetInfo())
405  HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
406 
407  ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
408  PP->getLangOpts(), *HeaderSearchTriple);
409 
410  PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
411 
412  if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules)
413  PP->getHeaderSearchInfo().setModuleCachePath(getSpecificModuleCachePath());
414 
415  // Handle generating dependencies, if requested.
417  if (!DepOpts.OutputFile.empty())
418  addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts));
419  if (!DepOpts.DOTOutputFile.empty())
422 
423  // If we don't have a collector, but we are collecting module dependencies,
424  // then we're the top level compiler instance and need to create one.
425  if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
426  ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
427  DepOpts.ModuleDependencyOutputDir);
428  }
429 
430  // If there is a module dep collector, register with other dep collectors
431  // and also (a) collect header maps and (b) TODO: input vfs overlay files.
432  if (ModuleDepCollector) {
433  addDependencyCollector(ModuleDepCollector);
434  collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector);
435  collectIncludePCH(*this, ModuleDepCollector);
436  collectVFSEntries(*this, ModuleDepCollector);
437  }
438 
439  for (auto &Listener : DependencyCollectors)
440  Listener->attachToPreprocessor(*PP);
441 
442  // Handle generating header include information, if requested.
443  if (DepOpts.ShowHeaderIncludes)
444  AttachHeaderIncludeGen(*PP, DepOpts);
445  if (!DepOpts.HeaderIncludeOutputFile.empty()) {
446  StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
447  if (OutputPath == "-")
448  OutputPath = "";
449  AttachHeaderIncludeGen(*PP, DepOpts,
450  /*ShowAllHeaders=*/true, OutputPath,
451  /*ShowDepth=*/false);
452  }
453 
455  AttachHeaderIncludeGen(*PP, DepOpts,
456  /*ShowAllHeaders=*/true, /*OutputPath=*/"",
457  /*ShowDepth=*/true, /*MSStyle=*/true);
458  }
459 }
460 
462  // Set up the module path, including the hash for the
463  // module-creation options.
464  SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath);
465  if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
466  llvm::sys::path::append(SpecificModuleCache,
468  return SpecificModuleCache.str();
469 }
470 
471 // ASTContext
472 
475  auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
477  PP.getBuiltinInfo());
478  Context->InitBuiltinTypes(getTarget(), getAuxTarget());
479  setASTContext(Context);
480 }
481 
482 // ExternalASTSource
483 
485  StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors,
486  void *DeserializationListener, bool OwnDeserializationListener) {
488  ModuleManager = createPCHExternalASTSource(
489  Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation,
490  AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(),
492  getFrontendOpts().ModuleFileExtensions, DependencyCollectors,
493  DeserializationListener, OwnDeserializationListener, Preamble,
494  getFrontendOpts().UseGlobalModuleIndex);
495 }
496 
498  StringRef Path, StringRef Sysroot, bool DisablePCHValidation,
499  bool AllowPCHWithCompilerErrors, Preprocessor &PP,
500  InMemoryModuleCache &ModuleCache, ASTContext &Context,
501  const PCHContainerReader &PCHContainerRdr,
502  ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
503  ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
504  void *DeserializationListener, bool OwnDeserializationListener,
505  bool Preamble, bool UseGlobalModuleIndex) {
507 
509  PP, ModuleCache, &Context, PCHContainerRdr, Extensions,
510  Sysroot.empty() ? "" : Sysroot.data(), DisablePCHValidation,
511  AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,
512  HSOpts.ModulesValidateSystemHeaders, UseGlobalModuleIndex));
513 
514  // We need the external source to be set up before we read the AST, because
515  // eagerly-deserialized declarations may use it.
516  Context.setExternalSource(Reader.get());
517 
518  Reader->setDeserializationListener(
519  static_cast<ASTDeserializationListener *>(DeserializationListener),
520  /*TakeOwnership=*/OwnDeserializationListener);
521 
522  for (auto &Listener : DependencyCollectors)
523  Listener->attachToASTReader(*Reader);
524 
525  switch (Reader->ReadAST(Path,
526  Preamble ? serialization::MK_Preamble
528  SourceLocation(),
530  case ASTReader::Success:
531  // Set the predefines buffer as suggested by the PCH reader. Typically, the
532  // predefines buffer will be empty.
533  PP.setPredefines(Reader->getSuggestedPredefines());
534  return Reader;
535 
536  case ASTReader::Failure:
537  // Unrecoverable failure: don't even try to process the input file.
538  break;
539 
540  case ASTReader::Missing:
545  // No suitable PCH file could be found. Return an error.
546  break;
547  }
548 
549  Context.setExternalSource(nullptr);
550  return nullptr;
551 }
552 
553 // Code Completion
554 
556  StringRef Filename,
557  unsigned Line,
558  unsigned Column) {
559  // Tell the source manager to chop off the given file at a specific
560  // line and column.
561  const FileEntry *Entry = PP.getFileManager().getFile(Filename);
562  if (!Entry) {
563  PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
564  << Filename;
565  return true;
566  }
567 
568  // Truncate the named file at the given line/column.
569  PP.SetCodeCompletionPoint(Entry, Line, Column);
570  return false;
571 }
572 
575  if (!CompletionConsumer) {
578  Loc.FileName, Loc.Line, Loc.Column,
579  getFrontendOpts().CodeCompleteOpts,
580  llvm::outs()));
581  if (!CompletionConsumer)
582  return;
584  Loc.Line, Loc.Column)) {
585  setCodeCompletionConsumer(nullptr);
586  return;
587  }
588 }
589 
591  FrontendTimerGroup.reset(
592  new llvm::TimerGroup("frontend", "Clang front-end time report"));
593  FrontendTimer.reset(
594  new llvm::Timer("frontend", "Clang front-end timer",
595  *FrontendTimerGroup));
596 }
597 
600  StringRef Filename,
601  unsigned Line,
602  unsigned Column,
603  const CodeCompleteOptions &Opts,
604  raw_ostream &OS) {
605  if (EnableCodeCompletion(PP, Filename, Line, Column))
606  return nullptr;
607 
608  // Set up the creation routine for code-completion.
609  return new PrintingCodeCompleteConsumer(Opts, OS);
610 }
611 
613  CodeCompleteConsumer *CompletionConsumer) {
614  TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
615  TUKind, CompletionConsumer));
616  // Attach the external sema source if there is any.
617  if (ExternalSemaSrc) {
618  TheSema->addExternalSource(ExternalSemaSrc.get());
619  ExternalSemaSrc->InitializeSema(*TheSema);
620  }
621 }
622 
623 // Output Files
624 
625 void CompilerInstance::addOutputFile(OutputFile &&OutFile) {
626  OutputFiles.push_back(std::move(OutFile));
627 }
628 
629 void CompilerInstance::clearOutputFiles(bool EraseFiles) {
630  for (OutputFile &OF : OutputFiles) {
631  if (!OF.TempFilename.empty()) {
632  if (EraseFiles) {
633  llvm::sys::fs::remove(OF.TempFilename);
634  } else {
635  SmallString<128> NewOutFile(OF.Filename);
636 
637  // If '-working-directory' was passed, the output filename should be
638  // relative to that.
639  FileMgr->FixupRelativePath(NewOutFile);
640  if (std::error_code ec =
641  llvm::sys::fs::rename(OF.TempFilename, NewOutFile)) {
642  getDiagnostics().Report(diag::err_unable_to_rename_temp)
643  << OF.TempFilename << OF.Filename << ec.message();
644 
645  llvm::sys::fs::remove(OF.TempFilename);
646  }
647  }
648  } else if (!OF.Filename.empty() && EraseFiles)
649  llvm::sys::fs::remove(OF.Filename);
650  }
651  OutputFiles.clear();
652  if (DeleteBuiltModules) {
653  for (auto &Module : BuiltModules)
655  BuiltModules.clear();
656  }
657  NonSeekStream.reset();
658 }
659 
660 std::unique_ptr<raw_pwrite_stream>
661 CompilerInstance::createDefaultOutputFile(bool Binary, StringRef InFile,
662  StringRef Extension) {
663  return createOutputFile(getFrontendOpts().OutputFile, Binary,
664  /*RemoveFileOnSignal=*/true, InFile, Extension,
665  /*UseTemporary=*/true);
666 }
667 
668 std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
669  return llvm::make_unique<llvm::raw_null_ostream>();
670 }
671 
672 std::unique_ptr<raw_pwrite_stream>
673 CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary,
674  bool RemoveFileOnSignal, StringRef InFile,
675  StringRef Extension, bool UseTemporary,
676  bool CreateMissingDirectories) {
677  std::string OutputPathName, TempPathName;
678  std::error_code EC;
679  std::unique_ptr<raw_pwrite_stream> OS = createOutputFile(
680  OutputPath, EC, Binary, RemoveFileOnSignal, InFile, Extension,
681  UseTemporary, CreateMissingDirectories, &OutputPathName, &TempPathName);
682  if (!OS) {
683  getDiagnostics().Report(diag::err_fe_unable_to_open_output) << OutputPath
684  << EC.message();
685  return nullptr;
686  }
687 
688  // Add the output file -- but don't try to remove "-", since this means we are
689  // using stdin.
691  OutputFile((OutputPathName != "-") ? OutputPathName : "", TempPathName));
692 
693  return OS;
694 }
695 
696 std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile(
697  StringRef OutputPath, std::error_code &Error, bool Binary,
698  bool RemoveFileOnSignal, StringRef InFile, StringRef Extension,
699  bool UseTemporary, bool CreateMissingDirectories,
700  std::string *ResultPathName, std::string *TempPathName) {
701  assert((!CreateMissingDirectories || UseTemporary) &&
702  "CreateMissingDirectories is only allowed when using temporary files");
703 
704  std::string OutFile, TempFile;
705  if (!OutputPath.empty()) {
706  OutFile = OutputPath;
707  } else if (InFile == "-") {
708  OutFile = "-";
709  } else if (!Extension.empty()) {
710  SmallString<128> Path(InFile);
711  llvm::sys::path::replace_extension(Path, Extension);
712  OutFile = Path.str();
713  } else {
714  OutFile = "-";
715  }
716 
717  std::unique_ptr<llvm::raw_fd_ostream> OS;
718  std::string OSFile;
719 
720  if (UseTemporary) {
721  if (OutFile == "-")
722  UseTemporary = false;
723  else {
724  llvm::sys::fs::file_status Status;
725  llvm::sys::fs::status(OutputPath, Status);
726  if (llvm::sys::fs::exists(Status)) {
727  // Fail early if we can't write to the final destination.
728  if (!llvm::sys::fs::can_write(OutputPath)) {
729  Error = make_error_code(llvm::errc::operation_not_permitted);
730  return nullptr;
731  }
732 
733  // Don't use a temporary if the output is a special file. This handles
734  // things like '-o /dev/null'
735  if (!llvm::sys::fs::is_regular_file(Status))
736  UseTemporary = false;
737  }
738  }
739  }
740 
741  if (UseTemporary) {
742  // Create a temporary file.
743  // Insert -%%%%%%%% before the extension (if any), and because some tools
744  // (noticeable, clang's own GlobalModuleIndex.cpp) glob for build
745  // artifacts, also append .tmp.
746  StringRef OutputExtension = llvm::sys::path::extension(OutFile);
747  SmallString<128> TempPath =
748  StringRef(OutFile).drop_back(OutputExtension.size());
749  TempPath += "-%%%%%%%%";
750  TempPath += OutputExtension;
751  TempPath += ".tmp";
752  int fd;
753  std::error_code EC =
754  llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
755 
756  if (CreateMissingDirectories &&
757  EC == llvm::errc::no_such_file_or_directory) {
758  StringRef Parent = llvm::sys::path::parent_path(OutputPath);
759  EC = llvm::sys::fs::create_directories(Parent);
760  if (!EC) {
761  EC = llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
762  }
763  }
764 
765  if (!EC) {
766  OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
767  OSFile = TempFile = TempPath.str();
768  }
769  // If we failed to create the temporary, fallback to writing to the file
770  // directly. This handles the corner case where we cannot write to the
771  // directory, but can write to the file.
772  }
773 
774  if (!OS) {
775  OSFile = OutFile;
776  OS.reset(new llvm::raw_fd_ostream(
777  OSFile, Error,
778  (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text)));
779  if (Error)
780  return nullptr;
781  }
782 
783  // Make sure the out stream file gets removed if we crash.
784  if (RemoveFileOnSignal)
785  llvm::sys::RemoveFileOnSignal(OSFile);
786 
787  if (ResultPathName)
788  *ResultPathName = OutFile;
789  if (TempPathName)
790  *TempPathName = TempFile;
791 
792  if (!Binary || OS->supportsSeeking())
793  return std::move(OS);
794 
795  auto B = llvm::make_unique<llvm::buffer_ostream>(*OS);
796  assert(!NonSeekStream);
797  NonSeekStream = std::move(OS);
798  return std::move(B);
799 }
800 
801 // Initialization Utilities
802 
806  hasPreprocessor() ? &getPreprocessor().getHeaderSearchInfo() : nullptr,
808 }
809 
810 // static
812  const FrontendInputFile &Input, DiagnosticsEngine &Diags,
813  FileManager &FileMgr, SourceManager &SourceMgr, HeaderSearch *HS,
814  DependencyOutputOptions &DepOpts, const FrontendOptions &Opts) {
820 
821  if (Input.isBuffer()) {
823  Input.getBuffer(), Kind));
824  assert(SourceMgr.getMainFileID().isValid() &&
825  "Couldn't establish MainFileID!");
826  return true;
827  }
828 
829  StringRef InputFile = Input.getFile();
830 
831  // Figure out where to get and map in the main file.
832  if (InputFile != "-") {
833  const FileEntry *File = FileMgr.getFile(InputFile, /*OpenFile=*/true);
834  if (!File) {
835  Diags.Report(diag::err_fe_error_reading) << InputFile;
836  return false;
837  }
838 
839  // The natural SourceManager infrastructure can't currently handle named
840  // pipes, but we would at least like to accept them for the main
841  // file. Detect them here, read them with the volatile flag so FileMgr will
842  // pick up the correct size, and simply override their contents as we do for
843  // STDIN.
844  if (File->isNamedPipe()) {
845  auto MB = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
846  if (MB) {
847  // Create a new virtual file that will have the correct size.
848  File = FileMgr.getVirtualFile(InputFile, (*MB)->getBufferSize(), 0);
849  SourceMgr.overrideFileContents(File, std::move(*MB));
850  } else {
851  Diags.Report(diag::err_cannot_open_file) << InputFile
852  << MB.getError().message();
853  return false;
854  }
855  }
856 
857  SourceMgr.setMainFileID(
858  SourceMgr.createFileID(File, SourceLocation(), Kind));
859  } else {
860  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> SBOrErr =
861  llvm::MemoryBuffer::getSTDIN();
862  if (std::error_code EC = SBOrErr.getError()) {
863  Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
864  return false;
865  }
866  std::unique_ptr<llvm::MemoryBuffer> SB = std::move(SBOrErr.get());
867 
868  const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
869  SB->getBufferSize(), 0);
870  SourceMgr.setMainFileID(
871  SourceMgr.createFileID(File, SourceLocation(), Kind));
872  SourceMgr.overrideFileContents(File, std::move(SB));
873  }
874 
875  assert(SourceMgr.getMainFileID().isValid() &&
876  "Couldn't establish MainFileID!");
877  return true;
878 }
879 
880 // High-Level Operations
881 
883  assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
884  assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
885  assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
886 
887  // FIXME: Take this as an argument, once all the APIs we used have moved to
888  // taking it as an input instead of hard-coding llvm::errs.
889  raw_ostream &OS = llvm::errs();
890 
891  if (!Act.PrepareToExecute(*this))
892  return false;
893 
894  // Create the target instance.
896  getInvocation().TargetOpts));
897  if (!hasTarget())
898  return false;
899 
900  // Create TargetInfo for the other side of CUDA and OpenMP compilation.
901  if ((getLangOpts().CUDA || getLangOpts().OpenMPIsDevice) &&
902  !getFrontendOpts().AuxTriple.empty()) {
903  auto TO = std::make_shared<TargetOptions>();
904  TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
905  TO->HostTriple = getTarget().getTriple().str();
907  }
908 
909  // Inform the target of the language options.
910  //
911  // FIXME: We shouldn't need to do this, the target should be immutable once
912  // created. This complexity should be lifted elsewhere.
914 
915  // Adjust target options based on codegen options.
917 
918  if (auto *Aux = getAuxTarget())
919  getTarget().setAuxTarget(Aux);
920 
921  // rewriter project will change target built-in bool type from its default.
922  if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
924 
925  // Validate/process some options.
926  if (getHeaderSearchOpts().Verbose)
927  OS << "clang -cc1 version " CLANG_VERSION_STRING
928  << " based upon " << BACKEND_PACKAGE_STRING
929  << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
930 
931  if (getFrontendOpts().ShowTimers)
933 
934  if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty())
935  llvm::EnableStatistics(false);
936 
937  for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
938  // Reset the ID tables if we are reusing the SourceManager and parsing
939  // regular files.
940  if (hasSourceManager() && !Act.isModelParsingAction())
942 
943  if (Act.BeginSourceFile(*this, FIF)) {
944  if (llvm::Error Err = Act.Execute()) {
945  consumeError(std::move(Err)); // FIXME this drops errors on the floor.
946  }
947  Act.EndSourceFile();
948  }
949  }
950 
951  // Notify the diagnostic client that all files were processed.
953 
954  if (getDiagnosticOpts().ShowCarets) {
955  // We can have multiple diagnostics sharing one diagnostic client.
956  // Get the total number of warnings/errors from the client.
957  unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
958  unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
959 
960  if (NumWarnings)
961  OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
962  if (NumWarnings && NumErrors)
963  OS << " and ";
964  if (NumErrors)
965  OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
966  if (NumWarnings || NumErrors) {
967  OS << " generated";
968  if (getLangOpts().CUDA) {
969  if (!getLangOpts().CUDAIsDevice) {
970  OS << " when compiling for host";
971  } else {
972  OS << " when compiling for " << getTargetOpts().CPU;
973  }
974  }
975  OS << ".\n";
976  }
977  }
978 
979  if (getFrontendOpts().ShowStats) {
980  if (hasFileManager()) {
982  OS << '\n';
983  }
984  llvm::PrintStatistics(OS);
985  }
986  StringRef StatsFile = getFrontendOpts().StatsFile;
987  if (!StatsFile.empty()) {
988  std::error_code EC;
989  auto StatS = llvm::make_unique<llvm::raw_fd_ostream>(StatsFile, EC,
990  llvm::sys::fs::F_Text);
991  if (EC) {
992  getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file)
993  << StatsFile << EC.message();
994  } else {
995  llvm::PrintStatisticsJSON(*StatS);
996  }
997  }
998 
999  return !getDiagnostics().getClient()->getNumErrors();
1000 }
1001 
1002 /// Determine the appropriate source input kind based on language
1003 /// options.
1005  if (LangOpts.OpenCL)
1006  return InputKind::OpenCL;
1007  if (LangOpts.CUDA)
1008  return InputKind::CUDA;
1009  if (LangOpts.ObjC)
1010  return LangOpts.CPlusPlus ? InputKind::ObjCXX : InputKind::ObjC;
1011  return LangOpts.CPlusPlus ? InputKind::CXX : InputKind::C;
1012 }
1013 
1014 /// Compile a module file for the given module, using the options
1015 /// provided by the importing compiler instance. Returns true if the module
1016 /// was built without errors.
1017 static bool
1018 compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
1019  StringRef ModuleName, FrontendInputFile Input,
1020  StringRef OriginalModuleMapFile, StringRef ModuleFileName,
1021  llvm::function_ref<void(CompilerInstance &)> PreBuildStep =
1022  [](CompilerInstance &) {},
1023  llvm::function_ref<void(CompilerInstance &)> PostBuildStep =
1024  [](CompilerInstance &) {}) {
1025  llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
1026 
1027  // Construct a compiler invocation for creating this module.
1028  auto Invocation =
1029  std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation());
1030 
1031  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1032 
1033  // For any options that aren't intended to affect how a module is built,
1034  // reset them to their default values.
1035  Invocation->getLangOpts()->resetNonModularOptions();
1036  PPOpts.resetNonModularOptions();
1037 
1038  // Remove any macro definitions that are explicitly ignored by the module.
1039  // They aren't supposed to affect how the module is built anyway.
1040  HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
1041  PPOpts.Macros.erase(
1042  std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(),
1043  [&HSOpts](const std::pair<std::string, bool> &def) {
1044  StringRef MacroDef = def.first;
1045  return HSOpts.ModulesIgnoreMacros.count(
1046  llvm::CachedHashString(MacroDef.split('=').first)) > 0;
1047  }),
1048  PPOpts.Macros.end());
1049 
1050  // If the original compiler invocation had -fmodule-name, pass it through.
1051  Invocation->getLangOpts()->ModuleName =
1052  ImportingInstance.getInvocation().getLangOpts()->ModuleName;
1053 
1054  // Note the name of the module we're building.
1055  Invocation->getLangOpts()->CurrentModule = ModuleName;
1056 
1057  // Make sure that the failed-module structure has been allocated in
1058  // the importing instance, and propagate the pointer to the newly-created
1059  // instance.
1060  PreprocessorOptions &ImportingPPOpts
1061  = ImportingInstance.getInvocation().getPreprocessorOpts();
1062  if (!ImportingPPOpts.FailedModules)
1063  ImportingPPOpts.FailedModules =
1064  std::make_shared<PreprocessorOptions::FailedModulesSet>();
1065  PPOpts.FailedModules = ImportingPPOpts.FailedModules;
1066 
1067  // If there is a module map file, build the module using the module map.
1068  // Set up the inputs/outputs so that we build the module from its umbrella
1069  // header.
1070  FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1071  FrontendOpts.OutputFile = ModuleFileName.str();
1072  FrontendOpts.DisableFree = false;
1073  FrontendOpts.GenerateGlobalModuleIndex = false;
1074  FrontendOpts.BuildingImplicitModule = true;
1075  FrontendOpts.OriginalModuleMap = OriginalModuleMapFile;
1076  // Force implicitly-built modules to hash the content of the module file.
1077  HSOpts.ModulesHashContent = true;
1078  FrontendOpts.Inputs = {Input};
1079 
1080  // Don't free the remapped file buffers; they are owned by our caller.
1081  PPOpts.RetainRemappedFileBuffers = true;
1082 
1083  Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
1084  assert(ImportingInstance.getInvocation().getModuleHash() ==
1085  Invocation->getModuleHash() && "Module hash mismatch!");
1086 
1087  // Construct a compiler instance that will be used to actually create the
1088  // module. Since we're sharing an in-memory module cache,
1089  // CompilerInstance::CompilerInstance is responsible for finalizing the
1090  // buffers to prevent use-after-frees.
1091  CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(),
1092  &ImportingInstance.getModuleCache());
1093  auto &Inv = *Invocation;
1094  Instance.setInvocation(std::move(Invocation));
1095 
1096  Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
1097  ImportingInstance.getDiagnosticClient()),
1098  /*ShouldOwnClient=*/true);
1099 
1100  // Note that this module is part of the module build stack, so that we
1101  // can detect cycles in the module graph.
1102  Instance.setFileManager(&ImportingInstance.getFileManager());
1103  Instance.createSourceManager(Instance.getFileManager());
1104  SourceManager &SourceMgr = Instance.getSourceManager();
1105  SourceMgr.setModuleBuildStack(
1106  ImportingInstance.getSourceManager().getModuleBuildStack());
1107  SourceMgr.pushModuleBuildStack(ModuleName,
1108  FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
1109 
1110  // If we're collecting module dependencies, we need to share a collector
1111  // between all of the module CompilerInstances. Other than that, we don't
1112  // want to produce any dependency output from the module build.
1113  Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
1114  Inv.getDependencyOutputOpts() = DependencyOutputOptions();
1115 
1116  ImportingInstance.getDiagnostics().Report(ImportLoc,
1117  diag::remark_module_build)
1118  << ModuleName << ModuleFileName;
1119 
1120  PreBuildStep(Instance);
1121 
1122  // Execute the action to actually build the module in-place. Use a separate
1123  // thread so that we get a stack large enough.
1124  llvm::CrashRecoveryContext CRC;
1125  CRC.RunSafelyOnThread(
1126  [&]() {
1128  Instance.ExecuteAction(Action);
1129  },
1131 
1132  PostBuildStep(Instance);
1133 
1134  ImportingInstance.getDiagnostics().Report(ImportLoc,
1135  diag::remark_module_build_done)
1136  << ModuleName;
1137 
1138  // Delete the temporary module map file.
1139  // FIXME: Even though we're executing under crash protection, it would still
1140  // be nice to do this with RemoveFileOnSignal when we can. However, that
1141  // doesn't make sense for all clients, so clean this up manually.
1142  Instance.clearOutputFiles(/*EraseFiles=*/true);
1143 
1144  return !Instance.getDiagnostics().hasErrorOccurred();
1145 }
1146 
1147 static const FileEntry *getPublicModuleMap(const FileEntry *File,
1148  FileManager &FileMgr) {
1149  StringRef Filename = llvm::sys::path::filename(File->getName());
1150  SmallString<128> PublicFilename(File->getDir()->getName());
1151  if (Filename == "module_private.map")
1152  llvm::sys::path::append(PublicFilename, "module.map");
1153  else if (Filename == "module.private.modulemap")
1154  llvm::sys::path::append(PublicFilename, "module.modulemap");
1155  else
1156  return nullptr;
1157  return FileMgr.getFile(PublicFilename);
1158 }
1159 
1160 /// Compile a module file for the given module, using the options
1161 /// provided by the importing compiler instance. Returns true if the module
1162 /// was built without errors.
1163 static bool compileModuleImpl(CompilerInstance &ImportingInstance,
1164  SourceLocation ImportLoc,
1165  Module *Module,
1166  StringRef ModuleFileName) {
1167  InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()),
1169 
1170  // Get or create the module map that we'll use to build this module.
1171  ModuleMap &ModMap
1172  = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1173  bool Result;
1174  if (const FileEntry *ModuleMapFile =
1175  ModMap.getContainingModuleMapFile(Module)) {
1176  // Canonicalize compilation to start with the public module map. This is
1177  // vital for submodules declarations in the private module maps to be
1178  // correctly parsed when depending on a top level module in the public one.
1179  if (const FileEntry *PublicMMFile = getPublicModuleMap(
1180  ModuleMapFile, ImportingInstance.getFileManager()))
1181  ModuleMapFile = PublicMMFile;
1182 
1183  // Use the module map where this module resides.
1184  Result = compileModuleImpl(
1185  ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1186  FrontendInputFile(ModuleMapFile->getName(), IK, +Module->IsSystem),
1187  ModMap.getModuleMapFileForUniquing(Module)->getName(),
1188  ModuleFileName);
1189  } else {
1190  // FIXME: We only need to fake up an input file here as a way of
1191  // transporting the module's directory to the module map parser. We should
1192  // be able to do that more directly, and parse from a memory buffer without
1193  // inventing this file.
1194  SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1195  llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
1196 
1197  std::string InferredModuleMapContent;
1198  llvm::raw_string_ostream OS(InferredModuleMapContent);
1199  Module->print(OS);
1200  OS.flush();
1201 
1202  Result = compileModuleImpl(
1203  ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1204  FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
1205  ModMap.getModuleMapFileForUniquing(Module)->getName(),
1206  ModuleFileName,
1207  [&](CompilerInstance &Instance) {
1208  std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
1209  llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
1210  ModuleMapFile = Instance.getFileManager().getVirtualFile(
1211  FakeModuleMapFile, InferredModuleMapContent.size(), 0);
1212  Instance.getSourceManager().overrideFileContents(
1213  ModuleMapFile, std::move(ModuleMapBuffer));
1214  });
1215  }
1216 
1217  // We've rebuilt a module. If we're allowed to generate or update the global
1218  // module index, record that fact in the importing compiler instance.
1219  if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
1220  ImportingInstance.setBuildGlobalModuleIndex(true);
1221  }
1222 
1223  return Result;
1224 }
1225 
1226 static bool compileAndLoadModule(CompilerInstance &ImportingInstance,
1227  SourceLocation ImportLoc,
1228  SourceLocation ModuleNameLoc, Module *Module,
1229  StringRef ModuleFileName) {
1230  DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1231 
1232  auto diagnoseBuildFailure = [&] {
1233  Diags.Report(ModuleNameLoc, diag::err_module_not_built)
1234  << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1235  };
1236 
1237  // FIXME: have LockFileManager return an error_code so that we can
1238  // avoid the mkdir when the directory already exists.
1239  StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
1240  llvm::sys::fs::create_directories(Dir);
1241 
1242  while (1) {
1243  unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1244  llvm::LockFileManager Locked(ModuleFileName);
1245  switch (Locked) {
1246  case llvm::LockFileManager::LFS_Error:
1247  // ModuleCache takes care of correctness and locks are only necessary for
1248  // performance. Fallback to building the module in case of any lock
1249  // related errors.
1250  Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure)
1251  << Module->Name << Locked.getErrorMessage();
1252  // Clear out any potential leftover.
1253  Locked.unsafeRemoveLockFile();
1254  LLVM_FALLTHROUGH;
1255  case llvm::LockFileManager::LFS_Owned:
1256  // We're responsible for building the module ourselves.
1257  if (!compileModuleImpl(ImportingInstance, ModuleNameLoc, Module,
1258  ModuleFileName)) {
1259  diagnoseBuildFailure();
1260  return false;
1261  }
1262  break;
1263 
1264  case llvm::LockFileManager::LFS_Shared:
1265  // Someone else is responsible for building the module. Wait for them to
1266  // finish.
1267  switch (Locked.waitForUnlock()) {
1268  case llvm::LockFileManager::Res_Success:
1269  ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1270  break;
1271  case llvm::LockFileManager::Res_OwnerDied:
1272  continue; // try again to get the lock.
1273  case llvm::LockFileManager::Res_Timeout:
1274  // Since ModuleCache takes care of correctness, we try waiting for
1275  // another process to complete the build so clang does not do it done
1276  // twice. If case of timeout, build it ourselves.
1277  Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout)
1278  << Module->Name;
1279  // Clear the lock file so that future invocations can make progress.
1280  Locked.unsafeRemoveLockFile();
1281  continue;
1282  }
1283  break;
1284  }
1285 
1286  // Try to read the module file, now that we've compiled it.
1287  ASTReader::ASTReadResult ReadResult =
1288  ImportingInstance.getModuleManager()->ReadAST(
1289  ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
1290  ModuleLoadCapabilities);
1291 
1292  if (ReadResult == ASTReader::OutOfDate &&
1293  Locked == llvm::LockFileManager::LFS_Shared) {
1294  // The module may be out of date in the presence of file system races,
1295  // or if one of its imports depends on header search paths that are not
1296  // consistent with this ImportingInstance. Try again...
1297  continue;
1298  } else if (ReadResult == ASTReader::Missing) {
1299  diagnoseBuildFailure();
1300  } else if (ReadResult != ASTReader::Success &&
1301  !Diags.hasErrorOccurred()) {
1302  // The ASTReader didn't diagnose the error, so conservatively report it.
1303  diagnoseBuildFailure();
1304  }
1305  return ReadResult == ASTReader::Success;
1306  }
1307 }
1308 
1309 /// Diagnose differences between the current definition of the given
1310 /// configuration macro and the definition provided on the command line.
1311 static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1312  Module *Mod, SourceLocation ImportLoc) {
1313  IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
1314  SourceManager &SourceMgr = PP.getSourceManager();
1315 
1316  // If this identifier has never had a macro definition, then it could
1317  // not have changed.
1318  if (!Id->hadMacroDefinition())
1319  return;
1320  auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
1321 
1322  // Find the macro definition from the command line.
1323  MacroInfo *CmdLineDefinition = nullptr;
1324  for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
1325  // We only care about the predefines buffer.
1326  FileID FID = SourceMgr.getFileID(MD->getLocation());
1327  if (FID.isInvalid() || FID != PP.getPredefinesFileID())
1328  continue;
1329  if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
1330  CmdLineDefinition = DMD->getMacroInfo();
1331  break;
1332  }
1333 
1334  auto *CurrentDefinition = PP.getMacroInfo(Id);
1335  if (CurrentDefinition == CmdLineDefinition) {
1336  // Macro matches. Nothing to do.
1337  } else if (!CurrentDefinition) {
1338  // This macro was defined on the command line, then #undef'd later.
1339  // Complain.
1340  PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1341  << true << ConfigMacro << Mod->getFullModuleName();
1342  auto LatestDef = LatestLocalMD->getDefinition();
1343  assert(LatestDef.isUndefined() &&
1344  "predefined macro went away with no #undef?");
1345  PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
1346  << true;
1347  return;
1348  } else if (!CmdLineDefinition) {
1349  // There was no definition for this macro in the predefines buffer,
1350  // but there was a local definition. Complain.
1351  PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1352  << false << ConfigMacro << Mod->getFullModuleName();
1353  PP.Diag(CurrentDefinition->getDefinitionLoc(),
1354  diag::note_module_def_undef_here)
1355  << false;
1356  } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
1357  /*Syntactically=*/true)) {
1358  // The macro definitions differ.
1359  PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1360  << false << ConfigMacro << Mod->getFullModuleName();
1361  PP.Diag(CurrentDefinition->getDefinitionLoc(),
1362  diag::note_module_def_undef_here)
1363  << false;
1364  }
1365 }
1366 
1367 /// Write a new timestamp file with the given path.
1368 static void writeTimestampFile(StringRef TimestampFile) {
1369  std::error_code EC;
1370  llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::F_None);
1371 }
1372 
1373 /// Prune the module cache of modules that haven't been accessed in
1374 /// a long time.
1375 static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
1376  struct stat StatBuf;
1377  llvm::SmallString<128> TimestampFile;
1378  TimestampFile = HSOpts.ModuleCachePath;
1379  assert(!TimestampFile.empty());
1380  llvm::sys::path::append(TimestampFile, "modules.timestamp");
1381 
1382  // Try to stat() the timestamp file.
1383  if (::stat(TimestampFile.c_str(), &StatBuf)) {
1384  // If the timestamp file wasn't there, create one now.
1385  if (errno == ENOENT) {
1386  writeTimestampFile(TimestampFile);
1387  }
1388  return;
1389  }
1390 
1391  // Check whether the time stamp is older than our pruning interval.
1392  // If not, do nothing.
1393  time_t TimeStampModTime = StatBuf.st_mtime;
1394  time_t CurrentTime = time(nullptr);
1395  if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
1396  return;
1397 
1398  // Write a new timestamp file so that nobody else attempts to prune.
1399  // There is a benign race condition here, if two Clang instances happen to
1400  // notice at the same time that the timestamp is out-of-date.
1401  writeTimestampFile(TimestampFile);
1402 
1403  // Walk the entire module cache, looking for unused module files and module
1404  // indices.
1405  std::error_code EC;
1406  SmallString<128> ModuleCachePathNative;
1407  llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
1408  for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd;
1409  Dir != DirEnd && !EC; Dir.increment(EC)) {
1410  // If we don't have a directory, there's nothing to look into.
1411  if (!llvm::sys::fs::is_directory(Dir->path()))
1412  continue;
1413 
1414  // Walk all of the files within this directory.
1415  for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
1416  File != FileEnd && !EC; File.increment(EC)) {
1417  // We only care about module and global module index files.
1418  StringRef Extension = llvm::sys::path::extension(File->path());
1419  if (Extension != ".pcm" && Extension != ".timestamp" &&
1420  llvm::sys::path::filename(File->path()) != "modules.idx")
1421  continue;
1422 
1423  // Look at this file. If we can't stat it, there's nothing interesting
1424  // there.
1425  if (::stat(File->path().c_str(), &StatBuf))
1426  continue;
1427 
1428  // If the file has been used recently enough, leave it there.
1429  time_t FileAccessTime = StatBuf.st_atime;
1430  if (CurrentTime - FileAccessTime <=
1431  time_t(HSOpts.ModuleCachePruneAfter)) {
1432  continue;
1433  }
1434 
1435  // Remove the file.
1436  llvm::sys::fs::remove(File->path());
1437 
1438  // Remove the timestamp file.
1439  std::string TimpestampFilename = File->path() + ".timestamp";
1440  llvm::sys::fs::remove(TimpestampFilename);
1441  }
1442 
1443  // If we removed all of the files in the directory, remove the directory
1444  // itself.
1445  if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
1446  llvm::sys::fs::directory_iterator() && !EC)
1447  llvm::sys::fs::remove(Dir->path());
1448  }
1449 }
1450 
1452  if (!ModuleManager) {
1453  if (!hasASTContext())
1454  createASTContext();
1455 
1456  // If we're implicitly building modules but not currently recursively
1457  // building a module, check whether we need to prune the module cache.
1458  if (getSourceManager().getModuleBuildStack().empty() &&
1459  !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() &&
1460  getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
1461  getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
1463  }
1464 
1466  std::string Sysroot = HSOpts.Sysroot;
1467  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1468  std::unique_ptr<llvm::Timer> ReadTimer;
1469  if (FrontendTimerGroup)
1470  ReadTimer = llvm::make_unique<llvm::Timer>("reading_modules",
1471  "Reading modules",
1472  *FrontendTimerGroup);
1473  ModuleManager = new ASTReader(
1475  getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions,
1476  Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation,
1477  /*AllowASTWithCompilerErrors=*/false,
1478  /*AllowConfigurationMismatch=*/false,
1480  getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));
1481  if (hasASTConsumer()) {
1482  ModuleManager->setDeserializationListener(
1483  getASTConsumer().GetASTDeserializationListener());
1485  getASTConsumer().GetASTMutationListener());
1486  }
1487  getASTContext().setExternalSource(ModuleManager);
1488  if (hasSema())
1489  ModuleManager->InitializeSema(getSema());
1490  if (hasASTConsumer())
1491  ModuleManager->StartTranslationUnit(&getASTConsumer());
1492 
1493  for (auto &Listener : DependencyCollectors)
1494  Listener->attachToASTReader(*ModuleManager);
1495  }
1496 }
1497 
1498 bool CompilerInstance::loadModuleFile(StringRef FileName) {
1499  llvm::Timer Timer;
1500  if (FrontendTimerGroup)
1501  Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(),
1502  *FrontendTimerGroup);
1503  llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
1504 
1505  // Helper to recursively read the module names for all modules we're adding.
1506  // We mark these as known and redirect any attempt to load that module to
1507  // the files we were handed.
1508  struct ReadModuleNames : ASTReaderListener {
1509  CompilerInstance &CI;
1511 
1512  ReadModuleNames(CompilerInstance &CI) : CI(CI) {}
1513 
1514  void ReadModuleName(StringRef ModuleName) override {
1515  LoadedModules.push_back(
1516  CI.getPreprocessor().getIdentifierInfo(ModuleName));
1517  }
1518 
1519  void registerAll() {
1520  for (auto *II : LoadedModules) {
1521  CI.KnownModules[II] = CI.getPreprocessor()
1523  .getModuleMap()
1524  .findModule(II->getName());
1525  }
1526  LoadedModules.clear();
1527  }
1528 
1529  void markAllUnavailable() {
1530  for (auto *II : LoadedModules) {
1531  if (Module *M = CI.getPreprocessor()
1533  .getModuleMap()
1534  .findModule(II->getName())) {
1535  M->HasIncompatibleModuleFile = true;
1536 
1537  // Mark module as available if the only reason it was unavailable
1538  // was missing headers.
1540  Stack.push_back(M);
1541  while (!Stack.empty()) {
1542  Module *Current = Stack.pop_back_val();
1543  if (Current->IsMissingRequirement) continue;
1544  Current->IsAvailable = true;
1545  Stack.insert(Stack.end(),
1546  Current->submodule_begin(), Current->submodule_end());
1547  }
1548  }
1549  }
1550  LoadedModules.clear();
1551  }
1552  };
1553 
1554  // If we don't already have an ASTReader, create one now.
1555  if (!ModuleManager)
1557 
1558  // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
1559  // ASTReader to diagnose it, since it can produce better errors that we can.
1560  bool ConfigMismatchIsRecoverable =
1561  getDiagnostics().getDiagnosticLevel(diag::warn_module_config_mismatch,
1562  SourceLocation())
1564 
1565  auto Listener = llvm::make_unique<ReadModuleNames>(*this);
1566  auto &ListenerRef = *Listener;
1567  ASTReader::ListenerScope ReadModuleNamesListener(*ModuleManager,
1568  std::move(Listener));
1569 
1570  // Try to load the module file.
1571  switch (ModuleManager->ReadAST(
1573  ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0)) {
1574  case ASTReader::Success:
1575  // We successfully loaded the module file; remember the set of provided
1576  // modules so that we don't try to load implicit modules for them.
1577  ListenerRef.registerAll();
1578  return true;
1579 
1581  // Ignore unusable module files.
1582  getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch)
1583  << FileName;
1584  // All modules provided by any files we tried and failed to load are now
1585  // unavailable; includes of those modules should now be handled textually.
1586  ListenerRef.markAllUnavailable();
1587  return true;
1588 
1589  default:
1590  return false;
1591  }
1592 }
1593 
1596  ModuleIdPath Path,
1598  bool IsInclusionDirective) {
1599  // Determine what file we're searching from.
1600  StringRef ModuleName = Path[0].first->getName();
1601  SourceLocation ModuleNameLoc = Path[0].second;
1602 
1603  // If we've already handled this import, just return the cached result.
1604  // This one-element cache is important to eliminate redundant diagnostics
1605  // when both the preprocessor and parser see the same import declaration.
1606  if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) {
1607  // Make the named module visible.
1608  if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
1609  ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
1610  ImportLoc);
1611  return LastModuleImportResult;
1612  }
1613 
1614  clang::Module *Module = nullptr;
1615 
1616  // If we don't already have information on this module, load the module now.
1617  llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
1618  = KnownModules.find(Path[0].first);
1619  if (Known != KnownModules.end()) {
1620  // Retrieve the cached top-level module.
1621  Module = Known->second;
1622  } else if (ModuleName == getLangOpts().CurrentModule) {
1623  // This is the module we're building.
1624  Module = PP->getHeaderSearchInfo().lookupModule(
1625  ModuleName, /*AllowSearch*/ true,
1626  /*AllowExtraModuleMapSearch*/ !IsInclusionDirective);
1627  /// FIXME: perhaps we should (a) look for a module using the module name
1628  // to file map (PrebuiltModuleFiles) and (b) diagnose if still not found?
1629  //if (Module == nullptr) {
1630  // getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1631  // << ModuleName;
1632  // ModuleBuildFailed = true;
1633  // return ModuleLoadResult();
1634  //}
1635  Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1636  } else {
1637  // Search for a module with the given name.
1638  Module = PP->getHeaderSearchInfo().lookupModule(ModuleName, true,
1639  !IsInclusionDirective);
1640  HeaderSearchOptions &HSOpts =
1641  PP->getHeaderSearchInfo().getHeaderSearchOpts();
1642 
1643  std::string ModuleFileName;
1644  enum ModuleSource {
1645  ModuleNotFound, ModuleCache, PrebuiltModulePath, ModuleBuildPragma
1646  } Source = ModuleNotFound;
1647 
1648  // Check to see if the module has been built as part of this compilation
1649  // via a module build pragma.
1650  auto BuiltModuleIt = BuiltModules.find(ModuleName);
1651  if (BuiltModuleIt != BuiltModules.end()) {
1652  ModuleFileName = BuiltModuleIt->second;
1653  Source = ModuleBuildPragma;
1654  }
1655 
1656  // Try to load the module from the prebuilt module path.
1657  if (Source == ModuleNotFound && (!HSOpts.PrebuiltModuleFiles.empty() ||
1658  !HSOpts.PrebuiltModulePaths.empty())) {
1659  ModuleFileName =
1660  PP->getHeaderSearchInfo().getPrebuiltModuleFileName(ModuleName);
1661  if (!ModuleFileName.empty())
1662  Source = PrebuiltModulePath;
1663  }
1664 
1665  // Try to load the module from the module cache.
1666  if (Source == ModuleNotFound && Module) {
1667  ModuleFileName = PP->getHeaderSearchInfo().getCachedModuleFileName(Module);
1668  Source = ModuleCache;
1669  }
1670 
1671  if (Source == ModuleNotFound) {
1672  // We can't find a module, error out here.
1673  getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1674  << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1675  ModuleBuildFailed = true;
1676  return ModuleLoadResult();
1677  }
1678 
1679  if (ModuleFileName.empty()) {
1680  if (Module && Module->HasIncompatibleModuleFile) {
1681  // We tried and failed to load a module file for this module. Fall
1682  // back to textual inclusion for its headers.
1684  }
1685 
1686  getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
1687  << ModuleName;
1688  ModuleBuildFailed = true;
1689  return ModuleLoadResult();
1690  }
1691 
1692  // If we don't already have an ASTReader, create one now.
1693  if (!ModuleManager)
1695 
1696  llvm::Timer Timer;
1697  if (FrontendTimerGroup)
1698  Timer.init("loading." + ModuleFileName, "Loading " + ModuleFileName,
1699  *FrontendTimerGroup);
1700  llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
1701  llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
1702 
1703  // Try to load the module file. If we are not trying to load from the
1704  // module cache, we don't know how to rebuild modules.
1705  unsigned ARRFlags = Source == ModuleCache ?
1707  Source == PrebuiltModulePath ?
1708  0 :
1710  switch (ModuleManager->ReadAST(ModuleFileName,
1711  Source == PrebuiltModulePath
1713  : Source == ModuleBuildPragma
1716  ImportLoc, ARRFlags)) {
1717  case ASTReader::Success: {
1718  if (Source != ModuleCache && !Module) {
1719  Module = PP->getHeaderSearchInfo().lookupModule(ModuleName, true,
1720  !IsInclusionDirective);
1721  if (!Module || !Module->getASTFile() ||
1722  FileMgr->getFile(ModuleFileName) != Module->getASTFile()) {
1723  // Error out if Module does not refer to the file in the prebuilt
1724  // module path.
1725  getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
1726  << ModuleName;
1727  ModuleBuildFailed = true;
1728  KnownModules[Path[0].first] = nullptr;
1729  return ModuleLoadResult();
1730  }
1731  }
1732  break;
1733  }
1734 
1735  case ASTReader::OutOfDate:
1736  case ASTReader::Missing: {
1737  if (Source != ModuleCache) {
1738  // We don't know the desired configuration for this module and don't
1739  // necessarily even have a module map. Since ReadAST already produces
1740  // diagnostics for these two cases, we simply error out here.
1741  ModuleBuildFailed = true;
1742  KnownModules[Path[0].first] = nullptr;
1743  return ModuleLoadResult();
1744  }
1745 
1746  // The module file is missing or out-of-date. Build it.
1747  assert(Module && "missing module file");
1748  // Check whether there is a cycle in the module graph.
1750  ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
1751  for (; Pos != PosEnd; ++Pos) {
1752  if (Pos->first == ModuleName)
1753  break;
1754  }
1755 
1756  if (Pos != PosEnd) {
1757  SmallString<256> CyclePath;
1758  for (; Pos != PosEnd; ++Pos) {
1759  CyclePath += Pos->first;
1760  CyclePath += " -> ";
1761  }
1762  CyclePath += ModuleName;
1763 
1764  getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1765  << ModuleName << CyclePath;
1766  return ModuleLoadResult();
1767  }
1768 
1769  // Check whether we have already attempted to build this module (but
1770  // failed).
1771  if (getPreprocessorOpts().FailedModules &&
1772  getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
1773  getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
1774  << ModuleName
1775  << SourceRange(ImportLoc, ModuleNameLoc);
1776  ModuleBuildFailed = true;
1777  return ModuleLoadResult();
1778  }
1779 
1780  // Try to compile and then load the module.
1781  if (!compileAndLoadModule(*this, ImportLoc, ModuleNameLoc, Module,
1782  ModuleFileName)) {
1783  assert(getDiagnostics().hasErrorOccurred() &&
1784  "undiagnosed error in compileAndLoadModule");
1785  if (getPreprocessorOpts().FailedModules)
1786  getPreprocessorOpts().FailedModules->addFailed(ModuleName);
1787  KnownModules[Path[0].first] = nullptr;
1788  ModuleBuildFailed = true;
1789  return ModuleLoadResult();
1790  }
1791 
1792  // Okay, we've rebuilt and now loaded the module.
1793  break;
1794  }
1795 
1797  if (Source == PrebuiltModulePath)
1798  // FIXME: We shouldn't be setting HadFatalFailure below if we only
1799  // produce a warning here!
1801  diag::warn_module_config_mismatch)
1802  << ModuleFileName;
1803  // Fall through to error out.
1804  LLVM_FALLTHROUGH;
1806  case ASTReader::HadErrors:
1808  // FIXME: The ASTReader will already have complained, but can we shoehorn
1809  // that diagnostic information into a more useful form?
1810  KnownModules[Path[0].first] = nullptr;
1811  return ModuleLoadResult();
1812 
1813  case ASTReader::Failure:
1815  // Already complained, but note now that we failed.
1816  KnownModules[Path[0].first] = nullptr;
1817  ModuleBuildFailed = true;
1818  return ModuleLoadResult();
1819  }
1820 
1821  // Cache the result of this top-level module lookup for later.
1822  Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1823  }
1824 
1825  // If we never found the module, fail.
1826  if (!Module)
1827  return ModuleLoadResult();
1828 
1829  // Verify that the rest of the module path actually corresponds to
1830  // a submodule.
1831  bool MapPrivateSubModToTopLevel = false;
1832  if (Path.size() > 1) {
1833  for (unsigned I = 1, N = Path.size(); I != N; ++I) {
1834  StringRef Name = Path[I].first->getName();
1835  clang::Module *Sub = Module->findSubmodule(Name);
1836 
1837  // If the user is requesting Foo.Private and it doesn't exist, try to
1838  // match Foo_Private and emit a warning asking for the user to write
1839  // @import Foo_Private instead. FIXME: remove this when existing clients
1840  // migrate off of Foo.Private syntax.
1841  if (!Sub && PP->getLangOpts().ImplicitModules && Name == "Private" &&
1842  Module == Module->getTopLevelModule()) {
1843  SmallString<128> PrivateModule(Module->Name);
1844  PrivateModule.append("_Private");
1845 
1847  auto &II = PP->getIdentifierTable().get(
1848  PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
1849  PrivPath.push_back(std::make_pair(&II, Path[0].second));
1850 
1851  if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, true,
1852  !IsInclusionDirective))
1853  Sub =
1854  loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
1855  if (Sub) {
1856  MapPrivateSubModToTopLevel = true;
1857  if (!getDiagnostics().isIgnored(
1858  diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
1859  getDiagnostics().Report(Path[I].second,
1860  diag::warn_no_priv_submodule_use_toplevel)
1861  << Path[I].first << Module->getFullModuleName() << PrivateModule
1862  << SourceRange(Path[0].second, Path[I].second)
1863  << FixItHint::CreateReplacement(SourceRange(Path[0].second),
1864  PrivateModule);
1866  diag::note_private_top_level_defined);
1867  }
1868  }
1869  }
1870 
1871  if (!Sub) {
1872  // Attempt to perform typo correction to find a module name that works.
1874  unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
1875 
1877  JEnd = Module->submodule_end();
1878  J != JEnd; ++J) {
1879  unsigned ED = Name.edit_distance((*J)->Name,
1880  /*AllowReplacements=*/true,
1881  BestEditDistance);
1882  if (ED <= BestEditDistance) {
1883  if (ED < BestEditDistance) {
1884  Best.clear();
1885  BestEditDistance = ED;
1886  }
1887 
1888  Best.push_back((*J)->Name);
1889  }
1890  }
1891 
1892  // If there was a clear winner, user it.
1893  if (Best.size() == 1) {
1894  getDiagnostics().Report(Path[I].second,
1895  diag::err_no_submodule_suggest)
1896  << Path[I].first << Module->getFullModuleName() << Best[0]
1897  << SourceRange(Path[0].second, Path[I-1].second)
1898  << FixItHint::CreateReplacement(SourceRange(Path[I].second),
1899  Best[0]);
1900 
1901  Sub = Module->findSubmodule(Best[0]);
1902  }
1903  }
1904 
1905  if (!Sub) {
1906  // No submodule by this name. Complain, and don't look for further
1907  // submodules.
1908  getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
1909  << Path[I].first << Module->getFullModuleName()
1910  << SourceRange(Path[0].second, Path[I-1].second);
1911  break;
1912  }
1913 
1914  Module = Sub;
1915  }
1916  }
1917 
1918  // Make the named module visible, if it's not already part of the module
1919  // we are parsing.
1920  if (ModuleName != getLangOpts().CurrentModule) {
1921  if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) {
1922  // We have an umbrella header or directory that doesn't actually include
1923  // all of the headers within the directory it covers. Complain about
1924  // this missing submodule and recover by forgetting that we ever saw
1925  // this submodule.
1926  // FIXME: Should we detect this at module load time? It seems fairly
1927  // expensive (and rare).
1928  getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
1929  << Module->getFullModuleName()
1930  << SourceRange(Path.front().second, Path.back().second);
1931 
1933  }
1934 
1935  // Check whether this module is available.
1937  getDiagnostics(), Module)) {
1938  getDiagnostics().Report(ImportLoc, diag::note_module_import_here)
1939  << SourceRange(Path.front().second, Path.back().second);
1940  LastModuleImportLoc = ImportLoc;
1941  LastModuleImportResult = ModuleLoadResult();
1942  return ModuleLoadResult();
1943  }
1944 
1945  ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc);
1946  }
1947 
1948  // Check for any configuration macros that have changed.
1949  clang::Module *TopModule = Module->getTopLevelModule();
1950  for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
1952  Module, ImportLoc);
1953  }
1954 
1955  // Resolve any remaining module using export_as for this one.
1956  getPreprocessor()
1958  .getModuleMap()
1959  .resolveLinkAsDependencies(TopModule);
1960 
1961  LastModuleImportLoc = ImportLoc;
1962  LastModuleImportResult = ModuleLoadResult(Module);
1963  return LastModuleImportResult;
1964 }
1965 
1967  StringRef ModuleName,
1968  StringRef Source) {
1969  // Avoid creating filenames with special characters.
1970  SmallString<128> CleanModuleName(ModuleName);
1971  for (auto &C : CleanModuleName)
1972  if (!isAlphanumeric(C))
1973  C = '_';
1974 
1975  // FIXME: Using a randomized filename here means that our intermediate .pcm
1976  // output is nondeterministic (as .pcm files refer to each other by name).
1977  // Can this affect the output in any way?
1978  SmallString<128> ModuleFileName;
1979  if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
1980  CleanModuleName, "pcm", ModuleFileName)) {
1981  getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output)
1982  << ModuleFileName << EC.message();
1983  return;
1984  }
1985  std::string ModuleMapFileName = (CleanModuleName + ".map").str();
1986 
1987  FrontendInputFile Input(
1988  ModuleMapFileName,
1989  InputKind(getLanguageFromOptions(*Invocation->getLangOpts()),
1990  InputKind::ModuleMap, /*Preprocessed*/true));
1991 
1992  std::string NullTerminatedSource(Source.str());
1993 
1994  auto PreBuildStep = [&](CompilerInstance &Other) {
1995  // Create a virtual file containing our desired source.
1996  // FIXME: We shouldn't need to do this.
1997  const FileEntry *ModuleMapFile = Other.getFileManager().getVirtualFile(
1998  ModuleMapFileName, NullTerminatedSource.size(), 0);
1999  Other.getSourceManager().overrideFileContents(
2000  ModuleMapFile,
2001  llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource.c_str()));
2002 
2003  Other.BuiltModules = std::move(BuiltModules);
2004  Other.DeleteBuiltModules = false;
2005  };
2006 
2007  auto PostBuildStep = [this](CompilerInstance &Other) {
2008  BuiltModules = std::move(Other.BuiltModules);
2009  };
2010 
2011  // Build the module, inheriting any modules that we've built locally.
2012  if (compileModuleImpl(*this, ImportLoc, ModuleName, Input, StringRef(),
2013  ModuleFileName, PreBuildStep, PostBuildStep)) {
2014  BuiltModules[ModuleName] = ModuleFileName.str();
2015  llvm::sys::RemoveFileOnSignal(ModuleFileName);
2016  }
2017 }
2018 
2021  SourceLocation ImportLoc) {
2022  if (!ModuleManager)
2024  if (!ModuleManager)
2025  return;
2026 
2027  ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc);
2028 }
2029 
2031  SourceLocation TriggerLoc) {
2032  if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
2033  return nullptr;
2034  if (!ModuleManager)
2036  // Can't do anything if we don't have the module manager.
2037  if (!ModuleManager)
2038  return nullptr;
2039  // Get an existing global index. This loads it if not already
2040  // loaded.
2041  ModuleManager->loadGlobalIndex();
2042  GlobalModuleIndex *GlobalIndex = ModuleManager->getGlobalIndex();
2043  // If the global index doesn't exist, create it.
2044  if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
2045  hasPreprocessor()) {
2046  llvm::sys::fs::create_directories(
2047  getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
2050  getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
2051  // FIXME this drops the error on the floor. This code is only used for
2052  // typo correction and drops more than just this one source of errors
2053  // (such as the directory creation failure above). It should handle the
2054  // error.
2055  consumeError(std::move(Err));
2056  return nullptr;
2057  }
2058  ModuleManager->resetForReload();
2059  ModuleManager->loadGlobalIndex();
2060  GlobalIndex = ModuleManager->getGlobalIndex();
2061  }
2062  // For finding modules needing to be imported for fixit messages,
2063  // we need to make the global index cover all modules, so we do that here.
2064  if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
2066  bool RecreateIndex = false;
2067  for (ModuleMap::module_iterator I = MMap.module_begin(),
2068  E = MMap.module_end(); I != E; ++I) {
2069  Module *TheModule = I->second;
2070  const FileEntry *Entry = TheModule->getASTFile();
2071  if (!Entry) {
2073  Path.push_back(std::make_pair(
2074  getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
2075  std::reverse(Path.begin(), Path.end());
2076  // Load a module as hidden. This also adds it to the global index.
2077  loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
2078  RecreateIndex = true;
2079  }
2080  }
2081  if (RecreateIndex) {
2084  getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
2085  // FIXME As above, this drops the error on the floor.
2086  consumeError(std::move(Err));
2087  return nullptr;
2088  }
2089  ModuleManager->resetForReload();
2090  ModuleManager->loadGlobalIndex();
2091  GlobalIndex = ModuleManager->getGlobalIndex();
2092  }
2093  HaveFullGlobalModuleIndex = true;
2094  }
2095  return GlobalIndex;
2096 }
2097 
2098 // Check global module index for missing imports.
2099 bool
2101  SourceLocation TriggerLoc) {
2102  // Look for the symbol in non-imported modules, but only if an error
2103  // actually occurred.
2104  if (!buildingModule()) {
2105  // Load global module index, or retrieve a previously loaded one.
2107  TriggerLoc);
2108 
2109  // Only if we have a global index.
2110  if (GlobalIndex) {
2111  GlobalModuleIndex::HitSet FoundModules;
2112 
2113  // Find the modules that reference the identifier.
2114  // Note that this only finds top-level modules.
2115  // We'll let diagnoseTypo find the actual declaration module.
2116  if (GlobalIndex->lookupIdentifier(Name, FoundModules))
2117  return true;
2118  }
2119  }
2120 
2121  return false;
2122 }
2123 void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(takeSema()); }
2124 
2127  ExternalSemaSrc = std::move(ESS);
2128 }
std::string OutputFile
The output file, if any.
unsigned IsAvailable
Whether this module is available in the current translation unit.
Definition: Module.h:219
void setExternalSource(IntrusiveRefCntPtr< ExternalASTSource > Source)
Attach an external AST source to the AST context.
Definition: ASTContext.cpp:915
static bool compileAndLoadModule(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName)
Defines the clang::ASTContext interface.
LangOptions & getLangOpts()
ASTReadResult
The result of reading the control block of an AST file, which can fail for various reasons...
Definition: ASTReader.h:380
std::string OriginalModuleMap
When the input is a module map, the original module map file from which that map was inferred...
std::string Name
The name of this module.
Definition: Module.h:67
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
void createCodeCompletionConsumer()
Create a code completion consumer using the invocation; note that this will cause the source manager ...
bool hasErrorOccurred() const
Definition: Diagnostic.h:749
std::string ModuleDependencyOutputDir
The directory to copy module dependencies to when collecting them.
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:510
std::string DwarfDebugFlags
The string to embed in the debug information for the compile unit, if non-empty.
std::vector< std::string > PrebuiltModulePaths
The directories used to load prebuilt module files.
SelectorTable & getSelectorTable()
Definition: Preprocessor.h:912
std::string DOTOutputFile
The file to write GraphViz-formatted header dependencies to.
FileManager * createFileManager(IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Create the file manager and replace any existing one with it.
std::vector< Module * >::iterator submodule_iterator
Definition: Module.h:560
VerifyDiagnosticConsumer - Create a diagnostic client which will use markers in the input source to c...
void createFrontendTimer()
Create the frontend timer and replace any existing one with it.
bool RemappedFilesKeepOriginalName
True if the SourceManager should report the original file name for contents of files that were remapp...
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:116
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
virtual void adjust(LangOptions &Opts)
Set forced language options.
Definition: TargetInfo.cpp:324
void setCodeCompletionConsumer(CodeCompleteConsumer *Value)
setCodeCompletionConsumer - Replace the current code completion consumer; the compiler instance takes...
Defines the clang::FileManager interface and associated types.
void EndSourceFile()
Perform any per-file post processing, deallocate per-file objects, and run statistics and output file...
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation&#39;s diagnostic options and replace any existing one ...
submodule_iterator submodule_begin()
Definition: Module.h:563
static void writeTimestampFile(StringRef TimestampFile)
Write a new timestamp file with the given path.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...
Format getFormat() const
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:985
bool buildingModule() const
Returns true if this instance is building a module.
Definition: ModuleLoader.h:84
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Module.h:457
Defines the SourceManager interface.
void createSema(TranslationUnitKind TUKind, CodeCompleteConsumer *CompletionConsumer)
Create the Sema object to be used for parsing.
FileManager & getFileManager() const
Definition: Preprocessor.h:906
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
Abstract base class for actions which can be performed by the frontend.
const FileEntry * getModuleMapFileForUniquing(const Module *M) const
Get the module map file that (along with the module name) uniquely identifies this module...
Definition: ModuleMap.cpp:1223
DiagnosticOptions & getDiagnosticOpts()
std::string HeaderIncludeOutputFile
The file to write header include output to.
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
const FileEntry * getASTFile() const
The serialized AST file for this module, if one was created.
Definition: Module.h:472
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1297
bool InitializeSourceManager(const FrontendInputFile &Input)
InitializeSourceManager - Initialize the source manager to set InputFile as the main file...
InMemoryModuleCache & getModuleCache() const
void setAuxTarget(TargetInfo *Value)
Replace the current AuxTarget.
ChainedDiagnosticConsumer - Chain two diagnostic clients so that diagnostics go to the first client a...
llvm::StringMap< Module * >::const_iterator module_iterator
Definition: ModuleMap.h:683
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::shared_ptr< FailedModulesSet > FailedModules
The set of modules that failed to build.
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Definition: Diagnostic.h:518
ASTContext & getASTContext() const
The client can handle an AST file that cannot load because it is out-of-date relative to its input fi...
Definition: ASTReader.h:1513
void setModuleManager(IntrusiveRefCntPtr< ASTReader > Reader)
The AST file has errors.
Definition: ASTReader.h:403
virtual void adjustTargetOptions(const CodeGenOptions &CGOpts, TargetOptions &TargetOpts) const
Adjust target options based on codegen options.
Definition: TargetInfo.h:1039
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
llvm::SmallSetVector< llvm::CachedHashString, 16 > ModulesIgnoreMacros
The set of macro names that should be ignored for the purposes of computing the module hash...
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1496
DependencyOutputOptions & getDependencyOutputOpts()
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:77
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:660
std::string ModuleCachePath
The directory used for the module cache.
Builtin::Context & getBuiltinInfo()
Definition: Preprocessor.h:913
void setPredefines(const char *P)
Set the predefines for this Preprocessor.
float __ovld __cnfn normalize(float p)
Returns a vector in the same direction as p but with a length of 1.
static bool compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input, StringRef OriginalModuleMapFile, StringRef ModuleFileName, llvm::function_ref< void(CompilerInstance &)> PreBuildStep=[](CompilerInstance &) {}, llvm::function_ref< void(CompilerInstance &)> PostBuildStep=[](CompilerInstance &) {})
Compile a module file for the given module, using the options provided by the importing compiler inst...
The client can handle an AST file that cannot load because it is missing.
Definition: ASTReader.h:1509
void setExternalSemaSource(IntrusiveRefCntPtr< ExternalSemaSource > ESS)
One of these records is kept for each identifier that is lexed.
void setMainFileID(FileID FID)
Set the file ID for the main source file.
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:99
static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, DiagnosticsEngine &Diags, StringRef OutputFile)
void loadModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, StringRef Source) override
Attempt to load the given module from the specified source buffer.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input)
Prepare the action for processing the input file Input.
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
static llvm::Error writeIndex(FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, llvm::StringRef Path)
Write a global index into the given.
const llvm::MemoryBuffer * getBuffer() const
static void collectHeaderMaps(const HeaderSearch &HS, std::shared_ptr< ModuleDependencyCollector > MDC)
ShowIncludesDestination ShowIncludesDest
Destination of cl.exe style /showIncludes info.
unsigned getNumErrors() const
Definition: Diagnostic.h:1505
Definition: Format.h:2274
A source location that has been parsed on the command line.
llvm::SmallPtrSet< ModuleFile *, 4 > HitSet
A set of module files in which we found a result.
void setASTContext(ASTContext *Value)
setASTContext - Replace the current AST context.
CodeGenOptions & getCodeGenOpts()
__DEVICE__ int max(int __a, int __b)
The client can handle an AST file that cannot load because it&#39;s compiled configuration doesn&#39;t match ...
Definition: ASTReader.h:1522
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
unsigned BuildingImplicitModule
Whether we are performing an implicit module build.
Describes a module or submodule.
Definition: Module.h:64
Languages that the frontend can parse and compile.
void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc) override
Make the given module visible.
bool SetCodeCompletionPoint(const FileEntry *File, unsigned Line, unsigned Column)
Specify the point at which code-completion will be performed.
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition: Module.h:467
bool isInvalid() const
Module * findModule(StringRef Name) const
Retrieve a module with the given name.
Definition: ModuleMap.cpp:764
std::error_code make_error_code(BuildPreambleError Error)
void setBuildGlobalModuleIndex(bool Build)
Set the flag indicating whether we should (re)build the global module index.
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition: Module.cpp:213
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:908
FrontendOptions & getFrontendOpts()
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition: Visibility.h:33
std::vector< std::string > VFSOverlayFiles
The set of user-provided virtual filesystem overlay files.
PreprocessorOutputOptions & getPreprocessorOutputOpts()
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
ASTConsumer & getASTConsumer() const
Defines the Diagnostic-related interfaces.
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
void setASTConsumer(std::unique_ptr< ASTConsumer > Value)
setASTConsumer - Replace the current AST consumer; the compiler instance takes ownership of Value...
unsigned IsFromModuleFile
Whether this module was loaded from a module file.
Definition: Module.h:222
submodule_iterator submodule_end()
Definition: Module.h:565
InputKind getKind() const
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
unsigned DisableModuleHash
Whether we should disable the use of the hash string within the module cache.
HeaderSearchOptions & getHeaderSearchOpts()
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro, Module *Mod, SourceLocation ImportLoc)
Diagnose differences between the current definition of the given configuration macro and the definiti...
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
The AST file itself appears corrupted.
Definition: ASTReader.h:386
bool DisablePCHValidation
When true, disables most of the normal validation performed on precompiled headers.
void createPCHExternalASTSource(StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors, void *DeserializationListener, bool OwnDeserializationListener)
Create an external AST source to read a PCH file and attach it to the AST context.
std::vector< std::pair< std::string, llvm::MemoryBuffer * > > RemappedFileBuffers
The set of file-to-buffer remappings, which take existing files on the system (the first part of each...
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:158
const FileEntry * getContainingModuleMapFile(const Module *Module) const
Retrieve the module map file containing the definition of the given module.
Definition: ModuleMap.cpp:1215
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers)...
Definition: Module.h:232
NodeId Parent
Definition: ASTDiff.cpp:191
bool DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:328
static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, const CodeGenOptions *CodeGenOpts, DiagnosticsEngine &Diags)
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:227
const DirectoryEntry * getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
Module * findSubmodule(StringRef Name) const
Find the submodule with the given name.
Definition: Module.cpp:316
Describes the result of attempting to load a module.
Definition: ModuleLoader.h:35
StringRef Filename
Definition: Format.cpp:1712
bool isValid() const
bool lookupIdentifier(llvm::StringRef Name, HitSet &Hits)
Look for all of the module files with information about the given identifier, e.g., a global function, variable, or type with that name.
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
Exposes information about the current target.
Definition: TargetInfo.h:161
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
A simple code-completion consumer that prints the results it receives in a simple format...
File is from a prebuilt module path.
Definition: Module.h:59
unsigned ModuleCachePruneInterval
The interval (in seconds) between pruning operations.
std::string getSpecificModuleCachePath()
int Id
Definition: ASTDiff.cpp:190
const AnnotatedLine * Line
RAII object to temporarily add an AST callback listener.
Definition: ASTReader.h:1587
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, unsigned LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
StringRef getFile() const
Defines version macros and version-related utility functions for Clang.
void print(raw_ostream &OS, unsigned Indent=0) const
Print the module map for this module to the given stream.
Definition: Module.cpp:419
Defines the clang::Preprocessor interface.
ASTReaderListenter implementation to set SuggestedPredefines of ASTReader which is required to use a ...
Definition: ASTReader.h:317
void createASTContext()
Create the AST context.
unsigned ShowHeaderIncludes
Show header inclusions (-H).
LLVM_READONLY bool isAlphanumeric(unsigned char c)
Return true if this character is an ASCII letter or digit: [a-zA-Z0-9].
Definition: CharInfo.h:117
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: Transformer.h:232
static bool checkModuleIsAvailable(const LangOptions &LangOpts, const TargetInfo &TargetInfo, DiagnosticsEngine &Diags, Module *M)
Check that the given module is available, producing a diagnostic if not.
HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized. ...
Definition: HeaderSearch.h:268
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
void AttachHeaderIncludeGen(Preprocessor &PP, const DependencyOutputOptions &DepOpts, bool ShowAllHeaders=false, StringRef OutputPath={}, bool ShowDepth=true, bool MSStyle=false)
AttachHeaderIncludeGen - Create a header include list generator, and attach it to the given preproces...
static InputKind::Language getLanguageFromOptions(const LangOptions &LangOpts)
Determine the appropriate source input kind based on language options.
module_iterator module_begin() const
Definition: ModuleMap.h:685
TargetInfo * getAuxTarget() const
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
Definition: Targets.cpp:602
An input file for the front end.
The result type of a method or function.
void addOutputFile(OutputFile &&OutFile)
addOutputFile - Add an output file onto the list of tracked output files.
static bool EnableCodeCompletion(Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column)
The client can&#39;t handle any AST loading failures.
Definition: ASTReader.h:1505
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, StringRef BaseInput, StringRef Extension, bool UseTemporary, bool CreateMissingDirectories=false)
Create a new output file and add it to the list of tracked output files, optionally deriving the outp...
The AST file was missing.
Definition: ASTReader.h:389
bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Check global module index for missing imports.
std::unique_ptr< Sema > takeSema()
In-memory cache for modules.
std::string CPU
If given, the name of the target CPU to generate code for.
Definition: TargetOptions.h:36
SourceManager & getSourceManager() const
Definition: Preprocessor.h:907
const DirectoryEntry * Directory
The build directory of this module.
Definition: Module.h:97
void overrideFileContents(const FileEntry *SourceFile, llvm::MemoryBuffer *Buffer, bool DoNotFree)
Override the contents of the given source file by providing an already-allocated buffer.
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition: FileManager.h:92
FileSystemOptions & getFileSystemOpts()
The control block was read successfully.
Definition: ASTReader.h:383
Kind
void ApplyHeaderSearchOptions(HeaderSearch &HS, const HeaderSearchOptions &HSOpts, const LangOptions &Lang, const llvm::Triple &triple)
Apply the header search options to get given HeaderSearch object.
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler&#39;s CompilerInvocation object...
File is a PCH file treated as the preamble.
Definition: Module.h:53
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
StringRef getName() const
Definition: FileManager.h:83
File is a PCH file treated as such.
Definition: Module.h:50
ParsedSourceLocation CodeCompletionAt
If given, enable code completion at the provided location.
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
bool isNamedPipe() const
Check whether the file is a named pipe (and thus can&#39;t be opened by the native FileManager methods)...
Definition: FileManager.h:98
MacroDirective * getLocalMacroDirectiveHistory(const IdentifierInfo *II) const
Given an identifier, return the latest non-imported macro directive for that identifier.
unsigned getNumWarnings() const
Definition: Diagnostic.h:1506
Options for controlling the compiler diagnostics engine.
std::vector< FrontendInputFile > Inputs
The input files and their types.
virtual void finish()
Callback to inform the diagnostic client that processing of all source files has ended.
Definition: Diagnostic.h:1532
All of the names in this module are hidden.
Definition: Module.h:275
File is an implicitly-loaded module.
Definition: Module.h:44
unsigned ModulesValidateSystemHeaders
Whether to validate system input files when a module is loaded.
IdentifierTable & getIdentifierTable()
Definition: Preprocessor.h:910
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...
bool PrepareToExecute(CompilerInstance &CI)
Prepare the action to execute on the given compiler instance.
std::string DiagnosticSerializationFile
The file to serialize diagnostics to (non-appending).
The kind of a file that we&#39;ve been handed as an input.
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1563
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition: Warnings.cpp:43
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
void PrintStats() const
std::string ModuleName
The module currently being compiled as specified by -fmodule-name.
Definition: LangOptions.h:220
const FileEntry * getVirtualFile(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:23
void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc)
Push an entry to the module build stack.
unsigned GenerateGlobalModuleIndex
Whether we can generate the global module index if needed.
void createPreprocessor(TranslationUnitKind TUKind)
Create the preprocessor, using the invocation, file, and source managers, and replace any existing on...
A global index for a set of module files, providing information about the identifiers within those mo...
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
module_iterator module_end() const
Definition: ModuleMap.h:686
The AST file was writtten with a different language/target configuration.
Definition: ASTReader.h:400
std::string OutputFile
The file to write dependency output to.
Abstract interface for a consumer of code-completion information.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Options controlling the behavior of code completion.
void getHeaderMapFileNames(SmallVectorImpl< std::string > &Names) const
Get filenames for all registered header maps.
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
Dataflow Directional Tag Classes.
Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc) const
Based on the way the client configured the DiagnosticsEngine object, classify the specified diagnosti...
Definition: Diagnostic.h:836
bool isValid() const
Return true if this is a valid SourceLocation object.
void setDeserializationListener(ASTDeserializationListener *Listener, bool TakeOwnership=false)
Set the AST deserialization listener.
Definition: ASTReader.cpp:808
unsigned IsMissingRequirement
Whether this module is missing a feature from Requirements.
Definition: Module.h:210
void setPreprocessor(std::shared_ptr< Preprocessor > Value)
Replace the current preprocessor.
FileManager & getFileManager() const
Return the current file manager to the caller.
PreprocessorOptions & getPreprocessorOpts()
static void InitializeFileRemapping(DiagnosticsEngine &Diags, SourceManager &SourceMgr, FileManager &FileMgr, const PreprocessorOptions &InitOpts)
llvm::Error Execute()
Set the source manager&#39;s main input file, and run the action.
off_t getSize() const
Definition: FileManager.h:86
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:354
File is an explicitly-loaded module.
Definition: Module.h:47
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
FileID getMainFileID() const
Returns the FileID of the main source file.
bool loadModuleFile(StringRef FileName)
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false, bool ShouldCloseOpenFile=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
unsigned UseGlobalModuleIndex
Whether we can use the global module index if available.
Used for handling and querying diagnostic IDs.
void setASTMutationListener(ASTMutationListener *Listener)
Attach an AST mutation listener to the AST context.
Definition: ASTContext.h:1090
Helper class for holding the data necessary to invoke the compiler.
static const FileEntry * getPublicModuleMap(const FileEntry *File, FileManager &FileMgr)
static void pruneModuleCache(const HeaderSearchOptions &HSOpts)
Prune the module cache of modules that haven&#39;t been accessed in a long time.
SourceManager & getSourceManager() const
Return the current source manager.
Language
The language for the input, used to select and validate the language standard and possible actions...
static void collectIncludePCH(CompilerInstance &CI, std::shared_ptr< ModuleDependencyCollector > MDC)
FrontendOptions - Options for controlling the behavior of the frontend.
Abstract interface for a module loader.
Definition: ModuleLoader.h:73
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
std::string StatsFile
Filename to write statistics to.
SourceLocation DefinitionLoc
The location of the module definition.
Definition: Module.h:70
void clearOutputFiles(bool EraseFiles)
clearOutputFiles - Clear the output file list.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:126
TargetInfo & getTarget() const
void setOverridenFilesKeepOriginalName(bool value)
Set true if the SourceManager should report the original file name for contents of files that were ov...
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
Create the default output file (from the invocation&#39;s options) and add it to the list of tracked outp...
void setTarget(TargetInfo *Value)
Replace the current Target.
Cached information about one directory (either on disk or in the virtual file system).
Definition: FileManager.h:45
unsigned DisableFree
Disable memory freeing on exit.
virtual void Initialize(ASTContext &Context)
Initialize - This is called to initialize the consumer, providing the ASTContext. ...
Definition: ASTConsumer.h:47
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
DiagnosticConsumer & getDiagnosticClient() const
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
Definition: Preprocessor.h:987
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:900
unsigned ModuleCachePruneAfter
The time (in seconds) after which an unused module file will be considered unused and will...
void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts, const PCHContainerReader &PCHContainerRdr, const FrontendOptions &FEOpts)
InitializePreprocessor - Initialize the preprocessor getting it and the environment ready to process ...
void setModuleBuildStack(ModuleBuildStack stack)
Set the module build stack.
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:362
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string...
Definition: Diagnostic.h:129
std::vector< std::pair< std::string, std::string > > RemappedFiles
The set of file remappings, which take existing files on the system (the first part of each pair) and...
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
void resolveLinkAsDependencies(Module *Mod)
Use PendingLinkAsModule information to mark top level link names that are going to be replaced by exp...
Definition: ModuleMap.cpp:58
unsigned HasIncompatibleModuleFile
Whether we tried and failed to load a module file for this module.
Definition: Module.h:213
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
Defines the clang::TargetInfo interface.
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition: ASTReader.h:393
A SourceLocation and its associated SourceManager.
NameVisibilityKind
Describes the visibility of the various names within a particular module.
Definition: Module.h:273
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
std::vector< std::string > ConfigMacros
The set of "configuration macros", which are macros that (intentionally) change how this module is bu...
Definition: Module.h:350
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
The AST file was written by a different version of Clang.
Definition: ASTReader.h:396
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
virtual void setAuxTarget(const TargetInfo *Aux)
Definition: TargetInfo.h:1372
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
static void collectVFSEntries(CompilerInstance &CI, std::shared_ptr< ModuleDependencyCollector > MDC)
bool isIgnored(unsigned DiagID, SourceLocation Loc) const
Determine whether the diagnostic is known to be ignored.
Definition: Diagnostic.h:821
Defines utilities for dealing with stack allocation and stack space.
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions, ASTReaderListener &Listener, bool ValidateDiagnosticOptions)
Read the control block for the named AST file.
Definition: ASTReader.cpp:5030
GlobalModuleIndex * loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Load, create, or return global module.
std::string Sysroot
If non-empty, the directory to use as a "virtual system root" for include paths.
A trivial tuple used to represent a source range.
std::map< std::string, std::string > PrebuiltModuleFiles
The mapping of module names to prebuilt module files.
TargetOptions & getTargetOpts()
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
llvm::vfs::FileSystem & getVirtualFileSystem() const
Definition: FileManager.h:224
This class handles loading and caching of source files into memory.
void setDiagnostics(DiagnosticsEngine *Value)
setDiagnostics - Replace the current diagnostics engine.
void noSignedCharForObjCBool()
Definition: TargetInfo.h:686
std::string getModuleHash() const
Retrieve a module hash string that is suitable for uniquely identifying the conditions under which th...
std::string DiagnosticLogFile
The file to log diagnostic output to.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:124
bool ownsClient() const
Determine whether this DiagnosticsEngine object own its client.
Definition: Diagnostic.h:514
StringRef getName() const
Definition: FileManager.h:51
virtual bool isModelParsingAction() const
Is this action invoked on a model file?