clang  3.9.0
CompilerInstance.cpp
Go to the documentation of this file.
1 //===--- CompilerInstance.cpp ---------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Basic/Version.h"
19 #include "clang/Config/config.h"
27 #include "clang/Frontend/Utils.h"
29 #include "clang/Lex/HeaderSearch.h"
30 #include "clang/Lex/PTHManager.h"
31 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Sema/Sema.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/Support/CrashRecoveryContext.h"
38 #include "llvm/Support/Errc.h"
39 #include "llvm/Support/FileSystem.h"
40 #include "llvm/Support/Host.h"
41 #include "llvm/Support/LockFileManager.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/Program.h"
45 #include "llvm/Support/Signals.h"
46 #include "llvm/Support/Timer.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include <sys/stat.h>
49 #include <system_error>
50 #include <time.h>
51 #include <utility>
52 
53 using namespace clang;
54 
55 CompilerInstance::CompilerInstance(
56  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
57  bool BuildingModule)
58  : ModuleLoader(BuildingModule), Invocation(new CompilerInvocation()),
59  ModuleManager(nullptr),
60  ThePCHContainerOperations(std::move(PCHContainerOps)),
61  BuildGlobalModuleIndex(false), HaveFullGlobalModuleIndex(false),
62  ModuleBuildFailed(false) {}
63 
65  assert(OutputFiles.empty() && "Still output files in flight?");
66 }
67 
69  Invocation = Value;
70 }
71 
73  return (BuildGlobalModuleIndex ||
74  (ModuleManager && ModuleManager->isGlobalIndexUnavailable() &&
76  !ModuleBuildFailed;
77 }
78 
80  Diagnostics = Value;
81 }
82 
85 
87  FileMgr = Value;
88  if (Value)
89  VirtualFileSystem = Value->getVirtualFileSystem();
90  else
91  VirtualFileSystem.reset();
92 }
93 
95  SourceMgr = Value;
96 }
97 
99 
101  Context = Value;
102 
103  if (Context && Consumer)
105 }
106 
108  TheSema.reset(S);
109 }
110 
111 void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
112  Consumer = std::move(Value);
113 
114  if (Context && Consumer)
116 }
117 
119  CompletionConsumer.reset(Value);
120 }
121 
122 std::unique_ptr<Sema> CompilerInstance::takeSema() {
123  return std::move(TheSema);
124 }
125 
127  return ModuleManager;
128 }
130  ModuleManager = std::move(Reader);
131 }
132 
133 std::shared_ptr<ModuleDependencyCollector>
135  return ModuleDepCollector;
136 }
137 
139  std::shared_ptr<ModuleDependencyCollector> Collector) {
140  ModuleDepCollector = std::move(Collector);
141 }
142 
143 // Diagnostics
144 static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
145  const CodeGenOptions *CodeGenOpts,
146  DiagnosticsEngine &Diags) {
147  std::error_code EC;
148  std::unique_ptr<raw_ostream> StreamOwner;
149  raw_ostream *OS = &llvm::errs();
150  if (DiagOpts->DiagnosticLogFile != "-") {
151  // Create the output stream.
152  auto FileOS = llvm::make_unique<llvm::raw_fd_ostream>(
153  DiagOpts->DiagnosticLogFile, EC,
154  llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
155  if (EC) {
156  Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
157  << DiagOpts->DiagnosticLogFile << EC.message();
158  } else {
159  FileOS->SetUnbuffered();
160  OS = FileOS.get();
161  StreamOwner = std::move(FileOS);
162  }
163  }
164 
165  // Chain in the diagnostic client which will log the diagnostics.
166  auto Logger = llvm::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
167  std::move(StreamOwner));
168  if (CodeGenOpts)
169  Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
170  assert(Diags.ownsClient());
171  Diags.setClient(
172  new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
173 }
174 
176  DiagnosticsEngine &Diags,
177  StringRef OutputFile) {
178  auto SerializedConsumer =
179  clang::serialized_diags::create(OutputFile, DiagOpts);
180 
181  if (Diags.ownsClient()) {
183  Diags.takeClient(), std::move(SerializedConsumer)));
184  } else {
186  Diags.getClient(), std::move(SerializedConsumer)));
187  }
188 }
189 
191  bool ShouldOwnClient) {
192  Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
193  ShouldOwnClient, &getCodeGenOpts());
194 }
195 
198  DiagnosticConsumer *Client,
199  bool ShouldOwnClient,
200  const CodeGenOptions *CodeGenOpts) {
203  Diags(new DiagnosticsEngine(DiagID, Opts));
204 
205  // Create the diagnostic client for reporting errors or for
206  // implementing -verify.
207  if (Client) {
208  Diags->setClient(Client, ShouldOwnClient);
209  } else
210  Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
211 
212  // Chain in -verify checker, if requested.
213  if (Opts->VerifyDiagnostics)
214  Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
215 
216  // Chain in -diagnostic-log-file dumper, if requested.
217  if (!Opts->DiagnosticLogFile.empty())
218  SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
219 
220  if (!Opts->DiagnosticSerializationFile.empty())
221  SetupSerializedDiagnostics(Opts, *Diags,
223 
224  // Configure our handling of diagnostics.
225  ProcessWarningOptions(*Diags, *Opts);
226 
227  return Diags;
228 }
229 
230 // File Manager
231 
233  if (!hasVirtualFileSystem()) {
234  // TODO: choose the virtual file system based on the CompilerInvocation.
236  }
237  FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem);
238 }
239 
240 // Source Manager
241 
243  SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
244 }
245 
246 // Initialize the remapping of files to alternative contents, e.g.,
247 // those specified through other files.
249  SourceManager &SourceMgr,
250  FileManager &FileMgr,
251  const PreprocessorOptions &InitOpts) {
252  // Remap files in the source manager (with buffers).
253  for (const auto &RB : InitOpts.RemappedFileBuffers) {
254  // Create the file entry for the file that we're mapping from.
255  const FileEntry *FromFile =
256  FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0);
257  if (!FromFile) {
258  Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first;
259  if (!InitOpts.RetainRemappedFileBuffers)
260  delete RB.second;
261  continue;
262  }
263 
264  // Override the contents of the "from" file with the contents of
265  // the "to" file.
266  SourceMgr.overrideFileContents(FromFile, RB.second,
267  InitOpts.RetainRemappedFileBuffers);
268  }
269 
270  // Remap files in the source manager (with other files).
271  for (const auto &RF : InitOpts.RemappedFiles) {
272  // Find the file that we're mapping to.
273  const FileEntry *ToFile = FileMgr.getFile(RF.second);
274  if (!ToFile) {
275  Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
276  continue;
277  }
278 
279  // Create the file entry for the file that we're mapping from.
280  const FileEntry *FromFile =
281  FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0);
282  if (!FromFile) {
283  Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first;
284  continue;
285  }
286 
287  // Override the contents of the "from" file with the contents of
288  // the "to" file.
289  SourceMgr.overrideFileContents(FromFile, ToFile);
290  }
291 
294 }
295 
296 // Preprocessor
297 
299  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
300 
301  // Create a PTH manager if we are using some form of a token cache.
302  PTHManager *PTHMgr = nullptr;
303  if (!PPOpts.TokenCache.empty())
304  PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
305 
306  // Create the Preprocessor.
307  HeaderSearch *HeaderInfo = new HeaderSearch(&getHeaderSearchOpts(),
309  getDiagnostics(),
310  getLangOpts(),
311  &getTarget());
313  getSourceManager(), *HeaderInfo, *this, PTHMgr,
314  /*OwnsHeaderSearch=*/true, TUKind);
315  PP->Initialize(getTarget(), getAuxTarget());
316 
317  // Note that this is different then passing PTHMgr to Preprocessor's ctor.
318  // That argument is used as the IdentifierInfoLookup argument to
319  // IdentifierTable's ctor.
320  if (PTHMgr) {
321  PTHMgr->setPreprocessor(&*PP);
322  PP->setPTHManager(PTHMgr);
323  }
324 
325  if (PPOpts.DetailedRecord)
326  PP->createPreprocessingRecord();
327 
328  // Apply remappings to the source manager.
329  InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
330  PP->getFileManager(), PPOpts);
331 
332  // Predefine macros and configure the preprocessor.
334  getFrontendOpts());
335 
336  // Initialize the header search object.
337  ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
338  PP->getLangOpts(), PP->getTargetInfo().getTriple());
339 
340  PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
341 
342  if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules)
343  PP->getHeaderSearchInfo().setModuleCachePath(getSpecificModuleCachePath());
344 
345  // Handle generating dependencies, if requested.
347  if (!DepOpts.OutputFile.empty())
348  TheDependencyFileGenerator.reset(
350  if (!DepOpts.DOTOutputFile.empty())
353 
354  // If we don't have a collector, but we are collecting module dependencies,
355  // then we're the top level compiler instance and need to create one.
356  if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
357  ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
358  DepOpts.ModuleDependencyOutputDir);
359  }
360 
361  if (ModuleDepCollector)
362  addDependencyCollector(ModuleDepCollector);
363 
364  for (auto &Listener : DependencyCollectors)
365  Listener->attachToPreprocessor(*PP);
366 
367  // Handle generating header include information, if requested.
368  if (DepOpts.ShowHeaderIncludes)
369  AttachHeaderIncludeGen(*PP, DepOpts);
370  if (!DepOpts.HeaderIncludeOutputFile.empty()) {
371  StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
372  if (OutputPath == "-")
373  OutputPath = "";
374  AttachHeaderIncludeGen(*PP, DepOpts,
375  /*ShowAllHeaders=*/true, OutputPath,
376  /*ShowDepth=*/false);
377  }
378 
379  if (DepOpts.PrintShowIncludes) {
380  AttachHeaderIncludeGen(*PP, DepOpts,
381  /*ShowAllHeaders=*/true, /*OutputPath=*/"",
382  /*ShowDepth=*/true, /*MSStyle=*/true);
383  }
384 }
385 
387  // Set up the module path, including the hash for the
388  // module-creation options.
389  SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath);
390  if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
391  llvm::sys::path::append(SpecificModuleCache,
393  return SpecificModuleCache.str();
394 }
395 
396 // ASTContext
397 
400  auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
402  PP.getBuiltinInfo());
405 }
406 
407 // ExternalASTSource
408 
410  StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors,
411  void *DeserializationListener, bool OwnDeserializationListener) {
412  bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
413  ModuleManager = createPCHExternalASTSource(
414  Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation,
415  AllowPCHWithCompilerErrors, getPreprocessor(), getASTContext(),
417  getFrontendOpts().ModuleFileExtensions,
418  DeserializationListener,
419  OwnDeserializationListener, Preamble,
420  getFrontendOpts().UseGlobalModuleIndex);
421 }
422 
424  StringRef Path, StringRef Sysroot, bool DisablePCHValidation,
425  bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context,
426  const PCHContainerReader &PCHContainerRdr,
428  void *DeserializationListener, bool OwnDeserializationListener,
429  bool Preamble, bool UseGlobalModuleIndex) {
431 
433  PP, Context, PCHContainerRdr, Extensions,
434  Sysroot.empty() ? "" : Sysroot.data(), DisablePCHValidation,
435  AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,
436  HSOpts.ModulesValidateSystemHeaders, UseGlobalModuleIndex));
437 
438  // We need the external source to be set up before we read the AST, because
439  // eagerly-deserialized declarations may use it.
440  Context.setExternalSource(Reader.get());
441 
442  Reader->setDeserializationListener(
443  static_cast<ASTDeserializationListener *>(DeserializationListener),
444  /*TakeOwnership=*/OwnDeserializationListener);
445  switch (Reader->ReadAST(Path,
446  Preamble ? serialization::MK_Preamble
448  SourceLocation(),
450  case ASTReader::Success:
451  // Set the predefines buffer as suggested by the PCH reader. Typically, the
452  // predefines buffer will be empty.
453  PP.setPredefines(Reader->getSuggestedPredefines());
454  return Reader;
455 
456  case ASTReader::Failure:
457  // Unrecoverable failure: don't even try to process the input file.
458  break;
459 
460  case ASTReader::Missing:
465  // No suitable PCH file could be found. Return an error.
466  break;
467  }
468 
469  Context.setExternalSource(nullptr);
470  return nullptr;
471 }
472 
473 // Code Completion
474 
476  StringRef Filename,
477  unsigned Line,
478  unsigned Column) {
479  // Tell the source manager to chop off the given file at a specific
480  // line and column.
481  const FileEntry *Entry = PP.getFileManager().getFile(Filename);
482  if (!Entry) {
483  PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
484  << Filename;
485  return true;
486  }
487 
488  // Truncate the named file at the given line/column.
489  PP.SetCodeCompletionPoint(Entry, Line, Column);
490  return false;
491 }
492 
495  if (!CompletionConsumer) {
498  Loc.FileName, Loc.Line, Loc.Column,
499  getFrontendOpts().CodeCompleteOpts,
500  llvm::outs()));
501  if (!CompletionConsumer)
502  return;
504  Loc.Line, Loc.Column)) {
505  setCodeCompletionConsumer(nullptr);
506  return;
507  }
508 
509  if (CompletionConsumer->isOutputBinary() &&
510  llvm::sys::ChangeStdoutToBinary()) {
511  getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
512  setCodeCompletionConsumer(nullptr);
513  }
514 }
515 
517  FrontendTimerGroup.reset(new llvm::TimerGroup("Clang front-end time report"));
518  FrontendTimer.reset(
519  new llvm::Timer("Clang front-end timer", *FrontendTimerGroup));
520 }
521 
524  StringRef Filename,
525  unsigned Line,
526  unsigned Column,
527  const CodeCompleteOptions &Opts,
528  raw_ostream &OS) {
529  if (EnableCodeCompletion(PP, Filename, Line, Column))
530  return nullptr;
531 
532  // Set up the creation routine for code-completion.
533  return new PrintingCodeCompleteConsumer(Opts, OS);
534 }
535 
537  CodeCompleteConsumer *CompletionConsumer) {
538  TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
539  TUKind, CompletionConsumer));
540 }
541 
542 // Output Files
543 
544 void CompilerInstance::addOutputFile(OutputFile &&OutFile) {
545  OutputFiles.push_back(std::move(OutFile));
546 }
547 
548 void CompilerInstance::clearOutputFiles(bool EraseFiles) {
549  for (OutputFile &OF : OutputFiles) {
550  if (!OF.TempFilename.empty()) {
551  if (EraseFiles) {
552  llvm::sys::fs::remove(OF.TempFilename);
553  } else {
554  SmallString<128> NewOutFile(OF.Filename);
555 
556  // If '-working-directory' was passed, the output filename should be
557  // relative to that.
558  FileMgr->FixupRelativePath(NewOutFile);
559  if (std::error_code ec =
560  llvm::sys::fs::rename(OF.TempFilename, NewOutFile)) {
561  getDiagnostics().Report(diag::err_unable_to_rename_temp)
562  << OF.TempFilename << OF.Filename << ec.message();
563 
564  llvm::sys::fs::remove(OF.TempFilename);
565  }
566  }
567  } else if (!OF.Filename.empty() && EraseFiles)
568  llvm::sys::fs::remove(OF.Filename);
569  }
570  OutputFiles.clear();
571  NonSeekStream.reset();
572 }
573 
574 std::unique_ptr<raw_pwrite_stream>
575 CompilerInstance::createDefaultOutputFile(bool Binary, StringRef InFile,
576  StringRef Extension) {
577  return createOutputFile(getFrontendOpts().OutputFile, Binary,
578  /*RemoveFileOnSignal=*/true, InFile, Extension,
579  /*UseTemporary=*/true);
580 }
581 
582 std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
583  return llvm::make_unique<llvm::raw_null_ostream>();
584 }
585 
586 std::unique_ptr<raw_pwrite_stream>
587 CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary,
588  bool RemoveFileOnSignal, StringRef InFile,
589  StringRef Extension, bool UseTemporary,
590  bool CreateMissingDirectories) {
591  std::string OutputPathName, TempPathName;
592  std::error_code EC;
593  std::unique_ptr<raw_pwrite_stream> OS = createOutputFile(
594  OutputPath, EC, Binary, RemoveFileOnSignal, InFile, Extension,
595  UseTemporary, CreateMissingDirectories, &OutputPathName, &TempPathName);
596  if (!OS) {
597  getDiagnostics().Report(diag::err_fe_unable_to_open_output) << OutputPath
598  << EC.message();
599  return nullptr;
600  }
601 
602  // Add the output file -- but don't try to remove "-", since this means we are
603  // using stdin.
605  OutputFile((OutputPathName != "-") ? OutputPathName : "", TempPathName));
606 
607  return OS;
608 }
609 
610 std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile(
611  StringRef OutputPath, std::error_code &Error, bool Binary,
612  bool RemoveFileOnSignal, StringRef InFile, StringRef Extension,
613  bool UseTemporary, bool CreateMissingDirectories,
614  std::string *ResultPathName, std::string *TempPathName) {
615  assert((!CreateMissingDirectories || UseTemporary) &&
616  "CreateMissingDirectories is only allowed when using temporary files");
617 
618  std::string OutFile, TempFile;
619  if (!OutputPath.empty()) {
620  OutFile = OutputPath;
621  } else if (InFile == "-") {
622  OutFile = "-";
623  } else if (!Extension.empty()) {
624  SmallString<128> Path(InFile);
625  llvm::sys::path::replace_extension(Path, Extension);
626  OutFile = Path.str();
627  } else {
628  OutFile = "-";
629  }
630 
631  std::unique_ptr<llvm::raw_fd_ostream> OS;
632  std::string OSFile;
633 
634  if (UseTemporary) {
635  if (OutFile == "-")
636  UseTemporary = false;
637  else {
638  llvm::sys::fs::file_status Status;
639  llvm::sys::fs::status(OutputPath, Status);
640  if (llvm::sys::fs::exists(Status)) {
641  // Fail early if we can't write to the final destination.
642  if (!llvm::sys::fs::can_write(OutputPath)) {
643  Error = make_error_code(llvm::errc::operation_not_permitted);
644  return nullptr;
645  }
646 
647  // Don't use a temporary if the output is a special file. This handles
648  // things like '-o /dev/null'
649  if (!llvm::sys::fs::is_regular_file(Status))
650  UseTemporary = false;
651  }
652  }
653  }
654 
655  if (UseTemporary) {
656  // Create a temporary file.
657  SmallString<128> TempPath;
658  TempPath = OutFile;
659  TempPath += "-%%%%%%%%";
660  int fd;
661  std::error_code EC =
662  llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
663 
664  if (CreateMissingDirectories &&
665  EC == llvm::errc::no_such_file_or_directory) {
666  StringRef Parent = llvm::sys::path::parent_path(OutputPath);
667  EC = llvm::sys::fs::create_directories(Parent);
668  if (!EC) {
669  EC = llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
670  }
671  }
672 
673  if (!EC) {
674  OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
675  OSFile = TempFile = TempPath.str();
676  }
677  // If we failed to create the temporary, fallback to writing to the file
678  // directly. This handles the corner case where we cannot write to the
679  // directory, but can write to the file.
680  }
681 
682  if (!OS) {
683  OSFile = OutFile;
684  OS.reset(new llvm::raw_fd_ostream(
685  OSFile, Error,
686  (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text)));
687  if (Error)
688  return nullptr;
689  }
690 
691  // Make sure the out stream file gets removed if we crash.
692  if (RemoveFileOnSignal)
693  llvm::sys::RemoveFileOnSignal(OSFile);
694 
695  if (ResultPathName)
696  *ResultPathName = OutFile;
697  if (TempPathName)
698  *TempPathName = TempFile;
699 
700  if (!Binary || OS->supportsSeeking())
701  return std::move(OS);
702 
703  auto B = llvm::make_unique<llvm::buffer_ostream>(*OS);
704  assert(!NonSeekStream);
705  NonSeekStream = std::move(OS);
706  return std::move(B);
707 }
708 
709 // Initialization Utilities
710 
714  hasPreprocessor() ? &getPreprocessor().getHeaderSearchInfo() : nullptr,
716 }
717 
718 // static
721  FileManager &FileMgr, SourceManager &SourceMgr, HeaderSearch *HS,
722  DependencyOutputOptions &DepOpts, const FrontendOptions &Opts) {
725 
726  if (Input.isBuffer()) {
727  SourceMgr.setMainFileID(SourceMgr.createFileID(
728  std::unique_ptr<llvm::MemoryBuffer>(Input.getBuffer()), Kind));
729  assert(SourceMgr.getMainFileID().isValid() &&
730  "Couldn't establish MainFileID!");
731  return true;
732  }
733 
734  StringRef InputFile = Input.getFile();
735 
736  // Figure out where to get and map in the main file.
737  if (InputFile != "-") {
738  const FileEntry *File;
739  if (Opts.FindPchSource.empty()) {
740  File = FileMgr.getFile(InputFile, /*OpenFile=*/true);
741  } else {
742  // When building a pch file in clang-cl mode, the .h file is built as if
743  // it was included by a cc file. Since the driver doesn't know about
744  // all include search directories, the frontend must search the input
745  // file through HeaderSearch here, as if it had been included by the
746  // cc file at Opts.FindPchSource.
747  const FileEntry *FindFile = FileMgr.getFile(Opts.FindPchSource);
748  if (!FindFile) {
749  Diags.Report(diag::err_fe_error_reading) << Opts.FindPchSource;
750  return false;
751  }
752  const DirectoryLookup *UnusedCurDir;
754  Includers;
755  Includers.push_back(std::make_pair(FindFile, FindFile->getDir()));
756  File = HS->LookupFile(InputFile, SourceLocation(), /*isAngled=*/false,
757  /*FromDir=*/nullptr,
758  /*CurDir=*/UnusedCurDir, Includers,
759  /*SearchPath=*/nullptr,
760  /*RelativePath=*/nullptr,
761  /*RequestingModule=*/nullptr,
762  /*SuggestedModule=*/nullptr, /*SkipCache=*/true);
763  // Also add the header to /showIncludes output.
764  if (File)
765  DepOpts.ShowIncludesPretendHeader = File->getName();
766  }
767  if (!File) {
768  Diags.Report(diag::err_fe_error_reading) << InputFile;
769  return false;
770  }
771 
772  // The natural SourceManager infrastructure can't currently handle named
773  // pipes, but we would at least like to accept them for the main
774  // file. Detect them here, read them with the volatile flag so FileMgr will
775  // pick up the correct size, and simply override their contents as we do for
776  // STDIN.
777  if (File->isNamedPipe()) {
778  auto MB = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
779  if (MB) {
780  // Create a new virtual file that will have the correct size.
781  File = FileMgr.getVirtualFile(InputFile, (*MB)->getBufferSize(), 0);
782  SourceMgr.overrideFileContents(File, std::move(*MB));
783  } else {
784  Diags.Report(diag::err_cannot_open_file) << InputFile
785  << MB.getError().message();
786  return false;
787  }
788  }
789 
790  SourceMgr.setMainFileID(
791  SourceMgr.createFileID(File, SourceLocation(), Kind));
792  } else {
793  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> SBOrErr =
794  llvm::MemoryBuffer::getSTDIN();
795  if (std::error_code EC = SBOrErr.getError()) {
796  Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
797  return false;
798  }
799  std::unique_ptr<llvm::MemoryBuffer> SB = std::move(SBOrErr.get());
800 
801  const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
802  SB->getBufferSize(), 0);
803  SourceMgr.setMainFileID(
804  SourceMgr.createFileID(File, SourceLocation(), Kind));
805  SourceMgr.overrideFileContents(File, std::move(SB));
806  }
807 
808  assert(SourceMgr.getMainFileID().isValid() &&
809  "Couldn't establish MainFileID!");
810  return true;
811 }
812 
813 // High-Level Operations
814 
816  assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
817  assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
818  assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
819 
820  // FIXME: Take this as an argument, once all the APIs we used have moved to
821  // taking it as an input instead of hard-coding llvm::errs.
822  raw_ostream &OS = llvm::errs();
823 
824  // Create the target instance.
826  getInvocation().TargetOpts));
827  if (!hasTarget())
828  return false;
829 
830  // Create TargetInfo for the other side of CUDA compilation.
831  if (getLangOpts().CUDA && !getFrontendOpts().AuxTriple.empty()) {
832  auto TO = std::make_shared<TargetOptions>();
833  TO->Triple = getFrontendOpts().AuxTriple;
834  TO->HostTriple = getTarget().getTriple().str();
836  }
837 
838  // Inform the target of the language options.
839  //
840  // FIXME: We shouldn't need to do this, the target should be immutable once
841  // created. This complexity should be lifted elsewhere.
843 
844  // rewriter project will change target built-in bool type from its default.
845  if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
847 
848  // Validate/process some options.
850  OS << "clang -cc1 version " CLANG_VERSION_STRING
851  << " based upon " << BACKEND_PACKAGE_STRING
852  << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
853 
854  if (getFrontendOpts().ShowTimers)
856 
857  if (getFrontendOpts().ShowStats)
858  llvm::EnableStatistics();
859 
860  for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
861  // Reset the ID tables if we are reusing the SourceManager and parsing
862  // regular files.
863  if (hasSourceManager() && !Act.isModelParsingAction())
865 
866  if (Act.BeginSourceFile(*this, FIF)) {
867  Act.Execute();
868  Act.EndSourceFile();
869  }
870  }
871 
872  // Notify the diagnostic client that all files were processed.
874 
875  if (getDiagnosticOpts().ShowCarets) {
876  // We can have multiple diagnostics sharing one diagnostic client.
877  // Get the total number of warnings/errors from the client.
878  unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
879  unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
880 
881  if (NumWarnings)
882  OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
883  if (NumWarnings && NumErrors)
884  OS << " and ";
885  if (NumErrors)
886  OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
887  if (NumWarnings || NumErrors)
888  OS << " generated.\n";
889  }
890 
893  OS << "\n";
894  }
895 
896  return !getDiagnostics().getClient()->getNumErrors();
897 }
898 
899 /// \brief Determine the appropriate source input kind based on language
900 /// options.
902  if (LangOpts.OpenCL)
903  return IK_OpenCL;
904  if (LangOpts.CUDA)
905  return IK_CUDA;
906  if (LangOpts.ObjC1)
907  return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
908  return LangOpts.CPlusPlus? IK_CXX : IK_C;
909 }
910 
911 /// \brief Compile a module file for the given module, using the options
912 /// provided by the importing compiler instance. Returns true if the module
913 /// was built without errors.
914 static bool compileModuleImpl(CompilerInstance &ImportingInstance,
915  SourceLocation ImportLoc,
916  Module *Module,
917  StringRef ModuleFileName) {
918  ModuleMap &ModMap
919  = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
920 
921  // Construct a compiler invocation for creating this module.
923  (new CompilerInvocation(ImportingInstance.getInvocation()));
924 
925  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
926 
927  // For any options that aren't intended to affect how a module is built,
928  // reset them to their default values.
929  Invocation->getLangOpts()->resetNonModularOptions();
930  PPOpts.resetNonModularOptions();
931 
932  // Remove any macro definitions that are explicitly ignored by the module.
933  // They aren't supposed to affect how the module is built anyway.
934  const HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
935  PPOpts.Macros.erase(
936  std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(),
937  [&HSOpts](const std::pair<std::string, bool> &def) {
938  StringRef MacroDef = def.first;
939  return HSOpts.ModulesIgnoreMacros.count(MacroDef.split('=').first) > 0;
940  }),
941  PPOpts.Macros.end());
942 
943  // Note the name of the module we're building.
944  Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName();
945 
946  // Make sure that the failed-module structure has been allocated in
947  // the importing instance, and propagate the pointer to the newly-created
948  // instance.
949  PreprocessorOptions &ImportingPPOpts
950  = ImportingInstance.getInvocation().getPreprocessorOpts();
951  if (!ImportingPPOpts.FailedModules)
953  PPOpts.FailedModules = ImportingPPOpts.FailedModules;
954 
955  // If there is a module map file, build the module using the module map.
956  // Set up the inputs/outputs so that we build the module from its umbrella
957  // header.
958  FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
959  FrontendOpts.OutputFile = ModuleFileName.str();
960  FrontendOpts.DisableFree = false;
961  FrontendOpts.GenerateGlobalModuleIndex = false;
962  FrontendOpts.BuildingImplicitModule = true;
963  FrontendOpts.Inputs.clear();
964  InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts());
965 
966  // Don't free the remapped file buffers; they are owned by our caller.
967  PPOpts.RetainRemappedFileBuffers = true;
968 
969  Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
970  assert(ImportingInstance.getInvocation().getModuleHash() ==
971  Invocation->getModuleHash() && "Module hash mismatch!");
972 
973  // Construct a compiler instance that will be used to actually create the
974  // module.
975  CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(),
976  /*BuildingModule=*/true);
977  Instance.setInvocation(&*Invocation);
978 
979  Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
980  ImportingInstance.getDiagnosticClient()),
981  /*ShouldOwnClient=*/true);
982 
983  Instance.setVirtualFileSystem(&ImportingInstance.getVirtualFileSystem());
984 
985  // Note that this module is part of the module build stack, so that we
986  // can detect cycles in the module graph.
987  Instance.setFileManager(&ImportingInstance.getFileManager());
988  Instance.createSourceManager(Instance.getFileManager());
989  SourceManager &SourceMgr = Instance.getSourceManager();
990  SourceMgr.setModuleBuildStack(
991  ImportingInstance.getSourceManager().getModuleBuildStack());
992  SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(),
993  FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
994 
995  // If we're collecting module dependencies, we need to share a collector
996  // between all of the module CompilerInstances. Other than that, we don't
997  // want to produce any dependency output from the module build.
998  Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
999  Invocation->getDependencyOutputOpts() = DependencyOutputOptions();
1000 
1001  // Get or create the module map that we'll use to build this module.
1002  std::string InferredModuleMapContent;
1003  if (const FileEntry *ModuleMapFile =
1004  ModMap.getContainingModuleMapFile(Module)) {
1005  // Use the module map where this module resides.
1006  FrontendOpts.Inputs.emplace_back(ModuleMapFile->getName(), IK);
1007  } else {
1008  SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1009  llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
1010  FrontendOpts.Inputs.emplace_back(FakeModuleMapFile, IK);
1011 
1012  llvm::raw_string_ostream OS(InferredModuleMapContent);
1013  Module->print(OS);
1014  OS.flush();
1015 
1016  std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
1017  llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
1018  ModuleMapFile = Instance.getFileManager().getVirtualFile(
1019  FakeModuleMapFile, InferredModuleMapContent.size(), 0);
1020  SourceMgr.overrideFileContents(ModuleMapFile, std::move(ModuleMapBuffer));
1021  }
1022 
1023  // Construct a module-generating action. Passing through the module map is
1024  // safe because the FileManager is shared between the compiler instances.
1025  GenerateModuleAction CreateModuleAction(
1026  ModMap.getModuleMapFileForUniquing(Module), Module->IsSystem);
1027 
1028  ImportingInstance.getDiagnostics().Report(ImportLoc,
1029  diag::remark_module_build)
1030  << Module->Name << ModuleFileName;
1031 
1032  // Execute the action to actually build the module in-place. Use a separate
1033  // thread so that we get a stack large enough.
1034  const unsigned ThreadStackSize = 8 << 20;
1035  llvm::CrashRecoveryContext CRC;
1036  CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(CreateModuleAction); },
1037  ThreadStackSize);
1038 
1039  ImportingInstance.getDiagnostics().Report(ImportLoc,
1040  diag::remark_module_build_done)
1041  << Module->Name;
1042 
1043  // Delete the temporary module map file.
1044  // FIXME: Even though we're executing under crash protection, it would still
1045  // be nice to do this with RemoveFileOnSignal when we can. However, that
1046  // doesn't make sense for all clients, so clean this up manually.
1047  Instance.clearOutputFiles(/*EraseFiles=*/true);
1048 
1049  // We've rebuilt a module. If we're allowed to generate or update the global
1050  // module index, record that fact in the importing compiler instance.
1051  if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
1052  ImportingInstance.setBuildGlobalModuleIndex(true);
1053  }
1054 
1055  return !Instance.getDiagnostics().hasErrorOccurred();
1056 }
1057 
1058 static bool compileAndLoadModule(CompilerInstance &ImportingInstance,
1059  SourceLocation ImportLoc,
1060  SourceLocation ModuleNameLoc, Module *Module,
1061  StringRef ModuleFileName) {
1062  DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1063 
1064  auto diagnoseBuildFailure = [&] {
1065  Diags.Report(ModuleNameLoc, diag::err_module_not_built)
1066  << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1067  };
1068 
1069  // FIXME: have LockFileManager return an error_code so that we can
1070  // avoid the mkdir when the directory already exists.
1071  StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
1072  llvm::sys::fs::create_directories(Dir);
1073 
1074  while (1) {
1075  unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1076  llvm::LockFileManager Locked(ModuleFileName);
1077  switch (Locked) {
1078  case llvm::LockFileManager::LFS_Error:
1079  Diags.Report(ModuleNameLoc, diag::err_module_lock_failure)
1080  << Module->Name << Locked.getErrorMessage();
1081  return false;
1082 
1083  case llvm::LockFileManager::LFS_Owned:
1084  // We're responsible for building the module ourselves.
1085  if (!compileModuleImpl(ImportingInstance, ModuleNameLoc, Module,
1086  ModuleFileName)) {
1087  diagnoseBuildFailure();
1088  return false;
1089  }
1090  break;
1091 
1092  case llvm::LockFileManager::LFS_Shared:
1093  // Someone else is responsible for building the module. Wait for them to
1094  // finish.
1095  switch (Locked.waitForUnlock()) {
1096  case llvm::LockFileManager::Res_Success:
1097  ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1098  break;
1099  case llvm::LockFileManager::Res_OwnerDied:
1100  continue; // try again to get the lock.
1101  case llvm::LockFileManager::Res_Timeout:
1102  Diags.Report(ModuleNameLoc, diag::err_module_lock_timeout)
1103  << Module->Name;
1104  // Clear the lock file so that future invokations can make progress.
1105  Locked.unsafeRemoveLockFile();
1106  return false;
1107  }
1108  break;
1109  }
1110 
1111  // Try to read the module file, now that we've compiled it.
1112  ASTReader::ASTReadResult ReadResult =
1113  ImportingInstance.getModuleManager()->ReadAST(
1114  ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
1115  ModuleLoadCapabilities);
1116 
1117  if (ReadResult == ASTReader::OutOfDate &&
1118  Locked == llvm::LockFileManager::LFS_Shared) {
1119  // The module may be out of date in the presence of file system races,
1120  // or if one of its imports depends on header search paths that are not
1121  // consistent with this ImportingInstance. Try again...
1122  continue;
1123  } else if (ReadResult == ASTReader::Missing) {
1124  diagnoseBuildFailure();
1125  } else if (ReadResult != ASTReader::Success &&
1126  !Diags.hasErrorOccurred()) {
1127  // The ASTReader didn't diagnose the error, so conservatively report it.
1128  diagnoseBuildFailure();
1129  }
1130  return ReadResult == ASTReader::Success;
1131  }
1132 }
1133 
1134 /// \brief Diagnose differences between the current definition of the given
1135 /// configuration macro and the definition provided on the command line.
1136 static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1137  Module *Mod, SourceLocation ImportLoc) {
1138  IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
1139  SourceManager &SourceMgr = PP.getSourceManager();
1140 
1141  // If this identifier has never had a macro definition, then it could
1142  // not have changed.
1143  if (!Id->hadMacroDefinition())
1144  return;
1145  auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
1146 
1147  // Find the macro definition from the command line.
1148  MacroInfo *CmdLineDefinition = nullptr;
1149  for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
1150  // We only care about the predefines buffer.
1151  FileID FID = SourceMgr.getFileID(MD->getLocation());
1152  if (FID.isInvalid() || FID != PP.getPredefinesFileID())
1153  continue;
1154  if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
1155  CmdLineDefinition = DMD->getMacroInfo();
1156  break;
1157  }
1158 
1159  auto *CurrentDefinition = PP.getMacroInfo(Id);
1160  if (CurrentDefinition == CmdLineDefinition) {
1161  // Macro matches. Nothing to do.
1162  } else if (!CurrentDefinition) {
1163  // This macro was defined on the command line, then #undef'd later.
1164  // Complain.
1165  PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1166  << true << ConfigMacro << Mod->getFullModuleName();
1167  auto LatestDef = LatestLocalMD->getDefinition();
1168  assert(LatestDef.isUndefined() &&
1169  "predefined macro went away with no #undef?");
1170  PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
1171  << true;
1172  return;
1173  } else if (!CmdLineDefinition) {
1174  // There was no definition for this macro in the predefines buffer,
1175  // but there was a local definition. Complain.
1176  PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1177  << false << ConfigMacro << Mod->getFullModuleName();
1178  PP.Diag(CurrentDefinition->getDefinitionLoc(),
1179  diag::note_module_def_undef_here)
1180  << false;
1181  } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
1182  /*Syntactically=*/true)) {
1183  // The macro definitions differ.
1184  PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1185  << false << ConfigMacro << Mod->getFullModuleName();
1186  PP.Diag(CurrentDefinition->getDefinitionLoc(),
1187  diag::note_module_def_undef_here)
1188  << false;
1189  }
1190 }
1191 
1192 /// \brief Write a new timestamp file with the given path.
1193 static void writeTimestampFile(StringRef TimestampFile) {
1194  std::error_code EC;
1195  llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::F_None);
1196 }
1197 
1198 /// \brief Prune the module cache of modules that haven't been accessed in
1199 /// a long time.
1200 static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
1201  struct stat StatBuf;
1202  llvm::SmallString<128> TimestampFile;
1203  TimestampFile = HSOpts.ModuleCachePath;
1204  assert(!TimestampFile.empty());
1205  llvm::sys::path::append(TimestampFile, "modules.timestamp");
1206 
1207  // Try to stat() the timestamp file.
1208  if (::stat(TimestampFile.c_str(), &StatBuf)) {
1209  // If the timestamp file wasn't there, create one now.
1210  if (errno == ENOENT) {
1211  writeTimestampFile(TimestampFile);
1212  }
1213  return;
1214  }
1215 
1216  // Check whether the time stamp is older than our pruning interval.
1217  // If not, do nothing.
1218  time_t TimeStampModTime = StatBuf.st_mtime;
1219  time_t CurrentTime = time(nullptr);
1220  if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
1221  return;
1222 
1223  // Write a new timestamp file so that nobody else attempts to prune.
1224  // There is a benign race condition here, if two Clang instances happen to
1225  // notice at the same time that the timestamp is out-of-date.
1226  writeTimestampFile(TimestampFile);
1227 
1228  // Walk the entire module cache, looking for unused module files and module
1229  // indices.
1230  std::error_code EC;
1231  SmallString<128> ModuleCachePathNative;
1232  llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
1233  for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd;
1234  Dir != DirEnd && !EC; Dir.increment(EC)) {
1235  // If we don't have a directory, there's nothing to look into.
1236  if (!llvm::sys::fs::is_directory(Dir->path()))
1237  continue;
1238 
1239  // Walk all of the files within this directory.
1240  for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
1241  File != FileEnd && !EC; File.increment(EC)) {
1242  // We only care about module and global module index files.
1243  StringRef Extension = llvm::sys::path::extension(File->path());
1244  if (Extension != ".pcm" && Extension != ".timestamp" &&
1245  llvm::sys::path::filename(File->path()) != "modules.idx")
1246  continue;
1247 
1248  // Look at this file. If we can't stat it, there's nothing interesting
1249  // there.
1250  if (::stat(File->path().c_str(), &StatBuf))
1251  continue;
1252 
1253  // If the file has been used recently enough, leave it there.
1254  time_t FileAccessTime = StatBuf.st_atime;
1255  if (CurrentTime - FileAccessTime <=
1256  time_t(HSOpts.ModuleCachePruneAfter)) {
1257  continue;
1258  }
1259 
1260  // Remove the file.
1261  llvm::sys::fs::remove(File->path());
1262 
1263  // Remove the timestamp file.
1264  std::string TimpestampFilename = File->path() + ".timestamp";
1265  llvm::sys::fs::remove(TimpestampFilename);
1266  }
1267 
1268  // If we removed all of the files in the directory, remove the directory
1269  // itself.
1270  if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
1271  llvm::sys::fs::directory_iterator() && !EC)
1272  llvm::sys::fs::remove(Dir->path());
1273  }
1274 }
1275 
1277  if (!ModuleManager) {
1278  if (!hasASTContext())
1279  createASTContext();
1280 
1281  // If we're implicitly building modules but not currently recursively
1282  // building a module, check whether we need to prune the module cache.
1283  if (getSourceManager().getModuleBuildStack().empty() &&
1284  !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() &&
1285  getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
1286  getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
1288  }
1289 
1291  std::string Sysroot = HSOpts.Sysroot;
1292  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1293  std::unique_ptr<llvm::Timer> ReadTimer;
1294  if (FrontendTimerGroup)
1295  ReadTimer = llvm::make_unique<llvm::Timer>("Reading modules",
1296  *FrontendTimerGroup);
1297  ModuleManager = new ASTReader(
1299  getFrontendOpts().ModuleFileExtensions,
1300  Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation,
1301  /*AllowASTWithCompilerErrors=*/false,
1302  /*AllowConfigurationMismatch=*/false,
1305  std::move(ReadTimer));
1306  if (hasASTConsumer()) {
1307  ModuleManager->setDeserializationListener(
1308  getASTConsumer().GetASTDeserializationListener());
1310  getASTConsumer().GetASTMutationListener());
1311  }
1312  getASTContext().setExternalSource(ModuleManager);
1313  if (hasSema())
1314  ModuleManager->InitializeSema(getSema());
1315  if (hasASTConsumer())
1316  ModuleManager->StartTranslationUnit(&getASTConsumer());
1317 
1318  if (TheDependencyFileGenerator)
1319  TheDependencyFileGenerator->AttachToASTReader(*ModuleManager);
1320  for (auto &Listener : DependencyCollectors)
1321  Listener->attachToASTReader(*ModuleManager);
1322  }
1323 }
1324 
1326  llvm::Timer Timer;
1327  if (FrontendTimerGroup)
1328  Timer.init("Preloading " + FileName.str(), *FrontendTimerGroup);
1329  llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
1330 
1331  // Helper to recursively read the module names for all modules we're adding.
1332  // We mark these as known and redirect any attempt to load that module to
1333  // the files we were handed.
1334  struct ReadModuleNames : ASTReaderListener {
1335  CompilerInstance &CI;
1337 
1338  ReadModuleNames(CompilerInstance &CI) : CI(CI) {}
1339 
1340  void ReadModuleName(StringRef ModuleName) override {
1341  LoadedModules.push_back(
1342  CI.getPreprocessor().getIdentifierInfo(ModuleName));
1343  }
1344 
1345  void registerAll() {
1346  for (auto *II : LoadedModules) {
1347  CI.KnownModules[II] = CI.getPreprocessor()
1349  .getModuleMap()
1350  .findModule(II->getName());
1351  }
1352  LoadedModules.clear();
1353  }
1354 
1355  void markAllUnavailable() {
1356  for (auto *II : LoadedModules) {
1357  if (Module *M = CI.getPreprocessor()
1359  .getModuleMap()
1360  .findModule(II->getName()))
1361  M->HasIncompatibleModuleFile = true;
1362  }
1363  LoadedModules.clear();
1364  }
1365  };
1366 
1367  // If we don't already have an ASTReader, create one now.
1368  if (!ModuleManager)
1370 
1371  auto Listener = llvm::make_unique<ReadModuleNames>(*this);
1372  auto &ListenerRef = *Listener;
1373  ASTReader::ListenerScope ReadModuleNamesListener(*ModuleManager,
1374  std::move(Listener));
1375 
1376  // Try to load the module file.
1377  switch (ModuleManager->ReadAST(FileName, serialization::MK_ExplicitModule,
1378  SourceLocation(),
1380  case ASTReader::Success:
1381  // We successfully loaded the module file; remember the set of provided
1382  // modules so that we don't try to load implicit modules for them.
1383  ListenerRef.registerAll();
1384  return true;
1385 
1387  // Ignore unusable module files.
1388  getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch)
1389  << FileName;
1390  // All modules provided by any files we tried and failed to load are now
1391  // unavailable; includes of those modules should now be handled textually.
1392  ListenerRef.markAllUnavailable();
1393  return true;
1394 
1395  default:
1396  return false;
1397  }
1398 }
1399 
1402  ModuleIdPath Path,
1404  bool IsInclusionDirective) {
1405  // Determine what file we're searching from.
1406  StringRef ModuleName = Path[0].first->getName();
1407  SourceLocation ModuleNameLoc = Path[0].second;
1408 
1409  // If we've already handled this import, just return the cached result.
1410  // This one-element cache is important to eliminate redundant diagnostics
1411  // when both the preprocessor and parser see the same import declaration.
1412  if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) {
1413  // Make the named module visible.
1414  if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
1415  ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
1416  ImportLoc);
1417  return LastModuleImportResult;
1418  }
1419 
1420  clang::Module *Module = nullptr;
1421 
1422  // If we don't already have information on this module, load the module now.
1424  = KnownModules.find(Path[0].first);
1425  if (Known != KnownModules.end()) {
1426  // Retrieve the cached top-level module.
1427  Module = Known->second;
1428  } else if (ModuleName == getLangOpts().CurrentModule) {
1429  // This is the module we're building.
1430  Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
1431  Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1432  } else {
1433  // Search for a module with the given name.
1434  Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
1435  if (!Module) {
1436  getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1437  << ModuleName
1438  << SourceRange(ImportLoc, ModuleNameLoc);
1439  ModuleBuildFailed = true;
1440  return ModuleLoadResult();
1441  }
1442 
1443  std::string ModuleFileName =
1444  PP->getHeaderSearchInfo().getModuleFileName(Module);
1445  if (ModuleFileName.empty()) {
1446  if (Module->HasIncompatibleModuleFile) {
1447  // We tried and failed to load a module file for this module. Fall
1448  // back to textual inclusion for its headers.
1449  return ModuleLoadResult(nullptr, /*missingExpected*/true);
1450  }
1451 
1452  getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
1453  << ModuleName;
1454  ModuleBuildFailed = true;
1455  return ModuleLoadResult();
1456  }
1457 
1458  // If we don't already have an ASTReader, create one now.
1459  if (!ModuleManager)
1461 
1462  llvm::Timer Timer;
1463  if (FrontendTimerGroup)
1464  Timer.init("Loading " + ModuleFileName, *FrontendTimerGroup);
1465  llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
1466 
1467  // Try to load the module file.
1468  unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing;
1469  switch (ModuleManager->ReadAST(ModuleFileName,
1471  ImportLoc, ARRFlags)) {
1472  case ASTReader::Success:
1473  break;
1474 
1475  case ASTReader::OutOfDate:
1476  case ASTReader::Missing: {
1477  // The module file is missing or out-of-date. Build it.
1478  assert(Module && "missing module file");
1479  // Check whether there is a cycle in the module graph.
1481  ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
1482  for (; Pos != PosEnd; ++Pos) {
1483  if (Pos->first == ModuleName)
1484  break;
1485  }
1486 
1487  if (Pos != PosEnd) {
1488  SmallString<256> CyclePath;
1489  for (; Pos != PosEnd; ++Pos) {
1490  CyclePath += Pos->first;
1491  CyclePath += " -> ";
1492  }
1493  CyclePath += ModuleName;
1494 
1495  getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1496  << ModuleName << CyclePath;
1497  return ModuleLoadResult();
1498  }
1499 
1500  // Check whether we have already attempted to build this module (but
1501  // failed).
1502  if (getPreprocessorOpts().FailedModules &&
1503  getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
1504  getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
1505  << ModuleName
1506  << SourceRange(ImportLoc, ModuleNameLoc);
1507  ModuleBuildFailed = true;
1508  return ModuleLoadResult();
1509  }
1510 
1511  // Try to compile and then load the module.
1512  if (!compileAndLoadModule(*this, ImportLoc, ModuleNameLoc, Module,
1513  ModuleFileName)) {
1514  assert(getDiagnostics().hasErrorOccurred() &&
1515  "undiagnosed error in compileAndLoadModule");
1516  if (getPreprocessorOpts().FailedModules)
1517  getPreprocessorOpts().FailedModules->addFailed(ModuleName);
1518  KnownModules[Path[0].first] = nullptr;
1519  ModuleBuildFailed = true;
1520  return ModuleLoadResult();
1521  }
1522 
1523  // Okay, we've rebuilt and now loaded the module.
1524  break;
1525  }
1526 
1529  case ASTReader::HadErrors:
1531  // FIXME: The ASTReader will already have complained, but can we shoehorn
1532  // that diagnostic information into a more useful form?
1533  KnownModules[Path[0].first] = nullptr;
1534  return ModuleLoadResult();
1535 
1536  case ASTReader::Failure:
1538  // Already complained, but note now that we failed.
1539  KnownModules[Path[0].first] = nullptr;
1540  ModuleBuildFailed = true;
1541  return ModuleLoadResult();
1542  }
1543 
1544  // Cache the result of this top-level module lookup for later.
1545  Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1546  }
1547 
1548  // If we never found the module, fail.
1549  if (!Module)
1550  return ModuleLoadResult();
1551 
1552  // Verify that the rest of the module path actually corresponds to
1553  // a submodule.
1554  if (Path.size() > 1) {
1555  for (unsigned I = 1, N = Path.size(); I != N; ++I) {
1556  StringRef Name = Path[I].first->getName();
1557  clang::Module *Sub = Module->findSubmodule(Name);
1558 
1559  if (!Sub) {
1560  // Attempt to perform typo correction to find a module name that works.
1563 
1565  JEnd = Module->submodule_end();
1566  J != JEnd; ++J) {
1567  unsigned ED = Name.edit_distance((*J)->Name,
1568  /*AllowReplacements=*/true,
1569  BestEditDistance);
1570  if (ED <= BestEditDistance) {
1571  if (ED < BestEditDistance) {
1572  Best.clear();
1573  BestEditDistance = ED;
1574  }
1575 
1576  Best.push_back((*J)->Name);
1577  }
1578  }
1579 
1580  // If there was a clear winner, user it.
1581  if (Best.size() == 1) {
1582  getDiagnostics().Report(Path[I].second,
1583  diag::err_no_submodule_suggest)
1584  << Path[I].first << Module->getFullModuleName() << Best[0]
1585  << SourceRange(Path[0].second, Path[I-1].second)
1586  << FixItHint::CreateReplacement(SourceRange(Path[I].second),
1587  Best[0]);
1588 
1589  Sub = Module->findSubmodule(Best[0]);
1590  }
1591  }
1592 
1593  if (!Sub) {
1594  // No submodule by this name. Complain, and don't look for further
1595  // submodules.
1596  getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
1597  << Path[I].first << Module->getFullModuleName()
1598  << SourceRange(Path[0].second, Path[I-1].second);
1599  break;
1600  }
1601 
1602  Module = Sub;
1603  }
1604  }
1605 
1606  // Make the named module visible, if it's not already part of the module
1607  // we are parsing.
1608  if (ModuleName != getLangOpts().CurrentModule) {
1609  if (!Module->IsFromModuleFile) {
1610  // We have an umbrella header or directory that doesn't actually include
1611  // all of the headers within the directory it covers. Complain about
1612  // this missing submodule and recover by forgetting that we ever saw
1613  // this submodule.
1614  // FIXME: Should we detect this at module load time? It seems fairly
1615  // expensive (and rare).
1616  getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
1617  << Module->getFullModuleName()
1618  << SourceRange(Path.front().second, Path.back().second);
1619 
1620  return ModuleLoadResult(nullptr, true);
1621  }
1622 
1623  // Check whether this module is available.
1624  clang::Module::Requirement Requirement;
1626  if (!Module->isAvailable(getLangOpts(), getTarget(), Requirement,
1627  MissingHeader)) {
1628  if (MissingHeader.FileNameLoc.isValid()) {
1629  getDiagnostics().Report(MissingHeader.FileNameLoc,
1630  diag::err_module_header_missing)
1631  << MissingHeader.IsUmbrella << MissingHeader.FileName;
1632  } else {
1633  getDiagnostics().Report(ImportLoc, diag::err_module_unavailable)
1634  << Module->getFullModuleName()
1635  << Requirement.second << Requirement.first
1636  << SourceRange(Path.front().second, Path.back().second);
1637  }
1638  LastModuleImportLoc = ImportLoc;
1639  LastModuleImportResult = ModuleLoadResult();
1640  return ModuleLoadResult();
1641  }
1642 
1643  ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc);
1644  }
1645 
1646  // Check for any configuration macros that have changed.
1647  clang::Module *TopModule = Module->getTopLevelModule();
1648  for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
1650  Module, ImportLoc);
1651  }
1652 
1653  LastModuleImportLoc = ImportLoc;
1654  LastModuleImportResult = ModuleLoadResult(Module, false);
1655  return LastModuleImportResult;
1656 }
1657 
1660  SourceLocation ImportLoc) {
1661  if (!ModuleManager)
1663  if (!ModuleManager)
1664  return;
1665 
1666  ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc);
1667 }
1668 
1670  SourceLocation TriggerLoc) {
1671  if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
1672  return nullptr;
1673  if (!ModuleManager)
1675  // Can't do anything if we don't have the module manager.
1676  if (!ModuleManager)
1677  return nullptr;
1678  // Get an existing global index. This loads it if not already
1679  // loaded.
1680  ModuleManager->loadGlobalIndex();
1681  GlobalModuleIndex *GlobalIndex = ModuleManager->getGlobalIndex();
1682  // If the global index doesn't exist, create it.
1683  if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
1684  hasPreprocessor()) {
1685  llvm::sys::fs::create_directories(
1686  getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
1689  getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
1690  ModuleManager->resetForReload();
1691  ModuleManager->loadGlobalIndex();
1692  GlobalIndex = ModuleManager->getGlobalIndex();
1693  }
1694  // For finding modules needing to be imported for fixit messages,
1695  // we need to make the global index cover all modules, so we do that here.
1696  if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
1698  bool RecreateIndex = false;
1700  E = MMap.module_end(); I != E; ++I) {
1701  Module *TheModule = I->second;
1702  const FileEntry *Entry = TheModule->getASTFile();
1703  if (!Entry) {
1705  Path.push_back(std::make_pair(
1706  getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
1707  std::reverse(Path.begin(), Path.end());
1708  // Load a module as hidden. This also adds it to the global index.
1709  loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
1710  RecreateIndex = true;
1711  }
1712  }
1713  if (RecreateIndex) {
1716  getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
1717  ModuleManager->resetForReload();
1718  ModuleManager->loadGlobalIndex();
1719  GlobalIndex = ModuleManager->getGlobalIndex();
1720  }
1721  HaveFullGlobalModuleIndex = true;
1722  }
1723  return GlobalIndex;
1724 }
1725 
1726 // Check global module index for missing imports.
1727 bool
1729  SourceLocation TriggerLoc) {
1730  // Look for the symbol in non-imported modules, but only if an error
1731  // actually occurred.
1732  if (!buildingModule()) {
1733  // Load global module index, or retrieve a previously loaded one.
1735  TriggerLoc);
1736 
1737  // Only if we have a global index.
1738  if (GlobalIndex) {
1739  GlobalModuleIndex::HitSet FoundModules;
1740 
1741  // Find the modules that reference the identifier.
1742  // Note that this only finds top-level modules.
1743  // We'll let diagnoseTypo find the actual declaration module.
1744  if (GlobalIndex->lookupIdentifier(Name, FoundModules))
1745  return true;
1746  }
1747  }
1748 
1749  return false;
1750 }
std::string OutputFile
The output file, if any.
SourceManager & getSourceManager() const
Definition: Preprocessor.h:694
void setExternalSource(IntrusiveRefCntPtr< ExternalASTSource > Source)
Attach an external AST source to the AST context.
Definition: ASTContext.cpp:817
static bool compileAndLoadModule(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName)
Defines the clang::ASTContext interface.
virtual bool isModelParsingAction() const
Is this action invoked on a model file?
LangOptions & getLangOpts()
ASTReadResult
The result of reading the control block of an AST file, which can fail for various reasons...
Definition: ASTReader.h:326
ASTContext & getASTContext() const
std::string Name
The name of this module.
Definition: Basic/Module.h:50
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
void createCodeCompletionConsumer()
Create a code completion consumer using the invocation; note that this will cause the source manager ...
std::vector< std::pair< std::string, bool > > Macros
std::string ModuleDependencyOutputDir
The directory to copy module dependencies to when collecting them.
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:369
module_iterator module_begin() const
Definition: ModuleMap.h:528
std::string DwarfDebugFlags
The string to embed in the debug information for the compile unit, if non-empty.
SelectorTable & getSelectorTable()
Definition: Preprocessor.h:699
std::string DOTOutputFile
The file to write GraphViz-formatted header dependencies to.
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:117
void setCodeCompletionConsumer(CodeCompleteConsumer *Value)
setCodeCompletionConsumer - Replace the current code completion consumer; the compiler instance takes...
vfs::FileSystem & getVirtualFileSystem() const
Defines the clang::FileManager interface and associated types.
Module * findModule(StringRef Name) const
Retrieve a module with the given name.
Definition: ModuleMap.cpp:518
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's diagnostic options and replace any existing one ...
submodule_iterator submodule_begin()
Definition: Basic/Module.h:474
static void writeTimestampFile(StringRef TimestampFile)
Write a new timestamp file with the given path.
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Basic/Module.h:368
Defines the SourceManager interface.
IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
Definition: Preprocessor.h:934
void createSema(TranslationUnitKind TUKind, CodeCompleteConsumer *CompletionConsumer)
Create the Sema object to be used for parsing.
Abstract base class for actions which can be performed by the frontend.
DiagnosticOptions & getDiagnosticOpts()
std::string HeaderIncludeOutputFile
The file to write header include output to.
bool hasErrorOccurred() const
Definition: Diagnostic.h:580
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1124
bool InitializeSourceManager(const FrontendInputFile &Input)
InitializeSourceManager - Initialize the source manager to set InputFile as the main file...
void setAuxTarget(TargetInfo *Value)
Replace the current AuxTarget.
ChainedDiagnosticConsumer - Chain two diagnostic clients so that diagnostics go to the first client a...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Definition: Diagnostic.h:377
std::error_code make_error_code(ParseError e)
Definition: Format.cpp:419
The client can handle an AST file that cannot load because it is out-of-date relative to its input fi...
Definition: ASTReader.h:1366
void setModuleManager(IntrusiveRefCntPtr< ASTReader > Reader)
The AST file has errors.
Definition: ASTReader.h:343
TargetInfo & getTarget() const
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
std::string getModuleHash() const
Retrieve a module hash string that is suitable for uniquely identifying the conditions under which th...
const FileEntry * getASTFile() const
The serialized AST file for this module, if one was created.
Definition: Basic/Module.h:384
SourceManager & getSourceManager() const
Return the current source manager.
static PTHManager * Create(StringRef file, DiagnosticsEngine &Diags)
Create - This method creates PTHManager objects.
Definition: PTHLexer.cpp:434
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1314
DependencyOutputOptions & getDependencyOutputOpts()
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:78
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:590
std::string ModuleCachePath
The directory used for the module cache.
Builtin::Context & getBuiltinInfo()
Definition: Preprocessor.h:700
llvm::SmallSetVector< std::string, 16 > ModulesIgnoreMacros
The set of macro names that should be ignored for the purposes of computing the module hash...
void setPredefines(const char *P)
Set the predefines for this Preprocessor.
Definition: Preprocessor.h:929
off_t getSize() const
Definition: FileManager.h:88
The client can handle an AST file that cannot load because it is missing.
Definition: ASTReader.h:1363
virtual void adjust(const LangOptions &Opts)
Set forced language options.
std::string FindPchSource
If non-empty, search the pch input file as it was a header.
std::string getFullModuleName() const
Retrieve the full name of this module, including the path from its top-level module.
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.
unsigned getNumWarnings() const
Definition: Diagnostic.h:1323
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:93
static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, DiagnosticsEngine &Diags, StringRef OutputFile)
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input)
Prepare the action for processing the input file Input.
void createFileManager()
Create the file manager and replace any existing one with it.
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
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.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
Definition: Preprocessor.h:858
unsigned DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
void setASTContext(ASTContext *Value)
setASTContext - Replace the current AST context.
CodeGenOptions & getCodeGenOpts()
bool buildingModule() const
Returns true if this instance is building a module.
Definition: ModuleLoader.h:67
llvm::MemoryBuffer * getBuffer() const
The client can handle an AST file that cannot load because it's compiled configuration doesn't match ...
Definition: ASTReader.h:1373
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
ASTConsumer & getASTConsumer() const
unsigned BuildingImplicitModule
Whether we are performing an implicit module build.
Describes a module or submodule.
Definition: Basic/Module.h:47
FileManager & getFileManager() const
Definition: Preprocessor.h:693
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.
unsigned ShowStats
Show frontend performance metrics and statistics.
void setBuildGlobalModuleIndex(bool Build)
Set the flag indicating whether we should (re)build the global module index.
bool isAvailable() const
Determine whether this module is available for use within the current translation unit...
Definition: Basic/Module.h:314
FrontendOptions & getFrontendOpts()
IntrusiveRefCntPtr< vfs::FileSystem > getVirtualFileSystem() const
Definition: FileManager.h:226
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition: Visibility.h:32
DiagnosticConsumer & getDiagnosticClient() const
PreprocessorOutputOptions & getPreprocessorOutputOpts()
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:695
IntrusiveRefCntPtr< FailedModulesSet > FailedModules
The set of modules that failed to build.
module_iterator module_end() const
Definition: ModuleMap.h:529
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: Basic/Module.h:163
const FileEntry * getContainingModuleMapFile(const Module *Module) const
Retrieve the module map file containing the definition of the given module.
Definition: ModuleMap.cpp:832
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
submodule_iterator submodule_end()
Definition: Basic/Module.h:476
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...
The AST file itself appears corrupted.
Definition: ASTReader.h:331
detail::InMemoryDirectory::const_iterator I
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...
bool DisablePCHValidation
When true, disables most of the normal validation performed on precompiled headers.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
std::vector< Module * >::iterator submodule_iterator
Definition: Basic/Module.h:471
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.
bool hasVirtualFileSystem() const
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:137
static ErrorCode writeIndex(FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, StringRef Path)
Write a global index into the given.
llvm::StringMap< Module * >::const_iterator module_iterator
Definition: ModuleMap.h:527
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers)...
Definition: Basic/Module.h:173
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
static DependencyFileGenerator * CreateAndAttachToPreprocessor(Preprocessor &PP, const DependencyOutputOptions &Opts)
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:263
void setPreprocessor(Preprocessor *Value)
Replace the current preprocessor.
Module * findSubmodule(StringRef Name) const
Find the submodule with the given name.
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:107
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
void setVirtualFileSystem(IntrusiveRefCntPtr< vfs::FileSystem > FS)
Replace the current virtual file system.
Describes the result of attempting to load a module.
Definition: ModuleLoader.h:33
std::string ShowIncludesPretendHeader
In /showIncludes mode, pretend the main TU is a header with this name.
StringRef Filename
Definition: Format.cpp:1194
ASTContext * Context
Exposes information about the current target.
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
A simple code-completion consumer that prints the results it receives in a simple format...
void setInvocation(CompilerInvocation *Value)
setInvocation - Replace the current invocation.
unsigned ModuleCachePruneInterval
The interval (in seconds) between pruning operations.
friend class ASTContext
Definition: Type.h:4178
static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts)
Determine the appropriate source input kind based on language options.
HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized. ...
Definition: HeaderSearch.h:258
const FileEntry * LookupFile(StringRef Filename, SourceLocation IncludeLoc, bool isAngled, const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir, ArrayRef< std::pair< const FileEntry *, const DirectoryEntry * >> Includers, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, bool SkipCache=false, bool BuildSystemModule=false)
Given a "foo" or <foo> reference, look up the indicated file, return null on failure.
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
std::string getSpecificModuleCachePath()
RAII object to temporarily add an AST callback listener.
Definition: ASTReader.h:1430
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...
Defines version macros and version-related utility functions for Clang.
Defines the clang::Preprocessor interface.
void createASTContext()
Create the AST context.
unsigned ShowHeaderIncludes
Show header inclusions (-H).
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
Definition: Targets.cpp:8554
An input file for the front end.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
void addOutputFile(OutputFile &&OutFile)
addOutputFile - Add an output file onto the list of tracked output files.
unsigned BestEditDistance
static bool EnableCodeCompletion(Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column)
std::string AuxTriple
Auxiliary triple for CUDA compilation.
The client can't handle any AST loading failures.
Definition: ASTReader.h:1360
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...
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition: Basic/Module.h:379
The AST file was missing.
Definition: ASTReader.h:333
bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Check global module index for missing imports.
std::unique_ptr< Sema > takeSema()
const DirectoryEntry * Directory
The build directory of this module.
Definition: Basic/Module.h:62
void overrideFileContents(const FileEntry *SourceFile, llvm::MemoryBuffer *Buffer, bool DoNotFree)
Override the contents of the given source file by providing an already-allocated buffer.
bool hasSourceManager() const
FileSystemOptions & getFileSystemOpts()
bool isNamedPipe() const
Check whether the file is a named pipe (and thus can't be opened by the native FileManager methods)...
Definition: FileManager.h:101
The control block was read successfully.
Definition: ASTReader.h:329
#define false
Definition: stdbool.h:33
StringRef FileName
Definition: Format.cpp:1313
Kind
void setPreprocessor(Preprocessor *pp)
Definition: PTHManager.h:134
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's CompilerInvocation object...
File is a PCH file treated as the preamble.
const char * getName() const
Definition: FileManager.h:85
void print(raw_ostream &OS, unsigned Indent=0) const
Print the module map for this module to the given stream.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
bool Execute()
Set the source manager's main input file, and run the action.
const TemplateArgument * iterator
Definition: Type.h:4233
File is a PCH file treated as such.
ParsedSourceLocation CodeCompletionAt
If given, enable code completion at the provided location.
AnnotatedLine & Line
bool isValid() const
Return true if this is a valid SourceLocation object.
Options for controlling the compiler diagnostics engine.
static bool compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName)
Compile a module file for the given module, using the options provided by the importing compiler inst...
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:1351
All of the names in this module are hidden.
Definition: Basic/Module.h:208
File is an implicitly-loaded module.
unsigned ModulesValidateSystemHeaders
Whether to validate system input files when a module is loaded.
IdentifierTable & getIdentifierTable()
Definition: Preprocessor.h:697
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:53
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...
std::string DiagnosticSerializationFile
The file to serialize diagnostics to (non-appending).
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1381
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:44
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
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...
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
void InitBuiltinTypes(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize built-in types.
Definition: ASTContext.cpp:982
void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc)
Push an entry to the module build stack.
FileID getMainFileID() const
Returns the FileID of the main source file.
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...
void resetNonModularOptions()
Reset any options that are not considered when building a module.
A global index for a set of module files, providing information about the identifiers within those mo...
The AST file was writtten with a different language/target configuration.
Definition: ASTReader.h:341
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:687
const char * getName() const
Definition: FileManager.h:45
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
Definition: Preprocessor.h:777
TargetInfo * getAuxTarget() const
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 setDeserializationListener(ASTDeserializationListener *Listener, bool TakeOwnership=false)
Set the AST deserialization listener.
Definition: ASTReader.cpp:659
Stored information about a header directive that was found in the module map file but has not been re...
Definition: Basic/Module.h:130
PreprocessorOptions & getPreprocessorOpts()
static void InitializeFileRemapping(DiagnosticsEngine &Diags, SourceManager &SourceMgr, FileManager &FileMgr, const PreprocessorOptions &InitOpts)
StringRef getFile() const
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:312
File is an explicitly-loaded module.
FileManager & getFileManager() const
Return the current file manager to the caller.
bool loadModuleFile(StringRef FileName)
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false, bool ShouldCloseOpenFile=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
unsigned UseGlobalModuleIndex
Whether we can use the global module index if available.
Used for handling and querying diagnostic IDs.
void setASTMutationListener(ASTMutationListener *Listener)
Attach an AST mutation listener to the AST context.
Definition: ASTContext.h:952
Helper class for holding the data necessary to invoke the compiler.
static void pruneModuleCache(const HeaderSearchOptions &HSOpts)
Prune the module cache of modules that haven't been accessed in a long time.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
detail::InMemoryDirectory::const_iterator E
FrontendOptions - Options for controlling the behavior of the frontend.
Abstract interface for a module loader.
Definition: ModuleLoader.h:56
Defines the Diagnostic-related interfaces.
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:34
SourceLocation DefinitionLoc
The location of the module definition.
Definition: Basic/Module.h:53
void clearOutputFiles(bool EraseFiles)
clearOutputFiles - Clear the output file list.
void BuryPointer(const void *Ptr)
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:840
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:104
bool isInvalid() const
MacroDirective * getLocalMacroDirectiveHistory(const IdentifierInfo *II) const
Given an identifier, return the latest non-imported macro directive for that identifier.
void setOverridenFilesKeepOriginalName(bool value)
Set true if the SourceManager should report the original file name for contents of files that were ov...
std::pair< std::string, bool > Requirement
An individual requirement: a feature name and a flag indicating the required state of that feature...
Definition: Basic/Module.h:142
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's options) and add it to the list of tracked outp...
void setTarget(TargetInfo *Value)
Replace the current Target.
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
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:50
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
unsigned ModuleCachePruneAfter
The time (in seconds) after which an unused module file will be considered unused and will...
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
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:172
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:115
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 StringRef Input
unsigned HasIncompatibleModuleFile
Whether we tried and failed to load a module file for this module.
Definition: Basic/Module.h:154
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:336
A SourceLocation and its associated SourceManager.
NameVisibilityKind
Describes the visibility of the various names within a particular module.
Definition: Basic/Module.h:206
void PrintStats() const
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: Basic/Module.h:279
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
unsigned getNumErrors() const
Definition: Diagnostic.h:1322
The AST file was written by a different version of Clang.
Definition: ASTReader.h:338
#define CLANG_VERSION_STRING
A string that describes the Clang version number, e.g., "1.0".
Definition: Version.h:30
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
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.
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
bool ownsClient() const
Determine whether this DiagnosticsEngine object own its client.
Definition: Diagnostic.h:373
A trivial tuple used to represent a source range.
unsigned PrintShowIncludes
Print cl.exe style /showIncludes info.
bool isValid() const
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition: FileManager.h:95
This class handles loading and caching of source files into memory.
void setDiagnostics(DiagnosticsEngine *Value)
setDiagnostics - Replace the current diagnostics engine.
std::string DiagnosticLogFile
The file to log diagnostic output to.
std::string TokenCache
If given, a PTH cache file to use for speeding up header parsing.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:97
bool lookupIdentifier(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.