clang  16.0.0git
ASTWriter.cpp
Go to the documentation of this file.
1 //===- ASTWriter.cpp - AST File Writer ------------------------------------===//
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 AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "MultiOnDiskHashTable.h"
16 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/OpenMPClause.h"
34 #include "clang/AST/TemplateName.h"
35 #include "clang/AST/Type.h"
37 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/LLVM.h"
43 #include "clang/Basic/Lambda.h"
45 #include "clang/Basic/Module.h"
51 #include "clang/Basic/Specifiers.h"
52 #include "clang/Basic/TargetInfo.h"
54 #include "clang/Basic/Version.h"
55 #include "clang/Lex/HeaderSearch.h"
57 #include "clang/Lex/MacroInfo.h"
58 #include "clang/Lex/ModuleMap.h"
60 #include "clang/Lex/Preprocessor.h"
62 #include "clang/Lex/Token.h"
65 #include "clang/Sema/Sema.h"
66 #include "clang/Sema/Weak.h"
74 #include "llvm/ADT/APFloat.h"
75 #include "llvm/ADT/APInt.h"
76 #include "llvm/ADT/APSInt.h"
77 #include "llvm/ADT/ArrayRef.h"
78 #include "llvm/ADT/DenseMap.h"
79 #include "llvm/ADT/Hashing.h"
80 #include "llvm/ADT/Optional.h"
81 #include "llvm/ADT/PointerIntPair.h"
82 #include "llvm/ADT/STLExtras.h"
83 #include "llvm/ADT/ScopeExit.h"
84 #include "llvm/ADT/SmallPtrSet.h"
85 #include "llvm/ADT/SmallString.h"
86 #include "llvm/ADT/SmallVector.h"
87 #include "llvm/ADT/StringMap.h"
88 #include "llvm/ADT/StringRef.h"
89 #include "llvm/Bitstream/BitCodes.h"
90 #include "llvm/Bitstream/BitstreamWriter.h"
91 #include "llvm/Support/Casting.h"
92 #include "llvm/Support/Compression.h"
93 #include "llvm/Support/DJB.h"
94 #include "llvm/Support/Endian.h"
95 #include "llvm/Support/EndianStream.h"
96 #include "llvm/Support/Error.h"
97 #include "llvm/Support/ErrorHandling.h"
98 #include "llvm/Support/LEB128.h"
99 #include "llvm/Support/MemoryBuffer.h"
100 #include "llvm/Support/OnDiskHashTable.h"
101 #include "llvm/Support/Path.h"
102 #include "llvm/Support/SHA1.h"
103 #include "llvm/Support/TimeProfiler.h"
104 #include "llvm/Support/VersionTuple.h"
105 #include "llvm/Support/raw_ostream.h"
106 #include <algorithm>
107 #include <cassert>
108 #include <cstdint>
109 #include <cstdlib>
110 #include <cstring>
111 #include <ctime>
112 #include <limits>
113 #include <memory>
114 #include <queue>
115 #include <tuple>
116 #include <utility>
117 #include <vector>
118 
119 using namespace clang;
120 using namespace clang::serialization;
121 
122 template <typename T, typename Allocator>
123 static StringRef bytes(const std::vector<T, Allocator> &v) {
124  if (v.empty()) return StringRef();
125  return StringRef(reinterpret_cast<const char*>(&v[0]),
126  sizeof(T) * v.size());
127 }
128 
129 template <typename T>
130 static StringRef bytes(const SmallVectorImpl<T> &v) {
131  return StringRef(reinterpret_cast<const char*>(v.data()),
132  sizeof(T) * v.size());
133 }
134 
135 static std::string bytes(const std::vector<bool> &V) {
136  std::string Str;
137  Str.reserve(V.size() / 8);
138  for (unsigned I = 0, E = V.size(); I < E;) {
139  char Byte = 0;
140  for (unsigned Bit = 0; Bit < 8 && I < E; ++Bit, ++I)
141  Byte |= V[I] << Bit;
142  Str += Byte;
143  }
144  return Str;
145 }
146 
147 //===----------------------------------------------------------------------===//
148 // Type serialization
149 //===----------------------------------------------------------------------===//
150 
152  switch (id) {
153 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
154  case Type::CLASS_ID: return TYPE_##CODE_ID;
155 #include "clang/Serialization/TypeBitCodes.def"
156  case Type::Builtin:
157  llvm_unreachable("shouldn't be serializing a builtin type this way");
158  }
159  llvm_unreachable("bad type kind");
160 }
161 
162 namespace {
163 
164 std::set<const FileEntry *> GetAffectingModuleMaps(const HeaderSearch &HS,
165  Module *RootModule) {
166  std::set<const FileEntry *> ModuleMaps{};
167  std::set<const Module *> ProcessedModules;
168  SmallVector<const Module *> ModulesToProcess{RootModule};
169 
171  HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
172 
173  if (FilesByUID.size() > HS.header_file_size())
174  FilesByUID.resize(HS.header_file_size());
175 
176  for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
177  const FileEntry *File = FilesByUID[UID];
178  if (!File)
179  continue;
180 
181  const HeaderFileInfo *HFI =
182  HS.getExistingFileInfo(File, /*WantExternal*/ false);
183  if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
184  continue;
185 
186  for (const auto &KH : HS.findAllModulesForHeader(File)) {
187  if (!KH.getModule())
188  continue;
189  ModulesToProcess.push_back(KH.getModule());
190  }
191  }
192 
193  while (!ModulesToProcess.empty()) {
194  auto *CurrentModule = ModulesToProcess.pop_back_val();
195  ProcessedModules.insert(CurrentModule);
196 
197  Optional<FileEntryRef> ModuleMapFile =
198  HS.getModuleMap().getModuleMapFileForUniquing(CurrentModule);
199  if (!ModuleMapFile) {
200  continue;
201  }
202 
203  ModuleMaps.insert(*ModuleMapFile);
204 
205  for (auto *ImportedModule : (CurrentModule)->Imports) {
206  if (!ImportedModule ||
207  ProcessedModules.find(ImportedModule) != ProcessedModules.end()) {
208  continue;
209  }
210  ModulesToProcess.push_back(ImportedModule);
211  }
212 
213  for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses)
214  if (UndeclaredModule &&
215  ProcessedModules.find(UndeclaredModule) == ProcessedModules.end())
216  ModulesToProcess.push_back(UndeclaredModule);
217  }
218 
219  return ModuleMaps;
220 }
221 
222 class ASTTypeWriter {
223  ASTWriter &Writer;
224  ASTWriter::RecordData Record;
225  ASTRecordWriter BasicWriter;
226 
227 public:
228  ASTTypeWriter(ASTWriter &Writer)
229  : Writer(Writer), BasicWriter(Writer, Record) {}
230 
231  uint64_t write(QualType T) {
232  if (T.hasLocalNonFastQualifiers()) {
234  BasicWriter.writeQualType(T.getLocalUnqualifiedType());
235  BasicWriter.writeQualifiers(Qs);
236  return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
237  }
238 
239  const Type *typePtr = T.getTypePtr();
241  atw.write(typePtr);
242  return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
243  /*abbrev*/ 0);
244  }
245 };
246 
247 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
248  using LocSeq = SourceLocationSequence;
249 
250  ASTRecordWriter &Record;
251  LocSeq *Seq;
252 
253  void addSourceLocation(SourceLocation Loc) {
254  Record.AddSourceLocation(Loc, Seq);
255  }
256  void addSourceRange(SourceRange Range) { Record.AddSourceRange(Range, Seq); }
257 
258 public:
259  TypeLocWriter(ASTRecordWriter &Record, LocSeq *Seq)
260  : Record(Record), Seq(Seq) {}
261 
262 #define ABSTRACT_TYPELOC(CLASS, PARENT)
263 #define TYPELOC(CLASS, PARENT) \
264  void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
265 #include "clang/AST/TypeLocNodes.def"
266 
267  void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
268  void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
269 };
270 
271 } // namespace
272 
273 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
274  // nothing to do
275 }
276 
277 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
278  addSourceLocation(TL.getBuiltinLoc());
279  if (TL.needsExtraLocalData()) {
280  Record.push_back(TL.getWrittenTypeSpec());
281  Record.push_back(static_cast<uint64_t>(TL.getWrittenSignSpec()));
282  Record.push_back(static_cast<uint64_t>(TL.getWrittenWidthSpec()));
283  Record.push_back(TL.hasModeAttr());
284  }
285 }
286 
287 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
288  addSourceLocation(TL.getNameLoc());
289 }
290 
291 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
292  addSourceLocation(TL.getStarLoc());
293 }
294 
295 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
296  // nothing to do
297 }
298 
299 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
300  // nothing to do
301 }
302 
303 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
304  addSourceLocation(TL.getCaretLoc());
305 }
306 
307 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
308  addSourceLocation(TL.getAmpLoc());
309 }
310 
311 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
312  addSourceLocation(TL.getAmpAmpLoc());
313 }
314 
315 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
316  addSourceLocation(TL.getStarLoc());
317  Record.AddTypeSourceInfo(TL.getClassTInfo());
318 }
319 
320 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
321  addSourceLocation(TL.getLBracketLoc());
322  addSourceLocation(TL.getRBracketLoc());
323  Record.push_back(TL.getSizeExpr() ? 1 : 0);
324  if (TL.getSizeExpr())
325  Record.AddStmt(TL.getSizeExpr());
326 }
327 
328 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
329  VisitArrayTypeLoc(TL);
330 }
331 
332 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
333  VisitArrayTypeLoc(TL);
334 }
335 
336 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
337  VisitArrayTypeLoc(TL);
338 }
339 
340 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
342  VisitArrayTypeLoc(TL);
343 }
344 
345 void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
347  addSourceLocation(TL.getAttrNameLoc());
349  addSourceLocation(range.getBegin());
350  addSourceLocation(range.getEnd());
351  Record.AddStmt(TL.getAttrExprOperand());
352 }
353 
354 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
356  addSourceLocation(TL.getNameLoc());
357 }
358 
359 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
360  addSourceLocation(TL.getNameLoc());
361 }
362 
363 void TypeLocWriter::VisitDependentVectorTypeLoc(
365  addSourceLocation(TL.getNameLoc());
366 }
367 
368 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
369  addSourceLocation(TL.getNameLoc());
370 }
371 
372 void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
373  addSourceLocation(TL.getAttrNameLoc());
375  addSourceLocation(range.getBegin());
376  addSourceLocation(range.getEnd());
377  Record.AddStmt(TL.getAttrRowOperand());
378  Record.AddStmt(TL.getAttrColumnOperand());
379 }
380 
381 void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
383  addSourceLocation(TL.getAttrNameLoc());
385  addSourceLocation(range.getBegin());
386  addSourceLocation(range.getEnd());
387  Record.AddStmt(TL.getAttrRowOperand());
388  Record.AddStmt(TL.getAttrColumnOperand());
389 }
390 
391 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
392  addSourceLocation(TL.getLocalRangeBegin());
393  addSourceLocation(TL.getLParenLoc());
394  addSourceLocation(TL.getRParenLoc());
395  addSourceRange(TL.getExceptionSpecRange());
396  addSourceLocation(TL.getLocalRangeEnd());
397  for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
398  Record.AddDeclRef(TL.getParam(i));
399 }
400 
401 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
402  VisitFunctionTypeLoc(TL);
403 }
404 
405 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
406  VisitFunctionTypeLoc(TL);
407 }
408 
409 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
410  addSourceLocation(TL.getNameLoc());
411 }
412 
413 void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) {
414  addSourceLocation(TL.getNameLoc());
415 }
416 
417 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
418  addSourceLocation(TL.getNameLoc());
419 }
420 
421 void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
422  if (TL.getNumProtocols()) {
423  addSourceLocation(TL.getProtocolLAngleLoc());
424  addSourceLocation(TL.getProtocolRAngleLoc());
425  }
426  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
427  addSourceLocation(TL.getProtocolLoc(i));
428 }
429 
430 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
431  addSourceLocation(TL.getTypeofLoc());
432  addSourceLocation(TL.getLParenLoc());
433  addSourceLocation(TL.getRParenLoc());
434 }
435 
436 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
437  addSourceLocation(TL.getTypeofLoc());
438  addSourceLocation(TL.getLParenLoc());
439  addSourceLocation(TL.getRParenLoc());
441 }
442 
443 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
444  addSourceLocation(TL.getDecltypeLoc());
445  addSourceLocation(TL.getRParenLoc());
446 }
447 
448 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
449  addSourceLocation(TL.getKWLoc());
450  addSourceLocation(TL.getLParenLoc());
451  addSourceLocation(TL.getRParenLoc());
453 }
454 
455 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
456  addSourceLocation(TL.getNameLoc());
457  Record.push_back(TL.isConstrained());
458  if (TL.isConstrained()) {
460  addSourceLocation(TL.getTemplateKWLoc());
461  addSourceLocation(TL.getConceptNameLoc());
462  Record.AddDeclRef(TL.getFoundDecl());
463  addSourceLocation(TL.getLAngleLoc());
464  addSourceLocation(TL.getRAngleLoc());
465  for (unsigned I = 0; I < TL.getNumArgs(); ++I)
467  TL.getTypePtr()->getTypeConstraintArguments()[I].getKind(),
468  TL.getArgLocInfo(I));
469  }
470  Record.push_back(TL.isDecltypeAuto());
471  if (TL.isDecltypeAuto())
472  addSourceLocation(TL.getRParenLoc());
473 }
474 
475 void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
477  addSourceLocation(TL.getTemplateNameLoc());
478 }
479 
480 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
481  addSourceLocation(TL.getNameLoc());
482 }
483 
484 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
485  addSourceLocation(TL.getNameLoc());
486 }
487 
488 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
489  Record.AddAttr(TL.getAttr());
490 }
491 
492 void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
493  // Nothing to do.
494 }
495 
496 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
497  addSourceLocation(TL.getNameLoc());
498 }
499 
500 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
502  addSourceLocation(TL.getNameLoc());
503 }
504 
505 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
507  addSourceLocation(TL.getNameLoc());
508 }
509 
510 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
512  addSourceLocation(TL.getTemplateKeywordLoc());
513  addSourceLocation(TL.getTemplateNameLoc());
514  addSourceLocation(TL.getLAngleLoc());
515  addSourceLocation(TL.getRAngleLoc());
516  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
518  TL.getArgLoc(i).getLocInfo());
519 }
520 
521 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
522  addSourceLocation(TL.getLParenLoc());
523  addSourceLocation(TL.getRParenLoc());
524 }
525 
526 void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
527  addSourceLocation(TL.getExpansionLoc());
528 }
529 
530 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
531  addSourceLocation(TL.getElaboratedKeywordLoc());
533 }
534 
535 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
536  addSourceLocation(TL.getNameLoc());
537 }
538 
539 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
540  addSourceLocation(TL.getElaboratedKeywordLoc());
542  addSourceLocation(TL.getNameLoc());
543 }
544 
545 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
547  addSourceLocation(TL.getElaboratedKeywordLoc());
549  addSourceLocation(TL.getTemplateKeywordLoc());
550  addSourceLocation(TL.getTemplateNameLoc());
551  addSourceLocation(TL.getLAngleLoc());
552  addSourceLocation(TL.getRAngleLoc());
553  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
555  TL.getArgLoc(I).getLocInfo());
556 }
557 
558 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
559  addSourceLocation(TL.getEllipsisLoc());
560 }
561 
562 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
563  addSourceLocation(TL.getNameLoc());
564 }
565 
566 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
567  Record.push_back(TL.hasBaseTypeAsWritten());
568  addSourceLocation(TL.getTypeArgsLAngleLoc());
569  addSourceLocation(TL.getTypeArgsRAngleLoc());
570  for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
571  Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
572  addSourceLocation(TL.getProtocolLAngleLoc());
573  addSourceLocation(TL.getProtocolRAngleLoc());
574  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
575  addSourceLocation(TL.getProtocolLoc(i));
576 }
577 
578 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
579  addSourceLocation(TL.getStarLoc());
580 }
581 
582 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
583  addSourceLocation(TL.getKWLoc());
584  addSourceLocation(TL.getLParenLoc());
585  addSourceLocation(TL.getRParenLoc());
586 }
587 
588 void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
589  addSourceLocation(TL.getKWLoc());
590 }
591 
592 void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
593  addSourceLocation(TL.getNameLoc());
594 }
595 void TypeLocWriter::VisitDependentBitIntTypeLoc(
597  addSourceLocation(TL.getNameLoc());
598 }
599 
600 void ASTWriter::WriteTypeAbbrevs() {
601  using namespace llvm;
602 
603  std::shared_ptr<BitCodeAbbrev> Abv;
604 
605  // Abbreviation for TYPE_EXT_QUAL
606  Abv = std::make_shared<BitCodeAbbrev>();
607  Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
608  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
609  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
610  TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
611 }
612 
613 //===----------------------------------------------------------------------===//
614 // ASTWriter Implementation
615 //===----------------------------------------------------------------------===//
616 
617 static void EmitBlockID(unsigned ID, const char *Name,
618  llvm::BitstreamWriter &Stream,
619  ASTWriter::RecordDataImpl &Record) {
620  Record.clear();
621  Record.push_back(ID);
622  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
623 
624  // Emit the block name if present.
625  if (!Name || Name[0] == 0)
626  return;
627  Record.clear();
628  while (*Name)
629  Record.push_back(*Name++);
630  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
631 }
632 
633 static void EmitRecordID(unsigned ID, const char *Name,
634  llvm::BitstreamWriter &Stream,
635  ASTWriter::RecordDataImpl &Record) {
636  Record.clear();
637  Record.push_back(ID);
638  while (*Name)
639  Record.push_back(*Name++);
640  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
641 }
642 
643 static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
644  ASTWriter::RecordDataImpl &Record) {
645 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
646  RECORD(STMT_STOP);
649  RECORD(STMT_NULL);
651  RECORD(STMT_CASE);
655  RECORD(STMT_IF);
658  RECORD(STMT_DO);
659  RECORD(STMT_FOR);
660  RECORD(STMT_GOTO);
665  RECORD(STMT_DECL);
681  RECORD(EXPR_CALL);
697  RECORD(EXPR_STMT);
772 #undef RECORD
773 }
774 
775 void ASTWriter::WriteBlockInfoBlock() {
776  RecordData Record;
777  Stream.EnterBlockInfoBlock();
778 
779 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
780 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
781 
782  // Control Block.
783  BLOCK(CONTROL_BLOCK);
784  RECORD(METADATA);
788  RECORD(IMPORTS);
792 
793  BLOCK(OPTIONS_BLOCK);
799 
800  BLOCK(INPUT_FILES_BLOCK);
803 
804  // AST Top-Level Block.
805  BLOCK(AST_BLOCK);
861 
862  // SourceManager Block.
863  BLOCK(SOURCE_MANAGER_BLOCK);
869 
870  // Preprocessor Block.
871  BLOCK(PREPROCESSOR_BLOCK);
876  RECORD(PP_TOKEN);
877 
878  // Submodule Block.
879  BLOCK(SUBMODULE_BLOCK);
899 
900  // Comments Block.
901  BLOCK(COMMENTS_BLOCK);
903 
904  // Decls and Types block.
905  BLOCK(DECLTYPES_BLOCK);
907  RECORD(TYPE_COMPLEX);
908  RECORD(TYPE_POINTER);
909  RECORD(TYPE_BLOCK_POINTER);
910  RECORD(TYPE_LVALUE_REFERENCE);
911  RECORD(TYPE_RVALUE_REFERENCE);
912  RECORD(TYPE_MEMBER_POINTER);
913  RECORD(TYPE_CONSTANT_ARRAY);
914  RECORD(TYPE_INCOMPLETE_ARRAY);
915  RECORD(TYPE_VARIABLE_ARRAY);
916  RECORD(TYPE_VECTOR);
917  RECORD(TYPE_EXT_VECTOR);
918  RECORD(TYPE_FUNCTION_NO_PROTO);
919  RECORD(TYPE_FUNCTION_PROTO);
920  RECORD(TYPE_TYPEDEF);
921  RECORD(TYPE_TYPEOF_EXPR);
922  RECORD(TYPE_TYPEOF);
923  RECORD(TYPE_RECORD);
924  RECORD(TYPE_ENUM);
925  RECORD(TYPE_OBJC_INTERFACE);
926  RECORD(TYPE_OBJC_OBJECT_POINTER);
927  RECORD(TYPE_DECLTYPE);
928  RECORD(TYPE_ELABORATED);
929  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
930  RECORD(TYPE_UNRESOLVED_USING);
931  RECORD(TYPE_INJECTED_CLASS_NAME);
932  RECORD(TYPE_OBJC_OBJECT);
933  RECORD(TYPE_TEMPLATE_TYPE_PARM);
934  RECORD(TYPE_TEMPLATE_SPECIALIZATION);
935  RECORD(TYPE_DEPENDENT_NAME);
936  RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
937  RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
938  RECORD(TYPE_PAREN);
939  RECORD(TYPE_MACRO_QUALIFIED);
940  RECORD(TYPE_PACK_EXPANSION);
941  RECORD(TYPE_ATTRIBUTED);
942  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
943  RECORD(TYPE_AUTO);
944  RECORD(TYPE_UNARY_TRANSFORM);
945  RECORD(TYPE_ATOMIC);
946  RECORD(TYPE_DECAYED);
947  RECORD(TYPE_ADJUSTED);
948  RECORD(TYPE_OBJC_TYPE_PARAM);
952  RECORD(DECL_ENUM);
969  RECORD(DECL_VAR);
1014  RECORD(DECL_EMPTY);
1022 
1023  // Statements and Exprs can occur in the Decls and Types block.
1024  AddStmtsExprs(Stream, Record);
1025 
1026  BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1030 
1031  // Decls and Types block.
1032  BLOCK(EXTENSION_BLOCK);
1034 
1035  BLOCK(UNHASHED_CONTROL_BLOCK);
1036  RECORD(SIGNATURE);
1040 
1041 #undef RECORD
1042 #undef BLOCK
1043  Stream.ExitBlock();
1044 }
1045 
1046 /// Prepares a path for being written to an AST file by converting it
1047 /// to an absolute path and removing nested './'s.
1048 ///
1049 /// \return \c true if the path was changed.
1050 static bool cleanPathForOutput(FileManager &FileMgr,
1051  SmallVectorImpl<char> &Path) {
1052  bool Changed = FileMgr.makeAbsolutePath(Path);
1053  return Changed | llvm::sys::path::remove_dots(Path);
1054 }
1055 
1056 /// Adjusts the given filename to only write out the portion of the
1057 /// filename that is not part of the system root directory.
1058 ///
1059 /// \param Filename the file name to adjust.
1060 ///
1061 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1062 /// the returned filename will be adjusted by this root directory.
1063 ///
1064 /// \returns either the original filename (if it needs no adjustment) or the
1065 /// adjusted filename (which points into the @p Filename parameter).
1066 static const char *
1067 adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1068  assert(Filename && "No file name to adjust?");
1069 
1070  if (BaseDir.empty())
1071  return Filename;
1072 
1073  // Verify that the filename and the system root have the same prefix.
1074  unsigned Pos = 0;
1075  for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1076  if (Filename[Pos] != BaseDir[Pos])
1077  return Filename; // Prefixes don't match.
1078 
1079  // We hit the end of the filename before we hit the end of the system root.
1080  if (!Filename[Pos])
1081  return Filename;
1082 
1083  // If there's not a path separator at the end of the base directory nor
1084  // immediately after it, then this isn't within the base directory.
1085  if (!llvm::sys::path::is_separator(Filename[Pos])) {
1086  if (!llvm::sys::path::is_separator(BaseDir.back()))
1087  return Filename;
1088  } else {
1089  // If the file name has a '/' at the current position, skip over the '/'.
1090  // We distinguish relative paths from absolute paths by the
1091  // absence of '/' at the beginning of relative paths.
1092  //
1093  // FIXME: This is wrong. We distinguish them by asking if the path is
1094  // absolute, which isn't the same thing. And there might be multiple '/'s
1095  // in a row. Use a better mechanism to indicate whether we have emitted an
1096  // absolute or relative path.
1097  ++Pos;
1098  }
1099 
1100  return Filename + Pos;
1101 }
1102 
1103 std::pair<ASTFileSignature, ASTFileSignature>
1104 ASTWriter::createSignature(StringRef AllBytes, StringRef ASTBlockBytes) {
1105  llvm::SHA1 Hasher;
1106  Hasher.update(ASTBlockBytes);
1107  ASTFileSignature ASTBlockHash = ASTFileSignature::create(Hasher.result());
1108 
1109  // Add the remaining bytes (i.e. bytes before the unhashed control block that
1110  // are not part of the AST block).
1111  Hasher.update(
1112  AllBytes.take_front(ASTBlockBytes.bytes_end() - AllBytes.bytes_begin()));
1113  Hasher.update(
1114  AllBytes.take_back(AllBytes.bytes_end() - ASTBlockBytes.bytes_end()));
1115  ASTFileSignature Signature = ASTFileSignature::create(Hasher.result());
1116 
1117  return std::make_pair(ASTBlockHash, Signature);
1118 }
1119 
1120 ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1121  ASTContext &Context) {
1122  using namespace llvm;
1123 
1124  // Flush first to prepare the PCM hash (signature).
1125  Stream.FlushToWord();
1126  auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1127 
1128  // Enter the block and prepare to write records.
1129  RecordData Record;
1130  Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1131 
1132  // For implicit modules, write the hash of the PCM as its signature.
1133  ASTFileSignature Signature;
1134  if (WritingModule &&
1136  ASTFileSignature ASTBlockHash;
1137  auto ASTBlockStartByte = ASTBlockRange.first >> 3;
1138  auto ASTBlockByteLength = (ASTBlockRange.second >> 3) - ASTBlockStartByte;
1139  std::tie(ASTBlockHash, Signature) = createSignature(
1140  StringRef(Buffer.begin(), StartOfUnhashedControl),
1141  StringRef(Buffer.begin() + ASTBlockStartByte, ASTBlockByteLength));
1142 
1143  Record.append(ASTBlockHash.begin(), ASTBlockHash.end());
1144  Stream.EmitRecord(AST_BLOCK_HASH, Record);
1145  Record.clear();
1146  Record.append(Signature.begin(), Signature.end());
1147  Stream.EmitRecord(SIGNATURE, Record);
1148  Record.clear();
1149  }
1150 
1151  // Diagnostic options.
1152  const auto &Diags = Context.getDiagnostics();
1153  const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1154 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1155 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1156  Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1157 #include "clang/Basic/DiagnosticOptions.def"
1158  Record.push_back(DiagOpts.Warnings.size());
1159  for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1160  AddString(DiagOpts.Warnings[I], Record);
1161  Record.push_back(DiagOpts.Remarks.size());
1162  for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1163  AddString(DiagOpts.Remarks[I], Record);
1164  // Note: we don't serialize the log or serialization file names, because they
1165  // are generally transient files and will almost always be overridden.
1166  Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1167  Record.clear();
1168 
1169  // Write out the diagnostic/pragma mappings.
1170  WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1171 
1172  // Header search entry usage.
1173  auto HSEntryUsage = PP.getHeaderSearchInfo().computeUserEntryUsage();
1174  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1175  Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1176  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1177  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1178  unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1179  {
1180  RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1181  HSEntryUsage.size()};
1182  Stream.EmitRecordWithBlob(HSUsageAbbrevCode, Record, bytes(HSEntryUsage));
1183  }
1184 
1185  // Leave the options block.
1186  Stream.ExitBlock();
1187  return Signature;
1188 }
1189 
1190 /// Write the control block.
1191 void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1192  StringRef isysroot) {
1193  using namespace llvm;
1194 
1195  Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1196  RecordData Record;
1197 
1198  // Metadata
1199  auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1200  MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1201  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1202  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1203  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1204  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1205  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1206  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1207  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1208  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1209  unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1210  assert((!WritingModule || isysroot.empty()) &&
1211  "writing module as a relocatable PCH?");
1212  {
1213  RecordData::value_type Record[] = {
1214  METADATA,
1215  VERSION_MAJOR,
1216  VERSION_MINOR,
1217  CLANG_VERSION_MAJOR,
1218  CLANG_VERSION_MINOR,
1219  !isysroot.empty(),
1220  IncludeTimestamps,
1221  ASTHasCompilerErrors};
1222  Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1224  }
1225 
1226  if (WritingModule) {
1227  // Module name
1228  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1229  Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1230  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1231  unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1232  RecordData::value_type Record[] = {MODULE_NAME};
1233  Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1234  }
1235 
1236  if (WritingModule && WritingModule->Directory) {
1237  SmallString<128> BaseDir;
1239  // Use the current working directory as the base path for all inputs.
1240  auto *CWD =
1241  Context.getSourceManager().getFileManager().getDirectory(".").get();
1242  BaseDir.assign(CWD->getName());
1243  } else {
1244  BaseDir.assign(WritingModule->Directory->getName());
1245  }
1246  cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1247 
1248  // If the home of the module is the current working directory, then we
1249  // want to pick up the cwd of the build process loading the module, not
1250  // our cwd, when we load this module.
1251  if (!(PP.getHeaderSearchInfo()
1255  WritingModule->Directory->getName() != StringRef(".")) {
1256  // Module directory.
1257  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1258  Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1259  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1260  unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1261 
1262  RecordData::value_type Record[] = {MODULE_DIRECTORY};
1263  Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1264  }
1265 
1266  // Write out all other paths relative to the base directory if possible.
1267  BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1268  } else if (!isysroot.empty()) {
1269  // Write out paths relative to the sysroot if possible.
1270  BaseDirectory = std::string(isysroot);
1271  }
1272 
1273  // Module map file
1274  if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1275  Record.clear();
1276 
1277  auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1278  AddPath(WritingModule->PresumedModuleMapFile.empty()
1279  ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1280  : StringRef(WritingModule->PresumedModuleMapFile),
1281  Record);
1282 
1283  // Additional module map files.
1284  if (auto *AdditionalModMaps =
1285  Map.getAdditionalModuleMapFiles(WritingModule)) {
1286  Record.push_back(AdditionalModMaps->size());
1287  SmallVector<const FileEntry *, 1> ModMaps(AdditionalModMaps->begin(),
1288  AdditionalModMaps->end());
1289  llvm::sort(ModMaps, [](const FileEntry *A, const FileEntry *B) {
1290  return A->getName() < B->getName();
1291  });
1292  for (const FileEntry *F : ModMaps)
1293  AddPath(F->getName(), Record);
1294  } else {
1295  Record.push_back(0);
1296  }
1297 
1298  Stream.EmitRecord(MODULE_MAP_FILE, Record);
1299  }
1300 
1301  // Imports
1302  if (Chain) {
1303  serialization::ModuleManager &Mgr = Chain->getModuleManager();
1304  Record.clear();
1305 
1306  for (ModuleFile &M : Mgr) {
1307  // Skip modules that weren't directly imported.
1308  if (!M.isDirectlyImported())
1309  continue;
1310 
1311  Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1312  AddSourceLocation(M.ImportLoc, Record);
1313 
1314  // If we have calculated signature, there is no need to store
1315  // the size or timestamp.
1316  Record.push_back(M.Signature ? 0 : M.File->getSize());
1317  Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1318 
1319  llvm::append_range(Record, M.Signature);
1320 
1321  AddString(M.ModuleName, Record);
1322  AddPath(M.FileName, Record);
1323  }
1324  Stream.EmitRecord(IMPORTS, Record);
1325  }
1326 
1327  // Write the options block.
1328  Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1329 
1330  // Language options.
1331  Record.clear();
1332  const LangOptions &LangOpts = Context.getLangOpts();
1333 #define LANGOPT(Name, Bits, Default, Description) \
1334  Record.push_back(LangOpts.Name);
1335 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1336  Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1337 #include "clang/Basic/LangOptions.def"
1338 #define SANITIZER(NAME, ID) \
1339  Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1340 #include "clang/Basic/Sanitizers.def"
1341 
1342  Record.push_back(LangOpts.ModuleFeatures.size());
1343  for (StringRef Feature : LangOpts.ModuleFeatures)
1344  AddString(Feature, Record);
1345 
1346  Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1347  AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1348 
1349  AddString(LangOpts.CurrentModule, Record);
1350 
1351  // Comment options.
1352  Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1353  for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1354  AddString(I, Record);
1355  }
1356  Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1357 
1358  // OpenMP offloading options.
1359  Record.push_back(LangOpts.OMPTargetTriples.size());
1360  for (auto &T : LangOpts.OMPTargetTriples)
1361  AddString(T.getTriple(), Record);
1362 
1363  AddString(LangOpts.OMPHostIRFile, Record);
1364 
1365  Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1366 
1367  // Target options.
1368  Record.clear();
1369  const TargetInfo &Target = Context.getTargetInfo();
1370  const TargetOptions &TargetOpts = Target.getTargetOpts();
1371  AddString(TargetOpts.Triple, Record);
1372  AddString(TargetOpts.CPU, Record);
1373  AddString(TargetOpts.TuneCPU, Record);
1374  AddString(TargetOpts.ABI, Record);
1375  Record.push_back(TargetOpts.FeaturesAsWritten.size());
1376  for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1377  AddString(TargetOpts.FeaturesAsWritten[I], Record);
1378  }
1379  Record.push_back(TargetOpts.Features.size());
1380  for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1381  AddString(TargetOpts.Features[I], Record);
1382  }
1383  Stream.EmitRecord(TARGET_OPTIONS, Record);
1384 
1385  // File system options.
1386  Record.clear();
1387  const FileSystemOptions &FSOpts =
1389  AddString(FSOpts.WorkingDir, Record);
1390  Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1391 
1392  // Header search options.
1393  Record.clear();
1394  const HeaderSearchOptions &HSOpts
1396  AddString(HSOpts.Sysroot, Record);
1397 
1398  // Include entries.
1399  Record.push_back(HSOpts.UserEntries.size());
1400  for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1401  const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1402  AddString(Entry.Path, Record);
1403  Record.push_back(static_cast<unsigned>(Entry.Group));
1404  Record.push_back(Entry.IsFramework);
1405  Record.push_back(Entry.IgnoreSysRoot);
1406  }
1407 
1408  // System header prefixes.
1409  Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1410  for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1411  AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1412  Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1413  }
1414 
1415  AddString(HSOpts.ResourceDir, Record);
1416  AddString(HSOpts.ModuleCachePath, Record);
1417  AddString(HSOpts.ModuleUserBuildPath, Record);
1418  Record.push_back(HSOpts.DisableModuleHash);
1419  Record.push_back(HSOpts.ImplicitModuleMaps);
1420  Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1421  Record.push_back(HSOpts.EnablePrebuiltImplicitModules);
1422  Record.push_back(HSOpts.UseBuiltinIncludes);
1423  Record.push_back(HSOpts.UseStandardSystemIncludes);
1424  Record.push_back(HSOpts.UseStandardCXXIncludes);
1425  Record.push_back(HSOpts.UseLibcxx);
1426  // Write out the specific module cache path that contains the module files.
1427  AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1428  Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1429 
1430  // Preprocessor options.
1431  Record.clear();
1432  const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1433 
1434  // Macro definitions.
1435  Record.push_back(PPOpts.Macros.size());
1436  for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1437  AddString(PPOpts.Macros[I].first, Record);
1438  Record.push_back(PPOpts.Macros[I].second);
1439  }
1440 
1441  // Includes
1442  Record.push_back(PPOpts.Includes.size());
1443  for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1444  AddString(PPOpts.Includes[I], Record);
1445 
1446  // Macro includes
1447  Record.push_back(PPOpts.MacroIncludes.size());
1448  for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1449  AddString(PPOpts.MacroIncludes[I], Record);
1450 
1451  Record.push_back(PPOpts.UsePredefines);
1452  // Detailed record is important since it is used for the module cache hash.
1453  Record.push_back(PPOpts.DetailedRecord);
1454  AddString(PPOpts.ImplicitPCHInclude, Record);
1455  Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1456  Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1457 
1458  // Leave the options block.
1459  Stream.ExitBlock();
1460 
1461  // Original file name and file ID
1462  SourceManager &SM = Context.getSourceManager();
1463  if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1464  auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1465  FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1466  FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1467  FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1468  unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1469 
1470  Record.clear();
1471  Record.push_back(ORIGINAL_FILE);
1472  AddFileID(SM.getMainFileID(), Record);
1473  EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1474  }
1475 
1476  Record.clear();
1477  AddFileID(SM.getMainFileID(), Record);
1478  Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1479 
1480  WriteInputFiles(Context.SourceMgr,
1482  Stream.ExitBlock();
1483 }
1484 
1485 namespace {
1486 
1487 /// An input file.
1488 struct InputFileEntry {
1490  bool IsSystemFile;
1491  bool IsTransient;
1492  bool BufferOverridden;
1493  bool IsTopLevelModuleMap;
1494  uint32_t ContentHash[2];
1495 
1496  InputFileEntry(FileEntryRef File) : File(File) {}
1497 };
1498 
1499 } // namespace
1500 
1501 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1502  HeaderSearchOptions &HSOpts) {
1503  using namespace llvm;
1504 
1505  Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1506 
1507  // Create input-file abbreviation.
1508  auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1509  IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1510  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1511  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1512  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1513  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1514  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1515  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1516  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1517  unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1518 
1519  // Create input file hash abbreviation.
1520  auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1521  IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1522  IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1523  IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1524  unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1525 
1526  // Get all ContentCache objects for files.
1527  std::vector<InputFileEntry> UserFiles;
1528  std::vector<InputFileEntry> SystemFiles;
1529  for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1530  // Get this source location entry.
1531  const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1532  assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1533 
1534  // We only care about file entries that were not overridden.
1535  if (!SLoc->isFile())
1536  continue;
1537  const SrcMgr::FileInfo &File = SLoc->getFile();
1538  const SrcMgr::ContentCache *Cache = &File.getContentCache();
1539  if (!Cache->OrigEntry)
1540  continue;
1541 
1542  // Do not emit input files that do not affect current module.
1543  if (!IsSLocAffecting[I])
1544  continue;
1545 
1546  InputFileEntry Entry(*Cache->OrigEntry);
1547  Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1548  Entry.IsTransient = Cache->IsTransient;
1549  Entry.BufferOverridden = Cache->BufferOverridden;
1550  Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1551  File.getIncludeLoc().isInvalid();
1552 
1553  auto ContentHash = hash_code(-1);
1554  if (PP->getHeaderSearchInfo()
1557  auto MemBuff = Cache->getBufferIfLoaded();
1558  if (MemBuff)
1559  ContentHash = hash_value(MemBuff->getBuffer());
1560  else
1561  PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1562  << Entry.File.getName();
1563  }
1564  auto CH = llvm::APInt(64, ContentHash);
1565  Entry.ContentHash[0] =
1566  static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1567  Entry.ContentHash[1] =
1568  static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1569 
1570  if (Entry.IsSystemFile)
1571  SystemFiles.push_back(Entry);
1572  else
1573  UserFiles.push_back(Entry);
1574  }
1575 
1576  // User files go at the front, system files at the back.
1577  auto SortedFiles = llvm::concat<InputFileEntry>(std::move(UserFiles),
1578  std::move(SystemFiles));
1579 
1580  unsigned UserFilesNum = 0;
1581  // Write out all of the input files.
1582  std::vector<uint64_t> InputFileOffsets;
1583  for (const auto &Entry : SortedFiles) {
1584  uint32_t &InputFileID = InputFileIDs[Entry.File];
1585  if (InputFileID != 0)
1586  continue; // already recorded this file.
1587 
1588  // Record this entry's offset.
1589  InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1590 
1591  InputFileID = InputFileOffsets.size();
1592 
1593  if (!Entry.IsSystemFile)
1594  ++UserFilesNum;
1595 
1596  // Emit size/modification time for this file.
1597  // And whether this file was overridden.
1598  {
1599  RecordData::value_type Record[] = {
1600  INPUT_FILE,
1601  InputFileOffsets.size(),
1602  (uint64_t)Entry.File.getSize(),
1603  (uint64_t)getTimestampForOutput(Entry.File),
1604  Entry.BufferOverridden,
1605  Entry.IsTransient,
1606  Entry.IsTopLevelModuleMap};
1607 
1608  EmitRecordWithPath(IFAbbrevCode, Record, Entry.File.getName());
1609  }
1610 
1611  // Emit content hash for this file.
1612  {
1613  RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1614  Entry.ContentHash[1]};
1615  Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1616  }
1617  }
1618 
1619  Stream.ExitBlock();
1620 
1621  // Create input file offsets abbreviation.
1622  auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1623  OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1624  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1625  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1626  // input files
1627  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1628  unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1629 
1630  // Write input file offsets.
1631  RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1632  InputFileOffsets.size(), UserFilesNum};
1633  Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1634 }
1635 
1636 //===----------------------------------------------------------------------===//
1637 // Source Manager Serialization
1638 //===----------------------------------------------------------------------===//
1639 
1640 /// Create an abbreviation for the SLocEntry that refers to a
1641 /// file.
1642 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1643  using namespace llvm;
1644 
1645  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1646  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1647  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1648  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1649  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1650  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1651  // FileEntry fields.
1652  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1653  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1654  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1655  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1656  return Stream.EmitAbbrev(std::move(Abbrev));
1657 }
1658 
1659 /// Create an abbreviation for the SLocEntry that refers to a
1660 /// buffer.
1661 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1662  using namespace llvm;
1663 
1664  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1665  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1666  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1667  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1668  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1669  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1670  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1671  return Stream.EmitAbbrev(std::move(Abbrev));
1672 }
1673 
1674 /// Create an abbreviation for the SLocEntry that refers to a
1675 /// buffer's blob.
1676 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1677  bool Compressed) {
1678  using namespace llvm;
1679 
1680  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1681  Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1682  : SM_SLOC_BUFFER_BLOB));
1683  if (Compressed)
1684  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1685  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1686  return Stream.EmitAbbrev(std::move(Abbrev));
1687 }
1688 
1689 /// Create an abbreviation for the SLocEntry that refers to a macro
1690 /// expansion.
1691 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1692  using namespace llvm;
1693 
1694  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1695  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1696  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1697  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1698  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
1699  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
1700  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1701  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1702  return Stream.EmitAbbrev(std::move(Abbrev));
1703 }
1704 
1705 /// Emit key length and data length as ULEB-encoded data, and return them as a
1706 /// pair.
1707 static std::pair<unsigned, unsigned>
1708 emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
1709  llvm::encodeULEB128(KeyLen, Out);
1710  llvm::encodeULEB128(DataLen, Out);
1711  return std::make_pair(KeyLen, DataLen);
1712 }
1713 
1714 namespace {
1715 
1716  // Trait used for the on-disk hash table of header search information.
1717  class HeaderFileInfoTrait {
1718  ASTWriter &Writer;
1719 
1720  // Keep track of the framework names we've used during serialization.
1721  SmallString<128> FrameworkStringData;
1722  llvm::StringMap<unsigned> FrameworkNameOffset;
1723 
1724  public:
1725  HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1726 
1727  struct key_type {
1728  StringRef Filename;
1729  off_t Size;
1730  time_t ModTime;
1731  };
1732  using key_type_ref = const key_type &;
1733 
1734  using UnresolvedModule =
1735  llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1736 
1737  struct data_type {
1738  const HeaderFileInfo &HFI;
1739  ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1740  UnresolvedModule Unresolved;
1741  };
1742  using data_type_ref = const data_type &;
1743 
1744  using hash_value_type = unsigned;
1745  using offset_type = unsigned;
1746 
1747  hash_value_type ComputeHash(key_type_ref key) {
1748  // The hash is based only on size/time of the file, so that the reader can
1749  // match even when symlinking or excess path elements ("foo/../", "../")
1750  // change the form of the name. However, complete path is still the key.
1751  return llvm::hash_combine(key.Size, key.ModTime);
1752  }
1753 
1754  std::pair<unsigned, unsigned>
1755  EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1756  unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
1757  unsigned DataLen = 1 + 4 + 4;
1758  for (auto ModInfo : Data.KnownHeaders)
1759  if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1760  DataLen += 4;
1761  if (Data.Unresolved.getPointer())
1762  DataLen += 4;
1763  return emitULEBKeyDataLength(KeyLen, DataLen, Out);
1764  }
1765 
1766  void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1767  using namespace llvm::support;
1768 
1769  endian::Writer LE(Out, little);
1770  LE.write<uint64_t>(key.Size);
1771  KeyLen -= 8;
1772  LE.write<uint64_t>(key.ModTime);
1773  KeyLen -= 8;
1774  Out.write(key.Filename.data(), KeyLen);
1775  }
1776 
1777  void EmitData(raw_ostream &Out, key_type_ref key,
1778  data_type_ref Data, unsigned DataLen) {
1779  using namespace llvm::support;
1780 
1781  endian::Writer LE(Out, little);
1782  uint64_t Start = Out.tell(); (void)Start;
1783 
1784  unsigned char Flags = (Data.HFI.isImport << 5)
1785  | (Data.HFI.isPragmaOnce << 4)
1786  | (Data.HFI.DirInfo << 1)
1787  | Data.HFI.IndexHeaderMapHeader;
1788  LE.write<uint8_t>(Flags);
1789 
1790  if (!Data.HFI.ControllingMacro)
1791  LE.write<uint32_t>(Data.HFI.ControllingMacroID);
1792  else
1793  LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1794 
1795  unsigned Offset = 0;
1796  if (!Data.HFI.Framework.empty()) {
1797  // If this header refers into a framework, save the framework name.
1798  llvm::StringMap<unsigned>::iterator Pos
1799  = FrameworkNameOffset.find(Data.HFI.Framework);
1800  if (Pos == FrameworkNameOffset.end()) {
1801  Offset = FrameworkStringData.size() + 1;
1802  FrameworkStringData.append(Data.HFI.Framework);
1803  FrameworkStringData.push_back(0);
1804 
1805  FrameworkNameOffset[Data.HFI.Framework] = Offset;
1806  } else
1807  Offset = Pos->second;
1808  }
1809  LE.write<uint32_t>(Offset);
1810 
1811  auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1812  if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1813  uint32_t Value = (ModID << 3) | (unsigned)Role;
1814  assert((Value >> 3) == ModID && "overflow in header module info");
1815  LE.write<uint32_t>(Value);
1816  }
1817  };
1818 
1819  for (auto ModInfo : Data.KnownHeaders)
1820  EmitModule(ModInfo.getModule(), ModInfo.getRole());
1821  if (Data.Unresolved.getPointer())
1822  EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
1823 
1824  assert(Out.tell() - Start == DataLen && "Wrong data length");
1825  }
1826 
1827  const char *strings_begin() const { return FrameworkStringData.begin(); }
1828  const char *strings_end() const { return FrameworkStringData.end(); }
1829  };
1830 
1831 } // namespace
1832 
1833 /// Write the header search block for the list of files that
1834 ///
1835 /// \param HS The header search structure to save.
1836 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1837  HeaderFileInfoTrait GeneratorTrait(*this);
1838  llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1839  SmallVector<const char *, 4> SavedStrings;
1840  unsigned NumHeaderSearchEntries = 0;
1841 
1842  // Find all unresolved headers for the current module. We generally will
1843  // have resolved them before we get here, but not necessarily: we might be
1844  // compiling a preprocessed module, where there is no requirement for the
1845  // original files to exist any more.
1846  const HeaderFileInfo Empty; // So we can take a reference.
1847  if (WritingModule) {
1848  llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1849  while (!Worklist.empty()) {
1850  Module *M = Worklist.pop_back_val();
1851  // We don't care about headers in unimportable submodules.
1852  if (M->isUnimportable())
1853  continue;
1854 
1855  // Map to disk files where possible, to pick up any missing stat
1856  // information. This also means we don't need to check the unresolved
1857  // headers list when emitting resolved headers in the first loop below.
1858  // FIXME: It'd be preferable to avoid doing this if we were given
1859  // sufficient stat information in the module map.
1860  HS.getModuleMap().resolveHeaderDirectives(M, /*File=*/llvm::None);
1861 
1862  // If the file didn't exist, we can still create a module if we were given
1863  // enough information in the module map.
1864  for (auto U : M->MissingHeaders) {
1865  // Check that we were given enough information to build a module
1866  // without this file existing on disk.
1867  if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1868  PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1869  << WritingModule->getFullModuleName() << U.Size.has_value()
1870  << U.FileName;
1871  continue;
1872  }
1873 
1874  // Form the effective relative pathname for the file.
1876  llvm::sys::path::append(Filename, U.FileName);
1877  PreparePathForOutput(Filename);
1878 
1879  StringRef FilenameDup = strdup(Filename.c_str());
1880  SavedStrings.push_back(FilenameDup.data());
1881 
1882  HeaderFileInfoTrait::key_type Key = {
1883  FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
1884  };
1885  HeaderFileInfoTrait::data_type Data = {
1886  Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
1887  };
1888  // FIXME: Deal with cases where there are multiple unresolved header
1889  // directives in different submodules for the same header.
1890  Generator.insert(Key, Data, GeneratorTrait);
1891  ++NumHeaderSearchEntries;
1892  }
1893 
1894  Worklist.append(M->submodule_begin(), M->submodule_end());
1895  }
1896  }
1897 
1899  HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1900 
1901  if (FilesByUID.size() > HS.header_file_size())
1902  FilesByUID.resize(HS.header_file_size());
1903 
1904  for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1905  const FileEntry *File = FilesByUID[UID];
1906  if (!File)
1907  continue;
1908 
1909  // Get the file info. This will load info from the external source if
1910  // necessary. Skip emitting this file if we have no information on it
1911  // as a header file (in which case HFI will be null) or if it hasn't
1912  // changed since it was loaded. Also skip it if it's for a modular header
1913  // from a different module; in that case, we rely on the module(s)
1914  // containing the header to provide this information.
1915  const HeaderFileInfo *HFI =
1916  HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1917  if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
1918  continue;
1919 
1920  // Massage the file path into an appropriate form.
1921  StringRef Filename = File->getName();
1922  SmallString<128> FilenameTmp(Filename);
1923  if (PreparePathForOutput(FilenameTmp)) {
1924  // If we performed any translation on the file name at all, we need to
1925  // save this string, since the generator will refer to it later.
1926  Filename = StringRef(strdup(FilenameTmp.c_str()));
1927  SavedStrings.push_back(Filename.data());
1928  }
1929 
1930  HeaderFileInfoTrait::key_type Key = {
1931  Filename, File->getSize(), getTimestampForOutput(File)
1932  };
1933  HeaderFileInfoTrait::data_type Data = {
1934  *HFI, HS.getModuleMap().findResolvedModulesForHeader(File), {}
1935  };
1936  Generator.insert(Key, Data, GeneratorTrait);
1937  ++NumHeaderSearchEntries;
1938  }
1939 
1940  // Create the on-disk hash table in a buffer.
1941  SmallString<4096> TableData;
1942  uint32_t BucketOffset;
1943  {
1944  using namespace llvm::support;
1945 
1946  llvm::raw_svector_ostream Out(TableData);
1947  // Make sure that no bucket is at offset 0
1948  endian::write<uint32_t>(Out, 0, little);
1949  BucketOffset = Generator.Emit(Out, GeneratorTrait);
1950  }
1951 
1952  // Create a blob abbreviation
1953  using namespace llvm;
1954 
1955  auto Abbrev = std::make_shared<BitCodeAbbrev>();
1956  Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1957  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1958  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1959  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1960  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1961  unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1962 
1963  // Write the header search table
1964  RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1965  NumHeaderSearchEntries, TableData.size()};
1966  TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
1967  Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
1968 
1969  // Free all of the strings we had to duplicate.
1970  for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1971  free(const_cast<char *>(SavedStrings[I]));
1972 }
1973 
1974 static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
1975  unsigned SLocBufferBlobCompressedAbbrv,
1976  unsigned SLocBufferBlobAbbrv) {
1977  using RecordDataType = ASTWriter::RecordData::value_type;
1978 
1979  // Compress the buffer if possible. We expect that almost all PCM
1980  // consumers will not want its contents.
1981  SmallVector<uint8_t, 0> CompressedBuffer;
1982  if (llvm::compression::zstd::isAvailable()) {
1983  llvm::compression::zstd::compress(
1984  llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer, 9);
1985  RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
1986  Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
1987  llvm::toStringRef(CompressedBuffer));
1988  return;
1989  }
1990  if (llvm::compression::zlib::isAvailable()) {
1991  llvm::compression::zlib::compress(
1992  llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer);
1993  RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
1994  Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
1995  llvm::toStringRef(CompressedBuffer));
1996  return;
1997  }
1998 
1999  RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2000  Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2001 }
2002 
2003 /// Writes the block containing the serialized form of the
2004 /// source manager.
2005 ///
2006 /// TODO: We should probably use an on-disk hash table (stored in a
2007 /// blob), indexed based on the file name, so that we only create
2008 /// entries for files that we actually need. In the common case (no
2009 /// errors), we probably won't have to create file entries for any of
2010 /// the files in the AST.
2011 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2012  const Preprocessor &PP) {
2013  RecordData Record;
2014 
2015  // Enter the source manager block.
2016  Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
2017  const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
2018 
2019  // Abbreviations for the various kinds of source-location entries.
2020  unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2021  unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
2022  unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2023  unsigned SLocBufferBlobCompressedAbbrv =
2024  CreateSLocBufferBlobAbbrev(Stream, true);
2025  unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
2026 
2027  // Write out the source location entry table. We skip the first
2028  // entry, which is always the same dummy entry.
2029  std::vector<uint32_t> SLocEntryOffsets;
2030  uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
2031  RecordData PreloadSLocs;
2032  SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2033  for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
2034  I != N; ++I) {
2035  // Get this source location entry.
2036  const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
2037  FileID FID = FileID::get(I);
2038  assert(&SourceMgr.getSLocEntry(FID) == SLoc);
2039 
2040  // Record the offset of this source-location entry.
2041  uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
2042  assert((Offset >> 32) == 0 && "SLocEntry offset too large");
2043 
2044  // Figure out which record code to use.
2045  unsigned Code;
2046  if (SLoc->isFile()) {
2047  const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
2048  if (Cache->OrigEntry) {
2049  Code = SM_SLOC_FILE_ENTRY;
2050  } else
2051  Code = SM_SLOC_BUFFER_ENTRY;
2052  } else
2053  Code = SM_SLOC_EXPANSION_ENTRY;
2054  Record.clear();
2055  Record.push_back(Code);
2056 
2057  if (SLoc->isFile()) {
2058  const SrcMgr::FileInfo &File = SLoc->getFile();
2059  const SrcMgr::ContentCache *Content = &File.getContentCache();
2060  // Do not emit files that were not listed as inputs.
2061  if (!IsSLocAffecting[I])
2062  continue;
2063  SLocEntryOffsets.push_back(Offset);
2064  // Starting offset of this entry within this module, so skip the dummy.
2065  Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2066  AddSourceLocation(File.getIncludeLoc(), Record);
2067  Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2068  Record.push_back(File.hasLineDirectives());
2069 
2070  bool EmitBlob = false;
2071  if (Content->OrigEntry) {
2072  assert(Content->OrigEntry == Content->ContentsEntry &&
2073  "Writing to AST an overridden file is not supported");
2074 
2075  // The source location entry is a file. Emit input file ID.
2076  assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2077  Record.push_back(InputFileIDs[Content->OrigEntry]);
2078 
2079  Record.push_back(getAdjustedNumCreatedFIDs(FID));
2080 
2081  FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
2082  if (FDI != FileDeclIDs.end()) {
2083  Record.push_back(FDI->second->FirstDeclIndex);
2084  Record.push_back(FDI->second->DeclIDs.size());
2085  } else {
2086  Record.push_back(0);
2087  Record.push_back(0);
2088  }
2089 
2090  Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2091 
2092  if (Content->BufferOverridden || Content->IsTransient)
2093  EmitBlob = true;
2094  } else {
2095  // The source location entry is a buffer. The blob associated
2096  // with this entry contains the contents of the buffer.
2097 
2098  // We add one to the size so that we capture the trailing NULL
2099  // that is required by llvm::MemoryBuffer::getMemBuffer (on
2100  // the reader side).
2102  Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2103  StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2104  Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
2105  StringRef(Name.data(), Name.size() + 1));
2106  EmitBlob = true;
2107 
2108  if (Name == "<built-in>")
2109  PreloadSLocs.push_back(SLocEntryOffsets.size());
2110  }
2111 
2112  if (EmitBlob) {
2113  // Include the implicit terminating null character in the on-disk buffer
2114  // if we're writing it uncompressed.
2116  Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2117  if (!Buffer)
2118  Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2119  StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2120  emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2121  SLocBufferBlobAbbrv);
2122  }
2123  } else {
2124  // The source location entry is a macro expansion.
2125  const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2126  SLocEntryOffsets.push_back(Offset);
2127  // Starting offset of this entry within this module, so skip the dummy.
2128  Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2129  LocSeq::State Seq;
2130  AddSourceLocation(Expansion.getSpellingLoc(), Record, Seq);
2131  AddSourceLocation(Expansion.getExpansionLocStart(), Record, Seq);
2132  AddSourceLocation(Expansion.isMacroArgExpansion()
2133  ? SourceLocation()
2134  : Expansion.getExpansionLocEnd(),
2135  Record, Seq);
2136  Record.push_back(Expansion.isExpansionTokenRange());
2137 
2138  // Compute the token length for this macro expansion.
2139  SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
2140  if (I + 1 != N)
2141  NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2142  Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
2143  Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
2144  }
2145  }
2146 
2147  Stream.ExitBlock();
2148 
2149  if (SLocEntryOffsets.empty())
2150  return;
2151 
2152  // Write the source-location offsets table into the AST block. This
2153  // table is used for lazily loading source-location information.
2154  using namespace llvm;
2155 
2156  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2157  Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2158  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2159  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2160  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2161  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2162  unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2163  {
2164  RecordData::value_type Record[] = {
2165  SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2166  getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2167  SLocEntryOffsetsBase - SourceManagerBlockOffset};
2168  Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2169  bytes(SLocEntryOffsets));
2170  }
2171  // Write the source location entry preloads array, telling the AST
2172  // reader which source locations entries it should load eagerly.
2173  Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
2174 
2175  // Write the line table. It depends on remapping working, so it must come
2176  // after the source location offsets.
2177  if (SourceMgr.hasLineTable()) {
2178  LineTableInfo &LineTable = SourceMgr.getLineTable();
2179 
2180  Record.clear();
2181 
2182  // Emit the needed file names.
2183  llvm::DenseMap<int, int> FilenameMap;
2184  FilenameMap[-1] = -1; // For unspecified filenames.
2185  for (const auto &L : LineTable) {
2186  if (L.first.ID < 0)
2187  continue;
2188  for (auto &LE : L.second) {
2189  if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2190  FilenameMap.size() - 1)).second)
2191  AddPath(LineTable.getFilename(LE.FilenameID), Record);
2192  }
2193  }
2194  Record.push_back(0);
2195 
2196  // Emit the line entries
2197  for (const auto &L : LineTable) {
2198  // Only emit entries for local files.
2199  if (L.first.ID < 0)
2200  continue;
2201 
2202  AddFileID(L.first, Record);
2203 
2204  // Emit the line entries
2205  Record.push_back(L.second.size());
2206  for (const auto &LE : L.second) {
2207  Record.push_back(LE.FileOffset);
2208  Record.push_back(LE.LineNo);
2209  Record.push_back(FilenameMap[LE.FilenameID]);
2210  Record.push_back((unsigned)LE.FileKind);
2211  Record.push_back(LE.IncludeOffset);
2212  }
2213  }
2214 
2215  Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2216  }
2217 }
2218 
2219 //===----------------------------------------------------------------------===//
2220 // Preprocessor Serialization
2221 //===----------------------------------------------------------------------===//
2222 
2223 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2224  const Preprocessor &PP) {
2225  if (MacroInfo *MI = MD->getMacroInfo())
2226  if (MI->isBuiltinMacro())
2227  return true;
2228 
2229  if (IsModule) {
2230  SourceLocation Loc = MD->getLocation();
2231  if (Loc.isInvalid())
2232  return true;
2233  if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2234  return true;
2235  }
2236 
2237  return false;
2238 }
2239 
2240 void ASTWriter::writeIncludedFiles(raw_ostream &Out, const Preprocessor &PP) {
2241  using namespace llvm::support;
2242 
2243  const Preprocessor::IncludedFilesSet &IncludedFiles = PP.getIncludedFiles();
2244 
2245  std::vector<uint32_t> IncludedInputFileIDs;
2246  IncludedInputFileIDs.reserve(IncludedFiles.size());
2247 
2248  for (const FileEntry *File : IncludedFiles) {
2249  auto InputFileIt = InputFileIDs.find(File);
2250  if (InputFileIt == InputFileIDs.end())
2251  continue;
2252  IncludedInputFileIDs.push_back(InputFileIt->second);
2253  }
2254 
2255  llvm::sort(IncludedInputFileIDs);
2256 
2257  endian::Writer LE(Out, little);
2258  LE.write<uint32_t>(IncludedInputFileIDs.size());
2259  for (uint32_t ID : IncludedInputFileIDs)
2260  LE.write<uint32_t>(ID);
2261 }
2262 
2263 /// Writes the block containing the serialized form of the
2264 /// preprocessor.
2265 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2266  uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2267 
2269  if (PPRec)
2270  WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
2271 
2272  RecordData Record;
2273  RecordData ModuleMacroRecord;
2274 
2275  // If the preprocessor __COUNTER__ value has been bumped, remember it.
2276  if (PP.getCounterValue() != 0) {
2277  RecordData::value_type Record[] = {PP.getCounterValue()};
2278  Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2279  }
2280 
2281  // If we have a recorded #pragma assume_nonnull, remember it so it can be
2282  // replayed when the preamble terminates into the main file.
2283  SourceLocation AssumeNonNullLoc =
2285  if (AssumeNonNullLoc.isValid()) {
2286  assert(PP.isRecordingPreamble());
2287  AddSourceLocation(AssumeNonNullLoc, Record);
2288  Stream.EmitRecord(PP_ASSUME_NONNULL_LOC, Record);
2289  Record.clear();
2290  }
2291 
2292  if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2293  assert(!IsModule);
2294  auto SkipInfo = PP.getPreambleSkipInfo();
2295  if (SkipInfo) {
2296  Record.push_back(true);
2297  AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2298  AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2299  Record.push_back(SkipInfo->FoundNonSkipPortion);
2300  Record.push_back(SkipInfo->FoundElse);
2301  AddSourceLocation(SkipInfo->ElseLoc, Record);
2302  } else {
2303  Record.push_back(false);
2304  }
2305  for (const auto &Cond : PP.getPreambleConditionalStack()) {
2306  AddSourceLocation(Cond.IfLoc, Record);
2307  Record.push_back(Cond.WasSkipping);
2308  Record.push_back(Cond.FoundNonSkip);
2309  Record.push_back(Cond.FoundElse);
2310  }
2311  Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2312  Record.clear();
2313  }
2314 
2315  // Enter the preprocessor block.
2316  Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2317 
2318  // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2319  // FIXME: Include a location for the use, and say which one was used.
2320  if (PP.SawDateOrTime())
2321  PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2322 
2323  // Loop over all the macro directives that are live at the end of the file,
2324  // emitting each to the PP section.
2325 
2326  // Construct the list of identifiers with macro directives that need to be
2327  // serialized.
2329  // It is meaningless to emit macros for named modules. It only wastes times
2330  // and spaces.
2331  if (!isWritingStdCXXNamedModules())
2332  for (auto &Id : PP.getIdentifierTable())
2333  if (Id.second->hadMacroDefinition() &&
2334  (!Id.second->isFromAST() ||
2335  Id.second->hasChangedSinceDeserialization()))
2336  MacroIdentifiers.push_back(Id.second);
2337  // Sort the set of macro definitions that need to be serialized by the
2338  // name of the macro, to provide a stable ordering.
2339  llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2340 
2341  // Emit the macro directives as a list and associate the offset with the
2342  // identifier they belong to.
2343  for (const IdentifierInfo *Name : MacroIdentifiers) {
2345  uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2346  assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2347 
2348  // Write out any exported module macros.
2349  bool EmittedModuleMacros = false;
2350  // C+=20 Header Units are compiled module interfaces, but they preserve
2351  // macros that are live (i.e. have a defined value) at the end of the
2352  // compilation. So when writing a header unit, we preserve only the final
2353  // value of each macro (and discard any that are undefined). Header units
2354  // do not have sub-modules (although they might import other header units).
2355  // PCH files, conversely, retain the history of each macro's define/undef
2356  // and of leaf macros in sub modules.
2357  if (IsModule && WritingModule->isHeaderUnit()) {
2358  // This is for the main TU when it is a C++20 header unit.
2359  // We preserve the final state of defined macros, and we do not emit ones
2360  // that are undefined.
2361  if (!MD || shouldIgnoreMacro(MD, IsModule, PP) ||
2363  continue;
2364  AddSourceLocation(MD->getLocation(), Record);
2365  Record.push_back(MD->getKind());
2366  if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2367  Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2368  } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2369  Record.push_back(VisMD->isPublic());
2370  }
2371  ModuleMacroRecord.push_back(getSubmoduleID(WritingModule));
2372  ModuleMacroRecord.push_back(getMacroRef(MD->getMacroInfo(), Name));
2373  Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2374  ModuleMacroRecord.clear();
2375  EmittedModuleMacros = true;
2376  } else {
2377  // Emit the macro directives in reverse source order.
2378  for (; MD; MD = MD->getPrevious()) {
2379  // Once we hit an ignored macro, we're done: the rest of the chain
2380  // will all be ignored macros.
2381  if (shouldIgnoreMacro(MD, IsModule, PP))
2382  break;
2383  AddSourceLocation(MD->getLocation(), Record);
2384  Record.push_back(MD->getKind());
2385  if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2386  Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2387  } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2388  Record.push_back(VisMD->isPublic());
2389  }
2390  }
2391 
2392  // We write out exported module macros for PCH as well.
2393  auto Leafs = PP.getLeafModuleMacros(Name);
2394  SmallVector<ModuleMacro *, 8> Worklist(Leafs.begin(), Leafs.end());
2395  llvm::DenseMap<ModuleMacro *, unsigned> Visits;
2396  while (!Worklist.empty()) {
2397  auto *Macro = Worklist.pop_back_val();
2398 
2399  // Emit a record indicating this submodule exports this macro.
2400  ModuleMacroRecord.push_back(getSubmoduleID(Macro->getOwningModule()));
2401  ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2402  for (auto *M : Macro->overrides())
2403  ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2404 
2405  Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2406  ModuleMacroRecord.clear();
2407 
2408  // Enqueue overridden macros once we've visited all their ancestors.
2409  for (auto *M : Macro->overrides())
2410  if (++Visits[M] == M->getNumOverridingMacros())
2411  Worklist.push_back(M);
2412 
2413  EmittedModuleMacros = true;
2414  }
2415  }
2416  if (Record.empty() && !EmittedModuleMacros)
2417  continue;
2418 
2419  IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2420  Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2421  Record.clear();
2422  }
2423 
2424  /// Offsets of each of the macros into the bitstream, indexed by
2425  /// the local macro ID
2426  ///
2427  /// For each identifier that is associated with a macro, this map
2428  /// provides the offset into the bitstream where that macro is
2429  /// defined.
2430  std::vector<uint32_t> MacroOffsets;
2431 
2432  for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2433  const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2434  MacroInfo *MI = MacroInfosToEmit[I].MI;
2435  MacroID ID = MacroInfosToEmit[I].ID;
2436 
2437  if (ID < FirstMacroID) {
2438  assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2439  continue;
2440  }
2441 
2442  // Record the local offset of this macro.
2443  unsigned Index = ID - FirstMacroID;
2444  if (Index >= MacroOffsets.size())
2445  MacroOffsets.resize(Index + 1);
2446 
2447  uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2448  assert((Offset >> 32) == 0 && "Macro offset too large");
2449  MacroOffsets[Index] = Offset;
2450 
2451  AddIdentifierRef(Name, Record);
2452  AddSourceLocation(MI->getDefinitionLoc(), Record);
2453  AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2454  Record.push_back(MI->isUsed());
2455  Record.push_back(MI->isUsedForHeaderGuard());
2456  Record.push_back(MI->getNumTokens());
2457  unsigned Code;
2458  if (MI->isObjectLike()) {
2459  Code = PP_MACRO_OBJECT_LIKE;
2460  } else {
2461  Code = PP_MACRO_FUNCTION_LIKE;
2462 
2463  Record.push_back(MI->isC99Varargs());
2464  Record.push_back(MI->isGNUVarargs());
2465  Record.push_back(MI->hasCommaPasting());
2466  Record.push_back(MI->getNumParams());
2467  for (const IdentifierInfo *Param : MI->params())
2468  AddIdentifierRef(Param, Record);
2469  }
2470 
2471  // If we have a detailed preprocessing record, record the macro definition
2472  // ID that corresponds to this macro.
2473  if (PPRec)
2474  Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2475 
2476  Stream.EmitRecord(Code, Record);
2477  Record.clear();
2478 
2479  // Emit the tokens array.
2480  for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2481  // Note that we know that the preprocessor does not have any annotation
2482  // tokens in it because they are created by the parser, and thus can't
2483  // be in a macro definition.
2484  const Token &Tok = MI->getReplacementToken(TokNo);
2485  AddToken(Tok, Record);
2486  Stream.EmitRecord(PP_TOKEN, Record);
2487  Record.clear();
2488  }
2489  ++NumMacros;
2490  }
2491 
2492  Stream.ExitBlock();
2493 
2494  // Write the offsets table for macro IDs.
2495  using namespace llvm;
2496 
2497  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2498  Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2499  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2500  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2501  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2502  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2503 
2504  unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2505  {
2506  RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2507  FirstMacroID - NUM_PREDEF_MACRO_IDS,
2508  MacroOffsetsBase - ASTBlockStartOffset};
2509  Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2510  }
2511 
2512  {
2513  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2514  Abbrev->Add(BitCodeAbbrevOp(PP_INCLUDED_FILES));
2515  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2516  unsigned IncludedFilesAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2517 
2518  SmallString<2048> Buffer;
2519  raw_svector_ostream Out(Buffer);
2520  writeIncludedFiles(Out, PP);
2521  RecordData::value_type Record[] = {PP_INCLUDED_FILES};
2522  Stream.EmitRecordWithBlob(IncludedFilesAbbrev, Record, Buffer.data(),
2523  Buffer.size());
2524  }
2525 }
2526 
2527 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2528  uint64_t MacroOffsetsBase) {
2529  if (PPRec.local_begin() == PPRec.local_end())
2530  return;
2531 
2532  SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2533 
2534  // Enter the preprocessor block.
2535  Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2536 
2537  // If the preprocessor has a preprocessing record, emit it.
2538  unsigned NumPreprocessingRecords = 0;
2539  using namespace llvm;
2540 
2541  // Set up the abbreviation for
2542  unsigned InclusionAbbrev = 0;
2543  {
2544  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2545  Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2546  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2547  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2548  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2549  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2550  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2551  InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2552  }
2553 
2554  unsigned FirstPreprocessorEntityID
2555  = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2557  unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2558  RecordData Record;
2559  for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2560  EEnd = PPRec.local_end();
2561  E != EEnd;
2562  (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2563  Record.clear();
2564 
2565  uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2566  assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2567  PreprocessedEntityOffsets.push_back(
2568  PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
2569 
2570  if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2571  // Record this macro definition's ID.
2572  MacroDefinitions[MD] = NextPreprocessorEntityID;
2573 
2574  AddIdentifierRef(MD->getName(), Record);
2575  Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2576  continue;
2577  }
2578 
2579  if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2580  Record.push_back(ME->isBuiltinMacro());
2581  if (ME->isBuiltinMacro())
2582  AddIdentifierRef(ME->getName(), Record);
2583  else
2584  Record.push_back(MacroDefinitions[ME->getDefinition()]);
2585  Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2586  continue;
2587  }
2588 
2589  if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2590  Record.push_back(PPD_INCLUSION_DIRECTIVE);
2591  Record.push_back(ID->getFileName().size());
2592  Record.push_back(ID->wasInQuotes());
2593  Record.push_back(static_cast<unsigned>(ID->getKind()));
2594  Record.push_back(ID->importedModule());
2595  SmallString<64> Buffer;
2596  Buffer += ID->getFileName();
2597  // Check that the FileEntry is not null because it was not resolved and
2598  // we create a PCH even with compiler errors.
2599  if (ID->getFile())
2600  Buffer += ID->getFile()->getName();
2601  Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2602  continue;
2603  }
2604 
2605  llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2606  }
2607  Stream.ExitBlock();
2608 
2609  // Write the offsets table for the preprocessing record.
2610  if (NumPreprocessingRecords > 0) {
2611  assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2612 
2613  // Write the offsets table for identifier IDs.
2614  using namespace llvm;
2615 
2616  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2617  Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2618  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2619  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2620  unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2621 
2622  RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2623  FirstPreprocessorEntityID -
2625  Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2626  bytes(PreprocessedEntityOffsets));
2627  }
2628 
2629  // Write the skipped region table for the preprocessing record.
2630  ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2631  if (SkippedRanges.size() > 0) {
2632  std::vector<PPSkippedRange> SerializedSkippedRanges;
2633  SerializedSkippedRanges.reserve(SkippedRanges.size());
2634  for (auto const& Range : SkippedRanges)
2635  SerializedSkippedRanges.emplace_back(Range);
2636 
2637  using namespace llvm;
2638  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2639  Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2640  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2641  unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2642 
2643  Record.clear();
2644  Record.push_back(PPD_SKIPPED_RANGES);
2645  Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2646  bytes(SerializedSkippedRanges));
2647  }
2648 }
2649 
2651  if (!Mod)
2652  return 0;
2653 
2654  auto Known = SubmoduleIDs.find(Mod);
2655  if (Known != SubmoduleIDs.end())
2656  return Known->second;
2657 
2658  auto *Top = Mod->getTopLevelModule();
2659  if (Top != WritingModule &&
2660  (getLangOpts().CompilingPCH ||
2661  !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
2662  return 0;
2663 
2664  return SubmoduleIDs[Mod] = NextSubmoduleID++;
2665 }
2666 
2667 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2668  unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2669  // FIXME: This can easily happen, if we have a reference to a submodule that
2670  // did not result in us loading a module file for that submodule. For
2671  // instance, a cross-top-level-module 'conflict' declaration will hit this.
2672  // assert((ID || !Mod) &&
2673  // "asked for module ID for non-local, non-imported module");
2674  return ID;
2675 }
2676 
2677 /// Compute the number of modules within the given tree (including the
2678 /// given module).
2679 static unsigned getNumberOfModules(Module *Mod) {
2680  unsigned ChildModules = 0;
2681  for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
2682  Sub != SubEnd; ++Sub)
2683  ChildModules += getNumberOfModules(*Sub);
2684 
2685  return ChildModules + 1;
2686 }
2687 
2688 void ASTWriter::WriteSubmodules(Module *WritingModule) {
2689  // Enter the submodule description block.
2690  Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2691 
2692  // Write the abbreviations needed for the submodules block.
2693  using namespace llvm;
2694 
2695  auto Abbrev = std::make_shared<BitCodeAbbrev>();
2696  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2697  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2698  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2699  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Kind
2700  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2701  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2702  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2703  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2704  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2705  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2706  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2707  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2708  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2709  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2710  unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2711 
2712  Abbrev = std::make_shared<BitCodeAbbrev>();
2713  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2714  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2715  unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2716 
2717  Abbrev = std::make_shared<BitCodeAbbrev>();
2718  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2719  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2720  unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2721 
2722  Abbrev = std::make_shared<BitCodeAbbrev>();
2723  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2724  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2725  unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2726 
2727  Abbrev = std::make_shared<BitCodeAbbrev>();
2728  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2729  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2730  unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2731 
2732  Abbrev = std::make_shared<BitCodeAbbrev>();
2733  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2734  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2735  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2736  unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2737 
2738  Abbrev = std::make_shared<BitCodeAbbrev>();
2739  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2740  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2741  unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2742 
2743  Abbrev = std::make_shared<BitCodeAbbrev>();
2744  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2745  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2746  unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2747 
2748  Abbrev = std::make_shared<BitCodeAbbrev>();
2749  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2750  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2751  unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2752 
2753  Abbrev = std::make_shared<BitCodeAbbrev>();
2754  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2755  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2756  unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2757 
2758  Abbrev = std::make_shared<BitCodeAbbrev>();
2759  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2760  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2761  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2762  unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2763 
2764  Abbrev = std::make_shared<BitCodeAbbrev>();
2765  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2766  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2767  unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2768 
2769  Abbrev = std::make_shared<BitCodeAbbrev>();
2770  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2771  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2772  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2773  unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2774 
2775  Abbrev = std::make_shared<BitCodeAbbrev>();
2776  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2777  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2778  unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2779 
2780  // Write the submodule metadata block.
2781  RecordData::value_type Record[] = {
2782  getNumberOfModules(WritingModule),
2783  FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2784  Stream.EmitRecord(SUBMODULE_METADATA, Record);
2785 
2786  // Write all of the submodules.
2787  std::queue<Module *> Q;
2788  Q.push(WritingModule);
2789  while (!Q.empty()) {
2790  Module *Mod = Q.front();
2791  Q.pop();
2792  unsigned ID = getSubmoduleID(Mod);
2793 
2794  uint64_t ParentID = 0;
2795  if (Mod->Parent) {
2796  assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2797  ParentID = SubmoduleIDs[Mod->Parent];
2798  }
2799 
2800  // Emit the definition of the block.
2801  {
2802  RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2803  ID,
2804  ParentID,
2805  (RecordData::value_type)Mod->Kind,
2806  Mod->IsFramework,
2807  Mod->IsExplicit,
2808  Mod->IsSystem,
2809  Mod->IsExternC,
2810  Mod->InferSubmodules,
2812  Mod->InferExportWildcard,
2814  Mod->ModuleMapIsPrivate};
2815  Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2816  }
2817 
2818  // Emit the requirements.
2819  for (const auto &R : Mod->Requirements) {
2820  RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2821  Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2822  }
2823 
2824  // Emit the umbrella header, if there is one.
2825  if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
2826  RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2827  Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2828  UmbrellaHeader.NameAsWritten);
2829  } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
2830  RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2831  Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2832  UmbrellaDir.NameAsWritten);
2833  }
2834 
2835  // Emit the headers.
2836  struct {
2837  unsigned RecordKind;
2838  unsigned Abbrev;
2839  Module::HeaderKind HeaderKind;
2840  } HeaderLists[] = {
2841  {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2842  {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2843  {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2844  {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2846  {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2847  };
2848  for (auto &HL : HeaderLists) {
2849  RecordData::value_type Record[] = {HL.RecordKind};
2850  for (auto &H : Mod->Headers[HL.HeaderKind])
2851  Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2852  }
2853 
2854  // Emit the top headers.
2855  {
2856  auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2857  RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2858  for (auto *H : TopHeaders) {
2859  SmallString<128> HeaderName(H->getName());
2860  PreparePathForOutput(HeaderName);
2861  Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
2862  }
2863  }
2864 
2865  // Emit the imports.
2866  if (!Mod->Imports.empty()) {
2867  RecordData Record;
2868  for (auto *I : Mod->Imports)
2869  Record.push_back(getSubmoduleID(I));
2870  Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2871  }
2872 
2873  // Emit the modules affecting compilation that were not imported.
2874  if (!Mod->AffectingClangModules.empty()) {
2875  RecordData Record;
2876  for (auto *I : Mod->AffectingClangModules)
2877  Record.push_back(getSubmoduleID(I));
2878  Stream.EmitRecord(SUBMODULE_AFFECTING_MODULES, Record);
2879  }
2880 
2881  // Emit the exports.
2882  if (!Mod->Exports.empty()) {
2883  RecordData Record;
2884  for (const auto &E : Mod->Exports) {
2885  // FIXME: This may fail; we don't require that all exported modules
2886  // are local or imported.
2887  Record.push_back(getSubmoduleID(E.getPointer()));
2888  Record.push_back(E.getInt());
2889  }
2890  Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2891  }
2892 
2893  //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2894  // Might be unnecessary as use declarations are only used to build the
2895  // module itself.
2896 
2897  // TODO: Consider serializing undeclared uses of modules.
2898 
2899  // Emit the link libraries.
2900  for (const auto &LL : Mod->LinkLibraries) {
2901  RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2902  LL.IsFramework};
2903  Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2904  }
2905 
2906  // Emit the conflicts.
2907  for (const auto &C : Mod->Conflicts) {
2908  // FIXME: This may fail; we don't require that all conflicting modules
2909  // are local or imported.
2910  RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2911  getSubmoduleID(C.Other)};
2912  Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2913  }
2914 
2915  // Emit the configuration macros.
2916  for (const auto &CM : Mod->ConfigMacros) {
2917  RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2918  Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
2919  }
2920 
2921  // Emit the initializers, if any.
2922  RecordData Inits;
2923  for (Decl *D : Context->getModuleInitializers(Mod))
2924  Inits.push_back(GetDeclRef(D));
2925  if (!Inits.empty())
2926  Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2927 
2928  // Emit the name of the re-exported module, if any.
2929  if (!Mod->ExportAsModule.empty()) {
2930  RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
2931  Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
2932  }
2933 
2934  // Queue up the submodules of this module.
2935  for (auto *M : Mod->submodules())
2936  Q.push(M);
2937  }
2938 
2939  Stream.ExitBlock();
2940 
2941  assert((NextSubmoduleID - FirstSubmoduleID ==
2942  getNumberOfModules(WritingModule)) &&
2943  "Wrong # of submodules; found a reference to a non-local, "
2944  "non-imported submodule?");
2945 }
2946 
2947 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2948  bool isModule) {
2949  llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2950  DiagStateIDMap;
2951  unsigned CurrID = 0;
2952  RecordData Record;
2953 
2954  auto EncodeDiagStateFlags =
2955  [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
2956  unsigned Result = (unsigned)DS->ExtBehavior;
2957  for (unsigned Val :
2958  {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
2959  (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
2960  (unsigned)DS->SuppressSystemWarnings})
2961  Result = (Result << 1) | Val;
2962  return Result;
2963  };
2964 
2965  unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
2966  Record.push_back(Flags);
2967 
2968  auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2969  bool IncludeNonPragmaStates) {
2970  // Ensure that the diagnostic state wasn't modified since it was created.
2971  // We will not correctly round-trip this information otherwise.
2972  assert(Flags == EncodeDiagStateFlags(State) &&
2973  "diag state flags vary in single AST file");
2974 
2975  unsigned &DiagStateID = DiagStateIDMap[State];
2976  Record.push_back(DiagStateID);
2977 
2978  if (DiagStateID == 0) {
2979  DiagStateID = ++CurrID;
2980 
2981  // Add a placeholder for the number of mappings.
2982  auto SizeIdx = Record.size();
2983  Record.emplace_back();
2984  for (const auto &I : *State) {
2985  if (I.second.isPragma() || IncludeNonPragmaStates) {
2986  Record.push_back(I.first);
2987  Record.push_back(I.second.serialize());
2988  }
2989  }
2990  // Update the placeholder.
2991  Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
2992  }
2993  };
2994 
2995  AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2996 
2997  // Reserve a spot for the number of locations with state transitions.
2998  auto NumLocationsIdx = Record.size();
2999  Record.emplace_back();
3000 
3001  // Emit the state transitions.
3002  unsigned NumLocations = 0;
3003  for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3004  if (!FileIDAndFile.first.isValid() ||
3005  !FileIDAndFile.second.HasLocalTransitions)
3006  continue;
3007  ++NumLocations;
3008 
3009  SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3010  assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3011  AddSourceLocation(Loc, Record);
3012 
3013  Record.push_back(FileIDAndFile.second.StateTransitions.size());
3014  for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3015  Record.push_back(getAdjustedOffset(StatePoint.Offset));
3016  AddDiagState(StatePoint.State, false);
3017  }
3018  }
3019 
3020  // Backpatch the number of locations.
3021  Record[NumLocationsIdx] = NumLocations;
3022 
3023  // Emit CurDiagStateLoc. Do it last in order to match source order.
3024  //
3025  // This also protects against a hypothetical corner case with simulating
3026  // -Werror settings for implicit modules in the ASTReader, where reading
3027  // CurDiagState out of context could change whether warning pragmas are
3028  // treated as errors.
3029  AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3030  AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3031 
3032  Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
3033 }
3034 
3035 //===----------------------------------------------------------------------===//
3036 // Type Serialization
3037 //===----------------------------------------------------------------------===//
3038 
3039 /// Write the representation of a type to the AST stream.
3040 void ASTWriter::WriteType(QualType T) {
3041  TypeIdx &IdxRef = TypeIdxs[T];
3042  if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3043  IdxRef = TypeIdx(NextTypeID++);
3044  TypeIdx Idx = IdxRef;
3045 
3046  assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
3047 
3048  // Emit the type's representation.
3049  uint64_t Offset = ASTTypeWriter(*this).write(T) - DeclTypesBlockStartOffset;
3050 
3051  // Record the offset for this type.
3052  unsigned Index = Idx.getIndex() - FirstTypeID;
3053  if (TypeOffsets.size() == Index)
3054  TypeOffsets.emplace_back(Offset);
3055  else if (TypeOffsets.size() < Index) {
3056  TypeOffsets.resize(Index + 1);
3057  TypeOffsets[Index].setBitOffset(Offset);
3058  } else {
3059  llvm_unreachable("Types emitted in wrong order");
3060  }
3061 }
3062 
3063 //===----------------------------------------------------------------------===//
3064 // Declaration Serialization
3065 //===----------------------------------------------------------------------===//
3066 
3067 /// Write the block containing all of the declaration IDs
3068 /// lexically declared within the given DeclContext.
3069 ///
3070 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3071 /// bitstream, or 0 if no block was written.
3072 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3073  DeclContext *DC) {
3074  if (DC->decls_empty())
3075  return 0;
3076 
3077  uint64_t Offset = Stream.GetCurrentBitNo();
3078  SmallVector<uint32_t, 128> KindDeclPairs;
3079  for (const auto *D : DC->decls()) {
3080  KindDeclPairs.push_back(D->getKind());
3081  KindDeclPairs.push_back(GetDeclRef(D));
3082  }
3083 
3084  ++NumLexicalDeclContexts;
3085  RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3086  Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3087  bytes(KindDeclPairs));
3088  return Offset;
3089 }
3090 
3091 void ASTWriter::WriteTypeDeclOffsets() {
3092  using namespace llvm;
3093 
3094  // Write the type offsets array
3095  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3096  Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
3097  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3098  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
3099  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3100  unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3101  {
3102  RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3103  FirstTypeID - NUM_PREDEF_TYPE_IDS};
3104  Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3105  }
3106 
3107  // Write the declaration offsets array
3108  Abbrev = std::make_shared<BitCodeAbbrev>();
3109  Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
3110  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3111  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
3112  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3113  unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3114  {
3115  RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3116  FirstDeclID - NUM_PREDEF_DECL_IDS};
3117  Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3118  }
3119 }
3120 
3121 void ASTWriter::WriteFileDeclIDsMap() {
3122  using namespace llvm;
3123 
3125  SortedFileDeclIDs.reserve(FileDeclIDs.size());
3126  for (const auto &P : FileDeclIDs)
3127  SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
3128  llvm::sort(SortedFileDeclIDs, llvm::less_first());
3129 
3130  // Join the vectors of DeclIDs from all files.
3131  SmallVector<DeclID, 256> FileGroupedDeclIDs;
3132  for (auto &FileDeclEntry : SortedFileDeclIDs) {
3133  DeclIDInFileInfo &Info = *FileDeclEntry.second;
3134  Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3135  llvm::stable_sort(Info.DeclIDs);
3136  for (auto &LocDeclEntry : Info.DeclIDs)
3137  FileGroupedDeclIDs.push_back(LocDeclEntry.second);
3138  }
3139 
3140  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3141  Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
3142  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3143  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3144  unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
3145  RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3146  FileGroupedDeclIDs.size()};
3147  Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
3148 }
3149 
3150 void ASTWriter::WriteComments() {
3151  Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
3152  auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3154  return;
3155  RecordData Record;
3156  for (const auto &FO : Context->Comments.OrderedComments) {
3157  for (const auto &OC : FO.second) {
3158  const RawComment *I = OC.second;
3159  Record.clear();
3160  AddSourceRange(I->getSourceRange(), Record);
3161  Record.push_back(I->getKind());
3162  Record.push_back(I->isTrailingComment());
3163  Record.push_back(I->isAlmostTrailingComment());
3164  Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3165  }
3166  }
3167 }
3168 
3169 //===----------------------------------------------------------------------===//
3170 // Global Method Pool and Selector Serialization
3171 //===----------------------------------------------------------------------===//
3172 
3173 namespace {
3174 
3175 // Trait used for the on-disk hash table used in the method pool.
3176 class ASTMethodPoolTrait {
3177  ASTWriter &Writer;
3178 
3179 public:
3180  using key_type = Selector;
3181  using key_type_ref = key_type;
3182 
3183  struct data_type {
3184  SelectorID ID;
3185  ObjCMethodList Instance, Factory;
3186  };
3187  using data_type_ref = const data_type &;
3188 
3189  using hash_value_type = unsigned;
3190  using offset_type = unsigned;
3191 
3192  explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3193 
3194  static hash_value_type ComputeHash(Selector Sel) {
3195  return serialization::ComputeHash(Sel);
3196  }
3197 
3198  std::pair<unsigned, unsigned>
3199  EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3200  data_type_ref Methods) {
3201  unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
3202  unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3203  for (const ObjCMethodList *Method = &Methods.Instance; Method;
3204  Method = Method->getNext())
3205  if (ShouldWriteMethodListNode(Method))
3206  DataLen += 4;
3207  for (const ObjCMethodList *Method = &Methods.Factory; Method;
3208  Method = Method->getNext())
3209  if (ShouldWriteMethodListNode(Method))
3210  DataLen += 4;
3211  return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3212  }
3213 
3214  void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3215  using namespace llvm::support;
3216 
3217  endian::Writer LE(Out, little);
3218  uint64_t Start = Out.tell();
3219  assert((Start >> 32) == 0 && "Selector key offset too large");
3220  Writer.SetSelectorOffset(Sel, Start);
3221  unsigned N = Sel.getNumArgs();
3222  LE.write<uint16_t>(N);
3223  if (N == 0)
3224  N = 1;
3225  for (unsigned I = 0; I != N; ++I)
3226  LE.write<uint32_t>(
3228  }
3229 
3230  void EmitData(raw_ostream& Out, key_type_ref,
3231  data_type_ref Methods, unsigned DataLen) {
3232  using namespace llvm::support;
3233 
3234  endian::Writer LE(Out, little);
3235  uint64_t Start = Out.tell(); (void)Start;
3236  LE.write<uint32_t>(Methods.ID);
3237  unsigned NumInstanceMethods = 0;
3238  for (const ObjCMethodList *Method = &Methods.Instance; Method;
3239  Method = Method->getNext())
3240  if (ShouldWriteMethodListNode(Method))
3241  ++NumInstanceMethods;
3242 
3243  unsigned NumFactoryMethods = 0;
3244  for (const ObjCMethodList *Method = &Methods.Factory; Method;
3245  Method = Method->getNext())
3246  if (ShouldWriteMethodListNode(Method))
3247  ++NumFactoryMethods;
3248 
3249  unsigned InstanceBits = Methods.Instance.getBits();
3250  assert(InstanceBits < 4);
3251  unsigned InstanceHasMoreThanOneDeclBit =
3252  Methods.Instance.hasMoreThanOneDecl();
3253  unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3254  (InstanceHasMoreThanOneDeclBit << 2) |
3255  InstanceBits;
3256  unsigned FactoryBits = Methods.Factory.getBits();
3257  assert(FactoryBits < 4);
3258  unsigned FactoryHasMoreThanOneDeclBit =
3259  Methods.Factory.hasMoreThanOneDecl();
3260  unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3261  (FactoryHasMoreThanOneDeclBit << 2) |
3262  FactoryBits;
3263  LE.write<uint16_t>(FullInstanceBits);
3264  LE.write<uint16_t>(FullFactoryBits);
3265  for (const ObjCMethodList *Method = &Methods.Instance; Method;
3266  Method = Method->getNext())
3267  if (ShouldWriteMethodListNode(Method))
3268  LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3269  for (const ObjCMethodList *Method = &Methods.Factory; Method;
3270  Method = Method->getNext())
3271  if (ShouldWriteMethodListNode(Method))
3272  LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3273 
3274  assert(Out.tell() - Start == DataLen && "Data length is wrong");
3275  }
3276 
3277 private:
3278  static bool ShouldWriteMethodListNode(const ObjCMethodList *Node) {
3279  return (Node->getMethod() && !Node->getMethod()->isFromASTFile());
3280  }
3281 };
3282 
3283 } // namespace
3284 
3285 /// Write ObjC data: selectors and the method pool.
3286 ///
3287 /// The method pool contains both instance and factory methods, stored
3288 /// in an on-disk hash table indexed by the selector. The hash table also
3289 /// contains an empty entry for every other selector known to Sema.
3290 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3291  using namespace llvm;
3292 
3293  // Do we have to do anything at all?
3294  if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
3295  return;
3296  unsigned NumTableEntries = 0;
3297  // Create and write out the blob that contains selectors and the method pool.
3298  {
3299  llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3300  ASTMethodPoolTrait Trait(*this);
3301 
3302  // Create the on-disk hash table representation. We walk through every
3303  // selector we've seen and look it up in the method pool.
3304  SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3305  for (auto &SelectorAndID : SelectorIDs) {
3306  Selector S = SelectorAndID.first;
3307  SelectorID ID = SelectorAndID.second;
3309  ASTMethodPoolTrait::data_type Data = {
3310  ID,
3311  ObjCMethodList(),
3312  ObjCMethodList()
3313  };
3314  if (F != SemaRef.MethodPool.end()) {
3315  Data.Instance = F->second.first;
3316  Data.Factory = F->second.second;
3317  }
3318  // Only write this selector if it's not in an existing AST or something
3319  // changed.
3320  if (Chain && ID < FirstSelectorID) {
3321  // Selector already exists. Did it change?
3322  bool changed = false;
3323  for (ObjCMethodList *M = &Data.Instance; M && M->getMethod();
3324  M = M->getNext()) {
3325  if (!M->getMethod()->isFromASTFile()) {
3326  changed = true;
3327  Data.Instance = *M;
3328  break;
3329  }
3330  }
3331  for (ObjCMethodList *M = &Data.Factory; M && M->getMethod();
3332  M = M->getNext()) {
3333  if (!M->getMethod()->isFromASTFile()) {
3334  changed = true;
3335  Data.Factory = *M;
3336  break;
3337  }
3338  }
3339  if (!changed)
3340  continue;
3341  } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3342  // A new method pool entry.
3343  ++NumTableEntries;
3344  }
3345  Generator.insert(S, Data, Trait);
3346  }
3347 
3348  // Create the on-disk hash table in a buffer.
3349  SmallString<4096> MethodPool;
3350  uint32_t BucketOffset;
3351  {
3352  using namespace llvm::support;
3353 
3354  ASTMethodPoolTrait Trait(*this);
3355  llvm::raw_svector_ostream Out(MethodPool);
3356  // Make sure that no bucket is at offset 0
3357  endian::write<uint32_t>(Out, 0, little);
3358  BucketOffset = Generator.Emit(Out, Trait);
3359  }
3360 
3361  // Create a blob abbreviation
3362  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3363  Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3364  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3365  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3366  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3367  unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3368 
3369  // Write the method pool
3370  {
3371  RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3372  NumTableEntries};
3373  Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3374  }
3375 
3376  // Create a blob abbreviation for the selector table offsets.
3377  Abbrev = std::make_shared<BitCodeAbbrev>();
3378  Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3379  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3380  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3381  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3382  unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3383 
3384  // Write the selector offsets table.
3385  {
3386  RecordData::value_type Record[] = {
3387  SELECTOR_OFFSETS, SelectorOffsets.size(),
3388  FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3389  Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3390  bytes(SelectorOffsets));
3391  }
3392  }
3393 }
3394 
3395 /// Write the selectors referenced in @selector expression into AST file.
3396 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3397  using namespace llvm;
3398 
3399  if (SemaRef.ReferencedSelectors.empty())
3400  return;
3401 
3402  RecordData Record;
3403  ASTRecordWriter Writer(*this, Record);
3404 
3405  // Note: this writes out all references even for a dependent AST. But it is
3406  // very tricky to fix, and given that @selector shouldn't really appear in
3407  // headers, probably not worth it. It's not a correctness issue.
3408  for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3409  Selector Sel = SelectorAndLocation.first;
3410  SourceLocation Loc = SelectorAndLocation.second;
3411  Writer.AddSelectorRef(Sel);
3412  Writer.AddSourceLocation(Loc);
3413  }
3414  Writer.Emit(REFERENCED_SELECTOR_POOL);
3415 }
3416 
3417 //===----------------------------------------------------------------------===//
3418 // Identifier Table Serialization
3419 //===----------------------------------------------------------------------===//
3420 
3421 /// Determine the declaration that should be put into the name lookup table to
3422 /// represent the given declaration in this module. This is usually D itself,
3423 /// but if D was imported and merged into a local declaration, we want the most
3424 /// recent local declaration instead. The chosen declaration will be the most
3425 /// recent declaration in any module that imports this one.
3427  NamedDecl *D) {
3428  if (!LangOpts.Modules || !D->isFromASTFile())
3429  return D;
3430 
3431  if (Decl *Redecl = D->getPreviousDecl()) {
3432  // For Redeclarable decls, a prior declaration might be local.
3433  for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3434  // If we find a local decl, we're done.
3435  if (!Redecl->isFromASTFile()) {
3436  // Exception: in very rare cases (for injected-class-names), not all
3437  // redeclarations are in the same semantic context. Skip ones in a
3438  // different context. They don't go in this lookup table at all.
3439  if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3441  continue;
3442  return cast<NamedDecl>(Redecl);
3443  }
3444 
3445  // If we find a decl from a (chained-)PCH stop since we won't find a
3446  // local one.
3447  if (Redecl->getOwningModuleID() == 0)
3448  break;
3449  }
3450  } else if (Decl *First = D->getCanonicalDecl()) {
3451  // For Mergeable decls, the first decl might be local.
3452  if (!First->isFromASTFile())
3453  return cast<NamedDecl>(First);
3454  }
3455 
3456  // All declarations are imported. Our most recent declaration will also be
3457  // the most recent one in anyone who imports us.
3458  return D;
3459 }
3460 
3461 namespace {
3462 
3463 class ASTIdentifierTableTrait {
3464  ASTWriter &Writer;
3465  Preprocessor &PP;
3466  IdentifierResolver &IdResolver;
3467  bool IsModule;
3468  bool NeedDecls;
3469  ASTWriter::RecordData *InterestingIdentifierOffsets;
3470 
3471  /// Determines whether this is an "interesting" identifier that needs a
3472  /// full IdentifierInfo structure written into the hash table. Notably, this
3473  /// doesn't check whether the name has macros defined; use PublicMacroIterator
3474  /// to check that.
3475  bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3476  if (MacroOffset || II->isPoisoned() ||
3477  (!IsModule && II->getObjCOrBuiltinID()) ||
3479  (NeedDecls && II->getFETokenInfo()))
3480  return true;
3481 
3482  return false;
3483  }
3484 
3485 public:
3486  using key_type = IdentifierInfo *;
3487  using key_type_ref = key_type;
3488 
3489  using data_type = IdentID;
3490  using data_type_ref = data_type;
3491 
3492  using hash_value_type = unsigned;
3493  using offset_type = unsigned;
3494 
3495  ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3496  IdentifierResolver &IdResolver, bool IsModule,
3497  ASTWriter::RecordData *InterestingIdentifierOffsets)
3498  : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3499  NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3500  InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3501 
3502  bool needDecls() const { return NeedDecls; }
3503 
3504  static hash_value_type ComputeHash(const IdentifierInfo* II) {
3505  return llvm::djbHash(II->getName());
3506  }
3507 
3508  bool isInterestingIdentifier(const IdentifierInfo *II) {
3509  auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3510  return isInterestingIdentifier(II, MacroOffset);
3511  }
3512 
3513  bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3514  return isInterestingIdentifier(II, 0);
3515  }
3516 
3517  std::pair<unsigned, unsigned>
3518  EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3519  // Record the location of the identifier data. This is used when generating
3520  // the mapping from persistent IDs to strings.
3521  Writer.SetIdentifierOffset(II, Out.tell());
3522 
3523  // Emit the offset of the key/data length information to the interesting
3524  // identifiers table if necessary.
3525  if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3526  InterestingIdentifierOffsets->push_back(Out.tell());
3527 
3528  unsigned KeyLen = II->getLength() + 1;
3529  unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3530  auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3531  if (isInterestingIdentifier(II, MacroOffset)) {
3532  DataLen += 2; // 2 bytes for builtin ID
3533  DataLen += 2; // 2 bytes for flags
3534  if (MacroOffset)
3535  DataLen += 4; // MacroDirectives offset.
3536 
3537  if (NeedDecls) {
3538  for (IdentifierResolver::iterator D = IdResolver.begin(II),
3539  DEnd = IdResolver.end();
3540  D != DEnd; ++D)
3541  DataLen += 4;
3542  }
3543  }
3544  return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3545  }
3546 
3547  void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3548  unsigned KeyLen) {
3549  Out.write(II->getNameStart(), KeyLen);
3550  }
3551 
3552  void EmitData(raw_ostream& Out, IdentifierInfo* II,
3553  IdentID ID, unsigned) {
3554  using namespace llvm::support;
3555 
3556  endian::Writer LE(Out, little);
3557 
3558  auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3559  if (!isInterestingIdentifier(II, MacroOffset)) {
3560  LE.write<uint32_t>(ID << 1);
3561  return;
3562  }
3563 
3564  LE.write<uint32_t>((ID << 1) | 0x01);
3565  uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3566  assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3567  LE.write<uint16_t>(Bits);
3568  Bits = 0;
3569  bool HadMacroDefinition = MacroOffset != 0;
3570  Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3571  Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3572  Bits = (Bits << 1) | unsigned(II->isPoisoned());
3573  Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3574  Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3575  LE.write<uint16_t>(Bits);
3576 
3577  if (HadMacroDefinition)
3578  LE.write<uint32_t>(MacroOffset);
3579 
3580  if (NeedDecls) {
3581  // Emit the declaration IDs in reverse order, because the
3582  // IdentifierResolver provides the declarations as they would be
3583  // visible (e.g., the function "stat" would come before the struct
3584  // "stat"), but the ASTReader adds declarations to the end of the list
3585  // (so we need to see the struct "stat" before the function "stat").
3586  // Only emit declarations that aren't from a chained PCH, though.
3587  SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3588  IdResolver.end());
3589  for (NamedDecl *D : llvm::reverse(Decls))
3590  LE.write<uint32_t>(
3591  Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), D)));
3592  }
3593  }
3594 };
3595 
3596 } // namespace
3597 
3598 /// Write the identifier table into the AST file.
3599 ///
3600 /// The identifier table consists of a blob containing string data
3601 /// (the actual identifiers themselves) and a separate "offsets" index
3602 /// that maps identifier IDs to locations within the blob.
3603 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3604  IdentifierResolver &IdResolver,
3605  bool IsModule) {
3606  using namespace llvm;
3607 
3608  RecordData InterestingIdents;
3609 
3610  // Create and write out the blob that contains the identifier
3611  // strings.
3612  {
3613  llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3614  ASTIdentifierTableTrait Trait(
3615  *this, PP, IdResolver, IsModule,
3616  (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
3617 
3618  // Look for any identifiers that were named while processing the
3619  // headers, but are otherwise not needed. We add these to the hash
3620  // table to enable checking of the predefines buffer in the case
3621  // where the user adds new macro definitions when building the AST
3622  // file.
3624  for (const auto &ID : PP.getIdentifierTable())
3625  IIs.push_back(ID.second);
3626  // Sort the identifiers lexicographically before getting them references so
3627  // that their order is stable.
3628  llvm::sort(IIs, llvm::deref<std::less<>>());
3629  for (const IdentifierInfo *II : IIs)
3630  if (Trait.isInterestingNonMacroIdentifier(II))
3631  getIdentifierRef(II);
3632 
3633  // Create the on-disk hash table representation. We only store offsets
3634  // for identifiers that appear here for the first time.
3635  IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3636  for (auto IdentIDPair : IdentifierIDs) {
3637  auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3638  IdentID ID = IdentIDPair.second;
3639  assert(II && "NULL identifier in identifier table");
3640  // Write out identifiers if either the ID is local or the identifier has
3641  // changed since it was loaded.
3642  if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3643  || II->hasChangedSinceDeserialization() ||
3644  (Trait.needDecls() &&
3646  Generator.insert(II, ID, Trait);
3647  }
3648 
3649  // Create the on-disk hash table in a buffer.
3651  uint32_t BucketOffset;
3652  {
3653  using namespace llvm::support;
3654 
3655  llvm::raw_svector_ostream Out(IdentifierTable);
3656  // Make sure that no bucket is at offset 0
3657  endian::write<uint32_t>(Out, 0, little);
3658  BucketOffset = Generator.Emit(Out, Trait);
3659  }
3660 
3661  // Create a blob abbreviation
3662  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3663  Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3664  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3665  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3666  unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3667 
3668  // Write the identifier table
3669  RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3670  Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3671  }
3672 
3673  // Write the offsets table for identifier IDs.
3674  auto Abbrev = std::make_shared<BitCodeAbbrev>();
3675  Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3676  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3677  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3678  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3679  unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3680 
3681 #ifndef NDEBUG
3682  for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3683  assert(IdentifierOffsets[I] && "Missing identifier offset?");
3684 #endif
3685 
3686  RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3687  IdentifierOffsets.size(),
3688  FirstIdentID - NUM_PREDEF_IDENT_IDS};
3689  Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3690  bytes(IdentifierOffsets));
3691 
3692  // In C++, write the list of interesting identifiers (those that are
3693  // defined as macros, poisoned, or similar unusual things).
3694  if (!InterestingIdents.empty())
3695  Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3696 }
3697 
3698 //===----------------------------------------------------------------------===//
3699 // DeclContext's Name Lookup Table Serialization
3700 //===----------------------------------------------------------------------===//
3701 
3702 namespace {
3703 
3704 // Trait used for the on-disk hash table used in the method pool.
3705 class ASTDeclContextNameLookupTrait {
3706  ASTWriter &Writer;
3708 
3709 public:
3710  using key_type = DeclarationNameKey;
3711  using key_type_ref = key_type;
3712 
3713  /// A start and end index into DeclIDs, representing a sequence of decls.
3714  using data_type = std::pair<unsigned, unsigned>;
3715  using data_type_ref = const data_type &;
3716 
3717  using hash_value_type = unsigned;
3718  using offset_type = unsigned;
3719 
3720  explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3721 
3722  template<typename Coll>
3723  data_type getData(const Coll &Decls) {
3724  unsigned Start = DeclIDs.size();
3725  for (NamedDecl *D : Decls) {
3726  DeclIDs.push_back(
3727  Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3728  }
3729  return std::make_pair(Start, DeclIDs.size());
3730  }
3731 
3732  data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3733  unsigned Start = DeclIDs.size();
3734  llvm::append_range(DeclIDs, FromReader);
3735  return std::make_pair(Start, DeclIDs.size());
3736  }
3737 
3738  static bool EqualKey(key_type_ref a, key_type_ref b) {
3739  return a == b;
3740  }
3741 
3742  hash_value_type ComputeHash(DeclarationNameKey Name) {
3743  return Name.getHash();
3744  }
3745 
3746  void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3747  assert(Writer.hasChain() &&
3748  "have reference to loaded module file but no chain?");
3749 
3750  using namespace llvm::support;
3751 
3752  endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
3753  }
3754 
3755  std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3756  DeclarationNameKey Name,
3757  data_type_ref Lookup) {
3758  unsigned KeyLen = 1;
3759  switch (Name.getKind()) {
3766  KeyLen += 4;
3767  break;
3769  KeyLen += 1;
3770  break;
3775  break;
3776  }
3777 
3778  // 4 bytes for each DeclID.
3779  unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3780 
3781  return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3782  }
3783 
3784  void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3785  using namespace llvm::support;
3786 
3787  endian::Writer LE(Out, little);
3788  LE.write<uint8_t>(Name.getKind());
3789  switch (Name.getKind()) {
3793  LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3794  return;
3798  LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3799  return;
3801  assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3802  "Invalid operator?");
3803  LE.write<uint8_t>(Name.getOperatorKind());
3804  return;
3809  return;
3810  }
3811 
3812  llvm_unreachable("Invalid name kind?");
3813  }
3814 
3815  void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3816  unsigned DataLen) {
3817  using namespace llvm::support;
3818 
3819  endian::Writer LE(Out, little);
3820  uint64_t Start = Out.tell(); (void)Start;
3821  for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3822  LE.write<uint32_t>(DeclIDs[I]);
3823  assert(Out.tell() - Start == DataLen && "Data length is wrong");
3824  }
3825 };
3826 
3827 } // namespace
3828 
3829 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3830  DeclContext *DC) {
3831  return Result.hasExternalDecls() &&
3832  DC->hasNeedToReconcileExternalVisibleStorage();
3833 }
3834 
3835 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3836  DeclContext *DC) {
3837  for (auto *D : Result.getLookupResult())
3838  if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3839  return false;
3840 
3841  return true;
3842 }
3843 
3844 void
3845 ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3846  llvm::SmallVectorImpl<char> &LookupTable) {
3847  assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3848  !ConstDC->hasLazyExternalLexicalLookups() &&
3849  "must call buildLookups first");
3850 
3851  // FIXME: We need to build the lookups table, which is logically const.
3852  auto *DC = const_cast<DeclContext*>(ConstDC);
3853  assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3854 
3855  // Create the on-disk hash table representation.
3857  ASTDeclContextNameLookupTrait> Generator;
3858  ASTDeclContextNameLookupTrait Trait(*this);
3859 
3860  // The first step is to collect the declaration names which we need to
3861  // serialize into the name lookup table, and to collect them in a stable
3862  // order.
3864 
3865  // We also build up small sets of the constructor and conversion function
3866  // names which are visible.
3867  llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
3868 
3869  for (auto &Lookup : *DC->buildLookup()) {
3870  auto &Name = Lookup.first;
3871  auto &Result = Lookup.second;
3872 
3873  // If there are no local declarations in our lookup result, we
3874  // don't need to write an entry for the name at all. If we can't
3875  // write out a lookup set without performing more deserialization,
3876  // just skip this entry.
3877  if (isLookupResultExternal(Result, DC) &&
3878  isLookupResultEntirelyExternal(Result, DC))
3879  continue;
3880 
3881  // We also skip empty results. If any of the results could be external and
3882  // the currently available results are empty, then all of the results are
3883  // external and we skip it above. So the only way we get here with an empty
3884  // results is when no results could have been external *and* we have
3885  // external results.
3886  //
3887  // FIXME: While we might want to start emitting on-disk entries for negative
3888  // lookups into a decl context as an optimization, today we *have* to skip
3889  // them because there are names with empty lookup results in decl contexts
3890  // which we can't emit in any stable ordering: we lookup constructors and
3891  // conversion functions in the enclosing namespace scope creating empty
3892  // results for them. This in almost certainly a bug in Clang's name lookup,
3893  // but that is likely to be hard or impossible to fix and so we tolerate it
3894  // here by omitting lookups with empty results.
3895  if (Lookup.second.getLookupResult().empty())
3896  continue;
3897 
3898  switch (Lookup.first.getNameKind()) {
3899  default:
3900  Names.push_back(Lookup.first);
3901  break;
3902 
3904  assert(isa<CXXRecordDecl>(DC) &&
3905  "Cannot have a constructor name outside of a class!");
3906  ConstructorNameSet.insert(Name);
3907  break;
3908 
3910  assert(isa<CXXRecordDecl>(DC) &&
3911  "Cannot have a conversion function name outside of a class!");
3912  ConversionNameSet.insert(Name);
3913  break;
3914  }
3915  }
3916 
3917  // Sort the names into a stable order.
3918  llvm::sort(Names);
3919 
3920  if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
3921  // We need to establish an ordering of constructor and conversion function
3922  // names, and they don't have an intrinsic ordering.
3923 
3924  // First we try the easy case by forming the current context's constructor
3925  // name and adding that name first. This is a very useful optimization to
3926  // avoid walking the lexical declarations in many cases, and it also
3927  // handles the only case where a constructor name can come from some other
3928  // lexical context -- when that name is an implicit constructor merged from
3929  // another declaration in the redecl chain. Any non-implicit constructor or
3930  // conversion function which doesn't occur in all the lexical contexts
3931  // would be an ODR violation.
3932  auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3933  Context->getCanonicalType(Context->getRecordType(D)));
3934  if (ConstructorNameSet.erase(ImplicitCtorName))
3935  Names.push_back(ImplicitCtorName);
3936 
3937  // If we still have constructors or conversion functions, we walk all the
3938  // names in the decl and add the constructors and conversion functions
3939  // which are visible in the order they lexically occur within the context.
3940  if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3941  for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3942  if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3943  auto Name = ChildND->getDeclName();
3944  switch (Name.getNameKind()) {
3945  default:
3946  continue;
3947 
3949  if (ConstructorNameSet.erase(Name))
3950  Names.push_back(Name);
3951  break;
3952 
3954  if (ConversionNameSet.erase(Name))
3955  Names.push_back(Name);
3956  break;
3957  }
3958 
3959  if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3960  break;
3961  }
3962 
3963  assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3964  "constructors by walking all the "
3965  "lexical members of the context.");
3966  assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3967  "conversion functions by walking all "
3968  "the lexical members of the context.");
3969  }
3970 
3971  // Next we need to do a lookup with each name into this decl context to fully
3972  // populate any results from external sources. We don't actually use the
3973  // results of these lookups because we only want to use the results after all
3974  // results have been loaded and the pointers into them will be stable.
3975  for (auto &Name : Names)
3976  DC->lookup(Name);
3977 
3978  // Now we need to insert the results for each name into the hash table. For
3979  // constructor names and conversion function names, we actually need to merge
3980  // all of the results for them into one list of results each and insert
3981  // those.
3982  SmallVector<NamedDecl *, 8> ConstructorDecls;
3983  SmallVector<NamedDecl *, 8> ConversionDecls;
3984 
3985  // Now loop over the names, either inserting them or appending for the two
3986  // special cases.
3987  for (auto &Name : Names) {
3988  DeclContext::lookup_result Result = DC->noload_lookup(Name);
3989 
3990  switch (Name.getNameKind()) {
3991  default:
3992  Generator.insert(Name, Trait.getData(Result), Trait);
3993  break;
3994 
3996  ConstructorDecls.append(Result.begin(), Result.end());
3997  break;
3998 
4000  ConversionDecls.append(Result.begin(), Result.end());
4001  break;
4002  }
4003  }
4004 
4005  // Handle our two special cases if we ended up having any. We arbitrarily use
4006  // the first declaration's name here because the name itself isn't part of
4007  // the key, only the kind of name is used.
4008  if (!ConstructorDecls.empty())
4009  Generator.insert(ConstructorDecls.front()->getDeclName(),
4010  Trait.getData(ConstructorDecls), Trait);
4011  if (!ConversionDecls.empty())
4012  Generator.insert(ConversionDecls.front()->getDeclName(),
4013  Trait.getData(ConversionDecls), Trait);
4014 
4015  // Create the on-disk hash table. Also emit the existing imported and
4016  // merged table if there is one.
4017  auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4018  Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
4019 }
4020 
4021 /// Write the block containing all of the declaration IDs
4022 /// visible from the given DeclContext.
4023 ///
4024 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4025 /// bitstream, or 0 if no block was written.
4026 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4027  DeclContext *DC) {
4028  // If we imported a key declaration of this namespace, write the visible
4029  // lookup results as an update record for it rather than including them
4030  // on this declaration. We will only look at key declarations on reload.
4031  if (isa<NamespaceDecl>(DC) && Chain &&
4032  Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4033  // Only do this once, for the first local declaration of the namespace.
4034  for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
4035  Prev = Prev->getPreviousDecl())
4036  if (!Prev->isFromASTFile())
4037  return 0;
4038 
4039  // Note that we need to emit an update record for the primary context.
4040  UpdatedDeclContexts.insert(DC->getPrimaryContext());
4041 
4042  // Make sure all visible decls are written. They will be recorded later. We
4043  // do this using a side data structure so we can sort the names into
4044  // a deterministic order.
4047  LookupResults;
4048  if (Map) {
4049  LookupResults.reserve(Map->size());
4050  for (auto &Entry : *Map)
4051  LookupResults.push_back(
4052  std::make_pair(Entry.first, Entry.second.getLookupResult()));
4053  }
4054 
4055  llvm::sort(LookupResults, llvm::less_first());
4056  for (auto &NameAndResult : LookupResults) {
4057  DeclarationName Name = NameAndResult.first;
4058  DeclContext::lookup_result Result = NameAndResult.second;
4059  if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4060  Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4061  // We have to work around a name lookup bug here where negative lookup
4062  // results for these names get cached in namespace lookup tables (these
4063  // names should never be looked up in a namespace).
4064  assert(Result.empty() && "Cannot have a constructor or conversion "
4065  "function name in a namespace!");
4066  continue;
4067  }
4068 
4069  for (NamedDecl *ND : Result)
4070  if (!ND->isFromASTFile())
4071  GetDeclRef(ND);
4072  }
4073 
4074  return 0;
4075  }
4076 
4077  if (DC->getPrimaryContext() != DC)
4078  return 0;
4079 
4080  // Skip contexts which don't support name lookup.
4081  if (!DC->isLookupContext())
4082  return 0;
4083 
4084  // If not in C++, we perform name lookup for the translation unit via the
4085  // IdentifierInfo chains, don't bother to build a visible-declarations table.
4086  if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4087  return 0;
4088 
4089  // Serialize the contents of the mapping used for lookup. Note that,
4090  // although we have two very different code paths, the serialized
4091  // representation is the same for both cases: a declaration name,
4092  // followed by a size, followed by references to the visible
4093  // declarations that have that name.
4094  uint64_t Offset = Stream.GetCurrentBitNo();
4095  StoredDeclsMap *Map = DC->buildLookup();
4096  if (!Map || Map->empty())
4097  return 0;
4098 
4099  // Create the on-disk hash table in a buffer.
4100  SmallString<4096> LookupTable;
4101  GenerateNameLookupTable(DC, LookupTable);
4102 
4103  // Write the lookup table
4104  RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4105  Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
4106  LookupTable);
4107  ++NumVisibleDeclContexts;
4108  return Offset;
4109 }
4110 
4111 /// Write an UPDATE_VISIBLE block for the given context.
4112 ///
4113 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4114 /// DeclContext in a dependent AST file. As such, they only exist for the TU
4115 /// (in C++), for namespaces, and for classes with forward-declared unscoped
4116 /// enumeration members (in C++11).
4117 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
4118  StoredDeclsMap *Map = DC->getLookupPtr();
4119  if (!Map || Map->empty())
4120  return;
4121 
4122  // Create the on-disk hash table in a buffer.
4123  SmallString<4096> LookupTable;
4124  GenerateNameLookupTable(DC, LookupTable);
4125 
4126  // If we're updating a namespace, select a key declaration as the key for the
4127  // update record; those are the only ones that will be checked on reload.
4128  if (isa<NamespaceDecl>(DC))
4129  DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4130 
4131  // Write the lookup table
4132  RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
4133  Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
4134 }
4135 
4136 /// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4137 void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4138  RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4139  Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4140 }
4141 
4142 /// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4143 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4144  if (!SemaRef.Context.getLangOpts().OpenCL)
4145  return;
4146 
4147  const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4148  RecordData Record;
4149  for (const auto &I:Opts.OptMap) {
4150  AddString(I.getKey(), Record);
4151  auto V = I.getValue();
4152  Record.push_back(V.Supported ? 1 : 0);
4153  Record.push_back(V.Enabled ? 1 : 0);
4154  Record.push_back(V.WithPragma ? 1 : 0);
4155  Record.push_back(V.Avail);
4156  Record.push_back(V.Core);
4157  Record.push_back(V.Opt);
4158  }
4159  Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4160 }
4161 void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4162  if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4163  RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4164  Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4165  }
4166 }
4167 
4168 void ASTWriter::WriteObjCCategories() {
4169  SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4170  RecordData Categories;
4171 
4172  for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4173  unsigned Size = 0;
4174  unsigned StartIndex = Categories.size();
4175 
4176  ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4177 
4178  // Allocate space for the size.
4179  Categories.push_back(0);
4180 
4181  // Add the categories.
4183  Cat = Class->known_categories_begin(),
4184  CatEnd = Class->known_categories_end();
4185  Cat != CatEnd; ++Cat, ++Size) {
4186  assert(getDeclID(*Cat) != 0 && "Bogus category");
4187  AddDeclRef(*Cat, Categories);
4188  }
4189 
4190  // Update the size.
4191  Categories[StartIndex] = Size;
4192 
4193  // Record this interface -> category map.
4194  ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4195  CategoriesMap.push_back(CatInfo);
4196  }
4197 
4198  // Sort the categories map by the definition ID, since the reader will be
4199  // performing binary searches on this information.
4200  llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4201 
4202  // Emit the categories map.
4203  using namespace llvm;
4204 
4205  auto Abbrev = std::make_shared<BitCodeAbbrev>();
4206  Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4207  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4208  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4209  unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
4210 
4211  RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4212  Stream.EmitRecordWithBlob(AbbrevID, Record,
4213  reinterpret_cast<char *>(CategoriesMap.data()),
4214  CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4215 
4216  // Emit the category lists.
4217  Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4218 }
4219 
4220 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4222 
4223  if (LPTMap.empty())
4224  return;
4225 
4226  RecordData Record;
4227  for (auto &LPTMapEntry : LPTMap) {
4228  const FunctionDecl *FD = LPTMapEntry.first;
4229  LateParsedTemplate &LPT = *LPTMapEntry.second;
4230  AddDeclRef(FD, Record);
4231  AddDeclRef(LPT.D, Record);
4232  Record.push_back(LPT.Toks.size());
4233 
4234  for (const auto &Tok : LPT.Toks) {
4235  AddToken(Tok, Record);
4236  }
4237  }
4238  Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4239 }
4240 
4241 /// Write the state of 'pragma clang optimize' at the end of the module.
4242 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4243  RecordData Record;
4244  SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4245  AddSourceLocation(PragmaLoc, Record);
4246  Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4247 }
4248 
4249 /// Write the state of 'pragma ms_struct' at the end of the module.
4250 void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4251  RecordData Record;
4252  Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4253  Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4254 }
4255 
4256 /// Write the state of 'pragma pointers_to_members' at the end of the
4257 //module.
4258 void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4259  RecordData Record;
4260  Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4261  AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4262  Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4263 }
4264 
4265 /// Write the state of 'pragma align/pack' at the end of the module.
4266 void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4267  // Don't serialize pragma align/pack state for modules, since it should only
4268  // take effect on a per-submodule basis.
4269  if (WritingModule)
4270  return;
4271 
4272  RecordData Record;
4273  AddAlignPackInfo(SemaRef.AlignPackStack.CurrentValue, Record);
4274  AddSourceLocation(SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
4275  Record.push_back(SemaRef.AlignPackStack.Stack.size());
4276  for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
4277  AddAlignPackInfo(StackEntry.Value, Record);
4278  AddSourceLocation(StackEntry.PragmaLocation, Record);
4279  AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4280  AddString(StackEntry.StackSlotLabel, Record);
4281  }
4282  Stream.EmitRecord(ALIGN_PACK_PRAGMA_OPTIONS, Record);
4283 }
4284 
4285 /// Write the state of 'pragma float_control' at the end of the module.
4286 void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4287  // Don't serialize pragma float_control state for modules,
4288  // since it should only take effect on a per-submodule basis.
4289  if (WritingModule)
4290  return;
4291 
4292  RecordData Record;
4293  Record.push_back(SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4294  AddSourceLocation(SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4295  Record.push_back(SemaRef.FpPragmaStack.Stack.size());
4296  for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4297  Record.push_back(StackEntry.Value.getAsOpaqueInt());
4298  AddSourceLocation(StackEntry.PragmaLocation, Record);
4299  AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4300  AddString(StackEntry.StackSlotLabel, Record);
4301  }
4302  Stream.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS, Record);
4303 }
4304 
4305 void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4306  ModuleFileExtensionWriter &Writer) {
4307  // Enter the extension block.
4308  Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4309 
4310  // Emit the metadata record abbreviation.
4311  auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4312  Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4313  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4314  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4315  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4316  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4317  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4318  unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
4319 
4320  // Emit the metadata record.
4321  RecordData Record;
4322  auto Metadata = Writer.getExtension()->getExtensionMetadata();
4323  Record.push_back(EXTENSION_METADATA);
4324  Record.push_back(Metadata.MajorVersion);
4325  Record.push_back(Metadata.MinorVersion);
4326  Record.push_back(Metadata.BlockName.size());
4327  Record.push_back(Metadata.UserInfo.size());
4328  SmallString<64> Buffer;
4329  Buffer += Metadata.BlockName;
4330  Buffer += Metadata.UserInfo;
4331  Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4332 
4333  // Emit the contents of the extension block.
4334  Writer.writeExtensionContents(SemaRef, Stream);
4335 
4336  // Exit the extension block.
4337  Stream.ExitBlock();
4338 }
4339 
4340 //===----------------------------------------------------------------------===//
4341 // General Serialization Routines
4342 //===----------------------------------------------------------------------===//
4343 
4345  auto &Record = *this;
4346  // FIXME: Clang can't handle the serialization/deserialization of
4347  // preferred_name properly now. See
4348  // https://github.com/llvm/llvm-project/issues/56490 for example.
4349  if (!A || (isa<PreferredNameAttr>(A) &&
4350  Writer->isWritingStdCXXNamedModules()))
4351  return Record.push_back(0);
4352 
4353  Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4354 
4355  Record.AddIdentifierRef(A->getAttrName());
4356  Record.AddIdentifierRef(A->getScopeName());
4357  Record.AddSourceRange(A->getRange());
4358  Record.AddSourceLocation(A->getScopeLoc());
4359  Record.push_back(A->getParsedKind());
4360  Record.push_back(A->getSyntax());
4361  Record.push_back(A->getAttributeSpellingListIndexRaw());
4362 
4363 #include "clang/Serialization/AttrPCHWrite.inc"
4364 }
4365 
4366 /// Emit the list of attributes to the specified record.
4368  push_back(Attrs.size());
4369  for (const auto *A : Attrs)
4370  AddAttr(A);
4371 }
4372 
4373 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4374  AddSourceLocation(Tok.getLocation(), Record);
4375  // FIXME: Should translate token kind to a stable encoding.
4376  Record.push_back(Tok.getKind());
4377  // FIXME: Should translate token flags to a stable encoding.
4378  Record.push_back(Tok.getFlags());
4379 
4380  if (Tok.isAnnotation()) {
4381  AddSourceLocation(Tok.getAnnotationEndLoc(), Record);
4382  switch (Tok.getKind()) {
4383  case tok::annot_pragma_loop_hint: {
4384  auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
4385  AddToken(Info->PragmaName, Record);
4386  AddToken(Info->Option, Record);
4387  Record.push_back(Info->Toks.size());
4388  for (const auto &T : Info->Toks)
4389  AddToken(T, Record);
4390  break;
4391  }
4392  // Some annotation tokens do not use the PtrData field.
4393  case tok::annot_pragma_openmp:
4394  case tok::annot_pragma_openmp_end:
4395  case tok::annot_pragma_unused:
4396  break;
4397  default:
4398  llvm_unreachable("missing serialization code for annotation token");
4399  }
4400  } else {
4401  Record.push_back(Tok.getLength());
4402  // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4403  // is needed.
4404  AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4405  }
4406 }
4407 
4408 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4409  Record.push_back(Str.size());
4410  Record.insert(Record.end(), Str.begin(), Str.end());
4411 }
4412 
4414  assert(Context && "should have context when outputting path");
4415 
4416  bool Changed =
4418 
4419  // Remove a prefix to make the path relative, if relevant.
4420  const char *PathBegin = Path.data();
4421  const char *PathPtr =
4422  adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4423  if (PathPtr != PathBegin) {
4424  Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4425  Changed = true;
4426  }
4427 
4428  return Changed;
4429 }
4430 
4431 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4432  SmallString<128> FilePath(Path);
4433  PreparePathForOutput(FilePath);
4434  AddString(FilePath, Record);
4435 }
4436 
4437 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4438  StringRef Path) {
4439  SmallString<128> FilePath(Path);
4440  PreparePathForOutput(FilePath);
4441  Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4442 }
4443 
4444 void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4445  RecordDataImpl &Record) {
4446  Record.push_back(Version.getMajor());
4447  if (Optional<unsigned> Minor = Version.getMinor())
4448  Record.push_back(*Minor + 1);
4449  else
4450  Record.push_back(0);
4451  if (Optional<unsigned> Subminor = Version.getSubminor())
4452  Record.push_back(*Subminor + 1);
4453  else
4454  Record.push_back(0);
4455 }
4456 
4457 /// Note that the identifier II occurs at the given offset
4458 /// within the identifier table.
4460  IdentID ID = IdentifierIDs[II];
4461  // Only store offsets new to this AST file. Other identifier names are looked
4462  // up earlier in the chain and thus don't need an offset.
4463  if (ID >= FirstIdentID)
4464  IdentifierOffsets[ID - FirstIdentID] = Offset;
4465 }
4466 
4467 /// Note that the selector Sel occurs at the given offset
4468 /// within the method pool/selector table.
4470  unsigned ID = SelectorIDs[Sel];
4471  assert(ID && "Unknown selector");
4472  // Don't record offsets for selectors that are also available in a different
4473  // file.
4474  if (ID < FirstSelectorID)
4475  return;
4476  SelectorOffsets[ID - FirstSelectorID] = Offset;
4477 }
4478 
4479 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4480  SmallVectorImpl<char> &Buffer,
4481  InMemoryModuleCache &ModuleCache,
4482  ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4483  bool IncludeTimestamps)
4484  : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4485  IncludeTimestamps(IncludeTimestamps) {
4486  for (const auto &Ext : Extensions) {
4487  if (auto Writer = Ext->createExtensionWriter(*this))
4488  ModuleFileExtensionWriters.push_back(std::move(Writer));
4489  }
4490 }
4491 
4492 ASTWriter::~ASTWriter() = default;
4493 
4495  assert(WritingAST && "can't determine lang opts when not writing AST");
4496  return Context->getLangOpts();
4497 }
4498 
4500  return IncludeTimestamps ? E->getModificationTime() : 0;
4501 }
4502 
4503 ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
4504  Module *WritingModule, StringRef isysroot,
4505  bool hasErrors,
4506  bool ShouldCacheASTInMemory) {
4507  llvm::TimeTraceScope scope("WriteAST", OutputFile);
4508  WritingAST = true;
4509 
4510  ASTHasCompilerErrors = hasErrors;
4511 
4512  // Emit the file header.
4513  Stream.Emit((unsigned)'C', 8);
4514  Stream.Emit((unsigned)'P', 8);
4515  Stream.Emit((unsigned)'C', 8);
4516  Stream.Emit((unsigned)'H', 8);
4517 
4518  WriteBlockInfoBlock();
4519 
4520  Context = &SemaRef.Context;
4521  PP = &SemaRef.PP;
4522  this->WritingModule = WritingModule;
4523  ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
4524  Context = nullptr;
4525  PP = nullptr;
4526  this->WritingModule = nullptr;
4527  this->BaseDirectory.clear();
4528 
4529  WritingAST = false;
4530  if (ShouldCacheASTInMemory) {
4531  // Construct MemoryBuffer and update buffer manager.
4532  ModuleCache.addBuiltPCM(OutputFile,
4533  llvm::MemoryBuffer::getMemBufferCopy(
4534  StringRef(Buffer.begin(), Buffer.size())));
4535  }
4536  return Signature;
4537 }
4538 
4539 template<typename Vector>
4540 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4541  ASTWriter::RecordData &Record) {
4542  for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4543  I != E; ++I) {
4544  Writer.AddDeclRef(*I, Record);
4545  }
4546 }
4547 
4548 void ASTWriter::collectNonAffectingInputFiles() {
4549  SourceManager &SrcMgr = PP->getSourceManager();
4550  unsigned N = SrcMgr.local_sloc_entry_size();
4551 
4552  IsSLocAffecting.resize(N, true);
4553 
4554  if (!WritingModule)
4555  return;
4556 
4557  auto AffectingModuleMaps =
4558  GetAffectingModuleMaps(PP->getHeaderSearchInfo(), WritingModule);
4559 
4560  unsigned FileIDAdjustment = 0;
4561  unsigned OffsetAdjustment = 0;
4562 
4563  NonAffectingFileIDAdjustments.reserve(N);
4564  NonAffectingOffsetAdjustments.reserve(N);
4565 
4566  NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4567  NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4568 
4569  for (unsigned I = 1; I != N; ++I) {
4570  const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(I);
4571  FileID FID = FileID::get(I);
4572  assert(&SrcMgr.getSLocEntry(FID) == SLoc);
4573 
4574  if (!SLoc->isFile())
4575  continue;
4576  const SrcMgr::FileInfo &File = SLoc->getFile();
4577  const SrcMgr::ContentCache *Cache = &File.getContentCache();
4578  if (!Cache->OrigEntry)
4579  continue;
4580 
4581  if (!isModuleMap(File.getFileCharacteristic()) ||
4582  AffectingModuleMaps.empty() ||
4583  AffectingModuleMaps.find(Cache->OrigEntry) != AffectingModuleMaps.end())
4584  continue;
4585 
4586  IsSLocAffecting[I] = false;
4587 
4588  FileIDAdjustment += 1;
4589  // Even empty files take up one element in the offset table.
4590  OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
4591 
4592  // If the previous file was non-affecting as well, just extend its entry
4593  // with our information.
4594  if (!NonAffectingFileIDs.empty() &&
4595  NonAffectingFileIDs.back().ID == FID.ID - 1) {
4596  NonAffectingFileIDs.back() = FID;
4597  NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
4598  NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
4599  NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
4600  continue;
4601  }
4602 
4603  NonAffectingFileIDs.push_back(FID);
4604  NonAffectingRanges.emplace_back(SrcMgr.getLocForStartOfFile(FID),
4605  SrcMgr.getLocForEndOfFile(FID));
4606  NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4607  NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4608  }
4609 }
4610 
4611 ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4612  Module *WritingModule) {
4613  using namespace llvm;
4614 
4615  bool isModule = WritingModule != nullptr;
4616 
4617  // Make sure that the AST reader knows to finalize itself.
4618  if (Chain)
4619  Chain->finalizeForWriting();
4620 
4621  ASTContext &Context = SemaRef.Context;
4622  Preprocessor &PP = SemaRef.PP;
4623 
4624  collectNonAffectingInputFiles();
4625 
4626  // Set up predefined declaration IDs.
4627  auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4628  if (D) {
4629  assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4630  DeclIDs[D] = ID;
4631  }
4632  };
4633  RegisterPredefDecl(Context.getTranslationUnitDecl(),
4635  RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4636  RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4637  RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4638  RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4640  RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4641  RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4642  RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4644  RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
4645  RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
4646  RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4648  RegisterPredefDecl(Context.MSGuidTagDecl,
4650  RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
4651  RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4653  RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4655  RegisterPredefDecl(Context.CFConstantStringTagDecl,
4657  RegisterPredefDecl(Context.TypePackElementDecl,
4659 
4660  // Build a record containing all of the tentative definitions in this file, in
4661  // TentativeDefinitions order. Generally, this record will be empty for
4662  // headers.
4663  RecordData TentativeDefinitions;
4664  AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4665 
4666  // Build a record containing all of the file scoped decls in this file.
4667  RecordData UnusedFileScopedDecls;
4668  if (!isModule)
4670  UnusedFileScopedDecls);
4671 
4672  // Build a record containing all of the delegating constructors we still need
4673  // to resolve.
4674  RecordData DelegatingCtorDecls;
4675  if (!isModule)
4676  AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4677 
4678  // Write the set of weak, undeclared identifiers. We always write the
4679  // entire table, since later PCH files in a PCH chain are only interested in
4680  // the results at the end of the chain.
4681  RecordData WeakUndeclaredIdentifiers;
4682  for (const auto &WeakUndeclaredIdentifierList :
4683  SemaRef.WeakUndeclaredIdentifiers) {
4684  const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
4685  for (const auto &WI : WeakUndeclaredIdentifierList.second) {
4686  AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4687  AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4688  AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4689  }
4690  }
4691 
4692  // Build a record containing all of the ext_vector declarations.
4693  RecordData ExtVectorDecls;
4694  AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4695 
4696  // Build a record containing all of the VTable uses information.
4697  RecordData VTableUses;
4698  if (!SemaRef.VTableUses.empty()) {
4699  for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4700  AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4701  AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4702  VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4703  }
4704  }
4705 
4706  // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4707  RecordData UnusedLocalTypedefNameCandidates;
4708  for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4709  AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4710 
4711  // Build a record containing all of pending implicit instantiations.
4712  RecordData PendingInstantiations;
4713  for (const auto &I : SemaRef.PendingInstantiations) {
4714  AddDeclRef(I.first, PendingInstantiations);
4715  AddSourceLocation(I.second, PendingInstantiations);
4716  }
4717  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4718  "There are local ones at end of translation unit!");
4719 
4720  // Build a record containing some declaration references.
4721  RecordData SemaDeclRefs;
4722  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
4723  AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4724  AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4725  AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
4726  }
4727 
4728  RecordData CUDASpecialDeclRefs;
4729  if (Context.getcudaConfigureCallDecl()) {
4730  AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4731  }
4732 
4733  // Build a record containing all of the known namespaces.
4734  RecordData KnownNamespaces;
4735  for (const auto &I : SemaRef.KnownNamespaces) {
4736  if (!I.second)
4737  AddDeclRef(I.first, KnownNamespaces);
4738  }
4739 
4740  // Build a record of all used, undefined objects that require definitions.
4741  RecordData UndefinedButUsed;
4742 
4744  SemaRef.getUndefinedButUsed(Undefined);
4745  for (const auto &I : Undefined) {
4746  AddDeclRef(I.first, UndefinedButUsed);
4747  AddSourceLocation(I.second, UndefinedButUsed);
4748  }
4749 
4750  // Build a record containing all delete-expressions that we would like to
4751  // analyze later in AST.
4752  RecordData DeleteExprsToAnalyze;
4753 
4754  if (!isModule) {
4755  for (const auto &DeleteExprsInfo :
4756  SemaRef.getMismatchingDeleteExpressions()) {
4757  AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4758  DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4759  for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4760  AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4761  DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4762  }
4763  }
4764  }
4765 
4766  // Write the control block
4767  WriteControlBlock(PP, Context, isysroot);
4768 
4769  // Write the remaining AST contents.
4770  Stream.FlushToWord();
4771  ASTBlockRange.first = Stream.GetCurrentBitNo();
4772  Stream.EnterSubblock(AST_BLOCK_ID, 5);
4773  ASTBlockStartOffset = Stream.GetCurrentBitNo();
4774 
4775  // This is so that older clang versions, before the introduction
4776  // of the control block, can read and reject the newer PCH format.
4777  {
4778  RecordData Record = {VERSION_MAJOR};
4779  Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4780  }
4781 
4782  // Create a lexical update block containing all of the declarations in the
4783  // translation unit that do not come from other AST files.
4784  const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4785  SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4786  for (const auto *D : TU->noload_decls()) {
4787  if (!D->isFromASTFile()) {
4788  NewGlobalKindDeclPairs.push_back(D->getKind());
4789  NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4790  }
4791  }
4792 
4793  auto Abv = std::make_shared<BitCodeAbbrev>();
4794  Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4795  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4796  unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
4797  {
4798  RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4799  Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4800  bytes(NewGlobalKindDeclPairs));
4801  }
4802 
4803  // And a visible updates block for the translation unit.
4804  Abv = std::make_shared<BitCodeAbbrev>();
4805  Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4806  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4807  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4808  UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
4809  WriteDeclContextVisibleUpdate(TU);
4810 
4811  // If we have any extern "C" names, write out a visible update for them.
4812  if (Context.ExternCContext)
4813  WriteDeclContextVisibleUpdate(Context.ExternCContext);
4814 
4815  // If the translation unit has an anonymous namespace, and we don't already
4816  // have an update block for it, write it as an update block.
4817  // FIXME: Why do we not do this if there's already an update block?
4818  if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4819  ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4820  if (Record.empty())
4821  Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4822  }
4823 
4824  // Add update records for all mangling numbers and static local numbers.
4825  // These aren't really update records, but this is a convenient way of
4826  // tagging this rare extra data onto the declarations.
4827  for (const auto &Number : Context.MangleNumbers)
4828  if (!Number.first->isFromASTFile())
4829  DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4830  Number.second));
4831  for (const auto &Number : Context.StaticLocalNumbers)
4832  if (!Number.first->isFromASTFile())
4833  DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4834  Number.second));
4835 
4836  // Make sure visible decls, added to DeclContexts previously loaded from
4837  // an AST file, are registered for serialization. Likewise for template
4838  // specializations added to imported templates.
4839  for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
4840  GetDeclRef(I);
4841  }
4842 
4843  // Make sure all decls associated with an identifier are registered for
4844  // serialization, if we're storing decls with identifiers.
4845  if (!WritingModule || !getLangOpts().CPlusPlus) {
4847  for (const auto &ID : PP.getIdentifierTable()) {
4848  const IdentifierInfo *II = ID.second;
4849  if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4850  IIs.push_back(II);
4851  }
4852  // Sort the identifiers to visit based on their name.
4853  llvm::sort(IIs, llvm::deref<std::less<>>());
4854  for (const IdentifierInfo *II : IIs) {
4855  for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4856  DEnd = SemaRef.IdResolver.end();
4857  D != DEnd; ++D) {
4858  GetDeclRef(*D);
4859  }
4860  }
4861  }
4862 
4863  // For method pool in the module, if it contains an entry for a selector,
4864  // the entry should be complete, containing everything introduced by that
4865  // module and all modules it imports. It's possible that the entry is out of
4866  // date, so we need to pull in the new content here.
4867 
4868  // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4869  // safe, we copy all selectors out.
4870  llvm::SmallVector<Selector, 256> AllSelectors;
4871  for (auto &SelectorAndID : SelectorIDs)
4872  AllSelectors.push_back(SelectorAndID.first);
4873  for (auto &Selector : AllSelectors)
4875 
4876  // Form the record of special types.
4877  RecordData SpecialTypes;
4878  AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
4879  AddTypeRef(Context.getFILEType(), SpecialTypes);
4880  AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4881  AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4882  AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4883  AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
4884  AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
4885  AddTypeRef(Context.getucontext_tType(), SpecialTypes);
4886 
4887  if (Chain) {
4888  // Write the mapping information describing our module dependencies and how
4889  // each of those modules were mapped into our own offset/ID space, so that
4890  // the reader can build the appropriate mapping to its own offset/ID space.
4891  // The map consists solely of a blob with the following format:
4892  // *(module-kind:i8
4893  // module-name-len:i16 module-name:len*i8
4894  // source-location-offset:i32
4895  // identifier-id:i32
4896  // preprocessed-entity-id:i32
4897  // macro-definition-id:i32
4898  // submodule-id:i32
4899  // selector-id:i32
4900  // declaration-id:i32
4901  // c++-base-specifiers-id:i32
4902  // type-id:i32)
4903  //
4904  // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
4905  // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
4906  // module name. Otherwise, it is the module file name.
4907  auto Abbrev = std::make_shared<BitCodeAbbrev>();
4908  Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4909  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4910  unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
4911  SmallString<2048> Buffer;
4912  {
4913  llvm::raw_svector_ostream Out(Buffer);
4914  for (ModuleFile &M : Chain->ModuleMgr) {
4915  using namespace llvm::support;
4916 
4917  endian::Writer LE(Out, little);
4918  LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
4919  StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
4920  LE.write<uint16_t>(Name.size());
4921  Out.write(Name.data(), Name.size());
4922 
4923  // Note: if a base ID was uint max, it would not be possible to load
4924  // another module after it or have more than one entity inside it.
4926 
4927  auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
4928  assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4929  if (ShouldWrite)
4930  LE.write<uint32_t>(BaseID);
4931  else
4932  LE.write<uint32_t>(None);
4933  };
4934 
4935  // These values should be unique within a chain, since they will be read
4936  // as keys into ContinuousRangeMaps.
4937  writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4938  writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4939  writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4940  writeBaseIDOrNone(M.BasePreprocessedEntityID,
4942  writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4943  writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4944  writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4945  writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
4946  }
4947  }
4948  RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
4949  Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4950  Buffer.data(), Buffer.size());
4951  }
4952 
4953  // Build a record containing all of the DeclsToCheckForDeferredDiags.
4954  SmallVector<serialization::DeclID, 64> DeclsToCheckForDeferredDiags;
4955  for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
4956  DeclsToCheckForDeferredDiags.push_back(GetDeclRef(D));
4957 
4958  RecordData DeclUpdatesOffsetsRecord;
4959 
4960  // Keep writing types, declarations, and declaration update records
4961  // until we've emitted all of them.
4962  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4963  DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
4964  WriteTypeAbbrevs();
4965  WriteDeclAbbrevs();
4966  do {
4967  WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4968  while (!DeclTypesToEmit.empty()) {
4969  DeclOrType DOT = DeclTypesToEmit.front();
4970  DeclTypesToEmit.pop();
4971  if (DOT.isType())
4972  WriteType(DOT.getType());
4973  else
4974  WriteDecl(Context, DOT.getDecl());
4975  }
4976  } while (!DeclUpdates.empty());
4977  Stream.ExitBlock();
4978 
4979  DoneWritingDeclsAndTypes = true;
4980 
4981  // These things can only be done once we've written out decls and types.
4982  WriteTypeDeclOffsets();
4983  if (!DeclUpdatesOffsetsRecord.empty())
4984  Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4985  WriteFileDeclIDsMap();
4986  WriteSourceManagerBlock(Context.getSourceManager(), PP);
4987  WriteComments();
4988  WritePreprocessor(PP, isModule);
4989  WriteHeaderSearch(PP.getHeaderSearchInfo());
4990  WriteSelectors(SemaRef);
4991  WriteReferencedSelectorsPool(SemaRef);
4992  WriteLateParsedTemplates(SemaRef);
4993  WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
4994  WriteFPPragmaOptions(SemaRef.CurFPFeatureOverrides());
4995  WriteOpenCLExtensions(SemaRef);
4996  WriteCUDAPragmas(SemaRef);
4997 
4998  // If we're emitting a module, write out the submodule information.
4999  if (WritingModule)
5000  WriteSubmodules(WritingModule);
5001 
5002  Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5003 
5004  // Write the record containing external, unnamed definitions.
5005  if (!EagerlyDeserializedDecls.empty())
5006  Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
5007 
5008  if (!ModularCodegenDecls.empty())
5009  Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5010 
5011  // Write the record containing tentative definitions.
5012  if (!TentativeDefinitions.empty())
5013  Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
5014 
5015  // Write the record containing unused file scoped decls.
5016  if (!UnusedFileScopedDecls.empty())
5017  Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
5018 
5019  // Write the record containing weak undeclared identifiers.
5020  if (!WeakUndeclaredIdentifiers.empty())
5021  Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
5022  WeakUndeclaredIdentifiers);
5023 
5024  // Write the record containing ext_vector type names.
5025  if (!ExtVectorDecls.empty())
5026  Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
5027 
5028  // Write the record containing VTable uses information.
5029  if (!VTableUses.empty())
5030  Stream.EmitRecord(VTABLE_USES, VTableUses);
5031 
5032  // Write the record containing potentially unused local typedefs.
5033  if (!UnusedLocalTypedefNameCandidates.empty())
5034  Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5035  UnusedLocalTypedefNameCandidates);
5036 
5037  // Write the record containing pending implicit instantiations.
5038  if (!PendingInstantiations.empty())
5039  Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
5040 
5041  // Write the record containing declaration references of Sema.
5042  if (!SemaDeclRefs.empty())
5043  Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
5044 
5045  // Write the record containing decls to be checked for deferred diags.
5046  if (!DeclsToCheckForDeferredDiags.empty())
5047  Stream.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5048  DeclsToCheckForDeferredDiags);
5049 
5050  // Write the record containing CUDA-specific declaration references.
5051  if (!CUDASpecialDeclRefs.empty())
5052  Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
5053 
5054  // Write the delegating constructors.
5055  if (!DelegatingCtorDecls.empty())
5056  Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
5057 
5058  // Write the known namespaces.
5059  if (!KnownNamespaces.empty())
5060  Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
5061 
5062  // Write the undefined internal functions and variables, and inline functions.
5063  if (!UndefinedButUsed.empty())
5064  Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
5065 
5066  if (!DeleteExprsToAnalyze.empty())
5067  Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5068 
5069  // Write the visible updates to DeclContexts.
5070  for (auto *DC : UpdatedDeclContexts)
5071  WriteDeclContextVisibleUpdate(DC);
5072 
5073  if (!WritingModule) {
5074  // Write the submodules that were imported, if any.
5075  struct ModuleInfo {
5076  uint64_t ID;
5077  Module *M;
5078  ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5079  };
5081  for (const auto *I : Context.local_imports()) {
5082  assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
5083  Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5084  I->getImportedModule()));
5085  }
5086 
5087  if (!Imports.empty()) {
5088  auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5089  return A.ID < B.ID;
5090  };
5091  auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5092  return A.ID == B.ID;
5093  };
5094 
5095  // Sort and deduplicate module IDs.
5096  llvm::sort(Imports, Cmp);
5097  Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
5098  Imports.end());
5099 
5100  RecordData ImportedModules;
5101  for (const auto &Import : Imports) {
5102  ImportedModules.push_back(Import.ID);
5103  // FIXME: If the module has macros imported then later has declarations
5104  // imported, this location won't be the right one as a location for the
5105  // declaration imports.
5106  AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
5107  }
5108 
5109  Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5110  }
5111  }
5112 
5113  WriteObjCCategories();
5114  if(!WritingModule) {
5115  WriteOptimizePragmaOptions(SemaRef);
5116  WriteMSStructPragmaOptions(SemaRef);
5117  WriteMSPointersToMembersPragmaOptions(SemaRef);
5118  }
5119  WritePackPragmaOptions(SemaRef);
5120  WriteFloatControlPragmaOptions(SemaRef);
5121 
5122  // Some simple statistics
5123  RecordData::value_type Record[] = {
5124  NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
5125  Stream.EmitRecord(STATISTICS, Record);
5126  Stream.ExitBlock();
5127  Stream.FlushToWord();
5128  ASTBlockRange.second = Stream.GetCurrentBitNo();
5129 
5130  // Write the module file extension blocks.
5131  for (const auto &ExtWriter : ModuleFileExtensionWriters)
5132  WriteModuleFileExtension(SemaRef, *ExtWriter);
5133 
5134  return writeUnhashedControlBlock(PP, Context);
5135 }
5136 
5137 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
5138  if (DeclUpdates.empty())
5139  return;
5140 
5141  DeclUpdateMap LocalUpdates;
5142  LocalUpdates.swap(DeclUpdates);
5143 
5144  for (auto &DeclUpdate : LocalUpdates) {
5145  const Decl *D = DeclUpdate.first;
5146 
5147  bool HasUpdatedBody = false;
5149  ASTRecordWriter Record(*this, RecordData);
5150  for (auto &Update : DeclUpdate.second) {
5152 
5153  // An updated body is emitted last, so that the reader doesn't need
5154  // to skip over the lazy body to reach statements for other records.
5156  HasUpdatedBody = true;
5157  else
5158  Record.push_back(Kind);
5159 
5160  switch (Kind) {
5164  assert(Update.getDecl() && "no decl to add?");
5165  Record.push_back(GetDeclRef(Update.getDecl()));
5166  break;
5167 
5169  break;
5170 
5172  // FIXME: Do we need to also save the template specialization kind here?
5173  Record.AddSourceLocation(Update.getLoc());
5174  break;
5175 
5177  const VarDecl *VD = cast<VarDecl>(D);
5178  Record.push_back(VD->isInline());
5179  Record.push_back(VD->isInlineSpecified());
5180  Record.AddVarDeclInit(VD);
5181  break;
5182  }
5183 
5185  Record.AddStmt(const_cast<Expr *>(
5186  cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
5187  break;
5188 
5190  Record.AddStmt(
5191  cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5192  break;
5193 
5195  auto *RD = cast<CXXRecordDecl>(D);
5196  UpdatedDeclContexts.insert(RD->getPrimaryContext());
5197  Record.push_back(RD->isParamDestroyedInCallee());
5198  Record.push_back(RD->getArgPassingRestrictions());
5199  Record.AddCXXDefinitionData(RD);
5200  Record.AddOffset(WriteDeclContextLexicalBlock(
5201  *Context, const_cast<CXXRecordDecl *>(RD)));
5202 
5203  // This state is sometimes updated by template instantiation, when we
5204  // switch from the specialization referring to the template declaration
5205  // to it referring to the template definition.
5206  if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5207  Record.push_back(MSInfo->getTemplateSpecializationKind());
5208  Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
5209  } else {
5210  auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5211  Record.push_back(Spec->getTemplateSpecializationKind());
5212  Record.AddSourceLocation(Spec->getPointOfInstantiation());
5213 
5214  // The instantiation might have been resolved to a partial
5215  // specialization. If so, record which one.
5216  auto From = Spec->getInstantiatedFrom();
5217  if (auto PartialSpec =
5218  From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5219  Record.push_back(true);
5220  Record.AddDeclRef(PartialSpec);
5221  Record.AddTemplateArgumentList(
5222  &Spec->getTemplateInstantiationArgs());
5223  } else {
5224  Record.push_back(false);
5225  }
5226  }
5227  Record.push_back(RD->getTagKind());
5228  Record.AddSourceLocation(RD->getLocation());
5229  Record.AddSourceLocation(RD->getBeginLoc());
5230  Record.AddSourceRange(RD->getBraceRange());
5231 
5232  // Instantiation may change attributes; write them all out afresh.
5233  Record.push_back(D->hasAttrs());
5234  if (D->hasAttrs())
5235  Record.AddAttributes(D->getAttrs());
5236 
5237  // FIXME: Ensure we don't get here for explicit instantiations.
5238  break;
5239  }
5240 
5242  Record.AddDeclRef(Update.getDecl());
5243  Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
5244  break;
5245 
5247  auto prototype =
5248  cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
5249  Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
5250  break;
5251  }
5252 
5254  Record.push_back(GetOrCreateTypeID(Update.getType()));
5255  break;
5256 
5257  case UPD_DECL_MARKED_USED:
5258  break;
5259 
5260  case UPD_MANGLING_NUMBER:
5262  Record.push_back(Update.getNumber());
5263  break;
5264 
5266  Record.AddSourceRange(
5267  D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
5268  break;
5269 
5271  auto *A = D->getAttr<OMPAllocateDeclAttr>();
5272  Record.push_back(A->getAllocatorType());
5273  Record.AddStmt(A->getAllocator());
5274  Record.AddStmt(A->getAlignment());
5275  Record.AddSourceRange(A->getRange());
5276  break;
5277  }
5278 
5280  Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
5281  Record.AddSourceRange(
5282  D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5283  break;
5284 
5285  case UPD_DECL_EXPORTED:
5286  Record.push_back(getSubmoduleID(Update.getModule()));
5287  break;
5288 
5290  Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
5291  break;
5292  }
5293  }
5294 
5295  if (HasUpdatedBody) {
5296  const auto *Def = cast<FunctionDecl>(D);
5297  Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
5298  Record.push_back(Def->isInlined());
5299  Record.AddSourceLocation(Def->getInnerLocStart());
5300  Record.AddFunctionDefinition(Def);
5301  }
5302 
5303  OffsetsRecord.push_back(GetDeclRef(D));
5304  OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
5305  }
5306 }
5307 
5309  RecordDataImpl &Record) {
5310  uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
5311  Record.push_back(Raw);
5312 }
5313 
5314 FileID ASTWriter::getAdjustedFileID(FileID FID) const {
5315  if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
5316  NonAffectingFileIDs.empty())
5317  return FID;
5318  auto It = llvm::lower_bound(NonAffectingFileIDs, FID);
5319  unsigned Idx = std::distance(NonAffectingFileIDs.begin(), It);
5320  unsigned Offset = NonAffectingFileIDAdjustments[Idx];
5321  return FileID::get(FID.getOpaqueValue() - Offset);
5322 }
5323 
5324 unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
5325  unsigned NumCreatedFIDs = PP->getSourceManager()
5326  .getLocalSLocEntry(FID.ID)
5327  .getFile()
5328  .NumCreatedFIDs;
5329 
5330  unsigned AdjustedNumCreatedFIDs = 0;
5331  for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
5332  if (IsSLocAffecting[I])
5333  ++AdjustedNumCreatedFIDs;
5334  return AdjustedNumCreatedFIDs;
5335 }
5336 
5337 SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
5338  if (Loc.isInvalid())
5339  return Loc;
5340  return Loc.getLocWithOffset(-getAdjustment(Loc.getOffset()));
5341 }
5342 
5343 SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
5344  return SourceRange(getAdjustedLocation(Range.getBegin()),
5345  getAdjustedLocation(Range.getEnd()));
5346 }
5347 
5349 ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
5350  return Offset - getAdjustment(Offset);
5351 }
5352 
5354 ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
5355  if (NonAffectingRanges.empty())
5356  return 0;
5357 
5358  if (PP->getSourceManager().isLoadedOffset(Offset))
5359  return 0;
5360 
5361  if (Offset > NonAffectingRanges.back().getEnd().getOffset())
5362  return NonAffectingOffsetAdjustments.back();
5363 
5364  if (Offset < NonAffectingRanges.front().getBegin().getOffset())
5365  return 0;
5366 
5367  auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
5368  return Range.getEnd().getOffset() < Offset;
5369  };
5370 
5371  auto It = llvm::lower_bound(NonAffectingRanges, Offset, Contains);
5372  unsigned Idx = std::distance(NonAffectingRanges.begin(), It);
5373  return NonAffectingOffsetAdjustments[Idx];
5374 }
5375 
5377  Record.push_back(getAdjustedFileID(FID).getOpaqueValue());
5378 }
5379 
5381  SourceLocationSequence *Seq) {
5382  Loc = getAdjustedLocation(Loc);
5383  Record.push_back(SourceLocationEncoding::encode(Loc, Seq));
5384 }
5385 
5387  SourceLocationSequence *Seq) {
5388  AddSourceLocation(Range.getBegin(), Record, Seq);
5389  AddSourceLocation(Range.getEnd(), Record, Seq);
5390 }
5391 
5392 void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5393  AddAPInt(Value.bitcastToAPInt());
5394 }
5395 
5397  Record.push_back(getIdentifierRef(II));
5398 }
5399 
5401  if (!II)
5402  return 0;
5403 
5404  IdentID &ID = IdentifierIDs[II];
5405  if (ID == 0)
5406  ID = NextIdentID++;
5407  return ID;
5408 }
5409 
5411  // Don't emit builtin macros like __LINE__ to the AST file unless they
5412  // have been redefined by the header (in which case they are not
5413  // isBuiltinMacro).
5414  if (!MI || MI->isBuiltinMacro())
5415  return 0;
5416 
5417  MacroID &ID = MacroIDs[MI];
5418  if (ID == 0) {
5419  ID = NextMacroID++;
5420  MacroInfoToEmitData Info = { Name, MI, ID };
5421  MacroInfosToEmit.push_back(Info);
5422  }
5423  return ID;
5424 }
5425 
5427  if (!MI || MI->isBuiltinMacro())
5428  return 0;
5429 
5430  assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5431  return MacroIDs[MI];
5432 }
5433 
5435  return IdentMacroDirectivesOffsetMap.lookup(Name);
5436 }
5437 
5439  Record->push_back(Writer->getSelectorRef(SelRef));
5440 }
5441 
5443  if (Sel.getAsOpaquePtr() == nullptr) {
5444  return 0;
5445  }
5446 
5447  SelectorID SID = SelectorIDs[Sel];
5448  if (SID == 0 && Chain) {
5449  // This might trigger a ReadSelector callback, which will set the ID for
5450  // this selector.
5451  Chain->LoadSelector(Sel);
5452  SID = SelectorIDs[Sel];
5453  }
5454  if (SID == 0) {
5455  SID = NextSelectorID++;
5456  SelectorIDs[Sel] = SID;
5457  }
5458  return SID;
5459 }
5460 
5462  AddDeclRef(Temp->getDestructor());
5463 }
5464 
5467  switch (Kind) {
5469  AddStmt(Arg.getAsExpr());
5470  break;
5472  AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
5473  break;
5475  AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5477  break;
5479  AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5482  break;
5488  // FIXME: Is this right?
5489  break;
5490  }
5491 }
5492 
5494  AddTemplateArgument(Arg.getArgument());
5495 
5497  bool InfoHasSameExpr
5498  = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5499  Record->push_back(InfoHasSameExpr);
5500  if (InfoHasSameExpr)
5501  return; // Avoid storing the same expr twice.
5502  }
5503  AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
5504 }
5505 
5507  if (!TInfo) {
5508  AddTypeRef(QualType());
5509  return;
5510  }
5511 
5512  AddTypeRef(TInfo->getType());
5513  AddTypeLoc(TInfo->getTypeLoc());
5514 }
5515 
5517  LocSeq::State Seq(OuterSeq);
5518  TypeLocWriter TLW(*this, Seq);
5519  for (; !TL.isNull(); TL = TL.getNextTypeLoc())
5520  TLW.Visit(TL);
5521 }
5522 
5524  Record.push_back(GetOrCreateTypeID(T));
5525 }
5526 
5528  assert(Context);
5529  return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5530  if (T.isNull())
5531  return TypeIdx();
5532  assert(!T.getLocalFastQualifiers());
5533 
5534  TypeIdx &Idx = TypeIdxs[T];
5535  if (Idx.getIndex() == 0) {
5536  if (DoneWritingDeclsAndTypes) {
5537  assert(0 && "New type seen after serializing all the types to emit!");
5538  return TypeIdx();
5539  }
5540 
5541  // We haven't seen this type before. Assign it a new ID and put it
5542  // into the queue of types to emit.
5543  Idx = TypeIdx(NextTypeID++);
5544  DeclTypesToEmit.push(T);
5545  }
5546  return Idx;
5547  });
5548 }
5549 
5551  assert(Context);
5552  return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5553  if (T.isNull())
5554  return TypeIdx();
5555  assert(!T.getLocalFastQualifiers());
5556 
5557  TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5558  assert(I != TypeIdxs.end() && "Type not emitted!");
5559  return I->second;
5560  });
5561 }
5562 
5563 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
5564  Record.push_back(GetDeclRef(D));
5565 }
5566 
5568  assert(WritingAST && "Cannot request a declaration ID before AST writing");
5569 
5570  if (!D) {
5571  return 0;
5572  }
5573 
5574  // If D comes from an AST file, its declaration ID is already known and
5575  // fixed.
5576  if (D->isFromASTFile())
5577  return D->getGlobalID();
5578 
5579  assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
5580  DeclID &ID = DeclIDs[D];
5581  if (ID == 0) {
5582  if (DoneWritingDeclsAndTypes) {
5583  assert(0 && "New decl seen after serializing all the decls to emit!");
5584  return 0;
5585  }
5586 
5587  // We haven't seen this declaration before. Give it a new ID and
5588  // enqueue it in the list of declarations to emit.
5589  ID = NextDeclID++;
5590  DeclTypesToEmit.push(const_cast<Decl *>(D));
5591  }
5592 
5593  return ID;
5594 }
5595 
5597  if (!D)
5598  return 0;
5599 
5600  // If D comes from an AST file, its declaration ID is already known and
5601  // fixed.
5602  if (D->isFromASTFile())
5603  return D->getGlobalID();
5604 
5605  assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5606  return DeclIDs[D];
5607 }
5608 
5609 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5610  assert(ID);
5611  assert(D);
5612 
5613  SourceLocation Loc = D->getLocation();
5614  if (Loc.isInvalid())
5615  return;
5616 
5617  // We only keep track of the file-level declarations of each file.
5618  if (!D->getLexicalDeclContext()->isFileContext())
5619  return;
5620  // FIXME: ParmVarDecls that are part of a function type of a parameter of
5621  // a function/objc method, should not have TU as lexical context.
5622  // TemplateTemplateParmDecls that are part of an alias template, should not
5623  // have TU as lexical context.
5624  if (isa<ParmVarDecl, TemplateTemplateParmDecl>(D))
5625  return;
5626 
5627  SourceManager &SM = Context->getSourceManager();
5628  SourceLocation FileLoc = SM.getFileLoc(Loc);
5629  assert(SM.isLocalSourceLocation(FileLoc));
5630  FileID FID;
5631  unsigned Offset;
5632  std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5633  if (FID.isInvalid())
5634  return;
5635  assert(SM.getSLocEntry(FID).isFile());
5636  assert(IsSLocAffecting[FID.ID]);
5637 
5638  std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
5639  if (!Info)
5640  Info = std::make_unique<DeclIDInFileInfo>();
5641 
5642  std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5643  LocDeclIDsTy &Decls = Info->DeclIDs;
5644  Decls.push_back(LocDecl);
5645 }
5646 
5648  assert(needsAnonymousDeclarationNumber(D) &&
5649  "expected an anonymous declaration");
5650 
5651  // Number the anonymous declarations within this context, if we've not
5652  // already done so.
5653  auto It = AnonymousDeclarationNumbers.find(D);
5654  if (It == AnonymousDeclarationNumbers.end()) {
5655  auto *DC = D->getLexicalDeclContext();
5656  numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5657  AnonymousDeclarationNumbers[ND] = Number;
5658  });
5659 
5660  It = AnonymousDeclarationNumbers.find(D);
5661  assert(It != AnonymousDeclarationNumbers.end() &&
5662  "declaration not found within its lexical context");
5663  }
5664 
5665  return It->second;
5666 }
5667 
5669  DeclarationName Name) {
5670  switch (Name.getNameKind()) {
5674  AddTypeSourceInfo(DNLoc.getNamedTypeInfo());
5675  break;
5676 
5678