xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/FrontendActions.cpp (revision e1e636193db45630c7881246d25902e57c43d24e)
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/AST/Decl.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/LangStandard.h"
14 #include "clang/Basic/Module.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Frontend/ASTConsumers.h"
17 #include "clang/Frontend/CompilerInstance.h"
18 #include "clang/Frontend/FrontendDiagnostic.h"
19 #include "clang/Frontend/MultiplexConsumer.h"
20 #include "clang/Frontend/Utils.h"
21 #include "clang/Lex/DependencyDirectivesScanner.h"
22 #include "clang/Lex/HeaderSearch.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Lex/PreprocessorOptions.h"
25 #include "clang/Sema/TemplateInstCallback.h"
26 #include "clang/Serialization/ASTReader.h"
27 #include "clang/Serialization/ASTWriter.h"
28 #include "clang/Serialization/ModuleFile.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/YAMLTraits.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <memory>
36 #include <optional>
37 #include <system_error>
38 
39 using namespace clang;
40 
41 namespace {
42 CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {
43   return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer()
44                                         : nullptr;
45 }
46 
47 void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {
48   if (Action.hasCodeCompletionSupport() &&
49       !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
50     CI.createCodeCompletionConsumer();
51 
52   if (!CI.hasSema())
53     CI.createSema(Action.getTranslationUnitKind(),
54                   GetCodeCompletionConsumer(CI));
55 }
56 } // namespace
57 
58 //===----------------------------------------------------------------------===//
59 // Custom Actions
60 //===----------------------------------------------------------------------===//
61 
62 std::unique_ptr<ASTConsumer>
63 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
64   return std::make_unique<ASTConsumer>();
65 }
66 
67 void InitOnlyAction::ExecuteAction() {
68 }
69 
70 // Basically PreprocessOnlyAction::ExecuteAction.
71 void ReadPCHAndPreprocessAction::ExecuteAction() {
72   Preprocessor &PP = getCompilerInstance().getPreprocessor();
73 
74   // Ignore unknown pragmas.
75   PP.IgnorePragmas();
76 
77   Token Tok;
78   // Start parsing the specified input file.
79   PP.EnterMainSourceFile();
80   do {
81     PP.Lex(Tok);
82   } while (Tok.isNot(tok::eof));
83 }
84 
85 std::unique_ptr<ASTConsumer>
86 ReadPCHAndPreprocessAction::CreateASTConsumer(CompilerInstance &CI,
87                                               StringRef InFile) {
88   return std::make_unique<ASTConsumer>();
89 }
90 
91 //===----------------------------------------------------------------------===//
92 // AST Consumer Actions
93 //===----------------------------------------------------------------------===//
94 
95 std::unique_ptr<ASTConsumer>
96 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
97   if (std::unique_ptr<raw_ostream> OS =
98           CI.createDefaultOutputFile(false, InFile))
99     return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
100   return nullptr;
101 }
102 
103 std::unique_ptr<ASTConsumer>
104 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
105   const FrontendOptions &Opts = CI.getFrontendOpts();
106   return CreateASTDumper(nullptr /*Dump to stdout.*/, Opts.ASTDumpFilter,
107                          Opts.ASTDumpDecls, Opts.ASTDumpAll,
108                          Opts.ASTDumpLookups, Opts.ASTDumpDeclTypes,
109                          Opts.ASTDumpFormat);
110 }
111 
112 std::unique_ptr<ASTConsumer>
113 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
114   return CreateASTDeclNodeLister();
115 }
116 
117 std::unique_ptr<ASTConsumer>
118 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
119   return CreateASTViewer();
120 }
121 
122 std::unique_ptr<ASTConsumer>
123 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
124   std::string Sysroot;
125   if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
126     return nullptr;
127 
128   std::string OutputFile;
129   std::unique_ptr<raw_pwrite_stream> OS =
130       CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
131   if (!OS)
132     return nullptr;
133 
134   if (!CI.getFrontendOpts().RelocatablePCH)
135     Sysroot.clear();
136 
137   const auto &FrontendOpts = CI.getFrontendOpts();
138   auto Buffer = std::make_shared<PCHBuffer>();
139   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
140   Consumers.push_back(std::make_unique<PCHGenerator>(
141       CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
142       FrontendOpts.ModuleFileExtensions,
143       CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
144       FrontendOpts.IncludeTimestamps, FrontendOpts.BuildingImplicitModule,
145       +CI.getLangOpts().CacheGeneratedPCH));
146   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
147       CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
148 
149   return std::make_unique<MultiplexConsumer>(std::move(Consumers));
150 }
151 
152 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
153                                                     std::string &Sysroot) {
154   Sysroot = CI.getHeaderSearchOpts().Sysroot;
155   if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
156     CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
157     return false;
158   }
159 
160   return true;
161 }
162 
163 std::unique_ptr<llvm::raw_pwrite_stream>
164 GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
165                                     std::string &OutputFile) {
166   // Because this is exposed via libclang we must disable RemoveFileOnSignal.
167   std::unique_ptr<raw_pwrite_stream> OS = CI.createDefaultOutputFile(
168       /*Binary=*/true, InFile, /*Extension=*/"", /*RemoveFileOnSignal=*/false);
169   if (!OS)
170     return nullptr;
171 
172   OutputFile = CI.getFrontendOpts().OutputFile;
173   return OS;
174 }
175 
176 bool GeneratePCHAction::shouldEraseOutputFiles() {
177   if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
178     return false;
179   return ASTFrontendAction::shouldEraseOutputFiles();
180 }
181 
182 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
183   CI.getLangOpts().CompilingPCH = true;
184   return true;
185 }
186 
187 std::unique_ptr<ASTConsumer>
188 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
189                                         StringRef InFile) {
190   std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
191   if (!OS)
192     return nullptr;
193 
194   std::string OutputFile = CI.getFrontendOpts().OutputFile;
195   std::string Sysroot;
196 
197   auto Buffer = std::make_shared<PCHBuffer>();
198   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
199 
200   Consumers.push_back(std::make_unique<PCHGenerator>(
201       CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
202       CI.getFrontendOpts().ModuleFileExtensions,
203       /*AllowASTWithErrors=*/
204       +CI.getFrontendOpts().AllowPCMWithCompilerErrors,
205       /*IncludeTimestamps=*/
206       +CI.getFrontendOpts().BuildingImplicitModule &&
207           +CI.getFrontendOpts().IncludeTimestamps,
208       /*BuildingImplicitModule=*/+CI.getFrontendOpts().BuildingImplicitModule,
209       /*ShouldCacheASTInMemory=*/
210       +CI.getFrontendOpts().BuildingImplicitModule));
211   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
212       CI, std::string(InFile), OutputFile, std::move(OS), Buffer));
213   return std::make_unique<MultiplexConsumer>(std::move(Consumers));
214 }
215 
216 bool GenerateModuleAction::shouldEraseOutputFiles() {
217   return !getCompilerInstance().getFrontendOpts().AllowPCMWithCompilerErrors &&
218          ASTFrontendAction::shouldEraseOutputFiles();
219 }
220 
221 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
222     CompilerInstance &CI) {
223   if (!CI.getLangOpts().Modules) {
224     CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
225     return false;
226   }
227 
228   return GenerateModuleAction::BeginSourceFileAction(CI);
229 }
230 
231 std::unique_ptr<raw_pwrite_stream>
232 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
233                                                     StringRef InFile) {
234   // If no output file was provided, figure out where this module would go
235   // in the module cache.
236   if (CI.getFrontendOpts().OutputFile.empty()) {
237     StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
238     if (ModuleMapFile.empty())
239       ModuleMapFile = InFile;
240 
241     HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
242     CI.getFrontendOpts().OutputFile =
243         HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
244                                    ModuleMapFile);
245   }
246 
247   // Because this is exposed via libclang we must disable RemoveFileOnSignal.
248   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, /*Extension=*/"",
249                                     /*RemoveFileOnSignal=*/false,
250                                     /*CreateMissingDirectories=*/true,
251                                     /*ForceUseTemporary=*/true);
252 }
253 
254 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
255     CompilerInstance &CI) {
256   CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
257 
258   return GenerateModuleAction::BeginSourceFileAction(CI);
259 }
260 
261 std::unique_ptr<ASTConsumer>
262 GenerateModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,
263                                                  StringRef InFile) {
264   CI.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
265   CI.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
266   CI.getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings = true;
267 
268   return GenerateModuleAction::CreateASTConsumer(CI, InFile);
269 }
270 
271 std::unique_ptr<raw_pwrite_stream>
272 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
273                                                 StringRef InFile) {
274   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
275 }
276 
277 bool GenerateHeaderUnitAction::BeginSourceFileAction(CompilerInstance &CI) {
278   if (!CI.getLangOpts().CPlusPlusModules) {
279     CI.getDiagnostics().Report(diag::err_module_interface_requires_cpp_modules);
280     return false;
281   }
282   CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderUnit);
283   return GenerateModuleAction::BeginSourceFileAction(CI);
284 }
285 
286 std::unique_ptr<raw_pwrite_stream>
287 GenerateHeaderUnitAction::CreateOutputFile(CompilerInstance &CI,
288                                            StringRef InFile) {
289   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
290 }
291 
292 SyntaxOnlyAction::~SyntaxOnlyAction() {
293 }
294 
295 std::unique_ptr<ASTConsumer>
296 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
297   return std::make_unique<ASTConsumer>();
298 }
299 
300 std::unique_ptr<ASTConsumer>
301 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
302                                         StringRef InFile) {
303   return std::make_unique<ASTConsumer>();
304 }
305 
306 std::unique_ptr<ASTConsumer>
307 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
308   return std::make_unique<ASTConsumer>();
309 }
310 
311 void VerifyPCHAction::ExecuteAction() {
312   CompilerInstance &CI = getCompilerInstance();
313   bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
314   const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
315   std::unique_ptr<ASTReader> Reader(new ASTReader(
316       CI.getPreprocessor(), CI.getModuleCache(), &CI.getASTContext(),
317       CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions,
318       Sysroot.empty() ? "" : Sysroot.c_str(),
319       DisableValidationForModuleKind::None,
320       /*AllowASTWithCompilerErrors*/ false,
321       /*AllowConfigurationMismatch*/ true,
322       /*ValidateSystemInputs*/ true));
323 
324   Reader->ReadAST(getCurrentFile(),
325                   Preamble ? serialization::MK_Preamble
326                            : serialization::MK_PCH,
327                   SourceLocation(),
328                   ASTReader::ARR_ConfigurationMismatch);
329 }
330 
331 namespace {
332 struct TemplightEntry {
333   std::string Name;
334   std::string Kind;
335   std::string Event;
336   std::string DefinitionLocation;
337   std::string PointOfInstantiation;
338 };
339 } // namespace
340 
341 namespace llvm {
342 namespace yaml {
343 template <> struct MappingTraits<TemplightEntry> {
344   static void mapping(IO &io, TemplightEntry &fields) {
345     io.mapRequired("name", fields.Name);
346     io.mapRequired("kind", fields.Kind);
347     io.mapRequired("event", fields.Event);
348     io.mapRequired("orig", fields.DefinitionLocation);
349     io.mapRequired("poi", fields.PointOfInstantiation);
350   }
351 };
352 } // namespace yaml
353 } // namespace llvm
354 
355 namespace {
356 class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
357   using CodeSynthesisContext = Sema::CodeSynthesisContext;
358 
359 public:
360   void initialize(const Sema &) override {}
361 
362   void finalize(const Sema &) override {}
363 
364   void atTemplateBegin(const Sema &TheSema,
365                        const CodeSynthesisContext &Inst) override {
366     displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
367   }
368 
369   void atTemplateEnd(const Sema &TheSema,
370                      const CodeSynthesisContext &Inst) override {
371     displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
372   }
373 
374 private:
375   static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
376     switch (Kind) {
377     case CodeSynthesisContext::TemplateInstantiation:
378       return "TemplateInstantiation";
379     case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:
380       return "DefaultTemplateArgumentInstantiation";
381     case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:
382       return "DefaultFunctionArgumentInstantiation";
383     case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:
384       return "ExplicitTemplateArgumentSubstitution";
385     case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:
386       return "DeducedTemplateArgumentSubstitution";
387     case CodeSynthesisContext::LambdaExpressionSubstitution:
388       return "LambdaExpressionSubstitution";
389     case CodeSynthesisContext::PriorTemplateArgumentSubstitution:
390       return "PriorTemplateArgumentSubstitution";
391     case CodeSynthesisContext::DefaultTemplateArgumentChecking:
392       return "DefaultTemplateArgumentChecking";
393     case CodeSynthesisContext::ExceptionSpecEvaluation:
394       return "ExceptionSpecEvaluation";
395     case CodeSynthesisContext::ExceptionSpecInstantiation:
396       return "ExceptionSpecInstantiation";
397     case CodeSynthesisContext::DeclaringSpecialMember:
398       return "DeclaringSpecialMember";
399     case CodeSynthesisContext::DeclaringImplicitEqualityComparison:
400       return "DeclaringImplicitEqualityComparison";
401     case CodeSynthesisContext::DefiningSynthesizedFunction:
402       return "DefiningSynthesizedFunction";
403     case CodeSynthesisContext::RewritingOperatorAsSpaceship:
404       return "RewritingOperatorAsSpaceship";
405     case CodeSynthesisContext::Memoization:
406       return "Memoization";
407     case CodeSynthesisContext::ConstraintsCheck:
408       return "ConstraintsCheck";
409     case CodeSynthesisContext::ConstraintSubstitution:
410       return "ConstraintSubstitution";
411     case CodeSynthesisContext::ConstraintNormalization:
412       return "ConstraintNormalization";
413     case CodeSynthesisContext::RequirementParameterInstantiation:
414       return "RequirementParameterInstantiation";
415     case CodeSynthesisContext::ParameterMappingSubstitution:
416       return "ParameterMappingSubstitution";
417     case CodeSynthesisContext::RequirementInstantiation:
418       return "RequirementInstantiation";
419     case CodeSynthesisContext::NestedRequirementConstraintsCheck:
420       return "NestedRequirementConstraintsCheck";
421     case CodeSynthesisContext::InitializingStructuredBinding:
422       return "InitializingStructuredBinding";
423     case CodeSynthesisContext::MarkingClassDllexported:
424       return "MarkingClassDllexported";
425     case CodeSynthesisContext::BuildingBuiltinDumpStructCall:
426       return "BuildingBuiltinDumpStructCall";
427     case CodeSynthesisContext::BuildingDeductionGuides:
428       return "BuildingDeductionGuides";
429     }
430     return "";
431   }
432 
433   template <bool BeginInstantiation>
434   static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
435                                     const CodeSynthesisContext &Inst) {
436     std::string YAML;
437     {
438       llvm::raw_string_ostream OS(YAML);
439       llvm::yaml::Output YO(OS);
440       TemplightEntry Entry =
441           getTemplightEntry<BeginInstantiation>(TheSema, Inst);
442       llvm::yaml::EmptyContext Context;
443       llvm::yaml::yamlize(YO, Entry, true, Context);
444     }
445     Out << "---" << YAML << "\n";
446   }
447 
448   static void printEntryName(const Sema &TheSema, const Decl *Entity,
449                              llvm::raw_string_ostream &OS) {
450     auto *NamedTemplate = cast<NamedDecl>(Entity);
451 
452     PrintingPolicy Policy = TheSema.Context.getPrintingPolicy();
453     // FIXME: Also ask for FullyQualifiedNames?
454     Policy.SuppressDefaultTemplateArgs = false;
455     NamedTemplate->getNameForDiagnostic(OS, Policy, true);
456 
457     if (!OS.str().empty())
458       return;
459 
460     Decl *Ctx = Decl::castFromDeclContext(NamedTemplate->getDeclContext());
461     NamedDecl *NamedCtx = dyn_cast_or_null<NamedDecl>(Ctx);
462 
463     if (const auto *Decl = dyn_cast<TagDecl>(NamedTemplate)) {
464       if (const auto *R = dyn_cast<RecordDecl>(Decl)) {
465         if (R->isLambda()) {
466           OS << "lambda at ";
467           Decl->getLocation().print(OS, TheSema.getSourceManager());
468           return;
469         }
470       }
471       OS << "unnamed " << Decl->getKindName();
472       return;
473     }
474 
475     assert(NamedCtx && "NamedCtx cannot be null");
476 
477     if (const auto *Decl = dyn_cast<ParmVarDecl>(NamedTemplate)) {
478       OS << "unnamed function parameter " << Decl->getFunctionScopeIndex()
479          << " ";
480       if (Decl->getFunctionScopeDepth() > 0)
481         OS << "(at depth " << Decl->getFunctionScopeDepth() << ") ";
482       OS << "of ";
483       NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
484       return;
485     }
486 
487     if (const auto *Decl = dyn_cast<TemplateTypeParmDecl>(NamedTemplate)) {
488       if (const Type *Ty = Decl->getTypeForDecl()) {
489         if (const auto *TTPT = dyn_cast_or_null<TemplateTypeParmType>(Ty)) {
490           OS << "unnamed template type parameter " << TTPT->getIndex() << " ";
491           if (TTPT->getDepth() > 0)
492             OS << "(at depth " << TTPT->getDepth() << ") ";
493           OS << "of ";
494           NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
495           return;
496         }
497       }
498     }
499 
500     if (const auto *Decl = dyn_cast<NonTypeTemplateParmDecl>(NamedTemplate)) {
501       OS << "unnamed template non-type parameter " << Decl->getIndex() << " ";
502       if (Decl->getDepth() > 0)
503         OS << "(at depth " << Decl->getDepth() << ") ";
504       OS << "of ";
505       NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
506       return;
507     }
508 
509     if (const auto *Decl = dyn_cast<TemplateTemplateParmDecl>(NamedTemplate)) {
510       OS << "unnamed template template parameter " << Decl->getIndex() << " ";
511       if (Decl->getDepth() > 0)
512         OS << "(at depth " << Decl->getDepth() << ") ";
513       OS << "of ";
514       NamedCtx->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
515       return;
516     }
517 
518     llvm_unreachable("Failed to retrieve a name for this entry!");
519     OS << "unnamed identifier";
520   }
521 
522   template <bool BeginInstantiation>
523   static TemplightEntry getTemplightEntry(const Sema &TheSema,
524                                           const CodeSynthesisContext &Inst) {
525     TemplightEntry Entry;
526     Entry.Kind = toString(Inst.Kind);
527     Entry.Event = BeginInstantiation ? "Begin" : "End";
528     llvm::raw_string_ostream OS(Entry.Name);
529     printEntryName(TheSema, Inst.Entity, OS);
530     const PresumedLoc DefLoc =
531         TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
532     if (!DefLoc.isInvalid())
533       Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
534                                  std::to_string(DefLoc.getLine()) + ":" +
535                                  std::to_string(DefLoc.getColumn());
536     const PresumedLoc PoiLoc =
537         TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
538     if (!PoiLoc.isInvalid()) {
539       Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
540                                    std::to_string(PoiLoc.getLine()) + ":" +
541                                    std::to_string(PoiLoc.getColumn());
542     }
543     return Entry;
544   }
545 };
546 } // namespace
547 
548 std::unique_ptr<ASTConsumer>
549 TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
550   return std::make_unique<ASTConsumer>();
551 }
552 
553 void TemplightDumpAction::ExecuteAction() {
554   CompilerInstance &CI = getCompilerInstance();
555 
556   // This part is normally done by ASTFrontEndAction, but needs to happen
557   // before Templight observers can be created
558   // FIXME: Move the truncation aspect of this into Sema, we delayed this till
559   // here so the source manager would be initialized.
560   EnsureSemaIsCreated(CI, *this);
561 
562   CI.getSema().TemplateInstCallbacks.push_back(
563       std::make_unique<DefaultTemplateInstCallback>());
564   ASTFrontendAction::ExecuteAction();
565 }
566 
567 namespace {
568   /// AST reader listener that dumps module information for a module
569   /// file.
570   class DumpModuleInfoListener : public ASTReaderListener {
571     llvm::raw_ostream &Out;
572 
573   public:
574     DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
575 
576 #define DUMP_BOOLEAN(Value, Text)                       \
577     Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
578 
579     bool ReadFullVersionInformation(StringRef FullVersion) override {
580       Out.indent(2)
581         << "Generated by "
582         << (FullVersion == getClangFullRepositoryVersion()? "this"
583                                                           : "a different")
584         << " Clang: " << FullVersion << "\n";
585       return ASTReaderListener::ReadFullVersionInformation(FullVersion);
586     }
587 
588     void ReadModuleName(StringRef ModuleName) override {
589       Out.indent(2) << "Module name: " << ModuleName << "\n";
590     }
591     void ReadModuleMapFile(StringRef ModuleMapPath) override {
592       Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
593     }
594 
595     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
596                              bool AllowCompatibleDifferences) override {
597       Out.indent(2) << "Language options:\n";
598 #define LANGOPT(Name, Bits, Default, Description) \
599       DUMP_BOOLEAN(LangOpts.Name, Description);
600 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
601       Out.indent(4) << Description << ": "                   \
602                     << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
603 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
604       Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
605 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
606 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
607 #include "clang/Basic/LangOptions.def"
608 
609       if (!LangOpts.ModuleFeatures.empty()) {
610         Out.indent(4) << "Module features:\n";
611         for (StringRef Feature : LangOpts.ModuleFeatures)
612           Out.indent(6) << Feature << "\n";
613       }
614 
615       return false;
616     }
617 
618     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
619                            bool AllowCompatibleDifferences) override {
620       Out.indent(2) << "Target options:\n";
621       Out.indent(4) << "  Triple: " << TargetOpts.Triple << "\n";
622       Out.indent(4) << "  CPU: " << TargetOpts.CPU << "\n";
623       Out.indent(4) << "  TuneCPU: " << TargetOpts.TuneCPU << "\n";
624       Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
625 
626       if (!TargetOpts.FeaturesAsWritten.empty()) {
627         Out.indent(4) << "Target features:\n";
628         for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
629              I != N; ++I) {
630           Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
631         }
632       }
633 
634       return false;
635     }
636 
637     bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
638                                bool Complain) override {
639       Out.indent(2) << "Diagnostic options:\n";
640 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
641 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
642       Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
643 #define VALUE_DIAGOPT(Name, Bits, Default) \
644       Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
645 #include "clang/Basic/DiagnosticOptions.def"
646 
647       Out.indent(4) << "Diagnostic flags:\n";
648       for (const std::string &Warning : DiagOpts->Warnings)
649         Out.indent(6) << "-W" << Warning << "\n";
650       for (const std::string &Remark : DiagOpts->Remarks)
651         Out.indent(6) << "-R" << Remark << "\n";
652 
653       return false;
654     }
655 
656     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
657                                  StringRef SpecificModuleCachePath,
658                                  bool Complain) override {
659       Out.indent(2) << "Header search options:\n";
660       Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
661       Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
662       Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
663       DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
664                    "Use builtin include directories [-nobuiltininc]");
665       DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
666                    "Use standard system include directories [-nostdinc]");
667       DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
668                    "Use standard C++ include directories [-nostdinc++]");
669       DUMP_BOOLEAN(HSOpts.UseLibcxx,
670                    "Use libc++ (rather than libstdc++) [-stdlib=]");
671       return false;
672     }
673 
674     bool ReadHeaderSearchPaths(const HeaderSearchOptions &HSOpts,
675                                bool Complain) override {
676       Out.indent(2) << "Header search paths:\n";
677       Out.indent(4) << "User entries:\n";
678       for (const auto &Entry : HSOpts.UserEntries)
679         Out.indent(6) << Entry.Path << "\n";
680       Out.indent(4) << "System header prefixes:\n";
681       for (const auto &Prefix : HSOpts.SystemHeaderPrefixes)
682         Out.indent(6) << Prefix.Prefix << "\n";
683       Out.indent(4) << "VFS overlay files:\n";
684       for (const auto &Overlay : HSOpts.VFSOverlayFiles)
685         Out.indent(6) << Overlay << "\n";
686       return false;
687     }
688 
689     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
690                                  bool ReadMacros, bool Complain,
691                                  std::string &SuggestedPredefines) override {
692       Out.indent(2) << "Preprocessor options:\n";
693       DUMP_BOOLEAN(PPOpts.UsePredefines,
694                    "Uses compiler/target-specific predefines [-undef]");
695       DUMP_BOOLEAN(PPOpts.DetailedRecord,
696                    "Uses detailed preprocessing record (for indexing)");
697 
698       if (ReadMacros) {
699         Out.indent(4) << "Predefined macros:\n";
700       }
701 
702       for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
703              I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
704            I != IEnd; ++I) {
705         Out.indent(6);
706         if (I->second)
707           Out << "-U";
708         else
709           Out << "-D";
710         Out << I->first << "\n";
711       }
712       return false;
713     }
714 
715     /// Indicates that a particular module file extension has been read.
716     void readModuleFileExtension(
717            const ModuleFileExtensionMetadata &Metadata) override {
718       Out.indent(2) << "Module file extension '"
719                     << Metadata.BlockName << "' " << Metadata.MajorVersion
720                     << "." << Metadata.MinorVersion;
721       if (!Metadata.UserInfo.empty()) {
722         Out << ": ";
723         Out.write_escaped(Metadata.UserInfo);
724       }
725 
726       Out << "\n";
727     }
728 
729     /// Tells the \c ASTReaderListener that we want to receive the
730     /// input files of the AST file via \c visitInputFile.
731     bool needsInputFileVisitation() override { return true; }
732 
733     /// Tells the \c ASTReaderListener that we want to receive the
734     /// input files of the AST file via \c visitInputFile.
735     bool needsSystemInputFileVisitation() override { return true; }
736 
737     /// Indicates that the AST file contains particular input file.
738     ///
739     /// \returns true to continue receiving the next input file, false to stop.
740     bool visitInputFile(StringRef Filename, bool isSystem,
741                         bool isOverridden, bool isExplicitModule) override {
742 
743       Out.indent(2) << "Input file: " << Filename;
744 
745       if (isSystem || isOverridden || isExplicitModule) {
746         Out << " [";
747         if (isSystem) {
748           Out << "System";
749           if (isOverridden || isExplicitModule)
750             Out << ", ";
751         }
752         if (isOverridden) {
753           Out << "Overridden";
754           if (isExplicitModule)
755             Out << ", ";
756         }
757         if (isExplicitModule)
758           Out << "ExplicitModule";
759 
760         Out << "]";
761       }
762 
763       Out << "\n";
764 
765       return true;
766     }
767 
768     /// Returns true if this \c ASTReaderListener wants to receive the
769     /// imports of the AST file via \c visitImport, false otherwise.
770     bool needsImportVisitation() const override { return true; }
771 
772     /// If needsImportVisitation returns \c true, this is called for each
773     /// AST file imported by this AST file.
774     void visitImport(StringRef ModuleName, StringRef Filename) override {
775       Out.indent(2) << "Imports module '" << ModuleName
776                     << "': " << Filename.str() << "\n";
777     }
778 #undef DUMP_BOOLEAN
779   };
780 }
781 
782 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
783   // The Object file reader also supports raw ast files and there is no point in
784   // being strict about the module file format in -module-file-info mode.
785   CI.getHeaderSearchOpts().ModuleFormat = "obj";
786   return true;
787 }
788 
789 static StringRef ModuleKindName(Module::ModuleKind MK) {
790   switch (MK) {
791   case Module::ModuleMapModule:
792     return "Module Map Module";
793   case Module::ModuleInterfaceUnit:
794     return "Interface Unit";
795   case Module::ModuleImplementationUnit:
796     return "Implementation Unit";
797   case Module::ModulePartitionInterface:
798     return "Partition Interface";
799   case Module::ModulePartitionImplementation:
800     return "Partition Implementation";
801   case Module::ModuleHeaderUnit:
802     return "Header Unit";
803   case Module::ExplicitGlobalModuleFragment:
804     return "Global Module Fragment";
805   case Module::ImplicitGlobalModuleFragment:
806     return "Implicit Module Fragment";
807   case Module::PrivateModuleFragment:
808     return "Private Module Fragment";
809   }
810   llvm_unreachable("unknown module kind!");
811 }
812 
813 void DumpModuleInfoAction::ExecuteAction() {
814   assert(isCurrentFileAST() && "dumping non-AST?");
815   // Set up the output file.
816   CompilerInstance &CI = getCompilerInstance();
817   StringRef OutputFileName = CI.getFrontendOpts().OutputFile;
818   if (!OutputFileName.empty() && OutputFileName != "-") {
819     std::error_code EC;
820     OutputStream.reset(new llvm::raw_fd_ostream(
821         OutputFileName.str(), EC, llvm::sys::fs::OF_TextWithCRLF));
822   }
823   llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs();
824 
825   Out << "Information for module file '" << getCurrentFile() << "':\n";
826   auto &FileMgr = CI.getFileManager();
827   auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
828   StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
829   bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
830                 Magic[2] == 'C' && Magic[3] == 'H');
831   Out << "  Module format: " << (IsRaw ? "raw" : "obj") << "\n";
832 
833   Preprocessor &PP = CI.getPreprocessor();
834   DumpModuleInfoListener Listener(Out);
835   HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
836 
837   // The FrontendAction::BeginSourceFile () method loads the AST so that much
838   // of the information is already available and modules should have been
839   // loaded.
840 
841   const LangOptions &LO = getCurrentASTUnit().getLangOpts();
842   if (LO.CPlusPlusModules && !LO.CurrentModule.empty()) {
843 
844     ASTReader *R = getCurrentASTUnit().getASTReader().get();
845     unsigned SubModuleCount = R->getTotalNumSubmodules();
846     serialization::ModuleFile &MF = R->getModuleManager().getPrimaryModule();
847     Out << "  ====== C++20 Module structure ======\n";
848 
849     if (MF.ModuleName != LO.CurrentModule)
850       Out << "  Mismatched module names : " << MF.ModuleName << " and "
851           << LO.CurrentModule << "\n";
852 
853     struct SubModInfo {
854       unsigned Idx;
855       Module *Mod;
856       Module::ModuleKind Kind;
857       std::string &Name;
858       bool Seen;
859     };
860     std::map<std::string, SubModInfo> SubModMap;
861     auto PrintSubMapEntry = [&](std::string Name, Module::ModuleKind Kind) {
862       Out << "    " << ModuleKindName(Kind) << " '" << Name << "'";
863       auto I = SubModMap.find(Name);
864       if (I == SubModMap.end())
865         Out << " was not found in the sub modules!\n";
866       else {
867         I->second.Seen = true;
868         Out << " is at index #" << I->second.Idx << "\n";
869       }
870     };
871     Module *Primary = nullptr;
872     for (unsigned Idx = 0; Idx <= SubModuleCount; ++Idx) {
873       Module *M = R->getModule(Idx);
874       if (!M)
875         continue;
876       if (M->Name == LO.CurrentModule) {
877         Primary = M;
878         Out << "  " << ModuleKindName(M->Kind) << " '" << LO.CurrentModule
879             << "' is the Primary Module at index #" << Idx << "\n";
880         SubModMap.insert({M->Name, {Idx, M, M->Kind, M->Name, true}});
881       } else
882         SubModMap.insert({M->Name, {Idx, M, M->Kind, M->Name, false}});
883     }
884     if (Primary) {
885       if (!Primary->submodules().empty())
886         Out << "   Sub Modules:\n";
887       for (auto *MI : Primary->submodules()) {
888         PrintSubMapEntry(MI->Name, MI->Kind);
889       }
890       if (!Primary->Imports.empty())
891         Out << "   Imports:\n";
892       for (auto *IMP : Primary->Imports) {
893         PrintSubMapEntry(IMP->Name, IMP->Kind);
894       }
895       if (!Primary->Exports.empty())
896         Out << "   Exports:\n";
897       for (unsigned MN = 0, N = Primary->Exports.size(); MN != N; ++MN) {
898         if (Module *M = Primary->Exports[MN].getPointer()) {
899           PrintSubMapEntry(M->Name, M->Kind);
900         }
901       }
902     }
903 
904     // Emit the macro definitions in the module file so that we can know how
905     // much definitions in the module file quickly.
906     // TODO: Emit the macro definition bodies completely.
907     if (auto FilteredMacros = llvm::make_filter_range(
908             R->getPreprocessor().macros(),
909             [](const auto &Macro) { return Macro.first->isFromAST(); });
910         !FilteredMacros.empty()) {
911       Out << "   Macro Definitions:\n";
912       for (/*<IdentifierInfo *, MacroState> pair*/ const auto &Macro :
913            FilteredMacros)
914         Out << "     " << Macro.first->getName() << "\n";
915     }
916 
917     // Now let's print out any modules we did not see as part of the Primary.
918     for (const auto &SM : SubModMap) {
919       if (!SM.second.Seen && SM.second.Mod) {
920         Out << "  " << ModuleKindName(SM.second.Kind) << " '" << SM.first
921             << "' at index #" << SM.second.Idx
922             << " has no direct reference in the Primary\n";
923       }
924     }
925     Out << "  ====== ======\n";
926   }
927 
928   // The reminder of the output is produced from the listener as the AST
929   // FileCcontrolBlock is (re-)parsed.
930   ASTReader::readASTFileControlBlock(
931       getCurrentFile(), FileMgr, CI.getModuleCache(),
932       CI.getPCHContainerReader(),
933       /*FindModuleFileExtensions=*/true, Listener,
934       HSOpts.ModulesValidateDiagnosticOptions);
935 }
936 
937 //===----------------------------------------------------------------------===//
938 // Preprocessor Actions
939 //===----------------------------------------------------------------------===//
940 
941 void DumpRawTokensAction::ExecuteAction() {
942   Preprocessor &PP = getCompilerInstance().getPreprocessor();
943   SourceManager &SM = PP.getSourceManager();
944 
945   // Start lexing the specified input file.
946   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
947   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
948   RawLex.SetKeepWhitespaceMode(true);
949 
950   Token RawTok;
951   RawLex.LexFromRawLexer(RawTok);
952   while (RawTok.isNot(tok::eof)) {
953     PP.DumpToken(RawTok, true);
954     llvm::errs() << "\n";
955     RawLex.LexFromRawLexer(RawTok);
956   }
957 }
958 
959 void DumpTokensAction::ExecuteAction() {
960   Preprocessor &PP = getCompilerInstance().getPreprocessor();
961   // Start preprocessing the specified input file.
962   Token Tok;
963   PP.EnterMainSourceFile();
964   do {
965     PP.Lex(Tok);
966     PP.DumpToken(Tok, true);
967     llvm::errs() << "\n";
968   } while (Tok.isNot(tok::eof));
969 }
970 
971 void PreprocessOnlyAction::ExecuteAction() {
972   Preprocessor &PP = getCompilerInstance().getPreprocessor();
973 
974   // Ignore unknown pragmas.
975   PP.IgnorePragmas();
976 
977   Token Tok;
978   // Start parsing the specified input file.
979   PP.EnterMainSourceFile();
980   do {
981     PP.Lex(Tok);
982   } while (Tok.isNot(tok::eof));
983 }
984 
985 void PrintPreprocessedAction::ExecuteAction() {
986   CompilerInstance &CI = getCompilerInstance();
987   // Output file may need to be set to 'Binary', to avoid converting Unix style
988   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>) on Windows.
989   //
990   // Look to see what type of line endings the file uses. If there's a
991   // CRLF, then we won't open the file up in binary mode. If there is
992   // just an LF or CR, then we will open the file up in binary mode.
993   // In this fashion, the output format should match the input format, unless
994   // the input format has inconsistent line endings.
995   //
996   // This should be a relatively fast operation since most files won't have
997   // all of their source code on a single line. However, that is still a
998   // concern, so if we scan for too long, we'll just assume the file should
999   // be opened in binary mode.
1000 
1001   bool BinaryMode = false;
1002   if (llvm::Triple(LLVM_HOST_TRIPLE).isOSWindows()) {
1003     BinaryMode = true;
1004     const SourceManager &SM = CI.getSourceManager();
1005     if (std::optional<llvm::MemoryBufferRef> Buffer =
1006             SM.getBufferOrNone(SM.getMainFileID())) {
1007       const char *cur = Buffer->getBufferStart();
1008       const char *end = Buffer->getBufferEnd();
1009       const char *next = (cur != end) ? cur + 1 : end;
1010 
1011       // Limit ourselves to only scanning 256 characters into the source
1012       // file.  This is mostly a check in case the file has no
1013       // newlines whatsoever.
1014       if (end - cur > 256)
1015         end = cur + 256;
1016 
1017       while (next < end) {
1018         if (*cur == 0x0D) {  // CR
1019           if (*next == 0x0A) // CRLF
1020             BinaryMode = false;
1021 
1022           break;
1023         } else if (*cur == 0x0A) // LF
1024           break;
1025 
1026         ++cur;
1027         ++next;
1028       }
1029     }
1030   }
1031 
1032   std::unique_ptr<raw_ostream> OS =
1033       CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
1034   if (!OS) return;
1035 
1036   // If we're preprocessing a module map, start by dumping the contents of the
1037   // module itself before switching to the input buffer.
1038   auto &Input = getCurrentInput();
1039   if (Input.getKind().getFormat() == InputKind::ModuleMap) {
1040     if (Input.isFile()) {
1041       (*OS) << "# 1 \"";
1042       OS->write_escaped(Input.getFile());
1043       (*OS) << "\"\n";
1044     }
1045     getCurrentModule()->print(*OS);
1046     (*OS) << "#pragma clang module contents\n";
1047   }
1048 
1049   DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
1050                            CI.getPreprocessorOutputOpts());
1051 }
1052 
1053 void PrintPreambleAction::ExecuteAction() {
1054   switch (getCurrentFileKind().getLanguage()) {
1055   case Language::C:
1056   case Language::CXX:
1057   case Language::ObjC:
1058   case Language::ObjCXX:
1059   case Language::OpenCL:
1060   case Language::OpenCLCXX:
1061   case Language::CUDA:
1062   case Language::HIP:
1063   case Language::HLSL:
1064     break;
1065 
1066   case Language::Unknown:
1067   case Language::Asm:
1068   case Language::LLVM_IR:
1069   case Language::RenderScript:
1070     // We can't do anything with these.
1071     return;
1072   }
1073 
1074   // We don't expect to find any #include directives in a preprocessed input.
1075   if (getCurrentFileKind().isPreprocessed())
1076     return;
1077 
1078   CompilerInstance &CI = getCompilerInstance();
1079   auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
1080   if (Buffer) {
1081     unsigned Preamble =
1082         Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
1083     llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
1084   }
1085 }
1086 
1087 void DumpCompilerOptionsAction::ExecuteAction() {
1088   CompilerInstance &CI = getCompilerInstance();
1089   std::unique_ptr<raw_ostream> OSP =
1090       CI.createDefaultOutputFile(false, getCurrentFile());
1091   if (!OSP)
1092     return;
1093 
1094   raw_ostream &OS = *OSP;
1095   const Preprocessor &PP = CI.getPreprocessor();
1096   const LangOptions &LangOpts = PP.getLangOpts();
1097 
1098   // FIXME: Rather than manually format the JSON (which is awkward due to
1099   // needing to remove trailing commas), this should make use of a JSON library.
1100   // FIXME: Instead of printing enums as an integral value and specifying the
1101   // type as a separate field, use introspection to print the enumerator.
1102 
1103   OS << "{\n";
1104   OS << "\n\"features\" : [\n";
1105   {
1106     llvm::SmallString<128> Str;
1107 #define FEATURE(Name, Predicate)                                               \
1108   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
1109       .toVector(Str);
1110 #include "clang/Basic/Features.def"
1111 #undef FEATURE
1112     // Remove the newline and comma from the last entry to ensure this remains
1113     // valid JSON.
1114     OS << Str.substr(0, Str.size() - 2);
1115   }
1116   OS << "\n],\n";
1117 
1118   OS << "\n\"extensions\" : [\n";
1119   {
1120     llvm::SmallString<128> Str;
1121 #define EXTENSION(Name, Predicate)                                             \
1122   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
1123       .toVector(Str);
1124 #include "clang/Basic/Features.def"
1125 #undef EXTENSION
1126     // Remove the newline and comma from the last entry to ensure this remains
1127     // valid JSON.
1128     OS << Str.substr(0, Str.size() - 2);
1129   }
1130   OS << "\n]\n";
1131 
1132   OS << "}";
1133 }
1134 
1135 void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
1136   CompilerInstance &CI = getCompilerInstance();
1137   SourceManager &SM = CI.getPreprocessor().getSourceManager();
1138   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
1139 
1140   llvm::SmallVector<dependency_directives_scan::Token, 16> Tokens;
1141   llvm::SmallVector<dependency_directives_scan::Directive, 32> Directives;
1142   if (scanSourceForDependencyDirectives(
1143           FromFile.getBuffer(), Tokens, Directives, &CI.getDiagnostics(),
1144           SM.getLocForStartOfFile(SM.getMainFileID()))) {
1145     assert(CI.getDiagnostics().hasErrorOccurred() &&
1146            "no errors reported for failure");
1147 
1148     // Preprocess the source when verifying the diagnostics to capture the
1149     // 'expected' comments.
1150     if (CI.getDiagnosticOpts().VerifyDiagnostics) {
1151       // Make sure we don't emit new diagnostics!
1152       CI.getDiagnostics().setSuppressAllDiagnostics(true);
1153       Preprocessor &PP = getCompilerInstance().getPreprocessor();
1154       PP.EnterMainSourceFile();
1155       Token Tok;
1156       do {
1157         PP.Lex(Tok);
1158       } while (Tok.isNot(tok::eof));
1159     }
1160     return;
1161   }
1162   printDependencyDirectivesAsSource(FromFile.getBuffer(), Directives,
1163                                     llvm::outs());
1164 }
1165 
1166 void GetDependenciesByModuleNameAction::ExecuteAction() {
1167   CompilerInstance &CI = getCompilerInstance();
1168   Preprocessor &PP = CI.getPreprocessor();
1169   SourceManager &SM = PP.getSourceManager();
1170   FileID MainFileID = SM.getMainFileID();
1171   SourceLocation FileStart = SM.getLocForStartOfFile(MainFileID);
1172   SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1173   IdentifierInfo *ModuleID = PP.getIdentifierInfo(ModuleName);
1174   Path.push_back(std::make_pair(ModuleID, FileStart));
1175   auto ModResult = CI.loadModule(FileStart, Path, Module::Hidden, false);
1176   PPCallbacks *CB = PP.getPPCallbacks();
1177   CB->moduleImport(SourceLocation(), Path, ModResult);
1178 }
1179