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