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