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