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