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