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