clang  5.0.0
FrontendAction.cpp
Go to the documentation of this file.
1 //===--- FrontendAction.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/DeclGroup.h"
14 #include "clang/Frontend/ASTUnit.h"
20 #include "clang/Frontend/Utils.h"
21 #include "clang/Lex/HeaderSearch.h"
23 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Parse/ParseAST.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/Timer.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include <system_error>
35 using namespace clang;
36 
37 LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
38 
39 namespace {
40 
41 class DelegatingDeserializationListener : public ASTDeserializationListener {
43  bool DeletePrevious;
44 
45 public:
46  explicit DelegatingDeserializationListener(
47  ASTDeserializationListener *Previous, bool DeletePrevious)
48  : Previous(Previous), DeletePrevious(DeletePrevious) {}
49  ~DelegatingDeserializationListener() override {
50  if (DeletePrevious)
51  delete Previous;
52  }
53 
54  void ReaderInitialized(ASTReader *Reader) override {
55  if (Previous)
56  Previous->ReaderInitialized(Reader);
57  }
58  void IdentifierRead(serialization::IdentID ID,
59  IdentifierInfo *II) override {
60  if (Previous)
61  Previous->IdentifierRead(ID, II);
62  }
63  void TypeRead(serialization::TypeIdx Idx, QualType T) override {
64  if (Previous)
65  Previous->TypeRead(Idx, T);
66  }
67  void DeclRead(serialization::DeclID ID, const Decl *D) override {
68  if (Previous)
69  Previous->DeclRead(ID, D);
70  }
71  void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
72  if (Previous)
73  Previous->SelectorRead(ID, Sel);
74  }
75  void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
76  MacroDefinitionRecord *MD) override {
77  if (Previous)
78  Previous->MacroDefinitionRead(PPID, MD);
79  }
80 };
81 
82 /// \brief Dumps deserialized declarations.
83 class DeserializedDeclsDumper : public DelegatingDeserializationListener {
84 public:
85  explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
86  bool DeletePrevious)
87  : DelegatingDeserializationListener(Previous, DeletePrevious) {}
88 
89  void DeclRead(serialization::DeclID ID, const Decl *D) override {
90  llvm::outs() << "PCH DECL: " << D->getDeclKindName();
91  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
92  llvm::outs() << " - " << *ND;
93  llvm::outs() << "\n";
94 
95  DelegatingDeserializationListener::DeclRead(ID, D);
96  }
97 };
98 
99 /// \brief Checks deserialized declarations and emits error if a name
100 /// matches one given in command-line using -error-on-deserialized-decl.
101 class DeserializedDeclsChecker : public DelegatingDeserializationListener {
102  ASTContext &Ctx;
103  std::set<std::string> NamesToCheck;
104 
105 public:
106  DeserializedDeclsChecker(ASTContext &Ctx,
107  const std::set<std::string> &NamesToCheck,
109  bool DeletePrevious)
110  : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
111  NamesToCheck(NamesToCheck) {}
112 
113  void DeclRead(serialization::DeclID ID, const Decl *D) override {
114  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
115  if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
116  unsigned DiagID
118  "%0 was deserialized");
119  Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
120  << ND->getNameAsString();
121  }
122 
123  DelegatingDeserializationListener::DeclRead(ID, D);
124  }
125 };
126 
127 } // end anonymous namespace
128 
129 FrontendAction::FrontendAction() : Instance(nullptr) {}
130 
132 
134  std::unique_ptr<ASTUnit> AST) {
135  this->CurrentInput = CurrentInput;
136  CurrentASTUnit = std::move(AST);
137 }
138 
142  CI.getLangOpts().CurrentModule, /*AllowSearch*/false);
143 }
144 
145 std::unique_ptr<ASTConsumer>
146 FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
147  StringRef InFile) {
148  std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
149  if (!Consumer)
150  return nullptr;
151 
152  // If there are no registered plugins we don't need to wrap the consumer
153  if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
154  return Consumer;
155 
156  // Collect the list of plugins that go before the main action (in Consumers)
157  // or after it (in AfterConsumers)
158  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
159  std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
160  for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
161  ie = FrontendPluginRegistry::end();
162  it != ie; ++it) {
163  std::unique_ptr<PluginASTAction> P = it->instantiate();
164  PluginASTAction::ActionType ActionType = P->getActionType();
165  if (ActionType == PluginASTAction::Cmdline) {
166  // This is O(|plugins| * |add_plugins|), but since both numbers are
167  // way below 50 in practice, that's ok.
168  for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
169  i != e; ++i) {
170  if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
172  break;
173  }
174  }
175  }
176  if ((ActionType == PluginASTAction::AddBeforeMainAction ||
177  ActionType == PluginASTAction::AddAfterMainAction) &&
178  P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) {
179  std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
180  if (ActionType == PluginASTAction::AddBeforeMainAction) {
181  Consumers.push_back(std::move(PluginConsumer));
182  } else {
183  AfterConsumers.push_back(std::move(PluginConsumer));
184  }
185  }
186  }
187 
188  // Add to Consumers the main consumer, then all the plugins that go after it
189  Consumers.push_back(std::move(Consumer));
190  for (auto &C : AfterConsumers) {
191  Consumers.push_back(std::move(C));
192  }
193 
194  return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
195 }
196 
197 /// For preprocessed files, if the first line is the linemarker and specifies
198 /// the original source file name, use that name as the input file name.
199 /// Returns the location of the first token after the line marker directive.
200 ///
201 /// \param CI The compiler instance.
202 /// \param InputFile Populated with the filename from the line marker.
203 /// \param IsModuleMap If \c true, add a line note corresponding to this line
204 /// directive. (We need to do this because the directive will not be
205 /// visited by the preprocessor.)
207  std::string &InputFile,
208  bool IsModuleMap = false) {
209  auto &SourceMgr = CI.getSourceManager();
210  auto MainFileID = SourceMgr.getMainFileID();
211 
212  bool Invalid = false;
213  const auto *MainFileBuf = SourceMgr.getBuffer(MainFileID, &Invalid);
214  if (Invalid)
215  return SourceLocation();
216 
217  std::unique_ptr<Lexer> RawLexer(
218  new Lexer(MainFileID, MainFileBuf, SourceMgr, CI.getLangOpts()));
219 
220  // If the first line has the syntax of
221  //
222  // # NUM "FILENAME"
223  //
224  // we use FILENAME as the input file name.
225  Token T;
226  if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash)
227  return SourceLocation();
228  if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
229  T.getKind() != tok::numeric_constant)
230  return SourceLocation();
231 
232  unsigned LineNo;
233  SourceLocation LineNoLoc = T.getLocation();
234  if (IsModuleMap) {
236  if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
237  .getAsInteger(10, LineNo))
238  return SourceLocation();
239  }
240 
241  RawLexer->LexFromRawLexer(T);
242  if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
243  return SourceLocation();
244 
245  StringLiteralParser Literal(T, CI.getPreprocessor());
246  if (Literal.hadError)
247  return SourceLocation();
248  RawLexer->LexFromRawLexer(T);
249  if (T.isNot(tok::eof) && !T.isAtStartOfLine())
250  return SourceLocation();
251  InputFile = Literal.GetString().str();
252 
253  if (IsModuleMap)
255  LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
256  false, SrcMgr::C_User_ModuleMap);
257 
258  return T.getLocation();
259 }
260 
261 static SmallVectorImpl<char> &
262 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
263  Includes.append(RHS.begin(), RHS.end());
264  return Includes;
265 }
266 
267 static void addHeaderInclude(StringRef HeaderName,
268  SmallVectorImpl<char> &Includes,
269  const LangOptions &LangOpts,
270  bool IsExternC) {
271  if (IsExternC && LangOpts.CPlusPlus)
272  Includes += "extern \"C\" {\n";
273  if (LangOpts.ObjC1)
274  Includes += "#import \"";
275  else
276  Includes += "#include \"";
277 
278  Includes += HeaderName;
279 
280  Includes += "\"\n";
281  if (IsExternC && LangOpts.CPlusPlus)
282  Includes += "}\n";
283 }
284 
285 /// \brief Collect the set of header includes needed to construct the given
286 /// module and update the TopHeaders file set of the module.
287 ///
288 /// \param Module The module we're collecting includes from.
289 ///
290 /// \param Includes Will be augmented with the set of \#includes or \#imports
291 /// needed to load all of the named headers.
292 static std::error_code collectModuleHeaderIncludes(
293  const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
294  ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
295  // Don't collect any headers for unavailable modules.
296  if (!Module->isAvailable())
297  return std::error_code();
298 
299  // Resolve all lazy header directives to header files.
300  ModMap.resolveHeaderDirectives(Module);
301 
302  // If any headers are missing, we can't build this module. In most cases,
303  // diagnostics for this should have already been produced; we only get here
304  // if explicit stat information was provided.
305  // FIXME: If the name resolves to a file with different stat information,
306  // produce a better diagnostic.
307  if (!Module->MissingHeaders.empty()) {
308  auto &MissingHeader = Module->MissingHeaders.front();
309  Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
310  << MissingHeader.IsUmbrella << MissingHeader.FileName;
311  return std::error_code();
312  }
313 
314  // Add includes for each of these headers.
315  for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
316  for (Module::Header &H : Module->Headers[HK]) {
317  Module->addTopHeader(H.Entry);
318  // Use the path as specified in the module map file. We'll look for this
319  // file relative to the module build directory (the directory containing
320  // the module map file) so this will find the same file that we found
321  // while parsing the module map.
322  addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
323  }
324  }
325  // Note that Module->PrivateHeaders will not be a TopHeader.
326 
327  if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
328  Module->addTopHeader(UmbrellaHeader.Entry);
329  if (Module->Parent)
330  // Include the umbrella header for submodules.
331  addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
332  Module->IsExternC);
333  } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
334  // Add all of the headers we find in this subdirectory.
335  std::error_code EC;
336  SmallString<128> DirNative;
337  llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
338 
339  vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
340  for (vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
341  Dir != End && !EC; Dir.increment(EC)) {
342  // Check whether this entry has an extension typically associated with
343  // headers.
344  if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName()))
345  .Cases(".h", ".H", ".hh", ".hpp", true)
346  .Default(false))
347  continue;
348 
349  const FileEntry *Header = FileMgr.getFile(Dir->getName());
350  // FIXME: This shouldn't happen unless there is a file system race. Is
351  // that worth diagnosing?
352  if (!Header)
353  continue;
354 
355  // If this header is marked 'unavailable' in this module, don't include
356  // it.
357  if (ModMap.isHeaderUnavailableInModule(Header, Module))
358  continue;
359 
360  // Compute the relative path from the directory to this file.
361  SmallVector<StringRef, 16> Components;
362  auto PathIt = llvm::sys::path::rbegin(Dir->getName());
363  for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
364  Components.push_back(*PathIt);
365  SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
366  for (auto It = Components.rbegin(), End = Components.rend(); It != End;
367  ++It)
368  llvm::sys::path::append(RelativeHeader, *It);
369 
370  // Include this header as part of the umbrella directory.
371  Module->addTopHeader(Header);
372  addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC);
373  }
374 
375  if (EC)
376  return EC;
377  }
378 
379  // Recurse into submodules.
381  SubEnd = Module->submodule_end();
382  Sub != SubEnd; ++Sub)
383  if (std::error_code Err = collectModuleHeaderIncludes(
384  LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
385  return Err;
386 
387  return std::error_code();
388 }
389 
390 static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
391  bool IsPreprocessed,
392  std::string &PresumedModuleMapFile,
393  unsigned &Offset) {
394  auto &SrcMgr = CI.getSourceManager();
396 
397  // Map the current input to a file.
398  FileID ModuleMapID = SrcMgr.getMainFileID();
399  const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
400 
401  // If the module map is preprocessed, handle the initial line marker;
402  // line directives are not part of the module map syntax in general.
403  Offset = 0;
404  if (IsPreprocessed) {
405  SourceLocation EndOfLineMarker =
406  ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
407  if (EndOfLineMarker.isValid())
408  Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
409  }
410 
411  // Load the module map file.
412  if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
413  PresumedModuleMapFile))
414  return true;
415 
416  if (SrcMgr.getBuffer(ModuleMapID)->getBufferSize() == Offset)
417  Offset = 0;
418 
419  return false;
420 }
421 
423  StringRef ModuleMapFilename) {
424  if (CI.getLangOpts().CurrentModule.empty()) {
425  CI.getDiagnostics().Report(diag::err_missing_module_name);
426 
427  // FIXME: Eventually, we could consider asking whether there was just
428  // a single module described in the module map, and use that as a
429  // default. Then it would be fairly trivial to just "compile" a module
430  // map with a single module (the common case).
431  return nullptr;
432  }
433 
434  // Dig out the module definition.
437  /*AllowSearch=*/false);
438  if (!M) {
439  CI.getDiagnostics().Report(diag::err_missing_module)
440  << CI.getLangOpts().CurrentModule << ModuleMapFilename;
441 
442  return nullptr;
443  }
444 
445  // Check whether we can build this module at all.
447  CI.getDiagnostics(), M))
448  return nullptr;
449 
450  // Inform the preprocessor that includes from within the input buffer should
451  // be resolved relative to the build directory of the module map file.
453 
454  // If the module was inferred from a different module map (via an expanded
455  // umbrella module definition), track that fact.
456  // FIXME: It would be preferable to fill this in as part of processing
457  // the module map, rather than adding it after the fact.
458  StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
459  if (!OriginalModuleMapName.empty()) {
460  auto *OriginalModuleMap =
461  CI.getFileManager().getFile(OriginalModuleMapName,
462  /*openFile*/ true);
463  if (!OriginalModuleMap) {
464  CI.getDiagnostics().Report(diag::err_module_map_not_found)
465  << OriginalModuleMapName;
466  return nullptr;
467  }
468  if (OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
470  M->IsInferred = true;
472  .setInferredModuleAllowedBy(M, OriginalModuleMap);
473  }
474  }
475 
476  // If we're being run from the command-line, the module build stack will not
477  // have been filled in yet, so complete it now in order to allow us to detect
478  // module cycles.
480  if (SourceMgr.getModuleBuildStack().empty())
482  FullSourceLoc(SourceLocation(), SourceMgr));
483  return M;
484 }
485 
486 /// Compute the input buffer that should be used to build the specified module.
487 static std::unique_ptr<llvm::MemoryBuffer>
489  FileManager &FileMgr = CI.getFileManager();
490 
491  // Collect the set of #includes we need to build the module.
492  SmallString<256> HeaderContents;
493  std::error_code Err = std::error_code();
494  if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
495  addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
496  CI.getLangOpts(), M->IsExternC);
498  CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
500  HeaderContents);
501 
502  if (Err) {
503  CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
504  << M->getFullModuleName() << Err.message();
505  return nullptr;
506  }
507 
508  return llvm::MemoryBuffer::getMemBufferCopy(
509  HeaderContents, Module::getModuleInputBufferName());
510 }
511 
513  const FrontendInputFile &RealInput) {
514  FrontendInputFile Input(RealInput);
515  assert(!Instance && "Already processing a source file!");
516  assert(!Input.isEmpty() && "Unexpected empty filename!");
517  setCurrentInput(Input);
518  setCompilerInstance(&CI);
519 
520  StringRef InputFile = Input.getFile();
521  bool HasBegunSourceFile = false;
522  bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
524  if (!BeginInvocation(CI))
525  goto failure;
526 
527  // If we're replaying the build of an AST file, import it and set up
528  // the initial state from its build.
529  if (ReplayASTFile) {
531 
532  // The AST unit populates its own diagnostics engine rather than ours.
534  new DiagnosticsEngine(Diags->getDiagnosticIDs(),
535  &Diags->getDiagnosticOptions()));
536  ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
537 
538  std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
540  ASTDiags, CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs);
541  if (!AST)
542  goto failure;
543 
544  // Options relating to how we treat the input (but not what we do with it)
545  // are inherited from the AST unit.
546  CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
547  CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
548  CI.getLangOpts() = AST->getLangOpts();
549 
550  // Set the shared objects, these are reset when we finish processing the
551  // file, otherwise the CompilerInstance will happily destroy them.
552  CI.setFileManager(&AST->getFileManager());
554  CI.getSourceManager().initializeForReplay(AST->getSourceManager());
555 
556  // Preload all the module files loaded transitively by the AST unit. Also
557  // load all module map files that were parsed as part of building the AST
558  // unit.
559  if (auto ASTReader = AST->getASTReader()) {
560  auto &MM = ASTReader->getModuleManager();
561  auto &PrimaryModule = MM.getPrimaryModule();
562 
563  for (ModuleFile &MF : MM)
564  if (&MF != &PrimaryModule)
565  CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
566 
567  ASTReader->visitTopLevelModuleMaps(PrimaryModule,
568  [&](const FileEntry *FE) {
569  CI.getFrontendOpts().ModuleMapFiles.push_back(FE->getName());
570  });
571  }
572 
573  // Set up the input file for replay purposes.
574  auto Kind = AST->getInputKind();
575  if (Kind.getFormat() == InputKind::ModuleMap) {
576  Module *ASTModule =
577  AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
578  AST->getLangOpts().CurrentModule, /*AllowSearch*/ false);
579  assert(ASTModule && "module file does not define its own module");
580  Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
581  } else {
582  auto &SM = CI.getSourceManager();
583  FileID ID = SM.getMainFileID();
584  if (auto *File = SM.getFileEntryForID(ID))
585  Input = FrontendInputFile(File->getName(), Kind);
586  else
587  Input = FrontendInputFile(SM.getBuffer(ID), Kind);
588  }
589  setCurrentInput(Input, std::move(AST));
590  }
591 
592  // AST files follow a very different path, since they share objects via the
593  // AST unit.
594  if (Input.getKind().getFormat() == InputKind::Precompiled) {
595  assert(!usesPreprocessorOnly() && "this case was handled above");
596  assert(hasASTFileSupport() &&
597  "This action does not have AST file support!");
598 
600 
601  std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
602  InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
603  CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs);
604 
605  if (!AST)
606  goto failure;
607 
608  // Inform the diagnostic client we are processing a source file.
609  CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
610  HasBegunSourceFile = true;
611 
612  // Set the shared objects, these are reset when we finish processing the
613  // file, otherwise the CompilerInstance will happily destroy them.
614  CI.setFileManager(&AST->getFileManager());
615  CI.setSourceManager(&AST->getSourceManager());
616  CI.setPreprocessor(AST->getPreprocessorPtr());
617  Preprocessor &PP = CI.getPreprocessor();
619  PP.getLangOpts());
620  CI.setASTContext(&AST->getASTContext());
621 
622  setCurrentInput(Input, std::move(AST));
623 
624  // Initialize the action.
625  if (!BeginSourceFileAction(CI))
626  goto failure;
627 
628  // Create the AST consumer.
629  CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
630  if (!CI.hasASTConsumer())
631  goto failure;
632 
633  return true;
634  }
635 
636  if (!CI.hasVirtualFileSystem()) {
639  CI.getDiagnostics()))
640  CI.setVirtualFileSystem(VFS);
641  else
642  goto failure;
643  }
644 
645  // Set up the file and source managers, if needed.
646  if (!CI.hasFileManager())
647  CI.createFileManager();
648  if (!CI.hasSourceManager())
650 
651  // Set up embedding for any specified files. Do this before we load any
652  // source files, including the primary module map for the compilation.
653  for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
654  if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true))
656  else
657  CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
658  }
661 
662  // IR files bypass the rest of initialization.
663  if (Input.getKind().getLanguage() == InputKind::LLVM_IR) {
664  assert(hasIRSupport() &&
665  "This action does not have IR file support!");
666 
667  // Inform the diagnostic client we are processing a source file.
668  CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
669  HasBegunSourceFile = true;
670 
671  // Initialize the action.
672  if (!BeginSourceFileAction(CI))
673  goto failure;
674 
675  // Initialize the main file entry.
676  if (!CI.InitializeSourceManager(CurrentInput))
677  goto failure;
678 
679  return true;
680  }
681 
682  // If the implicit PCH include is actually a directory, rather than
683  // a single file, search for a suitable PCH file in that directory.
684  if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
685  FileManager &FileMgr = CI.getFileManager();
687  StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
688  std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
689  if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
690  std::error_code EC;
691  SmallString<128> DirNative;
692  llvm::sys::path::native(PCHDir->getName(), DirNative);
693  bool Found = false;
694  vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
695  for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
696  Dir != DirEnd && !EC; Dir.increment(EC)) {
697  // Check whether this is an acceptable AST file.
699  Dir->getName(), FileMgr, CI.getPCHContainerReader(),
701  SpecificModuleCachePath)) {
702  PPOpts.ImplicitPCHInclude = Dir->getName();
703  Found = true;
704  break;
705  }
706  }
707 
708  if (!Found) {
709  CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
710  goto failure;
711  }
712  }
713  }
714 
715  // Set up the preprocessor if needed. When parsing model files the
716  // preprocessor of the original source is reused.
717  if (!isModelParsingAction())
719 
720  // Inform the diagnostic client we are processing a source file.
722  &CI.getPreprocessor());
723  HasBegunSourceFile = true;
724 
725  // Initialize the main file entry.
726  if (!CI.InitializeSourceManager(Input))
727  goto failure;
728 
729  // For module map files, we first parse the module map and synthesize a
730  // "<module-includes>" buffer before more conventional processing.
731  if (Input.getKind().getFormat() == InputKind::ModuleMap) {
732  CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
733 
734  std::string PresumedModuleMapFile;
735  unsigned OffsetToContents;
736  if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
737  Input.isPreprocessed(),
738  PresumedModuleMapFile, OffsetToContents))
739  goto failure;
740 
741  auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
742  if (!CurrentModule)
743  goto failure;
744 
745  CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
746 
747  if (OffsetToContents)
748  // If the module contents are in the same file, skip to them.
749  CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
750  else {
751  // Otherwise, convert the module description to a suitable input buffer.
752  auto Buffer = getInputBufferForModule(CI, CurrentModule);
753  if (!Buffer)
754  goto failure;
755 
756  // Reinitialize the main file entry to refer to the new input.
758  Buffer.release(), Input.getKind().withFormat(InputKind::Source),
759  CurrentModule->IsSystem)))
760  goto failure;
761  }
762  }
763 
764  // Initialize the action.
765  if (!BeginSourceFileAction(CI))
766  goto failure;
767 
768  // Create the AST context and consumer unless this is a preprocessor only
769  // action.
770  if (!usesPreprocessorOnly()) {
771  // Parsing a model file should reuse the existing ASTContext.
772  if (!isModelParsingAction())
773  CI.createASTContext();
774 
775  // For preprocessed files, check if the first line specifies the original
776  // source file name with a linemarker.
777  std::string PresumedInputFile = InputFile;
778  if (Input.isPreprocessed())
779  ReadOriginalFileName(CI, PresumedInputFile);
780 
781  std::unique_ptr<ASTConsumer> Consumer =
782  CreateWrappedASTConsumer(CI, PresumedInputFile);
783  if (!Consumer)
784  goto failure;
785 
786  // FIXME: should not overwrite ASTMutationListener when parsing model files?
787  if (!isModelParsingAction())
788  CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
789 
790  if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
791  // Convert headers to PCH and chain them.
792  IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
793  source = createChainedIncludesSource(CI, FinalReader);
794  if (!source)
795  goto failure;
796  CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
797  CI.getASTContext().setExternalSource(source);
798  } else if (CI.getLangOpts().Modules ||
799  !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
800  // Use PCM or PCH.
801  assert(hasPCHSupport() && "This action does not have PCH support!");
802  ASTDeserializationListener *DeserialListener =
803  Consumer->GetASTDeserializationListener();
804  bool DeleteDeserialListener = false;
806  DeserialListener = new DeserializedDeclsDumper(DeserialListener,
807  DeleteDeserialListener);
808  DeleteDeserialListener = true;
809  }
811  DeserialListener = new DeserializedDeclsChecker(
812  CI.getASTContext(),
814  DeserialListener, DeleteDeserialListener);
815  DeleteDeserialListener = true;
816  }
817  if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
821  CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
822  DeleteDeserialListener);
823  if (!CI.getASTContext().getExternalSource())
824  goto failure;
825  }
826  // If modules are enabled, create the module manager before creating
827  // any builtins, so that all declarations know that they might be
828  // extended by an external source.
829  if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
831  CI.createModuleManager();
832  CI.getModuleManager()->setDeserializationListener(DeserialListener,
833  DeleteDeserialListener);
834  }
835  }
836 
837  CI.setASTConsumer(std::move(Consumer));
838  if (!CI.hasASTConsumer())
839  goto failure;
840  }
841 
842  // Initialize built-in info as long as we aren't using an external AST
843  // source.
844  if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
846  Preprocessor &PP = CI.getPreprocessor();
848  PP.getLangOpts());
849  } else {
850  // FIXME: If this is a problem, recover from it by creating a multiplex
851  // source.
852  assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
853  "modules enabled but created an external source that "
854  "doesn't support modules");
855  }
856 
857  // If we were asked to load any module map files, do so now.
858  for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
859  if (auto *File = CI.getFileManager().getFile(Filename))
861  File, /*IsSystem*/false);
862  else
863  CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
864  }
865 
866  // If we were asked to load any module files, do so now.
867  for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
868  if (!CI.loadModuleFile(ModuleFile))
869  goto failure;
870 
871  // If there is a layout overrides file, attach an external AST source that
872  // provides the layouts from that file.
873  if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
876  Override(new LayoutOverrideSource(
878  CI.getASTContext().setExternalSource(Override);
879  }
880 
881  return true;
882 
883  // If we failed, reset state since the client will not end up calling the
884  // matching EndSourceFile().
885 failure:
886  if (HasBegunSourceFile)
888  CI.clearOutputFiles(/*EraseFiles=*/true);
889  CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
891  setCompilerInstance(nullptr);
892  return false;
893 }
894 
897 
898  if (CI.hasFrontendTimer()) {
899  llvm::TimeRegion Timer(CI.getFrontendTimer());
900  ExecuteAction();
901  }
902  else ExecuteAction();
903 
904  // If we are supposed to rebuild the global module index, do so now unless
905  // there were any module-build failures.
906  if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
907  CI.hasPreprocessor()) {
908  StringRef Cache =
910  if (!Cache.empty())
913  }
914 
915  return true;
916 }
917 
920 
921  // Inform the diagnostic client we are done with this source file.
923 
924  // Inform the preprocessor we are done.
925  if (CI.hasPreprocessor())
927 
928  // Finalize the action.
930 
931  // Sema references the ast consumer, so reset sema first.
932  //
933  // FIXME: There is more per-file stuff we could just drop here?
934  bool DisableFree = CI.getFrontendOpts().DisableFree;
935  if (DisableFree) {
936  CI.resetAndLeakSema();
938  BuryPointer(CI.takeASTConsumer().get());
939  } else {
940  CI.setSema(nullptr);
941  CI.setASTContext(nullptr);
942  CI.setASTConsumer(nullptr);
943  }
944 
945  if (CI.getFrontendOpts().ShowStats) {
946  llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
951  llvm::errs() << "\n";
952  }
953 
954  // Cleanup the output streams, and erase the output files if instructed by the
955  // FrontendAction.
956  CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
957 
958  if (isCurrentFileAST()) {
959  if (DisableFree) {
963  BuryPointer(CurrentASTUnit.release());
964  } else {
965  CI.setPreprocessor(nullptr);
966  CI.setSourceManager(nullptr);
967  CI.setFileManager(nullptr);
968  }
969  }
970 
971  setCompilerInstance(nullptr);
973  CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
974 }
975 
978 }
979 
980 //===----------------------------------------------------------------------===//
981 // Utility Actions
982 //===----------------------------------------------------------------------===//
983 
986  if (!CI.hasPreprocessor())
987  return;
988 
989  // FIXME: Move the truncation aspect of this into Sema, we delayed this till
990  // here so the source manager would be initialized.
991  if (hasCodeCompletionSupport() &&
994 
995  // Use a code completion consumer?
996  CodeCompleteConsumer *CompletionConsumer = nullptr;
997  if (CI.hasCodeCompletionConsumer())
998  CompletionConsumer = &CI.getCodeCompletionConsumer();
999 
1000  if (!CI.hasSema())
1001  CI.createSema(getTranslationUnitKind(), CompletionConsumer);
1002 
1005 }
1006 
1007 void PluginASTAction::anchor() { }
1008 
1009 std::unique_ptr<ASTConsumer>
1011  StringRef InFile) {
1012  llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1013 }
1014 
1015 std::unique_ptr<ASTConsumer>
1017  StringRef InFile) {
1018  return WrappedAction->CreateASTConsumer(CI, InFile);
1019 }
1021  return WrappedAction->BeginInvocation(CI);
1022 }
1024  WrappedAction->setCurrentInput(getCurrentInput());
1025  WrappedAction->setCompilerInstance(&CI);
1026  auto Ret = WrappedAction->BeginSourceFileAction(CI);
1027  // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1028  setCurrentInput(WrappedAction->getCurrentInput());
1029  return Ret;
1030 }
1032  WrappedAction->ExecuteAction();
1033 }
1035  WrappedAction->EndSourceFileAction();
1036 }
1037 
1039  return WrappedAction->usesPreprocessorOnly();
1040 }
1042  return WrappedAction->getTranslationUnitKind();
1043 }
1045  return WrappedAction->hasPCHSupport();
1046 }
1048  return WrappedAction->hasASTFileSupport();
1049 }
1051  return WrappedAction->hasIRSupport();
1052 }
1054  return WrappedAction->hasCodeCompletionSupport();
1055 }
1056 
1058  std::unique_ptr<FrontendAction> WrappedAction)
1059  : WrappedAction(std::move(WrappedAction)) {}
1060 
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:266
void setExternalSource(IntrusiveRefCntPtr< ExternalASTSource > Source)
Attach an external AST source to the AST context.
Definition: ASTContext.cpp:825
Defines the clang::ASTContext interface.
virtual bool isModelParsingAction() const
Is this action invoked on a model file?
static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts, const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts, StringRef ExistingModuleCachePath)
Determine whether the given AST file is acceptable to load into a translation unit with the given lan...
Definition: ASTReader.cpp:4834
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
getSpelling - This method is used to get the spelling of a token into a preallocated buffer...
Definition: Lexer.cpp:370
LangOptions & getLangOpts()
ASTContext & getASTContext() const
std::string OriginalModuleMap
When the input is a module map, the original module map file from which that map was inferred...
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
Header getUmbrellaHeader() const
Retrieve the header that serves as the umbrella header for this module.
Definition: Module.h:441
void createCodeCompletionConsumer()
Create a code completion consumer using the invocation; note that this will cause the source manager ...
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens...
Definition: Lexer.h:46
Smart pointer class that efficiently represents Objective-C method names.
SmallVector< UnresolvedHeaderDirective, 1 > MissingHeaders
Headers that are mentioned in the module map file but could not be found on the file system...
Definition: Module.h:172
A (possibly-)qualified type.
Definition: Type.h:616
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:116
IntrusiveRefCntPtr< ExternalSemaSource > createChainedIncludesSource(CompilerInstance &CI, IntrusiveRefCntPtr< ExternalSemaSource > &Reader)
The ChainedIncludesSource class converts headers to chained PCHs in memory, mainly for testing...
void EndSourceFile()
Perform any per-file post processing, deallocate per-file objects, and run statistics and output file...
submodule_iterator submodule_begin()
Definition: Module.h:514
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "...
Definition: Module.h:212
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:123
void createSema(TranslationUnitKind TUKind, CodeCompleteConsumer *CompletionConsumer)
Create the Sema object to be used for parsing.
Load everything, including Sema.
Definition: ASTUnit.h:637
virtual bool hasCodeCompletionSupport() const
Does this action support use with code completion?
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:81
CompilerInstance & getCompilerInstance() const
void EndSourceFile()
Inform the preprocessor callbacks that processing is complete.
bool hasCodeCompletionConsumer() const
StringRef P
virtual bool usesPreprocessorOnly() const =0
Does this action only use the preprocessor?
llvm::MemoryBuffer * getBuffer(FileID FID, SourceLocation Loc, bool *Invalid=nullptr) const
Return the buffer for the specified FileID.
bool hasErrorOccurred() const
Definition: Diagnostic.h:660
llvm::Registry< PluginASTAction > FrontendPluginRegistry
The frontend plugin registry.
TypePropertyCache< Private > Cache
Definition: Type.cpp:3326
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Provide a default implementation which returns aborts; this method should never be called by Frontend...
Language getLanguage() const
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:63
std::unique_ptr< llvm::MemoryBuffer > Buffer
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1205
bool InitializeSourceManager(const FrontendInputFile &Input)
InitializeSourceManager - Initialize the source manager to set InputFile as the main file...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, bool UseDebugInfo=false, bool OnlyLocalDecls=false, ArrayRef< RemappedFile > RemappedFiles=None, bool CaptureDiagnostics=false, bool AllowPCHWithCompilerErrors=false, bool UserFilesAreVolatile=false)
Create a ASTUnit from an AST file.
Definition: ASTUnit.cpp:675
void setModuleManager(IntrusiveRefCntPtr< ASTReader > Reader)
static SourceLocation ReadOriginalFileName(CompilerInstance &CI, std::string &InputFile, bool IsModuleMap=false)
For preprocessed files, if the first line is the linemarker and specifies the original source file na...
TargetInfo & getTarget() const
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1428
SourceManager & getSourceManager() const
Return the current source manager.
virtual directory_iterator dir_begin(const Twine &Dir, std::error_code &EC)=0
Get a directory_iterator for Dir.
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:624
Builtin::Context & getBuiltinInfo()
Definition: Preprocessor.h:736
bool isHeaderUnavailableInModule(const FileEntry *Header, const Module *RequestingModule) const
Determine whether the given header is unavailable as part of the specified module.
Definition: ModuleMap.cpp:613
std::string PresumedModuleMapFile
The presumed file name for the module map defining this module.
Definition: Module.h:88
The virtual file system interface.
One of these records is kept for each identifier that is lexed.
const FrontendInputFile & getCurrentInput() const
StringRef getModuleCachePath() const
Retrieve the path to the module cache.
Definition: HeaderSearch.h:322
static StringRef getModuleInputBufferName()
Definition: Module.h:532
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
Record the location of a macro definition.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
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.
virtual bool hasASTFileSupport() const
Does this action support use with AST files?
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:725
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:158
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
void setASTContext(ASTContext *Value)
setASTContext - Replace the current AST context.
CodeGenOptions & getCodeGenOpts()
An input iterator over the recursive contents of a virtual path, similar to llvm::sys::fs::recursive_...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Compiling a module from a module map.
Definition: LangOptions.h:64
std::vector< std::string > ModulesEmbedFiles
The list of files to embed into the compiled module file.
Describes a module or submodule.
Definition: Module.h:57
Execute the action before the main action.
bool hasPCHSupport() const override
Does this action support use with PCH?
Action is determined by the cc1 command-line.
directory_iterator & increment(std::error_code &EC)
Equivalent to operator++, with an error code.
unsigned ShowStats
Show frontend performance metrics and statistics.
bool isAvailable() const
Determine whether this module is available for use within the current translation unit...
Definition: Module.h:352
FrontendOptions & getFrontendOpts()
uint32_t Offset
Definition: CacheTokens.cpp:43
ModuleManager & getModuleManager()
Retrieve the module manager.
Definition: ASTReader.h:1587
IntrusiveRefCntPtr< vfs::FileSystem > getVirtualFileSystem() const
Definition: FileManager.h:225
Load options and the preprocessor state.
Definition: ASTUnit.h:633
DiagnosticConsumer & getDiagnosticClient() const
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:147
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:731
Module * Parent
The parent of this module.
Definition: Module.h:79
unsigned IsInferred
Whether this is an inferred submodule (module * { ... }).
Definition: Module.h:215
static Module * prepareToBuildModule(CompilerInstance &CI, StringRef ModuleMapFilename)
StringRef getName() const
Definition: FileManager.h:84
Module * getCurrentModule() const
void setASTConsumer(std::unique_ptr< ASTConsumer > Value)
setASTConsumer - Replace the current AST consumer; the compiler instance takes ownership of Value...
unsigned SkipFunctionBodies
Emit ARC errors even if the migrator can fix them.
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
bool loadModuleMapFile(const FileEntry *File, bool IsSystem, FileID ID=FileID(), unsigned *Offset=nullptr, StringRef OriginalModuleMapFile=StringRef())
Read the contents of the given module map file.
submodule_iterator submodule_end()
Definition: Module.h:516
virtual bool BeginInvocation(CompilerInstance &CI)
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
tok::TokenKind getKind() const
Definition: Token.h:90
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
bool BeginInvocation(CompilerInstance &CI) override
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
HeaderSearchOptions & getHeaderSearchOpts()
detail::InMemoryDirectory::const_iterator I
bool DisablePCHValidation
When true, disables most of the normal validation performed on precompiled headers.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
DiagnosticsEngine & getDiagnostics() const
static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem, bool IsPreprocessed, std::string &PresumedModuleMapFile, unsigned &Offset)
std::vector< Module * >::iterator submodule_iterator
Definition: Module.h:511
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
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.
void initializeForReplay(const SourceManager &Old)
Initialize this source manager suitably to replay the compilation described by Old.
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
void resolveHeaderDirectives(const FileEntry *File) const
Resolve all lazy header directives for the specified file.
Definition: ModuleMap.cpp:1023
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:130
const DirectoryEntry * getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
void setVirtualFileSystem(IntrusiveRefCntPtr< vfs::FileSystem > FS)
Replace the current virtual file system.
StringRef Filename
Definition: Format.cpp:1301
virtual bool shouldEraseOutputFiles()
Callback at the end of processing a single input, to determine if the output files should be erased o...
std::vector< std::string > ChainedIncludes
Headers that will be converted to chained PCHs in memory.
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
void setCurrentInput(const FrontendInputFile &CurrentInput, std::unique_ptr< ASTUnit > AST=nullptr)
void setFileIsTransient(const FileEntry *SourceFile)
Specify that a file is transient.
bool hasIRSupport() const override
Does this action support use with IR files?
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
std::string getSpecificModuleCachePath()
WrapperFrontendAction(std::unique_ptr< FrontendAction > WrappedAction)
Construct a WrapperFrontendAction from an existing action, taking ownership of it.
StateNode * Previous
std::set< std::string > DeserializedPCHDeclsToErrorOn
This is a set of names for decls that we do not want to be deserialized, and we emit an error if they...
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Defines the clang::Preprocessor interface.
IntrusiveRefCntPtr< vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
Not compiling a module interface at all.
Definition: LangOptions.h:63
void createASTContext()
Create the AST context.
const char * getDeclKindName() const
Definition: DeclBase.cpp:102
virtual void EndSourceFileAction()
Callback at the end of processing a single input.
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
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.
unsigned ModulesEmbedAllFiles
Whether we should embed all used files into the PCM file.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:124
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
bool isNot(tok::TokenKind K) const
Definition: Token.h:96
virtual bool hasIRSupport() const
Does this action support use with IR files?
An input file for the front end.
InputKind withFormat(Format F) const
virtual std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile)=0
Create the AST consumer object for this action, if supported.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
DirectoryName getUmbrellaDir() const
Retrieve the directory for which this module serves as the umbrella.
Definition: Module.cpp:183
Information about a header directive as found in the module map file.
Definition: Module.h:135
virtual void ExecuteAction()=0
Callback to run the program action, using the initialized compiler instance.
void EndSourceFileAction() override
Callback at the end of processing a single input.
Module * lookupModule(StringRef ModuleName, bool AllowSearch=true)
Lookup a module Search for a module with the given name.
const SourceManager & SM
Definition: Format.cpp:1293
bool AllowPCHWithCompilerErrors
When true, a PCH with compiler errors will not be rejected.
const DirectoryEntry * Directory
The build directory of this module.
Definition: Module.h:84
bool hasSourceManager() const
FileSystemOptions & getFileSystemOpts()
Kind
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.
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any...
Definition: ASTContext.h:1014
bool Execute()
Set the source manager's main input file, and run the action.
std::vector< std::string > ModuleFiles
The list of additional prebuilt module files to load before processing the input. ...
ParsedSourceLocation CodeCompletionAt
If given, enable code completion at the provided location.
virtual TranslationUnitKind getTranslationUnitKind()
For AST-based actions, the kind of translation unit we're handling.
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
bool isValid() const
Return true if this is a valid SourceLocation object.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
LLVM IR: we accept this so that we can run the optimizer on it, and compile it to assembly or object ...
const std::string ID
const StringRef getCurrentFile() const
Information about a directory name as found in the module map file.
Definition: Module.h:144
IdentifierTable & getIdentifierTable()
Definition: Preprocessor.h:733
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
SmallVector< Header, 2 > Headers[5]
The headers that are part of this module.
Definition: Module.h:152
void ParseAST(Preprocessor &pp, ASTConsumer *C, ASTContext &Ctx, bool PrintStats=false, TranslationUnitKind TUKind=TU_Complete, CodeCompleteConsumer *CompletionConsumer=nullptr, bool SkipFunctionBodies=false)
Parse the entire file specified, notifying the ASTConsumer as the file is parsed. ...
Definition: ParseAST.cpp:98
static std::error_code collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag, ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl< char > &Includes)
Collect the set of header includes needed to construct the given module and update the TopHeaders fil...
void initializeBuiltins(IdentifierTable &Table, const LangOptions &LangOpts)
Mark the identifiers for all the builtins with their appropriate builtin ID # and mark any non-portab...
Definition: Builtins.cpp:81
InputKind getKind() const
std::unordered_map< std::string, std::vector< std::string > > PluginArgs
Args to pass to the plugins.
Execute the action after the main action.
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Definition: Diagnostic.h:689
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.
void createPreprocessor(TranslationUnitKind TUKind)
Create the preprocessor, using the invocation, file, and source managers, and replace any existing on...
std::string OverrideRecordLayoutsFile
File name of the file that will provide record layouts (in the format produced by -fdump-record-layou...
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...
bool hasFrontendTimer() const
void setPreprocessor(std::shared_ptr< Preprocessor > Value)
Replace the current preprocessor.
StringRef getFile() const
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:328
ModuleFile & getPrimaryModule()
Returns the primary module associated with the manager, that is, the first module loaded...
FileManager & getFileManager() const
Return the current file manager to the caller.
bool loadModuleFile(StringRef FileName)
void setASTMutationListener(ASTMutationListener *Listener)
Attach an AST mutation listener to the AST context.
Definition: ASTContext.h:1023
void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, bool IsFileEntry, bool IsFileExit, SrcMgr::CharacteristicKind FileKind)
Add a line note to the line table for the FileID and offset specified by Loc.
bool isCurrentFileAST() const
void addTopHeader(const FileEntry *File)
Add a top-level header associated with this module.
Definition: Module.h:454
SourceMgr(SourceMgr)
Format getFormat() const
virtual bool hasPCHSupport() const
Does this action support use with PCH?
void clearOutputFiles(bool EraseFiles)
clearOutputFiles - Clear the output file list.
void BuryPointer(const void *Ptr)
bool hasASTFileSupport() const override
Does this action support use with AST files?
void visitTopLevelModuleMaps(serialization::ModuleFile &MF, llvm::function_ref< void(const FileEntry *)> Visitor)
Visit all the top-level module maps loaded when building the given module file.
Definition: ASTReader.cpp:8965
void setCompilerInstance(CompilerInstance *Value)
void PrintStats() const
Print some statistics to stderr that indicate how well the hashing is doing.
CodeCompleteConsumer & getCodeCompletionConsumer() const
std::vector< std::string > AddPluginActions
The list of plugin actions to run in addition to the normal action.
Cached information about one directory (either on disk or in the virtual file system).
Definition: FileManager.h:45
unsigned DisableFree
Disable memory freeing on exit.
The type-property cache.
Definition: Type.cpp:3286
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
An external AST source that overrides the layout of a specified set of record types.
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
Definition: ASTBitCodes.h:157
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Definition: ASTBitCodes.h:142
void setMainFileDir(const DirectoryEntry *Dir)
Set the directory in which the main file should be considered to have been found, if it is not a real...
llvm::Timer & getFrontendTimer() const
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:237
StringLiteralParser - This decodes string escape characters and performs wide string analysis and Tra...
const StringRef Input
void PrintStats() const
Print statistics to stderr.
A SourceLocation and its associated SourceManager.
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
static std::unique_ptr< llvm::MemoryBuffer > getInputBufferForModule(CompilerInstance &CI, Module *M)
Compute the input buffer that should be used to build the specified module.
void setAllFilesAreTransient(bool Transient)
Specify that all files that are read during this compilation are transient.
void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine)
Instruct the preprocessor to skip part of the main source file.
SourceLocation getLocation() const
Definition: DeclBase.h:407
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
bool DumpDeserializedPCHDecls
Dump declarations that are deserialized from PCH, for testing.
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
virtual void BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP=nullptr)
Callback to inform the diagnostic client that processing of a source file is beginning.
Definition: Diagnostic.h:1420
static SmallVectorImpl< char > & operator+=(SmallVectorImpl< char > &Includes, StringRef RHS)
void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap)
Definition: ModuleMap.cpp:1103
TargetOptions & getTargetOpts()
This class handles loading and caching of source files into memory.
static void addHeaderInclude(StringRef HeaderName, SmallVectorImpl< char > &Includes, const LangOptions &LangOpts, bool IsExternC)
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:83
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:98
std::vector< std::string > ModuleMapFiles
The list of module map files to load before processing the input.