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/Frontend/ASTConsumers.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Frontend/FrontendDiagnostic.h"
15 #include "clang/Frontend/MultiplexConsumer.h"
16 #include "clang/Frontend/Utils.h"
17 #include "clang/Lex/HeaderSearch.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Lex/PreprocessorOptions.h"
20 #include "clang/Sema/TemplateInstCallback.h"
21 #include "clang/Serialization/ASTReader.h"
22 #include "clang/Serialization/ASTWriter.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/YAMLTraits.h"
28 #include <memory>
29 #include <system_error>
30 
31 using namespace clang;
32 
33 namespace {
34 CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {
35   return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer()
36                                         : nullptr;
37 }
38 
39 void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {
40   if (Action.hasCodeCompletionSupport() &&
41       !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
42     CI.createCodeCompletionConsumer();
43 
44   if (!CI.hasSema())
45     CI.createSema(Action.getTranslationUnitKind(),
46                   GetCodeCompletionConsumer(CI));
47 }
48 } // namespace
49 
50 //===----------------------------------------------------------------------===//
51 // Custom Actions
52 //===----------------------------------------------------------------------===//
53 
54 std::unique_ptr<ASTConsumer>
55 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
56   return llvm::make_unique<ASTConsumer>();
57 }
58 
59 void InitOnlyAction::ExecuteAction() {
60 }
61 
62 //===----------------------------------------------------------------------===//
63 // AST Consumer Actions
64 //===----------------------------------------------------------------------===//
65 
66 std::unique_ptr<ASTConsumer>
67 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
68   if (std::unique_ptr<raw_ostream> OS =
69           CI.createDefaultOutputFile(false, InFile))
70     return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
71   return nullptr;
72 }
73 
74 std::unique_ptr<ASTConsumer>
75 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
76   return CreateASTDumper(nullptr /*Dump to stdout.*/,
77                          CI.getFrontendOpts().ASTDumpFilter,
78                          CI.getFrontendOpts().ASTDumpDecls,
79                          CI.getFrontendOpts().ASTDumpAll,
80                          CI.getFrontendOpts().ASTDumpLookups);
81 }
82 
83 std::unique_ptr<ASTConsumer>
84 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
85   return CreateASTDeclNodeLister();
86 }
87 
88 std::unique_ptr<ASTConsumer>
89 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
90   return CreateASTViewer();
91 }
92 
93 std::unique_ptr<ASTConsumer>
94 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
95   std::string Sysroot;
96   if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
97     return nullptr;
98 
99   std::string OutputFile;
100   std::unique_ptr<raw_pwrite_stream> OS =
101       CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
102   if (!OS)
103     return nullptr;
104 
105   if (!CI.getFrontendOpts().RelocatablePCH)
106     Sysroot.clear();
107 
108   const auto &FrontendOpts = CI.getFrontendOpts();
109   auto Buffer = std::make_shared<PCHBuffer>();
110   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
111   Consumers.push_back(llvm::make_unique<PCHGenerator>(
112                         CI.getPreprocessor(), OutputFile, Sysroot,
113                         Buffer, FrontendOpts.ModuleFileExtensions,
114                         CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
115                         FrontendOpts.IncludeTimestamps));
116   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
117       CI, InFile, OutputFile, std::move(OS), Buffer));
118 
119   return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
120 }
121 
122 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
123                                                     std::string &Sysroot) {
124   Sysroot = CI.getHeaderSearchOpts().Sysroot;
125   if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
126     CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
127     return false;
128   }
129 
130   return true;
131 }
132 
133 std::unique_ptr<llvm::raw_pwrite_stream>
134 GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
135                                     std::string &OutputFile) {
136   // We use createOutputFile here because this is exposed via libclang, and we
137   // must disable the RemoveFileOnSignal behavior.
138   // We use a temporary to avoid race conditions.
139   std::unique_ptr<raw_pwrite_stream> OS =
140       CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
141                           /*RemoveFileOnSignal=*/false, InFile,
142                           /*Extension=*/"", /*useTemporary=*/true);
143   if (!OS)
144     return nullptr;
145 
146   OutputFile = CI.getFrontendOpts().OutputFile;
147   return OS;
148 }
149 
150 bool GeneratePCHAction::shouldEraseOutputFiles() {
151   if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
152     return false;
153   return ASTFrontendAction::shouldEraseOutputFiles();
154 }
155 
156 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
157   CI.getLangOpts().CompilingPCH = true;
158   return true;
159 }
160 
161 std::unique_ptr<ASTConsumer>
162 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
163                                         StringRef InFile) {
164   std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
165   if (!OS)
166     return nullptr;
167 
168   std::string OutputFile = CI.getFrontendOpts().OutputFile;
169   std::string Sysroot;
170 
171   auto Buffer = std::make_shared<PCHBuffer>();
172   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
173 
174   Consumers.push_back(llvm::make_unique<PCHGenerator>(
175                         CI.getPreprocessor(), OutputFile, Sysroot,
176                         Buffer, CI.getFrontendOpts().ModuleFileExtensions,
177                         /*AllowASTWithErrors=*/false,
178                         /*IncludeTimestamps=*/
179                           +CI.getFrontendOpts().BuildingImplicitModule));
180   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
181       CI, InFile, OutputFile, std::move(OS), Buffer));
182   return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
183 }
184 
185 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
186     CompilerInstance &CI) {
187   if (!CI.getLangOpts().Modules) {
188     CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
189     return false;
190   }
191 
192   return GenerateModuleAction::BeginSourceFileAction(CI);
193 }
194 
195 std::unique_ptr<raw_pwrite_stream>
196 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
197                                                     StringRef InFile) {
198   // If no output file was provided, figure out where this module would go
199   // in the module cache.
200   if (CI.getFrontendOpts().OutputFile.empty()) {
201     StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
202     if (ModuleMapFile.empty())
203       ModuleMapFile = InFile;
204 
205     HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
206     CI.getFrontendOpts().OutputFile =
207         HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
208                                    ModuleMapFile);
209   }
210 
211   // We use createOutputFile here because this is exposed via libclang, and we
212   // must disable the RemoveFileOnSignal behavior.
213   // We use a temporary to avoid race conditions.
214   return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
215                              /*RemoveFileOnSignal=*/false, InFile,
216                              /*Extension=*/"", /*useTemporary=*/true,
217                              /*CreateMissingDirectories=*/true);
218 }
219 
220 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
221     CompilerInstance &CI) {
222   if (!CI.getLangOpts().ModulesTS) {
223     CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts);
224     return false;
225   }
226 
227   CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
228 
229   return GenerateModuleAction::BeginSourceFileAction(CI);
230 }
231 
232 std::unique_ptr<raw_pwrite_stream>
233 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
234                                                 StringRef InFile) {
235   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
236 }
237 
238 bool GenerateHeaderModuleAction::PrepareToExecuteAction(
239     CompilerInstance &CI) {
240   if (!CI.getLangOpts().Modules && !CI.getLangOpts().ModulesTS) {
241     CI.getDiagnostics().Report(diag::err_header_module_requires_modules);
242     return false;
243   }
244 
245   auto &Inputs = CI.getFrontendOpts().Inputs;
246   if (Inputs.empty())
247     return GenerateModuleAction::BeginInvocation(CI);
248 
249   auto Kind = Inputs[0].getKind();
250 
251   // Convert the header file inputs into a single module input buffer.
252   SmallString<256> HeaderContents;
253   ModuleHeaders.reserve(Inputs.size());
254   for (const FrontendInputFile &FIF : Inputs) {
255     // FIXME: We should support re-compiling from an AST file.
256     if (FIF.getKind().getFormat() != InputKind::Source || !FIF.isFile()) {
257       CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
258           << (FIF.isFile() ? FIF.getFile()
259                            : FIF.getBuffer()->getBufferIdentifier());
260       return true;
261     }
262 
263     HeaderContents += "#include \"";
264     HeaderContents += FIF.getFile();
265     HeaderContents += "\"\n";
266     ModuleHeaders.push_back(FIF.getFile());
267   }
268   Buffer = llvm::MemoryBuffer::getMemBufferCopy(
269       HeaderContents, Module::getModuleInputBufferName());
270 
271   // Set that buffer up as our "real" input.
272   Inputs.clear();
273   Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
274 
275   return GenerateModuleAction::PrepareToExecuteAction(CI);
276 }
277 
278 bool GenerateHeaderModuleAction::BeginSourceFileAction(
279     CompilerInstance &CI) {
280   CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderModule);
281 
282   // Synthesize a Module object for the given headers.
283   auto &HS = CI.getPreprocessor().getHeaderSearchInfo();
284   SmallVector<Module::Header, 16> Headers;
285   for (StringRef Name : ModuleHeaders) {
286     const DirectoryLookup *CurDir = nullptr;
287     const FileEntry *FE = HS.LookupFile(
288         Name, SourceLocation(), /*Angled*/ false, nullptr, CurDir,
289         None, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
290     if (!FE) {
291       CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
292         << Name;
293       continue;
294     }
295     Headers.push_back({Name, FE});
296   }
297   HS.getModuleMap().createHeaderModule(CI.getLangOpts().CurrentModule, Headers);
298 
299   return GenerateModuleAction::BeginSourceFileAction(CI);
300 }
301 
302 std::unique_ptr<raw_pwrite_stream>
303 GenerateHeaderModuleAction::CreateOutputFile(CompilerInstance &CI,
304                                              StringRef InFile) {
305   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
306 }
307 
308 SyntaxOnlyAction::~SyntaxOnlyAction() {
309 }
310 
311 std::unique_ptr<ASTConsumer>
312 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
313   return llvm::make_unique<ASTConsumer>();
314 }
315 
316 std::unique_ptr<ASTConsumer>
317 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
318                                         StringRef InFile) {
319   return llvm::make_unique<ASTConsumer>();
320 }
321 
322 std::unique_ptr<ASTConsumer>
323 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
324   return llvm::make_unique<ASTConsumer>();
325 }
326 
327 void VerifyPCHAction::ExecuteAction() {
328   CompilerInstance &CI = getCompilerInstance();
329   bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
330   const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
331   std::unique_ptr<ASTReader> Reader(new ASTReader(
332       CI.getPreprocessor(), &CI.getASTContext(), CI.getPCHContainerReader(),
333       CI.getFrontendOpts().ModuleFileExtensions,
334       Sysroot.empty() ? "" : Sysroot.c_str(),
335       /*DisableValidation*/ false,
336       /*AllowPCHWithCompilerErrors*/ false,
337       /*AllowConfigurationMismatch*/ true,
338       /*ValidateSystemInputs*/ true));
339 
340   Reader->ReadAST(getCurrentFile(),
341                   Preamble ? serialization::MK_Preamble
342                            : serialization::MK_PCH,
343                   SourceLocation(),
344                   ASTReader::ARR_ConfigurationMismatch);
345 }
346 
347 namespace {
348 struct TemplightEntry {
349   std::string Name;
350   std::string Kind;
351   std::string Event;
352   std::string DefinitionLocation;
353   std::string PointOfInstantiation;
354 };
355 } // namespace
356 
357 namespace llvm {
358 namespace yaml {
359 template <> struct MappingTraits<TemplightEntry> {
360   static void mapping(IO &io, TemplightEntry &fields) {
361     io.mapRequired("name", fields.Name);
362     io.mapRequired("kind", fields.Kind);
363     io.mapRequired("event", fields.Event);
364     io.mapRequired("orig", fields.DefinitionLocation);
365     io.mapRequired("poi", fields.PointOfInstantiation);
366   }
367 };
368 } // namespace yaml
369 } // namespace llvm
370 
371 namespace {
372 class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
373   using CodeSynthesisContext = Sema::CodeSynthesisContext;
374 
375 public:
376   void initialize(const Sema &) override {}
377 
378   void finalize(const Sema &) override {}
379 
380   void atTemplateBegin(const Sema &TheSema,
381                        const CodeSynthesisContext &Inst) override {
382     displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
383   }
384 
385   void atTemplateEnd(const Sema &TheSema,
386                      const CodeSynthesisContext &Inst) override {
387     displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
388   }
389 
390 private:
391   static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
392     switch (Kind) {
393     case CodeSynthesisContext::TemplateInstantiation:
394       return "TemplateInstantiation";
395     case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:
396       return "DefaultTemplateArgumentInstantiation";
397     case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:
398       return "DefaultFunctionArgumentInstantiation";
399     case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:
400       return "ExplicitTemplateArgumentSubstitution";
401     case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:
402       return "DeducedTemplateArgumentSubstitution";
403     case CodeSynthesisContext::PriorTemplateArgumentSubstitution:
404       return "PriorTemplateArgumentSubstitution";
405     case CodeSynthesisContext::DefaultTemplateArgumentChecking:
406       return "DefaultTemplateArgumentChecking";
407     case CodeSynthesisContext::ExceptionSpecEvaluation:
408       return "ExceptionSpecEvaluation";
409     case CodeSynthesisContext::ExceptionSpecInstantiation:
410       return "ExceptionSpecInstantiation";
411     case CodeSynthesisContext::DeclaringSpecialMember:
412       return "DeclaringSpecialMember";
413     case CodeSynthesisContext::DefiningSynthesizedFunction:
414       return "DefiningSynthesizedFunction";
415     case CodeSynthesisContext::Memoization:
416       return "Memoization";
417     }
418     return "";
419   }
420 
421   template <bool BeginInstantiation>
422   static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
423                                     const CodeSynthesisContext &Inst) {
424     std::string YAML;
425     {
426       llvm::raw_string_ostream OS(YAML);
427       llvm::yaml::Output YO(OS);
428       TemplightEntry Entry =
429           getTemplightEntry<BeginInstantiation>(TheSema, Inst);
430       llvm::yaml::EmptyContext Context;
431       llvm::yaml::yamlize(YO, Entry, true, Context);
432     }
433     Out << "---" << YAML << "\n";
434   }
435 
436   template <bool BeginInstantiation>
437   static TemplightEntry getTemplightEntry(const Sema &TheSema,
438                                           const CodeSynthesisContext &Inst) {
439     TemplightEntry Entry;
440     Entry.Kind = toString(Inst.Kind);
441     Entry.Event = BeginInstantiation ? "Begin" : "End";
442     if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
443       llvm::raw_string_ostream OS(Entry.Name);
444       NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
445       const PresumedLoc DefLoc =
446         TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
447       if(!DefLoc.isInvalid())
448         Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
449                                    std::to_string(DefLoc.getLine()) + ":" +
450                                    std::to_string(DefLoc.getColumn());
451     }
452     const PresumedLoc PoiLoc =
453         TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
454     if (!PoiLoc.isInvalid()) {
455       Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
456                                    std::to_string(PoiLoc.getLine()) + ":" +
457                                    std::to_string(PoiLoc.getColumn());
458     }
459     return Entry;
460   }
461 };
462 } // namespace
463 
464 std::unique_ptr<ASTConsumer>
465 TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
466   return llvm::make_unique<ASTConsumer>();
467 }
468 
469 void TemplightDumpAction::ExecuteAction() {
470   CompilerInstance &CI = getCompilerInstance();
471 
472   // This part is normally done by ASTFrontEndAction, but needs to happen
473   // before Templight observers can be created
474   // FIXME: Move the truncation aspect of this into Sema, we delayed this till
475   // here so the source manager would be initialized.
476   EnsureSemaIsCreated(CI, *this);
477 
478   CI.getSema().TemplateInstCallbacks.push_back(
479       llvm::make_unique<DefaultTemplateInstCallback>());
480   ASTFrontendAction::ExecuteAction();
481 }
482 
483 namespace {
484   /// AST reader listener that dumps module information for a module
485   /// file.
486   class DumpModuleInfoListener : public ASTReaderListener {
487     llvm::raw_ostream &Out;
488 
489   public:
490     DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
491 
492 #define DUMP_BOOLEAN(Value, Text)                       \
493     Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
494 
495     bool ReadFullVersionInformation(StringRef FullVersion) override {
496       Out.indent(2)
497         << "Generated by "
498         << (FullVersion == getClangFullRepositoryVersion()? "this"
499                                                           : "a different")
500         << " Clang: " << FullVersion << "\n";
501       return ASTReaderListener::ReadFullVersionInformation(FullVersion);
502     }
503 
504     void ReadModuleName(StringRef ModuleName) override {
505       Out.indent(2) << "Module name: " << ModuleName << "\n";
506     }
507     void ReadModuleMapFile(StringRef ModuleMapPath) override {
508       Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
509     }
510 
511     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
512                              bool AllowCompatibleDifferences) override {
513       Out.indent(2) << "Language options:\n";
514 #define LANGOPT(Name, Bits, Default, Description) \
515       DUMP_BOOLEAN(LangOpts.Name, Description);
516 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
517       Out.indent(4) << Description << ": "                   \
518                     << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
519 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
520       Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
521 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
522 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
523 #include "clang/Basic/LangOptions.def"
524 
525       if (!LangOpts.ModuleFeatures.empty()) {
526         Out.indent(4) << "Module features:\n";
527         for (StringRef Feature : LangOpts.ModuleFeatures)
528           Out.indent(6) << Feature << "\n";
529       }
530 
531       return false;
532     }
533 
534     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
535                            bool AllowCompatibleDifferences) override {
536       Out.indent(2) << "Target options:\n";
537       Out.indent(4) << "  Triple: " << TargetOpts.Triple << "\n";
538       Out.indent(4) << "  CPU: " << TargetOpts.CPU << "\n";
539       Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
540 
541       if (!TargetOpts.FeaturesAsWritten.empty()) {
542         Out.indent(4) << "Target features:\n";
543         for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
544              I != N; ++I) {
545           Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
546         }
547       }
548 
549       return false;
550     }
551 
552     bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
553                                bool Complain) override {
554       Out.indent(2) << "Diagnostic options:\n";
555 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
556 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
557       Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
558 #define VALUE_DIAGOPT(Name, Bits, Default) \
559       Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
560 #include "clang/Basic/DiagnosticOptions.def"
561 
562       Out.indent(4) << "Diagnostic flags:\n";
563       for (const std::string &Warning : DiagOpts->Warnings)
564         Out.indent(6) << "-W" << Warning << "\n";
565       for (const std::string &Remark : DiagOpts->Remarks)
566         Out.indent(6) << "-R" << Remark << "\n";
567 
568       return false;
569     }
570 
571     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
572                                  StringRef SpecificModuleCachePath,
573                                  bool Complain) override {
574       Out.indent(2) << "Header search options:\n";
575       Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
576       Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
577       Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
578       DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
579                    "Use builtin include directories [-nobuiltininc]");
580       DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
581                    "Use standard system include directories [-nostdinc]");
582       DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
583                    "Use standard C++ include directories [-nostdinc++]");
584       DUMP_BOOLEAN(HSOpts.UseLibcxx,
585                    "Use libc++ (rather than libstdc++) [-stdlib=]");
586       return false;
587     }
588 
589     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
590                                  bool Complain,
591                                  std::string &SuggestedPredefines) override {
592       Out.indent(2) << "Preprocessor options:\n";
593       DUMP_BOOLEAN(PPOpts.UsePredefines,
594                    "Uses compiler/target-specific predefines [-undef]");
595       DUMP_BOOLEAN(PPOpts.DetailedRecord,
596                    "Uses detailed preprocessing record (for indexing)");
597 
598       if (!PPOpts.Macros.empty()) {
599         Out.indent(4) << "Predefined macros:\n";
600       }
601 
602       for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
603              I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
604            I != IEnd; ++I) {
605         Out.indent(6);
606         if (I->second)
607           Out << "-U";
608         else
609           Out << "-D";
610         Out << I->first << "\n";
611       }
612       return false;
613     }
614 
615     /// Indicates that a particular module file extension has been read.
616     void readModuleFileExtension(
617            const ModuleFileExtensionMetadata &Metadata) override {
618       Out.indent(2) << "Module file extension '"
619                     << Metadata.BlockName << "' " << Metadata.MajorVersion
620                     << "." << Metadata.MinorVersion;
621       if (!Metadata.UserInfo.empty()) {
622         Out << ": ";
623         Out.write_escaped(Metadata.UserInfo);
624       }
625 
626       Out << "\n";
627     }
628 
629     /// Tells the \c ASTReaderListener that we want to receive the
630     /// input files of the AST file via \c visitInputFile.
631     bool needsInputFileVisitation() override { return true; }
632 
633     /// Tells the \c ASTReaderListener that we want to receive the
634     /// input files of the AST file via \c visitInputFile.
635     bool needsSystemInputFileVisitation() override { return true; }
636 
637     /// Indicates that the AST file contains particular input file.
638     ///
639     /// \returns true to continue receiving the next input file, false to stop.
640     bool visitInputFile(StringRef Filename, bool isSystem,
641                         bool isOverridden, bool isExplicitModule) override {
642 
643       Out.indent(2) << "Input file: " << Filename;
644 
645       if (isSystem || isOverridden || isExplicitModule) {
646         Out << " [";
647         if (isSystem) {
648           Out << "System";
649           if (isOverridden || isExplicitModule)
650             Out << ", ";
651         }
652         if (isOverridden) {
653           Out << "Overridden";
654           if (isExplicitModule)
655             Out << ", ";
656         }
657         if (isExplicitModule)
658           Out << "ExplicitModule";
659 
660         Out << "]";
661       }
662 
663       Out << "\n";
664 
665       return true;
666     }
667 
668     /// Returns true if this \c ASTReaderListener wants to receive the
669     /// imports of the AST file via \c visitImport, false otherwise.
670     bool needsImportVisitation() const override { return true; }
671 
672     /// If needsImportVisitation returns \c true, this is called for each
673     /// AST file imported by this AST file.
674     void visitImport(StringRef ModuleName, StringRef Filename) override {
675       Out.indent(2) << "Imports module '" << ModuleName
676                     << "': " << Filename.str() << "\n";
677     }
678 #undef DUMP_BOOLEAN
679   };
680 }
681 
682 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
683   // The Object file reader also supports raw ast files and there is no point in
684   // being strict about the module file format in -module-file-info mode.
685   CI.getHeaderSearchOpts().ModuleFormat = "obj";
686   return true;
687 }
688 
689 void DumpModuleInfoAction::ExecuteAction() {
690   // Set up the output file.
691   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
692   StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
693   if (!OutputFileName.empty() && OutputFileName != "-") {
694     std::error_code EC;
695     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
696                                            llvm::sys::fs::F_Text));
697   }
698   llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
699 
700   Out << "Information for module file '" << getCurrentFile() << "':\n";
701   auto &FileMgr = getCompilerInstance().getFileManager();
702   auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
703   StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
704   bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
705                 Magic[2] == 'C' && Magic[3] == 'H');
706   Out << "  Module format: " << (IsRaw ? "raw" : "obj") << "\n";
707 
708   Preprocessor &PP = getCompilerInstance().getPreprocessor();
709   DumpModuleInfoListener Listener(Out);
710   HeaderSearchOptions &HSOpts =
711       PP.getHeaderSearchInfo().getHeaderSearchOpts();
712   ASTReader::readASTFileControlBlock(
713       getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
714       /*FindModuleFileExtensions=*/true, Listener,
715       HSOpts.ModulesValidateDiagnosticOptions);
716 }
717 
718 //===----------------------------------------------------------------------===//
719 // Preprocessor Actions
720 //===----------------------------------------------------------------------===//
721 
722 void DumpRawTokensAction::ExecuteAction() {
723   Preprocessor &PP = getCompilerInstance().getPreprocessor();
724   SourceManager &SM = PP.getSourceManager();
725 
726   // Start lexing the specified input file.
727   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
728   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
729   RawLex.SetKeepWhitespaceMode(true);
730 
731   Token RawTok;
732   RawLex.LexFromRawLexer(RawTok);
733   while (RawTok.isNot(tok::eof)) {
734     PP.DumpToken(RawTok, true);
735     llvm::errs() << "\n";
736     RawLex.LexFromRawLexer(RawTok);
737   }
738 }
739 
740 void DumpTokensAction::ExecuteAction() {
741   Preprocessor &PP = getCompilerInstance().getPreprocessor();
742   // Start preprocessing the specified input file.
743   Token Tok;
744   PP.EnterMainSourceFile();
745   do {
746     PP.Lex(Tok);
747     PP.DumpToken(Tok, true);
748     llvm::errs() << "\n";
749   } while (Tok.isNot(tok::eof));
750 }
751 
752 void PreprocessOnlyAction::ExecuteAction() {
753   Preprocessor &PP = getCompilerInstance().getPreprocessor();
754 
755   // Ignore unknown pragmas.
756   PP.IgnorePragmas();
757 
758   Token Tok;
759   // Start parsing the specified input file.
760   PP.EnterMainSourceFile();
761   do {
762     PP.Lex(Tok);
763   } while (Tok.isNot(tok::eof));
764 }
765 
766 void PrintPreprocessedAction::ExecuteAction() {
767   CompilerInstance &CI = getCompilerInstance();
768   // Output file may need to be set to 'Binary', to avoid converting Unix style
769   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
770   //
771   // Look to see what type of line endings the file uses. If there's a
772   // CRLF, then we won't open the file up in binary mode. If there is
773   // just an LF or CR, then we will open the file up in binary mode.
774   // In this fashion, the output format should match the input format, unless
775   // the input format has inconsistent line endings.
776   //
777   // This should be a relatively fast operation since most files won't have
778   // all of their source code on a single line. However, that is still a
779   // concern, so if we scan for too long, we'll just assume the file should
780   // be opened in binary mode.
781   bool BinaryMode = true;
782   bool InvalidFile = false;
783   const SourceManager& SM = CI.getSourceManager();
784   const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
785                                                      &InvalidFile);
786   if (!InvalidFile) {
787     const char *cur = Buffer->getBufferStart();
788     const char *end = Buffer->getBufferEnd();
789     const char *next = (cur != end) ? cur + 1 : end;
790 
791     // Limit ourselves to only scanning 256 characters into the source
792     // file.  This is mostly a sanity check in case the file has no
793     // newlines whatsoever.
794     if (end - cur > 256) end = cur + 256;
795 
796     while (next < end) {
797       if (*cur == 0x0D) {  // CR
798         if (*next == 0x0A)  // CRLF
799           BinaryMode = false;
800 
801         break;
802       } else if (*cur == 0x0A)  // LF
803         break;
804 
805       ++cur;
806       ++next;
807     }
808   }
809 
810   std::unique_ptr<raw_ostream> OS =
811       CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
812   if (!OS) return;
813 
814   // If we're preprocessing a module map, start by dumping the contents of the
815   // module itself before switching to the input buffer.
816   auto &Input = getCurrentInput();
817   if (Input.getKind().getFormat() == InputKind::ModuleMap) {
818     if (Input.isFile()) {
819       (*OS) << "# 1 \"";
820       OS->write_escaped(Input.getFile());
821       (*OS) << "\"\n";
822     }
823     getCurrentModule()->print(*OS);
824     (*OS) << "#pragma clang module contents\n";
825   }
826 
827   DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
828                            CI.getPreprocessorOutputOpts());
829 }
830 
831 void PrintPreambleAction::ExecuteAction() {
832   switch (getCurrentFileKind().getLanguage()) {
833   case InputKind::C:
834   case InputKind::CXX:
835   case InputKind::ObjC:
836   case InputKind::ObjCXX:
837   case InputKind::OpenCL:
838   case InputKind::CUDA:
839   case InputKind::HIP:
840     break;
841 
842   case InputKind::Unknown:
843   case InputKind::Asm:
844   case InputKind::LLVM_IR:
845   case InputKind::RenderScript:
846     // We can't do anything with these.
847     return;
848   }
849 
850   // We don't expect to find any #include directives in a preprocessed input.
851   if (getCurrentFileKind().isPreprocessed())
852     return;
853 
854   CompilerInstance &CI = getCompilerInstance();
855   auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
856   if (Buffer) {
857     unsigned Preamble =
858         Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
859     llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
860   }
861 }
862 
863 void DumpCompilerOptionsAction::ExecuteAction() {
864   CompilerInstance &CI = getCompilerInstance();
865   std::unique_ptr<raw_ostream> OSP =
866       CI.createDefaultOutputFile(false, getCurrentFile());
867   if (!OSP)
868     return;
869 
870   raw_ostream &OS = *OSP;
871   const Preprocessor &PP = CI.getPreprocessor();
872   const LangOptions &LangOpts = PP.getLangOpts();
873 
874   // FIXME: Rather than manually format the JSON (which is awkward due to
875   // needing to remove trailing commas), this should make use of a JSON library.
876   // FIXME: Instead of printing enums as an integral value and specifying the
877   // type as a separate field, use introspection to print the enumerator.
878 
879   OS << "{\n";
880   OS << "\n\"features\" : [\n";
881   {
882     llvm::SmallString<128> Str;
883 #define FEATURE(Name, Predicate)                                               \
884   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
885       .toVector(Str);
886 #include "clang/Basic/Features.def"
887 #undef FEATURE
888     // Remove the newline and comma from the last entry to ensure this remains
889     // valid JSON.
890     OS << Str.substr(0, Str.size() - 2);
891   }
892   OS << "\n],\n";
893 
894   OS << "\n\"extensions\" : [\n";
895   {
896     llvm::SmallString<128> Str;
897 #define EXTENSION(Name, Predicate)                                             \
898   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
899       .toVector(Str);
900 #include "clang/Basic/Features.def"
901 #undef EXTENSION
902     // Remove the newline and comma from the last entry to ensure this remains
903     // valid JSON.
904     OS << Str.substr(0, Str.size() - 2);
905   }
906   OS << "\n]\n";
907 
908   OS << "}";
909 }
910