1 //===--- FrontendAction.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/FrontendAction.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/DeclGroup.h"
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/Frontend/FrontendPluginRegistry.h"
18 #include "clang/Frontend/LayoutOverrideSource.h"
19 #include "clang/Frontend/MultiplexConsumer.h"
20 #include "clang/Frontend/Utils.h"
21 #include "clang/Lex/HeaderSearch.h"
22 #include "clang/Lex/LiteralSupport.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Lex/PreprocessorOptions.h"
25 #include "clang/Parse/ParseAST.h"
26 #include "clang/Serialization/ASTDeserializationListener.h"
27 #include "clang/Serialization/ASTReader.h"
28 #include "clang/Serialization/GlobalModuleIndex.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;
37 LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
41 class DelegatingDeserializationListener : public ASTDeserializationListener {
42 ASTDeserializationListener *Previous;
46 explicit DelegatingDeserializationListener(
47 ASTDeserializationListener *Previous, bool DeletePrevious)
48 : Previous(Previous), DeletePrevious(DeletePrevious) {}
49 ~DelegatingDeserializationListener() override {
54 void ReaderInitialized(ASTReader *Reader) override {
56 Previous->ReaderInitialized(Reader);
58 void IdentifierRead(serialization::IdentID ID,
59 IdentifierInfo *II) override {
61 Previous->IdentifierRead(ID, II);
63 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
65 Previous->TypeRead(Idx, T);
67 void DeclRead(serialization::DeclID ID, const Decl *D) override {
69 Previous->DeclRead(ID, D);
71 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
73 Previous->SelectorRead(ID, Sel);
75 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
76 MacroDefinitionRecord *MD) override {
78 Previous->MacroDefinitionRead(PPID, MD);
82 /// \brief Dumps deserialized declarations.
83 class DeserializedDeclsDumper : public DelegatingDeserializationListener {
85 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
87 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
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;
95 DelegatingDeserializationListener::DeclRead(ID, D);
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 {
103 std::set<std::string> NamesToCheck;
106 DeserializedDeclsChecker(ASTContext &Ctx,
107 const std::set<std::string> &NamesToCheck,
108 ASTDeserializationListener *Previous,
110 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
111 NamesToCheck(NamesToCheck) {}
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()) {
117 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
118 "%0 was deserialized");
119 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
120 << ND->getNameAsString();
123 DelegatingDeserializationListener::DeclRead(ID, D);
127 } // end anonymous namespace
129 FrontendAction::FrontendAction() : Instance(nullptr) {}
131 FrontendAction::~FrontendAction() {}
133 void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
134 std::unique_ptr<ASTUnit> AST) {
135 this->CurrentInput = CurrentInput;
136 CurrentASTUnit = std::move(AST);
139 Module *FrontendAction::getCurrentModule() const {
140 CompilerInstance &CI = getCompilerInstance();
141 return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
142 CI.getLangOpts().CurrentModule, /*AllowSearch*/false);
145 std::unique_ptr<ASTConsumer>
146 FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
148 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
152 // If there are no registered plugins we don't need to wrap the consumer
153 if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
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();
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();
170 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
171 ActionType = PluginASTAction::AddAfterMainAction;
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));
183 AfterConsumers.push_back(std::move(PluginConsumer));
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));
194 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
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.
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.)
206 static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
207 std::string &InputFile,
208 bool IsModuleMap = false) {
209 auto &SourceMgr = CI.getSourceManager();
210 auto MainFileID = SourceMgr.getMainFileID();
212 bool Invalid = false;
213 const auto *MainFileBuf = SourceMgr.getBuffer(MainFileID, &Invalid);
215 return SourceLocation();
217 std::unique_ptr<Lexer> RawLexer(
218 new Lexer(MainFileID, MainFileBuf, SourceMgr, CI.getLangOpts()));
220 // If the first line has the syntax of
224 // we use FILENAME as the input file name.
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();
233 SourceLocation LineNoLoc = T.getLocation();
235 llvm::SmallString<16> Buffer;
236 if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
237 .getAsInteger(10, LineNo))
238 return SourceLocation();
241 RawLexer->LexFromRawLexer(T);
242 if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
243 return SourceLocation();
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();
254 CI.getSourceManager().AddLineNote(
255 LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
256 false, SrcMgr::C_User_ModuleMap);
258 return T.getLocation();
261 static SmallVectorImpl<char> &
262 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
263 Includes.append(RHS.begin(), RHS.end());
267 static void addHeaderInclude(StringRef HeaderName,
268 SmallVectorImpl<char> &Includes,
269 const LangOptions &LangOpts,
271 if (IsExternC && LangOpts.CPlusPlus)
272 Includes += "extern \"C\" {\n";
274 Includes += "#import \"";
276 Includes += "#include \"";
278 Includes += HeaderName;
281 if (IsExternC && LangOpts.CPlusPlus)
285 /// \brief Collect the set of header includes needed to construct the given
286 /// module and update the TopHeaders file set of the module.
288 /// \param Module The module we're collecting includes from.
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();
299 // Resolve all lazy header directives to header files.
300 ModMap.resolveHeaderDirectives(Module);
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();
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);
325 // Note that Module->PrivateHeaders will not be a TopHeader.
327 if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
328 Module->addTopHeader(UmbrellaHeader.Entry);
330 // Include the umbrella header for submodules.
331 addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
333 } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
334 // Add all of the headers we find in this subdirectory.
336 SmallString<128> DirNative;
337 llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
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
344 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName()))
345 .Cases(".h", ".H", ".hh", ".hpp", true)
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?
355 // If this header is marked 'unavailable' in this module, don't include
357 if (ModMap.isHeaderUnavailableInModule(Header, Module))
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;
368 llvm::sys::path::append(RelativeHeader, *It);
370 // Include this header as part of the umbrella directory.
371 Module->addTopHeader(Header);
372 addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC);
379 // Recurse into submodules.
380 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
381 SubEnd = Module->submodule_end();
382 Sub != SubEnd; ++Sub)
383 if (std::error_code Err = collectModuleHeaderIncludes(
384 LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
387 return std::error_code();
390 static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
392 std::string &PresumedModuleMapFile,
394 auto &SrcMgr = CI.getSourceManager();
395 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
397 // Map the current input to a file.
398 FileID ModuleMapID = SrcMgr.getMainFileID();
399 const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
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.
404 if (IsPreprocessed) {
405 SourceLocation EndOfLineMarker =
406 ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
407 if (EndOfLineMarker.isValid())
408 Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
411 // Load the module map file.
412 if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
413 PresumedModuleMapFile))
416 if (SrcMgr.getBuffer(ModuleMapID)->getBufferSize() == Offset)
422 static Module *prepareToBuildModule(CompilerInstance &CI,
423 StringRef ModuleMapFilename) {
424 if (CI.getLangOpts().CurrentModule.empty()) {
425 CI.getDiagnostics().Report(diag::err_missing_module_name);
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).
434 // Dig out the module definition.
435 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
436 Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule,
437 /*AllowSearch=*/false);
439 CI.getDiagnostics().Report(diag::err_missing_module)
440 << CI.getLangOpts().CurrentModule << ModuleMapFilename;
445 // Check whether we can build this module at all.
446 if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
447 CI.getDiagnostics(), M))
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.
452 CI.getPreprocessor().setMainFileDir(M->Directory);
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,
463 if (!OriginalModuleMap) {
464 CI.getDiagnostics().Report(diag::err_module_map_not_found)
465 << OriginalModuleMapName;
468 if (OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
469 CI.getSourceManager().getMainFileID())) {
470 M->IsInferred = true;
471 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
472 .setInferredModuleAllowedBy(M, OriginalModuleMap);
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
479 SourceManager &SourceMgr = CI.getSourceManager();
480 if (SourceMgr.getModuleBuildStack().empty())
481 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
482 FullSourceLoc(SourceLocation(), SourceMgr));
486 /// Compute the input buffer that should be used to build the specified module.
487 static std::unique_ptr<llvm::MemoryBuffer>
488 getInputBufferForModule(CompilerInstance &CI, Module *M) {
489 FileManager &FileMgr = CI.getFileManager();
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);
497 Err = collectModuleHeaderIncludes(
498 CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
499 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
503 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
504 << M->getFullModuleName() << Err.message();
508 return llvm::MemoryBuffer::getMemBufferCopy(
509 HeaderContents, Module::getModuleInputBufferName());
512 bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
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);
520 StringRef InputFile = Input.getFile();
521 bool HasBegunSourceFile = false;
522 bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
523 usesPreprocessorOnly();
524 if (!BeginInvocation(CI))
527 // If we're replaying the build of an AST file, import it and set up
528 // the initial state from its build.
530 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
532 // The AST unit populates its own diagnostics engine rather than ours.
533 IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
534 new DiagnosticsEngine(Diags->getDiagnosticIDs(),
535 &Diags->getDiagnosticOptions()));
536 ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
538 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
539 InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly,
540 ASTDiags, CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs);
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();
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());
553 CI.createSourceManager(CI.getFileManager());
554 CI.getSourceManager().initializeForReplay(AST->getSourceManager());
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
559 if (auto ASTReader = AST->getASTReader()) {
560 auto &MM = ASTReader->getModuleManager();
561 auto &PrimaryModule = MM.getPrimaryModule();
563 for (ModuleFile &MF : MM)
564 if (&MF != &PrimaryModule)
565 CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
567 ASTReader->visitTopLevelModuleMaps(PrimaryModule,
568 [&](const FileEntry *FE) {
569 CI.getFrontendOpts().ModuleMapFiles.push_back(FE->getName());
573 // Set up the input file for replay purposes.
574 auto Kind = AST->getInputKind();
575 if (Kind.getFormat() == InputKind::ModuleMap) {
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);
582 auto &SM = CI.getSourceManager();
583 FileID ID = SM.getMainFileID();
584 if (auto *File = SM.getFileEntryForID(ID))
585 Input = FrontendInputFile(File->getName(), Kind);
587 Input = FrontendInputFile(SM.getBuffer(ID), Kind);
589 setCurrentInput(Input, std::move(AST));
592 // AST files follow a very different path, since they share objects via the
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!");
599 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
601 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
602 InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
603 CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs);
608 // Inform the diagnostic client we are processing a source file.
609 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
610 HasBegunSourceFile = true;
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();
618 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
620 CI.setASTContext(&AST->getASTContext());
622 setCurrentInput(Input, std::move(AST));
624 // Initialize the action.
625 if (!BeginSourceFileAction(CI))
628 // Create the AST consumer.
629 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
630 if (!CI.hasASTConsumer())
636 // Set up the file and source managers, if needed.
637 if (!CI.hasFileManager()) {
638 if (!CI.createFileManager()) {
642 if (!CI.hasSourceManager())
643 CI.createSourceManager(CI.getFileManager());
645 // Set up embedding for any specified files. Do this before we load any
646 // source files, including the primary module map for the compilation.
647 for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
648 if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true))
649 CI.getSourceManager().setFileIsTransient(FE);
651 CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
653 if (CI.getFrontendOpts().ModulesEmbedAllFiles)
654 CI.getSourceManager().setAllFilesAreTransient(true);
656 // IR files bypass the rest of initialization.
657 if (Input.getKind().getLanguage() == InputKind::LLVM_IR) {
658 assert(hasIRSupport() &&
659 "This action does not have IR file support!");
661 // Inform the diagnostic client we are processing a source file.
662 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
663 HasBegunSourceFile = true;
665 // Initialize the action.
666 if (!BeginSourceFileAction(CI))
669 // Initialize the main file entry.
670 if (!CI.InitializeSourceManager(CurrentInput))
676 // If the implicit PCH include is actually a directory, rather than
677 // a single file, search for a suitable PCH file in that directory.
678 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
679 FileManager &FileMgr = CI.getFileManager();
680 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
681 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
682 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
683 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
685 SmallString<128> DirNative;
686 llvm::sys::path::native(PCHDir->getName(), DirNative);
688 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
689 for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
690 Dir != DirEnd && !EC; Dir.increment(EC)) {
691 // Check whether this is an acceptable AST file.
692 if (ASTReader::isAcceptableASTFile(
693 Dir->getName(), FileMgr, CI.getPCHContainerReader(),
694 CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
695 SpecificModuleCachePath)) {
696 PPOpts.ImplicitPCHInclude = Dir->getName();
703 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
709 // Set up the preprocessor if needed. When parsing model files the
710 // preprocessor of the original source is reused.
711 if (!isModelParsingAction())
712 CI.createPreprocessor(getTranslationUnitKind());
714 // Inform the diagnostic client we are processing a source file.
715 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
716 &CI.getPreprocessor());
717 HasBegunSourceFile = true;
719 // Initialize the main file entry.
720 if (!CI.InitializeSourceManager(Input))
723 // For module map files, we first parse the module map and synthesize a
724 // "<module-includes>" buffer before more conventional processing.
725 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
726 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
728 std::string PresumedModuleMapFile;
729 unsigned OffsetToContents;
730 if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
731 Input.isPreprocessed(),
732 PresumedModuleMapFile, OffsetToContents))
735 auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
739 CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
741 if (OffsetToContents)
742 // If the module contents are in the same file, skip to them.
743 CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
745 // Otherwise, convert the module description to a suitable input buffer.
746 auto Buffer = getInputBufferForModule(CI, CurrentModule);
750 // Reinitialize the main file entry to refer to the new input.
751 auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
752 auto &SourceMgr = CI.getSourceManager();
753 auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
754 assert(BufferID.isValid() && "couldn't creaate module buffer ID");
755 SourceMgr.setMainFileID(BufferID);
759 // Initialize the action.
760 if (!BeginSourceFileAction(CI))
763 // Create the AST context and consumer unless this is a preprocessor only
765 if (!usesPreprocessorOnly()) {
766 // Parsing a model file should reuse the existing ASTContext.
767 if (!isModelParsingAction())
768 CI.createASTContext();
770 // For preprocessed files, check if the first line specifies the original
771 // source file name with a linemarker.
772 std::string PresumedInputFile = InputFile;
773 if (Input.isPreprocessed())
774 ReadOriginalFileName(CI, PresumedInputFile);
776 std::unique_ptr<ASTConsumer> Consumer =
777 CreateWrappedASTConsumer(CI, PresumedInputFile);
781 // FIXME: should not overwrite ASTMutationListener when parsing model files?
782 if (!isModelParsingAction())
783 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
785 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
786 // Convert headers to PCH and chain them.
787 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
788 source = createChainedIncludesSource(CI, FinalReader);
791 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
792 CI.getASTContext().setExternalSource(source);
793 } else if (CI.getLangOpts().Modules ||
794 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
796 assert(hasPCHSupport() && "This action does not have PCH support!");
797 ASTDeserializationListener *DeserialListener =
798 Consumer->GetASTDeserializationListener();
799 bool DeleteDeserialListener = false;
800 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
801 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
802 DeleteDeserialListener);
803 DeleteDeserialListener = true;
805 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
806 DeserialListener = new DeserializedDeclsChecker(
808 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
809 DeserialListener, DeleteDeserialListener);
810 DeleteDeserialListener = true;
812 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
813 CI.createPCHExternalASTSource(
814 CI.getPreprocessorOpts().ImplicitPCHInclude,
815 CI.getPreprocessorOpts().DisablePCHValidation,
816 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
817 DeleteDeserialListener);
818 if (!CI.getASTContext().getExternalSource())
821 // If modules are enabled, create the module manager before creating
822 // any builtins, so that all declarations know that they might be
823 // extended by an external source.
824 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
825 !CI.getASTContext().getExternalSource()) {
826 CI.createModuleManager();
827 CI.getModuleManager()->setDeserializationListener(DeserialListener,
828 DeleteDeserialListener);
832 CI.setASTConsumer(std::move(Consumer));
833 if (!CI.hasASTConsumer())
837 // Initialize built-in info as long as we aren't using an external AST
839 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
840 !CI.getASTContext().getExternalSource()) {
841 Preprocessor &PP = CI.getPreprocessor();
842 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
845 // FIXME: If this is a problem, recover from it by creating a multiplex
847 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
848 "modules enabled but created an external source that "
849 "doesn't support modules");
852 // If we were asked to load any module map files, do so now.
853 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
854 if (auto *File = CI.getFileManager().getFile(Filename))
855 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
856 File, /*IsSystem*/false);
858 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
861 // If we were asked to load any module files, do so now.
862 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
863 if (!CI.loadModuleFile(ModuleFile))
866 // If there is a layout overrides file, attach an external AST source that
867 // provides the layouts from that file.
868 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
869 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
870 IntrusiveRefCntPtr<ExternalASTSource>
871 Override(new LayoutOverrideSource(
872 CI.getFrontendOpts().OverrideRecordLayoutsFile));
873 CI.getASTContext().setExternalSource(Override);
878 // If we failed, reset state since the client will not end up calling the
879 // matching EndSourceFile().
881 if (HasBegunSourceFile)
882 CI.getDiagnosticClient().EndSourceFile();
883 CI.clearOutputFiles(/*EraseFiles=*/true);
884 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
885 setCurrentInput(FrontendInputFile());
886 setCompilerInstance(nullptr);
890 bool FrontendAction::Execute() {
891 CompilerInstance &CI = getCompilerInstance();
893 if (CI.hasFrontendTimer()) {
894 llvm::TimeRegion Timer(CI.getFrontendTimer());
897 else ExecuteAction();
899 // If we are supposed to rebuild the global module index, do so now unless
900 // there were any module-build failures.
901 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
902 CI.hasPreprocessor()) {
904 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
906 GlobalModuleIndex::writeIndex(CI.getFileManager(),
907 CI.getPCHContainerReader(), Cache);
913 void FrontendAction::EndSourceFile() {
914 CompilerInstance &CI = getCompilerInstance();
916 // Inform the diagnostic client we are done with this source file.
917 CI.getDiagnosticClient().EndSourceFile();
919 // Inform the preprocessor we are done.
920 if (CI.hasPreprocessor())
921 CI.getPreprocessor().EndSourceFile();
923 // Finalize the action.
924 EndSourceFileAction();
926 // Sema references the ast consumer, so reset sema first.
928 // FIXME: There is more per-file stuff we could just drop here?
929 bool DisableFree = CI.getFrontendOpts().DisableFree;
931 CI.resetAndLeakSema();
932 CI.resetAndLeakASTContext();
933 BuryPointer(CI.takeASTConsumer().get());
936 CI.setASTContext(nullptr);
937 CI.setASTConsumer(nullptr);
940 if (CI.getFrontendOpts().ShowStats) {
941 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
942 CI.getPreprocessor().PrintStats();
943 CI.getPreprocessor().getIdentifierTable().PrintStats();
944 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
945 CI.getSourceManager().PrintStats();
946 llvm::errs() << "\n";
949 // Cleanup the output streams, and erase the output files if instructed by the
951 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
953 if (isCurrentFileAST()) {
955 CI.resetAndLeakPreprocessor();
956 CI.resetAndLeakSourceManager();
957 CI.resetAndLeakFileManager();
958 BuryPointer(CurrentASTUnit.release());
960 CI.setPreprocessor(nullptr);
961 CI.setSourceManager(nullptr);
962 CI.setFileManager(nullptr);
966 setCompilerInstance(nullptr);
967 setCurrentInput(FrontendInputFile());
968 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
971 bool FrontendAction::shouldEraseOutputFiles() {
972 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
975 //===----------------------------------------------------------------------===//
977 //===----------------------------------------------------------------------===//
979 void ASTFrontendAction::ExecuteAction() {
980 CompilerInstance &CI = getCompilerInstance();
981 if (!CI.hasPreprocessor())
984 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
985 // here so the source manager would be initialized.
986 if (hasCodeCompletionSupport() &&
987 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
988 CI.createCodeCompletionConsumer();
990 // Use a code completion consumer?
991 CodeCompleteConsumer *CompletionConsumer = nullptr;
992 if (CI.hasCodeCompletionConsumer())
993 CompletionConsumer = &CI.getCodeCompletionConsumer();
996 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
998 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
999 CI.getFrontendOpts().SkipFunctionBodies);
1002 void PluginASTAction::anchor() { }
1004 std::unique_ptr<ASTConsumer>
1005 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1007 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1010 std::unique_ptr<ASTConsumer>
1011 WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1013 return WrappedAction->CreateASTConsumer(CI, InFile);
1015 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
1016 return WrappedAction->BeginInvocation(CI);
1018 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
1019 WrappedAction->setCurrentInput(getCurrentInput());
1020 WrappedAction->setCompilerInstance(&CI);
1021 auto Ret = WrappedAction->BeginSourceFileAction(CI);
1022 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1023 setCurrentInput(WrappedAction->getCurrentInput());
1026 void WrapperFrontendAction::ExecuteAction() {
1027 WrappedAction->ExecuteAction();
1029 void WrapperFrontendAction::EndSourceFileAction() {
1030 WrappedAction->EndSourceFileAction();
1033 bool WrapperFrontendAction::usesPreprocessorOnly() const {
1034 return WrappedAction->usesPreprocessorOnly();
1036 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
1037 return WrappedAction->getTranslationUnitKind();
1039 bool WrapperFrontendAction::hasPCHSupport() const {
1040 return WrappedAction->hasPCHSupport();
1042 bool WrapperFrontendAction::hasASTFileSupport() const {
1043 return WrappedAction->hasASTFileSupport();
1045 bool WrapperFrontendAction::hasIRSupport() const {
1046 return WrappedAction->hasIRSupport();
1048 bool WrapperFrontendAction::hasCodeCompletionSupport() const {
1049 return WrappedAction->hasCodeCompletionSupport();
1052 WrapperFrontendAction::WrapperFrontendAction(
1053 std::unique_ptr<FrontendAction> WrappedAction)
1054 : WrappedAction(std::move(WrappedAction)) {}