1 | //===-- CompilerInstance.h - Clang Compiler Instance ------------*- C++ -*-===// |
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 | #ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ |
10 | #define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ |
11 | |
12 | #include "clang/AST/ASTConsumer.h" |
13 | #include "clang/Basic/Diagnostic.h" |
14 | #include "clang/Basic/SourceManager.h" |
15 | #include "clang/Basic/TargetInfo.h" |
16 | #include "clang/Frontend/CompilerInvocation.h" |
17 | #include "clang/Frontend/PCHContainerOperations.h" |
18 | #include "clang/Frontend/Utils.h" |
19 | #include "clang/Lex/HeaderSearchOptions.h" |
20 | #include "clang/Lex/ModuleLoader.h" |
21 | #include "llvm/ADT/ArrayRef.h" |
22 | #include "llvm/ADT/DenseMap.h" |
23 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
24 | #include "llvm/ADT/StringRef.h" |
25 | #include "llvm/Support/BuryPointer.h" |
26 | #include "llvm/Support/FileSystem.h" |
27 | #include <cassert> |
28 | #include <list> |
29 | #include <memory> |
30 | #include <optional> |
31 | #include <string> |
32 | #include <utility> |
33 | |
34 | namespace llvm { |
35 | class raw_fd_ostream; |
36 | class Timer; |
37 | class TimerGroup; |
38 | } |
39 | |
40 | namespace clang { |
41 | class ASTContext; |
42 | class ASTReader; |
43 | |
44 | namespace serialization { |
45 | class ModuleFile; |
46 | } |
47 | |
48 | class CodeCompleteConsumer; |
49 | class DiagnosticsEngine; |
50 | class DiagnosticConsumer; |
51 | class FileManager; |
52 | class FrontendAction; |
53 | class InMemoryModuleCache; |
54 | class Module; |
55 | class Preprocessor; |
56 | class Sema; |
57 | class SourceManager; |
58 | class TargetInfo; |
59 | enum class DisableValidationForModuleKind; |
60 | |
61 | /// CompilerInstance - Helper class for managing a single instance of the Clang |
62 | /// compiler. |
63 | /// |
64 | /// The CompilerInstance serves two purposes: |
65 | /// (1) It manages the various objects which are necessary to run the compiler, |
66 | /// for example the preprocessor, the target information, and the AST |
67 | /// context. |
68 | /// (2) It provides utility routines for constructing and manipulating the |
69 | /// common Clang objects. |
70 | /// |
71 | /// The compiler instance generally owns the instance of all the objects that it |
72 | /// manages. However, clients can still share objects by manually setting the |
73 | /// object and retaking ownership prior to destroying the CompilerInstance. |
74 | /// |
75 | /// The compiler instance is intended to simplify clients, but not to lock them |
76 | /// in to the compiler instance for everything. When possible, utility functions |
77 | /// come in two forms; a short form that reuses the CompilerInstance objects, |
78 | /// and a long form that takes explicit instances of any required objects. |
79 | class CompilerInstance : public ModuleLoader { |
80 | /// The options used in this compiler instance. |
81 | std::shared_ptr<CompilerInvocation> Invocation; |
82 | |
83 | /// The diagnostics engine instance. |
84 | IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics; |
85 | |
86 | /// The target being compiled for. |
87 | IntrusiveRefCntPtr<TargetInfo> Target; |
88 | |
89 | /// Auxiliary Target info. |
90 | IntrusiveRefCntPtr<TargetInfo> AuxTarget; |
91 | |
92 | /// The file manager. |
93 | IntrusiveRefCntPtr<FileManager> FileMgr; |
94 | |
95 | /// The source manager. |
96 | IntrusiveRefCntPtr<SourceManager> SourceMgr; |
97 | |
98 | /// The cache of PCM files. |
99 | IntrusiveRefCntPtr<InMemoryModuleCache> ModuleCache; |
100 | |
101 | /// The preprocessor. |
102 | std::shared_ptr<Preprocessor> PP; |
103 | |
104 | /// The AST context. |
105 | IntrusiveRefCntPtr<ASTContext> Context; |
106 | |
107 | /// An optional sema source that will be attached to sema. |
108 | IntrusiveRefCntPtr<ExternalSemaSource> ExternalSemaSrc; |
109 | |
110 | /// The AST consumer. |
111 | std::unique_ptr<ASTConsumer> Consumer; |
112 | |
113 | /// The code completion consumer. |
114 | std::unique_ptr<CodeCompleteConsumer> CompletionConsumer; |
115 | |
116 | /// The semantic analysis object. |
117 | std::unique_ptr<Sema> TheSema; |
118 | |
119 | /// The frontend timer group. |
120 | std::unique_ptr<llvm::TimerGroup> FrontendTimerGroup; |
121 | |
122 | /// The frontend timer. |
123 | std::unique_ptr<llvm::Timer> FrontendTimer; |
124 | |
125 | /// The ASTReader, if one exists. |
126 | IntrusiveRefCntPtr<ASTReader> TheASTReader; |
127 | |
128 | /// The module dependency collector for crashdumps |
129 | std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector; |
130 | |
131 | /// The module provider. |
132 | std::shared_ptr<PCHContainerOperations> ThePCHContainerOperations; |
133 | |
134 | std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors; |
135 | |
136 | /// The set of top-level modules that has already been built on the |
137 | /// fly as part of this overall compilation action. |
138 | std::map<std::string, std::string, std::less<>> BuiltModules; |
139 | |
140 | /// Should we delete the BuiltModules when we're done? |
141 | bool DeleteBuiltModules = true; |
142 | |
143 | /// The location of the module-import keyword for the last module |
144 | /// import. |
145 | SourceLocation LastModuleImportLoc; |
146 | |
147 | /// The result of the last module import. |
148 | /// |
149 | ModuleLoadResult LastModuleImportResult; |
150 | |
151 | /// Whether we should (re)build the global module index once we |
152 | /// have finished with this translation unit. |
153 | bool BuildGlobalModuleIndex = false; |
154 | |
155 | /// We have a full global module index, with all modules. |
156 | bool HaveFullGlobalModuleIndex = false; |
157 | |
158 | /// One or more modules failed to build. |
159 | bool DisableGeneratingGlobalModuleIndex = false; |
160 | |
161 | /// The stream for verbose output if owned, otherwise nullptr. |
162 | std::unique_ptr<raw_ostream> OwnedVerboseOutputStream; |
163 | |
164 | /// The stream for verbose output. |
165 | raw_ostream *VerboseOutputStream = &llvm::errs(); |
166 | |
167 | /// Holds information about the output file. |
168 | /// |
169 | /// If TempFilename is not empty we must rename it to Filename at the end. |
170 | /// TempFilename may be empty and Filename non-empty if creating the temporary |
171 | /// failed. |
172 | struct OutputFile { |
173 | std::string Filename; |
174 | std::optional<llvm::sys::fs::TempFile> File; |
175 | |
176 | OutputFile(std::string filename, |
177 | std::optional<llvm::sys::fs::TempFile> file) |
178 | : Filename(std::move(filename)), File(std::move(file)) {} |
179 | }; |
180 | |
181 | /// The list of active output files. |
182 | std::list<OutputFile> OutputFiles; |
183 | |
184 | /// Force an output buffer. |
185 | std::unique_ptr<llvm::raw_pwrite_stream> OutputStream; |
186 | |
187 | CompilerInstance(const CompilerInstance &) = delete; |
188 | void operator=(const CompilerInstance &) = delete; |
189 | public: |
190 | explicit CompilerInstance( |
191 | std::shared_ptr<PCHContainerOperations> PCHContainerOps = |
192 | std::make_shared<PCHContainerOperations>(), |
193 | InMemoryModuleCache *SharedModuleCache = nullptr); |
194 | ~CompilerInstance() override; |
195 | |
196 | /// @name High-Level Operations |
197 | /// @{ |
198 | |
199 | /// ExecuteAction - Execute the provided action against the compiler's |
200 | /// CompilerInvocation object. |
201 | /// |
202 | /// This function makes the following assumptions: |
203 | /// |
204 | /// - The invocation options should be initialized. This function does not |
205 | /// handle the '-help' or '-version' options, clients should handle those |
206 | /// directly. |
207 | /// |
208 | /// - The diagnostics engine should have already been created by the client. |
209 | /// |
210 | /// - No other CompilerInstance state should have been initialized (this is |
211 | /// an unchecked error). |
212 | /// |
213 | /// - Clients should have initialized any LLVM target features that may be |
214 | /// required. |
215 | /// |
216 | /// - Clients should eventually call llvm_shutdown() upon the completion of |
217 | /// this routine to ensure that any managed objects are properly destroyed. |
218 | /// |
219 | /// Note that this routine may write output to 'stderr'. |
220 | /// |
221 | /// \param Act - The action to execute. |
222 | /// \return - True on success. |
223 | // |
224 | // FIXME: Eliminate the llvm_shutdown requirement, that should either be part |
225 | // of the context or else not CompilerInstance specific. |
226 | bool ExecuteAction(FrontendAction &Act); |
227 | |
228 | /// Load the list of plugins requested in the \c FrontendOptions. |
229 | void LoadRequestedPlugins(); |
230 | |
231 | /// @} |
232 | /// @name Compiler Invocation and Options |
233 | /// @{ |
234 | |
235 | bool hasInvocation() const { return Invocation != nullptr; } |
236 | |
237 | CompilerInvocation &getInvocation() { |
238 | assert(Invocation && "Compiler instance has no invocation!" ); |
239 | return *Invocation; |
240 | } |
241 | |
242 | std::shared_ptr<CompilerInvocation> getInvocationPtr() { return Invocation; } |
243 | |
244 | /// setInvocation - Replace the current invocation. |
245 | void setInvocation(std::shared_ptr<CompilerInvocation> Value); |
246 | |
247 | /// Indicates whether we should (re)build the global module index. |
248 | bool shouldBuildGlobalModuleIndex() const; |
249 | |
250 | /// Set the flag indicating whether we should (re)build the global |
251 | /// module index. |
252 | void setBuildGlobalModuleIndex(bool Build) { |
253 | BuildGlobalModuleIndex = Build; |
254 | } |
255 | |
256 | /// @} |
257 | /// @name Forwarding Methods |
258 | /// @{ |
259 | |
260 | AnalyzerOptions &getAnalyzerOpts() { return Invocation->getAnalyzerOpts(); } |
261 | |
262 | CodeGenOptions &getCodeGenOpts() { |
263 | return Invocation->getCodeGenOpts(); |
264 | } |
265 | const CodeGenOptions &getCodeGenOpts() const { |
266 | return Invocation->getCodeGenOpts(); |
267 | } |
268 | |
269 | DependencyOutputOptions &getDependencyOutputOpts() { |
270 | return Invocation->getDependencyOutputOpts(); |
271 | } |
272 | const DependencyOutputOptions &getDependencyOutputOpts() const { |
273 | return Invocation->getDependencyOutputOpts(); |
274 | } |
275 | |
276 | DiagnosticOptions &getDiagnosticOpts() { |
277 | return Invocation->getDiagnosticOpts(); |
278 | } |
279 | const DiagnosticOptions &getDiagnosticOpts() const { |
280 | return Invocation->getDiagnosticOpts(); |
281 | } |
282 | |
283 | FileSystemOptions &getFileSystemOpts() { |
284 | return Invocation->getFileSystemOpts(); |
285 | } |
286 | const FileSystemOptions &getFileSystemOpts() const { |
287 | return Invocation->getFileSystemOpts(); |
288 | } |
289 | |
290 | FrontendOptions &getFrontendOpts() { |
291 | return Invocation->getFrontendOpts(); |
292 | } |
293 | const FrontendOptions &getFrontendOpts() const { |
294 | return Invocation->getFrontendOpts(); |
295 | } |
296 | |
297 | InstallAPIOptions &getInstallAPIOpts() { |
298 | return Invocation->getInstallAPIOpts(); |
299 | } |
300 | const InstallAPIOptions &getInstallAPIOpts() const { |
301 | return Invocation->getInstallAPIOpts(); |
302 | } |
303 | |
304 | HeaderSearchOptions &() { |
305 | return Invocation->getHeaderSearchOpts(); |
306 | } |
307 | const HeaderSearchOptions &() const { |
308 | return Invocation->getHeaderSearchOpts(); |
309 | } |
310 | std::shared_ptr<HeaderSearchOptions> () const { |
311 | return Invocation->getHeaderSearchOptsPtr(); |
312 | } |
313 | |
314 | APINotesOptions &getAPINotesOpts() { return Invocation->getAPINotesOpts(); } |
315 | const APINotesOptions &getAPINotesOpts() const { |
316 | return Invocation->getAPINotesOpts(); |
317 | } |
318 | |
319 | LangOptions &getLangOpts() { return Invocation->getLangOpts(); } |
320 | const LangOptions &getLangOpts() const { return Invocation->getLangOpts(); } |
321 | |
322 | PreprocessorOptions &getPreprocessorOpts() { |
323 | return Invocation->getPreprocessorOpts(); |
324 | } |
325 | const PreprocessorOptions &getPreprocessorOpts() const { |
326 | return Invocation->getPreprocessorOpts(); |
327 | } |
328 | |
329 | PreprocessorOutputOptions &getPreprocessorOutputOpts() { |
330 | return Invocation->getPreprocessorOutputOpts(); |
331 | } |
332 | const PreprocessorOutputOptions &getPreprocessorOutputOpts() const { |
333 | return Invocation->getPreprocessorOutputOpts(); |
334 | } |
335 | |
336 | TargetOptions &getTargetOpts() { |
337 | return Invocation->getTargetOpts(); |
338 | } |
339 | const TargetOptions &getTargetOpts() const { |
340 | return Invocation->getTargetOpts(); |
341 | } |
342 | |
343 | /// @} |
344 | /// @name Diagnostics Engine |
345 | /// @{ |
346 | |
347 | bool hasDiagnostics() const { return Diagnostics != nullptr; } |
348 | |
349 | /// Get the current diagnostics engine. |
350 | DiagnosticsEngine &getDiagnostics() const { |
351 | assert(Diagnostics && "Compiler instance has no diagnostics!" ); |
352 | return *Diagnostics; |
353 | } |
354 | |
355 | IntrusiveRefCntPtr<DiagnosticsEngine> getDiagnosticsPtr() const { |
356 | assert(Diagnostics && "Compiler instance has no diagnostics!" ); |
357 | return Diagnostics; |
358 | } |
359 | |
360 | /// setDiagnostics - Replace the current diagnostics engine. |
361 | void setDiagnostics(DiagnosticsEngine *Value); |
362 | |
363 | DiagnosticConsumer &getDiagnosticClient() const { |
364 | assert(Diagnostics && Diagnostics->getClient() && |
365 | "Compiler instance has no diagnostic client!" ); |
366 | return *Diagnostics->getClient(); |
367 | } |
368 | |
369 | /// @} |
370 | /// @name VerboseOutputStream |
371 | /// @{ |
372 | |
373 | /// Replace the current stream for verbose output. |
374 | void setVerboseOutputStream(raw_ostream &Value); |
375 | |
376 | /// Replace the current stream for verbose output. |
377 | void setVerboseOutputStream(std::unique_ptr<raw_ostream> Value); |
378 | |
379 | /// Get the current stream for verbose output. |
380 | raw_ostream &getVerboseOutputStream() { |
381 | return *VerboseOutputStream; |
382 | } |
383 | |
384 | /// @} |
385 | /// @name Target Info |
386 | /// @{ |
387 | |
388 | bool hasTarget() const { return Target != nullptr; } |
389 | |
390 | TargetInfo &getTarget() const { |
391 | assert(Target && "Compiler instance has no target!" ); |
392 | return *Target; |
393 | } |
394 | |
395 | IntrusiveRefCntPtr<TargetInfo> getTargetPtr() const { |
396 | assert(Target && "Compiler instance has no target!" ); |
397 | return Target; |
398 | } |
399 | |
400 | /// Replace the current Target. |
401 | void setTarget(TargetInfo *Value); |
402 | |
403 | /// @} |
404 | /// @name AuxTarget Info |
405 | /// @{ |
406 | |
407 | TargetInfo *getAuxTarget() const { return AuxTarget.get(); } |
408 | |
409 | /// Replace the current AuxTarget. |
410 | void setAuxTarget(TargetInfo *Value); |
411 | |
412 | // Create Target and AuxTarget based on current options |
413 | bool createTarget(); |
414 | |
415 | /// @} |
416 | /// @name Virtual File System |
417 | /// @{ |
418 | |
419 | llvm::vfs::FileSystem &getVirtualFileSystem() const; |
420 | |
421 | /// @} |
422 | /// @name File Manager |
423 | /// @{ |
424 | |
425 | bool hasFileManager() const { return FileMgr != nullptr; } |
426 | |
427 | /// Return the current file manager to the caller. |
428 | FileManager &getFileManager() const { |
429 | assert(FileMgr && "Compiler instance has no file manager!" ); |
430 | return *FileMgr; |
431 | } |
432 | |
433 | IntrusiveRefCntPtr<FileManager> getFileManagerPtr() const { |
434 | assert(FileMgr && "Compiler instance has no file manager!" ); |
435 | return FileMgr; |
436 | } |
437 | |
438 | void resetAndLeakFileManager() { |
439 | llvm::BuryPointer(Ptr: FileMgr.get()); |
440 | FileMgr.resetWithoutRelease(); |
441 | } |
442 | |
443 | /// Replace the current file manager and virtual file system. |
444 | void setFileManager(FileManager *Value); |
445 | |
446 | /// @} |
447 | /// @name Source Manager |
448 | /// @{ |
449 | |
450 | bool hasSourceManager() const { return SourceMgr != nullptr; } |
451 | |
452 | /// Return the current source manager. |
453 | SourceManager &getSourceManager() const { |
454 | assert(SourceMgr && "Compiler instance has no source manager!" ); |
455 | return *SourceMgr; |
456 | } |
457 | |
458 | IntrusiveRefCntPtr<SourceManager> getSourceManagerPtr() const { |
459 | assert(SourceMgr && "Compiler instance has no source manager!" ); |
460 | return SourceMgr; |
461 | } |
462 | |
463 | void resetAndLeakSourceManager() { |
464 | llvm::BuryPointer(Ptr: SourceMgr.get()); |
465 | SourceMgr.resetWithoutRelease(); |
466 | } |
467 | |
468 | /// setSourceManager - Replace the current source manager. |
469 | void setSourceManager(SourceManager *Value); |
470 | |
471 | /// @} |
472 | /// @name Preprocessor |
473 | /// @{ |
474 | |
475 | bool hasPreprocessor() const { return PP != nullptr; } |
476 | |
477 | /// Return the current preprocessor. |
478 | Preprocessor &getPreprocessor() const { |
479 | assert(PP && "Compiler instance has no preprocessor!" ); |
480 | return *PP; |
481 | } |
482 | |
483 | std::shared_ptr<Preprocessor> getPreprocessorPtr() { return PP; } |
484 | |
485 | void resetAndLeakPreprocessor() { |
486 | llvm::BuryPointer(Ptr: new std::shared_ptr<Preprocessor>(PP)); |
487 | } |
488 | |
489 | /// Replace the current preprocessor. |
490 | void setPreprocessor(std::shared_ptr<Preprocessor> Value); |
491 | |
492 | /// @} |
493 | /// @name ASTContext |
494 | /// @{ |
495 | |
496 | bool hasASTContext() const { return Context != nullptr; } |
497 | |
498 | ASTContext &getASTContext() const { |
499 | assert(Context && "Compiler instance has no AST context!" ); |
500 | return *Context; |
501 | } |
502 | |
503 | IntrusiveRefCntPtr<ASTContext> getASTContextPtr() const { |
504 | assert(Context && "Compiler instance has no AST context!" ); |
505 | return Context; |
506 | } |
507 | |
508 | void resetAndLeakASTContext() { |
509 | llvm::BuryPointer(Ptr: Context.get()); |
510 | Context.resetWithoutRelease(); |
511 | } |
512 | |
513 | /// setASTContext - Replace the current AST context. |
514 | void setASTContext(ASTContext *Value); |
515 | |
516 | /// Replace the current Sema; the compiler instance takes ownership |
517 | /// of S. |
518 | void setSema(Sema *S); |
519 | |
520 | /// @} |
521 | /// @name ASTConsumer |
522 | /// @{ |
523 | |
524 | bool hasASTConsumer() const { return (bool)Consumer; } |
525 | |
526 | ASTConsumer &getASTConsumer() const { |
527 | assert(Consumer && "Compiler instance has no AST consumer!" ); |
528 | return *Consumer; |
529 | } |
530 | |
531 | /// takeASTConsumer - Remove the current AST consumer and give ownership to |
532 | /// the caller. |
533 | std::unique_ptr<ASTConsumer> takeASTConsumer() { return std::move(Consumer); } |
534 | |
535 | /// setASTConsumer - Replace the current AST consumer; the compiler instance |
536 | /// takes ownership of \p Value. |
537 | void setASTConsumer(std::unique_ptr<ASTConsumer> Value); |
538 | |
539 | /// @} |
540 | /// @name Semantic analysis |
541 | /// @{ |
542 | bool hasSema() const { return (bool)TheSema; } |
543 | |
544 | Sema &getSema() const { |
545 | assert(TheSema && "Compiler instance has no Sema object!" ); |
546 | return *TheSema; |
547 | } |
548 | |
549 | std::unique_ptr<Sema> takeSema(); |
550 | void resetAndLeakSema(); |
551 | |
552 | /// @} |
553 | /// @name Module Management |
554 | /// @{ |
555 | |
556 | IntrusiveRefCntPtr<ASTReader> getASTReader() const; |
557 | void setASTReader(IntrusiveRefCntPtr<ASTReader> Reader); |
558 | |
559 | std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const; |
560 | void setModuleDepCollector( |
561 | std::shared_ptr<ModuleDependencyCollector> Collector); |
562 | |
563 | std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const { |
564 | return ThePCHContainerOperations; |
565 | } |
566 | |
567 | /// Return the appropriate PCHContainerWriter depending on the |
568 | /// current CodeGenOptions. |
569 | const PCHContainerWriter &getPCHContainerWriter() const { |
570 | assert(Invocation && "cannot determine module format without invocation" ); |
571 | StringRef Format = getHeaderSearchOpts().ModuleFormat; |
572 | auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format); |
573 | if (!Writer) { |
574 | if (Diagnostics) |
575 | Diagnostics->Report(diag::err_module_format_unhandled) << Format; |
576 | llvm::report_fatal_error(reason: "unknown module format" ); |
577 | } |
578 | return *Writer; |
579 | } |
580 | |
581 | /// Return the appropriate PCHContainerReader depending on the |
582 | /// current CodeGenOptions. |
583 | const PCHContainerReader &getPCHContainerReader() const { |
584 | assert(Invocation && "cannot determine module format without invocation" ); |
585 | StringRef Format = getHeaderSearchOpts().ModuleFormat; |
586 | auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format); |
587 | if (!Reader) { |
588 | if (Diagnostics) |
589 | Diagnostics->Report(diag::err_module_format_unhandled) << Format; |
590 | llvm::report_fatal_error(reason: "unknown module format" ); |
591 | } |
592 | return *Reader; |
593 | } |
594 | |
595 | /// @} |
596 | /// @name Code Completion |
597 | /// @{ |
598 | |
599 | bool hasCodeCompletionConsumer() const { return (bool)CompletionConsumer; } |
600 | |
601 | CodeCompleteConsumer &getCodeCompletionConsumer() const { |
602 | assert(CompletionConsumer && |
603 | "Compiler instance has no code completion consumer!" ); |
604 | return *CompletionConsumer; |
605 | } |
606 | |
607 | /// setCodeCompletionConsumer - Replace the current code completion consumer; |
608 | /// the compiler instance takes ownership of \p Value. |
609 | void setCodeCompletionConsumer(CodeCompleteConsumer *Value); |
610 | |
611 | /// @} |
612 | /// @name Frontend timer |
613 | /// @{ |
614 | |
615 | bool hasFrontendTimer() const { return (bool)FrontendTimer; } |
616 | |
617 | llvm::Timer &getFrontendTimer() const { |
618 | assert(FrontendTimer && "Compiler instance has no frontend timer!" ); |
619 | return *FrontendTimer; |
620 | } |
621 | |
622 | /// @} |
623 | /// @name Output Files |
624 | /// @{ |
625 | |
626 | /// clearOutputFiles - Clear the output file list. The underlying output |
627 | /// streams must have been closed beforehand. |
628 | /// |
629 | /// \param EraseFiles - If true, attempt to erase the files from disk. |
630 | void clearOutputFiles(bool EraseFiles); |
631 | |
632 | /// @} |
633 | /// @name Construction Utility Methods |
634 | /// @{ |
635 | |
636 | /// Create the diagnostics engine using the invocation's diagnostic options |
637 | /// and replace any existing one with it. |
638 | /// |
639 | /// Note that this routine also replaces the diagnostic client, |
640 | /// allocating one if one is not provided. |
641 | /// |
642 | /// \param Client If non-NULL, a diagnostic client that will be |
643 | /// attached to (and, then, owned by) the DiagnosticsEngine inside this AST |
644 | /// unit. |
645 | /// |
646 | /// \param ShouldOwnClient If Client is non-NULL, specifies whether |
647 | /// the diagnostic object should take ownership of the client. |
648 | void createDiagnostics(DiagnosticConsumer *Client = nullptr, |
649 | bool ShouldOwnClient = true); |
650 | |
651 | /// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter. |
652 | /// |
653 | /// If no diagnostic client is provided, this creates a |
654 | /// DiagnosticConsumer that is owned by the returned diagnostic |
655 | /// object, if using directly the caller is responsible for |
656 | /// releasing the returned DiagnosticsEngine's client eventually. |
657 | /// |
658 | /// \param Opts - The diagnostic options; note that the created text |
659 | /// diagnostic object contains a reference to these options. |
660 | /// |
661 | /// \param Client If non-NULL, a diagnostic client that will be |
662 | /// attached to (and, then, owned by) the returned DiagnosticsEngine |
663 | /// object. |
664 | /// |
665 | /// \param CodeGenOpts If non-NULL, the code gen options in use, which may be |
666 | /// used by some diagnostics printers (for logging purposes only). |
667 | /// |
668 | /// \return The new object on success, or null on failure. |
669 | static IntrusiveRefCntPtr<DiagnosticsEngine> |
670 | createDiagnostics(DiagnosticOptions *Opts, |
671 | DiagnosticConsumer *Client = nullptr, |
672 | bool ShouldOwnClient = true, |
673 | const CodeGenOptions *CodeGenOpts = nullptr); |
674 | |
675 | /// Create the file manager and replace any existing one with it. |
676 | /// |
677 | /// \return The new file manager on success, or null on failure. |
678 | FileManager * |
679 | createFileManager(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr); |
680 | |
681 | /// Create the source manager and replace any existing one with it. |
682 | void createSourceManager(FileManager &FileMgr); |
683 | |
684 | /// Create the preprocessor, using the invocation, file, and source managers, |
685 | /// and replace any existing one with it. |
686 | void createPreprocessor(TranslationUnitKind TUKind); |
687 | |
688 | std::string getSpecificModuleCachePath(StringRef ModuleHash); |
689 | std::string getSpecificModuleCachePath() { |
690 | return getSpecificModuleCachePath(ModuleHash: getInvocation().getModuleHash()); |
691 | } |
692 | |
693 | /// Create the AST context. |
694 | void createASTContext(); |
695 | |
696 | /// Create an external AST source to read a PCH file and attach it to the AST |
697 | /// context. |
698 | void createPCHExternalASTSource( |
699 | StringRef Path, DisableValidationForModuleKind DisableValidation, |
700 | bool AllowPCHWithCompilerErrors, void *DeserializationListener, |
701 | bool OwnDeserializationListener); |
702 | |
703 | /// Create an external AST source to read a PCH file. |
704 | /// |
705 | /// \return - The new object on success, or null on failure. |
706 | static IntrusiveRefCntPtr<ASTReader> createPCHExternalASTSource( |
707 | StringRef Path, StringRef Sysroot, |
708 | DisableValidationForModuleKind DisableValidation, |
709 | bool AllowPCHWithCompilerErrors, Preprocessor &PP, |
710 | InMemoryModuleCache &ModuleCache, ASTContext &Context, |
711 | const PCHContainerReader &PCHContainerRdr, |
712 | ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, |
713 | ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors, |
714 | void *DeserializationListener, bool OwnDeserializationListener, |
715 | bool Preamble, bool UseGlobalModuleIndex); |
716 | |
717 | /// Create a code completion consumer using the invocation; note that this |
718 | /// will cause the source manager to truncate the input source file at the |
719 | /// completion point. |
720 | void createCodeCompletionConsumer(); |
721 | |
722 | /// Create a code completion consumer to print code completion results, at |
723 | /// \p Filename, \p Line, and \p Column, to the given output stream \p OS. |
724 | static CodeCompleteConsumer *createCodeCompletionConsumer( |
725 | Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column, |
726 | const CodeCompleteOptions &Opts, raw_ostream &OS); |
727 | |
728 | /// Create the Sema object to be used for parsing. |
729 | void createSema(TranslationUnitKind TUKind, |
730 | CodeCompleteConsumer *CompletionConsumer); |
731 | |
732 | /// Create the frontend timer and replace any existing one with it. |
733 | void createFrontendTimer(); |
734 | |
735 | /// Create the default output file (from the invocation's options) and add it |
736 | /// to the list of tracked output files. |
737 | /// |
738 | /// The files created by this are usually removed on signal, and, depending |
739 | /// on FrontendOptions, may also use a temporary file (that is, the data is |
740 | /// written to a temporary file which will atomically replace the target |
741 | /// output on success). |
742 | /// |
743 | /// \return - Null on error. |
744 | std::unique_ptr<raw_pwrite_stream> createDefaultOutputFile( |
745 | bool Binary = true, StringRef BaseInput = "" , StringRef Extension = "" , |
746 | bool RemoveFileOnSignal = true, bool CreateMissingDirectories = false, |
747 | bool ForceUseTemporary = false); |
748 | |
749 | /// Create a new output file, optionally deriving the output path name, and |
750 | /// add it to the list of tracked output files. |
751 | /// |
752 | /// \return - Null on error. |
753 | std::unique_ptr<raw_pwrite_stream> |
754 | createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, |
755 | bool UseTemporary, bool CreateMissingDirectories = false); |
756 | |
757 | private: |
758 | /// Create a new output file and add it to the list of tracked output files. |
759 | /// |
760 | /// If \p OutputPath is empty, then createOutputFile will derive an output |
761 | /// path location as \p BaseInput, with any suffix removed, and \p Extension |
762 | /// appended. If \p OutputPath is not stdout and \p UseTemporary |
763 | /// is true, createOutputFile will create a new temporary file that must be |
764 | /// renamed to \p OutputPath in the end. |
765 | /// |
766 | /// \param OutputPath - If given, the path to the output file. |
767 | /// \param Binary - The mode to open the file in. |
768 | /// \param RemoveFileOnSignal - Whether the file should be registered with |
769 | /// llvm::sys::RemoveFileOnSignal. Note that this is not safe for |
770 | /// multithreaded use, as the underlying signal mechanism is not reentrant |
771 | /// \param UseTemporary - Create a new temporary file that must be renamed to |
772 | /// OutputPath in the end. |
773 | /// \param CreateMissingDirectories - When \p UseTemporary is true, create |
774 | /// missing directories in the output path. |
775 | Expected<std::unique_ptr<raw_pwrite_stream>> |
776 | createOutputFileImpl(StringRef OutputPath, bool Binary, |
777 | bool RemoveFileOnSignal, bool UseTemporary, |
778 | bool CreateMissingDirectories); |
779 | |
780 | public: |
781 | std::unique_ptr<raw_pwrite_stream> createNullOutputFile(); |
782 | |
783 | /// @} |
784 | /// @name Initialization Utility Methods |
785 | /// @{ |
786 | |
787 | /// InitializeSourceManager - Initialize the source manager to set InputFile |
788 | /// as the main file. |
789 | /// |
790 | /// \return True on success. |
791 | bool InitializeSourceManager(const FrontendInputFile &Input); |
792 | |
793 | /// InitializeSourceManager - Initialize the source manager to set InputFile |
794 | /// as the main file. |
795 | /// |
796 | /// \return True on success. |
797 | static bool InitializeSourceManager(const FrontendInputFile &Input, |
798 | DiagnosticsEngine &Diags, |
799 | FileManager &FileMgr, |
800 | SourceManager &SourceMgr); |
801 | |
802 | /// @} |
803 | |
804 | void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> OutStream) { |
805 | OutputStream = std::move(OutStream); |
806 | } |
807 | |
808 | std::unique_ptr<llvm::raw_pwrite_stream> takeOutputStream() { |
809 | return std::move(OutputStream); |
810 | } |
811 | |
812 | void createASTReader(); |
813 | |
814 | bool loadModuleFile(StringRef FileName, |
815 | serialization::ModuleFile *&LoadedModuleFile); |
816 | |
817 | private: |
818 | /// Find a module, potentially compiling it, before reading its AST. This is |
819 | /// the guts of loadModule. |
820 | /// |
821 | /// For prebuilt modules, the Module is not expected to exist in |
822 | /// HeaderSearch's ModuleMap. If a ModuleFile by that name is in the |
823 | /// ModuleManager, then it will be loaded and looked up. |
824 | /// |
825 | /// For implicit modules, the Module is expected to already be in the |
826 | /// ModuleMap. First attempt to load it from the given path on disk. If that |
827 | /// fails, defer to compileModuleAndReadAST, which will first build and then |
828 | /// load it. |
829 | ModuleLoadResult findOrCompileModuleAndReadAST(StringRef ModuleName, |
830 | SourceLocation ImportLoc, |
831 | SourceLocation ModuleNameLoc, |
832 | bool IsInclusionDirective); |
833 | |
834 | public: |
835 | ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, |
836 | Module::NameVisibilityKind Visibility, |
837 | bool IsInclusionDirective) override; |
838 | |
839 | void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, |
840 | StringRef Source) override; |
841 | |
842 | void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, |
843 | SourceLocation ImportLoc) override; |
844 | |
845 | bool hadModuleLoaderFatalFailure() const { |
846 | return ModuleLoader::HadFatalFailure; |
847 | } |
848 | |
849 | GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override; |
850 | |
851 | bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override; |
852 | |
853 | void addDependencyCollector(std::shared_ptr<DependencyCollector> Listener) { |
854 | DependencyCollectors.push_back(x: std::move(Listener)); |
855 | } |
856 | |
857 | void setExternalSemaSource(IntrusiveRefCntPtr<ExternalSemaSource> ESS); |
858 | |
859 | InMemoryModuleCache &getModuleCache() const { return *ModuleCache; } |
860 | }; |
861 | |
862 | } // end namespace clang |
863 | |
864 | #endif |
865 | |