1 //===--- FrontendAction.cpp -----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Frontend/FrontendAction.h"
10 #include "clang/AST/ASTConsumer.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclGroup.h"
13 #include "clang/Basic/Builtins.h"
14 #include "clang/Basic/LangStandard.h"
15 #include "clang/Frontend/ASTUnit.h"
16 #include "clang/Frontend/CompilerInstance.h"
17 #include "clang/Frontend/FrontendDiagnostic.h"
18 #include "clang/Frontend/FrontendPluginRegistry.h"
19 #include "clang/Frontend/LayoutOverrideSource.h"
20 #include "clang/Frontend/MultiplexConsumer.h"
21 #include "clang/Frontend/Utils.h"
22 #include "clang/Lex/HeaderSearch.h"
23 #include "clang/Lex/LiteralSupport.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Lex/PreprocessorOptions.h"
26 #include "clang/Parse/ParseAST.h"
27 #include "clang/Serialization/ASTDeserializationListener.h"
28 #include "clang/Serialization/ASTReader.h"
29 #include "clang/Serialization/GlobalModuleIndex.h"
30 #include "llvm/Support/BuryPointer.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/Timer.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include <system_error>
37 using namespace clang;
39 LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
43 class DelegatingDeserializationListener : public ASTDeserializationListener {
44 ASTDeserializationListener *Previous;
48 explicit DelegatingDeserializationListener(
49 ASTDeserializationListener *Previous, bool DeletePrevious)
50 : Previous(Previous), DeletePrevious(DeletePrevious) {}
51 ~DelegatingDeserializationListener() override {
56 void ReaderInitialized(ASTReader *Reader) override {
58 Previous->ReaderInitialized(Reader);
60 void IdentifierRead(serialization::IdentID ID,
61 IdentifierInfo *II) override {
63 Previous->IdentifierRead(ID, II);
65 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
67 Previous->TypeRead(Idx, T);
69 void DeclRead(serialization::DeclID ID, const Decl *D) override {
71 Previous->DeclRead(ID, D);
73 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
75 Previous->SelectorRead(ID, Sel);
77 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
78 MacroDefinitionRecord *MD) override {
80 Previous->MacroDefinitionRead(PPID, MD);
84 /// Dumps deserialized declarations.
85 class DeserializedDeclsDumper : public DelegatingDeserializationListener {
87 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
89 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
91 void DeclRead(serialization::DeclID ID, const Decl *D) override {
92 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
93 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
94 llvm::outs() << " - ";
95 ND->printQualifiedName(llvm::outs());
99 DelegatingDeserializationListener::DeclRead(ID, D);
103 /// Checks deserialized declarations and emits error if a name
104 /// matches one given in command-line using -error-on-deserialized-decl.
105 class DeserializedDeclsChecker : public DelegatingDeserializationListener {
107 std::set<std::string> NamesToCheck;
110 DeserializedDeclsChecker(ASTContext &Ctx,
111 const std::set<std::string> &NamesToCheck,
112 ASTDeserializationListener *Previous,
114 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
115 NamesToCheck(NamesToCheck) {}
117 void DeclRead(serialization::DeclID ID, const Decl *D) override {
118 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
119 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
121 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
122 "%0 was deserialized");
123 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
124 << ND->getNameAsString();
127 DelegatingDeserializationListener::DeclRead(ID, D);
131 } // end anonymous namespace
133 FrontendAction::FrontendAction() : Instance(nullptr) {}
135 FrontendAction::~FrontendAction() {}
137 void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
138 std::unique_ptr<ASTUnit> AST) {
139 this->CurrentInput = CurrentInput;
140 CurrentASTUnit = std::move(AST);
143 Module *FrontendAction::getCurrentModule() const {
144 CompilerInstance &CI = getCompilerInstance();
145 return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
146 CI.getLangOpts().CurrentModule, /*AllowSearch*/false);
149 std::unique_ptr<ASTConsumer>
150 FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
152 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
156 // Validate -add-plugin args.
157 bool FoundAllPlugins = true;
158 for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
160 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
161 ie = FrontendPluginRegistry::end();
163 if (it->getName() == Arg)
167 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg;
168 FoundAllPlugins = false;
171 if (!FoundAllPlugins)
174 // If there are no registered plugins we don't need to wrap the consumer
175 if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
178 // If this is a code completion run, avoid invoking the plugin consumers
179 if (CI.hasCodeCompletionConsumer())
182 // Collect the list of plugins that go before the main action (in Consumers)
183 // or after it (in AfterConsumers)
184 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
185 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
186 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
187 ie = FrontendPluginRegistry::end();
189 std::unique_ptr<PluginASTAction> P = it->instantiate();
190 PluginASTAction::ActionType ActionType = P->getActionType();
191 if (ActionType == PluginASTAction::Cmdline) {
192 // This is O(|plugins| * |add_plugins|), but since both numbers are
193 // way below 50 in practice, that's ok.
194 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
196 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
197 ActionType = PluginASTAction::AddAfterMainAction;
202 if ((ActionType == PluginASTAction::AddBeforeMainAction ||
203 ActionType == PluginASTAction::AddAfterMainAction) &&
204 P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) {
205 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
206 if (ActionType == PluginASTAction::AddBeforeMainAction) {
207 Consumers.push_back(std::move(PluginConsumer));
209 AfterConsumers.push_back(std::move(PluginConsumer));
214 // Add to Consumers the main consumer, then all the plugins that go after it
215 Consumers.push_back(std::move(Consumer));
216 for (auto &C : AfterConsumers) {
217 Consumers.push_back(std::move(C));
220 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
223 /// For preprocessed files, if the first line is the linemarker and specifies
224 /// the original source file name, use that name as the input file name.
225 /// Returns the location of the first token after the line marker directive.
227 /// \param CI The compiler instance.
228 /// \param InputFile Populated with the filename from the line marker.
229 /// \param IsModuleMap If \c true, add a line note corresponding to this line
230 /// directive. (We need to do this because the directive will not be
231 /// visited by the preprocessor.)
232 static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
233 std::string &InputFile,
234 bool IsModuleMap = false) {
235 auto &SourceMgr = CI.getSourceManager();
236 auto MainFileID = SourceMgr.getMainFileID();
238 bool Invalid = false;
239 const auto *MainFileBuf = SourceMgr.getBuffer(MainFileID, &Invalid);
241 return SourceLocation();
243 std::unique_ptr<Lexer> RawLexer(
244 new Lexer(MainFileID, MainFileBuf, SourceMgr, CI.getLangOpts()));
246 // If the first line has the syntax of
250 // we use FILENAME as the input file name.
252 if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash)
253 return SourceLocation();
254 if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
255 T.getKind() != tok::numeric_constant)
256 return SourceLocation();
259 SourceLocation LineNoLoc = T.getLocation();
261 llvm::SmallString<16> Buffer;
262 if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
263 .getAsInteger(10, LineNo))
264 return SourceLocation();
267 RawLexer->LexFromRawLexer(T);
268 if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
269 return SourceLocation();
271 StringLiteralParser Literal(T, CI.getPreprocessor());
272 if (Literal.hadError)
273 return SourceLocation();
274 RawLexer->LexFromRawLexer(T);
275 if (T.isNot(tok::eof) && !T.isAtStartOfLine())
276 return SourceLocation();
277 InputFile = Literal.GetString().str();
280 CI.getSourceManager().AddLineNote(
281 LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
282 false, SrcMgr::C_User_ModuleMap);
284 return T.getLocation();
287 static SmallVectorImpl<char> &
288 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
289 Includes.append(RHS.begin(), RHS.end());
293 static void addHeaderInclude(StringRef HeaderName,
294 SmallVectorImpl<char> &Includes,
295 const LangOptions &LangOpts,
297 if (IsExternC && LangOpts.CPlusPlus)
298 Includes += "extern \"C\" {\n";
300 Includes += "#import \"";
302 Includes += "#include \"";
304 Includes += HeaderName;
307 if (IsExternC && LangOpts.CPlusPlus)
311 /// Collect the set of header includes needed to construct the given
312 /// module and update the TopHeaders file set of the module.
314 /// \param Module The module we're collecting includes from.
316 /// \param Includes Will be augmented with the set of \#includes or \#imports
317 /// needed to load all of the named headers.
318 static std::error_code collectModuleHeaderIncludes(
319 const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
320 ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
321 // Don't collect any headers for unavailable modules.
322 if (!Module->isAvailable())
323 return std::error_code();
325 // Resolve all lazy header directives to header files.
326 ModMap.resolveHeaderDirectives(Module);
328 // If any headers are missing, we can't build this module. In most cases,
329 // diagnostics for this should have already been produced; we only get here
330 // if explicit stat information was provided.
331 // FIXME: If the name resolves to a file with different stat information,
332 // produce a better diagnostic.
333 if (!Module->MissingHeaders.empty()) {
334 auto &MissingHeader = Module->MissingHeaders.front();
335 Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
336 << MissingHeader.IsUmbrella << MissingHeader.FileName;
337 return std::error_code();
340 // Add includes for each of these headers.
341 for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
342 for (Module::Header &H : Module->Headers[HK]) {
343 Module->addTopHeader(H.Entry);
344 // Use the path as specified in the module map file. We'll look for this
345 // file relative to the module build directory (the directory containing
346 // the module map file) so this will find the same file that we found
347 // while parsing the module map.
348 addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
351 // Note that Module->PrivateHeaders will not be a TopHeader.
353 if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
354 Module->addTopHeader(UmbrellaHeader.Entry);
356 // Include the umbrella header for submodules.
357 addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
359 } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
360 // Add all of the headers we find in this subdirectory.
362 SmallString<128> DirNative;
363 llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
365 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
366 for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
367 Dir != End && !EC; Dir.increment(EC)) {
368 // Check whether this entry has an extension typically associated with
370 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
371 .Cases(".h", ".H", ".hh", ".hpp", true)
375 auto Header = FileMgr.getFile(Dir->path());
376 // FIXME: This shouldn't happen unless there is a file system race. Is
377 // that worth diagnosing?
381 // If this header is marked 'unavailable' in this module, don't include
383 if (ModMap.isHeaderUnavailableInModule(*Header, Module))
386 // Compute the relative path from the directory to this file.
387 SmallVector<StringRef, 16> Components;
388 auto PathIt = llvm::sys::path::rbegin(Dir->path());
389 for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
390 Components.push_back(*PathIt);
391 SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
392 for (auto It = Components.rbegin(), End = Components.rend(); It != End;
394 llvm::sys::path::append(RelativeHeader, *It);
396 // Include this header as part of the umbrella directory.
397 Module->addTopHeader(*Header);
398 addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC);
405 // Recurse into submodules.
406 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
407 SubEnd = Module->submodule_end();
408 Sub != SubEnd; ++Sub)
409 if (std::error_code Err = collectModuleHeaderIncludes(
410 LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
413 return std::error_code();
416 static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
418 std::string &PresumedModuleMapFile,
420 auto &SrcMgr = CI.getSourceManager();
421 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
423 // Map the current input to a file.
424 FileID ModuleMapID = SrcMgr.getMainFileID();
425 const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
427 // If the module map is preprocessed, handle the initial line marker;
428 // line directives are not part of the module map syntax in general.
430 if (IsPreprocessed) {
431 SourceLocation EndOfLineMarker =
432 ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
433 if (EndOfLineMarker.isValid())
434 Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
437 // Load the module map file.
438 if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
439 PresumedModuleMapFile))
442 if (SrcMgr.getBuffer(ModuleMapID)->getBufferSize() == Offset)
448 static Module *prepareToBuildModule(CompilerInstance &CI,
449 StringRef ModuleMapFilename) {
450 if (CI.getLangOpts().CurrentModule.empty()) {
451 CI.getDiagnostics().Report(diag::err_missing_module_name);
453 // FIXME: Eventually, we could consider asking whether there was just
454 // a single module described in the module map, and use that as a
455 // default. Then it would be fairly trivial to just "compile" a module
456 // map with a single module (the common case).
460 // Dig out the module definition.
461 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
462 Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule,
463 /*AllowSearch=*/false);
465 CI.getDiagnostics().Report(diag::err_missing_module)
466 << CI.getLangOpts().CurrentModule << ModuleMapFilename;
471 // Check whether we can build this module at all.
472 if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
473 CI.getDiagnostics(), M))
476 // Inform the preprocessor that includes from within the input buffer should
477 // be resolved relative to the build directory of the module map file.
478 CI.getPreprocessor().setMainFileDir(M->Directory);
480 // If the module was inferred from a different module map (via an expanded
481 // umbrella module definition), track that fact.
482 // FIXME: It would be preferable to fill this in as part of processing
483 // the module map, rather than adding it after the fact.
484 StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
485 if (!OriginalModuleMapName.empty()) {
486 auto OriginalModuleMap =
487 CI.getFileManager().getFile(OriginalModuleMapName,
489 if (!OriginalModuleMap) {
490 CI.getDiagnostics().Report(diag::err_module_map_not_found)
491 << OriginalModuleMapName;
494 if (*OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
495 CI.getSourceManager().getMainFileID())) {
496 M->IsInferred = true;
497 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
498 .setInferredModuleAllowedBy(M, *OriginalModuleMap);
502 // If we're being run from the command-line, the module build stack will not
503 // have been filled in yet, so complete it now in order to allow us to detect
505 SourceManager &SourceMgr = CI.getSourceManager();
506 if (SourceMgr.getModuleBuildStack().empty())
507 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
508 FullSourceLoc(SourceLocation(), SourceMgr));
512 /// Compute the input buffer that should be used to build the specified module.
513 static std::unique_ptr<llvm::MemoryBuffer>
514 getInputBufferForModule(CompilerInstance &CI, Module *M) {
515 FileManager &FileMgr = CI.getFileManager();
517 // Collect the set of #includes we need to build the module.
518 SmallString<256> HeaderContents;
519 std::error_code Err = std::error_code();
520 if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
521 addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
522 CI.getLangOpts(), M->IsExternC);
523 Err = collectModuleHeaderIncludes(
524 CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
525 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
529 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
530 << M->getFullModuleName() << Err.message();
534 return llvm::MemoryBuffer::getMemBufferCopy(
535 HeaderContents, Module::getModuleInputBufferName());
538 bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
539 const FrontendInputFile &RealInput) {
540 FrontendInputFile Input(RealInput);
541 assert(!Instance && "Already processing a source file!");
542 assert(!Input.isEmpty() && "Unexpected empty filename!");
543 setCurrentInput(Input);
544 setCompilerInstance(&CI);
546 bool HasBegunSourceFile = false;
547 bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
548 usesPreprocessorOnly();
549 if (!BeginInvocation(CI))
552 // If we're replaying the build of an AST file, import it and set up
553 // the initial state from its build.
555 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
557 // The AST unit populates its own diagnostics engine rather than ours.
558 IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
559 new DiagnosticsEngine(Diags->getDiagnosticIDs(),
560 &Diags->getDiagnosticOptions()));
561 ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
563 // FIXME: What if the input is a memory buffer?
564 StringRef InputFile = Input.getFile();
566 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
567 InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly,
568 ASTDiags, CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs);
572 // Options relating to how we treat the input (but not what we do with it)
573 // are inherited from the AST unit.
574 CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
575 CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
576 CI.getLangOpts() = AST->getLangOpts();
578 // Set the shared objects, these are reset when we finish processing the
579 // file, otherwise the CompilerInstance will happily destroy them.
580 CI.setFileManager(&AST->getFileManager());
581 CI.createSourceManager(CI.getFileManager());
582 CI.getSourceManager().initializeForReplay(AST->getSourceManager());
584 // Preload all the module files loaded transitively by the AST unit. Also
585 // load all module map files that were parsed as part of building the AST
587 if (auto ASTReader = AST->getASTReader()) {
588 auto &MM = ASTReader->getModuleManager();
589 auto &PrimaryModule = MM.getPrimaryModule();
591 for (serialization::ModuleFile &MF : MM)
592 if (&MF != &PrimaryModule)
593 CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
595 ASTReader->visitTopLevelModuleMaps(PrimaryModule,
596 [&](const FileEntry *FE) {
597 CI.getFrontendOpts().ModuleMapFiles.push_back(FE->getName());
601 // Set up the input file for replay purposes.
602 auto Kind = AST->getInputKind();
603 if (Kind.getFormat() == InputKind::ModuleMap) {
605 AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
606 AST->getLangOpts().CurrentModule, /*AllowSearch*/ false);
607 assert(ASTModule && "module file does not define its own module");
608 Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
610 auto &OldSM = AST->getSourceManager();
611 FileID ID = OldSM.getMainFileID();
612 if (auto *File = OldSM.getFileEntryForID(ID))
613 Input = FrontendInputFile(File->getName(), Kind);
615 Input = FrontendInputFile(OldSM.getBuffer(ID), Kind);
617 setCurrentInput(Input, std::move(AST));
620 // AST files follow a very different path, since they share objects via the
622 if (Input.getKind().getFormat() == InputKind::Precompiled) {
623 assert(!usesPreprocessorOnly() && "this case was handled above");
624 assert(hasASTFileSupport() &&
625 "This action does not have AST file support!");
627 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
629 // FIXME: What if the input is a memory buffer?
630 StringRef InputFile = Input.getFile();
632 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
633 InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
634 CI.getFileSystemOpts(), CI.getCodeGenOpts().DebugTypeExtRefs);
639 // Inform the diagnostic client we are processing a source file.
640 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
641 HasBegunSourceFile = true;
643 // Set the shared objects, these are reset when we finish processing the
644 // file, otherwise the CompilerInstance will happily destroy them.
645 CI.setFileManager(&AST->getFileManager());
646 CI.setSourceManager(&AST->getSourceManager());
647 CI.setPreprocessor(AST->getPreprocessorPtr());
648 Preprocessor &PP = CI.getPreprocessor();
649 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
651 CI.setASTContext(&AST->getASTContext());
653 setCurrentInput(Input, std::move(AST));
655 // Initialize the action.
656 if (!BeginSourceFileAction(CI))
659 // Create the AST consumer.
660 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
661 if (!CI.hasASTConsumer())
667 // Set up the file and source managers, if needed.
668 if (!CI.hasFileManager()) {
669 if (!CI.createFileManager()) {
673 if (!CI.hasSourceManager())
674 CI.createSourceManager(CI.getFileManager());
676 // Set up embedding for any specified files. Do this before we load any
677 // source files, including the primary module map for the compilation.
678 for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
679 if (auto FE = CI.getFileManager().getFile(F, /*openFile*/true))
680 CI.getSourceManager().setFileIsTransient(*FE);
682 CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
684 if (CI.getFrontendOpts().ModulesEmbedAllFiles)
685 CI.getSourceManager().setAllFilesAreTransient(true);
687 // IR files bypass the rest of initialization.
688 if (Input.getKind().getLanguage() == Language::LLVM_IR) {
689 assert(hasIRSupport() &&
690 "This action does not have IR file support!");
692 // Inform the diagnostic client we are processing a source file.
693 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
694 HasBegunSourceFile = true;
696 // Initialize the action.
697 if (!BeginSourceFileAction(CI))
700 // Initialize the main file entry.
701 if (!CI.InitializeSourceManager(CurrentInput))
707 // If the implicit PCH include is actually a directory, rather than
708 // a single file, search for a suitable PCH file in that directory.
709 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
710 FileManager &FileMgr = CI.getFileManager();
711 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
712 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
713 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
714 if (auto PCHDir = FileMgr.getDirectory(PCHInclude)) {
716 SmallString<128> DirNative;
717 llvm::sys::path::native((*PCHDir)->getName(), DirNative);
719 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
720 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
722 Dir != DirEnd && !EC; Dir.increment(EC)) {
723 // Check whether this is an acceptable AST file.
724 if (ASTReader::isAcceptableASTFile(
725 Dir->path(), FileMgr, CI.getPCHContainerReader(),
726 CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
727 SpecificModuleCachePath)) {
728 PPOpts.ImplicitPCHInclude = Dir->path();
735 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
741 // Set up the preprocessor if needed. When parsing model files the
742 // preprocessor of the original source is reused.
743 if (!isModelParsingAction())
744 CI.createPreprocessor(getTranslationUnitKind());
746 // Inform the diagnostic client we are processing a source file.
747 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
748 &CI.getPreprocessor());
749 HasBegunSourceFile = true;
751 // Initialize the main file entry.
752 if (!CI.InitializeSourceManager(Input))
755 // For module map files, we first parse the module map and synthesize a
756 // "<module-includes>" buffer before more conventional processing.
757 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
758 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
760 std::string PresumedModuleMapFile;
761 unsigned OffsetToContents;
762 if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
763 Input.isPreprocessed(),
764 PresumedModuleMapFile, OffsetToContents))
767 auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
771 CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
773 if (OffsetToContents)
774 // If the module contents are in the same file, skip to them.
775 CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
777 // Otherwise, convert the module description to a suitable input buffer.
778 auto Buffer = getInputBufferForModule(CI, CurrentModule);
782 // Reinitialize the main file entry to refer to the new input.
783 auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
784 auto &SourceMgr = CI.getSourceManager();
785 auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
786 assert(BufferID.isValid() && "couldn't creaate module buffer ID");
787 SourceMgr.setMainFileID(BufferID);
791 // Initialize the action.
792 if (!BeginSourceFileAction(CI))
795 // If we were asked to load any module map files, do so now.
796 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
797 if (auto File = CI.getFileManager().getFile(Filename))
798 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
799 *File, /*IsSystem*/false);
801 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
804 // Add a module declaration scope so that modules from -fmodule-map-file
805 // arguments may shadow modules found implicitly in search paths.
807 .getHeaderSearchInfo()
809 .finishModuleDeclarationScope();
811 // Create the AST context and consumer unless this is a preprocessor only
813 if (!usesPreprocessorOnly()) {
814 // Parsing a model file should reuse the existing ASTContext.
815 if (!isModelParsingAction())
816 CI.createASTContext();
818 // For preprocessed files, check if the first line specifies the original
819 // source file name with a linemarker.
820 std::string PresumedInputFile = getCurrentFileOrBufferName();
821 if (Input.isPreprocessed())
822 ReadOriginalFileName(CI, PresumedInputFile);
824 std::unique_ptr<ASTConsumer> Consumer =
825 CreateWrappedASTConsumer(CI, PresumedInputFile);
829 // FIXME: should not overwrite ASTMutationListener when parsing model files?
830 if (!isModelParsingAction())
831 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
833 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
834 // Convert headers to PCH and chain them.
835 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
836 source = createChainedIncludesSource(CI, FinalReader);
839 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
840 CI.getASTContext().setExternalSource(source);
841 } else if (CI.getLangOpts().Modules ||
842 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
844 assert(hasPCHSupport() && "This action does not have PCH support!");
845 ASTDeserializationListener *DeserialListener =
846 Consumer->GetASTDeserializationListener();
847 bool DeleteDeserialListener = false;
848 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
849 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
850 DeleteDeserialListener);
851 DeleteDeserialListener = true;
853 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
854 DeserialListener = new DeserializedDeclsChecker(
856 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
857 DeserialListener, DeleteDeserialListener);
858 DeleteDeserialListener = true;
860 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
861 CI.createPCHExternalASTSource(
862 CI.getPreprocessorOpts().ImplicitPCHInclude,
863 CI.getPreprocessorOpts().DisablePCHValidation,
864 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
865 DeleteDeserialListener);
866 if (!CI.getASTContext().getExternalSource())
869 // If modules are enabled, create the module manager before creating
870 // any builtins, so that all declarations know that they might be
871 // extended by an external source.
872 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
873 !CI.getASTContext().getExternalSource()) {
874 CI.createASTReader();
875 CI.getASTReader()->setDeserializationListener(DeserialListener,
876 DeleteDeserialListener);
880 CI.setASTConsumer(std::move(Consumer));
881 if (!CI.hasASTConsumer())
885 // Initialize built-in info as long as we aren't using an external AST
887 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
888 !CI.getASTContext().getExternalSource()) {
889 Preprocessor &PP = CI.getPreprocessor();
890 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
893 // FIXME: If this is a problem, recover from it by creating a multiplex
895 assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
896 "modules enabled but created an external source that "
897 "doesn't support modules");
900 // If we were asked to load any module files, do so now.
901 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
902 if (!CI.loadModuleFile(ModuleFile))
905 // If there is a layout overrides file, attach an external AST source that
906 // provides the layouts from that file.
907 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
908 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
909 IntrusiveRefCntPtr<ExternalASTSource>
910 Override(new LayoutOverrideSource(
911 CI.getFrontendOpts().OverrideRecordLayoutsFile));
912 CI.getASTContext().setExternalSource(Override);
917 // If we failed, reset state since the client will not end up calling the
918 // matching EndSourceFile().
920 if (HasBegunSourceFile)
921 CI.getDiagnosticClient().EndSourceFile();
922 CI.clearOutputFiles(/*EraseFiles=*/true);
923 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
924 setCurrentInput(FrontendInputFile());
925 setCompilerInstance(nullptr);
929 llvm::Error FrontendAction::Execute() {
930 CompilerInstance &CI = getCompilerInstance();
932 if (CI.hasFrontendTimer()) {
933 llvm::TimeRegion Timer(CI.getFrontendTimer());
936 else ExecuteAction();
938 // If we are supposed to rebuild the global module index, do so now unless
939 // there were any module-build failures.
940 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
941 CI.hasPreprocessor()) {
943 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
944 if (!Cache.empty()) {
945 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
946 CI.getFileManager(), CI.getPCHContainerReader(), Cache)) {
947 // FIXME this drops the error on the floor, but
948 // Index/pch-from-libclang.c seems to rely on dropping at least some of
949 // the error conditions!
950 consumeError(std::move(Err));
955 return llvm::Error::success();
958 void FrontendAction::EndSourceFile() {
959 CompilerInstance &CI = getCompilerInstance();
961 // Inform the diagnostic client we are done with this source file.
962 CI.getDiagnosticClient().EndSourceFile();
964 // Inform the preprocessor we are done.
965 if (CI.hasPreprocessor())
966 CI.getPreprocessor().EndSourceFile();
968 // Finalize the action.
969 EndSourceFileAction();
971 // Sema references the ast consumer, so reset sema first.
973 // FIXME: There is more per-file stuff we could just drop here?
974 bool DisableFree = CI.getFrontendOpts().DisableFree;
976 CI.resetAndLeakSema();
977 CI.resetAndLeakASTContext();
978 llvm::BuryPointer(CI.takeASTConsumer().get());
981 CI.setASTContext(nullptr);
982 CI.setASTConsumer(nullptr);
985 if (CI.getFrontendOpts().ShowStats) {
986 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
987 CI.getPreprocessor().PrintStats();
988 CI.getPreprocessor().getIdentifierTable().PrintStats();
989 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
990 CI.getSourceManager().PrintStats();
991 llvm::errs() << "\n";
994 // Cleanup the output streams, and erase the output files if instructed by the
996 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
998 if (isCurrentFileAST()) {
1000 CI.resetAndLeakPreprocessor();
1001 CI.resetAndLeakSourceManager();
1002 CI.resetAndLeakFileManager();
1003 llvm::BuryPointer(std::move(CurrentASTUnit));
1005 CI.setPreprocessor(nullptr);
1006 CI.setSourceManager(nullptr);
1007 CI.setFileManager(nullptr);
1011 setCompilerInstance(nullptr);
1012 setCurrentInput(FrontendInputFile());
1013 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
1016 bool FrontendAction::shouldEraseOutputFiles() {
1017 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
1020 //===----------------------------------------------------------------------===//
1022 //===----------------------------------------------------------------------===//
1024 void ASTFrontendAction::ExecuteAction() {
1025 CompilerInstance &CI = getCompilerInstance();
1026 if (!CI.hasPreprocessor())
1029 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
1030 // here so the source manager would be initialized.
1031 if (hasCodeCompletionSupport() &&
1032 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
1033 CI.createCodeCompletionConsumer();
1035 // Use a code completion consumer?
1036 CodeCompleteConsumer *CompletionConsumer = nullptr;
1037 if (CI.hasCodeCompletionConsumer())
1038 CompletionConsumer = &CI.getCodeCompletionConsumer();
1041 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
1043 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
1044 CI.getFrontendOpts().SkipFunctionBodies);
1047 void PluginASTAction::anchor() { }
1049 std::unique_ptr<ASTConsumer>
1050 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1052 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1055 bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
1056 return WrappedAction->PrepareToExecuteAction(CI);
1058 std::unique_ptr<ASTConsumer>
1059 WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1061 return WrappedAction->CreateASTConsumer(CI, InFile);
1063 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
1064 return WrappedAction->BeginInvocation(CI);
1066 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
1067 WrappedAction->setCurrentInput(getCurrentInput());
1068 WrappedAction->setCompilerInstance(&CI);
1069 auto Ret = WrappedAction->BeginSourceFileAction(CI);
1070 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1071 setCurrentInput(WrappedAction->getCurrentInput());
1074 void WrapperFrontendAction::ExecuteAction() {
1075 WrappedAction->ExecuteAction();
1077 void WrapperFrontendAction::EndSourceFileAction() {
1078 WrappedAction->EndSourceFileAction();
1081 bool WrapperFrontendAction::usesPreprocessorOnly() const {
1082 return WrappedAction->usesPreprocessorOnly();
1084 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
1085 return WrappedAction->getTranslationUnitKind();
1087 bool WrapperFrontendAction::hasPCHSupport() const {
1088 return WrappedAction->hasPCHSupport();
1090 bool WrapperFrontendAction::hasASTFileSupport() const {
1091 return WrappedAction->hasASTFileSupport();
1093 bool WrapperFrontendAction::hasIRSupport() const {
1094 return WrappedAction->hasIRSupport();
1096 bool WrapperFrontendAction::hasCodeCompletionSupport() const {
1097 return WrappedAction->hasCodeCompletionSupport();
1100 WrapperFrontendAction::WrapperFrontendAction(
1101 std::unique_ptr<FrontendAction> WrappedAction)
1102 : WrappedAction(std::move(WrappedAction)) {}