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({std::string(Name), *FE});
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       DisableValidationForModuleKind::None,
341       /*AllowASTWithCompilerErrors*/ 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       PrintingPolicy Policy = TheSema.Context.getPrintingPolicy();
470       // FIXME: Also ask for FullyQualifiedNames?
471       Policy.SuppressDefaultTemplateArgs = false;
472       NamedTemplate->getNameForDiagnostic(OS, Policy, true);
473       const PresumedLoc DefLoc =
474         TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
475       if(!DefLoc.isInvalid())
476         Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
477                                    std::to_string(DefLoc.getLine()) + ":" +
478                                    std::to_string(DefLoc.getColumn());
479     }
480     const PresumedLoc PoiLoc =
481         TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
482     if (!PoiLoc.isInvalid()) {
483       Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
484                                    std::to_string(PoiLoc.getLine()) + ":" +
485                                    std::to_string(PoiLoc.getColumn());
486     }
487     return Entry;
488   }
489 };
490 } // namespace
491 
492 std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)493 TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
494   return std::make_unique<ASTConsumer>();
495 }
496 
ExecuteAction()497 void TemplightDumpAction::ExecuteAction() {
498   CompilerInstance &CI = getCompilerInstance();
499 
500   // This part is normally done by ASTFrontEndAction, but needs to happen
501   // before Templight observers can be created
502   // FIXME: Move the truncation aspect of this into Sema, we delayed this till
503   // here so the source manager would be initialized.
504   EnsureSemaIsCreated(CI, *this);
505 
506   CI.getSema().TemplateInstCallbacks.push_back(
507       std::make_unique<DefaultTemplateInstCallback>());
508   ASTFrontendAction::ExecuteAction();
509 }
510 
511 namespace {
512   /// AST reader listener that dumps module information for a module
513   /// file.
514   class DumpModuleInfoListener : public ASTReaderListener {
515     llvm::raw_ostream &Out;
516 
517   public:
DumpModuleInfoListener(llvm::raw_ostream & Out)518     DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
519 
520 #define DUMP_BOOLEAN(Value, Text)                       \
521     Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
522 
ReadFullVersionInformation(StringRef FullVersion)523     bool ReadFullVersionInformation(StringRef FullVersion) override {
524       Out.indent(2)
525         << "Generated by "
526         << (FullVersion == getClangFullRepositoryVersion()? "this"
527                                                           : "a different")
528         << " Clang: " << FullVersion << "\n";
529       return ASTReaderListener::ReadFullVersionInformation(FullVersion);
530     }
531 
ReadModuleName(StringRef ModuleName)532     void ReadModuleName(StringRef ModuleName) override {
533       Out.indent(2) << "Module name: " << ModuleName << "\n";
534     }
ReadModuleMapFile(StringRef ModuleMapPath)535     void ReadModuleMapFile(StringRef ModuleMapPath) override {
536       Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
537     }
538 
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)539     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
540                              bool AllowCompatibleDifferences) override {
541       Out.indent(2) << "Language options:\n";
542 #define LANGOPT(Name, Bits, Default, Description) \
543       DUMP_BOOLEAN(LangOpts.Name, Description);
544 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
545       Out.indent(4) << Description << ": "                   \
546                     << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
547 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
548       Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
549 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
550 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
551 #include "clang/Basic/LangOptions.def"
552 
553       if (!LangOpts.ModuleFeatures.empty()) {
554         Out.indent(4) << "Module features:\n";
555         for (StringRef Feature : LangOpts.ModuleFeatures)
556           Out.indent(6) << Feature << "\n";
557       }
558 
559       return false;
560     }
561 
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain,bool AllowCompatibleDifferences)562     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
563                            bool AllowCompatibleDifferences) override {
564       Out.indent(2) << "Target options:\n";
565       Out.indent(4) << "  Triple: " << TargetOpts.Triple << "\n";
566       Out.indent(4) << "  CPU: " << TargetOpts.CPU << "\n";
567       Out.indent(4) << "  TuneCPU: " << TargetOpts.TuneCPU << "\n";
568       Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
569 
570       if (!TargetOpts.FeaturesAsWritten.empty()) {
571         Out.indent(4) << "Target features:\n";
572         for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
573              I != N; ++I) {
574           Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
575         }
576       }
577 
578       return false;
579     }
580 
ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,bool Complain)581     bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
582                                bool Complain) override {
583       Out.indent(2) << "Diagnostic options:\n";
584 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
585 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
586       Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
587 #define VALUE_DIAGOPT(Name, Bits, Default) \
588       Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
589 #include "clang/Basic/DiagnosticOptions.def"
590 
591       Out.indent(4) << "Diagnostic flags:\n";
592       for (const std::string &Warning : DiagOpts->Warnings)
593         Out.indent(6) << "-W" << Warning << "\n";
594       for (const std::string &Remark : DiagOpts->Remarks)
595         Out.indent(6) << "-R" << Remark << "\n";
596 
597       return false;
598     }
599 
ReadHeaderSearchOptions(const HeaderSearchOptions & HSOpts,StringRef SpecificModuleCachePath,bool Complain)600     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
601                                  StringRef SpecificModuleCachePath,
602                                  bool Complain) override {
603       Out.indent(2) << "Header search options:\n";
604       Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
605       Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
606       Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
607       DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
608                    "Use builtin include directories [-nobuiltininc]");
609       DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
610                    "Use standard system include directories [-nostdinc]");
611       DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
612                    "Use standard C++ include directories [-nostdinc++]");
613       DUMP_BOOLEAN(HSOpts.UseLibcxx,
614                    "Use libc++ (rather than libstdc++) [-stdlib=]");
615       return false;
616     }
617 
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool Complain,std::string & SuggestedPredefines)618     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
619                                  bool Complain,
620                                  std::string &SuggestedPredefines) override {
621       Out.indent(2) << "Preprocessor options:\n";
622       DUMP_BOOLEAN(PPOpts.UsePredefines,
623                    "Uses compiler/target-specific predefines [-undef]");
624       DUMP_BOOLEAN(PPOpts.DetailedRecord,
625                    "Uses detailed preprocessing record (for indexing)");
626 
627       if (!PPOpts.Macros.empty()) {
628         Out.indent(4) << "Predefined macros:\n";
629       }
630 
631       for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
632              I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
633            I != IEnd; ++I) {
634         Out.indent(6);
635         if (I->second)
636           Out << "-U";
637         else
638           Out << "-D";
639         Out << I->first << "\n";
640       }
641       return false;
642     }
643 
644     /// Indicates that a particular module file extension has been read.
readModuleFileExtension(const ModuleFileExtensionMetadata & Metadata)645     void readModuleFileExtension(
646            const ModuleFileExtensionMetadata &Metadata) override {
647       Out.indent(2) << "Module file extension '"
648                     << Metadata.BlockName << "' " << Metadata.MajorVersion
649                     << "." << Metadata.MinorVersion;
650       if (!Metadata.UserInfo.empty()) {
651         Out << ": ";
652         Out.write_escaped(Metadata.UserInfo);
653       }
654 
655       Out << "\n";
656     }
657 
658     /// Tells the \c ASTReaderListener that we want to receive the
659     /// input files of the AST file via \c visitInputFile.
needsInputFileVisitation()660     bool needsInputFileVisitation() override { return true; }
661 
662     /// Tells the \c ASTReaderListener that we want to receive the
663     /// input files of the AST file via \c visitInputFile.
needsSystemInputFileVisitation()664     bool needsSystemInputFileVisitation() override { return true; }
665 
666     /// Indicates that the AST file contains particular input file.
667     ///
668     /// \returns true to continue receiving the next input file, false to stop.
visitInputFile(StringRef Filename,bool isSystem,bool isOverridden,bool isExplicitModule)669     bool visitInputFile(StringRef Filename, bool isSystem,
670                         bool isOverridden, bool isExplicitModule) override {
671 
672       Out.indent(2) << "Input file: " << Filename;
673 
674       if (isSystem || isOverridden || isExplicitModule) {
675         Out << " [";
676         if (isSystem) {
677           Out << "System";
678           if (isOverridden || isExplicitModule)
679             Out << ", ";
680         }
681         if (isOverridden) {
682           Out << "Overridden";
683           if (isExplicitModule)
684             Out << ", ";
685         }
686         if (isExplicitModule)
687           Out << "ExplicitModule";
688 
689         Out << "]";
690       }
691 
692       Out << "\n";
693 
694       return true;
695     }
696 
697     /// Returns true if this \c ASTReaderListener wants to receive the
698     /// imports of the AST file via \c visitImport, false otherwise.
needsImportVisitation() const699     bool needsImportVisitation() const override { return true; }
700 
701     /// If needsImportVisitation returns \c true, this is called for each
702     /// AST file imported by this AST file.
visitImport(StringRef ModuleName,StringRef Filename)703     void visitImport(StringRef ModuleName, StringRef Filename) override {
704       Out.indent(2) << "Imports module '" << ModuleName
705                     << "': " << Filename.str() << "\n";
706     }
707 #undef DUMP_BOOLEAN
708   };
709 }
710 
BeginInvocation(CompilerInstance & CI)711 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
712   // The Object file reader also supports raw ast files and there is no point in
713   // being strict about the module file format in -module-file-info mode.
714   CI.getHeaderSearchOpts().ModuleFormat = "obj";
715   return true;
716 }
717 
ExecuteAction()718 void DumpModuleInfoAction::ExecuteAction() {
719   // Set up the output file.
720   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
721   StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
722   if (!OutputFileName.empty() && OutputFileName != "-") {
723     std::error_code EC;
724     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
725                                            llvm::sys::fs::OF_Text));
726   }
727   llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
728 
729   Out << "Information for module file '" << getCurrentFile() << "':\n";
730   auto &FileMgr = getCompilerInstance().getFileManager();
731   auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
732   StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
733   bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
734                 Magic[2] == 'C' && Magic[3] == 'H');
735   Out << "  Module format: " << (IsRaw ? "raw" : "obj") << "\n";
736 
737   Preprocessor &PP = getCompilerInstance().getPreprocessor();
738   DumpModuleInfoListener Listener(Out);
739   HeaderSearchOptions &HSOpts =
740       PP.getHeaderSearchInfo().getHeaderSearchOpts();
741   ASTReader::readASTFileControlBlock(
742       getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
743       /*FindModuleFileExtensions=*/true, Listener,
744       HSOpts.ModulesValidateDiagnosticOptions);
745 }
746 
747 //===----------------------------------------------------------------------===//
748 // Preprocessor Actions
749 //===----------------------------------------------------------------------===//
750 
ExecuteAction()751 void DumpRawTokensAction::ExecuteAction() {
752   Preprocessor &PP = getCompilerInstance().getPreprocessor();
753   SourceManager &SM = PP.getSourceManager();
754 
755   // Start lexing the specified input file.
756   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
757   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
758   RawLex.SetKeepWhitespaceMode(true);
759 
760   Token RawTok;
761   RawLex.LexFromRawLexer(RawTok);
762   while (RawTok.isNot(tok::eof)) {
763     PP.DumpToken(RawTok, true);
764     llvm::errs() << "\n";
765     RawLex.LexFromRawLexer(RawTok);
766   }
767 }
768 
ExecuteAction()769 void DumpTokensAction::ExecuteAction() {
770   Preprocessor &PP = getCompilerInstance().getPreprocessor();
771   // Start preprocessing the specified input file.
772   Token Tok;
773   PP.EnterMainSourceFile();
774   do {
775     PP.Lex(Tok);
776     PP.DumpToken(Tok, true);
777     llvm::errs() << "\n";
778   } while (Tok.isNot(tok::eof));
779 }
780 
ExecuteAction()781 void PreprocessOnlyAction::ExecuteAction() {
782   Preprocessor &PP = getCompilerInstance().getPreprocessor();
783 
784   // Ignore unknown pragmas.
785   PP.IgnorePragmas();
786 
787   Token Tok;
788   // Start parsing the specified input file.
789   PP.EnterMainSourceFile();
790   do {
791     PP.Lex(Tok);
792   } while (Tok.isNot(tok::eof));
793 }
794 
ExecuteAction()795 void PrintPreprocessedAction::ExecuteAction() {
796   CompilerInstance &CI = getCompilerInstance();
797   // Output file may need to be set to 'Binary', to avoid converting Unix style
798   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
799   //
800   // Look to see what type of line endings the file uses. If there's a
801   // CRLF, then we won't open the file up in binary mode. If there is
802   // just an LF or CR, then we will open the file up in binary mode.
803   // In this fashion, the output format should match the input format, unless
804   // the input format has inconsistent line endings.
805   //
806   // This should be a relatively fast operation since most files won't have
807   // all of their source code on a single line. However, that is still a
808   // concern, so if we scan for too long, we'll just assume the file should
809   // be opened in binary mode.
810   bool BinaryMode = true;
811   const SourceManager& SM = CI.getSourceManager();
812   if (llvm::Optional<llvm::MemoryBufferRef> Buffer =
813           SM.getBufferOrNone(SM.getMainFileID())) {
814     const char *cur = Buffer->getBufferStart();
815     const char *end = Buffer->getBufferEnd();
816     const char *next = (cur != end) ? cur + 1 : end;
817 
818     // Limit ourselves to only scanning 256 characters into the source
819     // file.  This is mostly a sanity check in case the file has no
820     // newlines whatsoever.
821     if (end - cur > 256) end = cur + 256;
822 
823     while (next < end) {
824       if (*cur == 0x0D) {  // CR
825         if (*next == 0x0A)  // CRLF
826           BinaryMode = false;
827 
828         break;
829       } else if (*cur == 0x0A)  // LF
830         break;
831 
832       ++cur;
833       ++next;
834     }
835   }
836 
837   std::unique_ptr<raw_ostream> OS =
838       CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
839   if (!OS) return;
840 
841   // If we're preprocessing a module map, start by dumping the contents of the
842   // module itself before switching to the input buffer.
843   auto &Input = getCurrentInput();
844   if (Input.getKind().getFormat() == InputKind::ModuleMap) {
845     if (Input.isFile()) {
846       (*OS) << "# 1 \"";
847       OS->write_escaped(Input.getFile());
848       (*OS) << "\"\n";
849     }
850     getCurrentModule()->print(*OS);
851     (*OS) << "#pragma clang module contents\n";
852   }
853 
854   DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
855                            CI.getPreprocessorOutputOpts());
856 }
857 
ExecuteAction()858 void PrintPreambleAction::ExecuteAction() {
859   switch (getCurrentFileKind().getLanguage()) {
860   case Language::C:
861   case Language::CXX:
862   case Language::ObjC:
863   case Language::ObjCXX:
864   case Language::OpenCL:
865   case Language::CUDA:
866   case Language::HIP:
867     break;
868 
869   case Language::Unknown:
870   case Language::Asm:
871   case Language::LLVM_IR:
872   case Language::RenderScript:
873     // We can't do anything with these.
874     return;
875   }
876 
877   // We don't expect to find any #include directives in a preprocessed input.
878   if (getCurrentFileKind().isPreprocessed())
879     return;
880 
881   CompilerInstance &CI = getCompilerInstance();
882   auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
883   if (Buffer) {
884     unsigned Preamble =
885         Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
886     llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
887   }
888 }
889 
ExecuteAction()890 void DumpCompilerOptionsAction::ExecuteAction() {
891   CompilerInstance &CI = getCompilerInstance();
892   std::unique_ptr<raw_ostream> OSP =
893       CI.createDefaultOutputFile(false, getCurrentFile());
894   if (!OSP)
895     return;
896 
897   raw_ostream &OS = *OSP;
898   const Preprocessor &PP = CI.getPreprocessor();
899   const LangOptions &LangOpts = PP.getLangOpts();
900 
901   // FIXME: Rather than manually format the JSON (which is awkward due to
902   // needing to remove trailing commas), this should make use of a JSON library.
903   // FIXME: Instead of printing enums as an integral value and specifying the
904   // type as a separate field, use introspection to print the enumerator.
905 
906   OS << "{\n";
907   OS << "\n\"features\" : [\n";
908   {
909     llvm::SmallString<128> Str;
910 #define FEATURE(Name, Predicate)                                               \
911   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
912       .toVector(Str);
913 #include "clang/Basic/Features.def"
914 #undef FEATURE
915     // Remove the newline and comma from the last entry to ensure this remains
916     // valid JSON.
917     OS << Str.substr(0, Str.size() - 2);
918   }
919   OS << "\n],\n";
920 
921   OS << "\n\"extensions\" : [\n";
922   {
923     llvm::SmallString<128> Str;
924 #define EXTENSION(Name, Predicate)                                             \
925   ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
926       .toVector(Str);
927 #include "clang/Basic/Features.def"
928 #undef EXTENSION
929     // Remove the newline and comma from the last entry to ensure this remains
930     // valid JSON.
931     OS << Str.substr(0, Str.size() - 2);
932   }
933   OS << "\n]\n";
934 
935   OS << "}";
936 }
937 
ExecuteAction()938 void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
939   CompilerInstance &CI = getCompilerInstance();
940   SourceManager &SM = CI.getPreprocessor().getSourceManager();
941   llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
942 
943   llvm::SmallString<1024> Output;
944   llvm::SmallVector<minimize_source_to_dependency_directives::Token, 32> Toks;
945   if (minimizeSourceToDependencyDirectives(
946           FromFile.getBuffer(), Output, Toks, &CI.getDiagnostics(),
947           SM.getLocForStartOfFile(SM.getMainFileID()))) {
948     assert(CI.getDiagnostics().hasErrorOccurred() &&
949            "no errors reported for failure");
950 
951     // Preprocess the source when verifying the diagnostics to capture the
952     // 'expected' comments.
953     if (CI.getDiagnosticOpts().VerifyDiagnostics) {
954       // Make sure we don't emit new diagnostics!
955       CI.getDiagnostics().setSuppressAllDiagnostics(true);
956       Preprocessor &PP = getCompilerInstance().getPreprocessor();
957       PP.EnterMainSourceFile();
958       Token Tok;
959       do {
960         PP.Lex(Tok);
961       } while (Tok.isNot(tok::eof));
962     }
963     return;
964   }
965   llvm::outs() << Output;
966 }
967