llvm-project
1207 строк · 44.0 Кб
1//===--- FrontendActions.cpp ----------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Frontend/FrontendActions.h"10#include "clang/AST/ASTConsumer.h"11#include "clang/AST/Decl.h"12#include "clang/Basic/FileManager.h"13#include "clang/Basic/LangStandard.h"14#include "clang/Basic/Module.h"15#include "clang/Basic/TargetInfo.h"16#include "clang/Frontend/ASTConsumers.h"17#include "clang/Frontend/CompilerInstance.h"18#include "clang/Frontend/FrontendDiagnostic.h"19#include "clang/Frontend/MultiplexConsumer.h"20#include "clang/Frontend/Utils.h"21#include "clang/Lex/DependencyDirectivesScanner.h"22#include "clang/Lex/HeaderSearch.h"23#include "clang/Lex/Preprocessor.h"24#include "clang/Lex/PreprocessorOptions.h"25#include "clang/Sema/TemplateInstCallback.h"26#include "clang/Serialization/ASTReader.h"27#include "clang/Serialization/ASTWriter.h"28#include "clang/Serialization/ModuleFile.h"29#include "llvm/Support/ErrorHandling.h"30#include "llvm/Support/FileSystem.h"31#include "llvm/Support/MemoryBuffer.h"32#include "llvm/Support/Path.h"33#include "llvm/Support/YAMLTraits.h"34#include "llvm/Support/raw_ostream.h"35#include <memory>36#include <optional>37#include <system_error>38
39using namespace clang;40
41namespace {42CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {43return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer()44: nullptr;45}
46
47void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {48if (Action.hasCodeCompletionSupport() &&49!CI.getFrontendOpts().CodeCompletionAt.FileName.empty())50CI.createCodeCompletionConsumer();51
52if (!CI.hasSema())53CI.createSema(Action.getTranslationUnitKind(),54GetCodeCompletionConsumer(CI));55}
56} // namespace57
58//===----------------------------------------------------------------------===//
59// Custom Actions
60//===----------------------------------------------------------------------===//
61
62std::unique_ptr<ASTConsumer>63InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {64return std::make_unique<ASTConsumer>();65}
66
67void InitOnlyAction::ExecuteAction() {68}
69
70// Basically PreprocessOnlyAction::ExecuteAction.
71void ReadPCHAndPreprocessAction::ExecuteAction() {72Preprocessor &PP = getCompilerInstance().getPreprocessor();73
74// Ignore unknown pragmas.75PP.IgnorePragmas();76
77Token Tok;78// Start parsing the specified input file.79PP.EnterMainSourceFile();80do {81PP.Lex(Tok);82} while (Tok.isNot(tok::eof));83}
84
85std::unique_ptr<ASTConsumer>86ReadPCHAndPreprocessAction::CreateASTConsumer(CompilerInstance &CI,87StringRef InFile) {88return std::make_unique<ASTConsumer>();89}
90
91//===----------------------------------------------------------------------===//
92// AST Consumer Actions
93//===----------------------------------------------------------------------===//
94
95std::unique_ptr<ASTConsumer>96ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {97if (std::unique_ptr<raw_ostream> OS =98CI.createDefaultOutputFile(false, InFile))99return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);100return nullptr;101}
102
103std::unique_ptr<ASTConsumer>104ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {105const FrontendOptions &Opts = CI.getFrontendOpts();106return CreateASTDumper(nullptr /*Dump to stdout.*/, Opts.ASTDumpFilter,107Opts.ASTDumpDecls, Opts.ASTDumpAll,108Opts.ASTDumpLookups, Opts.ASTDumpDeclTypes,109Opts.ASTDumpFormat);110}
111
112std::unique_ptr<ASTConsumer>113ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {114return CreateASTDeclNodeLister();115}
116
117std::unique_ptr<ASTConsumer>118ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {119return CreateASTViewer();120}
121
122std::unique_ptr<ASTConsumer>123GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {124std::string Sysroot;125if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))126return nullptr;127
128std::string OutputFile;129std::unique_ptr<raw_pwrite_stream> OS =130CreateOutputFile(CI, InFile, /*ref*/ OutputFile);131if (!OS)132return nullptr;133
134if (!CI.getFrontendOpts().RelocatablePCH)135Sysroot.clear();136
137const auto &FrontendOpts = CI.getFrontendOpts();138auto Buffer = std::make_shared<PCHBuffer>();139std::vector<std::unique_ptr<ASTConsumer>> Consumers;140Consumers.push_back(std::make_unique<PCHGenerator>(141CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,142FrontendOpts.ModuleFileExtensions,143CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,144FrontendOpts.IncludeTimestamps, FrontendOpts.BuildingImplicitModule,145+CI.getLangOpts().CacheGeneratedPCH));146Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(147CI, std::string(InFile), OutputFile, std::move(OS), Buffer));148
149return std::make_unique<MultiplexConsumer>(std::move(Consumers));150}
151
152bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,153std::string &Sysroot) {154Sysroot = CI.getHeaderSearchOpts().Sysroot;155if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {156CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);157return false;158}159
160return true;161}
162
163std::unique_ptr<llvm::raw_pwrite_stream>164GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,165std::string &OutputFile) {166// Because this is exposed via libclang we must disable RemoveFileOnSignal.167std::unique_ptr<raw_pwrite_stream> OS = CI.createDefaultOutputFile(168/*Binary=*/true, InFile, /*Extension=*/"", /*RemoveFileOnSignal=*/false);169if (!OS)170return nullptr;171
172OutputFile = CI.getFrontendOpts().OutputFile;173return OS;174}
175
176bool GeneratePCHAction::shouldEraseOutputFiles() {177if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)178return false;179return ASTFrontendAction::shouldEraseOutputFiles();180}
181
182bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {183CI.getLangOpts().CompilingPCH = true;184return true;185}
186
187std::vector<std::unique_ptr<ASTConsumer>>188GenerateModuleAction::CreateMultiplexConsumer(CompilerInstance &CI,189StringRef InFile) {190std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);191if (!OS)192return {};193
194std::string OutputFile = CI.getFrontendOpts().OutputFile;195std::string Sysroot;196
197auto Buffer = std::make_shared<PCHBuffer>();198std::vector<std::unique_ptr<ASTConsumer>> Consumers;199
200Consumers.push_back(std::make_unique<PCHGenerator>(201CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,202CI.getFrontendOpts().ModuleFileExtensions,203/*AllowASTWithErrors=*/204+CI.getFrontendOpts().AllowPCMWithCompilerErrors,205/*IncludeTimestamps=*/206+CI.getFrontendOpts().BuildingImplicitModule &&207+CI.getFrontendOpts().IncludeTimestamps,208/*BuildingImplicitModule=*/+CI.getFrontendOpts().BuildingImplicitModule,209/*ShouldCacheASTInMemory=*/210+CI.getFrontendOpts().BuildingImplicitModule));211Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(212CI, std::string(InFile), OutputFile, std::move(OS), Buffer));213return Consumers;214}
215
216std::unique_ptr<ASTConsumer>217GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,218StringRef InFile) {219std::vector<std::unique_ptr<ASTConsumer>> Consumers =220CreateMultiplexConsumer(CI, InFile);221if (Consumers.empty())222return nullptr;223
224return std::make_unique<MultiplexConsumer>(std::move(Consumers));225}
226
227bool GenerateModuleAction::shouldEraseOutputFiles() {228return !getCompilerInstance().getFrontendOpts().AllowPCMWithCompilerErrors &&229ASTFrontendAction::shouldEraseOutputFiles();230}
231
232bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(233CompilerInstance &CI) {234if (!CI.getLangOpts().Modules) {235CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);236return false;237}238
239return GenerateModuleAction::BeginSourceFileAction(CI);240}
241
242std::unique_ptr<raw_pwrite_stream>243GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,244StringRef InFile) {245// If no output file was provided, figure out where this module would go246// in the module cache.247if (CI.getFrontendOpts().OutputFile.empty()) {248StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;249if (ModuleMapFile.empty())250ModuleMapFile = InFile;251
252HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();253CI.getFrontendOpts().OutputFile =254HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,255ModuleMapFile);256}257
258// Because this is exposed via libclang we must disable RemoveFileOnSignal.259return CI.createDefaultOutputFile(/*Binary=*/true, InFile, /*Extension=*/"",260/*RemoveFileOnSignal=*/false,261/*CreateMissingDirectories=*/true,262/*ForceUseTemporary=*/true);263}
264
265bool GenerateModuleInterfaceAction::BeginSourceFileAction(266CompilerInstance &CI) {267CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);268
269return GenerateModuleAction::BeginSourceFileAction(CI);270}
271
272std::unique_ptr<ASTConsumer>273GenerateModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,274StringRef InFile) {275std::vector<std::unique_ptr<ASTConsumer>> Consumers;276
277if (CI.getFrontendOpts().GenReducedBMI &&278!CI.getFrontendOpts().ModuleOutputPath.empty()) {279Consumers.push_back(std::make_unique<ReducedBMIGenerator>(280CI.getPreprocessor(), CI.getModuleCache(),281CI.getFrontendOpts().ModuleOutputPath));282}283
284Consumers.push_back(std::make_unique<CXX20ModulesGenerator>(285CI.getPreprocessor(), CI.getModuleCache(),286CI.getFrontendOpts().OutputFile));287
288return std::make_unique<MultiplexConsumer>(std::move(Consumers));289}
290
291std::unique_ptr<raw_pwrite_stream>292GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,293StringRef InFile) {294return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");295}
296
297std::unique_ptr<ASTConsumer>298GenerateReducedModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,299StringRef InFile) {300return std::make_unique<ReducedBMIGenerator>(CI.getPreprocessor(),301CI.getModuleCache(),302CI.getFrontendOpts().OutputFile);303}
304
305bool GenerateHeaderUnitAction::BeginSourceFileAction(CompilerInstance &CI) {306if (!CI.getLangOpts().CPlusPlusModules) {307CI.getDiagnostics().Report(diag::err_module_interface_requires_cpp_modules);308return false;309}310CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderUnit);311return GenerateModuleAction::BeginSourceFileAction(CI);312}
313
314std::unique_ptr<raw_pwrite_stream>315GenerateHeaderUnitAction::CreateOutputFile(CompilerInstance &CI,316StringRef InFile) {317return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");318}
319
320SyntaxOnlyAction::~SyntaxOnlyAction() {321}
322
323std::unique_ptr<ASTConsumer>324SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {325return std::make_unique<ASTConsumer>();326}
327
328std::unique_ptr<ASTConsumer>329DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,330StringRef InFile) {331return std::make_unique<ASTConsumer>();332}
333
334std::unique_ptr<ASTConsumer>335VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {336return std::make_unique<ASTConsumer>();337}
338
339void VerifyPCHAction::ExecuteAction() {340CompilerInstance &CI = getCompilerInstance();341bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;342const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;343std::unique_ptr<ASTReader> Reader(new ASTReader(344CI.getPreprocessor(), CI.getModuleCache(), &CI.getASTContext(),345CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions,346Sysroot.empty() ? "" : Sysroot.c_str(),347DisableValidationForModuleKind::None,348/*AllowASTWithCompilerErrors*/ false,349/*AllowConfigurationMismatch*/ true,350/*ValidateSystemInputs*/ true));351
352Reader->ReadAST(getCurrentFile(),353Preamble ? serialization::MK_Preamble354: serialization::MK_PCH,355SourceLocation(),356ASTReader::ARR_ConfigurationMismatch);357}
358
359namespace {360struct TemplightEntry {361std::string Name;362std::string Kind;363std::string Event;364std::string DefinitionLocation;365std::string PointOfInstantiation;366};367} // namespace368
369namespace llvm {370namespace yaml {371template <> struct MappingTraits<TemplightEntry> {372static void mapping(IO &io, TemplightEntry &fields) {373io.mapRequired("name", fields.Name);374io.mapRequired("kind", fields.Kind);375io.mapRequired("event", fields.Event);376io.mapRequired("orig", fields.DefinitionLocation);377io.mapRequired("poi", fields.PointOfInstantiation);378}379};380} // namespace yaml381} // namespace llvm382
383namespace {384class DefaultTemplateInstCallback : public TemplateInstantiationCallback {385using CodeSynthesisContext = Sema::CodeSynthesisContext;386
387public:388void initialize(const Sema &) override {}389
390void finalize(const Sema &) override {}391
392void atTemplateBegin(const Sema &TheSema,393const CodeSynthesisContext &Inst) override {394displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);395}396
397void atTemplateEnd(const Sema &TheSema,398const CodeSynthesisContext &Inst) override {399displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);400}401
402private:403static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {404switch (Kind) {405case CodeSynthesisContext::TemplateInstantiation:406return "TemplateInstantiation";407case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:408return "DefaultTemplateArgumentInstantiation";409case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:410return "DefaultFunctionArgumentInstantiation";411case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:412return "ExplicitTemplateArgumentSubstitution";413case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:414return "DeducedTemplateArgumentSubstitution";415case CodeSynthesisContext::LambdaExpressionSubstitution:416return "LambdaExpressionSubstitution";417case CodeSynthesisContext::PriorTemplateArgumentSubstitution:418return "PriorTemplateArgumentSubstitution";419case CodeSynthesisContext::DefaultTemplateArgumentChecking:420return "DefaultTemplateArgumentChecking";421case CodeSynthesisContext::ExceptionSpecEvaluation:422return "ExceptionSpecEvaluation";423case CodeSynthesisContext::ExceptionSpecInstantiation:424return "ExceptionSpecInstantiation";425case CodeSynthesisContext::DeclaringSpecialMember:426return "DeclaringSpecialMember";427case CodeSynthesisContext::DeclaringImplicitEqualityComparison:428return "DeclaringImplicitEqualityComparison";429case CodeSynthesisContext::DefiningSynthesizedFunction:430return "DefiningSynthesizedFunction";431case CodeSynthesisContext::RewritingOperatorAsSpaceship:432return "RewritingOperatorAsSpaceship";433case CodeSynthesisContext::Memoization:434return "Memoization";435case CodeSynthesisContext::ConstraintsCheck:436return "ConstraintsCheck";437case CodeSynthesisContext::ConstraintSubstitution:438return "ConstraintSubstitution";439case CodeSynthesisContext::ConstraintNormalization:440return "ConstraintNormalization";441case CodeSynthesisContext::RequirementParameterInstantiation:442return "RequirementParameterInstantiation";443case CodeSynthesisContext::ParameterMappingSubstitution:444return "ParameterMappingSubstitution";445case CodeSynthesisContext::RequirementInstantiation:446return "RequirementInstantiation";447case CodeSynthesisContext::NestedRequirementConstraintsCheck:448return "NestedRequirementConstraintsCheck";449case CodeSynthesisContext::InitializingStructuredBinding:450return "InitializingStructuredBinding";451case CodeSynthesisContext::MarkingClassDllexported:452return "MarkingClassDllexported";453case CodeSynthesisContext::BuildingBuiltinDumpStructCall:454return "BuildingBuiltinDumpStructCall";455case CodeSynthesisContext::BuildingDeductionGuides:456return "BuildingDeductionGuides";457case CodeSynthesisContext::TypeAliasTemplateInstantiation:458return "TypeAliasTemplateInstantiation";459}460return "";461}462
463template <bool BeginInstantiation>464static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,465const CodeSynthesisContext &Inst) {466std::string YAML;467{468llvm::raw_string_ostream OS(YAML);469llvm::yaml::Output YO(OS);470TemplightEntry Entry =471getTemplightEntry<BeginInstantiation>(TheSema, Inst);472llvm::yaml::EmptyContext Context;473llvm::yaml::yamlize(YO, Entry, true, Context);474}475Out << "---" << YAML << "\n";476}477
478static void printEntryName(const Sema &TheSema, const Decl *Entity,479llvm::raw_string_ostream &OS) {480auto *NamedTemplate = cast<NamedDecl>(Entity);481
482PrintingPolicy Policy = TheSema.Context.getPrintingPolicy();483// FIXME: Also ask for FullyQualifiedNames?484Policy.SuppressDefaultTemplateArgs = false;485NamedTemplate->getNameForDiagnostic(OS, Policy, true);486
487if (!OS.str().empty())488return;489
490Decl *Ctx = Decl::castFromDeclContext(NamedTemplate->getDeclContext());491NamedDecl *NamedCtx = dyn_cast_or_null<NamedDecl>(Ctx);492
493if (const auto *Decl = dyn_cast<TagDecl>(NamedTemplate)) {494if (const auto *R = dyn_cast<RecordDecl>(Decl)) {495if (R->isLambda()) {496OS << "lambda at ";497Decl->getLocation().print(OS, TheSema.getSourceManager());498return;499}500}501OS << "unnamed " << Decl->getKindName();502return;503}504
505assert(NamedCtx && "NamedCtx cannot be null");506
507if (const auto *Decl = dyn_cast<ParmVarDecl>(NamedTemplate)) {508OS << "unnamed function parameter " << Decl->getFunctionScopeIndex()509<< " ";510if (Decl->getFunctionScopeDepth() > 0)511OS << "(at depth " << Decl->getFunctionScopeDepth() << ") ";512OS << "of ";513NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);514return;515}516
517if (const auto *Decl = dyn_cast<TemplateTypeParmDecl>(NamedTemplate)) {518if (const Type *Ty = Decl->getTypeForDecl()) {519if (const auto *TTPT = dyn_cast_or_null<TemplateTypeParmType>(Ty)) {520OS << "unnamed template type parameter " << TTPT->getIndex() << " ";521if (TTPT->getDepth() > 0)522OS << "(at depth " << TTPT->getDepth() << ") ";523OS << "of ";524NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);525return;526}527}528}529
530if (const auto *Decl = dyn_cast<NonTypeTemplateParmDecl>(NamedTemplate)) {531OS << "unnamed template non-type parameter " << Decl->getIndex() << " ";532if (Decl->getDepth() > 0)533OS << "(at depth " << Decl->getDepth() << ") ";534OS << "of ";535NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);536return;537}538
539if (const auto *Decl = dyn_cast<TemplateTemplateParmDecl>(NamedTemplate)) {540OS << "unnamed template template parameter " << Decl->getIndex() << " ";541if (Decl->getDepth() > 0)542OS << "(at depth " << Decl->getDepth() << ") ";543OS << "of ";544NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);545return;546}547
548llvm_unreachable("Failed to retrieve a name for this entry!");549OS << "unnamed identifier";550}551
552template <bool BeginInstantiation>553static TemplightEntry getTemplightEntry(const Sema &TheSema,554const CodeSynthesisContext &Inst) {555TemplightEntry Entry;556Entry.Kind = toString(Inst.Kind);557Entry.Event = BeginInstantiation ? "Begin" : "End";558llvm::raw_string_ostream OS(Entry.Name);559printEntryName(TheSema, Inst.Entity, OS);560const PresumedLoc DefLoc =561TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());562if (!DefLoc.isInvalid())563Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +564std::to_string(DefLoc.getLine()) + ":" +565std::to_string(DefLoc.getColumn());566const PresumedLoc PoiLoc =567TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);568if (!PoiLoc.isInvalid()) {569Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +570std::to_string(PoiLoc.getLine()) + ":" +571std::to_string(PoiLoc.getColumn());572}573return Entry;574}575};576} // namespace577
578std::unique_ptr<ASTConsumer>579TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {580return std::make_unique<ASTConsumer>();581}
582
583void TemplightDumpAction::ExecuteAction() {584CompilerInstance &CI = getCompilerInstance();585
586// This part is normally done by ASTFrontEndAction, but needs to happen587// before Templight observers can be created588// FIXME: Move the truncation aspect of this into Sema, we delayed this till589// here so the source manager would be initialized.590EnsureSemaIsCreated(CI, *this);591
592CI.getSema().TemplateInstCallbacks.push_back(593std::make_unique<DefaultTemplateInstCallback>());594ASTFrontendAction::ExecuteAction();595}
596
597namespace {598/// AST reader listener that dumps module information for a module599/// file.600class DumpModuleInfoListener : public ASTReaderListener {601llvm::raw_ostream &Out;602
603public:604DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }605
606#define DUMP_BOOLEAN(Value, Text) \607Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"608
609bool ReadFullVersionInformation(StringRef FullVersion) override {610Out.indent(2)611<< "Generated by "612<< (FullVersion == getClangFullRepositoryVersion()? "this"613: "a different")614<< " Clang: " << FullVersion << "\n";615return ASTReaderListener::ReadFullVersionInformation(FullVersion);616}617
618void ReadModuleName(StringRef ModuleName) override {619Out.indent(2) << "Module name: " << ModuleName << "\n";620}621void ReadModuleMapFile(StringRef ModuleMapPath) override {622Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";623}624
625bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,626bool AllowCompatibleDifferences) override {627Out.indent(2) << "Language options:\n";628#define LANGOPT(Name, Bits, Default, Description) \629DUMP_BOOLEAN(LangOpts.Name, Description);630#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \631Out.indent(4) << Description << ": " \632<< static_cast<unsigned>(LangOpts.get##Name()) << "\n";633#define VALUE_LANGOPT(Name, Bits, Default, Description) \634Out.indent(4) << Description << ": " << LangOpts.Name << "\n";635#define BENIGN_LANGOPT(Name, Bits, Default, Description)636#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)637#include "clang/Basic/LangOptions.def"638
639if (!LangOpts.ModuleFeatures.empty()) {640Out.indent(4) << "Module features:\n";641for (StringRef Feature : LangOpts.ModuleFeatures)642Out.indent(6) << Feature << "\n";643}644
645return false;646}647
648bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,649bool AllowCompatibleDifferences) override {650Out.indent(2) << "Target options:\n";651Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";652Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";653Out.indent(4) << " TuneCPU: " << TargetOpts.TuneCPU << "\n";654Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";655
656if (!TargetOpts.FeaturesAsWritten.empty()) {657Out.indent(4) << "Target features:\n";658for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();659I != N; ++I) {660Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";661}662}663
664return false;665}666
667bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,668bool Complain) override {669Out.indent(2) << "Diagnostic options:\n";670#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);671#define ENUM_DIAGOPT(Name, Type, Bits, Default) \672Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";673#define VALUE_DIAGOPT(Name, Bits, Default) \674Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";675#include "clang/Basic/DiagnosticOptions.def"676
677Out.indent(4) << "Diagnostic flags:\n";678for (const std::string &Warning : DiagOpts->Warnings)679Out.indent(6) << "-W" << Warning << "\n";680for (const std::string &Remark : DiagOpts->Remarks)681Out.indent(6) << "-R" << Remark << "\n";682
683return false;684}685
686bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,687StringRef SpecificModuleCachePath,688bool Complain) override {689Out.indent(2) << "Header search options:\n";690Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";691Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";692Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";693DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,694"Use builtin include directories [-nobuiltininc]");695DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,696"Use standard system include directories [-nostdinc]");697DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,698"Use standard C++ include directories [-nostdinc++]");699DUMP_BOOLEAN(HSOpts.UseLibcxx,700"Use libc++ (rather than libstdc++) [-stdlib=]");701return false;702}703
704bool ReadHeaderSearchPaths(const HeaderSearchOptions &HSOpts,705bool Complain) override {706Out.indent(2) << "Header search paths:\n";707Out.indent(4) << "User entries:\n";708for (const auto &Entry : HSOpts.UserEntries)709Out.indent(6) << Entry.Path << "\n";710Out.indent(4) << "System header prefixes:\n";711for (const auto &Prefix : HSOpts.SystemHeaderPrefixes)712Out.indent(6) << Prefix.Prefix << "\n";713Out.indent(4) << "VFS overlay files:\n";714for (const auto &Overlay : HSOpts.VFSOverlayFiles)715Out.indent(6) << Overlay << "\n";716return false;717}718
719bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,720bool ReadMacros, bool Complain,721std::string &SuggestedPredefines) override {722Out.indent(2) << "Preprocessor options:\n";723DUMP_BOOLEAN(PPOpts.UsePredefines,724"Uses compiler/target-specific predefines [-undef]");725DUMP_BOOLEAN(PPOpts.DetailedRecord,726"Uses detailed preprocessing record (for indexing)");727
728if (ReadMacros) {729Out.indent(4) << "Predefined macros:\n";730}731
732for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator733I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();734I != IEnd; ++I) {735Out.indent(6);736if (I->second)737Out << "-U";738else739Out << "-D";740Out << I->first << "\n";741}742return false;743}744
745/// Indicates that a particular module file extension has been read.746void readModuleFileExtension(747const ModuleFileExtensionMetadata &Metadata) override {748Out.indent(2) << "Module file extension '"749<< Metadata.BlockName << "' " << Metadata.MajorVersion750<< "." << Metadata.MinorVersion;751if (!Metadata.UserInfo.empty()) {752Out << ": ";753Out.write_escaped(Metadata.UserInfo);754}755
756Out << "\n";757}758
759/// Tells the \c ASTReaderListener that we want to receive the760/// input files of the AST file via \c visitInputFile.761bool needsInputFileVisitation() override { return true; }762
763/// Tells the \c ASTReaderListener that we want to receive the764/// input files of the AST file via \c visitInputFile.765bool needsSystemInputFileVisitation() override { return true; }766
767/// Indicates that the AST file contains particular input file.768///769/// \returns true to continue receiving the next input file, false to stop.770bool visitInputFile(StringRef Filename, bool isSystem,771bool isOverridden, bool isExplicitModule) override {772
773Out.indent(2) << "Input file: " << Filename;774
775if (isSystem || isOverridden || isExplicitModule) {776Out << " [";777if (isSystem) {778Out << "System";779if (isOverridden || isExplicitModule)780Out << ", ";781}782if (isOverridden) {783Out << "Overridden";784if (isExplicitModule)785Out << ", ";786}787if (isExplicitModule)788Out << "ExplicitModule";789
790Out << "]";791}792
793Out << "\n";794
795return true;796}797
798/// Returns true if this \c ASTReaderListener wants to receive the799/// imports of the AST file via \c visitImport, false otherwise.800bool needsImportVisitation() const override { return true; }801
802/// If needsImportVisitation returns \c true, this is called for each803/// AST file imported by this AST file.804void visitImport(StringRef ModuleName, StringRef Filename) override {805Out.indent(2) << "Imports module '" << ModuleName806<< "': " << Filename.str() << "\n";807}808#undef DUMP_BOOLEAN809};810}
811
812bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {813// The Object file reader also supports raw ast files and there is no point in814// being strict about the module file format in -module-file-info mode.815CI.getHeaderSearchOpts().ModuleFormat = "obj";816return true;817}
818
819static StringRef ModuleKindName(Module::ModuleKind MK) {820switch (MK) {821case Module::ModuleMapModule:822return "Module Map Module";823case Module::ModuleInterfaceUnit:824return "Interface Unit";825case Module::ModuleImplementationUnit:826return "Implementation Unit";827case Module::ModulePartitionInterface:828return "Partition Interface";829case Module::ModulePartitionImplementation:830return "Partition Implementation";831case Module::ModuleHeaderUnit:832return "Header Unit";833case Module::ExplicitGlobalModuleFragment:834return "Global Module Fragment";835case Module::ImplicitGlobalModuleFragment:836return "Implicit Module Fragment";837case Module::PrivateModuleFragment:838return "Private Module Fragment";839}840llvm_unreachable("unknown module kind!");841}
842
843void DumpModuleInfoAction::ExecuteAction() {844assert(isCurrentFileAST() && "dumping non-AST?");845// Set up the output file.846CompilerInstance &CI = getCompilerInstance();847StringRef OutputFileName = CI.getFrontendOpts().OutputFile;848if (!OutputFileName.empty() && OutputFileName != "-") {849std::error_code EC;850OutputStream.reset(new llvm::raw_fd_ostream(851OutputFileName.str(), EC, llvm::sys::fs::OF_TextWithCRLF));852}853llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs();854
855Out << "Information for module file '" << getCurrentFile() << "':\n";856auto &FileMgr = CI.getFileManager();857auto Buffer = FileMgr.getBufferForFile(getCurrentFile());858StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();859bool IsRaw = Magic.starts_with("CPCH");860Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";861
862Preprocessor &PP = CI.getPreprocessor();863DumpModuleInfoListener Listener(Out);864HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();865
866// The FrontendAction::BeginSourceFile () method loads the AST so that much867// of the information is already available and modules should have been868// loaded.869
870const LangOptions &LO = getCurrentASTUnit().getLangOpts();871if (LO.CPlusPlusModules && !LO.CurrentModule.empty()) {872ASTReader *R = getCurrentASTUnit().getASTReader().get();873unsigned SubModuleCount = R->getTotalNumSubmodules();874serialization::ModuleFile &MF = R->getModuleManager().getPrimaryModule();875Out << " ====== C++20 Module structure ======\n";876
877if (MF.ModuleName != LO.CurrentModule)878Out << " Mismatched module names : " << MF.ModuleName << " and "879<< LO.CurrentModule << "\n";880
881struct SubModInfo {882unsigned Idx;883Module *Mod;884Module::ModuleKind Kind;885std::string &Name;886bool Seen;887};888std::map<std::string, SubModInfo> SubModMap;889auto PrintSubMapEntry = [&](std::string Name, Module::ModuleKind Kind) {890Out << " " << ModuleKindName(Kind) << " '" << Name << "'";891auto I = SubModMap.find(Name);892if (I == SubModMap.end())893Out << " was not found in the sub modules!\n";894else {895I->second.Seen = true;896Out << " is at index #" << I->second.Idx << "\n";897}898};899Module *Primary = nullptr;900for (unsigned Idx = 0; Idx <= SubModuleCount; ++Idx) {901Module *M = R->getModule(Idx);902if (!M)903continue;904if (M->Name == LO.CurrentModule) {905Primary = M;906Out << " " << ModuleKindName(M->Kind) << " '" << LO.CurrentModule907<< "' is the Primary Module at index #" << Idx << "\n";908SubModMap.insert({M->Name, {Idx, M, M->Kind, M->Name, true}});909} else910SubModMap.insert({M->Name, {Idx, M, M->Kind, M->Name, false}});911}912if (Primary) {913if (!Primary->submodules().empty())914Out << " Sub Modules:\n";915for (auto *MI : Primary->submodules()) {916PrintSubMapEntry(MI->Name, MI->Kind);917}918if (!Primary->Imports.empty())919Out << " Imports:\n";920for (auto *IMP : Primary->Imports) {921PrintSubMapEntry(IMP->Name, IMP->Kind);922}923if (!Primary->Exports.empty())924Out << " Exports:\n";925for (unsigned MN = 0, N = Primary->Exports.size(); MN != N; ++MN) {926if (Module *M = Primary->Exports[MN].getPointer()) {927PrintSubMapEntry(M->Name, M->Kind);928}929}930}931
932// Emit the macro definitions in the module file so that we can know how933// much definitions in the module file quickly.934// TODO: Emit the macro definition bodies completely.935if (auto FilteredMacros = llvm::make_filter_range(936R->getPreprocessor().macros(),937[](const auto &Macro) { return Macro.first->isFromAST(); });938!FilteredMacros.empty()) {939Out << " Macro Definitions:\n";940for (/*<IdentifierInfo *, MacroState> pair*/ const auto &Macro :941FilteredMacros)942Out << " " << Macro.first->getName() << "\n";943}944
945// Now let's print out any modules we did not see as part of the Primary.946for (const auto &SM : SubModMap) {947if (!SM.second.Seen && SM.second.Mod) {948Out << " " << ModuleKindName(SM.second.Kind) << " '" << SM.first949<< "' at index #" << SM.second.Idx950<< " has no direct reference in the Primary\n";951}952}953Out << " ====== ======\n";954}955
956// The reminder of the output is produced from the listener as the AST957// FileCcontrolBlock is (re-)parsed.958ASTReader::readASTFileControlBlock(959getCurrentFile(), FileMgr, CI.getModuleCache(),960CI.getPCHContainerReader(),961/*FindModuleFileExtensions=*/true, Listener,962HSOpts.ModulesValidateDiagnosticOptions);963}
964
965//===----------------------------------------------------------------------===//
966// Preprocessor Actions
967//===----------------------------------------------------------------------===//
968
969void DumpRawTokensAction::ExecuteAction() {970Preprocessor &PP = getCompilerInstance().getPreprocessor();971SourceManager &SM = PP.getSourceManager();972
973// Start lexing the specified input file.974llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());975Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());976RawLex.SetKeepWhitespaceMode(true);977
978Token RawTok;979RawLex.LexFromRawLexer(RawTok);980while (RawTok.isNot(tok::eof)) {981PP.DumpToken(RawTok, true);982llvm::errs() << "\n";983RawLex.LexFromRawLexer(RawTok);984}985}
986
987void DumpTokensAction::ExecuteAction() {988Preprocessor &PP = getCompilerInstance().getPreprocessor();989// Start preprocessing the specified input file.990Token Tok;991PP.EnterMainSourceFile();992do {993PP.Lex(Tok);994PP.DumpToken(Tok, true);995llvm::errs() << "\n";996} while (Tok.isNot(tok::eof));997}
998
999void PreprocessOnlyAction::ExecuteAction() {1000Preprocessor &PP = getCompilerInstance().getPreprocessor();1001
1002// Ignore unknown pragmas.1003PP.IgnorePragmas();1004
1005Token Tok;1006// Start parsing the specified input file.1007PP.EnterMainSourceFile();1008do {1009PP.Lex(Tok);1010} while (Tok.isNot(tok::eof));1011}
1012
1013void PrintPreprocessedAction::ExecuteAction() {1014CompilerInstance &CI = getCompilerInstance();1015// Output file may need to be set to 'Binary', to avoid converting Unix style1016// line feeds (<LF>) to Microsoft style line feeds (<CR><LF>) on Windows.1017//1018// Look to see what type of line endings the file uses. If there's a1019// CRLF, then we won't open the file up in binary mode. If there is1020// just an LF or CR, then we will open the file up in binary mode.1021// In this fashion, the output format should match the input format, unless1022// the input format has inconsistent line endings.1023//1024// This should be a relatively fast operation since most files won't have1025// all of their source code on a single line. However, that is still a1026// concern, so if we scan for too long, we'll just assume the file should1027// be opened in binary mode.1028
1029bool BinaryMode = false;1030if (llvm::Triple(LLVM_HOST_TRIPLE).isOSWindows()) {1031BinaryMode = true;1032const SourceManager &SM = CI.getSourceManager();1033if (std::optional<llvm::MemoryBufferRef> Buffer =1034SM.getBufferOrNone(SM.getMainFileID())) {1035const char *cur = Buffer->getBufferStart();1036const char *end = Buffer->getBufferEnd();1037const char *next = (cur != end) ? cur + 1 : end;1038
1039// Limit ourselves to only scanning 256 characters into the source1040// file. This is mostly a check in case the file has no1041// newlines whatsoever.1042if (end - cur > 256)1043end = cur + 256;1044
1045while (next < end) {1046if (*cur == 0x0D) { // CR1047if (*next == 0x0A) // CRLF1048BinaryMode = false;1049
1050break;1051} else if (*cur == 0x0A) // LF1052break;1053
1054++cur;1055++next;1056}1057}1058}1059
1060std::unique_ptr<raw_ostream> OS =1061CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());1062if (!OS) return;1063
1064// If we're preprocessing a module map, start by dumping the contents of the1065// module itself before switching to the input buffer.1066auto &Input = getCurrentInput();1067if (Input.getKind().getFormat() == InputKind::ModuleMap) {1068if (Input.isFile()) {1069(*OS) << "# 1 \"";1070OS->write_escaped(Input.getFile());1071(*OS) << "\"\n";1072}1073getCurrentModule()->print(*OS);1074(*OS) << "#pragma clang module contents\n";1075}1076
1077DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),1078CI.getPreprocessorOutputOpts());1079}
1080
1081void PrintPreambleAction::ExecuteAction() {1082switch (getCurrentFileKind().getLanguage()) {1083case Language::C:1084case Language::CXX:1085case Language::ObjC:1086case Language::ObjCXX:1087case Language::OpenCL:1088case Language::OpenCLCXX:1089case Language::CUDA:1090case Language::HIP:1091case Language::HLSL:1092case Language::CIR:1093break;1094
1095case Language::Unknown:1096case Language::Asm:1097case Language::LLVM_IR:1098case Language::RenderScript:1099// We can't do anything with these.1100return;1101}1102
1103// We don't expect to find any #include directives in a preprocessed input.1104if (getCurrentFileKind().isPreprocessed())1105return;1106
1107CompilerInstance &CI = getCompilerInstance();1108auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());1109if (Buffer) {1110unsigned Preamble =1111Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;1112llvm::outs().write((*Buffer)->getBufferStart(), Preamble);1113}1114}
1115
1116void DumpCompilerOptionsAction::ExecuteAction() {1117CompilerInstance &CI = getCompilerInstance();1118std::unique_ptr<raw_ostream> OSP =1119CI.createDefaultOutputFile(false, getCurrentFile());1120if (!OSP)1121return;1122
1123raw_ostream &OS = *OSP;1124const Preprocessor &PP = CI.getPreprocessor();1125const LangOptions &LangOpts = PP.getLangOpts();1126
1127// FIXME: Rather than manually format the JSON (which is awkward due to1128// needing to remove trailing commas), this should make use of a JSON library.1129// FIXME: Instead of printing enums as an integral value and specifying the1130// type as a separate field, use introspection to print the enumerator.1131
1132OS << "{\n";1133OS << "\n\"features\" : [\n";1134{1135llvm::SmallString<128> Str;1136#define FEATURE(Name, Predicate) \1137("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \1138.toVector(Str);1139#include "clang/Basic/Features.def"1140#undef FEATURE1141// Remove the newline and comma from the last entry to ensure this remains1142// valid JSON.1143OS << Str.substr(0, Str.size() - 2);1144}1145OS << "\n],\n";1146
1147OS << "\n\"extensions\" : [\n";1148{1149llvm::SmallString<128> Str;1150#define EXTENSION(Name, Predicate) \1151("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \1152.toVector(Str);1153#include "clang/Basic/Features.def"1154#undef EXTENSION1155// Remove the newline and comma from the last entry to ensure this remains1156// valid JSON.1157OS << Str.substr(0, Str.size() - 2);1158}1159OS << "\n]\n";1160
1161OS << "}";1162}
1163
1164void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {1165CompilerInstance &CI = getCompilerInstance();1166SourceManager &SM = CI.getPreprocessor().getSourceManager();1167llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());1168
1169llvm::SmallVector<dependency_directives_scan::Token, 16> Tokens;1170llvm::SmallVector<dependency_directives_scan::Directive, 32> Directives;1171if (scanSourceForDependencyDirectives(1172FromFile.getBuffer(), Tokens, Directives, &CI.getDiagnostics(),1173SM.getLocForStartOfFile(SM.getMainFileID()))) {1174assert(CI.getDiagnostics().hasErrorOccurred() &&1175"no errors reported for failure");1176
1177// Preprocess the source when verifying the diagnostics to capture the1178// 'expected' comments.1179if (CI.getDiagnosticOpts().VerifyDiagnostics) {1180// Make sure we don't emit new diagnostics!1181CI.getDiagnostics().setSuppressAllDiagnostics(true);1182Preprocessor &PP = getCompilerInstance().getPreprocessor();1183PP.EnterMainSourceFile();1184Token Tok;1185do {1186PP.Lex(Tok);1187} while (Tok.isNot(tok::eof));1188}1189return;1190}1191printDependencyDirectivesAsSource(FromFile.getBuffer(), Directives,1192llvm::outs());1193}
1194
1195void GetDependenciesByModuleNameAction::ExecuteAction() {1196CompilerInstance &CI = getCompilerInstance();1197Preprocessor &PP = CI.getPreprocessor();1198SourceManager &SM = PP.getSourceManager();1199FileID MainFileID = SM.getMainFileID();1200SourceLocation FileStart = SM.getLocForStartOfFile(MainFileID);1201SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;1202IdentifierInfo *ModuleID = PP.getIdentifierInfo(ModuleName);1203Path.push_back(std::make_pair(ModuleID, FileStart));1204auto ModResult = CI.loadModule(FileStart, Path, Module::Hidden, false);1205PPCallbacks *CB = PP.getPPCallbacks();1206CB->moduleImport(SourceLocation(), Path, ModResult);1207}
1208