clang  19.0.0git
CompilerInstance.h
Go to the documentation of this file.
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"
15 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Frontend/Utils.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;
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.
80  /// The options used in this compiler instance.
81  std::shared_ptr<CompilerInvocation> Invocation;
82 
83  /// The diagnostics engine instance.
85 
86  /// The target being compiled for.
88 
89  /// Auxiliary Target info.
91 
92  /// The file manager.
94 
95  /// The source manager.
97 
98  /// The cache of PCM files.
100 
101  /// The preprocessor.
102  std::shared_ptr<Preprocessor> PP;
103 
104  /// The AST context.
106 
107  /// An optional sema source that will be attached to sema.
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  /// Records the set of modules
137  class FailedModulesSet {
138  llvm::StringSet<> Failed;
139 
140  public:
141  bool hasAlreadyFailed(StringRef module) { return Failed.count(module) > 0; }
142 
143  void addFailed(StringRef module) { Failed.insert(module); }
144  };
145 
146  /// The set of modules that failed to build.
147  ///
148  /// This pointer will be shared among all of the compiler instances created
149  /// to (re)build modules, so that once a module fails to build anywhere,
150  /// other instances will see that the module has failed and won't try to
151  /// build it again.
152  std::shared_ptr<FailedModulesSet> FailedModules;
153 
154  /// The set of top-level modules that has already been built on the
155  /// fly as part of this overall compilation action.
156  std::map<std::string, std::string, std::less<>> BuiltModules;
157 
158  /// Should we delete the BuiltModules when we're done?
159  bool DeleteBuiltModules = true;
160 
161  /// The location of the module-import keyword for the last module
162  /// import.
163  SourceLocation LastModuleImportLoc;
164 
165  /// The result of the last module import.
166  ///
167  ModuleLoadResult LastModuleImportResult;
168 
169  /// Whether we should (re)build the global module index once we
170  /// have finished with this translation unit.
171  bool BuildGlobalModuleIndex = false;
172 
173  /// We have a full global module index, with all modules.
174  bool HaveFullGlobalModuleIndex = false;
175 
176  /// One or more modules failed to build.
177  bool DisableGeneratingGlobalModuleIndex = false;
178 
179  /// The stream for verbose output if owned, otherwise nullptr.
180  std::unique_ptr<raw_ostream> OwnedVerboseOutputStream;
181 
182  /// The stream for verbose output.
183  raw_ostream *VerboseOutputStream = &llvm::errs();
184 
185  /// Holds information about the output file.
186  ///
187  /// If TempFilename is not empty we must rename it to Filename at the end.
188  /// TempFilename may be empty and Filename non-empty if creating the temporary
189  /// failed.
190  struct OutputFile {
191  std::string Filename;
192  std::optional<llvm::sys::fs::TempFile> File;
193 
194  OutputFile(std::string filename,
195  std::optional<llvm::sys::fs::TempFile> file)
196  : Filename(std::move(filename)), File(std::move(file)) {}
197  };
198 
199  /// The list of active output files.
200  std::list<OutputFile> OutputFiles;
201 
202  /// Force an output buffer.
203  std::unique_ptr<llvm::raw_pwrite_stream> OutputStream;
204 
205  CompilerInstance(const CompilerInstance &) = delete;
206  void operator=(const CompilerInstance &) = delete;
207 public:
208  explicit CompilerInstance(
209  std::shared_ptr<PCHContainerOperations> PCHContainerOps =
210  std::make_shared<PCHContainerOperations>(),
211  InMemoryModuleCache *SharedModuleCache = nullptr);
212  ~CompilerInstance() override;
213 
214  /// @name High-Level Operations
215  /// @{
216 
217  /// ExecuteAction - Execute the provided action against the compiler's
218  /// CompilerInvocation object.
219  ///
220  /// This function makes the following assumptions:
221  ///
222  /// - The invocation options should be initialized. This function does not
223  /// handle the '-help' or '-version' options, clients should handle those
224  /// directly.
225  ///
226  /// - The diagnostics engine should have already been created by the client.
227  ///
228  /// - No other CompilerInstance state should have been initialized (this is
229  /// an unchecked error).
230  ///
231  /// - Clients should have initialized any LLVM target features that may be
232  /// required.
233  ///
234  /// - Clients should eventually call llvm_shutdown() upon the completion of
235  /// this routine to ensure that any managed objects are properly destroyed.
236  ///
237  /// Note that this routine may write output to 'stderr'.
238  ///
239  /// \param Act - The action to execute.
240  /// \return - True on success.
241  //
242  // FIXME: Eliminate the llvm_shutdown requirement, that should either be part
243  // of the context or else not CompilerInstance specific.
244  bool ExecuteAction(FrontendAction &Act);
245 
246  /// At the end of a compilation, print the number of warnings/errors.
247  void printDiagnosticStats();
248 
249  /// Load the list of plugins requested in the \c FrontendOptions.
250  void LoadRequestedPlugins();
251 
252  /// @}
253  /// @name Compiler Invocation and Options
254  /// @{
255 
256  bool hasInvocation() const { return Invocation != nullptr; }
257 
259  assert(Invocation && "Compiler instance has no invocation!");
260  return *Invocation;
261  }
262 
263  std::shared_ptr<CompilerInvocation> getInvocationPtr() { return Invocation; }
264 
265  /// setInvocation - Replace the current invocation.
266  void setInvocation(std::shared_ptr<CompilerInvocation> Value);
267 
268  /// Indicates whether we should (re)build the global module index.
269  bool shouldBuildGlobalModuleIndex() const;
270 
271  /// Set the flag indicating whether we should (re)build the global
272  /// module index.
273  void setBuildGlobalModuleIndex(bool Build) {
274  BuildGlobalModuleIndex = Build;
275  }
276 
277  /// @}
278  /// @name Forwarding Methods
279  /// @{
280 
281  AnalyzerOptions &getAnalyzerOpts() { return Invocation->getAnalyzerOpts(); }
282 
284  return Invocation->getCodeGenOpts();
285  }
287  return Invocation->getCodeGenOpts();
288  }
289 
291  return Invocation->getDependencyOutputOpts();
292  }
294  return Invocation->getDependencyOutputOpts();
295  }
296 
298  return Invocation->getDiagnosticOpts();
299  }
301  return Invocation->getDiagnosticOpts();
302  }
303 
305  return Invocation->getFileSystemOpts();
306  }
308  return Invocation->getFileSystemOpts();
309  }
310 
312  return Invocation->getFrontendOpts();
313  }
315  return Invocation->getFrontendOpts();
316  }
317 
319  return Invocation->getHeaderSearchOpts();
320  }
322  return Invocation->getHeaderSearchOpts();
323  }
324  std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
325  return Invocation->getHeaderSearchOptsPtr();
326  }
327 
328  APINotesOptions &getAPINotesOpts() { return Invocation->getAPINotesOpts(); }
330  return Invocation->getAPINotesOpts();
331  }
332 
333  LangOptions &getLangOpts() { return Invocation->getLangOpts(); }
334  const LangOptions &getLangOpts() const { return Invocation->getLangOpts(); }
335  std::shared_ptr<LangOptions> getLangOptsPtr() const {
336  return Invocation->getLangOptsPtr();
337  }
338 
340  return Invocation->getPreprocessorOpts();
341  }
343  return Invocation->getPreprocessorOpts();
344  }
345 
347  return Invocation->getPreprocessorOutputOpts();
348  }
350  return Invocation->getPreprocessorOutputOpts();
351  }
352 
354  return Invocation->getTargetOpts();
355  }
356  const TargetOptions &getTargetOpts() const {
357  return Invocation->getTargetOpts();
358  }
359 
360  /// @}
361  /// @name Diagnostics Engine
362  /// @{
363 
364  bool hasDiagnostics() const { return Diagnostics != nullptr; }
365 
366  /// Get the current diagnostics engine.
368  assert(Diagnostics && "Compiler instance has no diagnostics!");
369  return *Diagnostics;
370  }
371 
373  assert(Diagnostics && "Compiler instance has no diagnostics!");
374  return Diagnostics;
375  }
376 
377  /// setDiagnostics - Replace the current diagnostics engine.
379 
381  assert(Diagnostics && Diagnostics->getClient() &&
382  "Compiler instance has no diagnostic client!");
383  return *Diagnostics->getClient();
384  }
385 
386  /// @}
387  /// @name VerboseOutputStream
388  /// @{
389 
390  /// Replace the current stream for verbose output.
391  void setVerboseOutputStream(raw_ostream &Value);
392 
393  /// Replace the current stream for verbose output.
394  void setVerboseOutputStream(std::unique_ptr<raw_ostream> Value);
395 
396  /// Get the current stream for verbose output.
397  raw_ostream &getVerboseOutputStream() {
398  return *VerboseOutputStream;
399  }
400 
401  /// @}
402  /// @name Target Info
403  /// @{
404 
405  bool hasTarget() const { return Target != nullptr; }
406 
408  assert(Target && "Compiler instance has no target!");
409  return *Target;
410  }
411 
413  assert(Target && "Compiler instance has no target!");
414  return Target;
415  }
416 
417  /// Replace the current Target.
418  void setTarget(TargetInfo *Value);
419 
420  /// @}
421  /// @name AuxTarget Info
422  /// @{
423 
424  TargetInfo *getAuxTarget() const { return AuxTarget.get(); }
425 
426  /// Replace the current AuxTarget.
428 
429  // Create Target and AuxTarget based on current options
430  bool createTarget();
431 
432  /// @}
433  /// @name Virtual File System
434  /// @{
435 
436  llvm::vfs::FileSystem &getVirtualFileSystem() const;
437 
438  /// @}
439  /// @name File Manager
440  /// @{
441 
442  bool hasFileManager() const { return FileMgr != nullptr; }
443 
444  /// Return the current file manager to the caller.
446  assert(FileMgr && "Compiler instance has no file manager!");
447  return *FileMgr;
448  }
449 
451  assert(FileMgr && "Compiler instance has no file manager!");
452  return FileMgr;
453  }
454 
456  llvm::BuryPointer(FileMgr.get());
457  FileMgr.resetWithoutRelease();
458  }
459 
460  /// Replace the current file manager and virtual file system.
462 
463  /// @}
464  /// @name Source Manager
465  /// @{
466 
467  bool hasSourceManager() const { return SourceMgr != nullptr; }
468 
469  /// Return the current source manager.
471  assert(SourceMgr && "Compiler instance has no source manager!");
472  return *SourceMgr;
473  }
474 
476  assert(SourceMgr && "Compiler instance has no source manager!");
477  return SourceMgr;
478  }
479 
481  llvm::BuryPointer(SourceMgr.get());
482  SourceMgr.resetWithoutRelease();
483  }
484 
485  /// setSourceManager - Replace the current source manager.
487 
488  /// @}
489  /// @name Preprocessor
490  /// @{
491 
492  bool hasPreprocessor() const { return PP != nullptr; }
493 
494  /// Return the current preprocessor.
496  assert(PP && "Compiler instance has no preprocessor!");
497  return *PP;
498  }
499 
500  std::shared_ptr<Preprocessor> getPreprocessorPtr() { return PP; }
501 
503  llvm::BuryPointer(new std::shared_ptr<Preprocessor>(PP));
504  }
505 
506  /// Replace the current preprocessor.
507  void setPreprocessor(std::shared_ptr<Preprocessor> Value);
508 
509  /// @}
510  /// @name ASTContext
511  /// @{
512 
513  bool hasASTContext() const { return Context != nullptr; }
514 
516  assert(Context && "Compiler instance has no AST context!");
517  return *Context;
518  }
519 
521  assert(Context && "Compiler instance has no AST context!");
522  return Context;
523  }
524 
526  llvm::BuryPointer(Context.get());
527  Context.resetWithoutRelease();
528  }
529 
530  /// setASTContext - Replace the current AST context.
532 
533  /// Replace the current Sema; the compiler instance takes ownership
534  /// of S.
535  void setSema(Sema *S);
536 
537  /// @}
538  /// @name ASTConsumer
539  /// @{
540 
541  bool hasASTConsumer() const { return (bool)Consumer; }
542 
544  assert(Consumer && "Compiler instance has no AST consumer!");
545  return *Consumer;
546  }
547 
548  /// takeASTConsumer - Remove the current AST consumer and give ownership to
549  /// the caller.
550  std::unique_ptr<ASTConsumer> takeASTConsumer() { return std::move(Consumer); }
551 
552  /// setASTConsumer - Replace the current AST consumer; the compiler instance
553  /// takes ownership of \p Value.
554  void setASTConsumer(std::unique_ptr<ASTConsumer> Value);
555 
556  /// @}
557  /// @name Semantic analysis
558  /// @{
559  bool hasSema() const { return (bool)TheSema; }
560 
561  Sema &getSema() const {
562  assert(TheSema && "Compiler instance has no Sema object!");
563  return *TheSema;
564  }
565 
566  std::unique_ptr<Sema> takeSema();
567  void resetAndLeakSema();
568 
569  /// @}
570  /// @name Module Management
571  /// @{
572 
575 
576  std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const;
578  std::shared_ptr<ModuleDependencyCollector> Collector);
579 
580  std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const {
581  return ThePCHContainerOperations;
582  }
583 
584  /// Return the appropriate PCHContainerWriter depending on the
585  /// current CodeGenOptions.
587  assert(Invocation && "cannot determine module format without invocation");
588  StringRef Format = getHeaderSearchOpts().ModuleFormat;
589  auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format);
590  if (!Writer) {
591  if (Diagnostics)
592  Diagnostics->Report(diag::err_module_format_unhandled) << Format;
593  llvm::report_fatal_error("unknown module format");
594  }
595  return *Writer;
596  }
597 
598  /// Return the appropriate PCHContainerReader depending on the
599  /// current CodeGenOptions.
601  assert(Invocation && "cannot determine module format without invocation");
602  StringRef Format = getHeaderSearchOpts().ModuleFormat;
603  auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format);
604  if (!Reader) {
605  if (Diagnostics)
606  Diagnostics->Report(diag::err_module_format_unhandled) << Format;
607  llvm::report_fatal_error("unknown module format");
608  }
609  return *Reader;
610  }
611 
612  /// @}
613  /// @name Code Completion
614  /// @{
615 
616  bool hasCodeCompletionConsumer() const { return (bool)CompletionConsumer; }
617 
619  assert(CompletionConsumer &&
620  "Compiler instance has no code completion consumer!");
621  return *CompletionConsumer;
622  }
623 
624  /// setCodeCompletionConsumer - Replace the current code completion consumer;
625  /// the compiler instance takes ownership of \p Value.
627 
628  /// @}
629  /// @name Frontend timer
630  /// @{
631 
632  bool hasFrontendTimer() const { return (bool)FrontendTimer; }
633 
634  llvm::Timer &getFrontendTimer() const {
635  assert(FrontendTimer && "Compiler instance has no frontend timer!");
636  return *FrontendTimer;
637  }
638 
639  /// @}
640  /// @name Failed modules set
641  /// @{
642 
643  bool hasFailedModulesSet() const { return (bool)FailedModules; }
644 
646  FailedModules = std::make_shared<FailedModulesSet>();
647  }
648 
649  std::shared_ptr<FailedModulesSet> getFailedModulesSetPtr() const {
650  return FailedModules;
651  }
652 
653  void setFailedModulesSet(std::shared_ptr<FailedModulesSet> FMS) {
654  FailedModules = FMS;
655  }
656 
657  /// }
658  /// @name Output Files
659  /// @{
660 
661  /// clearOutputFiles - Clear the output file list. The underlying output
662  /// streams must have been closed beforehand.
663  ///
664  /// \param EraseFiles - If true, attempt to erase the files from disk.
665  void clearOutputFiles(bool EraseFiles);
666 
667  /// @}
668  /// @name Construction Utility Methods
669  /// @{
670 
671  /// Create the diagnostics engine using the invocation's diagnostic options
672  /// and replace any existing one with it.
673  ///
674  /// Note that this routine also replaces the diagnostic client,
675  /// allocating one if one is not provided.
676  ///
677  /// \param Client If non-NULL, a diagnostic client that will be
678  /// attached to (and, then, owned by) the DiagnosticsEngine inside this AST
679  /// unit.
680  ///
681  /// \param ShouldOwnClient If Client is non-NULL, specifies whether
682  /// the diagnostic object should take ownership of the client.
683  void createDiagnostics(DiagnosticConsumer *Client = nullptr,
684  bool ShouldOwnClient = true);
685 
686  /// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter.
687  ///
688  /// If no diagnostic client is provided, this creates a
689  /// DiagnosticConsumer that is owned by the returned diagnostic
690  /// object, if using directly the caller is responsible for
691  /// releasing the returned DiagnosticsEngine's client eventually.
692  ///
693  /// \param Opts - The diagnostic options; note that the created text
694  /// diagnostic object contains a reference to these options.
695  ///
696  /// \param Client If non-NULL, a diagnostic client that will be
697  /// attached to (and, then, owned by) the returned DiagnosticsEngine
698  /// object.
699  ///
700  /// \param CodeGenOpts If non-NULL, the code gen options in use, which may be
701  /// used by some diagnostics printers (for logging purposes only).
702  ///
703  /// \return The new object on success, or null on failure.
706  DiagnosticConsumer *Client = nullptr,
707  bool ShouldOwnClient = true,
708  const CodeGenOptions *CodeGenOpts = nullptr);
709 
710  /// Create the file manager and replace any existing one with it.
711  ///
712  /// \return The new file manager on success, or null on failure.
713  FileManager *
715 
716  /// Create the source manager and replace any existing one with it.
717  void createSourceManager(FileManager &FileMgr);
718 
719  /// Create the preprocessor, using the invocation, file, and source managers,
720  /// and replace any existing one with it.
722 
723  std::string getSpecificModuleCachePath(StringRef ModuleHash);
725  return getSpecificModuleCachePath(getInvocation().getModuleHash());
726  }
727 
728  /// Create the AST context.
729  void createASTContext();
730 
731  /// Create an external AST source to read a PCH file and attach it to the AST
732  /// context.
734  StringRef Path, DisableValidationForModuleKind DisableValidation,
735  bool AllowPCHWithCompilerErrors, void *DeserializationListener,
736  bool OwnDeserializationListener);
737 
738  /// Create an external AST source to read a PCH file.
739  ///
740  /// \return - The new object on success, or null on failure.
742  StringRef Path, StringRef Sysroot,
743  DisableValidationForModuleKind DisableValidation,
744  bool AllowPCHWithCompilerErrors, Preprocessor &PP,
745  InMemoryModuleCache &ModuleCache, ASTContext &Context,
746  const PCHContainerReader &PCHContainerRdr,
747  ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
748  ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
749  void *DeserializationListener, bool OwnDeserializationListener,
750  bool Preamble, bool UseGlobalModuleIndex);
751 
752  /// Create a code completion consumer using the invocation; note that this
753  /// will cause the source manager to truncate the input source file at the
754  /// completion point.
756 
757  /// Create a code completion consumer to print code completion results, at
758  /// \p Filename, \p Line, and \p Column, to the given output stream \p OS.
760  Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column,
761  const CodeCompleteOptions &Opts, raw_ostream &OS);
762 
763  /// Create the Sema object to be used for parsing.
764  void createSema(TranslationUnitKind TUKind,
765  CodeCompleteConsumer *CompletionConsumer);
766 
767  /// Create the frontend timer and replace any existing one with it.
768  void createFrontendTimer();
769 
770  /// Create the default output file (from the invocation's options) and add it
771  /// to the list of tracked output files.
772  ///
773  /// The files created by this are usually removed on signal, and, depending
774  /// on FrontendOptions, may also use a temporary file (that is, the data is
775  /// written to a temporary file which will atomically replace the target
776  /// output on success).
777  ///
778  /// \return - Null on error.
779  std::unique_ptr<raw_pwrite_stream> createDefaultOutputFile(
780  bool Binary = true, StringRef BaseInput = "", StringRef Extension = "",
781  bool RemoveFileOnSignal = true, bool CreateMissingDirectories = false,
782  bool ForceUseTemporary = false);
783 
784  /// Create a new output file, optionally deriving the output path name, and
785  /// add it to the list of tracked output files.
786  ///
787  /// \return - Null on error.
788  std::unique_ptr<raw_pwrite_stream>
789  createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
790  bool UseTemporary, bool CreateMissingDirectories = false);
791 
792 private:
793  /// Create a new output file and add it to the list of tracked output files.
794  ///
795  /// If \p OutputPath is empty, then createOutputFile will derive an output
796  /// path location as \p BaseInput, with any suffix removed, and \p Extension
797  /// appended. If \p OutputPath is not stdout and \p UseTemporary
798  /// is true, createOutputFile will create a new temporary file that must be
799  /// renamed to \p OutputPath in the end.
800  ///
801  /// \param OutputPath - If given, the path to the output file.
802  /// \param Binary - The mode to open the file in.
803  /// \param RemoveFileOnSignal - Whether the file should be registered with
804  /// llvm::sys::RemoveFileOnSignal. Note that this is not safe for
805  /// multithreaded use, as the underlying signal mechanism is not reentrant
806  /// \param UseTemporary - Create a new temporary file that must be renamed to
807  /// OutputPath in the end.
808  /// \param CreateMissingDirectories - When \p UseTemporary is true, create
809  /// missing directories in the output path.
811  createOutputFileImpl(StringRef OutputPath, bool Binary,
812  bool RemoveFileOnSignal, bool UseTemporary,
813  bool CreateMissingDirectories);
814 
815 public:
816  std::unique_ptr<raw_pwrite_stream> createNullOutputFile();
817 
818  /// @}
819  /// @name Initialization Utility Methods
820  /// @{
821 
822  /// InitializeSourceManager - Initialize the source manager to set InputFile
823  /// as the main file.
824  ///
825  /// \return True on success.
826  bool InitializeSourceManager(const FrontendInputFile &Input);
827 
828  /// InitializeSourceManager - Initialize the source manager to set InputFile
829  /// as the main file.
830  ///
831  /// \return True on success.
832  static bool InitializeSourceManager(const FrontendInputFile &Input,
833  DiagnosticsEngine &Diags,
834  FileManager &FileMgr,
835  SourceManager &SourceMgr);
836 
837  /// @}
838 
839  void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> OutStream) {
840  OutputStream = std::move(OutStream);
841  }
842 
843  std::unique_ptr<llvm::raw_pwrite_stream> takeOutputStream() {
844  return std::move(OutputStream);
845  }
846 
847  void createASTReader();
848 
849  bool loadModuleFile(StringRef FileName,
850  serialization::ModuleFile *&LoadedModuleFile);
851 
852 private:
853  /// Find a module, potentially compiling it, before reading its AST. This is
854  /// the guts of loadModule.
855  ///
856  /// For prebuilt modules, the Module is not expected to exist in
857  /// HeaderSearch's ModuleMap. If a ModuleFile by that name is in the
858  /// ModuleManager, then it will be loaded and looked up.
859  ///
860  /// For implicit modules, the Module is expected to already be in the
861  /// ModuleMap. First attempt to load it from the given path on disk. If that
862  /// fails, defer to compileModuleAndReadAST, which will first build and then
863  /// load it.
864  ModuleLoadResult findOrCompileModuleAndReadAST(StringRef ModuleName,
865  SourceLocation ImportLoc,
866  SourceLocation ModuleNameLoc,
867  bool IsInclusionDirective);
868 
869 public:
872  bool IsInclusionDirective) override;
873 
874  void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName,
875  StringRef Source) override;
876 
878  SourceLocation ImportLoc) override;
879 
882  }
883 
885 
886  bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override;
887 
888  void addDependencyCollector(std::shared_ptr<DependencyCollector> Listener) {
889  DependencyCollectors.push_back(std::move(Listener));
890  }
891 
893 
894  InMemoryModuleCache &getModuleCache() const { return *ModuleCache; }
895 };
896 
897 } // end namespace clang
898 
899 #endif
Defines the Diagnostic-related interfaces.
StringRef Filename
Definition: Format.cpp:2976
llvm::MachO::Target Target
Definition: MachO.h:50
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.
Definition: ASTConsumer.h:33
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
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.
CodeGenOptions & getCodeGenOpts()
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.
const HeaderSearchOptions & getHeaderSearchOpts() const
void createPreprocessor(TranslationUnitKind TUKind)
Create the preprocessor, using the invocation, file, and source managers, and replace any existing on...
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.
CompilerInvocation & getInvocation()
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
FileManager * createFileManager(IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Create the file manager and replace any existing one with it.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
std::shared_ptr< FailedModulesSet > getFailedModulesSetPtr() const
const APINotesOptions & getAPINotesOpts() const
const CodeGenOptions & getCodeGenOpts() const
const LangOptions & getLangOpts() const
TargetInfo & getTarget() const
GlobalModuleIndex * loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Load, create, or return global module.
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
IntrusiveRefCntPtr< SourceManager > getSourceManagerPtr() 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)
std::shared_ptr< Preprocessor > getPreprocessorPtr()
std::shared_ptr< LangOptions > getLangOptsPtr() const
std::string getSpecificModuleCachePath()
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
FrontendOptions & getFrontendOpts()
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
PreprocessorOptions & getPreprocessorOpts()
bool InitializeSourceManager(const FrontendInputFile &Input)
InitializeSourceManager - Initialize the source manager to set InputFile as the main file.
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
InMemoryModuleCache & getModuleCache() const
void setOutputStream(std::unique_ptr< llvm::raw_pwrite_stream > OutStream)
DependencyOutputOptions & getDependencyOutputOpts()
void setBuildGlobalModuleIndex(bool Build)
Set the flag indicating whether we should (re)build the global module index.
std::unique_ptr< Sema > takeSema()
CodeCompleteConsumer & getCodeCompletionConsumer() const
IntrusiveRefCntPtr< DiagnosticsEngine > getDiagnosticsPtr() const
void printDiagnosticStats()
At the end of a compilation, print the number of warnings/errors.
void setASTConsumer(std::unique_ptr< ASTConsumer > Value)
setASTConsumer - Replace the current AST consumer; the compiler instance takes ownership of Value.
IntrusiveRefCntPtr< ASTReader > getASTReader() const
FileSystemOptions & getFileSystemOpts()
ASTConsumer & getASTConsumer() const
void setTarget(TargetInfo *Value)
Replace the current Target.
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
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...
ASTContext & getASTContext() const
void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, StringRef Source) override
Attempt to create the given module from the specified source buffer.
const FrontendOptions & getFrontendOpts() const
SourceManager & getSourceManager() const
Return the current source manager.
TargetInfo * getAuxTarget() const
void setASTContext(ASTContext *Value)
setASTContext - Replace the current AST context.
void setFailedModulesSet(std::shared_ptr< FailedModulesSet > FMS)
llvm::Timer & getFrontendTimer() const
IntrusiveRefCntPtr< TargetInfo > getTargetPtr() const
void LoadRequestedPlugins()
Load the list of plugins requested in the FrontendOptions.
const TargetOptions & getTargetOpts() const
void setASTReader(IntrusiveRefCntPtr< ASTReader > Reader)
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
bool hadModuleLoaderFatalFailure() const
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
FileManager & getFileManager() const
Return the current file manager to the caller.
APINotesOptions & getAPINotesOpts()
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
std::shared_ptr< CompilerInvocation > getInvocationPtr()
const DependencyOutputOptions & getDependencyOutputOpts() const
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
bool hasFailedModulesSet() const
TargetOptions & getTargetOpts()
void setVerboseOutputStream(raw_ostream &Value)
Replace the current stream for verbose output.
IntrusiveRefCntPtr< FileManager > getFileManagerPtr() const
AnalyzerOptions & getAnalyzerOpts()
DiagnosticConsumer & getDiagnosticClient() const
HeaderSearchOptions & getHeaderSearchOpts()
raw_ostream & getVerboseOutputStream()
Get the current stream for verbose output.
const FileSystemOptions & getFileSystemOpts() const
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
llvm::vfs::FileSystem & getVirtualFileSystem() const
void createCodeCompletionConsumer()
Create a code completion consumer using the invocation; note that this will cause the source manager ...
LangOptions & getLangOpts()
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.
DiagnosticOptions & getDiagnosticOpts()
void clearOutputFiles(bool EraseFiles)
clearOutputFiles - Clear the output file list.
PreprocessorOutputOptions & getPreprocessorOutputOpts()
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
IntrusiveRefCntPtr< ASTContext > getASTContextPtr() const
const PCHContainerWriter & getPCHContainerWriter() const
Return the appropriate PCHContainerWriter depending on the current CodeGenOptions.
const DiagnosticOptions & getDiagnosticOpts() const
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
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.
const PreprocessorOptions & getPreprocessorOpts() const
bool loadModuleFile(StringRef FileName, serialization::ModuleFile *&LoadedModuleFile)
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...
Definition: Diagnostic.h:1751
Options for controlling the compiler diagnostics engine.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:193
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
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...
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
std::string ModuleFormat
The module/pch container format.
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...
Definition: LangOptions.h:482
Describes the result of attempting to load a module.
Definition: ModuleLoader.h:35
Abstract interface for a module loader.
Definition: ModuleLoader.h:82
Describes a module or submodule.
Definition: Module.h:105
NameVisibilityKind
Describes the visibility of the various names within a particular module.
Definition: Module.h:387
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.
Definition: Preprocessor.h:128
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:462
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Exposes information about the current target.
Definition: TargetInfo.h:218
Options for controlling the target.
Definition: TargetOptions.h:26
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:124
Defines the clang::TargetInfo interface.
@ ModuleFile
The module file (.pcm). Required.
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.
Definition: LangOptions.h:1074
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.
Definition: Visibility.h:34
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30