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 (const FrontendPluginRegistry::entry &Plugin :
161 FrontendPluginRegistry::entries()) {
162 if (Plugin.getName() == Arg)
166 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg;
167 FoundAllPlugins = false;
170 if (!FoundAllPlugins)
173 // If there are no registered plugins we don't need to wrap the consumer
174 if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
177 // If this is a code completion run, avoid invoking the plugin consumers
178 if (CI.hasCodeCompletionConsumer())
181 // Collect the list of plugins that go before the main action (in Consumers)
182 // or after it (in AfterConsumers)
183 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
184 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
185 for (const FrontendPluginRegistry::entry &Plugin :
186 FrontendPluginRegistry::entries()) {
187 std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
188 PluginASTAction::ActionType ActionType = P->getActionType();
189 if (ActionType == PluginASTAction::Cmdline) {
190 // This is O(|plugins| * |add_plugins|), but since both numbers are
191 // way below 50 in practice, that's ok.
192 if (llvm::any_of(CI.getFrontendOpts().AddPluginActions,
193 [&](const std::string &PluginAction) {
194 return PluginAction == Plugin.getName();
196 ActionType = PluginASTAction::AddAfterMainAction;
198 if ((ActionType == PluginASTAction::AddBeforeMainAction ||
199 ActionType == PluginASTAction::AddAfterMainAction) &&
202 CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
203 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
204 if (ActionType == PluginASTAction::AddBeforeMainAction) {
205 Consumers.push_back(std::move(PluginConsumer));
207 AfterConsumers.push_back(std::move(PluginConsumer));
212 // Add to Consumers the main consumer, then all the plugins that go after it
213 Consumers.push_back(std::move(Consumer));
214 for (auto &C : AfterConsumers) {
215 Consumers.push_back(std::move(C));
218 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
221 /// For preprocessed files, if the first line is the linemarker and specifies
222 /// the original source file name, use that name as the input file name.
223 /// Returns the location of the first token after the line marker directive.
225 /// \param CI The compiler instance.
226 /// \param InputFile Populated with the filename from the line marker.
227 /// \param IsModuleMap If \c true, add a line note corresponding to this line
228 /// directive. (We need to do this because the directive will not be
229 /// visited by the preprocessor.)
230 static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
231 std::string &InputFile,
232 bool IsModuleMap = false) {
233 auto &SourceMgr = CI.getSourceManager();
234 auto MainFileID = SourceMgr.getMainFileID();
236 bool Invalid = false;
237 const auto *MainFileBuf = SourceMgr.getBuffer(MainFileID, &Invalid);
239 return SourceLocation();
241 std::unique_ptr<Lexer> RawLexer(
242 new Lexer(MainFileID, MainFileBuf, SourceMgr, CI.getLangOpts()));
244 // If the first line has the syntax of
248 // we use FILENAME as the input file name.
250 if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash)
251 return SourceLocation();
252 if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
253 T.getKind() != tok::numeric_constant)
254 return SourceLocation();
257 SourceLocation LineNoLoc = T.getLocation();
259 llvm::SmallString<16> Buffer;
260 if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
261 .getAsInteger(10, LineNo))
262 return SourceLocation();
265 RawLexer->LexFromRawLexer(T);
266 if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
267 return SourceLocation();
269 StringLiteralParser Literal(T, CI.getPreprocessor());
270 if (Literal.hadError)
271 return SourceLocation();
272 RawLexer->LexFromRawLexer(T);
273 if (T.isNot(tok::eof) && !T.isAtStartOfLine())
274 return SourceLocation();
275 InputFile = Literal.GetString().str();
278 CI.getSourceManager().AddLineNote(
279 LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
280 false, SrcMgr::C_User_ModuleMap);
282 return T.getLocation();
285 static SmallVectorImpl<char> &
286 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
287 Includes.append(RHS.begin(), RHS.end());
291 static void addHeaderInclude(StringRef HeaderName,
292 SmallVectorImpl<char> &Includes,
293 const LangOptions &LangOpts,
295 if (IsExternC && LangOpts.CPlusPlus)
296 Includes += "extern \"C\" {\n";
298 Includes += "#import \"";
300 Includes += "#include \"";
302 Includes += HeaderName;
305 if (IsExternC && LangOpts.CPlusPlus)
309 /// Collect the set of header includes needed to construct the given
310 /// module and update the TopHeaders file set of the module.
312 /// \param Module The module we're collecting includes from.
314 /// \param Includes Will be augmented with the set of \#includes or \#imports
315 /// needed to load all of the named headers.
316 static std::error_code collectModuleHeaderIncludes(
317 const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
318 ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
319 // Don't collect any headers for unavailable modules.
320 if (!Module->isAvailable())
321 return std::error_code();
323 // Resolve all lazy header directives to header files.
324 ModMap.resolveHeaderDirectives(Module);
326 // If any headers are missing, we can't build this module. In most cases,
327 // diagnostics for this should have already been produced; we only get here
328 // if explicit stat information was provided.
329 // FIXME: If the name resolves to a file with different stat information,
330 // produce a better diagnostic.
331 if (!Module->MissingHeaders.empty()) {
332 auto &MissingHeader = Module->MissingHeaders.front();
333 Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
334 << MissingHeader.IsUmbrella << MissingHeader.FileName;
335 return std::error_code();
338 // Add includes for each of these headers.
339 for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
340 for (Module::Header &H : Module->Headers[HK]) {
341 Module->addTopHeader(H.Entry);
342 // Use the path as specified in the module map file. We'll look for this
343 // file relative to the module build directory (the directory containing
344 // the module map file) so this will find the same file that we found
345 // while parsing the module map.
346 addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
349 // Note that Module->PrivateHeaders will not be a TopHeader.
351 if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
352 Module->addTopHeader(UmbrellaHeader.Entry);
354 // Include the umbrella header for submodules.
355 addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
357 } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
358 // Add all of the headers we find in this subdirectory.
360 SmallString<128> DirNative;
361 llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
363 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
364 SmallVector<std::pair<std::string, const FileEntry *>, 8> Headers;
365 for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
366 Dir != End && !EC; Dir.increment(EC)) {
367 // Check whether this entry has an extension typically associated with
369 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
370 .Cases(".h", ".H", ".hh", ".hpp", true)
374 auto Header = FileMgr.getFile(Dir->path());
375 // FIXME: This shouldn't happen unless there is a file system race. Is
376 // that worth diagnosing?
380 // If this header is marked 'unavailable' in this module, don't include
382 if (ModMap.isHeaderUnavailableInModule(*Header, Module))
385 // Compute the relative path from the directory to this file.
386 SmallVector<StringRef, 16> Components;
387 auto PathIt = llvm::sys::path::rbegin(Dir->path());
388 for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
389 Components.push_back(*PathIt);
390 SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
391 for (auto It = Components.rbegin(), End = Components.rend(); It != End;
393 llvm::sys::path::append(RelativeHeader, *It);
395 std::string RelName = RelativeHeader.c_str();
396 Headers.push_back(std::make_pair(RelName, *Header));
402 // Sort header paths and make the header inclusion order deterministic
403 // across different OSs and filesystems.
404 llvm::sort(Headers.begin(), Headers.end(), [](
405 const std::pair<std::string, const FileEntry *> &LHS,
406 const std::pair<std::string, const FileEntry *> &RHS) {
407 return LHS.first < RHS.first;
409 for (auto &H : Headers) {
410 // Include this header as part of the umbrella directory.
411 Module->addTopHeader(H.second);
412 addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC);
416 // Recurse into submodules.
417 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
418 SubEnd = Module->submodule_end();
419 Sub != SubEnd; ++Sub)
420 if (std::error_code Err = collectModuleHeaderIncludes(
421 LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
424 return std::error_code();
427 static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
429 std::string &PresumedModuleMapFile,
431 auto &SrcMgr = CI.getSourceManager();
432 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
434 // Map the current input to a file.
435 FileID ModuleMapID = SrcMgr.getMainFileID();
436 const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
438 // If the module map is preprocessed, handle the initial line marker;
439 // line directives are not part of the module map syntax in general.
441 if (IsPreprocessed) {
442 SourceLocation EndOfLineMarker =
443 ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
444 if (EndOfLineMarker.isValid())
445 Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
448 // Load the module map file.
449 if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
450 PresumedModuleMapFile))
453 if (SrcMgr.getBuffer(ModuleMapID)->getBufferSize() == Offset)
459 static Module *prepareToBuildModule(CompilerInstance &CI,
460 StringRef ModuleMapFilename) {
461 if (CI.getLangOpts().CurrentModule.empty()) {
462 CI.getDiagnostics().Report(diag::err_missing_module_name);
464 // FIXME: Eventually, we could consider asking whether there was just
465 // a single module described in the module map, and use that as a
466 // default. Then it would be fairly trivial to just "compile" a module
467 // map with a single module (the common case).
471 // Dig out the module definition.
472 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
473 Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule,
474 /*AllowSearch=*/false);
476 CI.getDiagnostics().Report(diag::err_missing_module)
477 << CI.getLangOpts().CurrentModule << ModuleMapFilename;
482 // Check whether we can build this module at all.
483 if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
484 CI.getDiagnostics(), M))
487 // Inform the preprocessor that includes from within the input buffer should
488 // be resolved relative to the build directory of the module map file.
489 CI.getPreprocessor().setMainFileDir(M->Directory);
491 // If the module was inferred from a different module map (via an expanded
492 // umbrella module definition), track that fact.
493 // FIXME: It would be preferable to fill this in as part of processing
494 // the module map, rather than adding it after the fact.
495 StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
496 if (!OriginalModuleMapName.empty()) {
497 auto OriginalModuleMap =
498 CI.getFileManager().getFile(OriginalModuleMapName,
500 if (!OriginalModuleMap) {
501 CI.getDiagnostics().Report(diag::err_module_map_not_found)
502 << OriginalModuleMapName;
505 if (*OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
506 CI.getSourceManager().getMainFileID())) {
507 M->IsInferred = true;
508 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
509 .setInferredModuleAllowedBy(M, *OriginalModuleMap);
513 // If we're being run from the command-line, the module build stack will not
514 // have been filled in yet, so complete it now in order to allow us to detect
516 SourceManager &SourceMgr = CI.getSourceManager();
517 if (SourceMgr.getModuleBuildStack().empty())
518 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
519 FullSourceLoc(SourceLocation(), SourceMgr));
523 /// Compute the input buffer that should be used to build the specified module.
524 static std::unique_ptr<llvm::MemoryBuffer>
525 getInputBufferForModule(CompilerInstance &CI, Module *M) {
526 FileManager &FileMgr = CI.getFileManager();
528 // Collect the set of #includes we need to build the module.
529 SmallString<256> HeaderContents;
530 std::error_code Err = std::error_code();
531 if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
532 addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
533 CI.getLangOpts(), M->IsExternC);
534 Err = collectModuleHeaderIncludes(
535 CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
536 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
540 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
541 << M->getFullModuleName() << Err.message();
545 return llvm::MemoryBuffer::getMemBufferCopy(
546 HeaderContents, Module::getModuleInputBufferName());
549 bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
550 const FrontendInputFile &RealInput) {
551 FrontendInputFile Input(RealInput);
552 assert(!Instance && "Already processing a source file!");
553 assert(!Input.isEmpty() && "Unexpected empty filename!");
554 setCurrentInput(Input);
555 setCompilerInstance(&CI);
557 bool HasBegunSourceFile = false;
558 bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
559 usesPreprocessorOnly();
560 if (!BeginInvocation(CI))
563 // If we're replaying the build of an AST file, import it and set up
564 // the initial state from its build.
566 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
568 // The AST unit populates its own diagnostics engine rather than ours.
569 IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
570 new DiagnosticsEngine(Diags->getDiagnosticIDs(),
571 &Diags->getDiagnosticOptions()));
572 ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
574 // FIXME: What if the input is a memory buffer?
575 StringRef InputFile = Input.getFile();
577 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
578 std::string(InputFile), CI.getPCHContainerReader(),
579 ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(),
580 CI.getCodeGenOpts().DebugTypeExtRefs);
584 // Options relating to how we treat the input (but not what we do with it)
585 // are inherited from the AST unit.
586 CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
587 CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
588 CI.getLangOpts() = AST->getLangOpts();
590 // Set the shared objects, these are reset when we finish processing the
591 // file, otherwise the CompilerInstance will happily destroy them.
592 CI.setFileManager(&AST->getFileManager());
593 CI.createSourceManager(CI.getFileManager());
594 CI.getSourceManager().initializeForReplay(AST->getSourceManager());
596 // Preload all the module files loaded transitively by the AST unit. Also
597 // load all module map files that were parsed as part of building the AST
599 if (auto ASTReader = AST->getASTReader()) {
600 auto &MM = ASTReader->getModuleManager();
601 auto &PrimaryModule = MM.getPrimaryModule();
603 for (serialization::ModuleFile &MF : MM)
604 if (&MF != &PrimaryModule)
605 CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
607 ASTReader->visitTopLevelModuleMaps(
608 PrimaryModule, [&](const FileEntry *FE) {
609 CI.getFrontendOpts().ModuleMapFiles.push_back(
610 std::string(FE->getName()));
614 // Set up the input file for replay purposes.
615 auto Kind = AST->getInputKind();
616 if (Kind.getFormat() == InputKind::ModuleMap) {
618 AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
619 AST->getLangOpts().CurrentModule, /*AllowSearch*/ false);
620 assert(ASTModule && "module file does not define its own module");
621 Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
623 auto &OldSM = AST->getSourceManager();
624 FileID ID = OldSM.getMainFileID();
625 if (auto *File = OldSM.getFileEntryForID(ID))
626 Input = FrontendInputFile(File->getName(), Kind);
628 Input = FrontendInputFile(OldSM.getBuffer(ID), Kind);
630 setCurrentInput(Input, std::move(AST));
633 // AST files follow a very different path, since they share objects via the
635 if (Input.getKind().getFormat() == InputKind::Precompiled) {
636 assert(!usesPreprocessorOnly() && "this case was handled above");
637 assert(hasASTFileSupport() &&
638 "This action does not have AST file support!");
640 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
642 // FIXME: What if the input is a memory buffer?
643 StringRef InputFile = Input.getFile();
645 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
646 std::string(InputFile), CI.getPCHContainerReader(),
647 ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
648 CI.getCodeGenOpts().DebugTypeExtRefs);
653 // Inform the diagnostic client we are processing a source file.
654 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
655 HasBegunSourceFile = true;
657 // Set the shared objects, these are reset when we finish processing the
658 // file, otherwise the CompilerInstance will happily destroy them.
659 CI.setFileManager(&AST->getFileManager());
660 CI.setSourceManager(&AST->getSourceManager());
661 CI.setPreprocessor(AST->getPreprocessorPtr());
662 Preprocessor &PP = CI.getPreprocessor();
663 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
665 CI.setASTContext(&AST->getASTContext());
667 setCurrentInput(Input, std::move(AST));
669 // Initialize the action.
670 if (!BeginSourceFileAction(CI))
673 // Create the AST consumer.
674 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
675 if (!CI.hasASTConsumer())
681 // Set up the file and source managers, if needed.
682 if (!CI.hasFileManager()) {
683 if (!CI.createFileManager()) {
687 if (!CI.hasSourceManager())
688 CI.createSourceManager(CI.getFileManager());
690 // Set up embedding for any specified files. Do this before we load any
691 // source files, including the primary module map for the compilation.
692 for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
693 if (auto FE = CI.getFileManager().getFile(F, /*openFile*/true))
694 CI.getSourceManager().setFileIsTransient(*FE);
696 CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
698 if (CI.getFrontendOpts().ModulesEmbedAllFiles)
699 CI.getSourceManager().setAllFilesAreTransient(true);
701 // IR files bypass the rest of initialization.
702 if (Input.getKind().getLanguage() == Language::LLVM_IR) {
703 assert(hasIRSupport() &&
704 "This action does not have IR file support!");
706 // Inform the diagnostic client we are processing a source file.
707 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
708 HasBegunSourceFile = true;
710 // Initialize the action.
711 if (!BeginSourceFileAction(CI))
714 // Initialize the main file entry.
715 if (!CI.InitializeSourceManager(CurrentInput))
721 // If the implicit PCH include is actually a directory, rather than
722 // a single file, search for a suitable PCH file in that directory.
723 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
724 FileManager &FileMgr = CI.getFileManager();
725 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
726 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
727 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
728 if (auto PCHDir = FileMgr.getDirectory(PCHInclude)) {
730 SmallString<128> DirNative;
731 llvm::sys::path::native((*PCHDir)->getName(), DirNative);
733 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
734 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
736 Dir != DirEnd && !EC; Dir.increment(EC)) {
737 // Check whether this is an acceptable AST file.
738 if (ASTReader::isAcceptableASTFile(
739 Dir->path(), FileMgr, CI.getPCHContainerReader(),
740 CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
741 SpecificModuleCachePath)) {
742 PPOpts.ImplicitPCHInclude = std::string(Dir->path());
749 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
755 // Set up the preprocessor if needed. When parsing model files the
756 // preprocessor of the original source is reused.
757 if (!isModelParsingAction())
758 CI.createPreprocessor(getTranslationUnitKind());
760 // Inform the diagnostic client we are processing a source file.
761 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
762 &CI.getPreprocessor());
763 HasBegunSourceFile = true;
765 // Initialize the main file entry.
766 if (!CI.InitializeSourceManager(Input))
769 // For module map files, we first parse the module map and synthesize a
770 // "<module-includes>" buffer before more conventional processing.
771 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
772 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
774 std::string PresumedModuleMapFile;
775 unsigned OffsetToContents;
776 if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
777 Input.isPreprocessed(),
778 PresumedModuleMapFile, OffsetToContents))
781 auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
785 CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
787 if (OffsetToContents)
788 // If the module contents are in the same file, skip to them.
789 CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
791 // Otherwise, convert the module description to a suitable input buffer.
792 auto Buffer = getInputBufferForModule(CI, CurrentModule);
796 // Reinitialize the main file entry to refer to the new input.
797 auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
798 auto &SourceMgr = CI.getSourceManager();
799 auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
800 assert(BufferID.isValid() && "couldn't create module buffer ID");
801 SourceMgr.setMainFileID(BufferID);
805 // Initialize the action.
806 if (!BeginSourceFileAction(CI))
809 // If we were asked to load any module map files, do so now.
810 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
811 if (auto File = CI.getFileManager().getFile(Filename))
812 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
813 *File, /*IsSystem*/false);
815 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
818 // Add a module declaration scope so that modules from -fmodule-map-file
819 // arguments may shadow modules found implicitly in search paths.
821 .getHeaderSearchInfo()
823 .finishModuleDeclarationScope();
825 // Create the AST context and consumer unless this is a preprocessor only
827 if (!usesPreprocessorOnly()) {
828 // Parsing a model file should reuse the existing ASTContext.
829 if (!isModelParsingAction())
830 CI.createASTContext();
832 // For preprocessed files, check if the first line specifies the original
833 // source file name with a linemarker.
834 std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
835 if (Input.isPreprocessed())
836 ReadOriginalFileName(CI, PresumedInputFile);
838 std::unique_ptr<ASTConsumer> Consumer =
839 CreateWrappedASTConsumer(CI, PresumedInputFile);
843 // FIXME: should not overwrite ASTMutationListener when parsing model files?
844 if (!isModelParsingAction())
845 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
847 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
848 // Convert headers to PCH and chain them.
849 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
850 source = createChainedIncludesSource(CI, FinalReader);
853 CI.setASTReader(static_cast<ASTReader *>(FinalReader.get()));
854 CI.getASTContext().setExternalSource(source);
855 } else if (CI.getLangOpts().Modules ||
856 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
858 assert(hasPCHSupport() && "This action does not have PCH support!");
859 ASTDeserializationListener *DeserialListener =
860 Consumer->GetASTDeserializationListener();
861 bool DeleteDeserialListener = false;
862 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
863 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
864 DeleteDeserialListener);
865 DeleteDeserialListener = true;
867 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
868 DeserialListener = new DeserializedDeclsChecker(
870 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
871 DeserialListener, DeleteDeserialListener);
872 DeleteDeserialListener = true;
874 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
875 CI.createPCHExternalASTSource(
876 CI.getPreprocessorOpts().ImplicitPCHInclude,
877 CI.getPreprocessorOpts().DisablePCHValidation,
878 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
879 DeleteDeserialListener);
880 if (!CI.getASTContext().getExternalSource())
883 // If modules are enabled, create the AST reader before creating
884 // any builtins, so that all declarations know that they might be
885 // extended by an external source.
886 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
887 !CI.getASTContext().getExternalSource()) {
888 CI.createASTReader();
889 CI.getASTReader()->setDeserializationListener(DeserialListener,
890 DeleteDeserialListener);
894 CI.setASTConsumer(std::move(Consumer));
895 if (!CI.hasASTConsumer())
899 // Initialize built-in info as long as we aren't using an external AST
901 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
902 !CI.getASTContext().getExternalSource()) {
903 Preprocessor &PP = CI.getPreprocessor();
904 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
907 // FIXME: If this is a problem, recover from it by creating a multiplex
909 assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
910 "modules enabled but created an external source that "
911 "doesn't support modules");
914 // If we were asked to load any module files, do so now.
915 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
916 if (!CI.loadModuleFile(ModuleFile))
919 // If there is a layout overrides file, attach an external AST source that
920 // provides the layouts from that file.
921 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
922 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
923 IntrusiveRefCntPtr<ExternalASTSource>
924 Override(new LayoutOverrideSource(
925 CI.getFrontendOpts().OverrideRecordLayoutsFile));
926 CI.getASTContext().setExternalSource(Override);
931 // If we failed, reset state since the client will not end up calling the
932 // matching EndSourceFile().
934 if (HasBegunSourceFile)
935 CI.getDiagnosticClient().EndSourceFile();
936 CI.clearOutputFiles(/*EraseFiles=*/true);
937 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
938 setCurrentInput(FrontendInputFile());
939 setCompilerInstance(nullptr);
943 llvm::Error FrontendAction::Execute() {
944 CompilerInstance &CI = getCompilerInstance();
946 if (CI.hasFrontendTimer()) {
947 llvm::TimeRegion Timer(CI.getFrontendTimer());
950 else ExecuteAction();
952 // If we are supposed to rebuild the global module index, do so now unless
953 // there were any module-build failures.
954 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
955 CI.hasPreprocessor()) {
957 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
958 if (!Cache.empty()) {
959 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
960 CI.getFileManager(), CI.getPCHContainerReader(), Cache)) {
961 // FIXME this drops the error on the floor, but
962 // Index/pch-from-libclang.c seems to rely on dropping at least some of
963 // the error conditions!
964 consumeError(std::move(Err));
969 return llvm::Error::success();
972 void FrontendAction::EndSourceFile() {
973 CompilerInstance &CI = getCompilerInstance();
975 // Inform the diagnostic client we are done with this source file.
976 CI.getDiagnosticClient().EndSourceFile();
978 // Inform the preprocessor we are done.
979 if (CI.hasPreprocessor())
980 CI.getPreprocessor().EndSourceFile();
982 // Finalize the action.
983 EndSourceFileAction();
985 // Sema references the ast consumer, so reset sema first.
987 // FIXME: There is more per-file stuff we could just drop here?
988 bool DisableFree = CI.getFrontendOpts().DisableFree;
990 CI.resetAndLeakSema();
991 CI.resetAndLeakASTContext();
992 llvm::BuryPointer(CI.takeASTConsumer().get());
995 CI.setASTContext(nullptr);
996 CI.setASTConsumer(nullptr);
999 if (CI.getFrontendOpts().ShowStats) {
1000 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
1001 CI.getPreprocessor().PrintStats();
1002 CI.getPreprocessor().getIdentifierTable().PrintStats();
1003 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
1004 CI.getSourceManager().PrintStats();
1005 llvm::errs() << "\n";
1008 // Cleanup the output streams, and erase the output files if instructed by the
1010 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
1012 if (isCurrentFileAST()) {
1014 CI.resetAndLeakPreprocessor();
1015 CI.resetAndLeakSourceManager();
1016 CI.resetAndLeakFileManager();
1017 llvm::BuryPointer(std::move(CurrentASTUnit));
1019 CI.setPreprocessor(nullptr);
1020 CI.setSourceManager(nullptr);
1021 CI.setFileManager(nullptr);
1025 setCompilerInstance(nullptr);
1026 setCurrentInput(FrontendInputFile());
1027 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
1030 bool FrontendAction::shouldEraseOutputFiles() {
1031 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
1034 //===----------------------------------------------------------------------===//
1036 //===----------------------------------------------------------------------===//
1038 void ASTFrontendAction::ExecuteAction() {
1039 CompilerInstance &CI = getCompilerInstance();
1040 if (!CI.hasPreprocessor())
1043 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
1044 // here so the source manager would be initialized.
1045 if (hasCodeCompletionSupport() &&
1046 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
1047 CI.createCodeCompletionConsumer();
1049 // Use a code completion consumer?
1050 CodeCompleteConsumer *CompletionConsumer = nullptr;
1051 if (CI.hasCodeCompletionConsumer())
1052 CompletionConsumer = &CI.getCodeCompletionConsumer();
1055 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
1057 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
1058 CI.getFrontendOpts().SkipFunctionBodies);
1061 void PluginASTAction::anchor() { }
1063 std::unique_ptr<ASTConsumer>
1064 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1066 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1069 bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
1070 return WrappedAction->PrepareToExecuteAction(CI);
1072 std::unique_ptr<ASTConsumer>
1073 WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1075 return WrappedAction->CreateASTConsumer(CI, InFile);
1077 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
1078 return WrappedAction->BeginInvocation(CI);
1080 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
1081 WrappedAction->setCurrentInput(getCurrentInput());
1082 WrappedAction->setCompilerInstance(&CI);
1083 auto Ret = WrappedAction->BeginSourceFileAction(CI);
1084 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1085 setCurrentInput(WrappedAction->getCurrentInput());
1088 void WrapperFrontendAction::ExecuteAction() {
1089 WrappedAction->ExecuteAction();
1091 void WrapperFrontendAction::EndSourceFileAction() {
1092 WrappedAction->EndSourceFileAction();
1094 bool WrapperFrontendAction::shouldEraseOutputFiles() {
1095 return WrappedAction->shouldEraseOutputFiles();
1098 bool WrapperFrontendAction::usesPreprocessorOnly() const {
1099 return WrappedAction->usesPreprocessorOnly();
1101 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
1102 return WrappedAction->getTranslationUnitKind();
1104 bool WrapperFrontendAction::hasPCHSupport() const {
1105 return WrappedAction->hasPCHSupport();
1107 bool WrapperFrontendAction::hasASTFileSupport() const {
1108 return WrappedAction->hasASTFileSupport();
1110 bool WrapperFrontendAction::hasIRSupport() const {
1111 return WrappedAction->hasIRSupport();
1113 bool WrapperFrontendAction::hasCodeCompletionSupport() const {
1114 return WrappedAction->hasCodeCompletionSupport();
1117 WrapperFrontendAction::WrapperFrontendAction(
1118 std::unique_ptr<FrontendAction> WrappedAction)
1119 : WrappedAction(std::move(WrappedAction)) {}