1 //===--- FrontendActions.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/FrontendActions.h"
10 #include "clang/AST/ASTConsumer.h"
11 #include "clang/Basic/FileManager.h"
12 #include "clang/Basic/TargetInfo.h"
13 #include "clang/Basic/LangStandard.h"
14 #include "clang/Frontend/ASTConsumers.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/Frontend/MultiplexConsumer.h"
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Lex/DependencyDirectivesSourceMinimizer.h"
20 #include "clang/Lex/HeaderSearch.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Lex/PreprocessorOptions.h"
23 #include "clang/Sema/TemplateInstCallback.h"
24 #include "clang/Serialization/ASTReader.h"
25 #include "clang/Serialization/ASTWriter.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/YAMLTraits.h"
30 #include "llvm/Support/raw_ostream.h"
32 #include <system_error>
34 using namespace clang;
37 CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {
38 return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer()
42 void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {
43 if (Action.hasCodeCompletionSupport() &&
44 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
45 CI.createCodeCompletionConsumer();
48 CI.createSema(Action.getTranslationUnitKind(),
49 GetCodeCompletionConsumer(CI));
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 std::unique_ptr<ASTConsumer>
58 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
59 return std::make_unique<ASTConsumer>();
62 void InitOnlyAction::ExecuteAction() {
65 //===----------------------------------------------------------------------===//
66 // AST Consumer Actions
67 //===----------------------------------------------------------------------===//
69 std::unique_ptr<ASTConsumer>
70 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
71 if (std::unique_ptr<raw_ostream> OS =
72 CI.createDefaultOutputFile(false, InFile))
73 return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
77 std::unique_ptr<ASTConsumer>
78 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
79 const FrontendOptions &Opts = CI.getFrontendOpts();
80 return CreateASTDumper(nullptr /*Dump to stdout.*/, Opts.ASTDumpFilter,
81 Opts.ASTDumpDecls, Opts.ASTDumpAll,
82 Opts.ASTDumpLookups, Opts.ASTDumpDeclTypes,
86 std::unique_ptr<ASTConsumer>
87 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
88 return CreateASTDeclNodeLister();
91 std::unique_ptr<ASTConsumer>
92 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
93 return CreateASTViewer();
96 std::unique_ptr<ASTConsumer>
97 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
99 if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
102 std::string OutputFile;
103 std::unique_ptr<raw_pwrite_stream> OS =
104 CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
108 if (!CI.getFrontendOpts().RelocatablePCH)
111 const auto &FrontendOpts = CI.getFrontendOpts();
112 auto Buffer = std::make_shared<PCHBuffer>();
113 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
114 Consumers.push_back(std::make_unique<PCHGenerator>(
115 CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
116 FrontendOpts.ModuleFileExtensions,
117 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
118 FrontendOpts.IncludeTimestamps, +CI.getLangOpts().CacheGeneratedPCH));
119 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
120 CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
122 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
125 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
126 std::string &Sysroot) {
127 Sysroot = CI.getHeaderSearchOpts().Sysroot;
128 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
129 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
136 std::unique_ptr<llvm::raw_pwrite_stream>
137 GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
138 std::string &OutputFile) {
139 // We use createOutputFile here because this is exposed via libclang, and we
140 // must disable the RemoveFileOnSignal behavior.
141 // We use a temporary to avoid race conditions.
142 std::unique_ptr<raw_pwrite_stream> OS =
143 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
144 /*RemoveFileOnSignal=*/false, InFile,
145 /*Extension=*/"", CI.getFrontendOpts().UseTemporary);
149 OutputFile = CI.getFrontendOpts().OutputFile;
153 bool GeneratePCHAction::shouldEraseOutputFiles() {
154 if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
156 return ASTFrontendAction::shouldEraseOutputFiles();
159 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
160 CI.getLangOpts().CompilingPCH = true;
164 std::unique_ptr<ASTConsumer>
165 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
167 std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
171 std::string OutputFile = CI.getFrontendOpts().OutputFile;
174 auto Buffer = std::make_shared<PCHBuffer>();
175 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
177 Consumers.push_back(std::make_unique<PCHGenerator>(
178 CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
179 CI.getFrontendOpts().ModuleFileExtensions,
180 /*AllowASTWithErrors=*/false,
181 /*IncludeTimestamps=*/
182 +CI.getFrontendOpts().BuildingImplicitModule,
183 /*ShouldCacheASTInMemory=*/
184 +CI.getFrontendOpts().BuildingImplicitModule));
185 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
186 CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
187 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
190 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
191 CompilerInstance &CI) {
192 if (!CI.getLangOpts().Modules) {
193 CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
197 return GenerateModuleAction::BeginSourceFileAction(CI);
200 std::unique_ptr<raw_pwrite_stream>
201 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
203 // If no output file was provided, figure out where this module would go
204 // in the module cache.
205 if (CI.getFrontendOpts().OutputFile.empty()) {
206 StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
207 if (ModuleMapFile.empty())
208 ModuleMapFile = InFile;
210 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
211 CI.getFrontendOpts().OutputFile =
212 HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
216 // We use createOutputFile here because this is exposed via libclang, and we
217 // must disable the RemoveFileOnSignal behavior.
218 // We use a temporary to avoid race conditions.
219 return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
220 /*RemoveFileOnSignal=*/false, InFile,
221 /*Extension=*/"", /*UseTemporary=*/true,
222 /*CreateMissingDirectories=*/true);
225 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
226 CompilerInstance &CI) {
227 if (!CI.getLangOpts().ModulesTS && !CI.getLangOpts().CPlusPlusModules) {
228 CI.getDiagnostics().Report(diag::err_module_interface_requires_cpp_modules);
232 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
234 return GenerateModuleAction::BeginSourceFileAction(CI);
237 std::unique_ptr<raw_pwrite_stream>
238 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
240 return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
243 bool GenerateHeaderModuleAction::PrepareToExecuteAction(
244 CompilerInstance &CI) {
245 if (!CI.getLangOpts().Modules) {
246 CI.getDiagnostics().Report(diag::err_header_module_requires_modules);
250 auto &Inputs = CI.getFrontendOpts().Inputs;
252 return GenerateModuleAction::BeginInvocation(CI);
254 auto Kind = Inputs[0].getKind();
256 // Convert the header file inputs into a single module input buffer.
257 SmallString<256> HeaderContents;
258 ModuleHeaders.reserve(Inputs.size());
259 for (const FrontendInputFile &FIF : Inputs) {
260 // FIXME: We should support re-compiling from an AST file.
261 if (FIF.getKind().getFormat() != InputKind::Source || !FIF.isFile()) {
262 CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
263 << (FIF.isFile() ? FIF.getFile()
264 : FIF.getBuffer()->getBufferIdentifier());
268 HeaderContents += "#include \"";
269 HeaderContents += FIF.getFile();
270 HeaderContents += "\"\n";
271 ModuleHeaders.push_back(std::string(FIF.getFile()));
273 Buffer = llvm::MemoryBuffer::getMemBufferCopy(
274 HeaderContents, Module::getModuleInputBufferName());
276 // Set that buffer up as our "real" input.
278 Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
280 return GenerateModuleAction::PrepareToExecuteAction(CI);
283 bool GenerateHeaderModuleAction::BeginSourceFileAction(
284 CompilerInstance &CI) {
285 CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderModule);
287 // Synthesize a Module object for the given headers.
288 auto &HS = CI.getPreprocessor().getHeaderSearchInfo();
289 SmallVector<Module::Header, 16> Headers;
290 for (StringRef Name : ModuleHeaders) {
291 const DirectoryLookup *CurDir = nullptr;
292 Optional<FileEntryRef> FE = HS.LookupFile(
293 Name, SourceLocation(), /*Angled*/ false, nullptr, CurDir, None,
294 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
296 CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
300 Headers.push_back({std::string(Name), &FE->getFileEntry()});
302 HS.getModuleMap().createHeaderModule(CI.getLangOpts().CurrentModule, Headers);
304 return GenerateModuleAction::BeginSourceFileAction(CI);
307 std::unique_ptr<raw_pwrite_stream>
308 GenerateHeaderModuleAction::CreateOutputFile(CompilerInstance &CI,
310 return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
313 SyntaxOnlyAction::~SyntaxOnlyAction() {
316 std::unique_ptr<ASTConsumer>
317 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
318 return std::make_unique<ASTConsumer>();
321 std::unique_ptr<ASTConsumer>
322 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
324 return std::make_unique<ASTConsumer>();
327 std::unique_ptr<ASTConsumer>
328 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
329 return std::make_unique<ASTConsumer>();
332 void VerifyPCHAction::ExecuteAction() {
333 CompilerInstance &CI = getCompilerInstance();
334 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
335 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
336 std::unique_ptr<ASTReader> Reader(new ASTReader(
337 CI.getPreprocessor(), CI.getModuleCache(), &CI.getASTContext(),
338 CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions,
339 Sysroot.empty() ? "" : Sysroot.c_str(),
340 /*DisableValidation*/ false,
341 /*AllowPCHWithCompilerErrors*/ false,
342 /*AllowConfigurationMismatch*/ true,
343 /*ValidateSystemInputs*/ true));
345 Reader->ReadAST(getCurrentFile(),
346 Preamble ? serialization::MK_Preamble
347 : serialization::MK_PCH,
349 ASTReader::ARR_ConfigurationMismatch);
353 struct TemplightEntry {
357 std::string DefinitionLocation;
358 std::string PointOfInstantiation;
364 template <> struct MappingTraits<TemplightEntry> {
365 static void mapping(IO &io, TemplightEntry &fields) {
366 io.mapRequired("name", fields.Name);
367 io.mapRequired("kind", fields.Kind);
368 io.mapRequired("event", fields.Event);
369 io.mapRequired("orig", fields.DefinitionLocation);
370 io.mapRequired("poi", fields.PointOfInstantiation);
377 class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
378 using CodeSynthesisContext = Sema::CodeSynthesisContext;
381 void initialize(const Sema &) override {}
383 void finalize(const Sema &) override {}
385 void atTemplateBegin(const Sema &TheSema,
386 const CodeSynthesisContext &Inst) override {
387 displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
390 void atTemplateEnd(const Sema &TheSema,
391 const CodeSynthesisContext &Inst) override {
392 displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
396 static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
398 case CodeSynthesisContext::TemplateInstantiation:
399 return "TemplateInstantiation";
400 case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:
401 return "DefaultTemplateArgumentInstantiation";
402 case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:
403 return "DefaultFunctionArgumentInstantiation";
404 case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:
405 return "ExplicitTemplateArgumentSubstitution";
406 case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:
407 return "DeducedTemplateArgumentSubstitution";
408 case CodeSynthesisContext::PriorTemplateArgumentSubstitution:
409 return "PriorTemplateArgumentSubstitution";
410 case CodeSynthesisContext::DefaultTemplateArgumentChecking:
411 return "DefaultTemplateArgumentChecking";
412 case CodeSynthesisContext::ExceptionSpecEvaluation:
413 return "ExceptionSpecEvaluation";
414 case CodeSynthesisContext::ExceptionSpecInstantiation:
415 return "ExceptionSpecInstantiation";
416 case CodeSynthesisContext::DeclaringSpecialMember:
417 return "DeclaringSpecialMember";
418 case CodeSynthesisContext::DeclaringImplicitEqualityComparison:
419 return "DeclaringImplicitEqualityComparison";
420 case CodeSynthesisContext::DefiningSynthesizedFunction:
421 return "DefiningSynthesizedFunction";
422 case CodeSynthesisContext::RewritingOperatorAsSpaceship:
423 return "RewritingOperatorAsSpaceship";
424 case CodeSynthesisContext::Memoization:
425 return "Memoization";
426 case CodeSynthesisContext::ConstraintsCheck:
427 return "ConstraintsCheck";
428 case CodeSynthesisContext::ConstraintSubstitution:
429 return "ConstraintSubstitution";
430 case CodeSynthesisContext::ConstraintNormalization:
431 return "ConstraintNormalization";
432 case CodeSynthesisContext::ParameterMappingSubstitution:
433 return "ParameterMappingSubstitution";
434 case CodeSynthesisContext::RequirementInstantiation:
435 return "RequirementInstantiation";
436 case CodeSynthesisContext::NestedRequirementConstraintsCheck:
437 return "NestedRequirementConstraintsCheck";
438 case CodeSynthesisContext::InitializingStructuredBinding:
439 return "InitializingStructuredBinding";
440 case CodeSynthesisContext::MarkingClassDllexported:
441 return "MarkingClassDllexported";
446 template <bool BeginInstantiation>
447 static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
448 const CodeSynthesisContext &Inst) {
451 llvm::raw_string_ostream OS(YAML);
452 llvm::yaml::Output YO(OS);
453 TemplightEntry Entry =
454 getTemplightEntry<BeginInstantiation>(TheSema, Inst);
455 llvm::yaml::EmptyContext Context;
456 llvm::yaml::yamlize(YO, Entry, true, Context);
458 Out << "---" << YAML << "\n";
461 template <bool BeginInstantiation>
462 static TemplightEntry getTemplightEntry(const Sema &TheSema,
463 const CodeSynthesisContext &Inst) {
464 TemplightEntry Entry;
465 Entry.Kind = toString(Inst.Kind);
466 Entry.Event = BeginInstantiation ? "Begin" : "End";
467 if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
468 llvm::raw_string_ostream OS(Entry.Name);
469 NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
470 const PresumedLoc DefLoc =
471 TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
472 if(!DefLoc.isInvalid())
473 Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
474 std::to_string(DefLoc.getLine()) + ":" +
475 std::to_string(DefLoc.getColumn());
477 const PresumedLoc PoiLoc =
478 TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
479 if (!PoiLoc.isInvalid()) {
480 Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
481 std::to_string(PoiLoc.getLine()) + ":" +
482 std::to_string(PoiLoc.getColumn());
489 std::unique_ptr<ASTConsumer>
490 TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
491 return std::make_unique<ASTConsumer>();
494 void TemplightDumpAction::ExecuteAction() {
495 CompilerInstance &CI = getCompilerInstance();
497 // This part is normally done by ASTFrontEndAction, but needs to happen
498 // before Templight observers can be created
499 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
500 // here so the source manager would be initialized.
501 EnsureSemaIsCreated(CI, *this);
503 CI.getSema().TemplateInstCallbacks.push_back(
504 std::make_unique<DefaultTemplateInstCallback>());
505 ASTFrontendAction::ExecuteAction();
509 /// AST reader listener that dumps module information for a module
511 class DumpModuleInfoListener : public ASTReaderListener {
512 llvm::raw_ostream &Out;
515 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
517 #define DUMP_BOOLEAN(Value, Text) \
518 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
520 bool ReadFullVersionInformation(StringRef FullVersion) override {
523 << (FullVersion == getClangFullRepositoryVersion()? "this"
525 << " Clang: " << FullVersion << "\n";
526 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
529 void ReadModuleName(StringRef ModuleName) override {
530 Out.indent(2) << "Module name: " << ModuleName << "\n";
532 void ReadModuleMapFile(StringRef ModuleMapPath) override {
533 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
536 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
537 bool AllowCompatibleDifferences) override {
538 Out.indent(2) << "Language options:\n";
539 #define LANGOPT(Name, Bits, Default, Description) \
540 DUMP_BOOLEAN(LangOpts.Name, Description);
541 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
542 Out.indent(4) << Description << ": " \
543 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
544 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
545 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
546 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
547 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
548 #include "clang/Basic/LangOptions.def"
550 if (!LangOpts.ModuleFeatures.empty()) {
551 Out.indent(4) << "Module features:\n";
552 for (StringRef Feature : LangOpts.ModuleFeatures)
553 Out.indent(6) << Feature << "\n";
559 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
560 bool AllowCompatibleDifferences) override {
561 Out.indent(2) << "Target options:\n";
562 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
563 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
564 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
566 if (!TargetOpts.FeaturesAsWritten.empty()) {
567 Out.indent(4) << "Target features:\n";
568 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
570 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
577 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
578 bool Complain) override {
579 Out.indent(2) << "Diagnostic options:\n";
580 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
581 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
582 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
583 #define VALUE_DIAGOPT(Name, Bits, Default) \
584 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
585 #include "clang/Basic/DiagnosticOptions.def"
587 Out.indent(4) << "Diagnostic flags:\n";
588 for (const std::string &Warning : DiagOpts->Warnings)
589 Out.indent(6) << "-W" << Warning << "\n";
590 for (const std::string &Remark : DiagOpts->Remarks)
591 Out.indent(6) << "-R" << Remark << "\n";
596 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
597 StringRef SpecificModuleCachePath,
598 bool Complain) override {
599 Out.indent(2) << "Header search options:\n";
600 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
601 Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
602 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
603 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
604 "Use builtin include directories [-nobuiltininc]");
605 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
606 "Use standard system include directories [-nostdinc]");
607 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
608 "Use standard C++ include directories [-nostdinc++]");
609 DUMP_BOOLEAN(HSOpts.UseLibcxx,
610 "Use libc++ (rather than libstdc++) [-stdlib=]");
614 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
616 std::string &SuggestedPredefines) override {
617 Out.indent(2) << "Preprocessor options:\n";
618 DUMP_BOOLEAN(PPOpts.UsePredefines,
619 "Uses compiler/target-specific predefines [-undef]");
620 DUMP_BOOLEAN(PPOpts.DetailedRecord,
621 "Uses detailed preprocessing record (for indexing)");
623 if (!PPOpts.Macros.empty()) {
624 Out.indent(4) << "Predefined macros:\n";
627 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
628 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
635 Out << I->first << "\n";
640 /// Indicates that a particular module file extension has been read.
641 void readModuleFileExtension(
642 const ModuleFileExtensionMetadata &Metadata) override {
643 Out.indent(2) << "Module file extension '"
644 << Metadata.BlockName << "' " << Metadata.MajorVersion
645 << "." << Metadata.MinorVersion;
646 if (!Metadata.UserInfo.empty()) {
648 Out.write_escaped(Metadata.UserInfo);
654 /// Tells the \c ASTReaderListener that we want to receive the
655 /// input files of the AST file via \c visitInputFile.
656 bool needsInputFileVisitation() override { return true; }
658 /// Tells the \c ASTReaderListener that we want to receive the
659 /// input files of the AST file via \c visitInputFile.
660 bool needsSystemInputFileVisitation() override { return true; }
662 /// Indicates that the AST file contains particular input file.
664 /// \returns true to continue receiving the next input file, false to stop.
665 bool visitInputFile(StringRef Filename, bool isSystem,
666 bool isOverridden, bool isExplicitModule) override {
668 Out.indent(2) << "Input file: " << Filename;
670 if (isSystem || isOverridden || isExplicitModule) {
674 if (isOverridden || isExplicitModule)
679 if (isExplicitModule)
682 if (isExplicitModule)
683 Out << "ExplicitModule";
693 /// Returns true if this \c ASTReaderListener wants to receive the
694 /// imports of the AST file via \c visitImport, false otherwise.
695 bool needsImportVisitation() const override { return true; }
697 /// If needsImportVisitation returns \c true, this is called for each
698 /// AST file imported by this AST file.
699 void visitImport(StringRef ModuleName, StringRef Filename) override {
700 Out.indent(2) << "Imports module '" << ModuleName
701 << "': " << Filename.str() << "\n";
707 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
708 // The Object file reader also supports raw ast files and there is no point in
709 // being strict about the module file format in -module-file-info mode.
710 CI.getHeaderSearchOpts().ModuleFormat = "obj";
714 void DumpModuleInfoAction::ExecuteAction() {
715 // Set up the output file.
716 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
717 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
718 if (!OutputFileName.empty() && OutputFileName != "-") {
720 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
721 llvm::sys::fs::OF_Text));
723 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
725 Out << "Information for module file '" << getCurrentFile() << "':\n";
726 auto &FileMgr = getCompilerInstance().getFileManager();
727 auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
728 StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
729 bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
730 Magic[2] == 'C' && Magic[3] == 'H');
731 Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
733 Preprocessor &PP = getCompilerInstance().getPreprocessor();
734 DumpModuleInfoListener Listener(Out);
735 HeaderSearchOptions &HSOpts =
736 PP.getHeaderSearchInfo().getHeaderSearchOpts();
737 ASTReader::readASTFileControlBlock(
738 getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
739 /*FindModuleFileExtensions=*/true, Listener,
740 HSOpts.ModulesValidateDiagnosticOptions);
743 //===----------------------------------------------------------------------===//
744 // Preprocessor Actions
745 //===----------------------------------------------------------------------===//
747 void DumpRawTokensAction::ExecuteAction() {
748 Preprocessor &PP = getCompilerInstance().getPreprocessor();
749 SourceManager &SM = PP.getSourceManager();
751 // Start lexing the specified input file.
752 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
753 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
754 RawLex.SetKeepWhitespaceMode(true);
757 RawLex.LexFromRawLexer(RawTok);
758 while (RawTok.isNot(tok::eof)) {
759 PP.DumpToken(RawTok, true);
760 llvm::errs() << "\n";
761 RawLex.LexFromRawLexer(RawTok);
765 void DumpTokensAction::ExecuteAction() {
766 Preprocessor &PP = getCompilerInstance().getPreprocessor();
767 // Start preprocessing the specified input file.
769 PP.EnterMainSourceFile();
772 PP.DumpToken(Tok, true);
773 llvm::errs() << "\n";
774 } while (Tok.isNot(tok::eof));
777 void PreprocessOnlyAction::ExecuteAction() {
778 Preprocessor &PP = getCompilerInstance().getPreprocessor();
780 // Ignore unknown pragmas.
784 // Start parsing the specified input file.
785 PP.EnterMainSourceFile();
788 } while (Tok.isNot(tok::eof));
791 void PrintPreprocessedAction::ExecuteAction() {
792 CompilerInstance &CI = getCompilerInstance();
793 // Output file may need to be set to 'Binary', to avoid converting Unix style
794 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
796 // Look to see what type of line endings the file uses. If there's a
797 // CRLF, then we won't open the file up in binary mode. If there is
798 // just an LF or CR, then we will open the file up in binary mode.
799 // In this fashion, the output format should match the input format, unless
800 // the input format has inconsistent line endings.
802 // This should be a relatively fast operation since most files won't have
803 // all of their source code on a single line. However, that is still a
804 // concern, so if we scan for too long, we'll just assume the file should
805 // be opened in binary mode.
806 bool BinaryMode = true;
807 bool InvalidFile = false;
808 const SourceManager& SM = CI.getSourceManager();
809 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
812 const char *cur = Buffer->getBufferStart();
813 const char *end = Buffer->getBufferEnd();
814 const char *next = (cur != end) ? cur + 1 : end;
816 // Limit ourselves to only scanning 256 characters into the source
817 // file. This is mostly a sanity check in case the file has no
818 // newlines whatsoever.
819 if (end - cur > 256) end = cur + 256;
822 if (*cur == 0x0D) { // CR
823 if (*next == 0x0A) // CRLF
827 } else if (*cur == 0x0A) // LF
835 std::unique_ptr<raw_ostream> OS =
836 CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
839 // If we're preprocessing a module map, start by dumping the contents of the
840 // module itself before switching to the input buffer.
841 auto &Input = getCurrentInput();
842 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
843 if (Input.isFile()) {
845 OS->write_escaped(Input.getFile());
848 getCurrentModule()->print(*OS);
849 (*OS) << "#pragma clang module contents\n";
852 DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
853 CI.getPreprocessorOutputOpts());
856 void PrintPreambleAction::ExecuteAction() {
857 switch (getCurrentFileKind().getLanguage()) {
861 case Language::ObjCXX:
862 case Language::OpenCL:
867 case Language::Unknown:
869 case Language::LLVM_IR:
870 case Language::RenderScript:
871 // We can't do anything with these.
875 // We don't expect to find any #include directives in a preprocessed input.
876 if (getCurrentFileKind().isPreprocessed())
879 CompilerInstance &CI = getCompilerInstance();
880 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
883 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
884 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
888 void DumpCompilerOptionsAction::ExecuteAction() {
889 CompilerInstance &CI = getCompilerInstance();
890 std::unique_ptr<raw_ostream> OSP =
891 CI.createDefaultOutputFile(false, getCurrentFile());
895 raw_ostream &OS = *OSP;
896 const Preprocessor &PP = CI.getPreprocessor();
897 const LangOptions &LangOpts = PP.getLangOpts();
899 // FIXME: Rather than manually format the JSON (which is awkward due to
900 // needing to remove trailing commas), this should make use of a JSON library.
901 // FIXME: Instead of printing enums as an integral value and specifying the
902 // type as a separate field, use introspection to print the enumerator.
905 OS << "\n\"features\" : [\n";
907 llvm::SmallString<128> Str;
908 #define FEATURE(Name, Predicate) \
909 ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
911 #include "clang/Basic/Features.def"
913 // Remove the newline and comma from the last entry to ensure this remains
915 OS << Str.substr(0, Str.size() - 2);
919 OS << "\n\"extensions\" : [\n";
921 llvm::SmallString<128> Str;
922 #define EXTENSION(Name, Predicate) \
923 ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
925 #include "clang/Basic/Features.def"
927 // Remove the newline and comma from the last entry to ensure this remains
929 OS << Str.substr(0, Str.size() - 2);
936 void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
937 CompilerInstance &CI = getCompilerInstance();
938 SourceManager &SM = CI.getPreprocessor().getSourceManager();
939 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
941 llvm::SmallString<1024> Output;
942 llvm::SmallVector<minimize_source_to_dependency_directives::Token, 32> Toks;
943 if (minimizeSourceToDependencyDirectives(
944 FromFile->getBuffer(), Output, Toks, &CI.getDiagnostics(),
945 SM.getLocForStartOfFile(SM.getMainFileID()))) {
946 assert(CI.getDiagnostics().hasErrorOccurred() &&
947 "no errors reported for failure");
949 // Preprocess the source when verifying the diagnostics to capture the
950 // 'expected' comments.
951 if (CI.getDiagnosticOpts().VerifyDiagnostics) {
952 // Make sure we don't emit new diagnostics!
953 CI.getDiagnostics().setSuppressAllDiagnostics(true);
954 Preprocessor &PP = getCompilerInstance().getPreprocessor();
955 PP.EnterMainSourceFile();
959 } while (Tok.isNot(tok::eof));
963 llvm::outs() << Output;