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/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"
31 #include <memory>
32 #include <system_error>
33 
34 using namespace clang;
35 
36 namespace {
37 CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {
38   return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer()
39                                         : nullptr;
40 }
41 
42 void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {
43   if (Action.hasCodeCompletionSupport() &&
44       !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
45     CI.createCodeCompletionConsumer();
46 
47   if (!CI.hasSema())
48     CI.createSema(Action.getTranslationUnitKind(),
49                   GetCodeCompletionConsumer(CI));
50 }
51 } // namespace
52 
53 //===----------------------------------------------------------------------===//
54 // Custom Actions
55 //===----------------------------------------------------------------------===//
56 
57 std::unique_ptr<ASTConsumer>
58 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
59   return std::make_unique<ASTConsumer>();
60 }
61 
62 void InitOnlyAction::ExecuteAction() {
63 }
64 
65 //===----------------------------------------------------------------------===//
66 // AST Consumer Actions
67 //===----------------------------------------------------------------------===//
68 
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);
74   return nullptr;
75 }
76 
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,
83                          Opts.ASTDumpFormat);
84 }
85 
86 std::unique_ptr<ASTConsumer>
87 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
88   return CreateASTDeclNodeLister();
89 }
90 
91 std::unique_ptr<ASTConsumer>
92 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
93   return CreateASTViewer();
94 }
95 
96 std::unique_ptr<ASTConsumer>
97 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
98   std::string Sysroot;
99   if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
100     return nullptr;
101 
102   std::string OutputFile;
103   std::unique_ptr<raw_pwrite_stream> OS =
104       CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
105   if (!OS)
106     return nullptr;
107 
108   if (!CI.getFrontendOpts().RelocatablePCH)
109     Sysroot.clear();
110 
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));
121 
122   return std::make_unique<MultiplexConsumer>(std::move(Consumers));
123 }
124 
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);
130     return false;
131   }
132 
133   return true;
134 }
135 
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);
146   if (!OS)
147     return nullptr;
148 
149   OutputFile = CI.getFrontendOpts().OutputFile;
150   return OS;
151 }
152 
153 bool GeneratePCHAction::shouldEraseOutputFiles() {
154   if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
155     return false;
156   return ASTFrontendAction::shouldEraseOutputFiles();
157 }
158 
159 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
160   CI.getLangOpts().CompilingPCH = true;
161   return true;
162 }
163 
164 std::unique_ptr<ASTConsumer>
165 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
166                                         StringRef InFile) {
167   std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
168   if (!OS)
169     return nullptr;
170 
171   std::string OutputFile = CI.getFrontendOpts().OutputFile;
172   std::string Sysroot;
173 
174   auto Buffer = std::make_shared<PCHBuffer>();
175   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
176 
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));
188 }
189 
190 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
191     CompilerInstance &CI) {
192   if (!CI.getLangOpts().Modules) {
193     CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
194     return false;
195   }
196 
197   return GenerateModuleAction::BeginSourceFileAction(CI);
198 }
199 
200 std::unique_ptr<raw_pwrite_stream>
201 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
202                                                     StringRef InFile) {
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;
209 
210     HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
211     CI.getFrontendOpts().OutputFile =
212         HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
213                                    ModuleMapFile);
214   }
215 
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);
223 }
224 
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);
229     return false;
230   }
231 
232   CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
233 
234   return GenerateModuleAction::BeginSourceFileAction(CI);
235 }
236 
237 std::unique_ptr<raw_pwrite_stream>
238 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
239                                                 StringRef InFile) {
240   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
241 }
242 
243 bool GenerateHeaderModuleAction::PrepareToExecuteAction(
244     CompilerInstance &CI) {
245   if (!CI.getLangOpts().Modules) {
246     CI.getDiagnostics().Report(diag::err_header_module_requires_modules);
247     return false;
248   }
249 
250   auto &Inputs = CI.getFrontendOpts().Inputs;
251   if (Inputs.empty())
252     return GenerateModuleAction::BeginInvocation(CI);
253 
254   auto Kind = Inputs[0].getKind();
255 
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());
265       return true;
266     }
267 
268     HeaderContents += "#include \"";
269     HeaderContents += FIF.getFile();
270     HeaderContents += "\"\n";
271     ModuleHeaders.push_back(std::string(FIF.getFile()));
272   }
273   Buffer = llvm::MemoryBuffer::getMemBufferCopy(
274       HeaderContents, Module::getModuleInputBufferName());
275 
276   // Set that buffer up as our "real" input.
277   Inputs.clear();
278   Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
279 
280   return GenerateModuleAction::PrepareToExecuteAction(CI);
281 }
282 
283 bool GenerateHeaderModuleAction::BeginSourceFileAction(
284     CompilerInstance &CI) {
285   CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderModule);
286 
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);
295     if (!FE) {
296       CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
297         << Name;
298       continue;
299     }
300     Headers.push_back({std::string(Name), &FE->getFileEntry()});
301   }
302   HS.getModuleMap().createHeaderModule(CI.getLangOpts().CurrentModule, Headers);
303 
304   return GenerateModuleAction::BeginSourceFileAction(CI);
305 }
306 
307 std::unique_ptr<raw_pwrite_stream>
308 GenerateHeaderModuleAction::CreateOutputFile(CompilerInstance &CI,
309                                              StringRef InFile) {
310   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
311 }
312 
313 SyntaxOnlyAction::~SyntaxOnlyAction() {
314 }
315 
316 std::unique_ptr<ASTConsumer>
317 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
318   return std::make_unique<ASTConsumer>();
319 }
320 
321 std::unique_ptr<ASTConsumer>
322 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
323                                         StringRef InFile) {
324   return std::make_unique<ASTConsumer>();
325 }
326 
327 std::unique_ptr<ASTConsumer>
328 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
329   return std::make_unique<ASTConsumer>();
330 }
331 
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));
344 
345   Reader->ReadAST(getCurrentFile(),
346                   Preamble ? serialization::MK_Preamble
347                            : serialization::MK_PCH,
348                   SourceLocation(),
349                   ASTReader::ARR_ConfigurationMismatch);
350 }
351 
352 namespace {
353 struct TemplightEntry {
354   std::string Name;
355   std::string Kind;
356   std::string Event;
357   std::string DefinitionLocation;
358   std::string PointOfInstantiation;
359 };
360 } // namespace
361 
362 namespace llvm {
363 namespace yaml {
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);
371   }
372 };
373 } // namespace yaml
374 } // namespace llvm
375 
376 namespace {
377 class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
378   using CodeSynthesisContext = Sema::CodeSynthesisContext;
379 
380 public:
381   void initialize(const Sema &) override {}
382 
383   void finalize(const Sema &) override {}
384 
385   void atTemplateBegin(const Sema &TheSema,
386                        const CodeSynthesisContext &Inst) override {
387     displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
388   }
389 
390   void atTemplateEnd(const Sema &TheSema,
391                      const CodeSynthesisContext &Inst) override {
392     displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
393   }
394 
395 private:
396   static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
397     switch (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";
442     }
443     return "";
444   }
445 
446   template <bool BeginInstantiation>
447   static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
448                                     const CodeSynthesisContext &Inst) {
449     std::string YAML;
450     {
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);
457     }
458     Out << "---" << YAML << "\n";
459   }
460 
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());
476     }
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());
483     }
484     return Entry;
485   }
486 };
487 } // namespace
488 
489 std::unique_ptr<ASTConsumer>
490 TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
491   return std::make_unique<ASTConsumer>();
492 }
493 
494 void TemplightDumpAction::ExecuteAction() {
495   CompilerInstance &CI = getCompilerInstance();
496 
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);
502 
503   CI.getSema().TemplateInstCallbacks.push_back(
504       std::make_unique<DefaultTemplateInstCallback>());
505   ASTFrontendAction::ExecuteAction();
506 }
507 
508 namespace {
509   /// AST reader listener that dumps module information for a module
510   /// file.
511   class DumpModuleInfoListener : public ASTReaderListener {
512     llvm::raw_ostream &Out;
513 
514   public:
515     DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
516 
517 #define DUMP_BOOLEAN(Value, Text)                       \
518     Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
519 
520     bool ReadFullVersionInformation(StringRef FullVersion) override {
521       Out.indent(2)
522         << "Generated by "
523         << (FullVersion == getClangFullRepositoryVersion()? "this"
524                                                           : "a different")
525         << " Clang: " << FullVersion << "\n";
526       return ASTReaderListener::ReadFullVersionInformation(FullVersion);
527     }
528 
529     void ReadModuleName(StringRef ModuleName) override {
530       Out.indent(2) << "Module name: " << ModuleName << "\n";
531     }
532     void ReadModuleMapFile(StringRef ModuleMapPath) override {
533       Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
534     }
535 
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"
549 
550       if (!LangOpts.ModuleFeatures.empty()) {
551         Out.indent(4) << "Module features:\n";
552         for (StringRef Feature : LangOpts.ModuleFeatures)
553           Out.indent(6) << Feature << "\n";
554       }
555 
556       return false;
557     }
558 
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) << "  TuneCPU: " << TargetOpts.TuneCPU << "\n";
565       Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
566 
567       if (!TargetOpts.FeaturesAsWritten.empty()) {
568         Out.indent(4) << "Target features:\n";
569         for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
570              I != N; ++I) {
571           Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
572         }
573       }
574 
575       return false;
576     }
577 
578     bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
579                                bool Complain) override {
580       Out.indent(2) << "Diagnostic options:\n";
581 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
582 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
583       Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
584 #define VALUE_DIAGOPT(Name, Bits, Default) \
585       Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
586 #include "clang/Basic/DiagnosticOptions.def"
587 
588       Out.indent(4) << "Diagnostic flags:\n";
589       for (const std::string &Warning : DiagOpts->Warnings)
590         Out.indent(6) << "-W" << Warning << "\n";
591       for (const std::string &Remark : DiagOpts->Remarks)
592         Out.indent(6) << "-R" << Remark << "\n";
593 
594       return false;
595     }
596 
597     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
598                                  StringRef SpecificModuleCachePath,
599                                  bool Complain) override {
600       Out.indent(2) << "Header search options:\n";
601       Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
602       Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
603       Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
604       DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
605                    "Use builtin include directories [-nobuiltininc]");
606       DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
607                    "Use standard system include directories [-nostdinc]");
608       DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
609                    "Use standard C++ include directories [-nostdinc++]");
610       DUMP_BOOLEAN(HSOpts.UseLibcxx,
611                    "Use libc++ (rather than libstdc++) [-stdlib=]");
612       return false;
613     }
614 
615     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
616                                  bool Complain,
617                                  std::string &SuggestedPredefines) override {
618       Out.indent(2) << "Preprocessor options:\n";
619       DUMP_BOOLEAN(PPOpts.UsePredefines,
620                    "Uses compiler/target-specific predefines [-undef]");
621       DUMP_BOOLEAN(PPOpts.DetailedRecord,
622                    "Uses detailed preprocessing record (for indexing)");
623 
624       if (!PPOpts.Macros.empty()) {
625         Out.indent(4) << "Predefined macros:\n";
626       }
627 
628       for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
629              I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
630            I != IEnd; ++I) {
631         Out.indent(6);
632         if (I->second)
633           Out << "-U";
634         else
635           Out << "-D";
636         Out << I->first << "\n";
637       }
638       return false;
639     }
640 
641     /// Indicates that a particular module file extension has been read.
642     void readModuleFileExtension(
643            const ModuleFileExtensionMetadata &Metadata) override {
644       Out.indent(2) << "Module file extension '"
645                     << Metadata.BlockName << "' " << Metadata.MajorVersion
646                     << "." << Metadata.MinorVersion;
647       if (!Metadata.UserInfo.empty()) {
648         Out << ": ";
649         Out.write_escaped(Metadata.UserInfo);
650       }
651 
652       Out << "\n";
653     }
654 
655     /// Tells the \c ASTReaderListener that we want to receive the
656     /// input files of the AST file via \c visitInputFile.
657     bool needsInputFileVisitation() override { return true; }
658 
659     /// Tells the \c ASTReaderListener that we want to receive the
660     /// input files of the AST file via \c visitInputFile.
661     bool needsSystemInputFileVisitation() override { return true; }
662 
663     /// Indicates that the AST file contains particular input file.
664     ///
665     /// \returns true to continue receiving the next input file, false to stop.
666     bool visitInputFile(StringRef Filename, bool isSystem,
667                         bool isOverridden, bool isExplicitModule) override {
668 
669       Out.indent(2) << "Input file: " << Filename;
670 
671       if (isSystem || isOverridden || isExplicitModule) {
672         Out << " [";
673         if (isSystem) {
674           Out << "System";
675           if (isOverridden || isExplicitModule)
676             Out << ", ";
677         }
678         if (isOverridden) {
679           Out << "Overridden";
680           if (isExplicitModule)
681             Out << ", ";
682         }
683         if (isExplicitModule)
684           Out << "ExplicitModule";
685 
686         Out << "]";
687       }
688 
689       Out << "\n";
690 
691       return true;
692     }
693 
694     /// Returns true if this \c ASTReaderListener wants to receive the
695     /// imports of the AST file via \c visitImport, false otherwise.
696     bool needsImportVisitation() const override { return true; }
697 
698     /// If needsImportVisitation returns \c true, this is called for each
699     /// AST file imported by this AST file.
700     void visitImport(StringRef ModuleName, StringRef Filename) override {
701       Out.indent(2) << "Imports module '" << ModuleName
702                     << "': " << Filename.str() << "\n";
703     }
704 #undef DUMP_BOOLEAN
705   };
706 }
707 
708 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
709   // The Object file reader also supports raw ast files and there is no point in
710   // being strict about the module file format in -module-file-info mode.
711   CI.getHeaderSearchOpts().ModuleFormat = "obj";
712   return true;
713 }
714 
715 void DumpModuleInfoAction::ExecuteAction() {
716   // Set up the output file.
717   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
718   StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
719   if (!OutputFileName.empty() && OutputFileName != "-") {
720     std::error_code EC;
721     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
722                                            llvm::sys::fs::OF_Text));
723   }
724   llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
725 
726   Out << "Information for module file '" << getCurrentFile() << "':\n";
727   auto &FileMgr = getCompilerInstance().getFileManager();
728   auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
729   StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
730   bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
731                 Magic[2] == 'C' && Magic[3] == 'H');
732   Out << "  Module format: " << (IsRaw ? "raw" : "obj") << "\n";
733 
734   Preprocessor &PP = getCompilerInstance().getPreprocessor();
735   DumpModuleInfoListener Listener(Out);
736   HeaderSearchOptions &HSOpts =
737       PP.getHeaderSearchInfo().getHeaderSearchOpts();
738   ASTReader::readASTFileControlBlock(
739       getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
740       /*FindModuleFileExtensions=*/true, Listener,
741       HSOpts.ModulesValidateDiagnosticOptions);
742 }
743 
744 //===----------------------------------------------------------------------===//
745 // Preprocessor Actions
746 //===----------------------------------------------------------------------===//
747 
748 void DumpRawTokensAction::ExecuteAction() {
749   Preprocessor &PP = getCompilerInstance().getPreprocessor();
750   SourceManager &SM = PP.getSourceManager();
751 
752   // Start lexing the specified input file.
753   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
754   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
755   RawLex.SetKeepWhitespaceMode(true);
756 
757   Token RawTok;
758   RawLex.LexFromRawLexer(RawTok);
759   while (RawTok.isNot(tok::eof)) {
760     PP.DumpToken(RawTok, true);
761     llvm::errs() << "\n";
762     RawLex.LexFromRawLexer(RawTok);
763   }
764 }
765 
766 void DumpTokensAction::ExecuteAction() {
767   Preprocessor &PP = getCompilerInstance().getPreprocessor();
768   // Start preprocessing the specified input file.
769   Token Tok;
770   PP.EnterMainSourceFile();
771   do {
772     PP.Lex(Tok);
773     PP.DumpToken(Tok, true);
774     llvm::errs() << "\n";
775   } while (Tok.isNot(tok::eof));
776 }
777 
778 void PreprocessOnlyAction::ExecuteAction() {
779   Preprocessor &PP = getCompilerInstance().getPreprocessor();
780 
781   // Ignore unknown pragmas.
782   PP.IgnorePragmas();
783 
784   Token Tok;
785   // Start parsing the specified input file.
786   PP.EnterMainSourceFile();
787   do {
788     PP.Lex(Tok);
789   } while (Tok.isNot(tok::eof));
790 }
791 
792 void PrintPreprocessedAction::ExecuteAction() {
793   CompilerInstance &CI = getCompilerInstance();
794   // Output file may need to be set to 'Binary', to avoid converting Unix style
795   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
796   //
797   // Look to see what type of line endings the file uses. If there's a
798   // CRLF, then we won't open the file up in binary mode. If there is
799   // just an LF or CR, then we will open the file up in binary mode.
800   // In this fashion, the output format should match the input format, unless
801   // the input format has inconsistent line endings.
802   //
803   // This should be a relatively fast operation since most files won't have
804   // all of their source code on a single line. However, that is still a
805   // concern, so if we scan for too long, we'll just assume the file should
806   // be opened in binary mode.
807   bool BinaryMode = true;
808   bool InvalidFile = false;
809   const SourceManager& SM = CI.getSourceManager();
810   const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
811                                                      &InvalidFile);
812   if (!InvalidFile) {
813     const char *cur = Buffer->getBufferStart();
814     const char *end = Buffer->getBufferEnd();
815     const char *next = (cur != end) ? cur + 1 : end;
816 
817     // Limit ourselves to only scanning 256 characters into the source
818     // file.  This is mostly a sanity check in case the file has no
819     // newlines whatsoever.
820     if (end - cur > 256) end = cur + 256;
821 
822     while (next < end) {
823       if (*cur == 0x0D) {  // CR
824         if (*next == 0x0A)  // CRLF
825           BinaryMode = false;
826 
827         break;
828       } else if (*cur == 0x0A)  // LF
829         break;
830 
831       ++cur;
832       ++next;
833     }
834   }
835 
836   std::unique_ptr<raw_ostream> OS =
837       CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
838   if (!OS) return;
839 
840   // If we're preprocessing a module map, start by dumping the contents of the
841   // module itself before switching to the input buffer.
842   auto &Input = getCurrentInput();
843   if (Input.getKind().getFormat() == InputKind::ModuleMap) {
844     if (Input.isFile()) {
845       (*OS) << "# 1 \"";
846       OS->write_escaped(Input.getFile());
847       (*OS) << "\"\n";
848     }
849     getCurrentModule()->print(*OS);
850     (*OS) << "#pragma clang module contents\n";
851   }
852 
853   DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
854                            CI.getPreprocessorOutputOpts());
855 }
856 
857 void PrintPreambleAction::ExecuteAction() {
858   switch (getCurrentFileKind().getLanguage()) {
859   case Language::C:
860   case Language::CXX:
861   case Language::ObjC:
862   case Language::ObjCXX:
863   case Language::OpenCL:
864   case Language::CUDA:
865   case Language::HIP:
866     break;
867 
868   case Language::Unknown:
869   case Language::Asm:
870   case Language::LLVM_IR:
871   case Language::RenderScript:
872     // We can't do anything with these.
873     return;
874   }
875 
876   // We don't expect to find any #include directives in a preprocessed input.
877   if (getCurrentFileKind().isPreprocessed())
878     return;
879 
880   CompilerInstance &CI = getCompilerInstance();
881   auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
882   if (Buffer) {
883     unsigned Preamble =
884         Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
885     llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
886   }
887 }
888 
889 void DumpCompilerOptionsAction::ExecuteAction() {
890   CompilerInstance &CI = getCompilerInstance();
891   std::unique_ptr<raw_ostream> OSP =
892       CI.createDefaultOutputFile(false, getCurrentFile());
893   if (!OSP)
894     return;
895 
896   raw_ostream &OS = *OSP;
897   const Preprocessor &PP = CI.getPreprocessor();
898   const LangOptions &LangOpts = PP.getLangOpts();
899 
900   // FIXME: Rather than manually format the JSON (which is awkward due to
901   // needing to remove trailing commas), this should make use of a JSON library.
902   // FIXME: Instead of printing enums as an integral value and specifying the
903   // type as a separate field, use introspection to print the enumerator.
904 
905   OS << "{\n";
906   OS << "\n\"features\" : [\n";
907   {
908     llvm::SmallString<128> Str;
909 #define FEATURE(Name, Predicate)                                               \
910   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
911       .toVector(Str);
912 #include "clang/Basic/Features.def"
913 #undef FEATURE
914     // Remove the newline and comma from the last entry to ensure this remains
915     // valid JSON.
916     OS << Str.substr(0, Str.size() - 2);
917   }
918   OS << "\n],\n";
919 
920   OS << "\n\"extensions\" : [\n";
921   {
922     llvm::SmallString<128> Str;
923 #define EXTENSION(Name, Predicate)                                             \
924   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
925       .toVector(Str);
926 #include "clang/Basic/Features.def"
927 #undef EXTENSION
928     // Remove the newline and comma from the last entry to ensure this remains
929     // valid JSON.
930     OS << Str.substr(0, Str.size() - 2);
931   }
932   OS << "\n]\n";
933 
934   OS << "}";
935 }
936 
937 void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
938   CompilerInstance &CI = getCompilerInstance();
939   SourceManager &SM = CI.getPreprocessor().getSourceManager();
940   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
941 
942   llvm::SmallString<1024> Output;
943   llvm::SmallVector<minimize_source_to_dependency_directives::Token, 32> Toks;
944   if (minimizeSourceToDependencyDirectives(
945           FromFile->getBuffer(), Output, Toks, &CI.getDiagnostics(),
946           SM.getLocForStartOfFile(SM.getMainFileID()))) {
947     assert(CI.getDiagnostics().hasErrorOccurred() &&
948            "no errors reported for failure");
949 
950     // Preprocess the source when verifying the diagnostics to capture the
951     // 'expected' comments.
952     if (CI.getDiagnosticOpts().VerifyDiagnostics) {
953       // Make sure we don't emit new diagnostics!
954       CI.getDiagnostics().setSuppressAllDiagnostics(true);
955       Preprocessor &PP = getCompilerInstance().getPreprocessor();
956       PP.EnterMainSourceFile();
957       Token Tok;
958       do {
959         PP.Lex(Tok);
960       } while (Tok.isNot(tok::eof));
961     }
962     return;
963   }
964   llvm::outs() << Output;
965 }
966