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