clang: include/clang/Frontend/CompilerInstance.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9#ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
10#define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
11
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 "llvm/Support/VirtualFileSystem.h"
28#include
29#include
30#include
31#include
32#include
33#include
34
35namespace llvm {
36class raw_fd_ostream;
37class Timer;
38class TimerGroup;
39}
40
42class ASTContext;
43class ASTReader;
44
45namespace serialization {
47}
48
49class CodeCompleteConsumer;
50class DiagnosticsEngine;
51class DiagnosticConsumer;
52class FileManager;
53class FrontendAction;
54class InMemoryModuleCache;
56class Preprocessor;
57class Sema;
58class SourceManager;
59class TargetInfo;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
81
82 std::shared_ptr Invocation;
83
84
86
87
89
90
92
93
95
96
98
99
101
102
103 std::shared_ptr PP;
104
105
107
108
110
111
112 std::unique_ptr Consumer;
113
114
115 std::unique_ptr CompletionConsumer;
116
117
118 std::unique_ptr TheSema;
119
120
121 std::unique_ptrllvm::TimerGroup timerGroup;
122
123
124 std::unique_ptrllvm::Timer FrontendTimer;
125
126
128
129
130 std::shared_ptr ModuleDepCollector;
131
132
133 std::shared_ptr ThePCHContainerOperations;
134
135 std::vector<std::shared_ptr> DependencyCollectors;
136
137
138 class FailedModulesSet {
139 llvm::StringSet<> Failed;
140
141 public:
142 bool hasAlreadyFailed(StringRef module) { return Failed.count(module) > 0; }
143
144 void addFailed(StringRef module) { Failed.insert(module); }
145 };
146
147
148
149
150
151
152
153 std::shared_ptr FailedModules;
154
155
156
157 std::map<std::string, std::string, std::less<>> BuiltModules;
158
159
160 bool DeleteBuiltModules = true;
161
162
163
165
166
167
169
170
171
172 bool BuildGlobalModuleIndex = false;
173
174
175 bool HaveFullGlobalModuleIndex = false;
176
177
178 bool DisableGeneratingGlobalModuleIndex = false;
179
180
181 std::unique_ptr<raw_ostream> OwnedVerboseOutputStream;
182
183
184 raw_ostream *VerboseOutputStream = &llvm::errs();
185
186
187
188
189
190
191 struct OutputFile {
193 std::optionalllvm::sys::fs::TempFile File;
194
195 OutputFile(std::string filename,
196 std::optionalllvm::sys::fs::TempFile file)
197 : Filename(std::move(filename)), File(std::move(file)) {}
198 };
199
200
201 std::list OutputFiles;
202
203
204 std::unique_ptrllvm::raw\_pwrite\_stream OutputStream;
205
208public:
210 std::shared_ptr PCHContainerOps =
211 std::make_shared(),
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
246
247
249
250
252
253
254
255
256
258
260 assert(Invocation && "Compiler instance has no invocation!");
261 return *Invocation;
262 }
263
264 std::shared_ptr getInvocationPtr() { return Invocation; }
265
266
268
269
271
272
273
275 BuildGlobalModuleIndex = Build;
276 }
277
278
279
280
281
283
285 return Invocation->getCodeGenOpts();
286 }
288 return Invocation->getCodeGenOpts();
289 }
290
292 return Invocation->getDependencyOutputOpts();
293 }
295 return Invocation->getDependencyOutputOpts();
296 }
297
299 return Invocation->getDiagnosticOpts();
300 }
302 return Invocation->getDiagnosticOpts();
303 }
304
306 return Invocation->getFileSystemOpts();
307 }
309 return Invocation->getFileSystemOpts();
310 }
311
313 return Invocation->getFrontendOpts();
314 }
316 return Invocation->getFrontendOpts();
317 }
318
320 return Invocation->getHeaderSearchOpts();
321 }
323 return Invocation->getHeaderSearchOpts();
324 }
326 return Invocation->getHeaderSearchOptsPtr();
327 }
328
331 return Invocation->getAPINotesOpts();
332 }
333
337 return Invocation->getLangOptsPtr();
338 }
339
341 return Invocation->getPreprocessorOpts();
342 }
344 return Invocation->getPreprocessorOpts();
345 }
346
348 return Invocation->getPreprocessorOutputOpts();
349 }
351 return Invocation->getPreprocessorOutputOpts();
352 }
353
355 return Invocation->getTargetOpts();
356 }
358 return Invocation->getTargetOpts();
359 }
360
361
362
363
364
366
367
369 assert(Diagnostics && "Compiler instance has no diagnostics!");
370 return *Diagnostics;
371 }
372
374 assert(Diagnostics && "Compiler instance has no diagnostics!");
375 return Diagnostics;
376 }
377
378
380
382 assert(Diagnostics && Diagnostics->getClient() &&
383 "Compiler instance has no diagnostic client!");
384 return *Diagnostics->getClient();
385 }
386
387
388
389
390
391
393
394
396
397
399 return *VerboseOutputStream;
400 }
401
402
403
404
405
407
409 assert(Target && "Compiler instance has no target!");
411 }
412
414 assert(Target && "Compiler instance has no target!");
416 }
417
418
420
421
422
423
424
426
427
429
430
432
433
434
435
436
438
439
440
441
442
444
445
447 assert(FileMgr && "Compiler instance has no file manager!");
448 return *FileMgr;
449 }
450
452 assert(FileMgr && "Compiler instance has no file manager!");
453 return FileMgr;
454 }
455
457 llvm::BuryPointer(FileMgr.get());
458 FileMgr.resetWithoutRelease();
459 }
460
461
463
464
465
466
467
469
470
472 assert(SourceMgr && "Compiler instance has no source manager!");
473 return *SourceMgr;
474 }
475
477 assert(SourceMgr && "Compiler instance has no source manager!");
478 return SourceMgr;
479 }
480
482 llvm::BuryPointer(SourceMgr.get());
483 SourceMgr.resetWithoutRelease();
484 }
485
486
488
489
490
491
492
494
495
497 assert(PP && "Compiler instance has no preprocessor!");
498 return *PP;
499 }
500
502
504 llvm::BuryPointer(new std::shared_ptr(PP));
505 }
506
507
509
510
511
512
513
515
517 assert(Context && "Compiler instance has no AST context!");
518 return *Context;
519 }
520
522 assert(Context && "Compiler instance has no AST context!");
523 return Context;
524 }
525
527 llvm::BuryPointer(Context.get());
528 Context.resetWithoutRelease();
529 }
530
531
533
534
535
537
538
539
540
541
543
545 assert(Consumer && "Compiler instance has no AST consumer!");
546 return *Consumer;
547 }
548
549
550
551 std::unique_ptr takeASTConsumer() { return std::move(Consumer); }
552
553
554
556
557
558
559
560 bool hasSema() const { return (bool)TheSema; }
561
563 assert(TheSema && "Compiler instance has no Sema object!");
564 return *TheSema;
565 }
566
567 std::unique_ptr takeSema();
569
570
571
572
573
576
579 std::shared_ptr Collector);
580
582 return ThePCHContainerOperations;
583 }
584
585
586
588 assert(Invocation && "cannot determine module format without invocation");
590 auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format);
591 if (!Writer) {
592 if (Diagnostics)
593 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
594 llvm::report_fatal_error("unknown module format");
595 }
596 return *Writer;
597 }
598
599
600
602 assert(Invocation && "cannot determine module format without invocation");
604 auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format);
605 if (!Reader) {
606 if (Diagnostics)
607 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
608 llvm::report_fatal_error("unknown module format");
609 }
610 return *Reader;
611 }
612
613
614
615
616
618
620 assert(CompletionConsumer &&
621 "Compiler instance has no code completion consumer!");
622 return *CompletionConsumer;
623 }
624
625
626
628
629
630
631
632
633 llvm::TimerGroup &getTimerGroup() const { return *timerGroup; }
634
636 assert(FrontendTimer && "Compiler instance has no frontend timer!");
637 return *FrontendTimer;
638 }
639
640
641
642
643
645
647 FailedModules = std::make_shared();
648 }
649
651 return FailedModules;
652 }
653
655 FailedModules = FMS;
656 }
657
658
659
660
661
662
663
664
665
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
689 bool ShouldOwnClient = true);
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
712 bool ShouldOwnClient = true,
714
715
716
717
720
721
723
724
725
727
731 }
732
733
735
736
737
740 bool AllowPCHWithCompilerErrors, void *DeserializationListener,
741 bool OwnDeserializationListener);
742
743
744
745
747 StringRef Path, StringRef Sysroot,
749 bool AllowPCHWithCompilerErrors, Preprocessor &PP,
752 ArrayRef<std::shared_ptr> Extensions,
753 ArrayRef<std::shared_ptr> DependencyCollectors,
754 void *DeserializationListener, bool OwnDeserializationListener,
755 bool Preamble, bool UseGlobalModuleIndex);
756
757
758
759
761
762
763
767
768
771
772
774
775
776
777
778
779
780
781
782
783
785 bool Binary = true, StringRef BaseInput = "", StringRef Extension = "",
786 bool RemoveFileOnSignal = true, bool CreateMissingDirectories = false,
787 bool ForceUseTemporary = false);
788
789
790
791
792
793 std::unique_ptr<raw_pwrite_stream>
794 createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
795 bool UseTemporary, bool CreateMissingDirectories = false);
796
797private:
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
816 createOutputFileImpl(StringRef OutputPath, bool Binary,
817 bool RemoveFileOnSignal, bool UseTemporary,
818 bool CreateMissingDirectories);
819
820public:
822
823
824
825
826
827
828
829
830
832
833
834
835
836
841
842
843
844 void setOutputStream(std::unique_ptrllvm::raw\_pwrite\_stream OutStream) {
845 OutputStream = std::move(OutStream);
846 }
847
849 return std::move(OutputStream);
850 }
851
853
856
857private:
858
859
860
861
862
863
864
865
866
867
868
869 ModuleLoadResult findOrCompileModuleAndReadAST(StringRef ModuleName,
872 bool IsInclusionDirective);
873
874public:
877 bool IsInclusionDirective) override;
878
880 StringRef Source) override;
881
884
887 }
888
890
892
894 DependencyCollectors.push_back(std::move(Listener));
895 }
896
898
900};
901
902}
903
904#endif
Defines the Diagnostic-related interfaces.
llvm::MachO::Target Target
Defines the SourceManager interface.
Tracks various options which control how API notes are found and handled.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Stores options for the analyzer from the command line.
Abstract interface for a consumer of code-completion information.
Options controlling the behavior of code completion.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
void createPCHExternalASTSource(StringRef Path, DisableValidationForModuleKind DisableValidation, bool AllowPCHWithCompilerErrors, void *DeserializationListener, bool OwnDeserializationListener)
Create an external AST source to read a PCH file and attach it to the AST context.
DiagnosticConsumer & getDiagnosticClient() const
~CompilerInstance() override
void createPreprocessor(TranslationUnitKind TUKind)
Create the preprocessor, using the invocation, file, and source managers, and replace any existing on...
AnalyzerOptions & getAnalyzerOpts()
bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Check global module index for missing imports.
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
FileManager * createFileManager(IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Create the file manager and replace any existing one with it.
const HeaderSearchOptions & getHeaderSearchOpts() const
DependencyOutputOptions & getDependencyOutputOpts()
bool hasFileManager() const
const DependencyOutputOptions & getDependencyOutputOpts() const
std::shared_ptr< Preprocessor > getPreprocessorPtr()
TargetInfo * getAuxTarget() const
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
GlobalModuleIndex * loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Load, create, or return global module.
raw_ostream & getVerboseOutputStream()
Get the current stream for verbose output.
llvm::TimerGroup & getTimerGroup() const
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false)
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
void setExternalSemaSource(IntrusiveRefCntPtr< ExternalSemaSource > ESS)
void createDiagnostics(llvm::vfs::FileSystem &VFS, DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
const DiagnosticOptions & getDiagnosticOpts() const
std::string getSpecificModuleCachePath()
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
std::shared_ptr< FailedModulesSet > getFailedModulesSetPtr() const
const CodeGenOptions & getCodeGenOpts() const
FileSystemOptions & getFileSystemOpts()
llvm::Timer & getFrontendTimer() const
const TargetOptions & getTargetOpts() const
bool InitializeSourceManager(const FrontendInputFile &Input)
InitializeSourceManager - Initialize the source manager to set InputFile as the main file.
void setOutputStream(std::unique_ptr< llvm::raw_pwrite_stream > OutStream)
FileManager & getFileManager() const
Return the current file manager to the caller.
void setBuildGlobalModuleIndex(bool Build)
Set the flag indicating whether we should (re)build the global module index.
IntrusiveRefCntPtr< DiagnosticsEngine > getDiagnosticsPtr() const
std::unique_ptr< Sema > takeSema()
const PreprocessorOptions & getPreprocessorOpts() const
void printDiagnosticStats()
At the end of a compilation, print the number of warnings/errors.
void resetAndLeakSourceManager()
void setASTConsumer(std::unique_ptr< ASTConsumer > Value)
setASTConsumer - Replace the current AST consumer; the compiler instance takes ownership of Value.
PreprocessorOutputOptions & getPreprocessorOutputOpts()
IntrusiveRefCntPtr< FileManager > getFileManagerPtr() const
IntrusiveRefCntPtr< ASTContext > getASTContextPtr() const
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
IntrusiveRefCntPtr< ASTReader > getASTReader() const
void setTarget(TargetInfo *Value)
Replace the current Target.
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
std::shared_ptr< LangOptions > getLangOptsPtr() const
InMemoryModuleCache & getModuleCache() const
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
void createASTContext()
Create the AST context.
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, bool UseTemporary, bool CreateMissingDirectories=false)
Create a new output file, optionally deriving the output path name, and add it to the list of tracked...
bool hasASTContext() const
void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, StringRef Source) override
Attempt to create the given module from the specified source buffer.
void resetAndLeakFileManager()
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
void setASTContext(ASTContext *Value)
setASTContext - Replace the current AST context.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
ASTContext & getASTContext() const
void setFailedModulesSet(std::shared_ptr< FailedModulesSet > FMS)
void LoadRequestedPlugins()
Load the list of plugins requested in the FrontendOptions.
TargetOptions & getTargetOpts()
IntrusiveRefCntPtr< TargetInfo > getTargetPtr() const
void setASTReader(IntrusiveRefCntPtr< ASTReader > Reader)
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
bool hasInvocation() const
FrontendOptions & getFrontendOpts()
const FrontendOptions & getFrontendOpts() const
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
std::shared_ptr< CompilerInvocation > getInvocationPtr()
bool hasDiagnostics() const
bool hadModuleLoaderFatalFailure() const
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
HeaderSearchOptions & getHeaderSearchOpts()
void createFrontendTimer()
Create the frontend timer and replace any existing one with it.
void setDiagnostics(DiagnosticsEngine *Value)
setDiagnostics - Replace the current diagnostics engine.
bool hasCodeCompletionConsumer() const
void resetAndLeakASTContext()
const LangOptions & getLangOpts() const
bool hasFailedModulesSet() const
CompilerInvocation & getInvocation()
const PCHContainerWriter & getPCHContainerWriter() const
Return the appropriate PCHContainerWriter depending on the current CodeGenOptions.
void setVerboseOutputStream(raw_ostream &Value)
Replace the current stream for verbose output.
void resetAndLeakPreprocessor()
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
PreprocessorOptions & getPreprocessorOpts()
ASTConsumer & getASTConsumer() const
TargetInfo & getTarget() const
llvm::vfs::FileSystem & getVirtualFileSystem() const
const FileSystemOptions & getFileSystemOpts() const
void createCodeCompletionConsumer()
Create a code completion consumer using the invocation; note that this will cause the source manager ...
void setCodeCompletionConsumer(CodeCompleteConsumer *Value)
setCodeCompletionConsumer - Replace the current code completion consumer; the compiler instance takes...
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
void clearOutputFiles(bool EraseFiles)
clearOutputFiles - Clear the output file list.
DiagnosticOptions & getDiagnosticOpts()
LangOptions & getLangOpts()
const APINotesOptions & getAPINotesOpts() const
CodeGenOptions & getCodeGenOpts()
SourceManager & getSourceManager() const
Return the current source manager.
IntrusiveRefCntPtr< SourceManager > getSourceManagerPtr() const
CodeCompleteConsumer & getCodeCompletionConsumer() const
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
bool hasSourceManager() const
bool hasASTConsumer() const
APINotesOptions & getAPINotesOpts()
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
void setAuxTarget(TargetInfo *Value)
Replace the current AuxTarget.
void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc) override
Make the given module visible.
void createFailedModulesSet()
bool loadModuleFile(StringRef FileName, serialization::ModuleFile *&LoadedModuleFile)
bool hasPreprocessor() const
void setPreprocessor(std::shared_ptr< Preprocessor > Value)
Replace the current preprocessor.
void createSema(TranslationUnitKind TUKind, CodeCompleteConsumer *CompletionConsumer)
Create the Sema object to be used for parsing.
Helper class for holding the data necessary to invoke the compiler.
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Options for controlling the compiler diagnostics engine.
Concrete class used by the front-end to report problems and issues.
Implements support for file system lookup, file system caching, and directory search management.
Keeps track of options that affect how file operations are performed.
Abstract base class for actions which can be performed by the frontend.
An input file for the front end.
FrontendOptions - Options for controlling the behavior of the frontend.
A global index for a set of module files, providing information about the identifiers within those mo...
In-memory cache for modules.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Describes the result of attempting to load a module.
Abstract interface for a module loader.
Describes a module or submodule.
NameVisibilityKind
Describes the visibility of the various names within a particular module.
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
This abstract interface provides operations for creating containers for serialized ASTs (precompiled ...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g....
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Sema - This implements semantic analysis and AST building for C.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Exposes information about the current target.
Options for controlling the target.
Information about a module that has been loaded by the ASTReader.
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
TranslationUnitKind
Describes the kind of translation unit being processed.
DisableValidationForModuleKind
Whether to disable the normal validation performed on precompiled headers and module files when they ...
Visibility
Describes the different kinds of visibility that a declaration may have.
Diagnostic wrappers for TextAPI types for error reporting.