clang  16.0.0git
ASTReader.cpp
Go to the documentation of this file.
1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
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 ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclBase.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclGroup.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/Expr.h"
30 #include "clang/AST/ExprCXX.h"
34 #include "clang/AST/ODRHash.h"
35 #include "clang/AST/OpenMPClause.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
44 #include "clang/Basic/Diagnostic.h"
52 #include "clang/Basic/LLVM.h"
54 #include "clang/Basic/Module.h"
59 #include "clang/Basic/Sanitizers.h"
63 #include "clang/Basic/Specifiers.h"
64 #include "clang/Basic/TargetInfo.h"
66 #include "clang/Basic/TokenKinds.h"
67 #include "clang/Basic/Version.h"
68 #include "clang/Lex/HeaderSearch.h"
70 #include "clang/Lex/MacroInfo.h"
71 #include "clang/Lex/ModuleMap.h"
73 #include "clang/Lex/Preprocessor.h"
75 #include "clang/Lex/Token.h"
77 #include "clang/Sema/Scope.h"
78 #include "clang/Sema/Sema.h"
79 #include "clang/Sema/Weak.h"
91 #include "llvm/ADT/APFloat.h"
92 #include "llvm/ADT/APInt.h"
93 #include "llvm/ADT/APSInt.h"
94 #include "llvm/ADT/ArrayRef.h"
95 #include "llvm/ADT/DenseMap.h"
96 #include "llvm/ADT/FloatingPointMode.h"
97 #include "llvm/ADT/FoldingSet.h"
98 #include "llvm/ADT/Hashing.h"
99 #include "llvm/ADT/IntrusiveRefCntPtr.h"
100 #include "llvm/ADT/None.h"
101 #include "llvm/ADT/Optional.h"
102 #include "llvm/ADT/STLExtras.h"
103 #include "llvm/ADT/ScopeExit.h"
104 #include "llvm/ADT/SmallPtrSet.h"
105 #include "llvm/ADT/SmallString.h"
106 #include "llvm/ADT/SmallVector.h"
107 #include "llvm/ADT/StringExtras.h"
108 #include "llvm/ADT/StringMap.h"
109 #include "llvm/ADT/StringRef.h"
110 #include "llvm/ADT/Triple.h"
111 #include "llvm/ADT/iterator_range.h"
112 #include "llvm/Bitstream/BitstreamReader.h"
113 #include "llvm/Support/Casting.h"
114 #include "llvm/Support/Compiler.h"
115 #include "llvm/Support/Compression.h"
116 #include "llvm/Support/DJB.h"
117 #include "llvm/Support/Endian.h"
118 #include "llvm/Support/Error.h"
119 #include "llvm/Support/ErrorHandling.h"
120 #include "llvm/Support/FileSystem.h"
121 #include "llvm/Support/LEB128.h"
122 #include "llvm/Support/MemoryBuffer.h"
123 #include "llvm/Support/Path.h"
124 #include "llvm/Support/SaveAndRestore.h"
125 #include "llvm/Support/TimeProfiler.h"
126 #include "llvm/Support/Timer.h"
127 #include "llvm/Support/VersionTuple.h"
128 #include "llvm/Support/raw_ostream.h"
129 #include <algorithm>
130 #include <cassert>
131 #include <cstddef>
132 #include <cstdint>
133 #include <cstdio>
134 #include <ctime>
135 #include <iterator>
136 #include <limits>
137 #include <map>
138 #include <memory>
139 #include <string>
140 #include <system_error>
141 #include <tuple>
142 #include <utility>
143 #include <vector>
144 
145 using namespace clang;
146 using namespace clang::serialization;
147 using namespace clang::serialization::reader;
148 using llvm::BitstreamCursor;
149 
150 //===----------------------------------------------------------------------===//
151 // ChainedASTReaderListener implementation
152 //===----------------------------------------------------------------------===//
153 
154 bool
156  return First->ReadFullVersionInformation(FullVersion) ||
157  Second->ReadFullVersionInformation(FullVersion);
158 }
159 
160 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
161  First->ReadModuleName(ModuleName);
162  Second->ReadModuleName(ModuleName);
163 }
164 
165 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
166  First->ReadModuleMapFile(ModuleMapPath);
167  Second->ReadModuleMapFile(ModuleMapPath);
168 }
169 
170 bool
172  bool Complain,
173  bool AllowCompatibleDifferences) {
174  return First->ReadLanguageOptions(LangOpts, Complain,
175  AllowCompatibleDifferences) ||
176  Second->ReadLanguageOptions(LangOpts, Complain,
177  AllowCompatibleDifferences);
178 }
179 
181  const TargetOptions &TargetOpts, bool Complain,
182  bool AllowCompatibleDifferences) {
183  return First->ReadTargetOptions(TargetOpts, Complain,
184  AllowCompatibleDifferences) ||
185  Second->ReadTargetOptions(TargetOpts, Complain,
186  AllowCompatibleDifferences);
187 }
188 
190  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
191  return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
192  Second->ReadDiagnosticOptions(DiagOpts, Complain);
193 }
194 
195 bool
197  bool Complain) {
198  return First->ReadFileSystemOptions(FSOpts, Complain) ||
199  Second->ReadFileSystemOptions(FSOpts, Complain);
200 }
201 
203  const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
204  bool Complain) {
205  return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
206  Complain) ||
207  Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
208  Complain);
209 }
210 
212  const PreprocessorOptions &PPOpts, bool Complain,
213  std::string &SuggestedPredefines) {
214  return First->ReadPreprocessorOptions(PPOpts, Complain,
215  SuggestedPredefines) ||
216  Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
217 }
218 
220  unsigned Value) {
221  First->ReadCounter(M, Value);
222  Second->ReadCounter(M, Value);
223 }
224 
226  return First->needsInputFileVisitation() ||
227  Second->needsInputFileVisitation();
228 }
229 
231  return First->needsSystemInputFileVisitation() ||
232  Second->needsSystemInputFileVisitation();
233 }
234 
236  ModuleKind Kind) {
237  First->visitModuleFile(Filename, Kind);
238  Second->visitModuleFile(Filename, Kind);
239 }
240 
242  bool isSystem,
243  bool isOverridden,
244  bool isExplicitModule) {
245  bool Continue = false;
246  if (First->needsInputFileVisitation() &&
247  (!isSystem || First->needsSystemInputFileVisitation()))
248  Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
249  isExplicitModule);
250  if (Second->needsInputFileVisitation() &&
251  (!isSystem || Second->needsSystemInputFileVisitation()))
252  Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
253  isExplicitModule);
254  return Continue;
255 }
256 
258  const ModuleFileExtensionMetadata &Metadata) {
259  First->readModuleFileExtension(Metadata);
260  Second->readModuleFileExtension(Metadata);
261 }
262 
263 //===----------------------------------------------------------------------===//
264 // PCH validator implementation
265 //===----------------------------------------------------------------------===//
266 
268 
269 /// Compare the given set of language options against an existing set of
270 /// language options.
271 ///
272 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
273 /// \param AllowCompatibleDifferences If true, differences between compatible
274 /// language options will be permitted.
275 ///
276 /// \returns true if the languagae options mis-match, false otherwise.
277 static bool checkLanguageOptions(const LangOptions &LangOpts,
278  const LangOptions &ExistingLangOpts,
279  DiagnosticsEngine *Diags,
280  bool AllowCompatibleDifferences = true) {
281 #define LANGOPT(Name, Bits, Default, Description) \
282  if (ExistingLangOpts.Name != LangOpts.Name) { \
283  if (Diags) \
284  Diags->Report(diag::err_pch_langopt_mismatch) \
285  << Description << LangOpts.Name << ExistingLangOpts.Name; \
286  return true; \
287  }
288 
289 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
290  if (ExistingLangOpts.Name != LangOpts.Name) { \
291  if (Diags) \
292  Diags->Report(diag::err_pch_langopt_value_mismatch) \
293  << Description; \
294  return true; \
295  }
296 
297 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
298  if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
299  if (Diags) \
300  Diags->Report(diag::err_pch_langopt_value_mismatch) \
301  << Description; \
302  return true; \
303  }
304 
305 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
306  if (!AllowCompatibleDifferences) \
307  LANGOPT(Name, Bits, Default, Description)
308 
309 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
310  if (!AllowCompatibleDifferences) \
311  ENUM_LANGOPT(Name, Bits, Default, Description)
312 
313 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
314  if (!AllowCompatibleDifferences) \
315  VALUE_LANGOPT(Name, Bits, Default, Description)
316 
317 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
318 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
319 #define BENIGN_VALUE_LANGOPT(Name, Bits, Default, Description)
320 #include "clang/Basic/LangOptions.def"
321 
322  if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
323  if (Diags)
324  Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
325  return true;
326  }
327 
328  if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
329  if (Diags)
330  Diags->Report(diag::err_pch_langopt_value_mismatch)
331  << "target Objective-C runtime";
332  return true;
333  }
334 
335  if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
336  LangOpts.CommentOpts.BlockCommandNames) {
337  if (Diags)
338  Diags->Report(diag::err_pch_langopt_value_mismatch)
339  << "block command names";
340  return true;
341  }
342 
343  // Sanitizer feature mismatches are treated as compatible differences. If
344  // compatible differences aren't allowed, we still only want to check for
345  // mismatches of non-modular sanitizers (the only ones which can affect AST
346  // generation).
347  if (!AllowCompatibleDifferences) {
348  SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
349  SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
350  SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
351  ExistingSanitizers.clear(ModularSanitizers);
352  ImportedSanitizers.clear(ModularSanitizers);
353  if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
354  const std::string Flag = "-fsanitize=";
355  if (Diags) {
356 #define SANITIZER(NAME, ID) \
357  { \
358  bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
359  bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
360  if (InExistingModule != InImportedModule) \
361  Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
362  << InExistingModule << (Flag + NAME); \
363  }
364 #include "clang/Basic/Sanitizers.def"
365  }
366  return true;
367  }
368  }
369 
370  return false;
371 }
372 
373 /// Compare the given set of target options against an existing set of
374 /// target options.
375 ///
376 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
377 ///
378 /// \returns true if the target options mis-match, false otherwise.
379 static bool checkTargetOptions(const TargetOptions &TargetOpts,
380  const TargetOptions &ExistingTargetOpts,
381  DiagnosticsEngine *Diags,
382  bool AllowCompatibleDifferences = true) {
383 #define CHECK_TARGET_OPT(Field, Name) \
384  if (TargetOpts.Field != ExistingTargetOpts.Field) { \
385  if (Diags) \
386  Diags->Report(diag::err_pch_targetopt_mismatch) \
387  << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
388  return true; \
389  }
390 
391  // The triple and ABI must match exactly.
392  CHECK_TARGET_OPT(Triple, "target");
393  CHECK_TARGET_OPT(ABI, "target ABI");
394 
395  // We can tolerate different CPUs in many cases, notably when one CPU
396  // supports a strict superset of another. When allowing compatible
397  // differences skip this check.
398  if (!AllowCompatibleDifferences) {
399  CHECK_TARGET_OPT(CPU, "target CPU");
400  CHECK_TARGET_OPT(TuneCPU, "tune CPU");
401  }
402 
403 #undef CHECK_TARGET_OPT
404 
405  // Compare feature sets.
406  SmallVector<StringRef, 4> ExistingFeatures(
407  ExistingTargetOpts.FeaturesAsWritten.begin(),
408  ExistingTargetOpts.FeaturesAsWritten.end());
409  SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
410  TargetOpts.FeaturesAsWritten.end());
411  llvm::sort(ExistingFeatures);
412  llvm::sort(ReadFeatures);
413 
414  // We compute the set difference in both directions explicitly so that we can
415  // diagnose the differences differently.
416  SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
417  std::set_difference(
418  ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
419  ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
420  std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
421  ExistingFeatures.begin(), ExistingFeatures.end(),
422  std::back_inserter(UnmatchedReadFeatures));
423 
424  // If we are allowing compatible differences and the read feature set is
425  // a strict subset of the existing feature set, there is nothing to diagnose.
426  if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
427  return false;
428 
429  if (Diags) {
430  for (StringRef Feature : UnmatchedReadFeatures)
431  Diags->Report(diag::err_pch_targetopt_feature_mismatch)
432  << /* is-existing-feature */ false << Feature;
433  for (StringRef Feature : UnmatchedExistingFeatures)
434  Diags->Report(diag::err_pch_targetopt_feature_mismatch)
435  << /* is-existing-feature */ true << Feature;
436  }
437 
438  return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
439 }
440 
441 bool
443  bool Complain,
444  bool AllowCompatibleDifferences) {
445  const LangOptions &ExistingLangOpts = PP.getLangOpts();
446  return checkLanguageOptions(LangOpts, ExistingLangOpts,
447  Complain ? &Reader.Diags : nullptr,
448  AllowCompatibleDifferences);
449 }
450 
452  bool Complain,
453  bool AllowCompatibleDifferences) {
454  const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
455  return checkTargetOptions(TargetOpts, ExistingTargetOpts,
456  Complain ? &Reader.Diags : nullptr,
457  AllowCompatibleDifferences);
458 }
459 
460 namespace {
461 
462 using MacroDefinitionsMap =
463  llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
464 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
465 
466 } // namespace
467 
469  DiagnosticsEngine &Diags,
470  bool Complain) {
472 
473  // Check current mappings for new -Werror mappings, and the stored mappings
474  // for cases that were explicitly mapped to *not* be errors that are now
475  // errors because of options like -Werror.
476  DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
477 
478  for (DiagnosticsEngine *MappingSource : MappingSources) {
479  for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
480  diag::kind DiagID = DiagIDMappingPair.first;
481  Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
482  if (CurLevel < DiagnosticsEngine::Error)
483  continue; // not significant
484  Level StoredLevel =
485  StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
486  if (StoredLevel < DiagnosticsEngine::Error) {
487  if (Complain)
488  Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
489  Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
490  return true;
491  }
492  }
493  }
494 
495  return false;
496 }
497 
500  if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
501  return true;
502  return Ext >= diag::Severity::Error;
503 }
504 
506  DiagnosticsEngine &Diags,
507  bool IsSystem, bool Complain) {
508  // Top-level options
509  if (IsSystem) {
510  if (Diags.getSuppressSystemWarnings())
511  return false;
512  // If -Wsystem-headers was not enabled before, be conservative
513  if (StoredDiags.getSuppressSystemWarnings()) {
514  if (Complain)
515  Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
516  return true;
517  }
518  }
519 
520  if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
521  if (Complain)
522  Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
523  return true;
524  }
525 
526  if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
527  !StoredDiags.getEnableAllWarnings()) {
528  if (Complain)
529  Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
530  return true;
531  }
532 
533  if (isExtHandlingFromDiagsError(Diags) &&
534  !isExtHandlingFromDiagsError(StoredDiags)) {
535  if (Complain)
536  Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
537  return true;
538  }
539 
540  return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
541 }
542 
543 /// Return the top import module if it is implicit, nullptr otherwise.
545  Preprocessor &PP) {
546  // If the original import came from a file explicitly generated by the user,
547  // don't check the diagnostic mappings.
548  // FIXME: currently this is approximated by checking whether this is not a
549  // module import of an implicitly-loaded module file.
550  // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
551  // the transitive closure of its imports, since unrelated modules cannot be
552  // imported until after this module finishes validation.
553  ModuleFile *TopImport = &*ModuleMgr.rbegin();
554  while (!TopImport->ImportedBy.empty())
555  TopImport = TopImport->ImportedBy[0];
556  if (TopImport->Kind != MK_ImplicitModule)
557  return nullptr;
558 
559  StringRef ModuleName = TopImport->ModuleName;
560  assert(!ModuleName.empty() && "diagnostic options read before module name");
561 
562  Module *M =
563  PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
564  assert(M && "missing module");
565  return M;
566 }
567 
569  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
570  DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
571  IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
573  new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
574  // This should never fail, because we would have processed these options
575  // before writing them to an ASTFile.
576  ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
577 
578  ModuleManager &ModuleMgr = Reader.getModuleManager();
579  assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
580 
581  Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
582  if (!TopM)
583  return false;
584 
585  // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
586  // contains the union of their flags.
587  return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
588  Complain);
589 }
590 
591 /// Collect the macro definitions provided by the given preprocessor
592 /// options.
593 static void
595  MacroDefinitionsMap &Macros,
596  SmallVectorImpl<StringRef> *MacroNames = nullptr) {
597  for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
598  StringRef Macro = PPOpts.Macros[I].first;
599  bool IsUndef = PPOpts.Macros[I].second;
600 
601  std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
602  StringRef MacroName = MacroPair.first;
603  StringRef MacroBody = MacroPair.second;
604 
605  // For an #undef'd macro, we only care about the name.
606  if (IsUndef) {
607  if (MacroNames && !Macros.count(MacroName))
608  MacroNames->push_back(MacroName);
609 
610  Macros[MacroName] = std::make_pair("", true);
611  continue;
612  }
613 
614  // For a #define'd macro, figure out the actual definition.
615  if (MacroName.size() == Macro.size())
616  MacroBody = "1";
617  else {
618  // Note: GCC drops anything following an end-of-line character.
619  StringRef::size_type End = MacroBody.find_first_of("\n\r");
620  MacroBody = MacroBody.substr(0, End);
621  }
622 
623  if (MacroNames && !Macros.count(MacroName))
624  MacroNames->push_back(MacroName);
625  Macros[MacroName] = std::make_pair(MacroBody, false);
626  }
627 }
628 
633 };
634 
635 /// Check the preprocessor options deserialized from the control block
636 /// against the preprocessor options in an existing preprocessor.
637 ///
638 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
639 /// \param Validation If set to OptionValidateNone, ignore differences in
640 /// preprocessor options. If set to OptionValidateContradictions,
641 /// require that options passed both in the AST file and on the command
642 /// line (-D or -U) match, but tolerate options missing in one or the
643 /// other. If set to OptionValidateContradictions, require that there
644 /// are no differences in the options between the two.
646  const PreprocessorOptions &PPOpts,
647  const PreprocessorOptions &ExistingPPOpts, DiagnosticsEngine *Diags,
648  FileManager &FileMgr, std::string &SuggestedPredefines,
649  const LangOptions &LangOpts,
651  // Check macro definitions.
652  MacroDefinitionsMap ASTFileMacros;
653  collectMacroDefinitions(PPOpts, ASTFileMacros);
654  MacroDefinitionsMap ExistingMacros;
655  SmallVector<StringRef, 4> ExistingMacroNames;
656  collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
657 
658  for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
659  // Dig out the macro definition in the existing preprocessor options.
660  StringRef MacroName = ExistingMacroNames[I];
661  std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
662 
663  // Check whether we know anything about this macro name or not.
664  llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
665  ASTFileMacros.find(MacroName);
666  if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
667  if (Validation == OptionValidateStrictMatches) {
668  // If strict matches are requested, don't tolerate any extra defines on
669  // the command line that are missing in the AST file.
670  if (Diags) {
671  Diags->Report(diag::err_pch_macro_def_undef) << MacroName << true;
672  }
673  return true;
674  }
675  // FIXME: Check whether this identifier was referenced anywhere in the
676  // AST file. If so, we should reject the AST file. Unfortunately, this
677  // information isn't in the control block. What shall we do about it?
678 
679  if (Existing.second) {
680  SuggestedPredefines += "#undef ";
681  SuggestedPredefines += MacroName.str();
682  SuggestedPredefines += '\n';
683  } else {
684  SuggestedPredefines += "#define ";
685  SuggestedPredefines += MacroName.str();
686  SuggestedPredefines += ' ';
687  SuggestedPredefines += Existing.first.str();
688  SuggestedPredefines += '\n';
689  }
690  continue;
691  }
692 
693  // If the macro was defined in one but undef'd in the other, we have a
694  // conflict.
695  if (Existing.second != Known->second.second) {
696  if (Diags) {
697  Diags->Report(diag::err_pch_macro_def_undef)
698  << MacroName << Known->second.second;
699  }
700  return true;
701  }
702 
703  // If the macro was #undef'd in both, or if the macro bodies are identical,
704  // it's fine.
705  if (Existing.second || Existing.first == Known->second.first) {
706  ASTFileMacros.erase(Known);
707  continue;
708  }
709 
710  // The macro bodies differ; complain.
711  if (Diags) {
712  Diags->Report(diag::err_pch_macro_def_conflict)
713  << MacroName << Known->second.first << Existing.first;
714  }
715  return true;
716  }
717  if (Validation == OptionValidateStrictMatches) {
718  // If strict matches are requested, don't tolerate any extra defines in
719  // the AST file that are missing on the command line.
720  for (const auto &MacroName : ASTFileMacros.keys()) {
721  if (Diags) {
722  Diags->Report(diag::err_pch_macro_def_undef) << MacroName << false;
723  }
724  return true;
725  }
726  }
727 
728  // Check whether we're using predefines.
729  if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
730  Validation != OptionValidateNone) {
731  if (Diags) {
732  Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
733  }
734  return true;
735  }
736 
737  // Detailed record is important since it is used for the module cache hash.
738  if (LangOpts.Modules &&
739  PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
740  Validation != OptionValidateNone) {
741  if (Diags) {
742  Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
743  }
744  return true;
745  }
746 
747  // Compute the #include and #include_macros lines we need.
748  for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
749  StringRef File = ExistingPPOpts.Includes[I];
750 
751  if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
752  !ExistingPPOpts.PCHThroughHeader.empty()) {
753  // In case the through header is an include, we must add all the includes
754  // to the predefines so the start point can be determined.
755  SuggestedPredefines += "#include \"";
756  SuggestedPredefines += File;
757  SuggestedPredefines += "\"\n";
758  continue;
759  }
760 
761  if (File == ExistingPPOpts.ImplicitPCHInclude)
762  continue;
763 
764  if (llvm::is_contained(PPOpts.Includes, File))
765  continue;
766 
767  SuggestedPredefines += "#include \"";
768  SuggestedPredefines += File;
769  SuggestedPredefines += "\"\n";
770  }
771 
772  for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
773  StringRef File = ExistingPPOpts.MacroIncludes[I];
774  if (llvm::is_contained(PPOpts.MacroIncludes, File))
775  continue;
776 
777  SuggestedPredefines += "#__include_macros \"";
778  SuggestedPredefines += File;
779  SuggestedPredefines += "\"\n##\n";
780  }
781 
782  return false;
783 }
784 
786  bool Complain,
787  std::string &SuggestedPredefines) {
788  const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
789 
790  return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
791  Complain? &Reader.Diags : nullptr,
792  PP.getFileManager(),
793  SuggestedPredefines,
794  PP.getLangOpts());
795 }
796 
798  const PreprocessorOptions &PPOpts,
799  bool Complain,
800  std::string &SuggestedPredefines) {
801  return checkPreprocessorOptions(PPOpts, PP.getPreprocessorOpts(), nullptr,
802  PP.getFileManager(), SuggestedPredefines,
803  PP.getLangOpts(), OptionValidateNone);
804 }
805 
806 /// Check the header search options deserialized from the control block
807 /// against the header search options in an existing preprocessor.
808 ///
809 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
811  StringRef SpecificModuleCachePath,
812  StringRef ExistingModuleCachePath,
813  DiagnosticsEngine *Diags,
814  const LangOptions &LangOpts,
815  const PreprocessorOptions &PPOpts) {
816  if (LangOpts.Modules) {
817  if (SpecificModuleCachePath != ExistingModuleCachePath &&
819  if (Diags)
820  Diags->Report(diag::err_pch_modulecache_mismatch)
821  << SpecificModuleCachePath << ExistingModuleCachePath;
822  return true;
823  }
824  }
825 
826  return false;
827 }
828 
830  StringRef SpecificModuleCachePath,
831  bool Complain) {
832  return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
833  PP.getHeaderSearchInfo().getModuleCachePath(),
834  Complain ? &Reader.Diags : nullptr,
835  PP.getLangOpts(), PP.getPreprocessorOpts());
836 }
837 
838 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
839  PP.setCounterValue(Value);
840 }
841 
842 //===----------------------------------------------------------------------===//
843 // AST reader implementation
844 //===----------------------------------------------------------------------===//
845 
846 static uint64_t readULEB(const unsigned char *&P) {
847  unsigned Length = 0;
848  const char *Error = nullptr;
849 
850  uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
851  if (Error)
852  llvm::report_fatal_error(Error);
853  P += Length;
854  return Val;
855 }
856 
857 /// Read ULEB-encoded key length and data length.
858 static std::pair<unsigned, unsigned>
859 readULEBKeyDataLength(const unsigned char *&P) {
860  unsigned KeyLen = readULEB(P);
861  if ((unsigned)KeyLen != KeyLen)
862  llvm::report_fatal_error("key too large");
863 
864  unsigned DataLen = readULEB(P);
865  if ((unsigned)DataLen != DataLen)
866  llvm::report_fatal_error("data too large");
867 
868  return std::make_pair(KeyLen, DataLen);
869 }
870 
872  bool TakeOwnership) {
873  DeserializationListener = Listener;
874  OwnsDeserializationListener = TakeOwnership;
875 }
876 
878  return serialization::ComputeHash(Sel);
879 }
880 
881 std::pair<unsigned, unsigned>
883  return readULEBKeyDataLength(d);
884 }
885 
887 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
888  using namespace llvm::support;
889 
890  SelectorTable &SelTable = Reader.getContext().Selectors;
891  unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
892  IdentifierInfo *FirstII = Reader.getLocalIdentifier(
893  F, endian::readNext<uint32_t, little, unaligned>(d));
894  if (N == 0)
895  return SelTable.getNullarySelector(FirstII);
896  else if (N == 1)
897  return SelTable.getUnarySelector(FirstII);
898 
900  Args.push_back(FirstII);
901  for (unsigned I = 1; I != N; ++I)
902  Args.push_back(Reader.getLocalIdentifier(
903  F, endian::readNext<uint32_t, little, unaligned>(d)));
904 
905  return SelTable.getSelector(N, Args.data());
906 }
907 
910  unsigned DataLen) {
911  using namespace llvm::support;
912 
913  data_type Result;
914 
915  Result.ID = Reader.getGlobalSelectorID(
916  F, endian::readNext<uint32_t, little, unaligned>(d));
917  unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
918  unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
919  Result.InstanceBits = FullInstanceBits & 0x3;
920  Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
921  Result.FactoryBits = FullFactoryBits & 0x3;
922  Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
923  unsigned NumInstanceMethods = FullInstanceBits >> 3;
924  unsigned NumFactoryMethods = FullFactoryBits >> 3;
925 
926  // Load instance methods
927  for (unsigned I = 0; I != NumInstanceMethods; ++I) {
928  if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
929  F, endian::readNext<uint32_t, little, unaligned>(d)))
930  Result.Instance.push_back(Method);
931  }
932 
933  // Load factory methods
934  for (unsigned I = 0; I != NumFactoryMethods; ++I) {
935  if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
936  F, endian::readNext<uint32_t, little, unaligned>(d)))
937  Result.Factory.push_back(Method);
938  }
939 
940  return Result;
941 }
942 
944  return llvm::djbHash(a);
945 }
946 
947 std::pair<unsigned, unsigned>
949  return readULEBKeyDataLength(d);
950 }
951 
953 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
954  assert(n >= 2 && d[n-1] == '\0');
955  return StringRef((const char*) d, n-1);
956 }
957 
958 /// Whether the given identifier is "interesting".
960  bool IsModule) {
961  return II.hadMacroDefinition() || II.isPoisoned() ||
962  (!IsModule && II.getObjCOrBuiltinID()) ||
964  (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
965  II.getFETokenInfo());
966 }
967 
968 static bool readBit(unsigned &Bits) {
969  bool Value = Bits & 0x1;
970  Bits >>= 1;
971  return Value;
972 }
973 
975  using namespace llvm::support;
976 
977  unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
978  return Reader.getGlobalIdentifierID(F, RawID >> 1);
979 }
980 
981 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
982  if (!II.isFromAST()) {
983  II.setIsFromAST();
984  bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
985  if (isInterestingIdentifier(Reader, II, IsModule))
987  }
988 }
989 
991  const unsigned char* d,
992  unsigned DataLen) {
993  using namespace llvm::support;
994 
995  unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
996  bool IsInteresting = RawID & 0x01;
997 
998  // Wipe out the "is interesting" bit.
999  RawID = RawID >> 1;
1000 
1001  // Build the IdentifierInfo and link the identifier ID with it.
1002  IdentifierInfo *II = KnownII;
1003  if (!II) {
1004  II = &Reader.getIdentifierTable().getOwn(k);
1005  KnownII = II;
1006  }
1007  markIdentifierFromAST(Reader, *II);
1008  Reader.markIdentifierUpToDate(II);
1009 
1010  IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
1011  if (!IsInteresting) {
1012  // For uninteresting identifiers, there's nothing else to do. Just notify
1013  // the reader that we've finished loading this identifier.
1014  Reader.SetIdentifierInfo(ID, II);
1015  return II;
1016  }
1017 
1018  unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
1019  unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
1020  bool CPlusPlusOperatorKeyword = readBit(Bits);
1021  bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1022  bool Poisoned = readBit(Bits);
1023  bool ExtensionToken = readBit(Bits);
1024  bool HadMacroDefinition = readBit(Bits);
1025 
1026  assert(Bits == 0 && "Extra bits in the identifier?");
1027  DataLen -= 8;
1028 
1029  // Set or check the various bits in the IdentifierInfo structure.
1030  // Token IDs are read-only.
1031  if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1033  if (!F.isModule())
1034  II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1035  assert(II->isExtensionToken() == ExtensionToken &&
1036  "Incorrect extension token flag");
1037  (void)ExtensionToken;
1038  if (Poisoned)
1039  II->setIsPoisoned(true);
1040  assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1041  "Incorrect C++ operator keyword flag");
1042  (void)CPlusPlusOperatorKeyword;
1043 
1044  // If this identifier is a macro, deserialize the macro
1045  // definition.
1046  if (HadMacroDefinition) {
1047  uint32_t MacroDirectivesOffset =
1048  endian::readNext<uint32_t, little, unaligned>(d);
1049  DataLen -= 4;
1050 
1051  Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1052  }
1053 
1054  Reader.SetIdentifierInfo(ID, II);
1055 
1056  // Read all of the declarations visible at global scope with this
1057  // name.
1058  if (DataLen > 0) {
1059  SmallVector<uint32_t, 4> DeclIDs;
1060  for (; DataLen > 0; DataLen -= 4)
1061  DeclIDs.push_back(Reader.getGlobalDeclID(
1062  F, endian::readNext<uint32_t, little, unaligned>(d)));
1063  Reader.SetGloballyVisibleDecls(II, DeclIDs);
1064  }
1065 
1066  return II;
1067 }
1068 
1070  : Kind(Name.getNameKind()) {
1071  switch (Kind) {
1073  Data = (uint64_t)Name.getAsIdentifierInfo();
1074  break;
1078  Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1079  break;
1081  Data = Name.getCXXOverloadedOperator();
1082  break;
1084  Data = (uint64_t)Name.getCXXLiteralIdentifier();
1085  break;
1087  Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1088  ->getDeclName().getAsIdentifierInfo();
1089  break;
1094  Data = 0;
1095  break;
1096  }
1097 }
1098 
1100  llvm::FoldingSetNodeID ID;
1101  ID.AddInteger(Kind);
1102 
1103  switch (Kind) {
1107  ID.AddString(((IdentifierInfo*)Data)->getName());
1108  break;
1112  ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1113  break;
1115  ID.AddInteger((OverloadedOperatorKind)Data);
1116  break;
1121  break;
1122  }
1123 
1124  return ID.ComputeHash();
1125 }
1126 
1127 ModuleFile *
1128 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1129  using namespace llvm::support;
1130 
1131  uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1132  return Reader.getLocalModuleFile(F, ModuleFileID);
1133 }
1134 
1135 std::pair<unsigned, unsigned>
1136 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1137  return readULEBKeyDataLength(d);
1138 }
1139 
1141 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1142  using namespace llvm::support;
1143 
1144  auto Kind = (DeclarationName::NameKind)*d++;
1145  uint64_t Data;
1146  switch (Kind) {
1150  Data = (uint64_t)Reader.getLocalIdentifier(
1151  F, endian::readNext<uint32_t, little, unaligned>(d));
1152  break;
1156  Data =
1157  (uint64_t)Reader.getLocalSelector(
1158  F, endian::readNext<uint32_t, little, unaligned>(
1159  d)).getAsOpaquePtr();
1160  break;
1162  Data = *d++; // OverloadedOperatorKind
1163  break;
1168  Data = 0;
1169  break;
1170  }
1171 
1172  return DeclarationNameKey(Kind, Data);
1173 }
1174 
1175 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1176  const unsigned char *d,
1177  unsigned DataLen,
1178  data_type_builder &Val) {
1179  using namespace llvm::support;
1180 
1181  for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1182  uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1183  Val.insert(Reader.getGlobalDeclID(F, LocalID));
1184  }
1185 }
1186 
1187 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1188  BitstreamCursor &Cursor,
1189  uint64_t Offset,
1190  DeclContext *DC) {
1191  assert(Offset != 0);
1192 
1193  SavedStreamPosition SavedPosition(Cursor);
1194  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1195  Error(std::move(Err));
1196  return true;
1197  }
1198 
1199  RecordData Record;
1200  StringRef Blob;
1201  Expected<unsigned> MaybeCode = Cursor.ReadCode();
1202  if (!MaybeCode) {
1203  Error(MaybeCode.takeError());
1204  return true;
1205  }
1206  unsigned Code = MaybeCode.get();
1207 
1208  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1209  if (!MaybeRecCode) {
1210  Error(MaybeRecCode.takeError());
1211  return true;
1212  }
1213  unsigned RecCode = MaybeRecCode.get();
1214  if (RecCode != DECL_CONTEXT_LEXICAL) {
1215  Error("Expected lexical block");
1216  return true;
1217  }
1218 
1219  assert(!isa<TranslationUnitDecl>(DC) &&
1220  "expected a TU_UPDATE_LEXICAL record for TU");
1221  // If we are handling a C++ class template instantiation, we can see multiple
1222  // lexical updates for the same record. It's important that we select only one
1223  // of them, so that field numbering works properly. Just pick the first one we
1224  // see.
1225  auto &Lex = LexicalDecls[DC];
1226  if (!Lex.first) {
1227  Lex = std::make_pair(
1228  &M, llvm::makeArrayRef(
1229  reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1230  Blob.data()),
1231  Blob.size() / 4));
1232  }
1233  DC->setHasExternalLexicalStorage(true);
1234  return false;
1235 }
1236 
1237 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1238  BitstreamCursor &Cursor,
1239  uint64_t Offset,
1240  DeclID ID) {
1241  assert(Offset != 0);
1242 
1243  SavedStreamPosition SavedPosition(Cursor);
1244  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1245  Error(std::move(Err));
1246  return true;
1247  }
1248 
1249  RecordData Record;
1250  StringRef Blob;
1251  Expected<unsigned> MaybeCode = Cursor.ReadCode();
1252  if (!MaybeCode) {
1253  Error(MaybeCode.takeError());
1254  return true;
1255  }
1256  unsigned Code = MaybeCode.get();
1257 
1258  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1259  if (!MaybeRecCode) {
1260  Error(MaybeRecCode.takeError());
1261  return true;
1262  }
1263  unsigned RecCode = MaybeRecCode.get();
1264  if (RecCode != DECL_CONTEXT_VISIBLE) {
1265  Error("Expected visible lookup table block");
1266  return true;
1267  }
1268 
1269  // We can't safely determine the primary context yet, so delay attaching the
1270  // lookup table until we're done with recursive deserialization.
1271  auto *Data = (const unsigned char*)Blob.data();
1272  PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1273  return false;
1274 }
1275 
1276 void ASTReader::Error(StringRef Msg) const {
1277  Error(diag::err_fe_pch_malformed, Msg);
1278  if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1279  !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1280  Diag(diag::note_module_cache_path)
1281  << PP.getHeaderSearchInfo().getModuleCachePath();
1282  }
1283 }
1284 
1285 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1286  StringRef Arg3) const {
1287  if (Diags.isDiagnosticInFlight())
1288  Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1289  else
1290  Diag(DiagID) << Arg1 << Arg2 << Arg3;
1291 }
1292 
1293 void ASTReader::Error(llvm::Error &&Err) const {
1294  llvm::Error RemainingErr =
1295  handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1296  auto Diag = E.getDiagnostic().second;
1297 
1298  // Ideally we'd just emit it, but have to handle a possible in-flight
1299  // diagnostic. Note that the location is currently ignored as well.
1300  auto NumArgs = Diag.getStorage()->NumDiagArgs;
1301  assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1302  StringRef Arg1, Arg2, Arg3;
1303  switch (NumArgs) {
1304  case 3:
1305  Arg3 = Diag.getStringArg(2);
1306  [[fallthrough]];
1307  case 2:
1308  Arg2 = Diag.getStringArg(1);
1309  [[fallthrough]];
1310  case 1:
1311  Arg1 = Diag.getStringArg(0);
1312  }
1313  Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1314  });
1315  if (RemainingErr)
1316  Error(toString(std::move(RemainingErr)));
1317 }
1318 
1319 //===----------------------------------------------------------------------===//
1320 // Source Manager Deserialization
1321 //===----------------------------------------------------------------------===//
1322 
1323 /// Read the line table in the source manager block.
1324 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1325  unsigned Idx = 0;
1326  LineTableInfo &LineTable = SourceMgr.getLineTable();
1327 
1328  // Parse the file names
1329  std::map<int, int> FileIDs;
1330  FileIDs[-1] = -1; // For unspecified filenames.
1331  for (unsigned I = 0; Record[Idx]; ++I) {
1332  // Extract the file name
1333  auto Filename = ReadPath(F, Record, Idx);
1334  FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1335  }
1336  ++Idx;
1337 
1338  // Parse the line entries
1339  std::vector<LineEntry> Entries;
1340  while (Idx < Record.size()) {
1341  FileID FID = ReadFileID(F, Record, Idx);
1342 
1343  // Extract the line entries
1344  unsigned NumEntries = Record[Idx++];
1345  assert(NumEntries && "no line entries for file ID");
1346  Entries.clear();
1347  Entries.reserve(NumEntries);
1348  for (unsigned I = 0; I != NumEntries; ++I) {
1349  unsigned FileOffset = Record[Idx++];
1350  unsigned LineNo = Record[Idx++];
1351  int FilenameID = FileIDs[Record[Idx++]];
1353  = (SrcMgr::CharacteristicKind)Record[Idx++];
1354  unsigned IncludeOffset = Record[Idx++];
1355  Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1356  FileKind, IncludeOffset));
1357  }
1358  LineTable.AddEntry(FID, Entries);
1359  }
1360 }
1361 
1362 /// Read a source manager block
1363 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1364  using namespace SrcMgr;
1365 
1366  BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1367 
1368  // Set the source-location entry cursor to the current position in
1369  // the stream. This cursor will be used to read the contents of the
1370  // source manager block initially, and then lazily read
1371  // source-location entries as needed.
1372  SLocEntryCursor = F.Stream;
1373 
1374  // The stream itself is going to skip over the source manager block.
1375  if (llvm::Error Err = F.Stream.SkipBlock())
1376  return Err;
1377 
1378  // Enter the source manager block.
1379  if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1380  return Err;
1381  F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1382 
1383  RecordData Record;
1384  while (true) {
1386  SLocEntryCursor.advanceSkippingSubblocks();
1387  if (!MaybeE)
1388  return MaybeE.takeError();
1389  llvm::BitstreamEntry E = MaybeE.get();
1390 
1391  switch (E.Kind) {
1392  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1394  return llvm::createStringError(std::errc::illegal_byte_sequence,
1395  "malformed block record in AST file");
1396  case llvm::BitstreamEntry::EndBlock:
1397  return llvm::Error::success();
1398  case llvm::BitstreamEntry::Record:
1399  // The interesting case.
1400  break;
1401  }
1402 
1403  // Read a record.
1404  Record.clear();
1405  StringRef Blob;
1406  Expected<unsigned> MaybeRecord =
1407  SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1408  if (!MaybeRecord)
1409  return MaybeRecord.takeError();
1410  switch (MaybeRecord.get()) {
1411  default: // Default behavior: ignore.
1412  break;
1413 
1414  case SM_SLOC_FILE_ENTRY:
1415  case SM_SLOC_BUFFER_ENTRY:
1417  // Once we hit one of the source location entries, we're done.
1418  return llvm::Error::success();
1419  }
1420  }
1421 }
1422 
1424  if (ID == 0)
1425  return false;
1426 
1427  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1428  Error("source location entry ID out-of-range for AST file");
1429  return true;
1430  }
1431 
1432  // Local helper to read the (possibly-compressed) buffer data following the
1433  // entry record.
1434  auto ReadBuffer = [this](
1435  BitstreamCursor &SLocEntryCursor,
1436  StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1437  RecordData Record;
1438  StringRef Blob;
1439  Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1440  if (!MaybeCode) {
1441  Error(MaybeCode.takeError());
1442  return nullptr;
1443  }
1444  unsigned Code = MaybeCode.get();
1445 
1446  Expected<unsigned> MaybeRecCode =
1447  SLocEntryCursor.readRecord(Code, Record, &Blob);
1448  if (!MaybeRecCode) {
1449  Error(MaybeRecCode.takeError());
1450  return nullptr;
1451  }
1452  unsigned RecCode = MaybeRecCode.get();
1453 
1454  if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1455  // Inspect the first byte to differentiate zlib (\x78) and zstd
1456  // (little-endian 0xFD2FB528).
1457  const llvm::compression::Format F =
1458  Blob.size() > 0 && Blob.data()[0] == 0x78
1459  ? llvm::compression::Format::Zlib
1460  : llvm::compression::Format::Zstd;
1461  if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1462  Error(Reason);
1463  return nullptr;
1464  }
1465  SmallVector<uint8_t, 0> Decompressed;
1466  if (llvm::Error E = llvm::compression::decompress(
1467  F, llvm::arrayRefFromStringRef(Blob), Decompressed, Record[0])) {
1468  Error("could not decompress embedded file contents: " +
1469  llvm::toString(std::move(E)));
1470  return nullptr;
1471  }
1472  return llvm::MemoryBuffer::getMemBufferCopy(
1473  llvm::toStringRef(Decompressed), Name);
1474  } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1475  return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1476  } else {
1477  Error("AST record has invalid code");
1478  return nullptr;
1479  }
1480  };
1481 
1482  ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1483  if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1485  F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1486  Error(std::move(Err));
1487  return true;
1488  }
1489 
1490  BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1492 
1493  ++NumSLocEntriesRead;
1494  Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1495  if (!MaybeEntry) {
1496  Error(MaybeEntry.takeError());
1497  return true;
1498  }
1499  llvm::BitstreamEntry Entry = MaybeEntry.get();
1500 
1501  if (Entry.Kind != llvm::BitstreamEntry::Record) {
1502  Error("incorrectly-formatted source location entry in AST file");
1503  return true;
1504  }
1505 
1506  RecordData Record;
1507  StringRef Blob;
1508  Expected<unsigned> MaybeSLOC =
1509  SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1510  if (!MaybeSLOC) {
1511  Error(MaybeSLOC.takeError());
1512  return true;
1513  }
1514  switch (MaybeSLOC.get()) {
1515  default:
1516  Error("incorrectly-formatted source location entry in AST file");
1517  return true;
1518 
1519  case SM_SLOC_FILE_ENTRY: {
1520  // We will detect whether a file changed and return 'Failure' for it, but
1521  // we will also try to fail gracefully by setting up the SLocEntry.
1522  unsigned InputID = Record[4];
1523  InputFile IF = getInputFile(*F, InputID);
1525  bool OverriddenBuffer = IF.isOverridden();
1526 
1527  // Note that we only check if a File was returned. If it was out-of-date
1528  // we have complained but we will continue creating a FileID to recover
1529  // gracefully.
1530  if (!File)
1531  return true;
1532 
1533  SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1534  if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1535  // This is the module's main file.
1536  IncludeLoc = getImportLocation(F);
1537  }
1539  FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1540  FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1541  BaseOffset + Record[0]);
1542  SrcMgr::FileInfo &FileInfo =
1543  const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1544  FileInfo.NumCreatedFIDs = Record[5];
1545  if (Record[3])
1546  FileInfo.setHasLineDirectives();
1547 
1548  unsigned NumFileDecls = Record[7];
1549  if (NumFileDecls && ContextObj) {
1550  const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1551  assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1552  FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1553  NumFileDecls));
1554  }
1555 
1556  const SrcMgr::ContentCache &ContentCache =
1557  SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1558  if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1559  ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1560  !ContentCache.getBufferIfLoaded()) {
1561  auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1562  if (!Buffer)
1563  return true;
1564  SourceMgr.overrideFileContents(*File, std::move(Buffer));
1565  }
1566 
1567  break;
1568  }
1569 
1570  case SM_SLOC_BUFFER_ENTRY: {
1571  const char *Name = Blob.data();
1572  unsigned Offset = Record[0];
1574  FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1575  SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1576  if (IncludeLoc.isInvalid() && F->isModule()) {
1577  IncludeLoc = getImportLocation(F);
1578  }
1579 
1580  auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1581  if (!Buffer)
1582  return true;
1583  SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1584  BaseOffset + Offset, IncludeLoc);
1585  break;
1586  }
1587 
1588  case SM_SLOC_EXPANSION_ENTRY: {
1589  LocSeq::State Seq;
1590  SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1], Seq);
1591  SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2], Seq);
1592  SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3], Seq);
1593  SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd,
1594  Record[5], Record[4], ID,
1595  BaseOffset + Record[0]);
1596  break;
1597  }
1598  }
1599 
1600  return false;
1601 }
1602 
1603 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1604  if (ID == 0)
1605  return std::make_pair(SourceLocation(), "");
1606 
1607  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1608  Error("source location entry ID out-of-range for AST file");
1609  return std::make_pair(SourceLocation(), "");
1610  }
1611 
1612  // Find which module file this entry lands in.
1613  ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1614  if (!M->isModule())
1615  return std::make_pair(SourceLocation(), "");
1616 
1617  // FIXME: Can we map this down to a particular submodule? That would be
1618  // ideal.
1619  return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1620 }
1621 
1622 /// Find the location where the module F is imported.
1623 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1624  if (F->ImportLoc.isValid())
1625  return F->ImportLoc;
1626 
1627  // Otherwise we have a PCH. It's considered to be "imported" at the first
1628  // location of its includer.
1629  if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1630  // Main file is the importer.
1631  assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1632  return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1633  }
1634  return F->ImportedBy[0]->FirstLoc;
1635 }
1636 
1637 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1638 /// the abbreviations that are at the top of the block and then leave the cursor
1639 /// pointing into the block.
1640 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1641  unsigned BlockID,
1642  uint64_t *StartOfBlockOffset) {
1643  if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1644  return Err;
1645 
1646  if (StartOfBlockOffset)
1647  *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1648 
1649  while (true) {
1650  uint64_t Offset = Cursor.GetCurrentBitNo();
1651  Expected<unsigned> MaybeCode = Cursor.ReadCode();
1652  if (!MaybeCode)
1653  return MaybeCode.takeError();
1654  unsigned Code = MaybeCode.get();
1655 
1656  // We expect all abbrevs to be at the start of the block.
1657  if (Code != llvm::bitc::DEFINE_ABBREV) {
1658  if (llvm::Error Err = Cursor.JumpToBit(Offset))
1659  return Err;
1660  return llvm::Error::success();
1661  }
1662  if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1663  return Err;
1664  }
1665 }
1666 
1668  unsigned &Idx) {
1669  Token Tok;
1670  Tok.startToken();
1671  Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1672  Tok.setKind((tok::TokenKind)Record[Idx++]);
1673  Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1674 
1675  if (Tok.isAnnotation()) {
1676  Tok.setAnnotationEndLoc(ReadSourceLocation(F, Record, Idx));
1677  switch (Tok.getKind()) {
1678  case tok::annot_pragma_loop_hint: {
1679  auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1680  Info->PragmaName = ReadToken(F, Record, Idx);
1681  Info->Option = ReadToken(F, Record, Idx);
1682  unsigned NumTokens = Record[Idx++];
1683  SmallVector<Token, 4> Toks;
1684  Toks.reserve(NumTokens);
1685  for (unsigned I = 0; I < NumTokens; ++I)
1686  Toks.push_back(ReadToken(F, Record, Idx));
1687  Info->Toks = llvm::makeArrayRef(Toks).copy(PP.getPreprocessorAllocator());
1688  Tok.setAnnotationValue(static_cast<void *>(Info));
1689  break;
1690  }
1691  // Some annotation tokens do not use the PtrData field.
1692  case tok::annot_pragma_openmp:
1693  case tok::annot_pragma_openmp_end:
1694  case tok::annot_pragma_unused:
1695  break;
1696  default:
1697  llvm_unreachable("missing deserialization code for annotation token");
1698  }
1699  } else {
1700  Tok.setLength(Record[Idx++]);
1701  if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1702  Tok.setIdentifierInfo(II);
1703  }
1704  return Tok;
1705 }
1706 
1708  BitstreamCursor &Stream = F.MacroCursor;
1709 
1710  // Keep track of where we are in the stream, then jump back there
1711  // after reading this macro.
1712  SavedStreamPosition SavedPosition(Stream);
1713 
1714  if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1715  // FIXME this drops errors on the floor.
1716  consumeError(std::move(Err));
1717  return nullptr;
1718  }
1719  RecordData Record;
1721  MacroInfo *Macro = nullptr;
1722  llvm::MutableArrayRef<Token> MacroTokens;
1723 
1724  while (true) {
1725  // Advance to the next record, but if we get to the end of the block, don't
1726  // pop it (removing all the abbreviations from the cursor) since we want to
1727  // be able to reseek within the block and read entries.
1728  unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1729  Expected<llvm::BitstreamEntry> MaybeEntry =
1730  Stream.advanceSkippingSubblocks(Flags);
1731  if (!MaybeEntry) {
1732  Error(MaybeEntry.takeError());
1733  return Macro;
1734  }
1735  llvm::BitstreamEntry Entry = MaybeEntry.get();
1736 
1737  switch (Entry.Kind) {
1738  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1740  Error("malformed block record in AST file");
1741  return Macro;
1742  case llvm::BitstreamEntry::EndBlock:
1743  return Macro;
1744  case llvm::BitstreamEntry::Record:
1745  // The interesting case.
1746  break;
1747  }
1748 
1749  // Read a record.
1750  Record.clear();
1751  PreprocessorRecordTypes RecType;
1752  if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1753  RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1754  else {
1755  Error(MaybeRecType.takeError());
1756  return Macro;
1757  }
1758  switch (RecType) {
1759  case PP_MODULE_MACRO:
1761  return Macro;
1762 
1763  case PP_MACRO_OBJECT_LIKE:
1764  case PP_MACRO_FUNCTION_LIKE: {
1765  // If we already have a macro, that means that we've hit the end
1766  // of the definition of the macro we were looking for. We're
1767  // done.
1768  if (Macro)
1769  return Macro;
1770 
1771  unsigned NextIndex = 1; // Skip identifier ID.
1772  SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1773  MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1774  MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1775  MI->setIsUsed(Record[NextIndex++]);
1776  MI->setUsedForHeaderGuard(Record[NextIndex++]);
1777  MacroTokens = MI->allocateTokens(Record[NextIndex++],
1778  PP.getPreprocessorAllocator());
1779  if (RecType == PP_MACRO_FUNCTION_LIKE) {
1780  // Decode function-like macro info.
1781  bool isC99VarArgs = Record[NextIndex++];
1782  bool isGNUVarArgs = Record[NextIndex++];
1783  bool hasCommaPasting = Record[NextIndex++];
1784  MacroParams.clear();
1785  unsigned NumArgs = Record[NextIndex++];
1786  for (unsigned i = 0; i != NumArgs; ++i)
1787  MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1788 
1789  // Install function-like macro info.
1790  MI->setIsFunctionLike();
1791  if (isC99VarArgs) MI->setIsC99Varargs();
1792  if (isGNUVarArgs) MI->setIsGNUVarargs();
1793  if (hasCommaPasting) MI->setHasCommaPasting();
1794  MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1795  }
1796 
1797  // Remember that we saw this macro last so that we add the tokens that
1798  // form its body to it.
1799  Macro = MI;
1800 
1801  if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1802  Record[NextIndex]) {
1803  // We have a macro definition. Register the association
1805  GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1806  PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1807  PreprocessingRecord::PPEntityID PPID =
1808  PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1809  MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1810  PPRec.getPreprocessedEntity(PPID));
1811  if (PPDef)
1812  PPRec.RegisterMacroDefinition(Macro, PPDef);
1813  }
1814 
1815  ++NumMacrosRead;
1816  break;
1817  }
1818 
1819  case PP_TOKEN: {
1820  // If we see a TOKEN before a PP_MACRO_*, then the file is
1821  // erroneous, just pretend we didn't see this.
1822  if (!Macro) break;
1823  if (MacroTokens.empty()) {
1824  Error("unexpected number of macro tokens for a macro in AST file");
1825  return Macro;
1826  }
1827 
1828  unsigned Idx = 0;
1829  MacroTokens[0] = ReadToken(F, Record, Idx);
1830  MacroTokens = MacroTokens.drop_front();
1831  break;
1832  }
1833  }
1834  }
1835 }
1836 
1839  unsigned LocalID) const {
1840  if (!M.ModuleOffsetMap.empty())
1841  ReadModuleOffsetMap(M);
1842 
1845  assert(I != M.PreprocessedEntityRemap.end()
1846  && "Invalid index into preprocessed entity index remap");
1847 
1848  return LocalID + I->second;
1849 }
1850 
1852  return llvm::hash_combine(ikey.Size, ikey.ModTime);
1853 }
1854 
1856 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1857  internal_key_type ikey = {FE->getSize(),
1858  M.HasTimestamps ? FE->getModificationTime() : 0,
1859  FE->getName(), /*Imported*/ false};
1860  return ikey;
1861 }
1862 
1863 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1864  if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1865  return false;
1866 
1867  if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1868  return true;
1869 
1870  // Determine whether the actual files are equivalent.
1871  FileManager &FileMgr = Reader.getFileManager();
1872  auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1873  if (!Key.Imported) {
1874  if (auto File = FileMgr.getFile(Key.Filename))
1875  return *File;
1876  return nullptr;
1877  }
1878 
1879  std::string Resolved = std::string(Key.Filename);
1880  Reader.ResolveImportedPath(M, Resolved);
1881  if (auto File = FileMgr.getFile(Resolved))
1882  return *File;
1883  return nullptr;
1884  };
1885 
1886  const FileEntry *FEA = GetFile(a);
1887  const FileEntry *FEB = GetFile(b);
1888  return FEA && FEA == FEB;
1889 }
1890 
1891 std::pair<unsigned, unsigned>
1892 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1893  return readULEBKeyDataLength(d);
1894 }
1895 
1897 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1898  using namespace llvm::support;
1899 
1900  internal_key_type ikey;
1901  ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1902  ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1903  ikey.Filename = (const char *)d;
1904  ikey.Imported = true;
1905  return ikey;
1906 }
1907 
1909 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1910  unsigned DataLen) {
1911  using namespace llvm::support;
1912 
1913  const unsigned char *End = d + DataLen;
1914  HeaderFileInfo HFI;
1915  unsigned Flags = *d++;
1916  // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1917  HFI.isImport |= (Flags >> 5) & 0x01;
1918  HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1919  HFI.DirInfo = (Flags >> 1) & 0x07;
1920  HFI.IndexHeaderMapHeader = Flags & 0x01;
1921  HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1922  M, endian::readNext<uint32_t, little, unaligned>(d));
1923  if (unsigned FrameworkOffset =
1924  endian::readNext<uint32_t, little, unaligned>(d)) {
1925  // The framework offset is 1 greater than the actual offset,
1926  // since 0 is used as an indicator for "no framework name".
1927  StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1928  HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1929  }
1930 
1931  assert((End - d) % 4 == 0 &&
1932  "Wrong data length in HeaderFileInfo deserialization");
1933  while (d != End) {
1934  uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1935  auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
1936  LocalSMID >>= 3;
1937 
1938  // This header is part of a module. Associate it with the module to enable
1939  // implicit module import.
1940  SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1941  Module *Mod = Reader.getSubmodule(GlobalSMID);
1942  FileManager &FileMgr = Reader.getFileManager();
1943  ModuleMap &ModMap =
1944  Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1945 
1947  if (key.Imported)
1948  Reader.ResolveImportedPath(M, Filename);
1949  // FIXME: NameAsWritten
1950  Module::Header H = {std::string(key.Filename), "",
1951  *FileMgr.getFile(Filename)};
1952  ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1953  HFI.isModuleHeader |= ModuleMap::isModular(HeaderRole);
1954  }
1955 
1956  // This HeaderFileInfo was externally loaded.
1957  HFI.External = true;
1958  HFI.IsValid = true;
1959  return HFI;
1960 }
1961 
1963  uint32_t MacroDirectivesOffset) {
1964  assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1965  PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1966 }
1967 
1969  // Note that we are loading defined macros.
1970  Deserializing Macros(this);
1971 
1972  for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1973  BitstreamCursor &MacroCursor = I.MacroCursor;
1974 
1975  // If there was no preprocessor block, skip this file.
1976  if (MacroCursor.getBitcodeBytes().empty())
1977  continue;
1978 
1979  BitstreamCursor Cursor = MacroCursor;
1980  if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1981  Error(std::move(Err));
1982  return;
1983  }
1984 
1985  RecordData Record;
1986  while (true) {
1987  Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1988  if (!MaybeE) {
1989  Error(MaybeE.takeError());
1990  return;
1991  }
1992  llvm::BitstreamEntry E = MaybeE.get();
1993 
1994  switch (E.Kind) {
1995  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1997  Error("malformed block record in AST file");
1998  return;
1999  case llvm::BitstreamEntry::EndBlock:
2000  goto NextCursor;
2001 
2002  case llvm::BitstreamEntry::Record: {
2003  Record.clear();
2004  Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
2005  if (!MaybeRecord) {
2006  Error(MaybeRecord.takeError());
2007  return;
2008  }
2009  switch (MaybeRecord.get()) {
2010  default: // Default behavior: ignore.
2011  break;
2012 
2013  case PP_MACRO_OBJECT_LIKE:
2014  case PP_MACRO_FUNCTION_LIKE: {
2015  IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
2016  if (II->isOutOfDate())
2017  updateOutOfDateIdentifier(*II);
2018  break;
2019  }
2020 
2021  case PP_TOKEN:
2022  // Ignore tokens.
2023  break;
2024  }
2025  break;
2026  }
2027  }
2028  }
2029  NextCursor: ;
2030  }
2031 }
2032 
2033 namespace {
2034 
2035  /// Visitor class used to look up identifirs in an AST file.
2036  class IdentifierLookupVisitor {
2037  StringRef Name;
2038  unsigned NameHash;
2039  unsigned PriorGeneration;
2040  unsigned &NumIdentifierLookups;
2041  unsigned &NumIdentifierLookupHits;
2042  IdentifierInfo *Found = nullptr;
2043 
2044  public:
2045  IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2046  unsigned &NumIdentifierLookups,
2047  unsigned &NumIdentifierLookupHits)
2048  : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2049  PriorGeneration(PriorGeneration),
2050  NumIdentifierLookups(NumIdentifierLookups),
2051  NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2052 
2053  bool operator()(ModuleFile &M) {
2054  // If we've already searched this module file, skip it now.
2055  if (M.Generation <= PriorGeneration)
2056  return true;
2057 
2058  ASTIdentifierLookupTable *IdTable
2060  if (!IdTable)
2061  return false;
2062 
2063  ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2064  Found);
2065  ++NumIdentifierLookups;
2066  ASTIdentifierLookupTable::iterator Pos =
2067  IdTable->find_hashed(Name, NameHash, &Trait);
2068  if (Pos == IdTable->end())
2069  return false;
2070 
2071  // Dereferencing the iterator has the effect of building the
2072  // IdentifierInfo node and populating it with the various
2073  // declarations it needs.
2074  ++NumIdentifierLookupHits;
2075  Found = *Pos;
2076  return true;
2077  }
2078 
2079  // Retrieve the identifier info found within the module
2080  // files.
2081  IdentifierInfo *getIdentifierInfo() const { return Found; }
2082  };
2083 
2084 } // namespace
2085 
2087  // Note that we are loading an identifier.
2088  Deserializing AnIdentifier(this);
2089 
2090  unsigned PriorGeneration = 0;
2091  if (getContext().getLangOpts().Modules)
2092  PriorGeneration = IdentifierGeneration[&II];
2093 
2094  // If there is a global index, look there first to determine which modules
2095  // provably do not have any results for this identifier.
2097  GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2098  if (!loadGlobalIndex()) {
2099  if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2100  HitsPtr = &Hits;
2101  }
2102  }
2103 
2104  IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2105  NumIdentifierLookups,
2106  NumIdentifierLookupHits);
2107  ModuleMgr.visit(Visitor, HitsPtr);
2108  markIdentifierUpToDate(&II);
2109 }
2110 
2112  if (!II)
2113  return;
2114 
2115  II->setOutOfDate(false);
2116 
2117  // Update the generation for this identifier.
2118  if (getContext().getLangOpts().Modules)
2119  IdentifierGeneration[II] = getGeneration();
2120 }
2121 
2123  const PendingMacroInfo &PMInfo) {
2124  ModuleFile &M = *PMInfo.M;
2125 
2126  BitstreamCursor &Cursor = M.MacroCursor;
2127  SavedStreamPosition SavedPosition(Cursor);
2128  if (llvm::Error Err =
2129  Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2130  Error(std::move(Err));
2131  return;
2132  }
2133 
2134  struct ModuleMacroRecord {
2135  SubmoduleID SubModID;
2136  MacroInfo *MI;
2137  SmallVector<SubmoduleID, 8> Overrides;
2138  };
2140 
2141  // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2142  // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2143  // macro histroy.
2144  RecordData Record;
2145  while (true) {
2146  Expected<llvm::BitstreamEntry> MaybeEntry =
2147  Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2148  if (!MaybeEntry) {
2149  Error(MaybeEntry.takeError());
2150  return;
2151  }
2152  llvm::BitstreamEntry Entry = MaybeEntry.get();
2153 
2154  if (Entry.Kind != llvm::BitstreamEntry::Record) {
2155  Error("malformed block record in AST file");
2156  return;
2157  }
2158 
2159  Record.clear();
2160  Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2161  if (!MaybePP) {
2162  Error(MaybePP.takeError());
2163  return;
2164  }
2165  switch ((PreprocessorRecordTypes)MaybePP.get()) {
2167  break;
2168 
2169  case PP_MODULE_MACRO: {
2170  ModuleMacros.push_back(ModuleMacroRecord());
2171  auto &Info = ModuleMacros.back();
2172  Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2173  Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2174  for (int I = 2, N = Record.size(); I != N; ++I)
2175  Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2176  continue;
2177  }
2178 
2179  default:
2180  Error("malformed block record in AST file");
2181  return;
2182  }
2183 
2184  // We found the macro directive history; that's the last record
2185  // for this macro.
2186  break;
2187  }
2188 
2189  // Module macros are listed in reverse dependency order.
2190  {
2191  std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2193  for (auto &MMR : ModuleMacros) {
2194  Overrides.clear();
2195  for (unsigned ModID : MMR.Overrides) {
2196  Module *Mod = getSubmodule(ModID);
2197  auto *Macro = PP.getModuleMacro(Mod, II);
2198  assert(Macro && "missing definition for overridden macro");
2199  Overrides.push_back(Macro);
2200  }
2201 
2202  bool Inserted = false;
2203  Module *Owner = getSubmodule(MMR.SubModID);
2204  PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2205  }
2206  }
2207 
2208  // Don't read the directive history for a module; we don't have anywhere
2209  // to put it.
2210  if (M.isModule())
2211  return;
2212 
2213  // Deserialize the macro directives history in reverse source-order.
2214  MacroDirective *Latest = nullptr, *Earliest = nullptr;
2215  unsigned Idx = 0, N = Record.size();
2216  while (Idx < N) {
2217  MacroDirective *MD = nullptr;
2218  SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2219  MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2220  switch (K) {
2222  MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2223  MD = PP.AllocateDefMacroDirective(MI, Loc);
2224  break;
2225  }
2227  MD = PP.AllocateUndefMacroDirective(Loc);
2228  break;
2230  bool isPublic = Record[Idx++];
2231  MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2232  break;
2233  }
2234 
2235  if (!Latest)
2236  Latest = MD;
2237  if (Earliest)
2238  Earliest->setPrevious(MD);
2239  Earliest = MD;
2240  }
2241 
2242  if (Latest)
2243  PP.setLoadedMacroDirective(II, Earliest, Latest);
2244 }
2245 
2246 bool ASTReader::shouldDisableValidationForFile(
2247  const serialization::ModuleFile &M) const {
2248  if (DisableValidationKind == DisableValidationForModuleKind::None)
2249  return false;
2250 
2251  // If a PCH is loaded and validation is disabled for PCH then disable
2252  // validation for the PCH and the modules it loads.
2253  ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind);
2254 
2255  switch (K) {
2256  case MK_MainFile:
2257  case MK_Preamble:
2258  case MK_PCH:
2259  return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2260  case MK_ImplicitModule:
2261  case MK_ExplicitModule:
2262  case MK_PrebuiltModule:
2263  return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2264  }
2265 
2266  return false;
2267 }
2268 
2269 ASTReader::InputFileInfo
2270 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2271  // Go find this input file.
2272  BitstreamCursor &Cursor = F.InputFilesCursor;
2273  SavedStreamPosition SavedPosition(Cursor);
2274  if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2275  // FIXME this drops errors on the floor.
2276  consumeError(std::move(Err));
2277  }
2278 
2279  Expected<unsigned> MaybeCode = Cursor.ReadCode();
2280  if (!MaybeCode) {
2281  // FIXME this drops errors on the floor.
2282  consumeError(MaybeCode.takeError());
2283  }
2284  unsigned Code = MaybeCode.get();
2285  RecordData Record;
2286  StringRef Blob;
2287 
2288  if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2289  assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2290  "invalid record type for input file");
2291  else {
2292  // FIXME this drops errors on the floor.
2293  consumeError(Maybe.takeError());
2294  }
2295 
2296  assert(Record[0] == ID && "Bogus stored ID or offset");
2297  InputFileInfo R;
2298  R.StoredSize = static_cast<off_t>(Record[1]);
2299  R.StoredTime = static_cast<time_t>(Record[2]);
2300  R.Overridden = static_cast<bool>(Record[3]);
2301  R.Transient = static_cast<bool>(Record[4]);
2302  R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2303  R.Filename = std::string(Blob);
2304  ResolveImportedPath(F, R.Filename);
2305 
2306  Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2307  if (!MaybeEntry) // FIXME this drops errors on the floor.
2308  consumeError(MaybeEntry.takeError());
2309  llvm::BitstreamEntry Entry = MaybeEntry.get();
2310  assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2311  "expected record type for input file hash");
2312 
2313  Record.clear();
2314  if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2315  assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2316  "invalid record type for input file hash");
2317  else {
2318  // FIXME this drops errors on the floor.
2319  consumeError(Maybe.takeError());
2320  }
2321  R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2322  static_cast<uint64_t>(Record[0]);
2323  return R;
2324 }
2325 
2326 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2327 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2328  // If this ID is bogus, just return an empty input file.
2329  if (ID == 0 || ID > F.InputFilesLoaded.size())
2330  return InputFile();
2331 
2332  // If we've already loaded this input file, return it.
2333  if (F.InputFilesLoaded[ID-1].getFile())
2334  return F.InputFilesLoaded[ID-1];
2335 
2336  if (F.InputFilesLoaded[ID-1].isNotFound())
2337  return InputFile();
2338 
2339  // Go find this input file.
2340  BitstreamCursor &Cursor = F.InputFilesCursor;
2341  SavedStreamPosition SavedPosition(Cursor);
2342  if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2343  // FIXME this drops errors on the floor.
2344  consumeError(std::move(Err));
2345  }
2346 
2347  InputFileInfo FI = readInputFileInfo(F, ID);
2348  off_t StoredSize = FI.StoredSize;
2349  time_t StoredTime = FI.StoredTime;
2350  bool Overridden = FI.Overridden;
2351  bool Transient = FI.Transient;
2352  StringRef Filename = FI.Filename;
2353  uint64_t StoredContentHash = FI.ContentHash;
2354 
2356  expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2357 
2358  // For an overridden file, create a virtual file with the stored
2359  // size/timestamp.
2360  if ((Overridden || Transient) && !File)
2361  File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2362 
2363  if (!File) {
2364  if (Complain) {
2365  std::string ErrorStr = "could not find file '";
2366  ErrorStr += Filename;
2367  ErrorStr += "' referenced by AST file '";
2368  ErrorStr += F.FileName;
2369  ErrorStr += "'";
2370  Error(ErrorStr);
2371  }
2372  // Record that we didn't find the file.
2374  return InputFile();
2375  }
2376 
2377  // Check if there was a request to override the contents of the file
2378  // that was part of the precompiled header. Overriding such a file
2379  // can lead to problems when lexing using the source locations from the
2380  // PCH.
2381  SourceManager &SM = getSourceManager();
2382  // FIXME: Reject if the overrides are different.
2383  if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2384  if (Complain)
2385  Error(diag::err_fe_pch_file_overridden, Filename);
2386 
2387  // After emitting the diagnostic, bypass the overriding file to recover
2388  // (this creates a separate FileEntry).
2389  File = SM.bypassFileContentsOverride(*File);
2390  if (!File) {
2392  return InputFile();
2393  }
2394  }
2395 
2396  struct Change {
2397  enum ModificationKind {
2398  Size,
2399  ModTime,
2400  Content,
2401  None,
2402  } Kind;
2403  llvm::Optional<int64_t> Old = llvm::None;
2404  llvm::Optional<int64_t> New = llvm::None;
2405  };
2406  auto HasInputFileChanged = [&]() {
2407  if (StoredSize != File->getSize())
2408  return Change{Change::Size, StoredSize, File->getSize()};
2409  if (!shouldDisableValidationForFile(F) && StoredTime &&
2410  StoredTime != File->getModificationTime()) {
2411  Change MTimeChange = {Change::ModTime, StoredTime,
2412  File->getModificationTime()};
2413 
2414  // In case the modification time changes but not the content,
2415  // accept the cached file as legit.
2416  if (ValidateASTInputFilesContent &&
2417  StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2418  auto MemBuffOrError = FileMgr.getBufferForFile(File);
2419  if (!MemBuffOrError) {
2420  if (!Complain)
2421  return MTimeChange;
2422  std::string ErrorStr = "could not get buffer for file '";
2423  ErrorStr += File->getName();
2424  ErrorStr += "'";
2425  Error(ErrorStr);
2426  return MTimeChange;
2427  }
2428 
2429  // FIXME: hash_value is not guaranteed to be stable!
2430  auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2431  if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2432  return Change{Change::None};
2433 
2434  return Change{Change::Content};
2435  }
2436  return MTimeChange;
2437  }
2438  return Change{Change::None};
2439  };
2440 
2441  bool IsOutOfDate = false;
2442  auto FileChange = HasInputFileChanged();
2443  // For an overridden file, there is nothing to validate.
2444  if (!Overridden && FileChange.Kind != Change::None) {
2445  if (Complain && !Diags.isDiagnosticInFlight()) {
2446  // Build a list of the PCH imports that got us here (in reverse).
2447  SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2448  while (!ImportStack.back()->ImportedBy.empty())
2449  ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2450 
2451  // The top-level PCH is stale.
2452  StringRef TopLevelPCHName(ImportStack.back()->FileName);
2453  Diag(diag::err_fe_ast_file_modified)
2454  << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2455  << TopLevelPCHName << FileChange.Kind
2456  << (FileChange.Old && FileChange.New)
2457  << llvm::itostr(FileChange.Old.value_or(0))
2458  << llvm::itostr(FileChange.New.value_or(0));
2459 
2460  // Print the import stack.
2461  if (ImportStack.size() > 1) {
2462  Diag(diag::note_pch_required_by)
2463  << Filename << ImportStack[0]->FileName;
2464  for (unsigned I = 1; I < ImportStack.size(); ++I)
2465  Diag(diag::note_pch_required_by)
2466  << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2467  }
2468 
2469  Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2470  }
2471 
2472  IsOutOfDate = true;
2473  }
2474  // FIXME: If the file is overridden and we've already opened it,
2475  // issue an error (or split it into a separate FileEntry).
2476 
2477  InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2478 
2479  // Note that we've loaded this input file.
2480  F.InputFilesLoaded[ID-1] = IF;
2481  return IF;
2482 }
2483 
2484 /// If we are loading a relocatable PCH or module file, and the filename
2485 /// is not an absolute path, add the system or module root to the beginning of
2486 /// the file name.
2488  // Resolve relative to the base directory, if we have one.
2489  if (!M.BaseDirectory.empty())
2490  return ResolveImportedPath(Filename, M.BaseDirectory);
2491 }
2492 
2494  if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2495  return;
2496 
2497  SmallString<128> Buffer;
2498  llvm::sys::path::append(Buffer, Prefix, Filename);
2499  Filename.assign(Buffer.begin(), Buffer.end());
2500 }
2501 
2502 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2503  switch (ARR) {
2504  case ASTReader::Failure: return true;
2505  case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2506  case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2509  return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2510  case ASTReader::HadErrors: return true;
2511  case ASTReader::Success: return false;
2512  }
2513 
2514  llvm_unreachable("unknown ASTReadResult");
2515 }
2516 
2517 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2518  BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2519  bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2520  std::string &SuggestedPredefines) {
2521  if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2522  // FIXME this drops errors on the floor.
2523  consumeError(std::move(Err));
2524  return Failure;
2525  }
2526 
2527  // Read all of the records in the options block.
2528  RecordData Record;
2529  ASTReadResult Result = Success;
2530  while (true) {
2531  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2532  if (!MaybeEntry) {
2533  // FIXME this drops errors on the floor.
2534  consumeError(MaybeEntry.takeError());
2535  return Failure;
2536  }
2537  llvm::BitstreamEntry Entry = MaybeEntry.get();
2538 
2539  switch (Entry.Kind) {
2541  case llvm::BitstreamEntry::SubBlock:
2542  return Failure;
2543 
2544  case llvm::BitstreamEntry::EndBlock:
2545  return Result;
2546 
2547  case llvm::BitstreamEntry::Record:
2548  // The interesting case.
2549  break;
2550  }
2551 
2552  // Read and process a record.
2553  Record.clear();
2554  Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2555  if (!MaybeRecordType) {
2556  // FIXME this drops errors on the floor.
2557  consumeError(MaybeRecordType.takeError());
2558  return Failure;
2559  }
2560  switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2561  case LANGUAGE_OPTIONS: {
2562  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2563  if (ParseLanguageOptions(Record, Complain, Listener,
2564  AllowCompatibleConfigurationMismatch))
2565  Result = ConfigurationMismatch;
2566  break;
2567  }
2568 
2569  case TARGET_OPTIONS: {
2570  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2571  if (ParseTargetOptions(Record, Complain, Listener,
2572  AllowCompatibleConfigurationMismatch))
2573  Result = ConfigurationMismatch;
2574  break;
2575  }
2576 
2577  case FILE_SYSTEM_OPTIONS: {
2578  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2579  if (!AllowCompatibleConfigurationMismatch &&
2580  ParseFileSystemOptions(Record, Complain, Listener))
2581  Result = ConfigurationMismatch;
2582  break;
2583  }
2584 
2585  case HEADER_SEARCH_OPTIONS: {
2586  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2587  if (!AllowCompatibleConfigurationMismatch &&
2588  ParseHeaderSearchOptions(Record, Complain, Listener))
2589  Result = ConfigurationMismatch;
2590  break;
2591  }
2592 
2593  case PREPROCESSOR_OPTIONS:
2594  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2595  if (!AllowCompatibleConfigurationMismatch &&
2596  ParsePreprocessorOptions(Record, Complain, Listener,
2597  SuggestedPredefines))
2598  Result = ConfigurationMismatch;
2599  break;
2600  }
2601  }
2602 }
2603 
2605 ASTReader::ReadControlBlock(ModuleFile &F,
2607  const ModuleFile *ImportedBy,
2608  unsigned ClientLoadCapabilities) {
2609  BitstreamCursor &Stream = F.Stream;
2610 
2611  if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2612  Error(std::move(Err));
2613  return Failure;
2614  }
2615 
2616  // Lambda to read the unhashed control block the first time it's called.
2617  //
2618  // For PCM files, the unhashed control block cannot be read until after the
2619  // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2620  // need to look ahead before reading the IMPORTS record. For consistency,
2621  // this block is always read somehow (see BitstreamEntry::EndBlock).
2622  bool HasReadUnhashedControlBlock = false;
2623  auto readUnhashedControlBlockOnce = [&]() {
2624  if (!HasReadUnhashedControlBlock) {
2625  HasReadUnhashedControlBlock = true;
2626  if (ASTReadResult Result =
2627  readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2628  return Result;
2629  }
2630  return Success;
2631  };
2632 
2633  bool DisableValidation = shouldDisableValidationForFile(F);
2634 
2635  // Read all of the records and blocks in the control block.
2636  RecordData Record;
2637  unsigned NumInputs = 0;
2638  unsigned NumUserInputs = 0;
2639  StringRef BaseDirectoryAsWritten;
2640  while (true) {
2641  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2642  if (!MaybeEntry) {
2643  Error(MaybeEntry.takeError());
2644  return Failure;
2645  }
2646  llvm::BitstreamEntry Entry = MaybeEntry.get();
2647 
2648  switch (Entry.Kind) {
2650  Error("malformed block record in AST file");
2651  return Failure;
2652  case llvm::BitstreamEntry::EndBlock: {
2653  // Validate the module before returning. This call catches an AST with
2654  // no module name and no imports.
2655  if (ASTReadResult Result = readUnhashedControlBlockOnce())
2656  return Result;
2657 
2658  // Validate input files.
2659  const HeaderSearchOptions &HSOpts =
2660  PP.getHeaderSearchInfo().getHeaderSearchOpts();
2661 
2662  // All user input files reside at the index range [0, NumUserInputs), and
2663  // system input files reside at [NumUserInputs, NumInputs). For explicitly
2664  // loaded module files, ignore missing inputs.
2665  if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2666  F.Kind != MK_PrebuiltModule) {
2667  bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2668 
2669  // If we are reading a module, we will create a verification timestamp,
2670  // so we verify all input files. Otherwise, verify only user input
2671  // files.
2672 
2673  unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
2676  F.Kind == MK_ImplicitModule)
2677  N = NumUserInputs;
2678 
2679  for (unsigned I = 0; I < N; ++I) {
2680  InputFile IF = getInputFile(F, I+1, Complain);
2681  if (!IF.getFile() || IF.isOutOfDate())
2682  return OutOfDate;
2683  }
2684  }
2685 
2686  if (Listener)
2687  Listener->visitModuleFile(F.FileName, F.Kind);
2688 
2689  if (Listener && Listener->needsInputFileVisitation()) {
2690  unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2691  : NumUserInputs;
2692  for (unsigned I = 0; I < N; ++I) {
2693  bool IsSystem = I >= NumUserInputs;
2694  InputFileInfo FI = readInputFileInfo(F, I+1);
2695  Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2696  F.Kind == MK_ExplicitModule ||
2697  F.Kind == MK_PrebuiltModule);
2698  }
2699  }
2700 
2701  return Success;
2702  }
2703 
2704  case llvm::BitstreamEntry::SubBlock:
2705  switch (Entry.ID) {
2706  case INPUT_FILES_BLOCK_ID:
2707  F.InputFilesCursor = Stream;
2708  if (llvm::Error Err = Stream.SkipBlock()) {
2709  Error(std::move(Err));
2710  return Failure;
2711  }
2712  if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2713  Error("malformed block record in AST file");
2714  return Failure;
2715  }
2716  continue;
2717 
2718  case OPTIONS_BLOCK_ID:
2719  // If we're reading the first module for this group, check its options
2720  // are compatible with ours. For modules it imports, no further checking
2721  // is required, because we checked them when we built it.
2722  if (Listener && !ImportedBy) {
2723  // Should we allow the configuration of the module file to differ from
2724  // the configuration of the current translation unit in a compatible
2725  // way?
2726  //
2727  // FIXME: Allow this for files explicitly specified with -include-pch.
2728  bool AllowCompatibleConfigurationMismatch =
2730 
2731  ASTReadResult Result =
2732  ReadOptionsBlock(Stream, ClientLoadCapabilities,
2733  AllowCompatibleConfigurationMismatch, *Listener,
2734  SuggestedPredefines);
2735  if (Result == Failure) {
2736  Error("malformed block record in AST file");
2737  return Result;
2738  }
2739 
2740  if (DisableValidation ||
2741  (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2742  Result = Success;
2743 
2744  // If we can't load the module, exit early since we likely
2745  // will rebuild the module anyway. The stream may be in the
2746  // middle of a block.
2747  if (Result != Success)
2748  return Result;
2749  } else if (llvm::Error Err = Stream.SkipBlock()) {
2750  Error(std::move(Err));
2751  return Failure;
2752  }
2753  continue;
2754 
2755  default:
2756  if (llvm::Error Err = Stream.SkipBlock()) {
2757  Error(std::move(Err));
2758  return Failure;
2759  }
2760  continue;
2761  }
2762 
2763  case llvm::BitstreamEntry::Record:
2764  // The interesting case.
2765  break;
2766  }
2767 
2768  // Read and process a record.
2769  Record.clear();
2770  StringRef Blob;
2771  Expected<unsigned> MaybeRecordType =
2772  Stream.readRecord(Entry.ID, Record, &Blob);
2773  if (!MaybeRecordType) {
2774  Error(MaybeRecordType.takeError());
2775  return Failure;
2776  }
2777  switch ((ControlRecordTypes)MaybeRecordType.get()) {
2778  case METADATA: {
2779  if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2780  if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2781  Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2782  : diag::err_pch_version_too_new);
2783  return VersionMismatch;
2784  }
2785 
2786  bool hasErrors = Record[6];
2787  if (hasErrors && !DisableValidation) {
2788  // If requested by the caller and the module hasn't already been read
2789  // or compiled, mark modules on error as out-of-date.
2790  if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2791  canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2792  return OutOfDate;
2793 
2794  if (!AllowASTWithCompilerErrors) {
2795  Diag(diag::err_pch_with_compiler_errors);
2796  return HadErrors;
2797  }
2798  }
2799  if (hasErrors) {
2800  Diags.ErrorOccurred = true;
2801  Diags.UncompilableErrorOccurred = true;
2802  Diags.UnrecoverableErrorOccurred = true;
2803  }
2804 
2805  F.RelocatablePCH = Record[4];
2806  // Relative paths in a relocatable PCH are relative to our sysroot.
2807  if (F.RelocatablePCH)
2808  F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2809 
2810  F.HasTimestamps = Record[5];
2811 
2812  const std::string &CurBranch = getClangFullRepositoryVersion();
2813  StringRef ASTBranch = Blob;
2814  if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2815  if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2816  Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2817  return VersionMismatch;
2818  }
2819  break;
2820  }
2821 
2822  case IMPORTS: {
2823  // Validate the AST before processing any imports (otherwise, untangling
2824  // them can be error-prone and expensive). A module will have a name and
2825  // will already have been validated, but this catches the PCH case.
2826  if (ASTReadResult Result = readUnhashedControlBlockOnce())
2827  return Result;
2828 
2829  // Load each of the imported PCH files.
2830  unsigned Idx = 0, N = Record.size();
2831  while (Idx < N) {
2832  // Read information about the AST file.
2833  ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2834  // The import location will be the local one for now; we will adjust
2835  // all import locations of module imports after the global source
2836  // location info are setup, in ReadAST.
2837  SourceLocation ImportLoc =
2838  ReadUntranslatedSourceLocation(Record[Idx++]);
2839  off_t StoredSize = (off_t)Record[Idx++];
2840  time_t StoredModTime = (time_t)Record[Idx++];
2841  auto FirstSignatureByte = Record.begin() + Idx;
2842  ASTFileSignature StoredSignature = ASTFileSignature::create(
2843  FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2844  Idx += ASTFileSignature::size;
2845 
2846  std::string ImportedName = ReadString(Record, Idx);
2847  std::string ImportedFile;
2848 
2849  // For prebuilt and explicit modules first consult the file map for
2850  // an override. Note that here we don't search prebuilt module
2851  // directories, only the explicit name to file mappings. Also, we will
2852  // still verify the size/signature making sure it is essentially the
2853  // same file but perhaps in a different location.
2854  if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2855  ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2856  ImportedName, /*FileMapOnly*/ true);
2857 
2858  if (ImportedFile.empty())
2859  // Use BaseDirectoryAsWritten to ensure we use the same path in the
2860  // ModuleCache as when writing.
2861  ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2862  else
2863  SkipPath(Record, Idx);
2864 
2865  // If our client can't cope with us being out of date, we can't cope with
2866  // our dependency being missing.
2867  unsigned Capabilities = ClientLoadCapabilities;
2868  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2869  Capabilities &= ~ARR_Missing;
2870 
2871  // Load the AST file.
2872  auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2873  Loaded, StoredSize, StoredModTime,
2874  StoredSignature, Capabilities);
2875 
2876  // If we diagnosed a problem, produce a backtrace.
2877  bool recompilingFinalized =
2878  Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2879  getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2880  if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2881  Diag(diag::note_module_file_imported_by)
2882  << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2883  if (recompilingFinalized)
2884  Diag(diag::note_module_file_conflict);
2885 
2886  switch (Result) {
2887  case Failure: return Failure;
2888  // If we have to ignore the dependency, we'll have to ignore this too.
2889  case Missing:
2890  case OutOfDate: return OutOfDate;
2891  case VersionMismatch: return VersionMismatch;
2892  case ConfigurationMismatch: return ConfigurationMismatch;
2893  case HadErrors: return HadErrors;
2894  case Success: break;
2895  }
2896  }
2897  break;
2898  }
2899 
2900  case ORIGINAL_FILE:
2901  F.OriginalSourceFileID = FileID::get(Record[0]);
2904  ResolveImportedPath(F, F.OriginalSourceFileName);
2905  break;
2906 
2907  case ORIGINAL_FILE_ID:
2908  F.OriginalSourceFileID = FileID::get(Record[0]);
2909  break;
2910 
2911  case MODULE_NAME:
2912  F.ModuleName = std::string(Blob);
2913  Diag(diag::remark_module_import)
2914  << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2915  << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2916  if (Listener)
2917  Listener->ReadModuleName(F.ModuleName);
2918 
2919  // Validate the AST as soon as we have a name so we can exit early on
2920  // failure.
2921  if (ASTReadResult Result = readUnhashedControlBlockOnce())
2922  return Result;
2923 
2924  break;
2925 
2926  case MODULE_DIRECTORY: {
2927  // Save the BaseDirectory as written in the PCM for computing the module
2928  // filename for the ModuleCache.
2929  BaseDirectoryAsWritten = Blob;
2930  assert(!F.ModuleName.empty() &&
2931  "MODULE_DIRECTORY found before MODULE_NAME");
2932  // If we've already loaded a module map file covering this module, we may
2933  // have a better path for it (relative to the current build).
2934  Module *M = PP.getHeaderSearchInfo().lookupModule(
2935  F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
2936  /*AllowExtraModuleMapSearch*/ true);
2937  if (M && M->Directory) {
2938  // If we're implicitly loading a module, the base directory can't
2939  // change between the build and use.
2940  // Don't emit module relocation error if we have -fno-validate-pch
2941  if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2944  auto BuildDir = PP.getFileManager().getDirectory(Blob);
2945  if (!BuildDir || *BuildDir != M->Directory) {
2946  if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2947  Diag(diag::err_imported_module_relocated)
2948  << F.ModuleName << Blob << M->Directory->getName();
2949  return OutOfDate;
2950  }
2951  }
2953  } else {
2954  F.BaseDirectory = std::string(Blob);
2955  }
2956  break;
2957  }
2958 
2959  case MODULE_MAP_FILE:
2960  if (ASTReadResult Result =
2961  ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2962  return Result;
2963  break;
2964 
2965  case INPUT_FILE_OFFSETS:
2966  NumInputs = Record[0];
2967  NumUserInputs = Record[1];
2968  F.InputFileOffsets =
2969  (const llvm::support::unaligned_uint64_t *)Blob.data();
2970  F.InputFilesLoaded.resize(NumInputs);
2971  F.NumUserInputFiles = NumUserInputs;
2972  break;
2973  }
2974  }
2975 }
2976 
2977 void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob,
2978  Preprocessor &PP) {
2979  using namespace llvm::support;
2980 
2981  const unsigned char *D = (const unsigned char *)Blob.data();
2982  unsigned FileCount = endian::readNext<uint32_t, little, unaligned>(D);
2983 
2984  for (unsigned I = 0; I < FileCount; ++I) {
2985  size_t ID = endian::readNext<uint32_t, little, unaligned>(D);
2986  InputFileInfo IFI = readInputFileInfo(F, ID);
2987  if (llvm::ErrorOr<const FileEntry *> File =
2988  PP.getFileManager().getFile(IFI.Filename))
2989  PP.getIncludedFiles().insert(*File);
2990  }
2991 }
2992 
2993 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
2994  unsigned ClientLoadCapabilities) {
2995  BitstreamCursor &Stream = F.Stream;
2996 
2997  if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
2998  return Err;
2999  F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3000 
3001  // Read all of the records and blocks for the AST file.
3002  RecordData Record;
3003  while (true) {
3004  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3005  if (!MaybeEntry)
3006  return MaybeEntry.takeError();
3007  llvm::BitstreamEntry Entry = MaybeEntry.get();
3008 
3009  switch (Entry.Kind) {
3011  return llvm::createStringError(
3012  std::errc::illegal_byte_sequence,
3013  "error at end of module block in AST file");
3014  case llvm::BitstreamEntry::EndBlock:
3015  // Outside of C++, we do not store a lookup map for the translation unit.
3016  // Instead, mark it as needing a lookup map to be built if this module
3017  // contains any declarations lexically within it (which it always does!).
3018  // This usually has no cost, since we very rarely need the lookup map for
3019  // the translation unit outside C++.
3020  if (ASTContext *Ctx = ContextObj) {
3021  DeclContext *DC = Ctx->getTranslationUnitDecl();
3022  if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3024  }
3025 
3026  return llvm::Error::success();
3027  case llvm::BitstreamEntry::SubBlock:
3028  switch (Entry.ID) {
3029  case DECLTYPES_BLOCK_ID:
3030  // We lazily load the decls block, but we want to set up the
3031  // DeclsCursor cursor to point into it. Clone our current bitcode
3032  // cursor to it, enter the block and read the abbrevs in that block.
3033  // With the main cursor, we just skip over it.
3034  F.DeclsCursor = Stream;
3035  if (llvm::Error Err = Stream.SkipBlock())
3036  return Err;
3037  if (llvm::Error Err = ReadBlockAbbrevs(
3039  return Err;
3040  break;
3041 
3042  case PREPROCESSOR_BLOCK_ID:
3043  F.MacroCursor = Stream;
3044  if (!PP.getExternalSource())
3045  PP.setExternalSource(this);
3046 
3047  if (llvm::Error Err = Stream.SkipBlock())
3048  return Err;
3049  if (llvm::Error Err =
3050  ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3051  return Err;
3052  F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3053  break;
3054 
3056  F.PreprocessorDetailCursor = Stream;
3057 
3058  if (llvm::Error Err = Stream.SkipBlock()) {
3059  return Err;
3060  }
3061  if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3063  return Err;
3065  = F.PreprocessorDetailCursor.GetCurrentBitNo();
3066 
3067  if (!PP.getPreprocessingRecord())
3071  break;
3072 
3074  if (llvm::Error Err = ReadSourceManagerBlock(F))
3075  return Err;
3076  break;
3077 
3078  case SUBMODULE_BLOCK_ID:
3079  if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3080  return Err;
3081  break;
3082 
3083  case COMMENTS_BLOCK_ID: {
3084  BitstreamCursor C = Stream;
3085 
3086  if (llvm::Error Err = Stream.SkipBlock())
3087  return Err;
3088  if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3089  return Err;
3090  CommentsCursors.push_back(std::make_pair(C, &F));
3091  break;
3092  }
3093 
3094  default:
3095  if (llvm::Error Err = Stream.SkipBlock())
3096  return Err;
3097  break;
3098  }
3099  continue;
3100 
3101  case llvm::BitstreamEntry::Record:
3102  // The interesting case.
3103  break;
3104  }
3105 
3106  // Read and process a record.
3107  Record.clear();
3108  StringRef Blob;
3109  Expected<unsigned> MaybeRecordType =
3110  Stream.readRecord(Entry.ID, Record, &Blob);
3111  if (!MaybeRecordType)
3112  return MaybeRecordType.takeError();
3113  ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3114 
3115  // If we're not loading an AST context, we don't care about most records.
3116  if (!ContextObj) {
3117  switch (RecordType) {
3118  case IDENTIFIER_TABLE:
3119  case IDENTIFIER_OFFSET:
3121  case STATISTICS:
3122  case PP_ASSUME_NONNULL_LOC:
3123  case PP_CONDITIONAL_STACK:
3124  case PP_COUNTER_VALUE:
3126  case MODULE_OFFSET_MAP:
3129  case PPD_ENTITIES_OFFSETS:
3130  case HEADER_SEARCH_TABLE:
3131  case IMPORTED_MODULES:
3132  case MACRO_OFFSET:
3133  break;
3134  default:
3135  continue;
3136  }
3137  }
3138 
3139  switch (RecordType) {
3140  default: // Default behavior: ignore.
3141  break;
3142 
3143  case TYPE_OFFSET: {
3144  if (F.LocalNumTypes != 0)
3145  return llvm::createStringError(
3146  std::errc::illegal_byte_sequence,
3147  "duplicate TYPE_OFFSET record in AST file");
3148  F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3149  F.LocalNumTypes = Record[0];
3150  unsigned LocalBaseTypeIndex = Record[1];
3151  F.BaseTypeIndex = getTotalNumTypes();
3152 
3153  if (F.LocalNumTypes > 0) {
3154  // Introduce the global -> local mapping for types within this module.
3155  GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3156 
3157  // Introduce the local -> global mapping for types within this module.
3159  std::make_pair(LocalBaseTypeIndex,
3160  F.BaseTypeIndex - LocalBaseTypeIndex));
3161 
3162  TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3163  }
3164  break;
3165  }
3166 
3167  case DECL_OFFSET: {
3168  if (F.LocalNumDecls != 0)
3169  return llvm::createStringError(
3170  std::errc::illegal_byte_sequence,
3171  "duplicate DECL_OFFSET record in AST file");
3172  F.DeclOffsets = (const DeclOffset *)Blob.data();
3173  F.LocalNumDecls = Record[0];
3174  unsigned LocalBaseDeclID = Record[1];
3175  F.BaseDeclID = getTotalNumDecls();
3176 
3177  if (F.LocalNumDecls > 0) {
3178  // Introduce the global -> local mapping for declarations within this
3179  // module.
3180  GlobalDeclMap.insert(
3181  std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3182 
3183  // Introduce the local -> global mapping for declarations within this
3184  // module.
3186  std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3187 
3188  // Introduce the global -> local mapping for declarations within this
3189  // module.
3190  F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3191 
3192  DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3193  }
3194  break;
3195  }
3196 
3197  case TU_UPDATE_LEXICAL: {
3198  DeclContext *TU = ContextObj->getTranslationUnitDecl();
3199  LexicalContents Contents(
3200  reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3201  Blob.data()),
3202  static_cast<unsigned int>(Blob.size() / 4));
3203  TULexicalDecls.push_back(std::make_pair(&F, Contents));
3204  TU->setHasExternalLexicalStorage(true);
3205  break;
3206  }
3207 
3208  case UPDATE_VISIBLE: {
3209  unsigned Idx = 0;
3210  serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3211  auto *Data = (const unsigned char*)Blob.data();
3212  PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3213  // If we've already loaded the decl, perform the updates when we finish
3214  // loading this block.
3215  if (Decl *D = GetExistingDecl(ID))
3216  PendingUpdateRecords.push_back(
3217  PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3218  break;
3219  }
3220 
3221  case IDENTIFIER_TABLE:
3223  reinterpret_cast<const unsigned char *>(Blob.data());
3224  if (Record[0]) {
3225  F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3226  F.IdentifierTableData + Record[0],
3227  F.IdentifierTableData + sizeof(uint32_t),
3229  ASTIdentifierLookupTrait(*this, F));
3230 
3232  }
3233  break;
3234 
3235  case IDENTIFIER_OFFSET: {
3236  if (F.LocalNumIdentifiers != 0)
3237  return llvm::createStringError(
3238  std::errc::illegal_byte_sequence,
3239  "duplicate IDENTIFIER_OFFSET record in AST file");
3240  F.IdentifierOffsets = (const uint32_t *)Blob.data();
3241  F.LocalNumIdentifiers = Record[0];
3242  unsigned LocalBaseIdentifierID = Record[1];
3243  F.BaseIdentifierID = getTotalNumIdentifiers();
3244 
3245  if (F.LocalNumIdentifiers > 0) {
3246  // Introduce the global -> local mapping for identifiers within this
3247  // module.
3248  GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3249  &F));
3250 
3251  // Introduce the local -> global mapping for identifiers within this
3252  // module.
3254  std::make_pair(LocalBaseIdentifierID,
3255  F.BaseIdentifierID - LocalBaseIdentifierID));
3256 
3257  IdentifiersLoaded.resize(IdentifiersLoaded.size()
3258  + F.LocalNumIdentifiers);
3259  }
3260  break;
3261  }
3262 
3264  F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3265  break;
3266 
3268  // FIXME: Skip reading this record if our ASTConsumer doesn't care
3269  // about "interesting" decls (for instance, if we're building a module).
3270  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3271  EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3272  break;
3273 
3274  case MODULAR_CODEGEN_DECLS:
3275  // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3276  // them (ie: if we're not codegenerating this module).
3277  if (F.Kind == MK_MainFile ||
3278  getContext().getLangOpts().BuildingPCHWithObjectFile)
3279  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3280  EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3281  break;
3282 
3283  case SPECIAL_TYPES:
3284  if (SpecialTypes.empty()) {
3285  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3286  SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3287  break;
3288  }
3289 
3290  if (SpecialTypes.size() != Record.size())
3291  return llvm::createStringError(std::errc::illegal_byte_sequence,
3292  "invalid special-types record");
3293 
3294  for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3295  serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3296  if (!SpecialTypes[I])
3297  SpecialTypes[I] = ID;
3298  // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3299  // merge step?
3300  }
3301  break;
3302 
3303  case STATISTICS:
3304  TotalNumStatements += Record[0];
3305  TotalNumMacros += Record[1];
3306  TotalLexicalDeclContexts += Record[2];
3307  TotalVisibleDeclContexts += Record[3];
3308  break;
3309 
3311  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3312  UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3313  break;
3314 
3315  case DELEGATING_CTORS:
3316  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3317  DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3318  break;
3319 
3321  if (Record.size() % 3 != 0)
3322  return llvm::createStringError(std::errc::illegal_byte_sequence,
3323  "invalid weak identifiers record");
3324 
3325  // FIXME: Ignore weak undeclared identifiers from non-original PCH
3326  // files. This isn't the way to do it :)
3327  WeakUndeclaredIdentifiers.clear();
3328 
3329  // Translate the weak, undeclared identifiers into global IDs.
3330  for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3331  WeakUndeclaredIdentifiers.push_back(
3332  getGlobalIdentifierID(F, Record[I++]));
3333  WeakUndeclaredIdentifiers.push_back(
3334  getGlobalIdentifierID(F, Record[I++]));
3335  WeakUndeclaredIdentifiers.push_back(
3336  ReadSourceLocation(F, Record, I).getRawEncoding());
3337  }
3338  break;
3339 
3340  case SELECTOR_OFFSETS: {
3341  F.SelectorOffsets = (const uint32_t *)Blob.data();
3342  F.LocalNumSelectors = Record[0];
3343  unsigned LocalBaseSelectorID = Record[1];
3344  F.BaseSelectorID = getTotalNumSelectors();
3345 
3346  if (F.LocalNumSelectors > 0) {
3347  // Introduce the global -> local mapping for selectors within this
3348  // module.
3349  GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3350 
3351  // Introduce the local -> global mapping for selectors within this
3352  // module.
3354  std::make_pair(LocalBaseSelectorID,
3355  F.BaseSelectorID - LocalBaseSelectorID));
3356 
3357  SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3358  }
3359  break;
3360  }
3361 
3362  case METHOD_POOL:
3363  F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3364  if (Record[0])
3366  = ASTSelectorLookupTable::Create(
3367  F.SelectorLookupTableData + Record[0],
3369  ASTSelectorLookupTrait(*this, F));
3370  TotalNumMethodPoolEntries += Record[1];
3371  break;
3372 
3374  if (!Record.empty()) {
3375  for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3376  ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3377  Record[Idx++]));
3378  ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3379  getRawEncoding());
3380  }
3381  }
3382  break;
3383 
3384  case PP_ASSUME_NONNULL_LOC: {
3385  unsigned Idx = 0;
3386  if (!Record.empty())
3388  ReadSourceLocation(F, Record, Idx));
3389  break;
3390  }
3391 
3392  case PP_CONDITIONAL_STACK:
3393  if (!Record.empty()) {
3394  unsigned Idx = 0, End = Record.size() - 1;
3395  bool ReachedEOFWhileSkipping = Record[Idx++];
3397  if (ReachedEOFWhileSkipping) {
3398  SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3399  SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3400  bool FoundNonSkipPortion = Record[Idx++];
3401  bool FoundElse = Record[Idx++];
3402  SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3403  SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3404  FoundElse, ElseLoc);
3405  }
3406  SmallVector<PPConditionalInfo, 4> ConditionalStack;
3407  while (Idx < End) {
3408  auto Loc = ReadSourceLocation(F, Record, Idx);
3409  bool WasSkipping = Record[Idx++];
3410  bool FoundNonSkip = Record[Idx++];
3411  bool FoundElse = Record[Idx++];
3412  ConditionalStack.push_back(
3413  {Loc, WasSkipping, FoundNonSkip, FoundElse});
3414  }
3415  PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3416  }
3417  break;
3418 
3419  case PP_COUNTER_VALUE:
3420  if (!Record.empty() && Listener)
3421  Listener->ReadCounter(F, Record[0]);
3422  break;
3423 
3424  case FILE_SORTED_DECLS:
3425  F.FileSortedDecls = (const DeclID *)Blob.data();
3426  F.NumFileSortedDecls = Record[0];
3427  break;
3428 
3429  case SOURCE_LOCATION_OFFSETS: {
3430  F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3431  F.LocalNumSLocEntries = Record[0];
3432  SourceLocation::UIntTy SLocSpaceSize = Record[1];
3434  std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3435  SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3436  SLocSpaceSize);
3437  if (!F.SLocEntryBaseID) {
3438  if (!Diags.isDiagnosticInFlight()) {
3439  Diags.Report(SourceLocation(), diag::remark_sloc_usage);
3440  SourceMgr.noteSLocAddressSpaceUsage(Diags);
3441  }
3442  return llvm::createStringError(std::errc::invalid_argument,
3443  "ran out of source locations");
3444  }
3445  // Make our entry in the range map. BaseID is negative and growing, so
3446  // we invert it. Because we invert it, though, we need the other end of
3447  // the range.
3448  unsigned RangeStart =
3450  GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3452 
3453  // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3454  assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3455  GlobalSLocOffsetMap.insert(
3456  std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3457  - SLocSpaceSize,&F));
3458 
3459  // Initialize the remapping table.
3460  // Invalid stays invalid.
3461  F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3462  // This module. Base was 2 when being compiled.
3463  F.SLocRemap.insertOrReplace(std::make_pair(
3464  2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3465 
3466  TotalNumSLocEntries += F.LocalNumSLocEntries;
3467  break;
3468  }
3469 
3470  case MODULE_OFFSET_MAP:
3471  F.ModuleOffsetMap = Blob;
3472  break;
3473 
3475  ParseLineTable(F, Record);
3476  break;
3477 
3478  case SOURCE_LOCATION_PRELOADS: {
3479  // Need to transform from the local view (1-based IDs) to the global view,
3480  // which is based off F.SLocEntryBaseID.
3481  if (!F.PreloadSLocEntries.empty())
3482  return llvm::createStringError(
3483  std::errc::illegal_byte_sequence,
3484  "Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3485 
3486  F.PreloadSLocEntries.swap(Record);
3487  break;
3488  }
3489 
3490  case EXT_VECTOR_DECLS:
3491  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3492  ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3493  break;
3494 
3495  case VTABLE_USES:
3496  if (Record.size() % 3 != 0)
3497  return llvm::createStringError(std::errc::illegal_byte_sequence,
3498  "Invalid VTABLE_USES record");
3499 
3500  // Later tables overwrite earlier ones.
3501  // FIXME: Modules will have some trouble with this. This is clearly not
3502  // the right way to do this.
3503  VTableUses.clear();
3504 
3505  for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3506  VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3507  VTableUses.push_back(
3508  ReadSourceLocation(F, Record, Idx).getRawEncoding());
3509  VTableUses.push_back(Record[Idx++]);
3510  }
3511  break;
3512 
3514  if (PendingInstantiations.size() % 2 != 0)
3515  return llvm::createStringError(
3516  std::errc::illegal_byte_sequence,
3517  "Invalid existing PendingInstantiations");
3518 
3519  if (Record.size() % 2 != 0)
3520  return llvm::createStringError(
3521  std::errc::illegal_byte_sequence,
3522  "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3523 
3524  for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3525  PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3526  PendingInstantiations.push_back(
3527  ReadSourceLocation(F, Record, I).getRawEncoding());
3528  }
3529  break;
3530 
3531  case SEMA_DECL_REFS:
3532  if (Record.size() != 3)
3533  return llvm::createStringError(std::errc::illegal_byte_sequence,
3534  "Invalid SEMA_DECL_REFS block");
3535  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3536  SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3537  break;
3538 
3539  case PPD_ENTITIES_OFFSETS: {
3540  F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3541  assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3542  F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3543 
3544  unsigned LocalBasePreprocessedEntityID = Record[0];
3545 
3546  unsigned StartingID;
3547  if (!PP.getPreprocessingRecord())
3551  StartingID
3552  = PP.getPreprocessingRecord()
3553  ->allocateLoadedEntities(F.NumPreprocessedEntities);
3554  F.BasePreprocessedEntityID = StartingID;
3555 
3556  if (F.NumPreprocessedEntities > 0) {
3557  // Introduce the global -> local mapping for preprocessed entities in
3558  // this module.
3559  GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3560 
3561  // Introduce the local -> global mapping for preprocessed entities in
3562  // this module.
3564  std::make_pair(LocalBasePreprocessedEntityID,
3565  F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3566  }
3567 
3568  break;
3569  }
3570 
3571  case PPD_SKIPPED_RANGES: {
3572  F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3573  assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3574  F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3575 
3576  if (!PP.getPreprocessingRecord())
3581  ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3582 
3583  if (F.NumPreprocessedSkippedRanges > 0)
3584  GlobalSkippedRangeMap.insert(
3585  std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3586  break;
3587  }
3588 
3589  case DECL_UPDATE_OFFSETS:
3590  if (Record.size() % 2 != 0)
3591  return llvm::createStringError(
3592  std::errc::illegal_byte_sequence,
3593  "invalid DECL_UPDATE_OFFSETS block in AST file");
3594  for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3595  GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3596  DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3597 
3598  // If we've already loaded the decl, perform the updates when we finish
3599  // loading this block.
3600  if (Decl *D = GetExistingDecl(ID))
3601  PendingUpdateRecords.push_back(
3602  PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3603  }
3604  break;
3605 
3606  case OBJC_CATEGORIES_MAP:
3607  if (F.LocalNumObjCCategoriesInMap != 0)
3608  return llvm::createStringError(
3609  std::errc::illegal_byte_sequence,
3610  "duplicate OBJC_CATEGORIES_MAP record in AST file");
3611 
3612  F.LocalNumObjCCategoriesInMap = Record[0];
3613  F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3614  break;
3615 
3616  case OBJC_CATEGORIES:
3617  F.ObjCCategories.swap(Record);
3618  break;
3619 
3621  // Later tables overwrite earlier ones.
3622  // FIXME: Modules will have trouble with this.
3623  CUDASpecialDeclRefs.clear();
3624  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3625  CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3626  break;
3627 
3628  case HEADER_SEARCH_TABLE:
3629  F.HeaderFileInfoTableData = Blob.data();
3630  F.LocalNumHeaderFileInfos = Record[1];
3631  if (Record[0]) {
3633  = HeaderFileInfoLookupTable::Create(
3634  (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3635  (const unsigned char *)F.HeaderFileInfoTableData,
3636  HeaderFileInfoTrait(*this, F,
3637  &PP.getHeaderSearchInfo(),
3638  Blob.data() + Record[2]));
3639 
3643  }
3644  break;
3645 
3646  case FP_PRAGMA_OPTIONS:
3647  // Later tables overwrite earlier ones.
3648  FPPragmaOptions.swap(Record);
3649  break;
3650 
3651  case OPENCL_EXTENSIONS:
3652  for (unsigned I = 0, E = Record.size(); I != E; ) {
3653  auto Name = ReadString(Record, I);
3654  auto &OptInfo = OpenCLExtensions.OptMap[Name];
3655  OptInfo.Supported = Record[I++] != 0;
3656  OptInfo.Enabled = Record[I++] != 0;
3657  OptInfo.WithPragma = Record[I++] != 0;
3658  OptInfo.Avail = Record[I++];
3659  OptInfo.Core = Record[I++];
3660  OptInfo.Opt = Record[I++];
3661  }
3662  break;
3663 
3664  case TENTATIVE_DEFINITIONS:
3665  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3666  TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3667  break;
3668 
3669  case KNOWN_NAMESPACES:
3670  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3671  KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3672  break;
3673 
3674  case UNDEFINED_BUT_USED:
3675  if (UndefinedButUsed.size() % 2 != 0)
3676  return llvm::createStringError(std::errc::illegal_byte_sequence,
3677  "Invalid existing UndefinedButUsed");
3678 
3679  if (Record.size() % 2 != 0)
3680  return llvm::createStringError(std::errc::illegal_byte_sequence,
3681  "invalid undefined-but-used record");
3682  for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3683  UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3684  UndefinedButUsed.push_back(
3685  ReadSourceLocation(F, Record, I).getRawEncoding());
3686  }
3687  break;
3688 
3690  for (unsigned I = 0, N = Record.size(); I != N;) {
3691  DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3692  const uint64_t Count = Record[I++];
3693  DelayedDeleteExprs.push_back(Count);
3694  for (uint64_t C = 0; C < Count; ++C) {
3695  DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3696  bool IsArrayForm = Record[I++] == 1;
3697  DelayedDeleteExprs.push_back(IsArrayForm);
3698  }
3699  }
3700  break;
3701 
3702  case IMPORTED_MODULES:
3703  if (!F.isModule()) {
3704  // If we aren't loading a module (which has its own exports), make
3705  // all of the imported modules visible.
3706  // FIXME: Deal with macros-only imports.
3707  for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3708  unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3709  SourceLocation Loc = ReadSourceLocation(F, Record, I);
3710  if (GlobalID) {
3711  ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3712  if (DeserializationListener)
3713  DeserializationListener->ModuleImportRead(GlobalID, Loc);
3714  }
3715  }
3716  }
3717  break;
3718 
3719  case MACRO_OFFSET: {
3720  if (F.LocalNumMacros != 0)
3721  return llvm::createStringError(
3722  std::errc::illegal_byte_sequence,
3723  "duplicate MACRO_OFFSET record in AST file");
3724  F.MacroOffsets = (const uint32_t *)Blob.data();
3725  F.LocalNumMacros = Record[0];
3726  unsigned LocalBaseMacroID = Record[1];
3727  F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3728  F.BaseMacroID = getTotalNumMacros();
3729 
3730  if (F.LocalNumMacros > 0) {
3731  // Introduce the global -> local mapping for macros within this module.
3732  GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3733 
3734  // Introduce the local -> global mapping for macros within this module.
3736  std::make_pair(LocalBaseMacroID,
3737  F.BaseMacroID - LocalBaseMacroID));
3738 
3739  MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3740  }
3741  break;
3742  }
3743 
3744  case PP_INCLUDED_FILES:
3745  readIncludedFiles(F, Blob, PP);
3746  break;
3747 
3748  case LATE_PARSED_TEMPLATE:
3749  LateParsedTemplates.emplace_back(
3750  std::piecewise_construct, std::forward_as_tuple(&F),
3751  std::forward_as_tuple(Record.begin(), Record.end()));
3752  break;
3753 
3755  if (Record.size() != 1)
3756  return llvm::createStringError(std::errc::illegal_byte_sequence,
3757  "invalid pragma optimize record");
3758  OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3759  break;
3760 
3762  if (Record.size() != 1)
3763  return llvm::createStringError(std::errc::illegal_byte_sequence,
3764  "invalid pragma ms_struct record");
3765  PragmaMSStructState = Record[0];
3766  break;
3767 
3769  if (Record.size() != 2)
3770  return llvm::createStringError(
3771  std::errc::illegal_byte_sequence,
3772  "invalid pragma pointers to members record");
3773  PragmaMSPointersToMembersState = Record[0];
3774  PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3775  break;
3776 
3778  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3779  UnusedLocalTypedefNameCandidates.push_back(
3780  getGlobalDeclID(F, Record[I]));
3781  break;
3782 
3784  if (Record.size() != 1)
3785  return llvm::createStringError(std::errc::illegal_byte_sequence,
3786  "invalid cuda pragma options record");
3787  ForceCUDAHostDeviceDepth = Record[0];
3788  break;
3789 
3791  if (Record.size() < 3)
3792  return llvm::createStringError(std::errc::illegal_byte_sequence,
3793  "invalid pragma pack record");
3794  PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3795  PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3796  unsigned NumStackEntries = Record[2];
3797  unsigned Idx = 3;
3798  // Reset the stack when importing a new module.
3799  PragmaAlignPackStack.clear();
3800  for (unsigned I = 0; I < NumStackEntries; ++I) {
3801  PragmaAlignPackStackEntry Entry;
3802  Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3803  Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3804  Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3805  PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3806  Entry.SlotLabel = PragmaAlignPackStrings.back();
3807  PragmaAlignPackStack.push_back(Entry);
3808  }
3809  break;
3810  }
3811 
3813  if (Record.size() < 3)
3814  return llvm::createStringError(std::errc::illegal_byte_sequence,
3815  "invalid pragma float control record");
3816  FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3817  FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3818  unsigned NumStackEntries = Record[2];
3819  unsigned Idx = 3;
3820  // Reset the stack when importing a new module.
3821  FpPragmaStack.clear();
3822  for (unsigned I = 0; I < NumStackEntries; ++I) {
3823  FpPragmaStackEntry Entry;
3824  Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3825  Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3826  Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3827  FpPragmaStrings.push_back(ReadString(Record, Idx));
3828  Entry.SlotLabel = FpPragmaStrings.back();
3829  FpPragmaStack.push_back(Entry);
3830  }
3831  break;
3832  }
3833 
3835  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3836  DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3837  break;
3838  }
3839  }
3840 }
3841 
3842 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3843  assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3844 
3845  // Additional remapping information.
3846  const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3847  const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3848  F.ModuleOffsetMap = StringRef();
3849 
3850  // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3851  if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3852  F.SLocRemap.insert(std::make_pair(0U, 0));
3853  F.SLocRemap.insert(std::make_pair(2U, 1));
3854  }
3855 
3856  // Continuous range maps we may be updating in our module.
3857  using SLocRemapBuilder =
3859  2>::Builder;
3860  using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3861  SLocRemapBuilder SLocRemap(F.SLocRemap);
3862  RemapBuilder IdentifierRemap(F.IdentifierRemap);
3863  RemapBuilder MacroRemap(F.MacroRemap);
3864  RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3865  RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3866  RemapBuilder SelectorRemap(F.SelectorRemap);
3867  RemapBuilder DeclRemap(F.DeclRemap);
3868  RemapBuilder TypeRemap(F.TypeRemap);
3869 
3870  while (Data < DataEnd) {
3871  // FIXME: Looking up dependency modules by filename is horrible. Let's
3872  // start fixing this with prebuilt, explicit and implicit modules and see
3873  // how it goes...
3874  using namespace llvm::support;
3875  ModuleKind Kind = static_cast<ModuleKind>(
3876  endian::readNext<uint8_t, little, unaligned>(Data));
3877  uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3878  StringRef Name = StringRef((const char*)Data, Len);
3879  Data += Len;
3882  ? ModuleMgr.lookupByModuleName(Name)
3883  : ModuleMgr.lookupByFileName(Name));
3884  if (!OM) {
3885  std::string Msg =
3886  "SourceLocation remap refers to unknown module, cannot find ";
3887  Msg.append(std::string(Name));
3888  Error(Msg);
3889  return;
3890  }
3891 
3892  SourceLocation::UIntTy SLocOffset =
3893  endian::readNext<uint32_t, little, unaligned>(Data);
3894  uint32_t IdentifierIDOffset =
3895  endian::readNext<uint32_t, little, unaligned>(Data);
3896  uint32_t MacroIDOffset =
3897  endian::readNext<uint32_t, little, unaligned>(Data);
3898  uint32_t PreprocessedEntityIDOffset =
3899  endian::readNext<uint32_t, little, unaligned>(Data);
3900  uint32_t SubmoduleIDOffset =
3901  endian::readNext<uint32_t, little, unaligned>(Data);
3902  uint32_t SelectorIDOffset =
3903  endian::readNext<uint32_t, little, unaligned>(Data);
3904  uint32_t DeclIDOffset =
3905  endian::readNext<uint32_t, little, unaligned>(Data);
3906  uint32_t TypeIndexOffset =
3907  endian::readNext<uint32_t, little, unaligned>(Data);
3908 
3909  auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3910  RemapBuilder &Remap) {
3911  constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3912  if (Offset != None)
3913  Remap.insert(std::make_pair(Offset,
3914  static_cast<int>(BaseOffset - Offset)));
3915  };
3916 
3917  constexpr SourceLocation::UIntTy SLocNone =
3919  if (SLocOffset != SLocNone)
3920  SLocRemap.insert(std::make_pair(
3921  SLocOffset, static_cast<SourceLocation::IntTy>(
3922  OM->SLocEntryBaseOffset - SLocOffset)));
3923 
3924  mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3925  mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3926  mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3927  PreprocessedEntityRemap);
3928  mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3929  mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3930  mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3931  mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3932 
3933  // Global -> local mappings.
3934  F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3935  }
3936 }
3937 
3939 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3940  const ModuleFile *ImportedBy,
3941  unsigned ClientLoadCapabilities) {
3942  unsigned Idx = 0;
3943  F.ModuleMapPath = ReadPath(F, Record, Idx);
3944 
3945  // Try to resolve ModuleName in the current header search context and
3946  // verify that it is found in the same module map file as we saved. If the
3947  // top-level AST file is a main file, skip this check because there is no
3948  // usable header search context.
3949  assert(!F.ModuleName.empty() &&
3950  "MODULE_NAME should come before MODULE_MAP_FILE");
3951  if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3952  // An implicitly-loaded module file should have its module listed in some
3953  // module map file that we've already loaded.
3954  Module *M =
3956  auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3957  Optional<FileEntryRef> ModMap =
3958  M ? Map.getModuleMapFileForUniquing(M) : None;
3959  // Don't emit module relocation error if we have -fno-validate-pch
3962  !ModMap) {
3963  if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3964  if (auto ASTFE = M ? M->getASTFile() : None) {
3965  // This module was defined by an imported (explicit) module.
3966  Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3967  << ASTFE->getName();
3968  } else {
3969  // This module was built with a different module map.
3970  Diag(diag::err_imported_module_not_found)
3971  << F.ModuleName << F.FileName
3972  << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3973  << !ImportedBy;
3974  // In case it was imported by a PCH, there's a chance the user is
3975  // just missing to include the search path to the directory containing
3976  // the modulemap.
3977  if (ImportedBy && ImportedBy->Kind == MK_PCH)
3978  Diag(diag::note_imported_by_pch_module_not_found)
3979  << llvm::sys::path::parent_path(F.ModuleMapPath);
3980  }
3981  }
3982  return OutOfDate;
3983  }
3984 
3985  assert(M && M->Name == F.ModuleName && "found module with different name");
3986 
3987  // Check the primary module map file.
3988  auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3989  if (!StoredModMap || *StoredModMap != ModMap) {
3990  assert(ModMap && "found module is missing module map file");
3991  assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3992  "top-level import should be verified");
3993  bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3994  if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3995  Diag(diag::err_imported_module_modmap_changed)
3996  << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3997  << ModMap->getName() << F.ModuleMapPath << NotImported;
3998  return OutOfDate;
3999  }
4000 
4001  llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
4002  for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4003  // FIXME: we should use input files rather than storing names.
4004  std::string Filename = ReadPath(F, Record, Idx);
4005  auto SF = FileMgr.getFile(Filename, false, false);
4006  if (!SF) {
4007  if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4008  Error("could not find file '" + Filename +"' referenced by AST file");
4009  return OutOfDate;
4010  }
4011  AdditionalStoredMaps.insert(*SF);
4012  }
4013 
4014  // Check any additional module map files (e.g. module.private.modulemap)
4015  // that are not in the pcm.
4016  if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4017  for (const FileEntry *ModMap : *AdditionalModuleMaps) {
4018  // Remove files that match
4019  // Note: SmallPtrSet::erase is really remove
4020  if (!AdditionalStoredMaps.erase(ModMap)) {
4021  if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4022  Diag(diag::err_module_different_modmap)
4023  << F.ModuleName << /*new*/0 << ModMap->getName();
4024  return OutOfDate;
4025  }
4026  }
4027  }
4028 
4029  // Check any additional module map files that are in the pcm, but not
4030  // found in header search. Cases that match are already removed.
4031  for (const FileEntry *ModMap : AdditionalStoredMaps) {
4032  if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4033  Diag(diag::err_module_different_modmap)
4034  << F.ModuleName << /*not new*/1 << ModMap->getName();
4035  return OutOfDate;
4036  }
4037  }
4038 
4039  if (Listener)
4040  Listener->ReadModuleMapFile(F.ModuleMapPath);
4041  return Success;
4042 }
4043 
4044 /// Move the given method to the back of the global list of methods.
4046  // Find the entry for this selector in the method pool.
4048  = S.MethodPool.find(Method->getSelector());
4049  if (Known == S.MethodPool.end())
4050  return;
4051 
4052  // Retrieve the appropriate method list.
4053  ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4054  : Known->second.second;
4055  bool Found = false;
4056  for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4057  if (!Found) {
4058  if (List->getMethod() == Method) {
4059  Found = true;
4060  } else {
4061  // Keep searching.
4062  continue;
4063  }
4064  }
4065 
4066  if (List->getNext())
4067  List->setMethod(List->getNext()->getMethod());
4068  else
4069  List->setMethod(Method);
4070  }
4071 }
4072 
4073 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4074  assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4075  for (Decl *D : Names) {
4076  bool wasHidden = !D->isUnconditionallyVisible();
4077  D->setVisibleDespiteOwningModule();
4078 
4079  if (wasHidden && SemaObj) {
4080  if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4081  moveMethodToBackOfGlobalList(*SemaObj, Method);
4082  }
4083  }
4084  }
4085 }
4086 
4088  Module::NameVisibilityKind NameVisibility,
4089  SourceLocation ImportLoc) {
4092  Stack.push_back(Mod);
4093  while (!Stack.empty()) {
4094  Mod = Stack.pop_back_val();
4095 
4096  if (NameVisibility <= Mod->NameVisibility) {
4097  // This module already has this level of visibility (or greater), so
4098  // there is nothing more to do.
4099  continue;
4100  }
4101 
4102  if (Mod->isUnimportable()) {
4103  // Modules that aren't importable cannot be made visible.
4104  continue;
4105  }
4106 
4107  // Update the module's name visibility.
4108  Mod->NameVisibility = NameVisibility;
4109 
4110  // If we've already deserialized any names from this module,
4111  // mark them as visible.
4112  HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4113  if (Hidden != HiddenNamesMap.end()) {
4114  auto HiddenNames = std::move(*Hidden);
4115  HiddenNamesMap.erase(Hidden);
4116  makeNamesVisible(HiddenNames.second, HiddenNames.first);
4117  assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4118  "making names visible added hidden names");
4119  }
4120 
4121  // Push any exported modules onto the stack to be marked as visible.
4122  SmallVector<Module *, 16> Exports;
4123  Mod->getExportedModules(Exports);
4125  I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4126  Module *Exported = *I;
4127  if (Visited.insert(Exported).second)
4128  Stack.push_back(Exported);
4129  }
4130  }
4131 }
4132 
4133 /// We've merged the definition \p MergedDef into the existing definition
4134 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4135 /// visible.
4137  NamedDecl *MergedDef) {
4138  if (!Def->isUnconditionallyVisible()) {
4139  // If MergedDef is visible or becomes visible, make the definition visible.
4140  if (MergedDef->isUnconditionallyVisible())
4142  else {
4143  getContext().mergeDefinitionIntoModule(
4144  Def, MergedDef->getImportedOwningModule(),
4145  /*NotifyListeners*/ false);
4146  PendingMergedDefinitionsToDeduplicate.insert(Def);
4147  }
4148  }
4149 }
4150 
4152  if (GlobalIndex)
4153  return false;
4154 
4155  if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4156  !PP.getLangOpts().Modules)
4157  return true;
4158 
4159  // Try to load the global index.
4160  TriedLoadingGlobalIndex = true;
4161  StringRef ModuleCachePath
4162  = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4163  std::pair<GlobalModuleIndex *, llvm::Error> Result =
4164  GlobalModuleIndex::readIndex(ModuleCachePath);
4165  if (llvm::Error Err = std::move(Result.second)) {
4166  assert(!Result.first);
4167  consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4168  return true;
4169  }
4170 
4171  GlobalIndex.reset(Result.first);
4172  ModuleMgr.setGlobalIndex(GlobalIndex.get());
4173  return false;
4174 }
4175 
4177  return PP.getLangOpts().Modules && UseGlobalIndex &&
4178  !hasGlobalIndex() && TriedLoadingGlobalIndex;
4179 }
4180 
4182  // Overwrite the timestamp file contents so that file's mtime changes.
4183  std::string TimestampFilename = MF.getTimestampFilename();
4184  std::error_code EC;
4185  llvm::raw_fd_ostream OS(TimestampFilename, EC,
4186  llvm::sys::fs::OF_TextWithCRLF);
4187  if (EC)
4188  return;
4189  OS << "Timestamp file\n";
4190  OS.close();
4191  OS.clear_error(); // Avoid triggering a fatal error.
4192 }
4193 
4194 /// Given a cursor at the start of an AST file, scan ahead and drop the
4195 /// cursor into the start of the given block ID, returning false on success and
4196 /// true on failure.
4197 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4198  while (true) {
4199  Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4200  if (!MaybeEntry) {
4201  // FIXME this drops errors on the floor.
4202  consumeError(MaybeEntry.takeError());
4203  return true;
4204  }
4205  llvm::BitstreamEntry Entry = MaybeEntry.get();
4206 
4207  switch (Entry.Kind) {
4209  case llvm::BitstreamEntry::EndBlock:
4210  return true;
4211 
4212  case llvm::BitstreamEntry::Record:
4213  // Ignore top-level records.
4214  if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4215  break;
4216  else {
4217  // FIXME this drops errors on the floor.
4218  consumeError(Skipped.takeError());
4219  return true;
4220  }
4221 
4222  case llvm::BitstreamEntry::SubBlock:
4223  if (Entry.ID == BlockID) {
4224  if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4225  // FIXME this drops the error on the floor.
4226  consumeError(std::move(Err));
4227  return true;
4228  }
4229  // Found it!
4230  return false;
4231  }
4232 
4233  if (llvm::Error Err = Cursor.SkipBlock()) {
4234  // FIXME this drops the error on the floor.
4235  consumeError(std::move(Err));
4236  return true;
4237  }
4238  }
4239  }
4240 }
4241 
4243  ModuleKind Type,
4244  SourceLocation ImportLoc,
4245  unsigned ClientLoadCapabilities,
4247  llvm::TimeTraceScope scope("ReadAST", FileName);
4248 
4250  SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4251  llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4252  CurrentDeserializingModuleKind, Type);
4253 
4254  // Defer any pending actions until we get to the end of reading the AST file.
4255  Deserializing AnASTFile(this);
4256 
4257  // Bump the generation number.
4258  unsigned PreviousGeneration = 0;
4259  if (ContextObj)
4260  PreviousGeneration = incrementGeneration(*ContextObj);
4261 
4262  unsigned NumModules = ModuleMgr.size();
4264  if (ASTReadResult ReadResult =
4265  ReadASTCore(FileName, Type, ImportLoc,
4266  /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4267  ClientLoadCapabilities)) {
4268  ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);
4269 
4270  // If we find that any modules are unusable, the global index is going
4271  // to be out-of-date. Just remove it.
4272  GlobalIndex.reset();
4273  ModuleMgr.setGlobalIndex(nullptr);
4274  return ReadResult;
4275  }
4276 
4277  // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4278  // remove modules from this point. Various fields are updated during reading
4279  // the AST block and removing the modules would result in dangling pointers.
4280  // They are generally only incidentally dereferenced, ie. a binary search
4281  // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4282  // be dereferenced but it wouldn't actually be used.
4283 
4284  // Load the AST blocks of all of the modules that we loaded. We can still
4285  // hit errors parsing the ASTs at this point.
4286  for (ImportedModule &M : Loaded) {
4287  ModuleFile &F = *M.Mod;
4288 
4289  // Read the AST block.
4290  if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4291  Error(std::move(Err));
4292  return Failure;
4293  }
4294 
4295  // The AST block should always have a definition for the main module.
4296  if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4297  Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4298  return Failure;
4299  }
4300 
4301  // Read the extension blocks.
4303  if (llvm::Error Err = ReadExtensionBlock(F)) {
4304  Error(std::move(Err));
4305  return Failure;
4306  }
4307  }
4308 
4309  // Once read, set the ModuleFile bit base offset and update the size in
4310  // bits of all files we've seen.
4311  F.GlobalBitOffset = TotalModulesSizeInBits;
4312  TotalModulesSizeInBits += F.SizeInBits;
4313  GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4314  }
4315 
4316  // Preload source locations and interesting indentifiers.
4317  for (ImportedModule &M : Loaded) {
4318  ModuleFile &F = *M.Mod;
4319 
4320  // Preload SLocEntries.
4321  for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4322  int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4323  // Load it through the SourceManager and don't call ReadSLocEntry()
4324  // directly because the entry may have already been loaded in which case
4325  // calling ReadSLocEntry() directly would trigger an assertion in
4326  // SourceManager.
4327  SourceMgr.getLoadedSLocEntryByID(Index);
4328  }
4329 
4330  // Map the original source file ID into the ID space of the current
4331  // compilation.
4332  if (F.OriginalSourceFileID.isValid())
4333  F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);
4334 
4335  // Preload all the pending interesting identifiers by marking them out of
4336  // date.
4337  for (auto Offset : F.PreloadIdentifierOffsets) {
4338  const unsigned char *Data = F.IdentifierTableData + Offset;
4339 
4340  ASTIdentifierLookupTrait Trait(*this, F);
4341  auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4342  auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4343  auto &II = PP.getIdentifierTable().getOwn(Key);
4344  II.setOutOfDate(true);
4345 
4346  // Mark this identifier as being from an AST file so that we can track
4347  // whether we need to serialize it.
4348  markIdentifierFromAST(*this, II);
4349 
4350  // Associate the ID with the identifier so that the writer can reuse it.
4351  auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4352  SetIdentifierInfo(ID, &II);
4353  }
4354  }
4355 
4356  // Setup the import locations and notify the module manager that we've
4357  // committed to these module files.
4358  for (ImportedModule &M : Loaded) {
4359  ModuleFile &F = *M.Mod;
4360 
4361  ModuleMgr.moduleFileAccepted(&F);
4362 
4363  // Set the import location.
4364  F.DirectImportLoc = ImportLoc;
4365  // FIXME: We assume that locations from PCH / preamble do not need
4366  // any translation.
4367  if (!M.ImportedBy)
4368  F.ImportLoc = M.ImportLoc;
4369  else
4370  F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4371  }
4372 
4373  if (!PP.getLangOpts().CPlusPlus ||
4375  Type != MK_PrebuiltModule)) {
4376  // Mark all of the identifiers in the identifier table as being out of date,
4377  // so that various accessors know to check the loaded modules when the
4378  // identifier is used.
4379  //
4380  // For C++ modules, we don't need information on many identifiers (just
4381  // those that provide macros or are poisoned), so we mark all of
4382  // the interesting ones via PreloadIdentifierOffsets.
4384  IdEnd = PP.getIdentifierTable().end();
4385  Id != IdEnd; ++Id)
4386  Id->second->setOutOfDate(true);
4387  }
4388  // Mark selectors as out of date.
4389  for (auto Sel : SelectorGeneration)
4390  SelectorOutOfDate[Sel.first] = true;
4391 
4392  // Resolve any unresolved module exports.
4393  for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4394  UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4395  SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4396  Module *ResolvedMod = getSubmodule(GlobalID);
4397 
4398  switch (Unresolved.Kind) {
4399  case UnresolvedModuleRef::Conflict:
4400  if (ResolvedMod) {
4401  Module::Conflict Conflict;
4402  Conflict.Other = ResolvedMod;
4403  Conflict.Message = Unresolved.String.str();
4404  Unresolved.Mod->Conflicts.push_back(Conflict);
4405  }
4406  continue;
4407 
4408  case UnresolvedModuleRef::Import:
4409  if (ResolvedMod)
4410  Unresolved.Mod->Imports.insert(ResolvedMod);
4411  continue;
4412 
4413  case UnresolvedModuleRef::Affecting:
4414  if (ResolvedMod)
4415  Unresolved.Mod->AffectingClangModules.insert(ResolvedMod);
4416  continue;
4417 
4418  case UnresolvedModuleRef::Export:
4419  if (ResolvedMod || Unresolved.IsWildcard)
4420  Unresolved.Mod->Exports.push_back(
4421  Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4422  continue;
4423  }
4424  }
4425  UnresolvedModuleRefs.clear();
4426 
4427  if (Imported)
4428  Imported->append(ImportedModules.begin(),
4429  ImportedModules.end());
4430 
4431  // FIXME: How do we load the 'use'd modules? They may not be submodules.
4432  // Might be unnecessary as use declarations are only used to build the
4433  // module itself.
4434 
4435  if (ContextObj)
4436  InitializeContext();
4437 
4438  if (SemaObj)
4439  UpdateSema();
4440 
4441  if (DeserializationListener)
4442  DeserializationListener->ReaderInitialized(this);
4443 
4444  ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4445  if (PrimaryModule.OriginalSourceFileID.isValid()) {
4446  // If this AST file is a precompiled preamble, then set the
4447  // preamble file ID of the source manager to the file source file
4448  // from which the preamble was built.
4449  if (Type == MK_Preamble) {
4450  SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4451  } else if (Type == MK_MainFile) {
4452  SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4453  }
4454  }
4455 
4456  // For any Objective-C class definitions we have already loaded, make sure
4457  // that we load any additional categories.
4458  if (ContextObj) {
4459  for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4460  loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4461  ObjCClassesLoaded[I],
4462  PreviousGeneration);
4463  }
4464  }
4465 
4466  if (PP.getHeaderSearchInfo()
4469  // Now we are certain that the module and all modules it depends on are
4470  // up to date. Create or update timestamp files for modules that are
4471  // located in the module cache (not for PCH files that could be anywhere
4472  // in the filesystem).
4473  for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4474  ImportedModule &M = Loaded[I];
4475  if (M.Mod->Kind == MK_ImplicitModule) {
4476  updateModuleTimestamp(*M.Mod);
4477  }
4478  }
4479  }
4480 
4481  return Success;
4482 }
4483 
4484 static ASTFileSignature readASTFileSignature(StringRef PCH);
4485 
4486 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4487 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4488  // FIXME checking magic headers is done in other places such as
4489  // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4490  // always done the same. Unify it all with a helper.
4491  if (!Stream.canSkipToPos(4))
4492  return llvm::createStringError(std::errc::illegal_byte_sequence,
4493  "file too small to contain AST file magic");
4494  for (unsigned C : {'C', 'P', 'C', 'H'})
4495  if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4496  if (Res.get() != C)
4497  return llvm::createStringError(
4498  std::errc::illegal_byte_sequence,
4499  "file doesn't start with AST file magic");
4500  } else
4501  return Res.takeError();
4502  return llvm::Error::success();
4503 }
4504 
4506  switch (Kind) {
4507  case MK_PCH:
4508  return 0; // PCH
4509  case MK_ImplicitModule:
4510  case MK_ExplicitModule:
4511  case MK_PrebuiltModule:
4512  return 1; // module
4513  case MK_MainFile:
4514  case MK_Preamble:
4515  return 2; // main source file
4516  }
4517  llvm_unreachable("unknown module kind");
4518 }
4519 
4521 ASTReader::ReadASTCore(StringRef FileName,
4522  ModuleKind Type,
4523  SourceLocation ImportLoc,
4524  ModuleFile *ImportedBy,
4526  off_t ExpectedSize, time_t ExpectedModTime,
4527  ASTFileSignature ExpectedSignature,
4528  unsigned ClientLoadCapabilities) {
4529  ModuleFile *M;
4530  std::string ErrorStr;
4532  = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4533  getGeneration(), ExpectedSize, ExpectedModTime,
4534  ExpectedSignature, readASTFileSignature,
4535  M, ErrorStr);
4536 
4537  switch (AddResult) {
4539  Diag(diag::remark_module_import)
4540  << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4541  << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4542  return Success;
4543 
4545  // Load module file below.
4546  break;
4547 
4549  // The module file was missing; if the client can handle that, return
4550  // it.
4551  if (ClientLoadCapabilities & ARR_Missing)
4552  return Missing;
4553 
4554  // Otherwise, return an error.
4555  Diag(diag::err_ast_file_not_found)
4556  << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4557  << ErrorStr;
4558  return Failure;
4559 
4561  // We couldn't load the module file because it is out-of-date. If the
4562  // client can handle out-of-date, return it.
4563  if (ClientLoadCapabilities & ARR_OutOfDate)
4564  return OutOfDate;
4565 
4566  // Otherwise, return an error.
4567  Diag(diag::err_ast_file_out_of_date)
4568  << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4569  << ErrorStr;
4570  return Failure;
4571  }
4572 
4573  assert(M && "Missing module file");
4574 
4575  bool ShouldFinalizePCM = false;
4576  auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4577  auto &MC = getModuleManager().getModuleCache();
4578  if (ShouldFinalizePCM)
4579  MC.finalizePCM(FileName);
4580  else
4581  MC.tryToDropPCM(FileName);
4582  });
4583  ModuleFile &F = *M;
4584  BitstreamCursor &Stream = F.Stream;
4585  Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4586  F.SizeInBits = F.Buffer->getBufferSize() * 8;
4587 
4588  // Sniff for the signature.
4589  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4590  Diag(diag::err_ast_file_invalid)
4591  << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4592  return Failure;
4593  }
4594 
4595  // This is used for compatibility with older PCH formats.
4596  bool HaveReadControlBlock = false;
4597  while (true) {
4598  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4599  if (!MaybeEntry) {
4600  Error(MaybeEntry.takeError());
4601  return Failure;
4602  }
4603  llvm::BitstreamEntry Entry = MaybeEntry.get();
4604 
4605  switch (Entry.Kind) {
4607  case llvm::BitstreamEntry::Record:
4608  case llvm::BitstreamEntry::EndBlock:
4609  Error("invalid record at top-level of AST file");
4610  return Failure;
4611 
4612  case llvm::BitstreamEntry::SubBlock:
4613  break;
4614  }
4615 
4616  switch (Entry.ID) {
4617  case CONTROL_BLOCK_ID:
4618  HaveReadControlBlock = true;
4619  switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4620  case Success:
4621  // Check that we didn't try to load a non-module AST file as a module.
4622  //
4623  // FIXME: Should we also perform the converse check? Loading a module as
4624  // a PCH file sort of works, but it's a bit wonky.
4625  if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4626  Type == MK_PrebuiltModule) &&
4627  F.ModuleName.empty()) {
4628  auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4629  if (Result != OutOfDate ||
4630  (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4631  Diag(diag::err_module_file_not_module) << FileName;
4632  return Result;
4633  }
4634  break;
4635 
4636  case Failure: return Failure;
4637  case Missing: return Missing;
4638  case OutOfDate: return OutOfDate;
4639  case VersionMismatch: return VersionMismatch;
4640  case ConfigurationMismatch: return ConfigurationMismatch;
4641  case HadErrors: return HadErrors;
4642  }
4643  break;
4644 
4645  case AST_BLOCK_ID:
4646  if (!HaveReadControlBlock) {
4647  if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4648  Diag(diag::err_pch_version_too_old);
4649  return VersionMismatch;
4650  }
4651 
4652  // Record that we've loaded this module.
4653  Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4654  ShouldFinalizePCM = true;
4655  return Success;
4656 
4658  // This block is handled using look-ahead during ReadControlBlock. We
4659  // shouldn't get here!
4660  Error("malformed block record in AST file");
4661  return Failure;
4662 
4663  default:
4664  if (llvm::Error Err = Stream.SkipBlock()) {
4665  Error(std::move(Err));
4666  return Failure;
4667  }
4668  break;
4669  }
4670  }
4671 
4672  llvm_unreachable("unexpected break; expected return");
4673 }
4674 
4676 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4677  unsigned ClientLoadCapabilities) {
4678  const HeaderSearchOptions &HSOpts =
4680  bool AllowCompatibleConfigurationMismatch =
4682  bool DisableValidation = shouldDisableValidationForFile(F);
4683 
4684  ASTReadResult Result = readUnhashedControlBlockImpl(
4685  &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4686  Listener.get(),
4687  WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4688 
4689  // If F was directly imported by another module, it's implicitly validated by
4690  // the importing module.
4691  if (DisableValidation || WasImportedBy ||
4692  (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4693  return Success;
4694 
4695  if (Result == Failure) {
4696  Error("malformed block record in AST file");
4697  return Failure;
4698  }
4699 
4700  if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4701  // If this module has already been finalized in the ModuleCache, we're stuck
4702  // with it; we can only load a single version of each module.
4703  //
4704  // This can happen when a module is imported in two contexts: in one, as a
4705  // user module; in another, as a system module (due to an import from
4706  // another module marked with the [system] flag). It usually indicates a
4707  // bug in the module map: this module should also be marked with [system].
4708  //
4709  // If -Wno-system-headers (the default), and the first import is as a
4710  // system module, then validation will fail during the as-user import,
4711  // since -Werror flags won't have been validated. However, it's reasonable
4712  // to treat this consistently as a system module.
4713  //
4714  // If -Wsystem-headers, the PCM on disk was built with
4715  // -Wno-system-headers, and the first import is as a user module, then
4716  // validation will fail during the as-system import since the PCM on disk
4717  // doesn't guarantee that -Werror was respected. However, the -Werror
4718  // flags were checked during the initial as-user import.
4719  if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4720  Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4721  return Success;
4722  }
4723  }
4724 
4725  return Result;
4726 }
4727 
4728 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4729  ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4730  bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4731  bool ValidateDiagnosticOptions) {
4732  // Initialize a stream.
4733  BitstreamCursor Stream(StreamData);
4734 
4735  // Sniff for the signature.
4736  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4737  // FIXME this drops the error on the floor.
4738  consumeError(std::move(Err));
4739  return Failure;
4740  }
4741 
4742  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4744  return Failure;
4745 
4746  // Read all of the records in the options block.
4747  RecordData Record;
4748  ASTReadResult Result = Success;
4749  while (true) {
4750  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4751  if (!MaybeEntry) {
4752  // FIXME this drops the error on the floor.
4753  consumeError(MaybeEntry.takeError());
4754  return Failure;
4755  }
4756  llvm::BitstreamEntry Entry = MaybeEntry.get();
4757 
4758  switch (Entry.Kind) {
4760  case llvm::BitstreamEntry::SubBlock:
4761  return Failure;
4762 
4763  case llvm::BitstreamEntry::EndBlock:
4764  return Result;
4765 
4766  case llvm::BitstreamEntry::Record:
4767  // The interesting case.
4768  break;
4769  }
4770 
4771  // Read and process a record.
4772  Record.clear();
4773  StringRef Blob;
4774  Expected<unsigned> MaybeRecordType =
4775  Stream.readRecord(Entry.ID, Record, &Blob);
4776  if (!MaybeRecordType) {
4777  // FIXME this drops the error.
4778  return Failure;
4779  }
4780  switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4781  case SIGNATURE:
4782  if (F)
4783  F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4784  break;
4785  case AST_BLOCK_HASH:
4786  if (F)
4787  F->ASTBlockHash =
4788  ASTFileSignature::create(Record.begin(), Record.end());
4789  break;
4790  case DIAGNOSTIC_OPTIONS: {
4791  bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4792  if (Listener && ValidateDiagnosticOptions &&
4793  !AllowCompatibleConfigurationMismatch &&
4794  ParseDiagnosticOptions(Record, Complain, *Listener))
4795  Result = OutOfDate; // Don't return early. Read the signature.
4796  break;
4797  }
4798  case DIAG_PRAGMA_MAPPINGS:
4799  if (!F)
4800  break;
4801  if (F->PragmaDiagMappings.empty())
4802  F->PragmaDiagMappings.swap(Record);
4803  else
4804  F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4805  Record.begin(), Record.end());
4806  break;
4808  if (!F)
4809  break;
4810  unsigned Count = Record[0];
4811  const char *Byte = Blob.data();
4812  F->SearchPathUsage = llvm::BitVector(Count, false);
4813  for (unsigned I = 0; I < Count; ++Byte)
4814  for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
4815  if (*Byte & (1 << Bit))
4816  F->SearchPathUsage[I] = true;
4817  break;
4818  }
4819  }
4820 }
4821 
4822 /// Parse a record and blob containing module file extension metadata.
4824  const SmallVectorImpl<uint64_t> &Record,
4825  StringRef Blob,
4826  ModuleFileExtensionMetadata &Metadata) {
4827  if (Record.size() < 4) return true;
4828 
4829  Metadata.MajorVersion = Record[0];
4830  Metadata.MinorVersion = Record[1];
4831 
4832  unsigned BlockNameLen = Record[2];
4833  unsigned UserInfoLen = Record[3];
4834 
4835  if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4836 
4837  Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4838  Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4839  Blob.data() + BlockNameLen + UserInfoLen);
4840  return false;
4841 }
4842 
4843 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
4844  BitstreamCursor &Stream = F.Stream;
4845 
4846  RecordData Record;
4847  while (true) {
4848  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4849  if (!MaybeEntry)
4850  return MaybeEntry.takeError();
4851  llvm::BitstreamEntry Entry = MaybeEntry.get();
4852 
4853  switch (Entry.Kind) {
4854  case llvm::BitstreamEntry::SubBlock:
4855  if (llvm::Error Err = Stream.SkipBlock())
4856  return Err;
4857  continue;
4858  case llvm::BitstreamEntry::EndBlock:
4859  return llvm::Error::success();
4861  return llvm::createStringError(std::errc::illegal_byte_sequence,
4862  "malformed block record in AST file");
4863  case llvm::BitstreamEntry::Record:
4864  break;
4865  }
4866 
4867  Record.clear();
4868  StringRef Blob;
4869  Expected<unsigned> MaybeRecCode =
4870  Stream.readRecord(Entry.ID, Record, &Blob);
4871  if (!MaybeRecCode)
4872  return MaybeRecCode.takeError();
4873  switch (MaybeRecCode.get()) {
4874  case EXTENSION_METADATA: {
4875  ModuleFileExtensionMetadata Metadata;
4876  if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4877  return llvm::createStringError(
4878  std::errc::illegal_byte_sequence,
4879  "malformed EXTENSION_METADATA in AST file");
4880 
4881  // Find a module file extension with this block name.
4882  auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4883  if (Known == ModuleFileExtensions.end()) break;
4884 
4885  // Form a reader.
4886  if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4887  F, Stream)) {
4888  F.ExtensionReaders.push_back(std::move(Reader));
4889  }
4890 
4891  break;
4892  }
4893  }
4894  }
4895 
4896  return llvm::Error::success();
4897 }
4898 
4900  assert(ContextObj && "no context to initialize");
4901  ASTContext &Context = *ContextObj;
4902 
4903  // If there's a listener, notify them that we "read" the translation unit.
4904  if (DeserializationListener)
4905  DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4906  Context.getTranslationUnitDecl());
4907 
4908  // FIXME: Find a better way to deal with collisions between these
4909  // built-in types. Right now, we just ignore the problem.
4910 
4911  // Load the special types.
4912  if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4913  if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4914  if (!Context.CFConstantStringTypeDecl)
4915  Context.setCFConstantStringType(GetType(String));
4916  }
4917 
4918  if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4919  QualType FileType = GetType(File);
4920  if (FileType.isNull()) {
4921  Error("FILE type is NULL");
4922  return;
4923  }
4924 
4925  if (!Context.FILEDecl) {
4926  if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4927  Context.setFILEDecl(Typedef->getDecl());
4928  else {
4929  const TagType *Tag = FileType->getAs<TagType>();
4930  if (!Tag) {
4931  Error("Invalid FILE type in AST file");
4932  return;
4933  }
4934  Context.setFILEDecl(Tag->getDecl());
4935  }
4936  }
4937  }
4938 
4939  if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4940  QualType Jmp_bufType = GetType(Jmp_buf);
4941  if (Jmp_bufType.isNull()) {
4942  Error("jmp_buf type is NULL");
4943  return;
4944  }
4945 
4946  if (!Context.jmp_bufDecl) {
4947  if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4948  Context.setjmp_bufDecl(Typedef->getDecl());
4949  else {
4950  const TagType *Tag = Jmp_bufType->getAs<TagType>();
4951  if (!Tag) {
4952  Error("Invalid jmp_buf type in AST file");
4953  return;
4954  }
4955  Context.setjmp_bufDecl(Tag->getDecl());
4956  }
4957  }
4958  }
4959 
4960  if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4961  QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4962  if (Sigjmp_bufType.isNull()) {
4963  Error("sigjmp_buf type is NULL");
4964  return;
4965  }
4966 
4967  if (!Context.sigjmp_bufDecl) {
4968  if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4969  Context.setsigjmp_bufDecl(Typedef->getDecl());
4970  else {
4971  const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4972  assert(Tag && "Invalid sigjmp_buf type in AST file");
4973  Context.setsigjmp_bufDecl(Tag->getDecl());
4974  }
4975  }
4976  }
4977 
4978  if (unsigned ObjCIdRedef
4979  = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4980  if (Context.ObjCIdRedefinitionType.isNull())
4981  Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4982  }
4983 
4984  if (unsigned ObjCClassRedef
4985  = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4986  if (Context.ObjCClassRedefinitionType.isNull())
4987  Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4988  }
4989 
4990  if (unsigned ObjCSelRedef
4991  = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4992  if (Context.ObjCSelRedefinitionType.isNull())
4993  Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4994  }
4995 
4996  if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4997  QualType Ucontext_tType = GetType(Ucontext_t);
4998  if (Ucontext_tType.isNull()) {
4999  Error("ucontext_t type is NULL");
5000  return;
5001  }
5002 
5003  if (!Context.ucontext_tDecl) {
5004  if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5005  Context.setucontext_tDecl(Typedef->getDecl());
5006  else {
5007  const TagType *Tag = Ucontext_tType->getAs<TagType>();
5008  assert(Tag && "Invalid ucontext_t type in AST file");
5009  Context.setucontext_tDecl(Tag->getDecl());
5010  }
5011  }
5012  }
5013  }
5014 
5015  ReadPragmaDiagnosticMappings(Context.getDiagnostics());
5016 
5017  // If there were any CUDA special declarations, deserialize them.
5018  if (!CUDASpecialDeclRefs.empty()) {
5019  assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5020  Context.setcudaConfigureCallDecl(
5021  cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5022  }
5023 
5024  // Re-export any modules that were imported by a non-module AST file.
5025  // FIXME: This does not make macro-only imports visible again.
5026  for (auto &Import : ImportedModules) {
5027  if (Module *Imported = getSubmodule(Import.ID)) {
5028  makeModuleVisible(Imported, Module::AllVisible,
5029  /*ImportLoc=*/Import.ImportLoc);
5030  if (Import.ImportLoc.isValid())
5031  PP.makeModuleVisible(Imported, Import.ImportLoc);
5032  // This updates visibility for Preprocessor only. For Sema, which can be
5033  // nullptr here, we do the same later, in UpdateSema().
5034  }
5035  }
5036 }
5037 
5039  // Nothing to do for now.
5040 }
5041 
5042 /// Reads and return the signature record from \p PCH's control block, or
5043 /// else returns 0.
5045  BitstreamCursor Stream(PCH);
5046  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5047  // FIXME this drops the error on the floor.
5048  consumeError(std::move(Err));
5049  return ASTFileSignature();
5050  }
5051 
5052  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5054  return ASTFileSignature();
5055 
5056  // Scan for SIGNATURE inside the diagnostic options block.
5057  ASTReader::RecordData Record;
5058  while (true) {
5059  Expected<llvm::BitstreamEntry> MaybeEntry =
5060  Stream.advanceSkippingSubblocks();
5061  if (!MaybeEntry) {
5062  // FIXME this drops the error on the floor.
5063  consumeError(MaybeEntry.takeError());
5064  return ASTFileSignature();
5065  }
5066  llvm::BitstreamEntry Entry = MaybeEntry.get();
5067 
5068  if (Entry.Kind != llvm::BitstreamEntry::Record)
5069  return ASTFileSignature();
5070 
5071  Record.clear();
5072  StringRef Blob;
5073  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5074  if (!MaybeRecord) {
5075  // FIXME this drops the error on the floor.
5076  consumeError(MaybeRecord.takeError());
5077  return ASTFileSignature();
5078  }
5079  if (SIGNATURE == MaybeRecord.get())
5080  return ASTFileSignature::create(Record.begin(),
5081  Record.begin() + ASTFileSignature::size);
5082  }
5083 }
5084 
5085 /// Retrieve the name of the original source file name
5086 /// directly from the AST file, without actually loading the AST
5087 /// file.
5089  const std::string &ASTFileName, FileManager &FileMgr,
5090  const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5091  // Open the AST file.
5092  auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5093  /*RequiresNullTerminator=*/false);
5094  if (!Buffer) {
5095  Diags.Report(diag::err_fe_unable_to_read_pch_file)
5096  << ASTFileName << Buffer.getError().message();
5097  return std::string();
5098  }
5099 
5100  // Initialize the stream
5101  BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5102 
5103  // Sniff for the signature.
5104  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5105  Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5106  return std::string();
5107  }
5108 
5109  // Scan for the CONTROL_BLOCK_ID block.
5110  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5111  Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5112  return std::string();
5113  }
5114 
5115  // Scan for ORIGINAL_FILE inside the control block.
5116  RecordData Record;
5117  while (true) {
5118  Expected<llvm::BitstreamEntry> MaybeEntry =
5119  Stream.advanceSkippingSubblocks();
5120  if (!MaybeEntry) {
5121  // FIXME this drops errors on the floor.
5122  consumeError(MaybeEntry.takeError());
5123  return std::string();
5124  }
5125  llvm::BitstreamEntry Entry = MaybeEntry.get();
5126 
5127  if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5128  return std::string();
5129 
5130  if (Entry.Kind != llvm::BitstreamEntry::Record) {
5131  Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5132  return std::string();
5133  }
5134 
5135  Record.clear();
5136  StringRef Blob;
5137  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5138  if (!MaybeRecord) {
5139  // FIXME this drops the errors on the floor.
5140  consumeError(MaybeRecord.takeError());
5141  return std::string();
5142  }
5143  if (ORIGINAL_FILE == MaybeRecord.get())
5144  return Blob.str();
5145  }
5146 }
5147 
5148 namespace {
5149 
5150  class SimplePCHValidator : public ASTReaderListener {
5151  const LangOptions &ExistingLangOpts;
5152  const TargetOptions &ExistingTargetOpts;
5153  const PreprocessorOptions &ExistingPPOpts;
5154  std::string ExistingModuleCachePath;
5155  FileManager &FileMgr;
5156  bool StrictOptionMatches;
5157 
5158  public:
5159  SimplePCHValidator(const LangOptions &ExistingLangOpts,
5160  const TargetOptions &ExistingTargetOpts,
5161  const PreprocessorOptions &ExistingPPOpts,
5162  StringRef ExistingModuleCachePath, FileManager &FileMgr,
5163  bool StrictOptionMatches)
5164  : ExistingLangOpts(ExistingLangOpts),
5165  ExistingTargetOpts(ExistingTargetOpts),
5166  ExistingPPOpts(ExistingPPOpts),
5167  ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),
5168  StrictOptionMatches(StrictOptionMatches) {}
5169 
5170  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5171  bool AllowCompatibleDifferences) override {
5172  return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5173  AllowCompatibleDifferences);
5174  }
5175 
5176  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5177  bool AllowCompatibleDifferences) override {
5178  return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5179  AllowCompatibleDifferences);
5180  }
5181 
5182  bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5183  StringRef SpecificModuleCachePath,
5184  bool Complain) override {
5185  return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5186  ExistingModuleCachePath, nullptr,
5187  ExistingLangOpts, ExistingPPOpts);
5188  }
5189 
5190  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5191  bool Complain,
5192  std::string &SuggestedPredefines) override {
5193  return checkPreprocessorOptions(
5194  PPOpts, ExistingPPOpts, /*Diags=*/nullptr, FileMgr,
5195  SuggestedPredefines, ExistingLangOpts,
5196  StrictOptionMatches ? OptionValidateStrictMatches
5198  }
5199  };
5200 
5201 } // namespace
5202 
5204  StringRef Filename, FileManager &FileMgr,
5205  const InMemoryModuleCache &ModuleCache,
5206  const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5207  ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5208  // Open the AST file.
5209  std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5210  llvm::MemoryBuffer *Buffer = ModuleCache.lookupPCM(Filename);
5211  if (!Buffer) {
5212  // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5213  // read again later, but we do not have the context here to determine if it
5214  // is safe to change the result of InMemoryModuleCache::getPCMState().
5215 
5216  // FIXME: This allows use of the VFS; we do not allow use of the
5217  // VFS when actually loading a module.
5218  auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5219  if (!BufferOrErr)
5220  return true;
5221  OwnedBuffer = std::move(*BufferOrErr);
5222  Buffer = OwnedBuffer.get();
5223  }
5224 
5225  // Initialize the stream
5226  StringRef Bytes = PCHContainerRdr.ExtractPCH(*Buffer);
5227  BitstreamCursor Stream(Bytes);
5228 
5229  // Sniff for the signature.
5230  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5231  consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5232  return true;
5233  }
5234 
5235  // Scan for the CONTROL_BLOCK_ID block.
5236  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5237  return true;
5238 
5239  bool NeedsInputFiles = Listener.needsInputFileVisitation();
5240  bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5241  bool NeedsImports = Listener.needsImportVisitation();
5242  BitstreamCursor InputFilesCursor;
5243 
5244  RecordData Record;
5245  std::string ModuleDir;
5246  bool DoneWithControlBlock = false;
5247  while (!DoneWithControlBlock) {
5248  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5249  if (!MaybeEntry) {
5250  // FIXME this drops the error on the floor.
5251  consumeError(MaybeEntry.takeError());
5252  return true;
5253  }
5254  llvm::BitstreamEntry Entry = MaybeEntry.get();
5255 
5256  switch (Entry.Kind) {
5257  case llvm::BitstreamEntry::SubBlock: {
5258  switch (Entry.ID) {
5259  case OPTIONS_BLOCK_ID: {
5260  std::string IgnoredSuggestedPredefines;
5261  if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5262  /*AllowCompatibleConfigurationMismatch*/ false,
5263  Listener, IgnoredSuggestedPredefines) != Success)
5264  return true;
5265  break;
5266  }
5267 
5268  case INPUT_FILES_BLOCK_ID:
5269  InputFilesCursor = Stream;
5270  if (llvm::Error Err = Stream.SkipBlock()) {
5271  // FIXME this drops the error on the floor.
5272  consumeError(std::move(Err));
5273  return true;
5274  }
5275  if (NeedsInputFiles &&
5276  ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5277  return true;
5278  break;
5279 
5280  default:
5281  if (llvm::Error Err = Stream.SkipBlock()) {
5282  // FIXME this drops the error on the floor.
5283  consumeError(std::move(Err));
5284  return true;
5285  }
5286  break;
5287  }
5288 
5289  continue;
5290  }
5291 
5292  case llvm::BitstreamEntry::EndBlock:
5293  DoneWithControlBlock = true;
5294  break;
5295 
5297  return true;
5298 
5299  case llvm::BitstreamEntry::Record:
5300  break;
5301  }
5302 
5303  if (DoneWithControlBlock) break;
5304 
5305  Record.clear();
5306  StringRef Blob;
5307  Expected<unsigned> MaybeRecCode =
5308  Stream.readRecord(Entry.ID, Record, &Blob);
5309  if (!MaybeRecCode) {
5310  // FIXME this drops the error.
5311  return Failure;
5312  }
5313  switch ((ControlRecordTypes)MaybeRecCode.get()) {
5314  case METADATA:
5315  if (Record[0] != VERSION_MAJOR)
5316  return true;
5317  if (Listener.ReadFullVersionInformation(Blob))
5318  return true;
5319  break;
5320  case MODULE_NAME:
5321  Listener.ReadModuleName(Blob);
5322  break;
5323  case MODULE_DIRECTORY:
5324  ModuleDir = std::string(Blob);
5325  break;
5326  case MODULE_MAP_FILE: {
5327  unsigned Idx = 0;
5328  auto Path = ReadString(Record, Idx);
5329  ResolveImportedPath(Path, ModuleDir);
5330  Listener.ReadModuleMapFile(Path);
5331  break;
5332  }
5333  case INPUT_FILE_OFFSETS: {
5334  if (!NeedsInputFiles)
5335  break;
5336 
5337  unsigned NumInputFiles = Record[0];
5338  unsigned NumUserFiles = Record[1];
5339  const llvm::support::unaligned_uint64_t *InputFileOffs =
5340  (const llvm::support::unaligned_uint64_t *)Blob.data();
5341  for (unsigned I = 0; I != NumInputFiles; ++I) {
5342  // Go find this input file.
5343  bool isSystemFile = I >= NumUserFiles;
5344 
5345  if (isSystemFile && !NeedsSystemInputFiles)
5346  break; // the rest are system input files
5347 
5348  BitstreamCursor &Cursor = InputFilesCursor;
5349  SavedStreamPosition SavedPosition(Cursor);
5350  if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5351  // FIXME this drops errors on the floor.
5352  consumeError(std::move(Err));
5353  }
5354 
5355  Expected<unsigned> MaybeCode = Cursor.ReadCode();
5356  if (!MaybeCode) {
5357  // FIXME this drops errors on the floor.
5358  consumeError(MaybeCode.takeError());
5359  }
5360  unsigned Code = MaybeCode.get();
5361 
5362  RecordData Record;
5363  StringRef Blob;
5364  bool shouldContinue = false;
5365  Expected<unsigned> MaybeRecordType =
5366  Cursor.readRecord(Code, Record, &Blob);
5367  if (!MaybeRecordType) {
5368  // FIXME this drops errors on the floor.
5369  consumeError(MaybeRecordType.takeError());
5370  }
5371  switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5372  case INPUT_FILE_HASH:
5373  break;
5374  case INPUT_FILE:
5375  bool Overridden = static_cast<bool>(Record[3]);
5377  ResolveImportedPath(Filename, ModuleDir);
5378  shouldContinue = Listener.visitInputFile(
5379  Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5380  break;
5381  }
5382  if (!shouldContinue)
5383  break;
5384  }
5385  break;
5386  }
5387 
5388  case IMPORTS: {
5389  if (!NeedsImports)
5390  break;
5391 
5392  unsigned Idx = 0, N = Record.size();
5393  while (Idx < N) {
5394  // Read information about the AST file.
5395  Idx +=
5396  1 + 1 + 1 + 1 +
5397  ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5398  std::string ModuleName = ReadString(Record, Idx);
5399  std::string Filename = ReadString(Record, Idx);
5400  ResolveImportedPath(Filename, ModuleDir);
5401  Listener.visitImport(ModuleName, Filename);
5402  }
5403  break;
5404  }
5405 
5406  default:
5407  // No other validation to perform.
5408  break;
5409  }
5410  }
5411 
5412  // Look for module file extension blocks, if requested.
5413  if (FindModuleFileExtensions) {
5414  BitstreamCursor SavedStream = Stream;
5415  while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5416  bool DoneWithExtensionBlock = false;
5417  while (!DoneWithExtensionBlock) {
5418  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5419  if (!MaybeEntry) {
5420  // FIXME this drops the error.
5421  return true;
5422  }
5423  llvm::BitstreamEntry Entry = MaybeEntry.get();
5424 
5425  switch (Entry.Kind) {
5426  case llvm::BitstreamEntry::SubBlock:
5427  if (llvm::Error Err = Stream.SkipBlock()) {
5428  // FIXME this drops the error on the floor.
5429  consumeError(std::move(Err));
5430  return true;
5431  }
5432  continue;
5433 
5434  case llvm::BitstreamEntry::EndBlock:
5435  DoneWithExtensionBlock = true;
5436  continue;
5437 
5439  return true;
5440 
5441  case llvm::BitstreamEntry::Record:
5442  break;
5443  }
5444 
5445  Record.clear();
5446  StringRef Blob;
5447  Expected<unsigned> MaybeRecCode =
5448  Stream.readRecord(Entry.ID, Record, &Blob);
5449  if (!MaybeRecCode) {
5450  // FIXME this drops the error.
5451  return true;
5452  }
5453  switch (MaybeRecCode.get()) {
5454  case EXTENSION_METADATA: {
5455  ModuleFileExtensionMetadata Metadata;
5456  if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5457  return true;
5458 
5459  Listener.readModuleFileExtension(Metadata);
5460  break;
5461  }
5462  }
5463  }
5464  }
5465  Stream = SavedStream;
5466  }
5467 
5468  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5469  if (readUnhashedControlBlockImpl(
5470  nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5471  /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5472  ValidateDiagnosticOptions) != Success)
5473  return true;
5474 
5475  return false;
5476 }
5477 
5479  const InMemoryModuleCache &ModuleCache,
5480  const PCHContainerReader &PCHContainerRdr,
5481  const LangOptions &LangOpts,
5482  const TargetOptions &TargetOpts,
5483  const PreprocessorOptions &PPOpts,
5484  StringRef ExistingModuleCachePath,
5485  bool RequireStrictOptionMatches) {
5486  SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5487  ExistingModuleCachePath, FileMgr,
5488  RequireStrictOptionMatches);
5489  return !readASTFileControlBlock(Filename, FileMgr, ModuleCache,
5490  PCHContainerRdr,
5491  /*FindModuleFileExtensions=*/false, validator,
5492  /*ValidateDiagnosticOptions=*/true);
5493 }
5494 
5495 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5496  unsigned ClientLoadCapabilities) {
5497  // Enter the submodule block.
5498  if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5499  return Err;
5500 
5501  ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5502  bool First = true;
5503  Module *CurrentModule = nullptr;
5504  RecordData Record;
5505  while (true) {
5506  Expected<llvm::BitstreamEntry> MaybeEntry =
5507  F.Stream.advanceSkippingSubblocks();
5508  if (!MaybeEntry)
5509  return MaybeEntry.takeError();
5510  llvm::BitstreamEntry Entry = MaybeEntry.get();
5511 
5512  switch (Entry.Kind) {
5513  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5515  return llvm::createStringError(std::errc::illegal_byte_sequence,
5516  "malformed block record in AST file");
5517  case llvm::BitstreamEntry::EndBlock:
5518  return llvm::Error::success();
5519  case llvm::BitstreamEntry::Record:
5520  // The interesting case.
5521  break;
5522  }
5523 
5524  // Read a record.
5525  StringRef Blob;
5526  Record.clear();
5527  Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5528  if (!MaybeKind)
5529  return MaybeKind.takeError();
5530  unsigned Kind = MaybeKind.get();
5531 
5532  if ((Kind == SUBMODULE_METADATA) != First)
5533  return llvm::createStringError(
5534  std::errc::illegal_byte_sequence,
5535  "submodule metadata record should be at beginning of block");
5536  First = false;
5537 
5538  // Submodule information is only valid if we have a current module.
5539  // FIXME: Should we error on these cases?
5540  if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5542  continue;
5543 
5544  switch (Kind) {
5545  default: // Default behavior: ignore.
5546  break;
5547 
5548  case SUBMODULE_DEFINITION: {
5549  if (Record.size() < 12)
5550  return llvm::createStringError(std::errc::illegal_byte_sequence,
5551  "malformed module definition");
5552 
5553  StringRef Name = Blob;
5554  unsigned Idx = 0;
5555  SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5556  SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5557  Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5558  bool IsFramework = Record[Idx++];
5559  bool IsExplicit = Record[Idx++];
5560  bool IsSystem = Record[Idx++];
5561  bool IsExternC = Record[Idx++];
5562  bool InferSubmodules = Record[Idx++];
5563  bool InferExplicitSubmodules = Record[Idx++];
5564  bool InferExportWildcard = Record[Idx++];
5565  bool ConfigMacrosExhaustive = Record[Idx++];
5566  bool ModuleMapIsPrivate = Record[Idx++];
5567 
5568  Module *ParentModule = nullptr;
5569  if (Parent)
5570  ParentModule = getSubmodule(Parent);
5571 
5572  // Retrieve this (sub)module from the module map, creating it if
5573  // necessary.
5574  CurrentModule =
5575  ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5576  .first;
5577 
5578  // FIXME: set the definition loc for CurrentModule, or call
5579  // ModMap.setInferredModuleAllowedBy()
5580 
5581  SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5582  if (GlobalIndex >= SubmodulesLoaded.size() ||
5583  SubmodulesLoaded[GlobalIndex])
5584  return llvm::createStringError(std::errc::invalid_argument,
5585  "too many submodules");
5586 
5587  if (!ParentModule) {
5588  if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5589  // Don't emit module relocation error if we have -fno-validate-pch
5592  CurFile != F.File) {
5593  auto ConflictError =
5594  PartialDiagnostic(diag::err_module_file_conflict,
5595  ContextObj->DiagAllocator)
5596  << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5597  << F.File->getName();
5598  return DiagnosticError::create(CurrentImportLoc, ConflictError);
5599  }
5600  }
5601 
5602  F.DidReadTopLevelSubmodule = true;
5603  CurrentModule->setASTFile(F.File);
5604  CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5605  }
5606 
5607  CurrentModule->Kind = Kind;
5608  CurrentModule->Signature = F.Signature;
5609  CurrentModule->IsFromModuleFile = true;
5610  CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5611  CurrentModule->IsExternC = IsExternC;
5612  CurrentModule->InferSubmodules = InferSubmodules;
5613  CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5614  CurrentModule->InferExportWildcard = InferExportWildcard;
5615  CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5616  CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5617  if (DeserializationListener)
5618  DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5619 
5620  SubmodulesLoaded[GlobalIndex] = CurrentModule;
5621 
5622  // Clear out data that will be replaced by what is in the module file.
5623  CurrentModule->LinkLibraries.clear();
5624  CurrentModule->ConfigMacros.clear();
5625  CurrentModule->UnresolvedConflicts.clear();
5626  CurrentModule->Conflicts.clear();
5627 
5628  // The module is available unless it's missing a requirement; relevant
5629  // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5630  // Missing headers that were present when the module was built do not
5631  // make it unavailable -- if we got this far, this must be an explicitly
5632  // imported module file.
5633  CurrentModule->Requirements.clear();
5634  CurrentModule->MissingHeaders.clear();
5635  CurrentModule->IsUnimportable =
5636  ParentModule && ParentModule->IsUnimportable;
5637  CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5638  break;
5639  }
5640 
5642  // FIXME: This doesn't work for framework modules as `Filename` is the
5643  // name as written in the module file and does not include
5644  // `Headers/`, so this path will never exist.
5646  ResolveImportedPath(F, Filename);
5647  if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5648  if (!CurrentModule->getUmbrellaHeader()) {
5649  // FIXME: NameAsWritten
5650  ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5651  }
5652  // Note that it's too late at this point to return out of date if the
5653  // name from the PCM doesn't match up with the one in the module map,
5654  // but also quite unlikely since we will have already checked the
5655  // modification time and size of the module map file itself.
5656  }
5657  break;
5658  }
5659 
5660  case SUBMODULE_HEADER:
5663  // We lazily associate headers with their modules via the HeaderInfo table.
5664  // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5665  // of complete filenames or remove it entirely.
5666  break;
5667 
5670  // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5671  // them here.
5672  break;
5673 
5674  case SUBMODULE_TOPHEADER: {
5675  std::string HeaderName(Blob);
5676  ResolveImportedPath(F, HeaderName);
5677  CurrentModule->addTopHeaderFilename(HeaderName);
5678  break;
5679  }
5680 
5681  case SUBMODULE_UMBRELLA_DIR: {
5682  // See comments in SUBMODULE_UMBRELLA_HEADER
5683  std::string Dirname = std::string(Blob);
5684  ResolveImportedPath(F, Dirname);
5685  if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5686  if (!CurrentModule->getUmbrellaDir()) {
5687  // FIXME: NameAsWritten
5688  ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5689  }
5690  }
5691  break;
5692  }
5693 
5694  case SUBMODULE_METADATA: {
5695  F.BaseSubmoduleID = getTotalNumSubmodules();
5696  F.LocalNumSubmodules = Record[0];
5697  unsigned LocalBaseSubmoduleID = Record[1];
5698  if (F.LocalNumSubmodules > 0) {
5699  // Introduce the global -> local mapping for submodules within this
5700  // module.
5701  GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5702 
5703  // Introduce the local -> global mapping for submodules within this
5704  // module.
5706  std::make_pair(LocalBaseSubmoduleID,
5707  F.BaseSubmoduleID - LocalBaseSubmoduleID));
5708 
5709  SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5710  }
5711  break;
5712  }
5713 
5714  case SUBMODULE_IMPORTS:
5715  for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5716  UnresolvedModuleRef Unresolved;
5717  Unresolved.File = &F;
5718  Unresolved.Mod = CurrentModule;
5719  Unresolved.ID = Record[Idx];
5720  Unresolved.Kind = UnresolvedModuleRef::Import;
5721  Unresolved.IsWildcard = false;
5722  UnresolvedModuleRefs.push_back(Unresolved);
5723  }
5724  break;
5725 
5727  for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5728  UnresolvedModuleRef Unresolved;
5729  Unresolved.File = &F;
5730  Unresolved.Mod = CurrentModule;
5731  Unresolved.ID = Record[Idx];
5732  Unresolved.Kind = UnresolvedModuleRef::Affecting;
5733  Unresolved.IsWildcard = false;
5734  UnresolvedModuleRefs.push_back(Unresolved);
5735  }
5736  break;
5737 
5738  case SUBMODULE_EXPORTS:
5739  for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5740  UnresolvedModuleRef Unresolved;
5741  Unresolved.File = &F;
5742  Unresolved.Mod = CurrentModule;
5743  Unresolved.ID = Record[Idx];
5744  Unresolved.Kind = UnresolvedModuleRef::Export;
5745  Unresolved.IsWildcard = Record[Idx + 1];
5746  UnresolvedModuleRefs.push_back(Unresolved);
5747  }
5748 
5749  // Once we've loaded the set of exports, there's no reason to keep
5750  // the parsed, unresolved exports around.
5751  CurrentModule->UnresolvedExports.clear();
5752  break;
5753 
5754  case SUBMODULE_REQUIRES:
5755  CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5756  PP.getTargetInfo());
5757  break;
5758 
5760  ModMap.resolveLinkAsDependencies(CurrentModule);
5761  CurrentModule->LinkLibraries.push_back(
5762  Module::LinkLibrary(std::string(Blob), Record[0]));
5763  break;
5764 
5766  CurrentModule->ConfigMacros.push_back(Blob.str());
5767  break;
5768 
5769  case SUBMODULE_CONFLICT: {
5770  UnresolvedModuleRef Unresolved;
5771  Unresolved.File = &F;
5772  Unresolved.Mod = CurrentModule;
5773  Unresolved.ID = Record[0];
5774  Unresolved.Kind = UnresolvedModuleRef::Conflict;
5775  Unresolved.IsWildcard = false;
5776  Unresolved.String = Blob;
5777  UnresolvedModuleRefs.push_back(Unresolved);
5778  break;
5779  }
5780 
5781  case