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/Preprocessor.h"
23 #include "clang/Lex/PreprocessorOptions.h"
24 #include "clang/Parse/ParseAST.h"
25 #include "clang/Serialization/ASTDeserializationListener.h"
26 #include "clang/Serialization/ASTReader.h"
27 #include "clang/Serialization/GlobalModuleIndex.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/Path.h"
31 #include "llvm/Support/Timer.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include <system_error>
34 using namespace clang;
36 LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
40 class DelegatingDeserializationListener : public ASTDeserializationListener {
41 ASTDeserializationListener *Previous;
45 explicit DelegatingDeserializationListener(
46 ASTDeserializationListener *Previous, bool DeletePrevious)
47 : Previous(Previous), DeletePrevious(DeletePrevious) {}
48 ~DelegatingDeserializationListener() override {
53 void ReaderInitialized(ASTReader *Reader) override {
55 Previous->ReaderInitialized(Reader);
57 void IdentifierRead(serialization::IdentID ID,
58 IdentifierInfo *II) override {
60 Previous->IdentifierRead(ID, II);
62 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
64 Previous->TypeRead(Idx, T);
66 void DeclRead(serialization::DeclID ID, const Decl *D) override {
68 Previous->DeclRead(ID, D);
70 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
72 Previous->SelectorRead(ID, Sel);
74 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
75 MacroDefinitionRecord *MD) override {
77 Previous->MacroDefinitionRead(PPID, MD);
81 /// \brief Dumps deserialized declarations.
82 class DeserializedDeclsDumper : public DelegatingDeserializationListener {
84 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
86 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
88 void DeclRead(serialization::DeclID ID, const Decl *D) override {
89 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
90 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
91 llvm::outs() << " - " << *ND;
94 DelegatingDeserializationListener::DeclRead(ID, D);
98 /// \brief Checks deserialized declarations and emits error if a name
99 /// matches one given in command-line using -error-on-deserialized-decl.
100 class DeserializedDeclsChecker : public DelegatingDeserializationListener {
102 std::set<std::string> NamesToCheck;
105 DeserializedDeclsChecker(ASTContext &Ctx,
106 const std::set<std::string> &NamesToCheck,
107 ASTDeserializationListener *Previous,
109 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
110 NamesToCheck(NamesToCheck) {}
112 void DeclRead(serialization::DeclID ID, const Decl *D) override {
113 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
114 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
116 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
117 "%0 was deserialized");
118 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
119 << ND->getNameAsString();
122 DelegatingDeserializationListener::DeclRead(ID, D);
126 } // end anonymous namespace
128 FrontendAction::FrontendAction() : Instance(nullptr) {}
130 FrontendAction::~FrontendAction() {}
132 void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
133 std::unique_ptr<ASTUnit> AST) {
134 this->CurrentInput = CurrentInput;
135 CurrentASTUnit = std::move(AST);
138 std::unique_ptr<ASTConsumer>
139 FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
141 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
145 // If there are no registered plugins we don't need to wrap the consumer
146 if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
149 // Collect the list of plugins that go before the main action (in Consumers)
150 // or after it (in AfterConsumers)
151 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
152 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
153 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
154 ie = FrontendPluginRegistry::end();
156 std::unique_ptr<PluginASTAction> P = it->instantiate();
157 PluginASTAction::ActionType ActionType = P->getActionType();
158 if (ActionType == PluginASTAction::Cmdline) {
159 // This is O(|plugins| * |add_plugins|), but since both numbers are
160 // way below 50 in practice, that's ok.
161 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
163 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
164 ActionType = PluginASTAction::AddAfterMainAction;
169 if ((ActionType == PluginASTAction::AddBeforeMainAction ||
170 ActionType == PluginASTAction::AddAfterMainAction) &&
171 P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) {
172 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
173 if (ActionType == PluginASTAction::AddBeforeMainAction) {
174 Consumers.push_back(std::move(PluginConsumer));
176 AfterConsumers.push_back(std::move(PluginConsumer));
181 // Add to Consumers the main consumer, then all the plugins that go after it
182 Consumers.push_back(std::move(Consumer));
183 for (auto &C : AfterConsumers) {
184 Consumers.push_back(std::move(C));
187 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
190 bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
191 const FrontendInputFile &Input) {
192 assert(!Instance && "Already processing a source file!");
193 assert(!Input.isEmpty() && "Unexpected empty filename!");
194 setCurrentInput(Input);
195 setCompilerInstance(&CI);
197 StringRef InputFile = Input.getFile();
198 bool HasBegunSourceFile = false;
199 if (!BeginInvocation(CI))
202 // AST files follow a very different path, since they share objects via the
204 if (Input.getKind() == IK_AST) {
205 assert(!usesPreprocessorOnly() &&
206 "Attempt to pass AST file to preprocessor only action!");
207 assert(hasASTFileSupport() &&
208 "This action does not have AST file support!");
210 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
212 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
213 InputFile, CI.getPCHContainerReader(), Diags, CI.getFileSystemOpts(),
214 CI.getCodeGenOpts().DebugTypeExtRefs);
219 // Inform the diagnostic client we are processing a source file.
220 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
221 HasBegunSourceFile = true;
223 // Set the shared objects, these are reset when we finish processing the
224 // file, otherwise the CompilerInstance will happily destroy them.
225 CI.setFileManager(&AST->getFileManager());
226 CI.setSourceManager(&AST->getSourceManager());
227 CI.setPreprocessor(&AST->getPreprocessor());
228 CI.setASTContext(&AST->getASTContext());
230 setCurrentInput(Input, std::move(AST));
232 // Initialize the action.
233 if (!BeginSourceFileAction(CI, InputFile))
236 // Create the AST consumer.
237 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
238 if (!CI.hasASTConsumer())
244 if (!CI.hasVirtualFileSystem()) {
245 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
246 createVFSFromCompilerInvocation(CI.getInvocation(),
247 CI.getDiagnostics()))
248 CI.setVirtualFileSystem(VFS);
253 // Set up the file and source managers, if needed.
254 if (!CI.hasFileManager())
255 CI.createFileManager();
256 if (!CI.hasSourceManager())
257 CI.createSourceManager(CI.getFileManager());
259 // IR files bypass the rest of initialization.
260 if (Input.getKind() == IK_LLVM_IR) {
261 assert(hasIRSupport() &&
262 "This action does not have IR file support!");
264 // Inform the diagnostic client we are processing a source file.
265 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
266 HasBegunSourceFile = true;
268 // Initialize the action.
269 if (!BeginSourceFileAction(CI, InputFile))
272 // Initialize the main file entry.
273 if (!CI.InitializeSourceManager(CurrentInput))
279 // If the implicit PCH include is actually a directory, rather than
280 // a single file, search for a suitable PCH file in that directory.
281 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
282 FileManager &FileMgr = CI.getFileManager();
283 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
284 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
285 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
286 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
288 SmallString<128> DirNative;
289 llvm::sys::path::native(PCHDir->getName(), DirNative);
291 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
292 for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
293 Dir != DirEnd && !EC; Dir.increment(EC)) {
294 // Check whether this is an acceptable AST file.
295 if (ASTReader::isAcceptableASTFile(
296 Dir->getName(), FileMgr, CI.getPCHContainerReader(),
297 CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
298 SpecificModuleCachePath)) {
299 PPOpts.ImplicitPCHInclude = Dir->getName();
306 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
312 // Set up the preprocessor if needed. When parsing model files the
313 // preprocessor of the original source is reused.
314 if (!isModelParsingAction())
315 CI.createPreprocessor(getTranslationUnitKind());
317 // Inform the diagnostic client we are processing a source file.
318 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
319 &CI.getPreprocessor());
320 HasBegunSourceFile = true;
322 // Initialize the action.
323 if (!BeginSourceFileAction(CI, InputFile))
326 // Initialize the main file entry. It is important that this occurs after
327 // BeginSourceFileAction, which may change CurrentInput during module builds.
328 if (!CI.InitializeSourceManager(CurrentInput))
331 // Create the AST context and consumer unless this is a preprocessor only
333 if (!usesPreprocessorOnly()) {
334 // Parsing a model file should reuse the existing ASTContext.
335 if (!isModelParsingAction())
336 CI.createASTContext();
338 std::unique_ptr<ASTConsumer> Consumer =
339 CreateWrappedASTConsumer(CI, InputFile);
343 // FIXME: should not overwrite ASTMutationListener when parsing model files?
344 if (!isModelParsingAction())
345 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
347 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
348 // Convert headers to PCH and chain them.
349 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
350 source = createChainedIncludesSource(CI, FinalReader);
353 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
354 CI.getASTContext().setExternalSource(source);
355 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
357 assert(hasPCHSupport() && "This action does not have PCH support!");
358 ASTDeserializationListener *DeserialListener =
359 Consumer->GetASTDeserializationListener();
360 bool DeleteDeserialListener = false;
361 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
362 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
363 DeleteDeserialListener);
364 DeleteDeserialListener = true;
366 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
367 DeserialListener = new DeserializedDeclsChecker(
369 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
370 DeserialListener, DeleteDeserialListener);
371 DeleteDeserialListener = true;
373 CI.createPCHExternalASTSource(
374 CI.getPreprocessorOpts().ImplicitPCHInclude,
375 CI.getPreprocessorOpts().DisablePCHValidation,
376 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
377 DeleteDeserialListener);
378 if (!CI.getASTContext().getExternalSource())
382 CI.setASTConsumer(std::move(Consumer));
383 if (!CI.hasASTConsumer())
387 // Initialize built-in info as long as we aren't using an external AST
389 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
390 Preprocessor &PP = CI.getPreprocessor();
392 // If modules are enabled, create the module manager before creating
393 // any builtins, so that all declarations know that they might be
394 // extended by an external source.
395 if (CI.getLangOpts().Modules)
396 CI.createModuleManager();
398 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
401 // FIXME: If this is a problem, recover from it by creating a multiplex
403 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
404 "modules enabled but created an external source that "
405 "doesn't support modules");
408 // If we were asked to load any module map files, do so now.
409 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
410 if (auto *File = CI.getFileManager().getFile(Filename))
411 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
412 File, /*IsSystem*/false);
414 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
417 // If we were asked to load any module files, do so now.
418 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
419 if (!CI.loadModuleFile(ModuleFile))
422 // If there is a layout overrides file, attach an external AST source that
423 // provides the layouts from that file.
424 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
425 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
426 IntrusiveRefCntPtr<ExternalASTSource>
427 Override(new LayoutOverrideSource(
428 CI.getFrontendOpts().OverrideRecordLayoutsFile));
429 CI.getASTContext().setExternalSource(Override);
434 // If we failed, reset state since the client will not end up calling the
435 // matching EndSourceFile().
437 if (isCurrentFileAST()) {
438 CI.setASTContext(nullptr);
439 CI.setPreprocessor(nullptr);
440 CI.setSourceManager(nullptr);
441 CI.setFileManager(nullptr);
444 if (HasBegunSourceFile)
445 CI.getDiagnosticClient().EndSourceFile();
446 CI.clearOutputFiles(/*EraseFiles=*/true);
447 setCurrentInput(FrontendInputFile());
448 setCompilerInstance(nullptr);
452 bool FrontendAction::Execute() {
453 CompilerInstance &CI = getCompilerInstance();
455 if (CI.hasFrontendTimer()) {
456 llvm::TimeRegion Timer(CI.getFrontendTimer());
459 else ExecuteAction();
461 // If we are supposed to rebuild the global module index, do so now unless
462 // there were any module-build failures.
463 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
464 CI.hasPreprocessor()) {
466 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
468 GlobalModuleIndex::writeIndex(CI.getFileManager(),
469 CI.getPCHContainerReader(), Cache);
475 void FrontendAction::EndSourceFile() {
476 CompilerInstance &CI = getCompilerInstance();
478 // Inform the diagnostic client we are done with this source file.
479 CI.getDiagnosticClient().EndSourceFile();
481 // Inform the preprocessor we are done.
482 if (CI.hasPreprocessor())
483 CI.getPreprocessor().EndSourceFile();
485 // Finalize the action.
486 EndSourceFileAction();
488 // Sema references the ast consumer, so reset sema first.
490 // FIXME: There is more per-file stuff we could just drop here?
491 bool DisableFree = CI.getFrontendOpts().DisableFree;
493 CI.resetAndLeakSema();
494 CI.resetAndLeakASTContext();
495 BuryPointer(CI.takeASTConsumer().get());
498 CI.setASTContext(nullptr);
499 CI.setASTConsumer(nullptr);
502 if (CI.getFrontendOpts().ShowStats) {
503 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
504 CI.getPreprocessor().PrintStats();
505 CI.getPreprocessor().getIdentifierTable().PrintStats();
506 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
507 CI.getSourceManager().PrintStats();
508 llvm::errs() << "\n";
511 // Cleanup the output streams, and erase the output files if instructed by the
513 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
515 if (isCurrentFileAST()) {
517 CI.resetAndLeakPreprocessor();
518 CI.resetAndLeakSourceManager();
519 CI.resetAndLeakFileManager();
521 CI.setPreprocessor(nullptr);
522 CI.setSourceManager(nullptr);
523 CI.setFileManager(nullptr);
527 setCompilerInstance(nullptr);
528 setCurrentInput(FrontendInputFile());
531 bool FrontendAction::shouldEraseOutputFiles() {
532 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
535 //===----------------------------------------------------------------------===//
537 //===----------------------------------------------------------------------===//
539 void ASTFrontendAction::ExecuteAction() {
540 CompilerInstance &CI = getCompilerInstance();
541 if (!CI.hasPreprocessor())
544 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
545 // here so the source manager would be initialized.
546 if (hasCodeCompletionSupport() &&
547 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
548 CI.createCodeCompletionConsumer();
550 // Use a code completion consumer?
551 CodeCompleteConsumer *CompletionConsumer = nullptr;
552 if (CI.hasCodeCompletionConsumer())
553 CompletionConsumer = &CI.getCodeCompletionConsumer();
556 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
558 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
559 CI.getFrontendOpts().SkipFunctionBodies);
562 void PluginASTAction::anchor() { }
564 std::unique_ptr<ASTConsumer>
565 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
567 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
570 std::unique_ptr<ASTConsumer>
571 WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
573 return WrappedAction->CreateASTConsumer(CI, InFile);
575 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
576 return WrappedAction->BeginInvocation(CI);
578 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
579 StringRef Filename) {
580 WrappedAction->setCurrentInput(getCurrentInput());
581 WrappedAction->setCompilerInstance(&CI);
582 auto Ret = WrappedAction->BeginSourceFileAction(CI, Filename);
583 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
584 setCurrentInput(WrappedAction->getCurrentInput());
587 void WrapperFrontendAction::ExecuteAction() {
588 WrappedAction->ExecuteAction();
590 void WrapperFrontendAction::EndSourceFileAction() {
591 WrappedAction->EndSourceFileAction();
594 bool WrapperFrontendAction::usesPreprocessorOnly() const {
595 return WrappedAction->usesPreprocessorOnly();
597 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
598 return WrappedAction->getTranslationUnitKind();
600 bool WrapperFrontendAction::hasPCHSupport() const {
601 return WrappedAction->hasPCHSupport();
603 bool WrapperFrontendAction::hasASTFileSupport() const {
604 return WrappedAction->hasASTFileSupport();
606 bool WrapperFrontendAction::hasIRSupport() const {
607 return WrappedAction->hasIRSupport();
609 bool WrapperFrontendAction::hasCodeCompletionSupport() const {
610 return WrappedAction->hasCodeCompletionSupport();
613 WrapperFrontendAction::WrapperFrontendAction(
614 std::unique_ptr<FrontendAction> WrappedAction)
615 : WrappedAction(std::move(WrappedAction)) {}