2222#include  " clang/Basic/SourceLocation.h" 
2323#include  " clang/Basic/SourceManager.h" 
2424#include  " clang/Basic/TargetOptions.h" 
25+ #include  " clang/Frontend/PrecompiledPreamble.h" 
26+ #include  " clang/Frontend/StandaloneDiagnostic.h" 
2527#include  " clang/Lex/HeaderSearchOptions.h" 
2628#include  " clang/Lex/ModuleLoader.h" 
2729#include  " clang/Lex/PreprocessingRecord.h" 
2830#include  " clang/Sema/CodeCompleteConsumer.h" 
2931#include  " clang/Serialization/ASTBitCodes.h" 
30- #include  " clang/Frontend/PrecompiledPreamble .h" 
32+ #include  " clang/Serialization/ASTWriter .h" 
3133#include  " llvm/ADT/ArrayRef.h" 
3234#include  " llvm/ADT/DenseMap.h" 
3335#include  " llvm/ADT/IntrusiveRefCntPtr.h" 
3638#include  " llvm/ADT/StringMap.h" 
3739#include  " llvm/ADT/StringRef.h" 
3840#include  " llvm/ADT/iterator_range.h" 
41+ #include  " llvm/Bitstream/BitstreamWriter.h" 
3942#include  < cassert> 
4043#include  < cstddef> 
4144#include  < cstdint> 
@@ -88,25 +91,6 @@ enum class CaptureDiagsKind { None, All, AllWithoutNonErrorsFromIncludes };
8891
8992// / Utility class for loading a ASTContext from an AST file.
9093class  ASTUnit  {
91- public: 
92-   struct  StandaloneFixIt  {
93-     std::pair<unsigned , unsigned > RemoveRange;
94-     std::pair<unsigned , unsigned > InsertFromRange;
95-     std::string CodeToInsert;
96-     bool  BeforePreviousInsertions;
97-   };
98- 
99-   struct  StandaloneDiagnostic  {
100-     unsigned  ID;
101-     DiagnosticsEngine::Level Level;
102-     std::string Message;
103-     std::string Filename;
104-     unsigned  LocOffset;
105-     std::vector<std::pair<unsigned , unsigned >> Ranges;
106-     std::vector<StandaloneFixIt> FixIts;
107-   };
108- 
109- private: 
11094  std::unique_ptr<LangOptions> LangOpts;
11195  std::unique_ptr<CodeGenOptions> CodeGenOpts;
11296  //  FIXME: The documentation on \c LoadFrom* member functions states that the
@@ -129,7 +113,15 @@ class ASTUnit {
129113  bool  HadModuleLoaderFatalFailure = false ;
130114  bool  StorePreamblesInMemory = false ;
131115
132-   struct  ASTWriterData ;
116+   // / Utility struct for managing ASTWriter and its associated data streams.
117+   struct  ASTWriterData  {
118+     SmallString<128 > Buffer;
119+     llvm::BitstreamWriter Stream;
120+     ASTWriter Writer;
121+ 
122+     ASTWriterData (ModuleCache &ModCache, const  CodeGenOptions &CGOpts)
123+         : Stream(Buffer), Writer(Stream, Buffer, ModCache, CGOpts, {}) {}
124+   };
133125  std::unique_ptr<ASTWriterData> WriterData;
134126
135127  FileSystemOptions FileSystemOpts;
@@ -271,11 +263,6 @@ class ASTUnit {
271263  static  void  ConfigureDiags (IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
272264                             ASTUnit &AST, CaptureDiagsKind CaptureDiagnostics);
273265
274-   void  TranslateStoredDiagnostics (FileManager &FileMgr,
275-                                   SourceManager &SrcMan,
276-                       const  SmallVectorImpl<StandaloneDiagnostic> &Diags,
277-                             SmallVectorImpl<StoredDiagnostic> &Out);
278- 
279266  void  clearFileLevelDecls ();
280267
281268public: 
@@ -834,65 +821,24 @@ class ASTUnit {
834821      bool  IncludeBriefCommentsInCodeCompletion = false ,
835822      bool  UserFilesAreVolatile = false );
836823
837-   // / LoadFromCommandLine - Create an ASTUnit from a vector of command line
838-   // / arguments, which must specify exactly one source file.
839-   // /
840-   // / \param ArgBegin - The beginning of the argument vector.
841-   // /
842-   // / \param ArgEnd - The end of the argument vector.
843-   // /
844-   // / \param PCHContainerOps - The PCHContainerOperations to use for loading and
845-   // / creating modules.
846-   // /
847-   // / \param Diags - The diagnostics engine to use for reporting errors; its
848-   // / lifetime is expected to extend past that of the returned ASTUnit.
849-   // /
850-   // / \param ResourceFilesPath - The path to the compiler resource files.
851-   // /
852-   // / \param StorePreamblesInMemory - Whether to store PCH in memory. If false,
853-   // / PCH are stored in temporary files.
854-   // /
855-   // / \param PreambleStoragePath - The path to a directory, in which to create
856-   // / temporary PCH files. If empty, the default system temporary directory is
857-   // / used. This parameter is ignored if \p StorePreamblesInMemory is true.
858-   // /
859-   // / \param ModuleFormat - If provided, uses the specific module format.
860-   // /
861-   // / \param ErrAST - If non-null and parsing failed without any AST to return
862-   // / (e.g. because the PCH could not be loaded), this accepts the ASTUnit
863-   // / mainly to allow the caller to see the diagnostics.
864-   // /
865-   // / \param VFS - A llvm::vfs::FileSystem to be used for all file accesses.
866-   // / Note that preamble is saved to a temporary directory on a RealFileSystem,
867-   // / so in order for it to be loaded correctly, VFS should have access to
868-   // / it(i.e., be an overlay over RealFileSystem). RealFileSystem will be used
869-   // / if \p VFS is nullptr.
870-   // /
871-   //  FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
872-   //  shouldn't need to specify them at construction time.
873-   static  std::unique_ptr<ASTUnit> LoadFromCommandLine (
824+   friend  std::unique_ptr<ASTUnit> CreateASTUnitFromCommandLine (
874825      const  char  **ArgBegin, const  char  **ArgEnd,
875826      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
876827      std::shared_ptr<DiagnosticOptions> DiagOpts,
877828      IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
878-       bool  StorePreamblesInMemory = false ,
879-       StringRef PreambleStoragePath = StringRef(), bool OnlyLocalDecls = false,
880-       CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
881-       ArrayRef<RemappedFile> RemappedFiles = {},
882-       bool  RemappedFilesKeepOriginalName = true ,
883-       unsigned  PrecompilePreambleAfterNParses = 0 ,
884-       TranslationUnitKind TUKind = TU_Complete,
885-       bool  CacheCodeCompletionResults = false ,
886-       bool  IncludeBriefCommentsInCodeCompletion = false ,
887-       bool  AllowPCHWithCompilerErrors = false ,
888-       SkipFunctionBodiesScope SkipFunctionBodies =
889-           SkipFunctionBodiesScope::None,
890-       bool  SingleFileParse = false , bool  UserFilesAreVolatile = false ,
891-       bool  ForSerialization = false ,
892-       bool  RetainExcludedConditionalBlocks = false ,
893-       std::optional<StringRef> ModuleFormat = std::nullopt ,
894-       std::unique_ptr<ASTUnit> *ErrAST = nullptr ,
895-       IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr );
829+       bool  StorePreamblesInMemory, StringRef PreambleStoragePath,
830+       bool  OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
831+       ArrayRef<ASTUnit::RemappedFile> RemappedFiles,
832+       bool  RemappedFilesKeepOriginalName,
833+       unsigned  PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
834+       bool  CacheCodeCompletionResults,
835+       bool  IncludeBriefCommentsInCodeCompletion,
836+       bool  AllowPCHWithCompilerErrors,
837+       SkipFunctionBodiesScope SkipFunctionBodies, bool  SingleFileParse,
838+       bool  UserFilesAreVolatile, bool  ForSerialization,
839+       bool  RetainExcludedConditionalBlocks,
840+       std::optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST,
841+       IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);
896842
897843  // / Reparse the source files using the same command-line options that
898844  // / were originally used to produce this translation unit.
@@ -963,6 +909,44 @@ class ASTUnit {
963909  bool  serialize (raw_ostream &OS);
964910};
965911
912+ // / Diagnostic consumer that saves each diagnostic it is given.
913+ class  FilterAndStoreDiagnosticConsumer  : public  DiagnosticConsumer  {
914+   SmallVectorImpl<StoredDiagnostic> *StoredDiags;
915+   SmallVectorImpl<StandaloneDiagnostic> *StandaloneDiags;
916+   bool  CaptureNonErrorsFromIncludes = true ;
917+   const  LangOptions *LangOpts = nullptr ;
918+   SourceManager *SourceMgr = nullptr ;
919+ 
920+ public: 
921+   FilterAndStoreDiagnosticConsumer (
922+       SmallVectorImpl<StoredDiagnostic> *StoredDiags,
923+       SmallVectorImpl<StandaloneDiagnostic> *StandaloneDiags,
924+       bool  CaptureNonErrorsFromIncludes);
925+ 
926+   void  BeginSourceFile (const  LangOptions &LangOpts,
927+                        const  Preprocessor *PP = nullptr ) override ;
928+ 
929+   void  HandleDiagnostic (DiagnosticsEngine::Level Level,
930+                         const  Diagnostic &Info) override ;
931+ };
932+ 
933+ // / RAII object that optionally captures and filters diagnostics, if
934+ // / there is no diagnostic client to capture them already.
935+ class  CaptureDroppedDiagnostics  {
936+   DiagnosticsEngine &Diags;
937+   FilterAndStoreDiagnosticConsumer Client;
938+   DiagnosticConsumer *PreviousClient = nullptr ;
939+   std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
940+ 
941+ public: 
942+   CaptureDroppedDiagnostics (
943+       CaptureDiagsKind CaptureDiagnostics, DiagnosticsEngine &Diags,
944+       SmallVectorImpl<StoredDiagnostic> *StoredDiags,
945+       SmallVectorImpl<StandaloneDiagnostic> *StandaloneDiags);
946+ 
947+   ~CaptureDroppedDiagnostics ();
948+ };
949+ 
966950} //  namespace clang
967951
968952#endif  //  LLVM_CLANG_FRONTEND_ASTUNIT_H
0 commit comments