clang  10.0.0git
ASTWriter.h
Go to the documentation of this file.
1 //===- ASTWriter.h - AST File Writer ----------------------------*- 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 // This file defines the ASTWriter class, which writes an AST file
10 // containing a serialized representation of a translation unit.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H
15 #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H
16 
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/LLVM.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/DenseSet.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/Bitstream/BitstreamWriter.h"
34 #include <cassert>
35 #include <cstddef>
36 #include <cstdint>
37 #include <ctime>
38 #include <memory>
39 #include <queue>
40 #include <string>
41 #include <utility>
42 #include <vector>
43 
44 namespace llvm {
45 
46 class APFloat;
47 class APInt;
48 class APSInt;
49 
50 } // namespace llvm
51 
52 namespace clang {
53 
54 class ASTContext;
55 class ASTReader;
56 class ASTUnresolvedSet;
57 class Attr;
58 class CXXBaseSpecifier;
59 class CXXCtorInitializer;
60 class CXXRecordDecl;
61 class CXXTemporary;
62 class FileEntry;
63 class FPOptions;
64 class FunctionDecl;
65 class HeaderSearch;
66 class HeaderSearchOptions;
67 class IdentifierResolver;
68 class LangOptions;
69 class MacroDefinitionRecord;
70 class MacroInfo;
71 class Module;
72 class InMemoryModuleCache;
73 class ModuleFileExtension;
74 class ModuleFileExtensionWriter;
75 class NamedDecl;
76 class ObjCInterfaceDecl;
77 class PreprocessingRecord;
78 class Preprocessor;
79 struct QualifierInfo;
80 class RecordDecl;
81 class Sema;
82 class SourceManager;
83 class Stmt;
84 struct StoredDeclsList;
85 class SwitchCase;
86 class TemplateParameterList;
87 class Token;
88 class TypeSourceInfo;
89 
90 /// Writes an AST file containing the contents of a translation unit.
91 ///
92 /// The ASTWriter class produces a bitstream containing the serialized
93 /// representation of a given abstract syntax tree and its supporting
94 /// data structures. This bitstream can be de-serialized via an
95 /// instance of the ASTReader class.
97  public ASTMutationListener {
98 public:
99  friend class ASTDeclWriter;
100  friend class ASTRecordWriter;
101 
105 
106 private:
107  /// Map that provides the ID numbers of each type within the
108  /// output stream, plus those deserialized from a chained PCH.
109  ///
110  /// The ID numbers of types are consecutive (in order of discovery)
111  /// and start at 1. 0 is reserved for NULL. When types are actually
112  /// stored in the stream, the ID number is shifted by 2 bits to
113  /// allow for the const/volatile qualifiers.
114  ///
115  /// Keys in the map never have const/volatile qualifiers.
116  using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx,
118 
119  /// The bitstream writer used to emit this precompiled header.
120  llvm::BitstreamWriter &Stream;
121 
122  /// The buffer associated with the bitstream.
123  const SmallVectorImpl<char> &Buffer;
124 
125  /// The PCM manager which manages memory buffers for pcm files.
126  InMemoryModuleCache &ModuleCache;
127 
128  /// The ASTContext we're writing.
129  ASTContext *Context = nullptr;
130 
131  /// The preprocessor we're writing.
132  Preprocessor *PP = nullptr;
133 
134  /// The reader of existing AST files, if we're chaining.
135  ASTReader *Chain = nullptr;
136 
137  /// The module we're currently writing, if any.
138  Module *WritingModule = nullptr;
139 
140  /// The base directory for any relative paths we emit.
141  std::string BaseDirectory;
142 
143  /// Indicates whether timestamps should be written to the produced
144  /// module file. This is the case for files implicitly written to the
145  /// module cache, where we need the timestamps to determine if the module
146  /// file is up to date, but not otherwise.
147  bool IncludeTimestamps;
148 
149  /// Indicates when the AST writing is actively performing
150  /// serialization, rather than just queueing updates.
151  bool WritingAST = false;
152 
153  /// Indicates that we are done serializing the collection of decls
154  /// and types to emit.
155  bool DoneWritingDeclsAndTypes = false;
156 
157  /// Indicates that the AST contained compiler errors.
158  bool ASTHasCompilerErrors = false;
159 
160  /// Mapping from input file entries to the index into the
161  /// offset table where information about that input file is stored.
162  llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
163 
164  /// Stores a declaration or a type to be written to the AST file.
165  class DeclOrType {
166  public:
167  DeclOrType(Decl *D) : Stored(D), IsType(false) {}
168  DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {}
169 
170  bool isType() const { return IsType; }
171  bool isDecl() const { return !IsType; }
172 
173  QualType getType() const {
174  assert(isType() && "Not a type!");
175  return QualType::getFromOpaquePtr(Stored);
176  }
177 
178  Decl *getDecl() const {
179  assert(isDecl() && "Not a decl!");
180  return static_cast<Decl *>(Stored);
181  }
182 
183  private:
184  void *Stored;
185  bool IsType;
186  };
187 
188  /// The declarations and types to emit.
189  std::queue<DeclOrType> DeclTypesToEmit;
190 
191  /// The first ID number we can use for our own declarations.
193 
194  /// The decl ID that will be assigned to the next new decl.
195  serialization::DeclID NextDeclID = FirstDeclID;
196 
197  /// Map that provides the ID numbers of each declaration within
198  /// the output stream, as well as those deserialized from a chained PCH.
199  ///
200  /// The ID numbers of declarations are consecutive (in order of
201  /// discovery) and start at 2. 1 is reserved for the translation
202  /// unit, while 0 is reserved for NULL.
203  llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
204 
205  /// Offset of each declaration in the bitstream, indexed by
206  /// the declaration's ID.
207  std::vector<serialization::DeclOffset> DeclOffsets;
208 
209  /// Sorted (by file offset) vector of pairs of file offset/DeclID.
210  using LocDeclIDsTy =
212  struct DeclIDInFileInfo {
213  LocDeclIDsTy DeclIDs;
214 
215  /// Set when the DeclIDs vectors from all files are joined, this
216  /// indicates the index that this particular vector has in the global one.
217  unsigned FirstDeclIndex;
218  };
219  using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>;
220 
221  /// Map from file SLocEntries to info about the file-level declarations
222  /// that it contains.
223  FileDeclIDsTy FileDeclIDs;
224 
225  void associateDeclWithFile(const Decl *D, serialization::DeclID);
226 
227  /// The first ID number we can use for our own types.
229 
230  /// The type ID that will be assigned to the next new type.
231  serialization::TypeID NextTypeID = FirstTypeID;
232 
233  /// Map that provides the ID numbers of each type within the
234  /// output stream, plus those deserialized from a chained PCH.
235  ///
236  /// The ID numbers of types are consecutive (in order of discovery)
237  /// and start at 1. 0 is reserved for NULL. When types are actually
238  /// stored in the stream, the ID number is shifted by 2 bits to
239  /// allow for the const/volatile qualifiers.
240  ///
241  /// Keys in the map never have const/volatile qualifiers.
242  TypeIdxMap TypeIdxs;
243 
244  /// Offset of each type in the bitstream, indexed by
245  /// the type's ID.
246  std::vector<uint32_t> TypeOffsets;
247 
248  /// The first ID number we can use for our own identifiers.
250 
251  /// The identifier ID that will be assigned to the next new identifier.
252  serialization::IdentID NextIdentID = FirstIdentID;
253 
254  /// Map that provides the ID numbers of each identifier in
255  /// the output stream.
256  ///
257  /// The ID numbers for identifiers are consecutive (in order of
258  /// discovery), starting at 1. An ID of zero refers to a NULL
259  /// IdentifierInfo.
260  llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
261 
262  /// The first ID number we can use for our own macros.
264 
265  /// The identifier ID that will be assigned to the next new identifier.
266  serialization::MacroID NextMacroID = FirstMacroID;
267 
268  /// Map that provides the ID numbers of each macro.
269  llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
270 
271  struct MacroInfoToEmitData {
272  const IdentifierInfo *Name;
273  MacroInfo *MI;
275  };
276 
277  /// The macro infos to emit.
278  std::vector<MacroInfoToEmitData> MacroInfosToEmit;
279 
280  llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
281 
282  /// @name FlushStmt Caches
283  /// @{
284 
285  /// Set of parent Stmts for the currently serializing sub-stmt.
286  llvm::DenseSet<Stmt *> ParentStmts;
287 
288  /// Offsets of sub-stmts already serialized. The offset points
289  /// just after the stmt record.
290  llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
291 
292  /// @}
293 
294  /// Offsets of each of the identifier IDs into the identifier
295  /// table.
296  std::vector<uint32_t> IdentifierOffsets;
297 
298  /// The first ID number we can use for our own submodules.
299  serialization::SubmoduleID FirstSubmoduleID =
301 
302  /// The submodule ID that will be assigned to the next new submodule.
303  serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
304 
305  /// The first ID number we can use for our own selectors.
306  serialization::SelectorID FirstSelectorID =
308 
309  /// The selector ID that will be assigned to the next new selector.
310  serialization::SelectorID NextSelectorID = FirstSelectorID;
311 
312  /// Map that provides the ID numbers of each Selector.
313  llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
314 
315  /// Offset of each selector within the method pool/selector
316  /// table, indexed by the Selector ID (-1).
317  std::vector<uint32_t> SelectorOffsets;
318 
319  /// Mapping from macro definitions (as they occur in the preprocessing
320  /// record) to the macro IDs.
321  llvm::DenseMap<const MacroDefinitionRecord *,
322  serialization::PreprocessedEntityID> MacroDefinitions;
323 
324  /// Cache of indices of anonymous declarations within their lexical
325  /// contexts.
326  llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
327 
328  /// An update to a Decl.
329  class DeclUpdate {
330  /// A DeclUpdateKind.
331  unsigned Kind;
332  union {
333  const Decl *Dcl;
334  void *Type;
335  unsigned Loc;
336  unsigned Val;
337  Module *Mod;
338  const Attr *Attribute;
339  };
340 
341  public:
342  DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
343  DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
344  DeclUpdate(unsigned Kind, QualType Type)
345  : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
346  DeclUpdate(unsigned Kind, SourceLocation Loc)
347  : Kind(Kind), Loc(Loc.getRawEncoding()) {}
348  DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {}
349  DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {}
350  DeclUpdate(unsigned Kind, const Attr *Attribute)
351  : Kind(Kind), Attribute(Attribute) {}
352 
353  unsigned getKind() const { return Kind; }
354  const Decl *getDecl() const { return Dcl; }
355  QualType getType() const { return QualType::getFromOpaquePtr(Type); }
356 
357  SourceLocation getLoc() const {
358  return SourceLocation::getFromRawEncoding(Loc);
359  }
360 
361  unsigned getNumber() const { return Val; }
362  Module *getModule() const { return Mod; }
363  const Attr *getAttr() const { return Attribute; }
364  };
365 
367  using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
368 
369  /// Mapping from declarations that came from a chained PCH to the
370  /// record containing modifications to them.
371  DeclUpdateMap DeclUpdates;
372 
373  using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
374 
375  /// Map of first declarations from a chained PCH that point to the
376  /// most recent declarations in another PCH.
377  FirstLatestDeclMap FirstLatestDecls;
378 
379  /// Declarations encountered that might be external
380  /// definitions.
381  ///
382  /// We keep track of external definitions and other 'interesting' declarations
383  /// as we are emitting declarations to the AST file. The AST file contains a
384  /// separate record for these declarations, which are provided to the AST
385  /// consumer by the AST reader. This is behavior is required to properly cope with,
386  /// e.g., tentative variable definitions that occur within
387  /// headers. The declarations themselves are stored as declaration
388  /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
389  /// record.
390  SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
391  SmallVector<uint64_t, 16> ModularCodegenDecls;
392 
393  /// DeclContexts that have received extensions since their serialized
394  /// form.
395  ///
396  /// For namespaces, when we're chaining and encountering a namespace, we check
397  /// if its primary namespace comes from the chain. If it does, we add the
398  /// primary to this set, so that we can write out lexical content updates for
399  /// it.
401 
402  /// Keeps track of declarations that we must emit, even though we're
403  /// not guaranteed to be able to find them by walking the AST starting at the
404  /// translation unit.
405  SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
406 
407  /// The set of Objective-C class that have categories we
408  /// should serialize.
409  llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
410 
411  /// The set of declarations that may have redeclaration chains that
412  /// need to be serialized.
414 
415  /// A cache of the first local declaration for "interesting"
416  /// redeclaration chains.
417  llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
418 
419  /// Mapping from SwitchCase statements to IDs.
420  llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
421 
422  /// The number of statements written to the AST file.
423  unsigned NumStatements = 0;
424 
425  /// The number of macros written to the AST file.
426  unsigned NumMacros = 0;
427 
428  /// The number of lexical declcontexts written to the AST
429  /// file.
430  unsigned NumLexicalDeclContexts = 0;
431 
432  /// The number of visible declcontexts written to the AST
433  /// file.
434  unsigned NumVisibleDeclContexts = 0;
435 
436  /// A mapping from each known submodule to its ID number, which will
437  /// be a positive integer.
438  llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
439 
440  /// A list of the module file extension writers.
441  std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
442  ModuleFileExtensionWriters;
443 
444  /// Retrieve or create a submodule ID for this module.
445  unsigned getSubmoduleID(Module *Mod);
446 
447  /// Write the given subexpression to the bitstream.
448  void WriteSubStmt(Stmt *S);
449 
450  void WriteBlockInfoBlock();
451  void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
452  StringRef isysroot, const std::string &OutputFile);
453 
454  /// Write out the signature and diagnostic options, and return the signature.
455  ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
456  ASTContext &Context);
457 
458  /// Calculate hash of the pcm content.
459  static ASTFileSignature createSignature(StringRef Bytes);
460 
461  void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
462  bool Modules);
463  void WriteSourceManagerBlock(SourceManager &SourceMgr,
464  const Preprocessor &PP);
465  void WritePreprocessor(const Preprocessor &PP, bool IsModule);
466  void WriteHeaderSearch(const HeaderSearch &HS);
467  void WritePreprocessorDetail(PreprocessingRecord &PPRec);
468  void WriteSubmodules(Module *WritingModule);
469 
470  void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
471  bool isModule);
472 
473  unsigned TypeExtQualAbbrev = 0;
474  unsigned TypeFunctionProtoAbbrev = 0;
475  void WriteTypeAbbrevs();
476  void WriteType(QualType T);
477 
478  bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
479  bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
480 
481  void GenerateNameLookupTable(const DeclContext *DC,
482  llvm::SmallVectorImpl<char> &LookupTable);
483  uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
484  uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
485  void WriteTypeDeclOffsets();
486  void WriteFileDeclIDsMap();
487  void WriteComments();
488  void WriteSelectors(Sema &SemaRef);
489  void WriteReferencedSelectorsPool(Sema &SemaRef);
490  void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
491  bool IsModule);
492  void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
493  void WriteDeclContextVisibleUpdate(const DeclContext *DC);
494  void WriteFPPragmaOptions(const FPOptions &Opts);
495  void WriteOpenCLExtensions(Sema &SemaRef);
496  void WriteOpenCLExtensionTypes(Sema &SemaRef);
497  void WriteOpenCLExtensionDecls(Sema &SemaRef);
498  void WriteCUDAPragmas(Sema &SemaRef);
499  void WriteObjCCategories();
500  void WriteLateParsedTemplates(Sema &SemaRef);
501  void WriteOptimizePragmaOptions(Sema &SemaRef);
502  void WriteMSStructPragmaOptions(Sema &SemaRef);
503  void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
504  void WritePackPragmaOptions(Sema &SemaRef);
505  void WriteModuleFileExtension(Sema &SemaRef,
506  ModuleFileExtensionWriter &Writer);
507 
508  unsigned DeclParmVarAbbrev = 0;
509  unsigned DeclContextLexicalAbbrev = 0;
510  unsigned DeclContextVisibleLookupAbbrev = 0;
511  unsigned UpdateVisibleAbbrev = 0;
512  unsigned DeclRecordAbbrev = 0;
513  unsigned DeclTypedefAbbrev = 0;
514  unsigned DeclVarAbbrev = 0;
515  unsigned DeclFieldAbbrev = 0;
516  unsigned DeclEnumAbbrev = 0;
517  unsigned DeclObjCIvarAbbrev = 0;
518  unsigned DeclCXXMethodAbbrev = 0;
519 
520  unsigned DeclRefExprAbbrev = 0;
521  unsigned CharacterLiteralAbbrev = 0;
522  unsigned IntegerLiteralAbbrev = 0;
523  unsigned ExprImplicitCastAbbrev = 0;
524 
525  void WriteDeclAbbrevs();
526  void WriteDecl(ASTContext &Context, Decl *D);
527 
528  ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
529  const std::string &OutputFile,
530  Module *WritingModule);
531 
532 public:
533  /// Create a new precompiled header writer that outputs to
534  /// the given bitstream.
535  ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
536  InMemoryModuleCache &ModuleCache,
537  ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
538  bool IncludeTimestamps = true);
539  ~ASTWriter() override;
540 
541  const LangOptions &getLangOpts() const;
542 
543  /// Get a timestamp for output into the AST file. The actual timestamp
544  /// of the specified file may be ignored if we have been instructed to not
545  /// include timestamps in the output file.
546  time_t getTimestampForOutput(const FileEntry *E) const;
547 
548  /// Write a precompiled header for the given semantic analysis.
549  ///
550  /// \param SemaRef a reference to the semantic analysis object that processed
551  /// the AST to be written into the precompiled header.
552  ///
553  /// \param WritingModule The module that we are writing. If null, we are
554  /// writing a precompiled header.
555  ///
556  /// \param isysroot if non-empty, write a relocatable file whose headers
557  /// are relative to the given system root. If we're writing a module, its
558  /// build directory will be used in preference to this if both are available.
559  ///
560  /// \return the module signature, which eventually will be a hash of
561  /// the module but currently is merely a random 32-bit number.
562  ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile,
563  Module *WritingModule, StringRef isysroot,
564  bool hasErrors = false,
565  bool ShouldCacheASTInMemory = false);
566 
567  /// Emit a token.
568  void AddToken(const Token &Tok, RecordDataImpl &Record);
569 
570  /// Emit a source location.
571  void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
572 
573  /// Emit a source range.
574  void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
575 
576  /// Emit a reference to an identifier.
577  void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
578 
579  /// Get the unique number used to refer to the given selector.
580  serialization::SelectorID getSelectorRef(Selector Sel);
581 
582  /// Get the unique number used to refer to the given identifier.
583  serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
584 
585  /// Get the unique number used to refer to the given macro.
586  serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
587 
588  /// Determine the ID of an already-emitted macro.
589  serialization::MacroID getMacroID(MacroInfo *MI);
590 
591  uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
592 
593  /// Emit a reference to a type.
594  void AddTypeRef(QualType T, RecordDataImpl &Record);
595 
596  /// Force a type to be emitted and get its ID.
597  serialization::TypeID GetOrCreateTypeID(QualType T);
598 
599  /// Determine the type ID of an already-emitted type.
600  serialization::TypeID getTypeID(QualType T) const;
601 
602  /// Find the first local declaration of a given local redeclarable
603  /// decl.
604  const Decl *getFirstLocalDecl(const Decl *D);
605 
606  /// Is this a local declaration (that is, one that will be written to
607  /// our AST file)? This is the case for declarations that are neither imported
608  /// from another AST file nor predefined.
609  bool IsLocalDecl(const Decl *D) {
610  if (D->isFromASTFile())
611  return false;
612  auto I = DeclIDs.find(D);
613  return (I == DeclIDs.end() ||
615  };
616 
617  /// Emit a reference to a declaration.
618  void AddDeclRef(const Decl *D, RecordDataImpl &Record);
619 
620  /// Force a declaration to be emitted and get its ID.
621  serialization::DeclID GetDeclRef(const Decl *D);
622 
623  /// Determine the declaration ID of an already-emitted
624  /// declaration.
625  serialization::DeclID getDeclID(const Decl *D);
626 
627  unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
628 
629  /// Add a string to the given record.
630  void AddString(StringRef Str, RecordDataImpl &Record);
631 
632  /// Convert a path from this build process into one that is appropriate
633  /// for emission in the module file.
634  bool PreparePathForOutput(SmallVectorImpl<char> &Path);
635 
636  /// Add a path to the given record.
637  void AddPath(StringRef Path, RecordDataImpl &Record);
638 
639  /// Emit the current record with the given path as a blob.
640  void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
641  StringRef Path);
642 
643  /// Add a version tuple to the given record
644  void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
645 
646  /// Retrieve or create a submodule ID for this module, or return 0 if
647  /// the submodule is neither local (a submodle of the currently-written module)
648  /// nor from an imported module.
649  unsigned getLocalOrImportedSubmoduleID(Module *Mod);
650 
651  /// Note that the identifier II occurs at the given offset
652  /// within the identifier table.
653  void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
654 
655  /// Note that the selector Sel occurs at the given offset
656  /// within the method pool/selector table.
657  void SetSelectorOffset(Selector Sel, uint32_t Offset);
658 
659  /// Record an ID for the given switch-case statement.
660  unsigned RecordSwitchCaseID(SwitchCase *S);
661 
662  /// Retrieve the ID for the given switch-case statement.
663  unsigned getSwitchCaseID(SwitchCase *S);
664 
665  void ClearSwitchCaseIDs();
666 
667  unsigned getTypeExtQualAbbrev() const {
668  return TypeExtQualAbbrev;
669  }
670 
671  unsigned getTypeFunctionProtoAbbrev() const {
672  return TypeFunctionProtoAbbrev;
673  }
674 
675  unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
676  unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
677  unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
678  unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
679  unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
680  unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
681  unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
682  unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
683 
684  unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
685  unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
686  unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
687  unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
688 
689  bool hasChain() const { return Chain; }
690  ASTReader *getChain() const { return Chain; }
691 
692 private:
693  // ASTDeserializationListener implementation
694  void ReaderInitialized(ASTReader *Reader) override;
695  void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
696  void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
697  void TypeRead(serialization::TypeIdx Idx, QualType T) override;
698  void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
699  void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
700  MacroDefinitionRecord *MD) override;
701  void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
702 
703  // ASTMutationListener implementation.
704  void CompletedTagDefinition(const TagDecl *D) override;
705  void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
706  void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
707  void AddedCXXTemplateSpecialization(
708  const ClassTemplateDecl *TD,
709  const ClassTemplateSpecializationDecl *D) override;
710  void AddedCXXTemplateSpecialization(
711  const VarTemplateDecl *TD,
712  const VarTemplateSpecializationDecl *D) override;
713  void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
714  const FunctionDecl *D) override;
715  void ResolvedExceptionSpec(const FunctionDecl *FD) override;
716  void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
717  void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
718  const FunctionDecl *Delete,
719  Expr *ThisArg) override;
720  void CompletedImplicitDefinition(const FunctionDecl *D) override;
721  void InstantiationRequested(const ValueDecl *D) override;
722  void VariableDefinitionInstantiated(const VarDecl *D) override;
723  void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
724  void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
725  void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
726  void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
727  const ObjCInterfaceDecl *IFD) override;
728  void DeclarationMarkedUsed(const Decl *D) override;
729  void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
730  void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
731  const Attr *Attr) override;
732  void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
733  void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
734  void AddedAttributeToRecord(const Attr *Attr,
735  const RecordDecl *Record) override;
736 };
737 
738 /// AST and semantic-analysis consumer that generates a
739 /// precompiled header from the parsed source code.
740 class PCHGenerator : public SemaConsumer {
741  const Preprocessor &PP;
742  std::string OutputFile;
743  std::string isysroot;
744  Sema *SemaPtr;
745  std::shared_ptr<PCHBuffer> Buffer;
746  llvm::BitstreamWriter Stream;
747  ASTWriter Writer;
748  bool AllowASTWithErrors;
749  bool ShouldCacheASTInMemory;
750 
751 protected:
752  ASTWriter &getWriter() { return Writer; }
753  const ASTWriter &getWriter() const { return Writer; }
754  SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
755 
756 public:
757  PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
758  StringRef OutputFile, StringRef isysroot,
759  std::shared_ptr<PCHBuffer> Buffer,
760  ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
761  bool AllowASTWithErrors = false, bool IncludeTimestamps = true,
762  bool ShouldCacheASTInMemory = false);
763  ~PCHGenerator() override;
764 
765  void InitializeSema(Sema &S) override { SemaPtr = &S; }
766  void HandleTranslationUnit(ASTContext &Ctx) override;
767  ASTMutationListener *GetASTMutationListener() override;
768  ASTDeserializationListener *GetASTDeserializationListener() override;
769  bool hasEmittedPCH() const { return Buffer->IsComplete; }
770 };
771 
772 } // namespace clang
773 
774 #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H
unsigned getDeclEnumAbbrev() const
Definition: ASTWriter.h:680
Represents a function declaration or definition.
Definition: Decl.h:1783
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Smart pointer class that efficiently represents Objective-C method names.
unsigned getRawEncoding() const
When a SourceLocation itself cannot be used, this returns an (opaque) 32-bit integer encoding for it...
A (possibly-)qualified type.
Definition: Type.h:654
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
A structure for putting "fast"-unqualified QualTypes into a DenseMap.
Definition: ASTBitCodes.h:114
const unsigned NUM_PREDEF_TYPE_IDS
The number of predefined type IDs that are reserved for the PREDEF_TYPE_* constants.
Definition: ASTBitCodes.h:1034
Stmt - This represents one statement.
Definition: Stmt.h:66
C Language Family Type Representation.
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
Definition: ASTWriter.h:765
const unsigned int NUM_PREDEF_SELECTOR_IDS
The number of predefined selector IDs.
Definition: ASTBitCodes.h:156
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
The base class of the type hierarchy.
Definition: Type.h:1450
Declaration of a variable template.
Floating point control options.
Definition: LangOptions.h:357
unsigned getDeclRefExprAbbrev() const
Definition: ASTWriter.h:684
Represents a variable declaration or definition.
Definition: Decl.h:820
const unsigned int NUM_PREDEF_DECL_IDS
The number of declaration IDs that are predefined.
Definition: ASTBitCodes.h:1148
Represents a variable template specialization, which refers to a variable template with a given set o...
SmallVectorImpl< char > & getPCH() const
Definition: ASTWriter.h:754
Represents a parameter to a function.
Definition: Decl.h:1595
const ASTWriter & getWriter() const
Definition: ASTWriter.h:753
bool hasEmittedPCH() const
Definition: ASTWriter.h:769
Represents a struct/union/class.
Definition: Decl.h:3748
Represents a class template specialization, which refers to a class template with a given set of temp...
One of these records is kept for each identifier that is lexed.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
Record the location of a macro definition.
Represents a member of a struct/union/class.
Definition: Decl.h:2729
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
ASTReader * getChain() const
Definition: ASTWriter.h:690
uint32_t MacroID
An ID number that refers to a macro in an AST file.
Definition: ASTBitCodes.h:140
The signature of a module, which is a hash of the AST content.
Definition: Module.h:54
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
Describes a module or submodule.
Definition: Module.h:64
unsigned getDeclFieldAbbrev() const
Definition: ASTWriter.h:679
An abstract interface that should be implemented by clients that read ASTs and then require further s...
Definition: SemaConsumer.h:25
unsigned getIntegerLiteralAbbrev() const
Definition: ASTWriter.h:686
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
const FormatToken & Tok
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
bool hasChain() const
Definition: ASTWriter.h:689
unsigned getCharacterLiteralAbbrev() const
Definition: ASTWriter.h:685
void * getAsOpaquePtr() const
Definition: Type.h:699
Represents an ObjC class declaration.
Definition: DeclObjC.h:1186
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:158
unsigned getTypeFunctionProtoAbbrev() const
Definition: ASTWriter.h:671
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:336
unsigned Offset
Definition: Format.cpp:1827
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:619
This represents one expression.
Definition: Expr.h:108
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2649
unsigned getDeclRecordAbbrev() const
Definition: ASTWriter.h:676
IdentifierResolver - Keeps track of shadowed decls on enclosing scopes.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
unsigned getDeclTypedefAbbrev() const
Definition: ASTWriter.h:677
Abstract base class that writes a module file extension block into a module file. ...
In-memory cache for modules.
const unsigned int NUM_PREDEF_IDENT_IDS
The number of predefined identifier IDs.
Definition: ASTBitCodes.h:137
uint32_t SubmoduleID
An ID number that refers to a submodule in a module file.
Definition: ASTBitCodes.h:171
Kind
Encodes a location in the source.
llvm::APSInt APSInt
unsigned getDeclVarAbbrev() const
Definition: ASTWriter.h:678
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3219
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:78
ObjCCategoryDecl - Represents a category declaration.
Definition: DeclObjC.h:2294
unsigned getDeclCXXMethodAbbrev() const
Definition: ASTWriter.h:682
llvm::APInt APInt
Definition: Integral.h:27
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition: DeclBase.h:702
uint32_t TypeID
An ID number that refers to a type in an AST file.
Definition: ASTBitCodes.h:85
An array of decls optimized for the common case of only containing one entry.
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
Definition: ASTBitCodes.h:168
unsigned getExprImplicitCastAbbrev() const
Definition: ASTWriter.h:687
unsigned getTypeExtQualAbbrev() const
Definition: ASTWriter.h:667
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Definition: ASTBitCodes.h:153
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:340
unsigned getDeclParmVarAbbrev() const
Definition: ASTWriter.h:675
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:68
bool IsLocalDecl(const Decl *D)
Is this a local declaration (that is, one that will be written to our AST file)? This is the case for...
Definition: ASTWriter.h:609
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:134
const unsigned int NUM_PREDEF_MACRO_IDS
The number of predefined macro IDs.
Definition: ASTBitCodes.h:150
Defines the clang::SourceLocation class and associated facilities.
Represents a C++ struct/union/class.
Definition: DeclCXX.h:253
An object for streaming information to a record.
Declaration of a class template.
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:96
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:947
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code...
Definition: ASTWriter.h:740
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
Definition: Decl.h:223
ASTWriter & getWriter()
Definition: ASTWriter.h:752
unsigned getDeclObjCIvarAbbrev() const
Definition: ASTWriter.h:681
This class handles loading and caching of source files into memory.
Declaration of a template function.
Definition: DeclTemplate.h:977
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:88
Attr - This represents one attribute.
Definition: Attr.h:45
const unsigned int NUM_PREDEF_SUBMODULE_IDS
The number of predefined submodule IDs.
Definition: ASTBitCodes.h:174
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128