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