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