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