clang  9.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/Bitcode/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  Cursor.JumpToBit(Offset);
1152 
1153  RecordData Record;
1154  StringRef Blob;
1155  unsigned Code = Cursor.ReadCode();
1156  unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1157  if (RecCode != DECL_CONTEXT_LEXICAL) {
1158  Error("Expected lexical block");
1159  return true;
1160  }
1161 
1162  assert(!isa<TranslationUnitDecl>(DC) &&
1163  "expected a TU_UPDATE_LEXICAL record for TU");
1164  // If we are handling a C++ class template instantiation, we can see multiple
1165  // lexical updates for the same record. It's important that we select only one
1166  // of them, so that field numbering works properly. Just pick the first one we
1167  // see.
1168  auto &Lex = LexicalDecls[DC];
1169  if (!Lex.first) {
1170  Lex = std::make_pair(
1171  &M, llvm::makeArrayRef(
1172  reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1173  Blob.data()),
1174  Blob.size() / 4));
1175  }
1176  DC->setHasExternalLexicalStorage(true);
1177  return false;
1178 }
1179 
1180 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1181  BitstreamCursor &Cursor,
1182  uint64_t Offset,
1183  DeclID ID) {
1184  assert(Offset != 0);
1185 
1186  SavedStreamPosition SavedPosition(Cursor);
1187  Cursor.JumpToBit(Offset);
1188 
1189  RecordData Record;
1190  StringRef Blob;
1191  unsigned Code = Cursor.ReadCode();
1192  unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1193  if (RecCode != DECL_CONTEXT_VISIBLE) {
1194  Error("Expected visible lookup table block");
1195  return true;
1196  }
1197 
1198  // We can't safely determine the primary context yet, so delay attaching the
1199  // lookup table until we're done with recursive deserialization.
1200  auto *Data = (const unsigned char*)Blob.data();
1201  PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1202  return false;
1203 }
1204 
1205 void ASTReader::Error(StringRef Msg) const {
1206  Error(diag::err_fe_pch_malformed, Msg);
1207  if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1208  !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1209  Diag(diag::note_module_cache_path)
1210  << PP.getHeaderSearchInfo().getModuleCachePath();
1211  }
1212 }
1213 
1214 void ASTReader::Error(unsigned DiagID,
1215  StringRef Arg1, StringRef Arg2) const {
1216  if (Diags.isDiagnosticInFlight())
1217  Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1218  else
1219  Diag(DiagID) << Arg1 << Arg2;
1220 }
1221 
1222 //===----------------------------------------------------------------------===//
1223 // Source Manager Deserialization
1224 //===----------------------------------------------------------------------===//
1225 
1226 /// Read the line table in the source manager block.
1227 /// \returns true if there was an error.
1228 bool ASTReader::ParseLineTable(ModuleFile &F,
1229  const RecordData &Record) {
1230  unsigned Idx = 0;
1231  LineTableInfo &LineTable = SourceMgr.getLineTable();
1232 
1233  // Parse the file names
1234  std::map<int, int> FileIDs;
1235  FileIDs[-1] = -1; // For unspecified filenames.
1236  for (unsigned I = 0; Record[Idx]; ++I) {
1237  // Extract the file name
1238  auto Filename = ReadPath(F, Record, Idx);
1239  FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1240  }
1241  ++Idx;
1242 
1243  // Parse the line entries
1244  std::vector<LineEntry> Entries;
1245  while (Idx < Record.size()) {
1246  int FID = Record[Idx++];
1247  assert(FID >= 0 && "Serialized line entries for non-local file.");
1248  // Remap FileID from 1-based old view.
1249  FID += F.SLocEntryBaseID - 1;
1250 
1251  // Extract the line entries
1252  unsigned NumEntries = Record[Idx++];
1253  assert(NumEntries && "no line entries for file ID");
1254  Entries.clear();
1255  Entries.reserve(NumEntries);
1256  for (unsigned I = 0; I != NumEntries; ++I) {
1257  unsigned FileOffset = Record[Idx++];
1258  unsigned LineNo = Record[Idx++];
1259  int FilenameID = FileIDs[Record[Idx++]];
1261  = (SrcMgr::CharacteristicKind)Record[Idx++];
1262  unsigned IncludeOffset = Record[Idx++];
1263  Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1264  FileKind, IncludeOffset));
1265  }
1266  LineTable.AddEntry(FileID::get(FID), Entries);
1267  }
1268 
1269  return false;
1270 }
1271 
1272 /// Read a source manager block
1273 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1274  using namespace SrcMgr;
1275 
1276  BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1277 
1278  // Set the source-location entry cursor to the current position in
1279  // the stream. This cursor will be used to read the contents of the
1280  // source manager block initially, and then lazily read
1281  // source-location entries as needed.
1282  SLocEntryCursor = F.Stream;
1283 
1284  // The stream itself is going to skip over the source manager block.
1285  if (F.Stream.SkipBlock()) {
1286  Error("malformed block record in AST file");
1287  return true;
1288  }
1289 
1290  // Enter the source manager block.
1291  if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1292  Error("malformed source manager block record in AST file");
1293  return true;
1294  }
1295 
1296  RecordData Record;
1297  while (true) {
1298  llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1299 
1300  switch (E.Kind) {
1301  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1303  Error("malformed block record in AST file");
1304  return true;
1305  case llvm::BitstreamEntry::EndBlock:
1306  return false;
1307  case llvm::BitstreamEntry::Record:
1308  // The interesting case.
1309  break;
1310  }
1311 
1312  // Read a record.
1313  Record.clear();
1314  StringRef Blob;
1315  switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1316  default: // Default behavior: ignore.
1317  break;
1318 
1319  case SM_SLOC_FILE_ENTRY:
1320  case SM_SLOC_BUFFER_ENTRY:
1322  // Once we hit one of the source location entries, we're done.
1323  return false;
1324  }
1325  }
1326 }
1327 
1328 /// If a header file is not found at the path that we expect it to be
1329 /// and the PCH file was moved from its original location, try to resolve the
1330 /// file by assuming that header+PCH were moved together and the header is in
1331 /// the same place relative to the PCH.
1332 static std::string
1334  const std::string &OriginalDir,
1335  const std::string &CurrDir) {
1336  assert(OriginalDir != CurrDir &&
1337  "No point trying to resolve the file if the PCH dir didn't change");
1338 
1339  using namespace llvm::sys;
1340 
1341  SmallString<128> filePath(Filename);
1342  fs::make_absolute(filePath);
1343  assert(path::is_absolute(OriginalDir));
1344  SmallString<128> currPCHPath(CurrDir);
1345 
1346  path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1347  fileDirE = path::end(path::parent_path(filePath));
1348  path::const_iterator origDirI = path::begin(OriginalDir),
1349  origDirE = path::end(OriginalDir);
1350  // Skip the common path components from filePath and OriginalDir.
1351  while (fileDirI != fileDirE && origDirI != origDirE &&
1352  *fileDirI == *origDirI) {
1353  ++fileDirI;
1354  ++origDirI;
1355  }
1356  for (; origDirI != origDirE; ++origDirI)
1357  path::append(currPCHPath, "..");
1358  path::append(currPCHPath, fileDirI, fileDirE);
1359  path::append(currPCHPath, path::filename(Filename));
1360  return currPCHPath.str();
1361 }
1362 
1364  if (ID == 0)
1365  return false;
1366 
1367  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1368  Error("source location entry ID out-of-range for AST file");
1369  return true;
1370  }
1371 
1372  // Local helper to read the (possibly-compressed) buffer data following the
1373  // entry record.
1374  auto ReadBuffer = [this](
1375  BitstreamCursor &SLocEntryCursor,
1376  StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1377  RecordData Record;
1378  StringRef Blob;
1379  unsigned Code = SLocEntryCursor.ReadCode();
1380  unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1381 
1382  if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1383  if (!llvm::zlib::isAvailable()) {
1384  Error("zlib is not available");
1385  return nullptr;
1386  }
1387  SmallString<0> Uncompressed;
1388  if (llvm::Error E =
1389  llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1390  Error("could not decompress embedded file contents: " +
1391  llvm::toString(std::move(E)));
1392  return nullptr;
1393  }
1394  return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1395  } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1396  return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1397  } else {
1398  Error("AST record has invalid code");
1399  return nullptr;
1400  }
1401  };
1402 
1403  ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1404  F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1405  BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1406  unsigned BaseOffset = F->SLocEntryBaseOffset;
1407 
1408  ++NumSLocEntriesRead;
1409  llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1410  if (Entry.Kind != llvm::BitstreamEntry::Record) {
1411  Error("incorrectly-formatted source location entry in AST file");
1412  return true;
1413  }
1414 
1415  RecordData Record;
1416  StringRef Blob;
1417  switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1418  default:
1419  Error("incorrectly-formatted source location entry in AST file");
1420  return true;
1421 
1422  case SM_SLOC_FILE_ENTRY: {
1423  // We will detect whether a file changed and return 'Failure' for it, but
1424  // we will also try to fail gracefully by setting up the SLocEntry.
1425  unsigned InputID = Record[4];
1426  InputFile IF = getInputFile(*F, InputID);
1427  const FileEntry *File = IF.getFile();
1428  bool OverriddenBuffer = IF.isOverridden();
1429 
1430  // Note that we only check if a File was returned. If it was out-of-date
1431  // we have complained but we will continue creating a FileID to recover
1432  // gracefully.
1433  if (!File)
1434  return true;
1435 
1436  SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1437  if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1438  // This is the module's main file.
1439  IncludeLoc = getImportLocation(F);
1440  }
1442  FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1443  FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1444  ID, BaseOffset + Record[0]);
1445  SrcMgr::FileInfo &FileInfo =
1446  const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1447  FileInfo.NumCreatedFIDs = Record[5];
1448  if (Record[3])
1449  FileInfo.setHasLineDirectives();
1450 
1451  const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1452  unsigned NumFileDecls = Record[7];
1453  if (NumFileDecls && ContextObj) {
1454  assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1455  FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1456  NumFileDecls));
1457  }
1458 
1459  const SrcMgr::ContentCache *ContentCache
1460  = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1461  if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1462  ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1463  !ContentCache->getRawBuffer()) {
1464  auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1465  if (!Buffer)
1466  return true;
1467  SourceMgr.overrideFileContents(File, std::move(Buffer));
1468  }
1469 
1470  break;
1471  }
1472 
1473  case SM_SLOC_BUFFER_ENTRY: {
1474  const char *Name = Blob.data();
1475  unsigned Offset = Record[0];
1477  FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1478  SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1479  if (IncludeLoc.isInvalid() && F->isModule()) {
1480  IncludeLoc = getImportLocation(F);
1481  }
1482 
1483  auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1484  if (!Buffer)
1485  return true;
1486  SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1487  BaseOffset + Offset, IncludeLoc);
1488  break;
1489  }
1490 
1491  case SM_SLOC_EXPANSION_ENTRY: {
1492  SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1493  SourceMgr.createExpansionLoc(SpellingLoc,
1494  ReadSourceLocation(*F, Record[2]),
1495  ReadSourceLocation(*F, Record[3]),
1496  Record[5],
1497  Record[4],
1498  ID,
1499  BaseOffset + Record[0]);
1500  break;
1501  }
1502  }
1503 
1504  return false;
1505 }
1506 
1507 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1508  if (ID == 0)
1509  return std::make_pair(SourceLocation(), "");
1510 
1511  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1512  Error("source location entry ID out-of-range for AST file");
1513  return std::make_pair(SourceLocation(), "");
1514  }
1515 
1516  // Find which module file this entry lands in.
1517  ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1518  if (!M->isModule())
1519  return std::make_pair(SourceLocation(), "");
1520 
1521  // FIXME: Can we map this down to a particular submodule? That would be
1522  // ideal.
1523  return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1524 }
1525 
1526 /// Find the location where the module F is imported.
1527 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1528  if (F->ImportLoc.isValid())
1529  return F->ImportLoc;
1530 
1531  // Otherwise we have a PCH. It's considered to be "imported" at the first
1532  // location of its includer.
1533  if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1534  // Main file is the importer.
1535  assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1536  return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1537  }
1538  return F->ImportedBy[0]->FirstLoc;
1539 }
1540 
1541 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1542 /// specified cursor. Read the abbreviations that are at the top of the block
1543 /// and then leave the cursor pointing into the block.
1544 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1545  if (Cursor.EnterSubBlock(BlockID))
1546  return true;
1547 
1548  while (true) {
1549  uint64_t Offset = Cursor.GetCurrentBitNo();
1550  unsigned Code = Cursor.ReadCode();
1551 
1552  // We expect all abbrevs to be at the start of the block.
1553  if (Code != llvm::bitc::DEFINE_ABBREV) {
1554  Cursor.JumpToBit(Offset);
1555  return false;
1556  }
1557  Cursor.ReadAbbrevRecord();
1558  }
1559 }
1560 
1562  unsigned &Idx) {
1563  Token Tok;
1564  Tok.startToken();
1565  Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1566  Tok.setLength(Record[Idx++]);
1567  if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1568  Tok.setIdentifierInfo(II);
1569  Tok.setKind((tok::TokenKind)Record[Idx++]);
1570  Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1571  return Tok;
1572 }
1573 
1575  BitstreamCursor &Stream = F.MacroCursor;
1576 
1577  // Keep track of where we are in the stream, then jump back there
1578  // after reading this macro.
1579  SavedStreamPosition SavedPosition(Stream);
1580 
1581  Stream.JumpToBit(Offset);
1582  RecordData Record;
1584  MacroInfo *Macro = nullptr;
1585 
1586  while (true) {
1587  // Advance to the next record, but if we get to the end of the block, don't
1588  // pop it (removing all the abbreviations from the cursor) since we want to
1589  // be able to reseek within the block and read entries.
1590  unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1591  llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1592 
1593  switch (Entry.Kind) {
1594  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1596  Error("malformed block record in AST file");
1597  return Macro;
1598  case llvm::BitstreamEntry::EndBlock:
1599  return Macro;
1600  case llvm::BitstreamEntry::Record:
1601  // The interesting case.
1602  break;
1603  }
1604 
1605  // Read a record.
1606  Record.clear();
1607  PreprocessorRecordTypes RecType =
1608  (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1609  switch (RecType) {
1610  case PP_MODULE_MACRO:
1612  return Macro;
1613 
1614  case PP_MACRO_OBJECT_LIKE:
1615  case PP_MACRO_FUNCTION_LIKE: {
1616  // If we already have a macro, that means that we've hit the end
1617  // of the definition of the macro we were looking for. We're
1618  // done.
1619  if (Macro)
1620  return Macro;
1621 
1622  unsigned NextIndex = 1; // Skip identifier ID.
1623  SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1624  MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1625  MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1626  MI->setIsUsed(Record[NextIndex++]);
1627  MI->setUsedForHeaderGuard(Record[NextIndex++]);
1628 
1629  if (RecType == PP_MACRO_FUNCTION_LIKE) {
1630  // Decode function-like macro info.
1631  bool isC99VarArgs = Record[NextIndex++];
1632  bool isGNUVarArgs = Record[NextIndex++];
1633  bool hasCommaPasting = Record[NextIndex++];
1634  MacroParams.clear();
1635  unsigned NumArgs = Record[NextIndex++];
1636  for (unsigned i = 0; i != NumArgs; ++i)
1637  MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1638 
1639  // Install function-like macro info.
1640  MI->setIsFunctionLike();
1641  if (isC99VarArgs) MI->setIsC99Varargs();
1642  if (isGNUVarArgs) MI->setIsGNUVarargs();
1643  if (hasCommaPasting) MI->setHasCommaPasting();
1644  MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1645  }
1646 
1647  // Remember that we saw this macro last so that we add the tokens that
1648  // form its body to it.
1649  Macro = MI;
1650 
1651  if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1652  Record[NextIndex]) {
1653  // We have a macro definition. Register the association
1655  GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1656  PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1657  PreprocessingRecord::PPEntityID PPID =
1658  PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1659  MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1660  PPRec.getPreprocessedEntity(PPID));
1661  if (PPDef)
1662  PPRec.RegisterMacroDefinition(Macro, PPDef);
1663  }
1664 
1665  ++NumMacrosRead;
1666  break;
1667  }
1668 
1669  case PP_TOKEN: {
1670  // If we see a TOKEN before a PP_MACRO_*, then the file is
1671  // erroneous, just pretend we didn't see this.
1672  if (!Macro) break;
1673 
1674  unsigned Idx = 0;
1675  Token Tok = ReadToken(F, Record, Idx);
1676  Macro->AddTokenToBody(Tok);
1677  break;
1678  }
1679  }
1680  }
1681 }
1682 
1685  unsigned LocalID) const {
1686  if (!M.ModuleOffsetMap.empty())
1687  ReadModuleOffsetMap(M);
1688 
1691  assert(I != M.PreprocessedEntityRemap.end()
1692  && "Invalid index into preprocessed entity index remap");
1693 
1694  return LocalID + I->second;
1695 }
1696 
1698  return llvm::hash_combine(ikey.Size, ikey.ModTime);
1699 }
1700 
1702 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1703  internal_key_type ikey = {FE->getSize(),
1704  M.HasTimestamps ? FE->getModificationTime() : 0,
1705  FE->getName(), /*Imported*/ false};
1706  return ikey;
1707 }
1708 
1709 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1710  if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1711  return false;
1712 
1713  if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1714  return true;
1715 
1716  // Determine whether the actual files are equivalent.
1717  FileManager &FileMgr = Reader.getFileManager();
1718  auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1719  if (!Key.Imported)
1720  return FileMgr.getFile(Key.Filename);
1721 
1722  std::string Resolved = Key.Filename;
1723  Reader.ResolveImportedPath(M, Resolved);
1724  return FileMgr.getFile(Resolved);
1725  };
1726 
1727  const FileEntry *FEA = GetFile(a);
1728  const FileEntry *FEB = GetFile(b);
1729  return FEA && FEA == FEB;
1730 }
1731 
1732 std::pair<unsigned, unsigned>
1733 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1734  using namespace llvm::support;
1735 
1736  unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1737  unsigned DataLen = (unsigned) *d++;
1738  return std::make_pair(KeyLen, DataLen);
1739 }
1740 
1742 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1743  using namespace llvm::support;
1744 
1745  internal_key_type ikey;
1746  ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1747  ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1748  ikey.Filename = (const char *)d;
1749  ikey.Imported = true;
1750  return ikey;
1751 }
1752 
1754 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1755  unsigned DataLen) {
1756  using namespace llvm::support;
1757 
1758  const unsigned char *End = d + DataLen;
1759  HeaderFileInfo HFI;
1760  unsigned Flags = *d++;
1761  // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1762  HFI.isImport |= (Flags >> 5) & 0x01;
1763  HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1764  HFI.DirInfo = (Flags >> 1) & 0x07;
1765  HFI.IndexHeaderMapHeader = Flags & 0x01;
1766  // FIXME: Find a better way to handle this. Maybe just store a
1767  // "has been included" flag?
1768  HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1769  HFI.NumIncludes);
1770  HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1771  M, endian::readNext<uint32_t, little, unaligned>(d));
1772  if (unsigned FrameworkOffset =
1773  endian::readNext<uint32_t, little, unaligned>(d)) {
1774  // The framework offset is 1 greater than the actual offset,
1775  // since 0 is used as an indicator for "no framework name".
1776  StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1777  HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1778  }
1779 
1780  assert((End - d) % 4 == 0 &&
1781  "Wrong data length in HeaderFileInfo deserialization");
1782  while (d != End) {
1783  uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1784  auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1785  LocalSMID >>= 2;
1786 
1787  // This header is part of a module. Associate it with the module to enable
1788  // implicit module import.
1789  SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1790  Module *Mod = Reader.getSubmodule(GlobalSMID);
1791  FileManager &FileMgr = Reader.getFileManager();
1792  ModuleMap &ModMap =
1793  Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1794 
1795  std::string Filename = key.Filename;
1796  if (key.Imported)
1797  Reader.ResolveImportedPath(M, Filename);
1798  // FIXME: This is not always the right filename-as-written, but we're not
1799  // going to use this information to rebuild the module, so it doesn't make
1800  // a lot of difference.
1801  Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1802  ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1803  HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1804  }
1805 
1806  // This HeaderFileInfo was externally loaded.
1807  HFI.External = true;
1808  HFI.IsValid = true;
1809  return HFI;
1810 }
1811 
1813  ModuleFile *M,
1814  uint64_t MacroDirectivesOffset) {
1815  assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1816  PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1817 }
1818 
1820  // Note that we are loading defined macros.
1821  Deserializing Macros(this);
1822 
1823  for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1824  BitstreamCursor &MacroCursor = I.MacroCursor;
1825 
1826  // If there was no preprocessor block, skip this file.
1827  if (MacroCursor.getBitcodeBytes().empty())
1828  continue;
1829 
1830  BitstreamCursor Cursor = MacroCursor;
1831  Cursor.JumpToBit(I.MacroStartOffset);
1832 
1833  RecordData Record;
1834  while (true) {
1835  llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1836 
1837  switch (E.Kind) {
1838  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1840  Error("malformed block record in AST file");
1841  return;
1842  case llvm::BitstreamEntry::EndBlock:
1843  goto NextCursor;
1844 
1845  case llvm::BitstreamEntry::Record:
1846  Record.clear();
1847  switch (Cursor.readRecord(E.ID, Record)) {
1848  default: // Default behavior: ignore.
1849  break;
1850 
1851  case PP_MACRO_OBJECT_LIKE:
1852  case PP_MACRO_FUNCTION_LIKE: {
1853  IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1854  if (II->isOutOfDate())
1855  updateOutOfDateIdentifier(*II);
1856  break;
1857  }
1858 
1859  case PP_TOKEN:
1860  // Ignore tokens.
1861  break;
1862  }
1863  break;
1864  }
1865  }
1866  NextCursor: ;
1867  }
1868 }
1869 
1870 namespace {
1871 
1872  /// Visitor class used to look up identifirs in an AST file.
1873  class IdentifierLookupVisitor {
1874  StringRef Name;
1875  unsigned NameHash;
1876  unsigned PriorGeneration;
1877  unsigned &NumIdentifierLookups;
1878  unsigned &NumIdentifierLookupHits;
1879  IdentifierInfo *Found = nullptr;
1880 
1881  public:
1882  IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1883  unsigned &NumIdentifierLookups,
1884  unsigned &NumIdentifierLookupHits)
1885  : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1886  PriorGeneration(PriorGeneration),
1887  NumIdentifierLookups(NumIdentifierLookups),
1888  NumIdentifierLookupHits(NumIdentifierLookupHits) {}
1889 
1890  bool operator()(ModuleFile &M) {
1891  // If we've already searched this module file, skip it now.
1892  if (M.Generation <= PriorGeneration)
1893  return true;
1894 
1895  ASTIdentifierLookupTable *IdTable
1897  if (!IdTable)
1898  return false;
1899 
1900  ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
1901  Found);
1902  ++NumIdentifierLookups;
1903  ASTIdentifierLookupTable::iterator Pos =
1904  IdTable->find_hashed(Name, NameHash, &Trait);
1905  if (Pos == IdTable->end())
1906  return false;
1907 
1908  // Dereferencing the iterator has the effect of building the
1909  // IdentifierInfo node and populating it with the various
1910  // declarations it needs.
1911  ++NumIdentifierLookupHits;
1912  Found = *Pos;
1913  return true;
1914  }
1915 
1916  // Retrieve the identifier info found within the module
1917  // files.
1918  IdentifierInfo *getIdentifierInfo() const { return Found; }
1919  };
1920 
1921 } // namespace
1922 
1924  // Note that we are loading an identifier.
1925  Deserializing AnIdentifier(this);
1926 
1927  unsigned PriorGeneration = 0;
1928  if (getContext().getLangOpts().Modules)
1929  PriorGeneration = IdentifierGeneration[&II];
1930 
1931  // If there is a global index, look there first to determine which modules
1932  // provably do not have any results for this identifier.
1934  GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1935  if (!loadGlobalIndex()) {
1936  if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1937  HitsPtr = &Hits;
1938  }
1939  }
1940 
1941  IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1942  NumIdentifierLookups,
1943  NumIdentifierLookupHits);
1944  ModuleMgr.visit(Visitor, HitsPtr);
1945  markIdentifierUpToDate(&II);
1946 }
1947 
1949  if (!II)
1950  return;
1951 
1952  II->setOutOfDate(false);
1953 
1954  // Update the generation for this identifier.
1955  if (getContext().getLangOpts().Modules)
1956  IdentifierGeneration[II] = getGeneration();
1957 }
1958 
1960  const PendingMacroInfo &PMInfo) {
1961  ModuleFile &M = *PMInfo.M;
1962 
1963  BitstreamCursor &Cursor = M.MacroCursor;
1964  SavedStreamPosition SavedPosition(Cursor);
1965  Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
1966 
1967  struct ModuleMacroRecord {
1968  SubmoduleID SubModID;
1969  MacroInfo *MI;
1970  SmallVector<SubmoduleID, 8> Overrides;
1971  };
1973 
1974  // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1975  // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1976  // macro histroy.
1977  RecordData Record;
1978  while (true) {
1979  llvm::BitstreamEntry Entry =
1980  Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1981  if (Entry.Kind != llvm::BitstreamEntry::Record) {
1982  Error("malformed block record in AST file");
1983  return;
1984  }
1985 
1986  Record.clear();
1987  switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
1989  break;
1990 
1991  case PP_MODULE_MACRO: {
1992  ModuleMacros.push_back(ModuleMacroRecord());
1993  auto &Info = ModuleMacros.back();
1994  Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1995  Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
1996  for (int I = 2, N = Record.size(); I != N; ++I)
1997  Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
1998  continue;
1999  }
2000 
2001  default:
2002  Error("malformed block record in AST file");
2003  return;
2004  }
2005 
2006  // We found the macro directive history; that's the last record
2007  // for this macro.
2008  break;
2009  }
2010 
2011  // Module macros are listed in reverse dependency order.
2012  {
2013  std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2015  for (auto &MMR : ModuleMacros) {
2016  Overrides.clear();
2017  for (unsigned ModID : MMR.Overrides) {
2018  Module *Mod = getSubmodule(ModID);
2019  auto *Macro = PP.getModuleMacro(Mod, II);
2020  assert(Macro && "missing definition for overridden macro");
2021  Overrides.push_back(Macro);
2022  }
2023 
2024  bool Inserted = false;
2025  Module *Owner = getSubmodule(MMR.SubModID);
2026  PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2027  }
2028  }
2029 
2030  // Don't read the directive history for a module; we don't have anywhere
2031  // to put it.
2032  if (M.isModule())
2033  return;
2034 
2035  // Deserialize the macro directives history in reverse source-order.
2036  MacroDirective *Latest = nullptr, *Earliest = nullptr;
2037  unsigned Idx = 0, N = Record.size();
2038  while (Idx < N) {
2039  MacroDirective *MD = nullptr;
2040  SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2041  MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2042  switch (K) {
2044  MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2045  MD = PP.AllocateDefMacroDirective(MI, Loc);
2046  break;
2047  }
2049  MD = PP.AllocateUndefMacroDirective(Loc);
2050  break;
2052  bool isPublic = Record[Idx++];
2053  MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2054  break;
2055  }
2056 
2057  if (!Latest)
2058  Latest = MD;
2059  if (Earliest)
2060  Earliest->setPrevious(MD);
2061  Earliest = MD;
2062  }
2063 
2064  if (Latest)
2065  PP.setLoadedMacroDirective(II, Earliest, Latest);
2066 }
2067 
2068 ASTReader::InputFileInfo
2069 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2070  // Go find this input file.
2071  BitstreamCursor &Cursor = F.InputFilesCursor;
2072  SavedStreamPosition SavedPosition(Cursor);
2073  Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2074 
2075  unsigned Code = Cursor.ReadCode();
2076  RecordData Record;
2077  StringRef Blob;
2078 
2079  unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2080  assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2081  "invalid record type for input file");
2082  (void)Result;
2083 
2084  assert(Record[0] == ID && "Bogus stored ID or offset");
2085  InputFileInfo R;
2086  R.StoredSize = static_cast<off_t>(Record[1]);
2087  R.StoredTime = static_cast<time_t>(Record[2]);
2088  R.Overridden = static_cast<bool>(Record[3]);
2089  R.Transient = static_cast<bool>(Record[4]);
2090  R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2091  R.Filename = Blob;
2092  ResolveImportedPath(F, R.Filename);
2093  return R;
2094 }
2095 
2096 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2097 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2098  // If this ID is bogus, just return an empty input file.
2099  if (ID == 0 || ID > F.InputFilesLoaded.size())
2100  return InputFile();
2101 
2102  // If we've already loaded this input file, return it.
2103  if (F.InputFilesLoaded[ID-1].getFile())
2104  return F.InputFilesLoaded[ID-1];
2105 
2106  if (F.InputFilesLoaded[ID-1].isNotFound())
2107  return InputFile();
2108 
2109  // Go find this input file.
2110  BitstreamCursor &Cursor = F.InputFilesCursor;
2111  SavedStreamPosition SavedPosition(Cursor);
2112  Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2113 
2114  InputFileInfo FI = readInputFileInfo(F, ID);
2115  off_t StoredSize = FI.StoredSize;
2116  time_t StoredTime = FI.StoredTime;
2117  bool Overridden = FI.Overridden;
2118  bool Transient = FI.Transient;
2119  StringRef Filename = FI.Filename;
2120 
2121  const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
2122  // If we didn't find the file, resolve it relative to the
2123  // original directory from which this AST file was created.
2124  if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2125  F.OriginalDir != F.BaseDirectory) {
2126  std::string Resolved = resolveFileRelativeToOriginalDir(
2127  Filename, F.OriginalDir, F.BaseDirectory);
2128  if (!Resolved.empty())
2129  File = FileMgr.getFile(Resolved);
2130  }
2131 
2132  // For an overridden file, create a virtual file with the stored
2133  // size/timestamp.
2134  if ((Overridden || Transient) && File == nullptr)
2135  File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2136 
2137  if (File == nullptr) {
2138  if (Complain) {
2139  std::string ErrorStr = "could not find file '";
2140  ErrorStr += Filename;
2141  ErrorStr += "' referenced by AST file '";
2142  ErrorStr += F.FileName;
2143  ErrorStr += "'";
2144  Error(ErrorStr);
2145  }
2146  // Record that we didn't find the file.
2148  return InputFile();
2149  }
2150 
2151  // Check if there was a request to override the contents of the file
2152  // that was part of the precompiled header. Overriding such a file
2153  // can lead to problems when lexing using the source locations from the
2154  // PCH.
2155  SourceManager &SM = getSourceManager();
2156  // FIXME: Reject if the overrides are different.
2157  if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2158  if (Complain)
2159  Error(diag::err_fe_pch_file_overridden, Filename);
2160  // After emitting the diagnostic, recover by disabling the override so
2161  // that the original file will be used.
2162  //
2163  // FIXME: This recovery is just as broken as the original state; there may
2164  // be another precompiled module that's using the overridden contents, or
2165  // we might be half way through parsing it. Instead, we should treat the
2166  // overridden contents as belonging to a separate FileEntry.
2167  SM.disableFileContentsOverride(File);
2168  // The FileEntry is a virtual file entry with the size of the contents
2169  // that would override the original contents. Set it to the original's
2170  // size/time.
2171  FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2172  StoredSize, StoredTime);
2173  }
2174 
2175  bool IsOutOfDate = false;
2176 
2177  // For an overridden file, there is nothing to validate.
2178  if (!Overridden && //
2179  (StoredSize != File->getSize() ||
2180  (StoredTime && StoredTime != File->getModificationTime() &&
2181  !DisableValidation)
2182  )) {
2183  if (Complain) {
2184  // Build a list of the PCH imports that got us here (in reverse).
2185  SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2186  while (!ImportStack.back()->ImportedBy.empty())
2187  ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2188 
2189  // The top-level PCH is stale.
2190  StringRef TopLevelPCHName(ImportStack.back()->FileName);
2191  unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2192  if (DiagnosticKind == 0)
2193  Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2194  else if (DiagnosticKind == 1)
2195  Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2196  else
2197  Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2198 
2199  // Print the import stack.
2200  if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2201  Diag(diag::note_pch_required_by)
2202  << Filename << ImportStack[0]->FileName;
2203  for (unsigned I = 1; I < ImportStack.size(); ++I)
2204  Diag(diag::note_pch_required_by)
2205  << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2206  }
2207 
2208  if (!Diags.isDiagnosticInFlight())
2209  Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2210  }
2211 
2212  IsOutOfDate = true;
2213  }
2214  // FIXME: If the file is overridden and we've already opened it,
2215  // issue an error (or split it into a separate FileEntry).
2216 
2217  InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2218 
2219  // Note that we've loaded this input file.
2220  F.InputFilesLoaded[ID-1] = IF;
2221  return IF;
2222 }
2223 
2224 /// If we are loading a relocatable PCH or module file, and the filename
2225 /// is not an absolute path, add the system or module root to the beginning of
2226 /// the file name.
2228  // Resolve relative to the base directory, if we have one.
2229  if (!M.BaseDirectory.empty())
2230  return ResolveImportedPath(Filename, M.BaseDirectory);
2231 }
2232 
2233 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2234  if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2235  return;
2236 
2237  SmallString<128> Buffer;
2238  llvm::sys::path::append(Buffer, Prefix, Filename);
2239  Filename.assign(Buffer.begin(), Buffer.end());
2240 }
2241 
2242 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2243  switch (ARR) {
2244  case ASTReader::Failure: return true;
2245  case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2246  case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2249  return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2250  case ASTReader::HadErrors: return true;
2251  case ASTReader::Success: return false;
2252  }
2253 
2254  llvm_unreachable("unknown ASTReadResult");
2255 }
2256 
2257 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2258  BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2259  bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2260  std::string &SuggestedPredefines) {
2261  if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2262  return Failure;
2263 
2264  // Read all of the records in the options block.
2265  RecordData Record;
2266  ASTReadResult Result = Success;
2267  while (true) {
2268  llvm::BitstreamEntry Entry = Stream.advance();
2269 
2270  switch (Entry.Kind) {
2272  case llvm::BitstreamEntry::SubBlock:
2273  return Failure;
2274 
2275  case llvm::BitstreamEntry::EndBlock:
2276  return Result;
2277 
2278  case llvm::BitstreamEntry::Record:
2279  // The interesting case.
2280  break;
2281  }
2282 
2283  // Read and process a record.
2284  Record.clear();
2285  switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2286  case LANGUAGE_OPTIONS: {
2287  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2288  if (ParseLanguageOptions(Record, Complain, Listener,
2289  AllowCompatibleConfigurationMismatch))
2290  Result = ConfigurationMismatch;
2291  break;
2292  }
2293 
2294  case TARGET_OPTIONS: {
2295  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2296  if (ParseTargetOptions(Record, Complain, Listener,
2297  AllowCompatibleConfigurationMismatch))
2298  Result = ConfigurationMismatch;
2299  break;
2300  }
2301 
2302  case FILE_SYSTEM_OPTIONS: {
2303  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2304  if (!AllowCompatibleConfigurationMismatch &&
2305  ParseFileSystemOptions(Record, Complain, Listener))
2306  Result = ConfigurationMismatch;
2307  break;
2308  }
2309 
2310  case HEADER_SEARCH_OPTIONS: {
2311  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2312  if (!AllowCompatibleConfigurationMismatch &&
2313  ParseHeaderSearchOptions(Record, Complain, Listener))
2314  Result = ConfigurationMismatch;
2315  break;
2316  }
2317 
2318  case PREPROCESSOR_OPTIONS:
2319  bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2320  if (!AllowCompatibleConfigurationMismatch &&
2321  ParsePreprocessorOptions(Record, Complain, Listener,
2322  SuggestedPredefines))
2323  Result = ConfigurationMismatch;
2324  break;
2325  }
2326  }
2327 }
2328 
2330 ASTReader::ReadControlBlock(ModuleFile &F,
2332  const ModuleFile *ImportedBy,
2333  unsigned ClientLoadCapabilities) {
2334  BitstreamCursor &Stream = F.Stream;
2335  ASTReadResult Result = Success;
2336 
2337  if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2338  Error("malformed block record in AST file");
2339  return Failure;
2340  }
2341 
2342  // Lambda to read the unhashed control block the first time it's called.
2343  //
2344  // For PCM files, the unhashed control block cannot be read until after the
2345  // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2346  // need to look ahead before reading the IMPORTS record. For consistency,
2347  // this block is always read somehow (see BitstreamEntry::EndBlock).
2348  bool HasReadUnhashedControlBlock = false;
2349  auto readUnhashedControlBlockOnce = [&]() {
2350  if (!HasReadUnhashedControlBlock) {
2351  HasReadUnhashedControlBlock = true;
2352  if (ASTReadResult Result =
2353  readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2354  return Result;
2355  }
2356  return Success;
2357  };
2358 
2359  // Read all of the records and blocks in the control block.
2360  RecordData Record;
2361  unsigned NumInputs = 0;
2362  unsigned NumUserInputs = 0;
2363  StringRef BaseDirectoryAsWritten;
2364  while (true) {
2365  llvm::BitstreamEntry Entry = Stream.advance();
2366 
2367  switch (Entry.Kind) {
2369  Error("malformed block record in AST file");
2370  return Failure;
2371  case llvm::BitstreamEntry::EndBlock: {
2372  // Validate the module before returning. This call catches an AST with
2373  // no module name and no imports.
2374  if (ASTReadResult Result = readUnhashedControlBlockOnce())
2375  return Result;
2376 
2377  // Validate input files.
2378  const HeaderSearchOptions &HSOpts =
2379  PP.getHeaderSearchInfo().getHeaderSearchOpts();
2380 
2381  // All user input files reside at the index range [0, NumUserInputs), and
2382  // system input files reside at [NumUserInputs, NumInputs). For explicitly
2383  // loaded module files, ignore missing inputs.
2384  if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2385  F.Kind != MK_PrebuiltModule) {
2386  bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2387 
2388  // If we are reading a module, we will create a verification timestamp,
2389  // so we verify all input files. Otherwise, verify only user input
2390  // files.
2391 
2392  unsigned N = NumUserInputs;
2393  if (ValidateSystemInputs ||
2396  F.Kind == MK_ImplicitModule))
2397  N = NumInputs;
2398 
2399  for (unsigned I = 0; I < N; ++I) {
2400  InputFile IF = getInputFile(F, I+1, Complain);
2401  if (!IF.getFile() || IF.isOutOfDate())
2402  return OutOfDate;
2403  }
2404  }
2405 
2406  if (Listener)
2407  Listener->visitModuleFile(F.FileName, F.Kind);
2408 
2409  if (Listener && Listener->needsInputFileVisitation()) {
2410  unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2411  : NumUserInputs;
2412  for (unsigned I = 0; I < N; ++I) {
2413  bool IsSystem = I >= NumUserInputs;
2414  InputFileInfo FI = readInputFileInfo(F, I+1);
2415  Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2416  F.Kind == MK_ExplicitModule ||
2417  F.Kind == MK_PrebuiltModule);
2418  }
2419  }
2420 
2421  return Result;
2422  }
2423 
2424  case llvm::BitstreamEntry::SubBlock:
2425  switch (Entry.ID) {
2426  case INPUT_FILES_BLOCK_ID:
2427  F.InputFilesCursor = Stream;
2428  if (Stream.SkipBlock() || // Skip with the main cursor
2429  // Read the abbreviations
2430  ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2431  Error("malformed block record in AST file");
2432  return Failure;
2433  }
2434  continue;
2435 
2436  case OPTIONS_BLOCK_ID:
2437  // If we're reading the first module for this group, check its options
2438  // are compatible with ours. For modules it imports, no further checking
2439  // is required, because we checked them when we built it.
2440  if (Listener && !ImportedBy) {
2441  // Should we allow the configuration of the module file to differ from
2442  // the configuration of the current translation unit in a compatible
2443  // way?
2444  //
2445  // FIXME: Allow this for files explicitly specified with -include-pch.
2446  bool AllowCompatibleConfigurationMismatch =
2448 
2449  Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2450  AllowCompatibleConfigurationMismatch,
2451  *Listener, SuggestedPredefines);
2452  if (Result == Failure) {
2453  Error("malformed block record in AST file");
2454  return Result;
2455  }
2456 
2457  if (DisableValidation ||
2458  (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2459  Result = Success;
2460 
2461  // If we can't load the module, exit early since we likely
2462  // will rebuild the module anyway. The stream may be in the
2463  // middle of a block.
2464  if (Result != Success)
2465  return Result;
2466  } else if (Stream.SkipBlock()) {
2467  Error("malformed block record in AST file");
2468  return Failure;
2469  }
2470  continue;
2471 
2472  default:
2473  if (Stream.SkipBlock()) {
2474  Error("malformed block record in AST file");
2475  return Failure;
2476  }
2477  continue;
2478  }
2479 
2480  case llvm::BitstreamEntry::Record:
2481  // The interesting case.
2482  break;
2483  }
2484 
2485  // Read and process a record.
2486  Record.clear();
2487  StringRef Blob;
2488  switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2489  case METADATA: {
2490  if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2491  if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2492  Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2493  : diag::err_pch_version_too_new);
2494  return VersionMismatch;
2495  }
2496 
2497  bool hasErrors = Record[7];
2498  if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2499  Diag(diag::err_pch_with_compiler_errors);
2500  return HadErrors;
2501  }
2502  if (hasErrors) {
2503  Diags.ErrorOccurred = true;
2504  Diags.UncompilableErrorOccurred = true;
2505  Diags.UnrecoverableErrorOccurred = true;
2506  }
2507 
2508  F.RelocatablePCH = Record[4];
2509  // Relative paths in a relocatable PCH are relative to our sysroot.
2510  if (F.RelocatablePCH)
2511  F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2512 
2513  F.HasTimestamps = Record[5];
2514 
2515  F.PCHHasObjectFile = Record[6];
2516 
2517  const std::string &CurBranch = getClangFullRepositoryVersion();
2518  StringRef ASTBranch = Blob;
2519  if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2520  if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2521  Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2522  return VersionMismatch;
2523  }
2524  break;
2525  }
2526 
2527  case IMPORTS: {
2528  // Validate the AST before processing any imports (otherwise, untangling
2529  // them can be error-prone and expensive). A module will have a name and
2530  // will already have been validated, but this catches the PCH case.
2531  if (ASTReadResult Result = readUnhashedControlBlockOnce())
2532  return Result;
2533 
2534  // Load each of the imported PCH files.
2535  unsigned Idx = 0, N = Record.size();
2536  while (Idx < N) {
2537  // Read information about the AST file.
2538  ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2539  // The import location will be the local one for now; we will adjust
2540  // all import locations of module imports after the global source
2541  // location info are setup, in ReadAST.
2542  SourceLocation ImportLoc =
2543  ReadUntranslatedSourceLocation(Record[Idx++]);
2544  off_t StoredSize = (off_t)Record[Idx++];
2545  time_t StoredModTime = (time_t)Record[Idx++];
2546  ASTFileSignature StoredSignature = {
2547  {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2548  (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2549  (uint32_t)Record[Idx++]}}};
2550 
2551  std::string ImportedName = ReadString(Record, Idx);
2552  std::string ImportedFile;
2553 
2554  // For prebuilt and explicit modules first consult the file map for
2555  // an override. Note that here we don't search prebuilt module
2556  // directories, only the explicit name to file mappings. Also, we will
2557  // still verify the size/signature making sure it is essentially the
2558  // same file but perhaps in a different location.
2559  if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2560  ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2561  ImportedName, /*FileMapOnly*/ true);
2562 
2563  if (ImportedFile.empty())
2564  // Use BaseDirectoryAsWritten to ensure we use the same path in the
2565  // ModuleCache as when writing.
2566  ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2567  else
2568  SkipPath(Record, Idx);
2569 
2570  // If our client can't cope with us being out of date, we can't cope with
2571  // our dependency being missing.
2572  unsigned Capabilities = ClientLoadCapabilities;
2573  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2574  Capabilities &= ~ARR_Missing;
2575 
2576  // Load the AST file.
2577  auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2578  Loaded, StoredSize, StoredModTime,
2579  StoredSignature, Capabilities);
2580 
2581  // If we diagnosed a problem, produce a backtrace.
2582  if (isDiagnosedResult(Result, Capabilities))
2583  Diag(diag::note_module_file_imported_by)
2584  << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2585 
2586  switch (Result) {
2587  case Failure: return Failure;
2588  // If we have to ignore the dependency, we'll have to ignore this too.
2589  case Missing:
2590  case OutOfDate: return OutOfDate;
2591  case VersionMismatch: return VersionMismatch;
2592  case ConfigurationMismatch: return ConfigurationMismatch;
2593  case HadErrors: return HadErrors;
2594  case Success: break;
2595  }
2596  }
2597  break;
2598  }
2599 
2600  case ORIGINAL_FILE:
2601  F.OriginalSourceFileID = FileID::get(Record[0]);
2604  ResolveImportedPath(F, F.OriginalSourceFileName);
2605  break;
2606 
2607  case ORIGINAL_FILE_ID:
2608  F.OriginalSourceFileID = FileID::get(Record[0]);
2609  break;
2610 
2611  case ORIGINAL_PCH_DIR:
2612  F.OriginalDir = Blob;
2613  break;
2614 
2615  case MODULE_NAME:
2616  F.ModuleName = Blob;
2617  Diag(diag::remark_module_import)
2618  << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2619  << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2620  if (Listener)
2621  Listener->ReadModuleName(F.ModuleName);
2622 
2623  // Validate the AST as soon as we have a name so we can exit early on
2624  // failure.
2625  if (ASTReadResult Result = readUnhashedControlBlockOnce())
2626  return Result;
2627 
2628  break;
2629 
2630  case MODULE_DIRECTORY: {
2631  // Save the BaseDirectory as written in the PCM for computing the module
2632  // filename for the ModuleCache.
2633  BaseDirectoryAsWritten = Blob;
2634  assert(!F.ModuleName.empty() &&
2635  "MODULE_DIRECTORY found before MODULE_NAME");
2636  // If we've already loaded a module map file covering this module, we may
2637  // have a better path for it (relative to the current build).
2638  Module *M = PP.getHeaderSearchInfo().lookupModule(
2639  F.ModuleName, /*AllowSearch*/ true,
2640  /*AllowExtraModuleMapSearch*/ true);
2641  if (M && M->Directory) {
2642  // If we're implicitly loading a module, the base directory can't
2643  // change between the build and use.
2644  // Don't emit module relocation error if we have -fno-validate-pch
2645  if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2647  const DirectoryEntry *BuildDir =
2648  PP.getFileManager().getDirectory(Blob);
2649  if (!BuildDir || BuildDir != M->Directory) {
2650  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2651  Diag(diag::err_imported_module_relocated)
2652  << F.ModuleName << Blob << M->Directory->getName();
2653  return OutOfDate;
2654  }
2655  }
2656  F.BaseDirectory = M->Directory->getName();
2657  } else {
2658  F.BaseDirectory = Blob;
2659  }
2660  break;
2661  }
2662 
2663  case MODULE_MAP_FILE:
2664  if (ASTReadResult Result =
2665  ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2666  return Result;
2667  break;
2668 
2669  case INPUT_FILE_OFFSETS:
2670  NumInputs = Record[0];
2671  NumUserInputs = Record[1];
2672  F.InputFileOffsets =
2673  (const llvm::support::unaligned_uint64_t *)Blob.data();
2674  F.InputFilesLoaded.resize(NumInputs);
2675  F.NumUserInputFiles = NumUserInputs;
2676  break;
2677  }
2678  }
2679 }
2680 
2682 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2683  BitstreamCursor &Stream = F.Stream;
2684 
2685  if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2686  Error("malformed block record in AST file");
2687  return Failure;
2688  }
2689 
2690  // Read all of the records and blocks for the AST file.
2691  RecordData Record;
2692  while (true) {
2693  llvm::BitstreamEntry Entry = Stream.advance();
2694 
2695  switch (Entry.Kind) {
2697  Error("error at end of module block in AST file");
2698  return Failure;
2699  case llvm::BitstreamEntry::EndBlock:
2700  // Outside of C++, we do not store a lookup map for the translation unit.
2701  // Instead, mark it as needing a lookup map to be built if this module
2702  // contains any declarations lexically within it (which it always does!).
2703  // This usually has no cost, since we very rarely need the lookup map for
2704  // the translation unit outside C++.
2705  if (ASTContext *Ctx = ContextObj) {
2706  DeclContext *DC = Ctx->getTranslationUnitDecl();
2707  if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2709  }
2710 
2711  return Success;
2712  case llvm::BitstreamEntry::SubBlock:
2713  switch (Entry.ID) {
2714  case DECLTYPES_BLOCK_ID:
2715  // We lazily load the decls block, but we want to set up the
2716  // DeclsCursor cursor to point into it. Clone our current bitcode
2717  // cursor to it, enter the block and read the abbrevs in that block.
2718  // With the main cursor, we just skip over it.
2719  F.DeclsCursor = Stream;
2720  if (Stream.SkipBlock() || // Skip with the main cursor.
2721  // Read the abbrevs.
2722  ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2723  Error("malformed block record in AST file");
2724  return Failure;
2725  }
2726  break;
2727 
2728  case PREPROCESSOR_BLOCK_ID:
2729  F.MacroCursor = Stream;
2730  if (!PP.getExternalSource())
2731  PP.setExternalSource(this);
2732 
2733  if (Stream.SkipBlock() ||
2734  ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2735  Error("malformed block record in AST file");
2736  return Failure;
2737  }
2738  F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2739  break;
2740 
2742  F.PreprocessorDetailCursor = Stream;
2743  if (Stream.SkipBlock() ||
2744  ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2746  Error("malformed preprocessor detail record in AST file");
2747  return Failure;
2748  }
2750  = F.PreprocessorDetailCursor.GetCurrentBitNo();
2751 
2752  if (!PP.getPreprocessingRecord())
2753  PP.createPreprocessingRecord();
2754  if (!PP.getPreprocessingRecord()->getExternalSource())
2755  PP.getPreprocessingRecord()->SetExternalSource(*this);
2756  break;
2757 
2759  if (ReadSourceManagerBlock(F))
2760  return Failure;
2761  break;
2762 
2763  case SUBMODULE_BLOCK_ID:
2764  if (ASTReadResult Result =
2765  ReadSubmoduleBlock(F, ClientLoadCapabilities))
2766  return Result;
2767  break;
2768 
2769  case COMMENTS_BLOCK_ID: {
2770  BitstreamCursor C = Stream;
2771  if (Stream.SkipBlock() ||
2772  ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2773  Error("malformed comments block in AST file");
2774  return Failure;
2775  }
2776  CommentsCursors.push_back(std::make_pair(C, &F));
2777  break;
2778  }
2779 
2780  default:
2781  if (Stream.SkipBlock()) {
2782  Error("malformed block record in AST file");
2783  return Failure;
2784  }
2785  break;
2786  }
2787  continue;
2788 
2789  case llvm::BitstreamEntry::Record:
2790  // The interesting case.
2791  break;
2792  }
2793 
2794  // Read and process a record.
2795  Record.clear();
2796  StringRef Blob;
2797  auto RecordType =
2798  (ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob);
2799 
2800  // If we're not loading an AST context, we don't care about most records.
2801  if (!ContextObj) {
2802  switch (RecordType) {
2803  case IDENTIFIER_TABLE:
2804  case IDENTIFIER_OFFSET:
2806  case STATISTICS:
2807  case PP_CONDITIONAL_STACK:
2808  case PP_COUNTER_VALUE:
2810  case MODULE_OFFSET_MAP:
2813  case PPD_ENTITIES_OFFSETS:
2814  case HEADER_SEARCH_TABLE:
2815  case IMPORTED_MODULES:
2816  case MACRO_OFFSET:
2817  break;
2818  default:
2819  continue;
2820  }
2821  }
2822 
2823  switch (RecordType) {
2824  default: // Default behavior: ignore.
2825  break;
2826 
2827  case TYPE_OFFSET: {
2828  if (F.LocalNumTypes != 0) {
2829  Error("duplicate TYPE_OFFSET record in AST file");
2830  return Failure;
2831  }
2832  F.TypeOffsets = (const uint32_t *)Blob.data();
2833  F.LocalNumTypes = Record[0];
2834  unsigned LocalBaseTypeIndex = Record[1];
2835  F.BaseTypeIndex = getTotalNumTypes();
2836 
2837  if (F.LocalNumTypes > 0) {
2838  // Introduce the global -> local mapping for types within this module.
2839  GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2840 
2841  // Introduce the local -> global mapping for types within this module.
2843  std::make_pair(LocalBaseTypeIndex,
2844  F.BaseTypeIndex - LocalBaseTypeIndex));
2845 
2846  TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2847  }
2848  break;
2849  }
2850 
2851  case DECL_OFFSET: {
2852  if (F.LocalNumDecls != 0) {
2853  Error("duplicate DECL_OFFSET record in AST file");
2854  return Failure;
2855  }
2856  F.DeclOffsets = (const DeclOffset *)Blob.data();
2857  F.LocalNumDecls = Record[0];
2858  unsigned LocalBaseDeclID = Record[1];
2859  F.BaseDeclID = getTotalNumDecls();
2860 
2861  if (F.LocalNumDecls > 0) {
2862  // Introduce the global -> local mapping for declarations within this
2863  // module.
2864  GlobalDeclMap.insert(
2865  std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2866 
2867  // Introduce the local -> global mapping for declarations within this
2868  // module.
2870  std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2871 
2872  // Introduce the global -> local mapping for declarations within this
2873  // module.
2874  F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2875 
2876  DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2877  }
2878  break;
2879  }
2880 
2881  case TU_UPDATE_LEXICAL: {
2882  DeclContext *TU = ContextObj->getTranslationUnitDecl();
2883  LexicalContents Contents(
2884  reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2885  Blob.data()),
2886  static_cast<unsigned int>(Blob.size() / 4));
2887  TULexicalDecls.push_back(std::make_pair(&F, Contents));
2888  TU->setHasExternalLexicalStorage(true);
2889  break;
2890  }
2891 
2892  case UPDATE_VISIBLE: {
2893  unsigned Idx = 0;
2894  serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2895  auto *Data = (const unsigned char*)Blob.data();
2896  PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
2897  // If we've already loaded the decl, perform the updates when we finish
2898  // loading this block.
2899  if (Decl *D = GetExistingDecl(ID))
2900  PendingUpdateRecords.push_back(
2901  PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
2902  break;
2903  }
2904 
2905  case IDENTIFIER_TABLE:
2906  F.IdentifierTableData = Blob.data();
2907  if (Record[0]) {
2909  (const unsigned char *)F.IdentifierTableData + Record[0],
2910  (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2911  (const unsigned char *)F.IdentifierTableData,
2912  ASTIdentifierLookupTrait(*this, F));
2913 
2914  PP.getIdentifierTable().setExternalIdentifierLookup(this);
2915  }
2916  break;
2917 
2918  case IDENTIFIER_OFFSET: {
2919  if (F.LocalNumIdentifiers != 0) {
2920  Error("duplicate IDENTIFIER_OFFSET record in AST file");
2921  return Failure;
2922  }
2923  F.IdentifierOffsets = (const uint32_t *)Blob.data();
2924  F.LocalNumIdentifiers = Record[0];
2925  unsigned LocalBaseIdentifierID = Record[1];
2926  F.BaseIdentifierID = getTotalNumIdentifiers();
2927 
2928  if (F.LocalNumIdentifiers > 0) {
2929  // Introduce the global -> local mapping for identifiers within this
2930  // module.
2931  GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2932  &F));
2933 
2934  // Introduce the local -> global mapping for identifiers within this
2935  // module.
2937  std::make_pair(LocalBaseIdentifierID,
2938  F.BaseIdentifierID - LocalBaseIdentifierID));
2939 
2940  IdentifiersLoaded.resize(IdentifiersLoaded.size()
2941  + F.LocalNumIdentifiers);
2942  }
2943  break;
2944  }
2945 
2947  F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2948  break;
2949 
2951  // FIXME: Skip reading this record if our ASTConsumer doesn't care
2952  // about "interesting" decls (for instance, if we're building a module).
2953  for (unsigned I = 0, N = Record.size(); I != N; ++I)
2954  EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2955  break;
2956 
2957  case MODULAR_CODEGEN_DECLS:
2958  // FIXME: Skip reading this record if our ASTConsumer doesn't care about
2959  // them (ie: if we're not codegenerating this module).
2960  if (F.Kind == MK_MainFile)
2961  for (unsigned I = 0, N = Record.size(); I != N; ++I)
2962  EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2963  break;
2964 
2965  case SPECIAL_TYPES:
2966  if (SpecialTypes.empty()) {
2967  for (unsigned I = 0, N = Record.size(); I != N; ++I)
2968  SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2969  break;
2970  }
2971 
2972  if (SpecialTypes.size() != Record.size()) {
2973  Error("invalid special-types record");
2974  return Failure;
2975  }
2976 
2977  for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2978  serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2979  if (!SpecialTypes[I])
2980  SpecialTypes[I] = ID;
2981  // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2982  // merge step?
2983  }
2984  break;
2985 
2986  case STATISTICS:
2987  TotalNumStatements += Record[0];
2988  TotalNumMacros += Record[1];
2989  TotalLexicalDeclContexts += Record[2];
2990  TotalVisibleDeclContexts += Record[3];
2991  break;
2992 
2994  for (unsigned I = 0, N = Record.size(); I != N; ++I)
2995  UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2996  break;
2997 
2998  case DELEGATING_CTORS:
2999  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3000  DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3001  break;
3002 
3004  if (Record.size() % 4 != 0) {
3005  Error("invalid weak identifiers record");
3006  return Failure;
3007  }
3008 
3009  // FIXME: Ignore weak undeclared identifiers from non-original PCH
3010  // files. This isn't the way to do it :)
3011  WeakUndeclaredIdentifiers.clear();
3012 
3013  // Translate the weak, undeclared identifiers into global IDs.
3014  for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3015  WeakUndeclaredIdentifiers.push_back(
3016  getGlobalIdentifierID(F, Record[I++]));
3017  WeakUndeclaredIdentifiers.push_back(
3018  getGlobalIdentifierID(F, Record[I++]));
3019  WeakUndeclaredIdentifiers.push_back(
3020  ReadSourceLocation(F, Record, I).getRawEncoding());
3021  WeakUndeclaredIdentifiers.push_back(Record[I++]);
3022  }
3023  break;
3024 
3025  case SELECTOR_OFFSETS: {
3026  F.SelectorOffsets = (const uint32_t *)Blob.data();
3027  F.LocalNumSelectors = Record[0];
3028  unsigned LocalBaseSelectorID = Record[1];
3029  F.BaseSelectorID = getTotalNumSelectors();
3030 
3031  if (F.LocalNumSelectors > 0) {
3032  // Introduce the global -> local mapping for selectors within this
3033  // module.
3034  GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3035 
3036  // Introduce the local -> global mapping for selectors within this
3037  // module.
3039  std::make_pair(LocalBaseSelectorID,
3040  F.BaseSelectorID - LocalBaseSelectorID));
3041 
3042  SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3043  }
3044  break;
3045  }
3046 
3047  case METHOD_POOL:
3048  F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3049  if (Record[0])
3052  F.SelectorLookupTableData + Record[0],
3054  ASTSelectorLookupTrait(*this, F));
3055  TotalNumMethodPoolEntries += Record[1];
3056  break;
3057 
3059  if (!Record.empty()) {
3060  for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3061  ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3062  Record[Idx++]));
3063  ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3064  getRawEncoding());
3065  }
3066  }
3067  break;
3068 
3069  case PP_CONDITIONAL_STACK:
3070  if (!Record.empty()) {
3071  unsigned Idx = 0, End = Record.size() - 1;
3072  bool ReachedEOFWhileSkipping = Record[Idx++];
3074  if (ReachedEOFWhileSkipping) {
3075  SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3076  SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3077  bool FoundNonSkipPortion = Record[Idx++];
3078  bool FoundElse = Record[Idx++];
3079  SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3080  SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3081  FoundElse, ElseLoc);
3082  }
3083  SmallVector<PPConditionalInfo, 4> ConditionalStack;
3084  while (Idx < End) {
3085  auto Loc = ReadSourceLocation(F, Record, Idx);
3086  bool WasSkipping = Record[Idx++];
3087  bool FoundNonSkip = Record[Idx++];
3088  bool FoundElse = Record[Idx++];
3089  ConditionalStack.push_back(
3090  {Loc, WasSkipping, FoundNonSkip, FoundElse});
3091  }
3092  PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3093  }
3094  break;
3095 
3096  case PP_COUNTER_VALUE:
3097  if (!Record.empty() && Listener)
3098  Listener->ReadCounter(F, Record[0]);
3099  break;
3100 
3101  case FILE_SORTED_DECLS:
3102  F.FileSortedDecls = (const DeclID *)Blob.data();
3103  F.NumFileSortedDecls = Record[0];
3104  break;
3105 
3106  case SOURCE_LOCATION_OFFSETS: {
3107  F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3108  F.LocalNumSLocEntries = Record[0];
3109  unsigned SLocSpaceSize = Record[1];
3110  std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3111  SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3112  SLocSpaceSize);
3113  if (!F.SLocEntryBaseID) {
3114  Error("ran out of source locations");
3115  break;
3116  }
3117  // Make our entry in the range map. BaseID is negative and growing, so
3118  // we invert it. Because we invert it, though, we need the other end of
3119  // the range.
3120  unsigned RangeStart =
3121  unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3122  GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3124 
3125  // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3126  assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3127  GlobalSLocOffsetMap.insert(
3128  std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3129  - SLocSpaceSize,&F));
3130 
3131  // Initialize the remapping table.
3132  // Invalid stays invalid.
3133  F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3134  // This module. Base was 2 when being compiled.
3135  F.SLocRemap.insertOrReplace(std::make_pair(2U,
3136  static_cast<int>(F.SLocEntryBaseOffset - 2)));
3137 
3138  TotalNumSLocEntries += F.LocalNumSLocEntries;
3139  break;
3140  }
3141 
3142  case MODULE_OFFSET_MAP:
3143  F.ModuleOffsetMap = Blob;
3144  break;
3145 
3147  if (ParseLineTable(F, Record))
3148  return Failure;
3149  break;
3150 
3151  case SOURCE_LOCATION_PRELOADS: {
3152  // Need to transform from the local view (1-based IDs) to the global view,
3153  // which is based off F.SLocEntryBaseID.
3154  if (!F.PreloadSLocEntries.empty()) {
3155  Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3156  return Failure;
3157  }
3158 
3159  F.PreloadSLocEntries.swap(Record);
3160  break;
3161  }
3162 
3163  case EXT_VECTOR_DECLS:
3164  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3165  ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3166  break;
3167 
3168  case VTABLE_USES:
3169  if (Record.size() % 3 != 0) {
3170  Error("Invalid VTABLE_USES record");
3171  return Failure;
3172  }
3173 
3174  // Later tables overwrite earlier ones.
3175  // FIXME: Modules will have some trouble with this. This is clearly not
3176  // the right way to do this.
3177  VTableUses.clear();
3178 
3179  for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3180  VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3181  VTableUses.push_back(
3182  ReadSourceLocation(F, Record, Idx).getRawEncoding());
3183  VTableUses.push_back(Record[Idx++]);
3184  }
3185  break;
3186 
3188  if (PendingInstantiations.size() % 2 != 0) {
3189  Error("Invalid existing PendingInstantiations");
3190  return Failure;
3191  }
3192 
3193  if (Record.size() % 2 != 0) {
3194  Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3195  return Failure;
3196  }
3197 
3198  for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3199  PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3200  PendingInstantiations.push_back(
3201  ReadSourceLocation(F, Record, I).getRawEncoding());
3202  }
3203  break;
3204 
3205  case SEMA_DECL_REFS:
3206  if (Record.size() != 3) {
3207  Error("Invalid SEMA_DECL_REFS block");
3208  return Failure;
3209  }
3210  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3211  SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3212  break;
3213 
3214  case PPD_ENTITIES_OFFSETS: {
3215  F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3216  assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3217  F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3218 
3219  unsigned LocalBasePreprocessedEntityID = Record[0];
3220 
3221  unsigned StartingID;
3222  if (!PP.getPreprocessingRecord())
3223  PP.createPreprocessingRecord();
3224  if (!PP.getPreprocessingRecord()->getExternalSource())
3225  PP.getPreprocessingRecord()->SetExternalSource(*this);
3226  StartingID
3227  = PP.getPreprocessingRecord()
3228  ->allocateLoadedEntities(F.NumPreprocessedEntities);
3229  F.BasePreprocessedEntityID = StartingID;
3230 
3231  if (F.NumPreprocessedEntities > 0) {
3232  // Introduce the global -> local mapping for preprocessed entities in
3233  // this module.
3234  GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3235 
3236  // Introduce the local -> global mapping for preprocessed entities in
3237  // this module.
3239  std::make_pair(LocalBasePreprocessedEntityID,
3240  F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3241  }
3242 
3243  break;
3244  }
3245 
3246  case PPD_SKIPPED_RANGES: {
3247  F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3248  assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3249  F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3250 
3251  if (!PP.getPreprocessingRecord())
3252  PP.createPreprocessingRecord();
3253  if (!PP.getPreprocessingRecord()->getExternalSource())
3254  PP.getPreprocessingRecord()->SetExternalSource(*this);
3255  F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3256  ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3257 
3258  if (F.NumPreprocessedSkippedRanges > 0)
3259  GlobalSkippedRangeMap.insert(
3260  std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3261  break;
3262  }
3263 
3264  case DECL_UPDATE_OFFSETS:
3265  if (Record.size() % 2 != 0) {
3266  Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3267  return Failure;
3268  }
3269  for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3270  GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3271  DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3272 
3273  // If we've already loaded the decl, perform the updates when we finish
3274  // loading this block.
3275  if (Decl *D = GetExistingDecl(ID))
3276  PendingUpdateRecords.push_back(
3277  PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3278  }
3279  break;
3280 
3281  case OBJC_CATEGORIES_MAP:
3282  if (F.LocalNumObjCCategoriesInMap != 0) {
3283  Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3284  return Failure;
3285  }
3286 
3287  F.LocalNumObjCCategoriesInMap = Record[0];
3288  F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3289  break;
3290 
3291  case OBJC_CATEGORIES:
3292  F.ObjCCategories.swap(Record);
3293  break;
3294 
3296  // Later tables overwrite earlier ones.
3297  // FIXME: Modules will have trouble with this.
3298  CUDASpecialDeclRefs.clear();
3299  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3300  CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3301  break;
3302 
3303  case HEADER_SEARCH_TABLE:
3304  F.HeaderFileInfoTableData = Blob.data();
3305  F.LocalNumHeaderFileInfos = Record[1];
3306  if (Record[0]) {
3309  (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3310  (const unsigned char *)F.HeaderFileInfoTableData,
3311  HeaderFileInfoTrait(*this, F,
3312  &PP.getHeaderSearchInfo(),
3313  Blob.data() + Record[2]));
3314 
3315  PP.getHeaderSearchInfo().SetExternalSource(this);
3316  if (!PP.getHeaderSearchInfo().getExternalLookup())
3317  PP.getHeaderSearchInfo().SetExternalLookup(this);
3318  }
3319  break;
3320 
3321  case FP_PRAGMA_OPTIONS:
3322  // Later tables overwrite earlier ones.
3323  FPPragmaOptions.swap(Record);
3324  break;
3325 
3326  case OPENCL_EXTENSIONS:
3327  for (unsigned I = 0, E = Record.size(); I != E; ) {
3328  auto Name = ReadString(Record, I);
3329  auto &Opt = OpenCLExtensions.OptMap[Name];
3330  Opt.Supported = Record[I++] != 0;
3331  Opt.Enabled = Record[I++] != 0;
3332  Opt.Avail = Record[I++];
3333  Opt.Core = Record[I++];
3334  }
3335  break;
3336 
3338  for (unsigned I = 0, E = Record.size(); I != E;) {
3339  auto TypeID = static_cast<::TypeID>(Record[I++]);
3340  auto *Type = GetType(TypeID).getTypePtr();
3341  auto NumExt = static_cast<unsigned>(Record[I++]);
3342  for (unsigned II = 0; II != NumExt; ++II) {
3343  auto Ext = ReadString(Record, I);
3344  OpenCLTypeExtMap[Type].insert(Ext);
3345  }
3346  }
3347  break;
3348 
3350  for (unsigned I = 0, E = Record.size(); I != E;) {
3351  auto DeclID = static_cast<::DeclID>(Record[I++]);
3352  auto *Decl = GetDecl(DeclID);
3353  auto NumExt = static_cast<unsigned>(Record[I++]);
3354  for (unsigned II = 0; II != NumExt; ++II) {
3355  auto Ext = ReadString(Record, I);
3356  OpenCLDeclExtMap[Decl].insert(Ext);
3357  }
3358  }
3359  break;
3360 
3361  case TENTATIVE_DEFINITIONS:
3362  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3363  TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3364  break;
3365 
3366  case KNOWN_NAMESPACES:
3367  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3368  KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3369  break;
3370 
3371  case UNDEFINED_BUT_USED:
3372  if (UndefinedButUsed.size() % 2 != 0) {
3373  Error("Invalid existing UndefinedButUsed");
3374  return Failure;
3375  }
3376 
3377  if (Record.size() % 2 != 0) {
3378  Error("invalid undefined-but-used record");
3379  return Failure;
3380  }
3381  for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3382  UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3383  UndefinedButUsed.push_back(
3384  ReadSourceLocation(F, Record, I).getRawEncoding());
3385  }
3386  break;
3387 
3389  for (unsigned I = 0, N = Record.size(); I != N;) {
3390  DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3391  const uint64_t Count = Record[I++];
3392  DelayedDeleteExprs.push_back(Count);
3393  for (uint64_t C = 0; C < Count; ++C) {
3394  DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3395  bool IsArrayForm = Record[I++] == 1;
3396  DelayedDeleteExprs.push_back(IsArrayForm);
3397  }
3398  }
3399  break;
3400 
3401  case IMPORTED_MODULES:
3402  if (!F.isModule()) {
3403  // If we aren't loading a module (which has its own exports), make
3404  // all of the imported modules visible.
3405  // FIXME: Deal with macros-only imports.
3406  for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3407  unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3408  SourceLocation Loc = ReadSourceLocation(F, Record, I);
3409  if (GlobalID) {
3410  ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3411  if (DeserializationListener)
3412  DeserializationListener->ModuleImportRead(GlobalID, Loc);
3413  }
3414  }
3415  }
3416  break;
3417 
3418  case MACRO_OFFSET: {
3419  if (F.LocalNumMacros != 0) {
3420  Error("duplicate MACRO_OFFSET record in AST file");
3421  return Failure;
3422  }
3423  F.MacroOffsets = (const uint32_t *)Blob.data();
3424  F.LocalNumMacros = Record[0];
3425  unsigned LocalBaseMacroID = Record[1];
3426  F.BaseMacroID = getTotalNumMacros();
3427 
3428  if (F.LocalNumMacros > 0) {
3429  // Introduce the global -> local mapping for macros within this module.
3430  GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3431 
3432  // Introduce the local -> global mapping for macros within this module.
3434  std::make_pair(LocalBaseMacroID,
3435  F.BaseMacroID - LocalBaseMacroID));
3436 
3437  MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3438  }
3439  break;
3440  }
3441 
3442  case LATE_PARSED_TEMPLATE:
3443  LateParsedTemplates.append(Record.begin(), Record.end());
3444  break;
3445 
3447  if (Record.size() != 1) {
3448  Error("invalid pragma optimize record");
3449  return Failure;
3450  }
3451  OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3452  break;
3453 
3455  if (Record.size() != 1) {
3456  Error("invalid pragma ms_struct record");
3457  return Failure;
3458  }
3459  PragmaMSStructState = Record[0];
3460  break;
3461 
3463  if (Record.size() != 2) {
3464  Error("invalid pragma ms_struct record");
3465  return Failure;
3466  }
3467  PragmaMSPointersToMembersState = Record[0];
3468  PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3469  break;
3470 
3472  for (unsigned I = 0, N = Record.size(); I != N; ++I)
3473  UnusedLocalTypedefNameCandidates.push_back(
3474  getGlobalDeclID(F, Record[I]));
3475  break;
3476 
3478  if (Record.size() != 1) {
3479  Error("invalid cuda pragma options record");
3480  return Failure;
3481  }
3482  ForceCUDAHostDeviceDepth = Record[0];
3483  break;
3484 
3485  case PACK_PRAGMA_OPTIONS: {
3486  if (Record.size() < 3) {
3487  Error("invalid pragma pack record");
3488  return Failure;
3489  }
3490  PragmaPackCurrentValue = Record[0];
3491  PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3492  unsigned NumStackEntries = Record[2];
3493  unsigned Idx = 3;
3494  // Reset the stack when importing a new module.
3495  PragmaPackStack.clear();
3496  for (unsigned I = 0; I < NumStackEntries; ++I) {
3497  PragmaPackStackEntry Entry;
3498  Entry.Value = Record[Idx++];
3499  Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3500  Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3501  PragmaPackStrings.push_back(ReadString(Record, Idx));
3502  Entry.SlotLabel = PragmaPackStrings.back();
3503  PragmaPackStack.push_back(Entry);
3504  }
3505  break;
3506  }
3507  }
3508  }
3509 }
3510 
3511 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3512  assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3513 
3514  // Additional remapping information.
3515  const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3516  const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3517  F.ModuleOffsetMap = StringRef();
3518 
3519  // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3520  if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3521  F.SLocRemap.insert(std::make_pair(0U, 0));
3522  F.SLocRemap.insert(std::make_pair(2U, 1));
3523  }
3524 
3525  // Continuous range maps we may be updating in our module.
3526  using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3527  RemapBuilder SLocRemap(F.SLocRemap);
3528  RemapBuilder IdentifierRemap(F.IdentifierRemap);
3529  RemapBuilder MacroRemap(F.MacroRemap);
3530  RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3531  RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3532  RemapBuilder SelectorRemap(F.SelectorRemap);
3533  RemapBuilder DeclRemap(F.DeclRemap);
3534  RemapBuilder TypeRemap(F.TypeRemap);
3535 
3536  while (Data < DataEnd) {
3537  // FIXME: Looking up dependency modules by filename is horrible. Let's
3538  // start fixing this with prebuilt and explicit modules and see how it
3539  // goes...
3540  using namespace llvm::support;
3541  ModuleKind Kind = static_cast<ModuleKind>(
3542  endian::readNext<uint8_t, little, unaligned>(Data));
3543  uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3544  StringRef Name = StringRef((const char*)Data, Len);
3545  Data += Len;
3546  ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3547  ? ModuleMgr.lookupByModuleName(Name)
3548  : ModuleMgr.lookupByFileName(Name));
3549  if (!OM) {
3550  std::string Msg =
3551  "SourceLocation remap refers to unknown module, cannot find ";
3552  Msg.append(Name);
3553  Error(Msg);
3554  return;
3555  }
3556 
3557  uint32_t SLocOffset =
3558  endian::readNext<uint32_t, little, unaligned>(Data);
3559  uint32_t IdentifierIDOffset =
3560  endian::readNext<uint32_t, little, unaligned>(Data);
3561  uint32_t MacroIDOffset =
3562  endian::readNext<uint32_t, little, unaligned>(Data);
3563  uint32_t PreprocessedEntityIDOffset =
3564  endian::readNext<uint32_t, little, unaligned>(Data);
3565  uint32_t SubmoduleIDOffset =
3566  endian::readNext<uint32_t, little, unaligned>(Data);
3567  uint32_t SelectorIDOffset =
3568  endian::readNext<uint32_t, little, unaligned>(Data);
3569  uint32_t DeclIDOffset =
3570  endian::readNext<uint32_t, little, unaligned>(Data);
3571  uint32_t TypeIndexOffset =
3572  endian::readNext<uint32_t, little, unaligned>(Data);
3573 
3574  uint32_t None = std::numeric_limits<uint32_t>::max();
3575 
3576  auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3577  RemapBuilder &Remap) {
3578  if (Offset != None)
3579  Remap.insert(std::make_pair(Offset,
3580  static_cast<int>(BaseOffset - Offset)));
3581  };
3582  mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3583  mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3584  mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3585  mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3586  PreprocessedEntityRemap);
3587  mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3588  mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3589  mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3590  mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3591 
3592  // Global -> local mappings.
3593  F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3594  }
3595 }
3596 
3598 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3599  const ModuleFile *ImportedBy,
3600  unsigned ClientLoadCapabilities) {
3601  unsigned Idx = 0;
3602  F.ModuleMapPath = ReadPath(F, Record, Idx);
3603 
3604  // Try to resolve ModuleName in the current header search context and
3605  // verify that it is found in the same module map file as we saved. If the
3606  // top-level AST file is a main file, skip this check because there is no
3607  // usable header search context.
3608  assert(!F.ModuleName.empty() &&
3609  "MODULE_NAME should come before MODULE_MAP_FILE");
3610  if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3611  // An implicitly-loaded module file should have its module listed in some
3612  // module map file that we've already loaded.
3613  Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3614  auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3615  const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3616  // Don't emit module relocation error if we have -fno-validate-pch
3617  if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3618  assert(ImportedBy && "top-level import should be verified");
3619  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3620  if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3621  // This module was defined by an imported (explicit) module.
3622  Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3623  << ASTFE->getName();
3624  } else {
3625  // This module was built with a different module map.
3626  Diag(diag::err_imported_module_not_found)
3627  << F.ModuleName << F.FileName << ImportedBy->FileName
3628  << F.ModuleMapPath;
3629  // In case it was imported by a PCH, there's a chance the user is
3630  // just missing to include the search path to the directory containing
3631  // the modulemap.
3632  if (ImportedBy->Kind == MK_PCH)
3633  Diag(diag::note_imported_by_pch_module_not_found)
3634  << llvm::sys::path::parent_path(F.ModuleMapPath);
3635  }
3636  }
3637  return OutOfDate;
3638  }
3639 
3640  assert(M->Name == F.ModuleName && "found module with different name");
3641 
3642  // Check the primary module map file.
3643  const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3644  if (StoredModMap == nullptr || StoredModMap != ModMap) {
3645  assert(ModMap && "found module is missing module map file");
3646  assert(ImportedBy && "top-level import should be verified");
3647  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3648  Diag(diag::err_imported_module_modmap_changed)
3649  << F.ModuleName << ImportedBy->FileName
3650  << ModMap->getName() << F.ModuleMapPath;
3651  return OutOfDate;
3652  }
3653 
3654  llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3655  for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3656  // FIXME: we should use input files rather than storing names.
3657  std::string Filename = ReadPath(F, Record, Idx);
3658  const FileEntry *F =
3659  FileMgr.getFile(Filename, false, false);
3660  if (F == nullptr) {
3661  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3662  Error("could not find file '" + Filename +"' referenced by AST file");
3663  return OutOfDate;
3664  }
3665  AdditionalStoredMaps.insert(F);
3666  }
3667 
3668  // Check any additional module map files (e.g. module.private.modulemap)
3669  // that are not in the pcm.
3670  if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3671  for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3672  // Remove files that match
3673  // Note: SmallPtrSet::erase is really remove
3674  if (!AdditionalStoredMaps.erase(ModMap)) {
3675  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3676  Diag(diag::err_module_different_modmap)
3677  << F.ModuleName << /*new*/0 << ModMap->getName();
3678  return OutOfDate;
3679  }
3680  }
3681  }
3682 
3683  // Check any additional module map files that are in the pcm, but not
3684  // found in header search. Cases that match are already removed.
3685  for (const FileEntry *ModMap : AdditionalStoredMaps) {
3686  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3687  Diag(diag::err_module_different_modmap)
3688  << F.ModuleName << /*not new*/1 << ModMap->getName();
3689  return OutOfDate;
3690  }
3691  }
3692 
3693  if (Listener)
3694  Listener->ReadModuleMapFile(F.ModuleMapPath);
3695  return Success;
3696 }
3697 
3698 /// Move the given method to the back of the global list of methods.
3700  // Find the entry for this selector in the method pool.
3701  Sema::GlobalMethodPool::iterator Known
3702  = S.MethodPool.find(Method->getSelector());
3703  if (Known == S.MethodPool.end())
3704  return;
3705 
3706  // Retrieve the appropriate method list.
3707  ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3708  : Known->second.second;
3709  bool Found = false;
3710  for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3711  if (!Found) {
3712  if (List->getMethod() == Method) {
3713  Found = true;
3714  } else {
3715  // Keep searching.
3716  continue;
3717  }
3718  }
3719 
3720  if (List->getNext())
3721  List->setMethod(List->getNext()->getMethod());
3722  else
3723  List->setMethod(Method);
3724  }
3725 }
3726 
3727 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3728  assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3729  for (Decl *D : Names) {
3730  bool wasHidden = D->isHidden();
3731  D->setVisibleDespiteOwningModule();
3732 
3733  if (wasHidden && SemaObj) {
3734  if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3735  moveMethodToBackOfGlobalList(*SemaObj, Method);
3736  }
3737  }
3738  }
3739 }
3740 
3742  Module::NameVisibilityKind NameVisibility,
3743  SourceLocation ImportLoc) {
3744  llvm::SmallPtrSet<Module *, 4> Visited;
3746  Stack.push_back(Mod);
3747  while (!Stack.empty()) {
3748  Mod = Stack.pop_back_val();
3749 
3750  if (NameVisibility <= Mod->NameVisibility) {
3751  // This module already has this level of visibility (or greater), so
3752  // there is nothing more to do.
3753  continue;
3754  }
3755 
3756  if (!Mod->isAvailable()) {
3757  // Modules that aren't available cannot be made visible.
3758  continue;
3759  }
3760 
3761  // Update the module's name visibility.
3762  Mod->NameVisibility = NameVisibility;
3763 
3764  // If we've already deserialized any names from this module,
3765  // mark them as visible.
3766  HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3767  if (Hidden != HiddenNamesMap.end()) {
3768  auto HiddenNames = std::move(*Hidden);
3769  HiddenNamesMap.erase(Hidden);
3770  makeNamesVisible(HiddenNames.second, HiddenNames.first);
3771  assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3772  "making names visible added hidden names");
3773  }
3774 
3775  // Push any exported modules onto the stack to be marked as visible.
3776  SmallVector<Module *, 16> Exports;
3777  Mod->getExportedModules(Exports);
3779  I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3780  Module *Exported = *I;
3781  if (Visited.insert(Exported).second)
3782  Stack.push_back(Exported);
3783  }
3784  }
3785 }
3786 
3787 /// We've merged the definition \p MergedDef into the existing definition
3788 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3789 /// visible.
3791  NamedDecl *MergedDef) {
3792  if (Def->isHidden()) {
3793  // If MergedDef is visible or becomes visible, make the definition visible.
3794  if (!MergedDef->isHidden())
3796  else {
3797  getContext().mergeDefinitionIntoModule(
3798  Def, MergedDef->getImportedOwningModule(),
3799  /*NotifyListeners*/ false);
3800  PendingMergedDefinitionsToDeduplicate.insert(Def);
3801  }
3802  }
3803 }
3804 
3806  if (GlobalIndex)
3807  return false;
3808 
3809  if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3810  !PP.getLangOpts().Modules)
3811  return true;
3812 
3813  // Try to load the global index.
3814  TriedLoadingGlobalIndex = true;
3815  StringRef ModuleCachePath
3816  = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3817  std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3818  = GlobalModuleIndex::readIndex(ModuleCachePath);
3819  if (!Result.first)
3820  return true;
3821 
3822  GlobalIndex.reset(Result.first);
3823  ModuleMgr.setGlobalIndex(GlobalIndex.get());
3824  return false;
3825 }
3826 
3828  return PP.getLangOpts().Modules && UseGlobalIndex &&
3829  !hasGlobalIndex() && TriedLoadingGlobalIndex;
3830 }
3831 
3833  // Overwrite the timestamp file contents so that file's mtime changes.
3834  std::string TimestampFilename = MF.getTimestampFilename();
3835  std::error_code EC;
3836  llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3837  if (EC)
3838  return;
3839  OS << "Timestamp file\n";
3840  OS.close();
3841  OS.clear_error(); // Avoid triggering a fatal error.
3842 }
3843 
3844 /// Given a cursor at the start of an AST file, scan ahead and drop the
3845 /// cursor into the start of the given block ID, returning false on success and
3846 /// true on failure.
3847 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3848  while (true) {
3849  llvm::BitstreamEntry Entry = Cursor.advance();
3850  switch (Entry.Kind) {
3852  case llvm::BitstreamEntry::EndBlock:
3853  return true;
3854 
3855  case llvm::BitstreamEntry::Record:
3856  // Ignore top-level records.
3857  Cursor.skipRecord(Entry.ID);
3858  break;
3859 
3860  case llvm::BitstreamEntry::SubBlock:
3861  if (Entry.ID == BlockID) {
3862  if (Cursor.EnterSubBlock(BlockID))
3863  return true;
3864  // Found it!
3865  return false;
3866  }
3867 
3868  if (Cursor.SkipBlock())
3869  return true;
3870  }
3871  }
3872 }
3873 
3875  ModuleKind Type,
3876  SourceLocation ImportLoc,
3877  unsigned ClientLoadCapabilities,
3880  SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3881 
3882  // Defer any pending actions until we get to the end of reading the AST file.
3883  Deserializing AnASTFile(this);
3884 
3885  // Bump the generation number.
3886  unsigned PreviousGeneration = 0;
3887  if (ContextObj)
3888  PreviousGeneration = incrementGeneration(*ContextObj);
3889 
3890  unsigned NumModules = ModuleMgr.size();
3892  switch (ASTReadResult ReadResult =
3893  ReadASTCore(FileName, Type, ImportLoc,
3894  /*ImportedBy=*/nullptr, Loaded, 0, 0,
3895  ASTFileSignature(), ClientLoadCapabilities)) {
3896  case Failure:
3897  case Missing:
3898  case OutOfDate:
3899  case VersionMismatch:
3900  case ConfigurationMismatch:
3901  case HadErrors: {
3902  llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3903  for (const ImportedModule &IM : Loaded)
3904  LoadedSet.insert(IM.Mod);
3905 
3906  ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
3907  PP.getLangOpts().Modules
3908  ? &PP.getHeaderSearchInfo().getModuleMap()
3909  : nullptr);
3910 
3911  // If we find that any modules are unusable, the global index is going
3912  // to be out-of-date. Just remove it.
3913  GlobalIndex.reset();
3914  ModuleMgr.setGlobalIndex(nullptr);
3915  return ReadResult;
3916  }
3917  case Success:
3918  break;
3919  }
3920 
3921  // Here comes stuff that we only do once the entire chain is loaded.
3922 
3923  // Load the AST blocks of all of the modules that we loaded.
3924  for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3925  MEnd = Loaded.end();
3926  M != MEnd; ++M) {
3927  ModuleFile &F = *M->Mod;
3928 
3929  // Read the AST block.
3930  if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3931  return Result;
3932 
3933  // Read the extension blocks.
3935  if (ASTReadResult Result = ReadExtensionBlock(F))
3936  return Result;
3937  }
3938 
3939  // Once read, set the ModuleFile bit base offset and update the size in
3940  // bits of all files we've seen.
3941  F.GlobalBitOffset = TotalModulesSizeInBits;
3942  TotalModulesSizeInBits += F.SizeInBits;
3943  GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3944 
3945  // Preload SLocEntries.
3946  for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3947  int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3948  // Load it through the SourceManager and don't call ReadSLocEntry()
3949  // directly because the entry may have already been loaded in which case
3950  // calling ReadSLocEntry() directly would trigger an assertion in
3951  // SourceManager.
3952  SourceMgr.getLoadedSLocEntryByID(Index);
3953  }
3954 
3955  // Map the original source file ID into the ID space of the current
3956  // compilation.
3957  if (F.OriginalSourceFileID.isValid()) {
3958  F.OriginalSourceFileID = FileID::get(
3959  F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
3960  }
3961 
3962  // Preload all the pending interesting identifiers by marking them out of
3963  // date.
3964  for (auto Offset : F.PreloadIdentifierOffsets) {
3965  const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3967 
3968  ASTIdentifierLookupTrait Trait(*this, F);
3969  auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3970  auto Key = Trait.ReadKey(Data, KeyDataLen.first);
3971  auto &II = PP.getIdentifierTable().getOwn(Key);
3972  II.setOutOfDate(true);
3973 
3974  // Mark this identifier as being from an AST file so that we can track
3975  // whether we need to serialize it.
3976  markIdentifierFromAST(*this, II);
3977 
3978  // Associate the ID with the identifier so that the writer can reuse it.
3979  auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3980  SetIdentifierInfo(ID, &II);
3981  }
3982  }
3983 
3984  // Setup the import locations and notify the module manager that we've
3985  // committed to these module files.
3986  for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3987  MEnd = Loaded.end();
3988  M != MEnd; ++M) {
3989  ModuleFile &F = *M->Mod;
3990 
3991  ModuleMgr.moduleFileAccepted(&F);
3992 
3993  // Set the import location.
3994  F.DirectImportLoc = ImportLoc;
3995  // FIXME: We assume that locations from PCH / preamble do not need
3996  // any translation.
3997  if (!M->ImportedBy)
3998  F.ImportLoc = M->ImportLoc;
3999  else
4000  F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4001  }
4002 
4003  if (!PP.getLangOpts().CPlusPlus ||
4004  (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4005  Type != MK_PrebuiltModule)) {
4006  // Mark all of the identifiers in the identifier table as being out of date,
4007  // so that various accessors know to check the loaded modules when the
4008  // identifier is used.
4009  //
4010  // For C++ modules, we don't need information on many identifiers (just
4011  // those that provide macros or are poisoned), so we mark all of
4012  // the interesting ones via PreloadIdentifierOffsets.
4013  for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4014  IdEnd = PP.getIdentifierTable().end();
4015  Id != IdEnd; ++Id)
4016  Id->second->setOutOfDate(true);
4017  }
4018  // Mark selectors as out of date.
4019  for (auto Sel : SelectorGeneration)
4020  SelectorOutOfDate[Sel.first] = true;
4021 
4022  // Resolve any unresolved module exports.
4023  for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4024  UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4025  SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4026  Module *ResolvedMod = getSubmodule(GlobalID);
4027 
4028  switch (Unresolved.Kind) {
4029  case UnresolvedModuleRef::Conflict:
4030  if (ResolvedMod) {
4031  Module::Conflict Conflict;
4032  Conflict.Other = ResolvedMod;
4033  Conflict.Message = Unresolved.String.str();
4034  Unresolved.Mod->Conflicts.push_back(Conflict);
4035  }
4036  continue;
4037 
4038  case UnresolvedModuleRef::Import:
4039  if (ResolvedMod)
4040  Unresolved.Mod->Imports.insert(ResolvedMod);
4041  continue;
4042 
4043  case UnresolvedModuleRef::Export:
4044  if (ResolvedMod || Unresolved.IsWildcard)
4045  Unresolved.Mod->Exports.push_back(
4046  Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4047  continue;
4048  }
4049  }
4050  UnresolvedModuleRefs.clear();
4051 
4052  if (Imported)
4053  Imported->append(ImportedModules.begin(),
4054  ImportedModules.end());
4055 
4056  // FIXME: How do we load the 'use'd modules? They may not be submodules.
4057  // Might be unnecessary as use declarations are only used to build the
4058  // module itself.
4059 
4060  if (ContextObj)
4061  InitializeContext();
4062 
4063  if (SemaObj)
4064  UpdateSema();
4065 
4066  if (DeserializationListener)
4067  DeserializationListener->ReaderInitialized(this);
4068 
4069  ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4070  if (PrimaryModule.OriginalSourceFileID.isValid()) {
4071  // If this AST file is a precompiled preamble, then set the
4072  // preamble file ID of the source manager to the file source file
4073  // from which the preamble was built.
4074  if (Type == MK_Preamble) {
4075  SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4076  } else if (Type == MK_MainFile) {
4077  SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4078  }
4079  }
4080 
4081  // For any Objective-C class definitions we have already loaded, make sure
4082  // that we load any additional categories.
4083  if (ContextObj) {
4084  for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4085  loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4086  ObjCClassesLoaded[I],
4087  PreviousGeneration);
4088  }
4089  }
4090 
4091  if (PP.getHeaderSearchInfo()
4092  .getHeaderSearchOpts()
4093  .ModulesValidateOncePerBuildSession) {
4094  // Now we are certain that the module and all modules it depends on are
4095  // up to date. Create or update timestamp files for modules that are
4096  // located in the module cache (not for PCH files that could be anywhere
4097  // in the filesystem).
4098  for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4099  ImportedModule &M = Loaded[I];
4100  if (M.Mod->Kind == MK_ImplicitModule) {
4101  updateModuleTimestamp(*M.Mod);
4102  }
4103  }
4104  }
4105 
4106  return Success;
4107 }
4108 
4109 static ASTFileSignature readASTFileSignature(StringRef PCH);
4110 
4111 /// Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
4112 static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
4113  return Stream.canSkipToPos(4) &&
4114  Stream.Read(8) == 'C' &&
4115  Stream.Read(8) == 'P' &&
4116  Stream.Read(8) == 'C' &&
4117  Stream.Read(8) == 'H';
4118 }
4119 
4121  switch (Kind) {
4122  case MK_PCH:
4123  return 0; // PCH
4124  case MK_ImplicitModule:
4125  case MK_ExplicitModule:
4126  case MK_PrebuiltModule:
4127  return 1; // module
4128  case MK_MainFile:
4129  case MK_Preamble:
4130  return 2; // main source file
4131  }
4132  llvm_unreachable("unknown module kind");
4133 }
4134 
4136 ASTReader::ReadASTCore(StringRef FileName,
4137  ModuleKind Type,
4138  SourceLocation ImportLoc,
4139  ModuleFile *ImportedBy,
4141  off_t ExpectedSize, time_t ExpectedModTime,
4142  ASTFileSignature ExpectedSignature,
4143  unsigned ClientLoadCapabilities) {
4144  ModuleFile *M;
4145  std::string ErrorStr;
4147  = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4148  getGeneration(), ExpectedSize, ExpectedModTime,
4149  ExpectedSignature, readASTFileSignature,
4150  M, ErrorStr);
4151 
4152  switch (AddResult) {
4154  Diag(diag::remark_module_import)
4155  << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4156  << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4157  return Success;
4158 
4160  // Load module file below.
4161  break;
4162 
4164  // The module file was missing; if the client can handle that, return
4165  // it.
4166  if (ClientLoadCapabilities & ARR_Missing)
4167  return Missing;
4168 
4169  // Otherwise, return an error.
4170  Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4171  << FileName << !ErrorStr.empty()
4172  << ErrorStr;
4173  return Failure;
4174 
4176  // We couldn't load the module file because it is out-of-date. If the
4177  // client can handle out-of-date, return it.
4178  if (ClientLoadCapabilities & ARR_OutOfDate)
4179  return OutOfDate;
4180 
4181  // Otherwise, return an error.
4182  Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4183  << FileName << !ErrorStr.empty()
4184  << ErrorStr;
4185  return Failure;
4186  }
4187 
4188  assert(M && "Missing module file");
4189 
4190  bool ShouldFinalizePCM = false;
4191  auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4192  auto &MC = getModuleManager().getModuleCache();
4193  if (ShouldFinalizePCM)
4194  MC.finalizePCM(FileName);
4195  else
4196  MC.tryToDropPCM(FileName);
4197  });
4198  ModuleFile &F = *M;
4199  BitstreamCursor &Stream = F.Stream;
4200  Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4201  F.SizeInBits = F.Buffer->getBufferSize() * 8;
4202 
4203  // Sniff for the signature.
4204  if (!startsWithASTFileMagic(Stream)) {
4205  Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
4206  << FileName;
4207  return Failure;
4208  }
4209 
4210  // This is used for compatibility with older PCH formats.
4211  bool HaveReadControlBlock = false;
4212  while (true) {
4213  llvm::BitstreamEntry Entry = Stream.advance();
4214 
4215  switch (Entry.Kind) {
4217  case llvm::BitstreamEntry::Record:
4218  case llvm::BitstreamEntry::EndBlock:
4219  Error("invalid record at top-level of AST file");
4220  return Failure;
4221 
4222  case llvm::BitstreamEntry::SubBlock:
4223  break;
4224  }
4225 
4226  switch (Entry.ID) {
4227  case CONTROL_BLOCK_ID:
4228  HaveReadControlBlock = true;
4229  switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4230  case Success:
4231  // Check that we didn't try to load a non-module AST file as a module.
4232  //
4233  // FIXME: Should we also perform the converse check? Loading a module as
4234  // a PCH file sort of works, but it's a bit wonky.
4235  if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4236  Type == MK_PrebuiltModule) &&
4237  F.ModuleName.empty()) {
4238  auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4239  if (Result != OutOfDate ||
4240  (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4241  Diag(diag::err_module_file_not_module) << FileName;
4242  return Result;
4243  }
4244  break;
4245 
4246  case Failure: return Failure;
4247  case Missing: return Missing;
4248  case OutOfDate: return OutOfDate;
4249  case VersionMismatch: return VersionMismatch;
4250  case ConfigurationMismatch: return ConfigurationMismatch;
4251  case HadErrors: return HadErrors;
4252  }
4253  break;
4254 
4255  case AST_BLOCK_ID:
4256  if (!HaveReadControlBlock) {
4257  if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4258  Diag(diag::err_pch_version_too_old);
4259  return VersionMismatch;
4260  }
4261 
4262  // Record that we've loaded this module.
4263  Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4264  ShouldFinalizePCM = true;
4265  return Success;
4266 
4268  // This block is handled using look-ahead during ReadControlBlock. We
4269  // shouldn't get here!
4270  Error("malformed block record in AST file");
4271  return Failure;
4272 
4273  default:
4274  if (Stream.SkipBlock()) {
4275  Error("malformed block record in AST file");
4276  return Failure;
4277  }
4278  break;
4279  }
4280  }
4281 
4282  llvm_unreachable("unexpected break; expected return");
4283 }
4284 
4286 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4287  unsigned ClientLoadCapabilities) {
4288  const HeaderSearchOptions &HSOpts =
4289  PP.getHeaderSearchInfo().getHeaderSearchOpts();
4290  bool AllowCompatibleConfigurationMismatch =
4292 
4293  ASTReadResult Result = readUnhashedControlBlockImpl(
4294  &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4295  Listener.get(),
4296  WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4297 
4298  // If F was directly imported by another module, it's implicitly validated by
4299  // the importing module.
4300  if (DisableValidation || WasImportedBy ||
4301  (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4302  return Success;
4303 
4304  if (Result == Failure) {
4305  Error("malformed block record in AST file");
4306  return Failure;
4307  }
4308 
4309  if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4310  // If this module has already been finalized in the ModuleCache, we're stuck
4311  // with it; we can only load a single version of each module.
4312  //
4313  // This can happen when a module is imported in two contexts: in one, as a
4314  // user module; in another, as a system module (due to an import from
4315  // another module marked with the [system] flag). It usually indicates a
4316  // bug in the module map: this module should also be marked with [system].
4317  //
4318  // If -Wno-system-headers (the default), and the first import is as a
4319  // system module, then validation will fail during the as-user import,
4320  // since -Werror flags won't have been validated. However, it's reasonable
4321  // to treat this consistently as a system module.
4322  //
4323  // If -Wsystem-headers, the PCM on disk was built with
4324  // -Wno-system-headers, and the first import is as a user module, then
4325  // validation will fail during the as-system import since the PCM on disk
4326  // doesn't guarantee that -Werror was respected. However, the -Werror
4327  // flags were checked during the initial as-user import.
4328  if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4329  Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4330  return Success;
4331  }
4332  }
4333 
4334  return Result;
4335 }
4336 
4337 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4338  ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4339  bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4340  bool ValidateDiagnosticOptions) {
4341  // Initialize a stream.
4342  BitstreamCursor Stream(StreamData);
4343 
4344  // Sniff for the signature.
4345  if (!startsWithASTFileMagic(Stream))
4346  return Failure;
4347 
4348  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4350  return Failure;
4351 
4352  // Read all of the records in the options block.
4353  RecordData Record;
4354  ASTReadResult Result = Success;
4355  while (true) {
4356  llvm::BitstreamEntry Entry = Stream.advance();
4357 
4358  switch (Entry.Kind) {
4360  case llvm::BitstreamEntry::SubBlock:
4361  return Failure;
4362 
4363  case llvm::BitstreamEntry::EndBlock:
4364  return Result;
4365 
4366  case llvm::BitstreamEntry::Record:
4367  // The interesting case.
4368  break;
4369  }
4370 
4371  // Read and process a record.
4372  Record.clear();
4373  switch (
4374  (UnhashedControlBlockRecordTypes)Stream.readRecord(Entry.ID, Record)) {
4375  case SIGNATURE:
4376  if (F)
4377  std::copy(Record.begin(), Record.end(), F->Signature.data());
4378  break;
4379  case DIAGNOSTIC_OPTIONS: {
4380  bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4381  if (Listener && ValidateDiagnosticOptions &&
4382  !AllowCompatibleConfigurationMismatch &&
4383  ParseDiagnosticOptions(Record, Complain, *Listener))
4384  Result = OutOfDate; // Don't return early. Read the signature.
4385  break;
4386  }
4387  case DIAG_PRAGMA_MAPPINGS:
4388  if (!F)
4389  break;
4390  if (F->PragmaDiagMappings.empty())
4391  F->PragmaDiagMappings.swap(Record);
4392  else
4393  F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4394  Record.begin(), Record.end());
4395  break;
4396  }
4397  }
4398 }
4399 
4400 /// Parse a record and blob containing module file extension metadata.
4402  const SmallVectorImpl<uint64_t> &Record,
4403  StringRef Blob,
4404  ModuleFileExtensionMetadata &Metadata) {
4405  if (Record.size() < 4) return true;
4406 
4407  Metadata.MajorVersion = Record[0];
4408  Metadata.MinorVersion = Record[1];
4409 
4410  unsigned BlockNameLen = Record[2];
4411  unsigned UserInfoLen = Record[3];
4412 
4413  if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4414 
4415  Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4416  Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4417  Blob.data() + BlockNameLen + UserInfoLen);
4418  return false;
4419 }
4420 
4421 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4422  BitstreamCursor &Stream = F.Stream;
4423 
4424  RecordData Record;
4425  while (true) {
4426  llvm::BitstreamEntry Entry = Stream.advance();
4427  switch (Entry.Kind) {
4428  case llvm::BitstreamEntry::SubBlock:
4429  if (Stream.SkipBlock())
4430  return Failure;
4431 
4432  continue;
4433 
4434  case llvm::BitstreamEntry::EndBlock:
4435  return Success;
4436 
4438  return HadErrors;
4439 
4440  case llvm::BitstreamEntry::Record:
4441  break;
4442  }
4443 
4444  Record.clear();
4445  StringRef Blob;
4446  unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4447  switch (RecCode) {
4448  case EXTENSION_METADATA: {
4449  ModuleFileExtensionMetadata Metadata;
4450  if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4451  return Failure;
4452 
4453  // Find a module file extension with this block name.
4454  auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4455  if (Known == ModuleFileExtensions.end()) break;
4456 
4457  // Form a reader.
4458  if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4459  F, Stream)) {
4460  F.ExtensionReaders.push_back(std::move(Reader));
4461  }
4462 
4463  break;
4464  }
4465  }
4466  }
4467 
4468  return Success;
4469 }
4470 
4472  assert(ContextObj && "no context to initialize");
4473  ASTContext &Context = *ContextObj;
4474 
4475  // If there's a listener, notify them that we "read" the translation unit.
4476  if (DeserializationListener)
4477  DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4478  Context.getTranslationUnitDecl());
4479 
4480  // FIXME: Find a better way to deal with collisions between these
4481  // built-in types. Right now, we just ignore the problem.
4482 
4483  // Load the special types.
4484  if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4485  if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4486  if (!Context.CFConstantStringTypeDecl)
4487  Context.setCFConstantStringType(GetType(String));
4488  }
4489 
4490  if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4491  QualType FileType = GetType(File);
4492  if (FileType.isNull()) {
4493  Error("FILE type is NULL");
4494  return;
4495  }
4496 
4497  if (!Context.FILEDecl) {
4498  if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4499  Context.setFILEDecl(Typedef->getDecl());
4500  else {
4501  const TagType *Tag = FileType->getAs<TagType>();
4502  if (!Tag) {
4503  Error("Invalid FILE type in AST file");
4504  return;
4505  }
4506  Context.setFILEDecl(Tag->getDecl());
4507  }
4508  }
4509  }
4510 
4511  if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4512  QualType Jmp_bufType = GetType(Jmp_buf);
4513  if (Jmp_bufType.isNull()) {
4514  Error("jmp_buf type is NULL");
4515  return;
4516  }
4517 
4518  if (!Context.jmp_bufDecl) {
4519  if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4520  Context.setjmp_bufDecl(Typedef->getDecl());
4521  else {
4522  const TagType *Tag = Jmp_bufType->getAs<TagType>();
4523  if (!Tag) {
4524  Error("Invalid jmp_buf type in AST file");
4525  return;
4526  }
4527  Context.setjmp_bufDecl(Tag->getDecl());
4528  }
4529  }
4530  }
4531 
4532  if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4533  QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4534  if (Sigjmp_bufType.isNull()) {
4535  Error("sigjmp_buf type is NULL");
4536  return;
4537  }
4538 
4539  if (!Context.sigjmp_bufDecl) {
4540  if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4541  Context.setsigjmp_bufDecl(Typedef->getDecl());
4542  else {
4543  const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4544  assert(Tag && "Invalid sigjmp_buf type in AST file");
4545  Context.setsigjmp_bufDecl(Tag->getDecl());
4546  }
4547  }
4548  }
4549 
4550  if (unsigned ObjCIdRedef
4551  = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4552  if (Context.ObjCIdRedefinitionType.isNull())
4553  Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4554  }
4555 
4556  if (unsigned ObjCClassRedef
4557  = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4558  if (Context.ObjCClassRedefinitionType.isNull())
4559  Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4560  }
4561 
4562  if (unsigned ObjCSelRedef
4563  = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4564  if (Context.ObjCSelRedefinitionType.isNull())
4565  Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4566  }
4567 
4568  if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4569  QualType Ucontext_tType = GetType(Ucontext_t);
4570  if (Ucontext_tType.isNull()) {
4571  Error("ucontext_t type is NULL");
4572  return;
4573  }
4574 
4575  if (!Context.ucontext_tDecl) {
4576  if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4577  Context.setucontext_tDecl(Typedef->getDecl());
4578  else {
4579  const TagType *Tag = Ucontext_tType->getAs<TagType>();
4580  assert(Tag && "Invalid ucontext_t type in AST file");
4581  Context.setucontext_tDecl(Tag->getDecl());
4582  }
4583  }
4584  }
4585  }
4586 
4587  ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4588 
4589  // If there were any CUDA special declarations, deserialize them.
4590  if (!CUDASpecialDeclRefs.empty()) {
4591  assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4592  Context.setcudaConfigureCallDecl(
4593  cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4594  }
4595 
4596  // Re-export any modules that were imported by a non-module AST file.
4597  // FIXME: This does not make macro-only imports visible again.
4598  for (auto &Import : ImportedModules) {
4599  if (Module *Imported = getSubmodule(Import.ID)) {
4600  makeModuleVisible(Imported, Module::AllVisible,
4601  /*ImportLoc=*/Import.ImportLoc);
4602  if (Import.ImportLoc.isValid())
4603  PP.makeModuleVisible(Imported, Import.ImportLoc);
4604  // FIXME: should we tell Sema to make the module visible too?
4605  }
4606  }
4607  ImportedModules.clear();
4608 }
4609 
4611  // Nothing to do for now.
4612 }
4613 
4614 /// Reads and return the signature record from \p PCH's control block, or
4615 /// else returns 0.
4617  BitstreamCursor Stream(PCH);
4618  if (!startsWithASTFileMagic(Stream))
4619  return ASTFileSignature();
4620 
4621  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4623  return ASTFileSignature();
4624 
4625  // Scan for SIGNATURE inside the diagnostic options block.
4626  ASTReader::RecordData Record;
4627  while (true) {
4628  llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4629  if (Entry.Kind != llvm::BitstreamEntry::Record)
4630  return ASTFileSignature();
4631 
4632  Record.clear();
4633  StringRef Blob;
4634  if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4635  return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4636  (uint32_t)Record[3], (uint32_t)Record[4]}}};
4637  }
4638 }
4639 
4640 /// Retrieve the name of the original source file name
4641 /// directly from the AST file, without actually loading the AST
4642 /// file.
4644  const std::string &ASTFileName, FileManager &FileMgr,
4645  const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4646  // Open the AST file.
4647  auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4648  if (!Buffer) {
4649  Diags.Report(diag::err_fe_unable_to_read_pch_file)
4650  << ASTFileName << Buffer.getError().message();
4651  return std::string();
4652  }
4653 
4654  // Initialize the stream
4655  BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4656 
4657  // Sniff for the signature.
4658  if (!startsWithASTFileMagic(Stream)) {
4659  Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4660  return std::string();
4661  }
4662 
4663  // Scan for the CONTROL_BLOCK_ID block.
4664  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4665  Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4666  return std::string();
4667  }
4668 
4669  // Scan for ORIGINAL_FILE inside the control block.
4670  RecordData Record;
4671  while (true) {
4672  llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4673  if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4674  return std::string();
4675 
4676  if (Entry.Kind != llvm::BitstreamEntry::Record) {
4677  Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4678  return std::string();
4679  }
4680 
4681  Record.clear();
4682  StringRef Blob;
4683  if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4684  return Blob.str();
4685  }
4686 }
4687 
4688 namespace {
4689 
4690  class SimplePCHValidator : public ASTReaderListener {
4691  const LangOptions &ExistingLangOpts;
4692  const TargetOptions &ExistingTargetOpts;
4693  const PreprocessorOptions &ExistingPPOpts;
4694  std::string ExistingModuleCachePath;
4695  FileManager &FileMgr;
4696 
4697  public:
4698  SimplePCHValidator(const LangOptions &ExistingLangOpts,
4699  const TargetOptions &ExistingTargetOpts,
4700  const PreprocessorOptions &ExistingPPOpts,
4701  StringRef ExistingModuleCachePath,
4702  FileManager &FileMgr)
4703  : ExistingLangOpts(ExistingLangOpts),
4704  ExistingTargetOpts(ExistingTargetOpts),
4705  ExistingPPOpts(ExistingPPOpts),
4706  ExistingModuleCachePath(ExistingModuleCachePath),
4707  FileMgr(FileMgr) {}
4708 
4709  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4710  bool AllowCompatibleDifferences) override {
4711  return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4712  AllowCompatibleDifferences);
4713  }
4714 
4715  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4716  bool AllowCompatibleDifferences) override {
4717  return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4718  AllowCompatibleDifferences);
4719  }
4720 
4721  bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4722  StringRef SpecificModuleCachePath,
4723  bool Complain) override {
4724  return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4725  ExistingModuleCachePath,
4726  nullptr, ExistingLangOpts);
4727  }
4728 
4729  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4730  bool Complain,
4731  std::string &SuggestedPredefines) override {
4732  return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4733  SuggestedPredefines, ExistingLangOpts);
4734  }
4735  };
4736 
4737 } // namespace
4738 
4740  StringRef Filename, FileManager &FileMgr,
4741  const PCHContainerReader &PCHContainerRdr,
4742  bool FindModuleFileExtensions,
4743  ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
4744  // Open the AST file.
4745  // FIXME: This allows use of the VFS; we do not allow use of the
4746  // VFS when actually loading a module.
4747  auto Buffer = FileMgr.getBufferForFile(Filename);
4748  if (!Buffer) {
4749  return true;
4750  }
4751 
4752  // Initialize the stream
4753  StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
4754  BitstreamCursor Stream(Bytes);
4755 
4756  // Sniff for the signature.
4757  if (!startsWithASTFileMagic(Stream))
4758  return true;
4759 
4760  // Scan for the CONTROL_BLOCK_ID block.
4761  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4762  return true;
4763 
4764  bool NeedsInputFiles = Listener.needsInputFileVisitation();
4765  bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4766  bool NeedsImports = Listener.needsImportVisitation();
4767  BitstreamCursor InputFilesCursor;
4768 
4769  RecordData Record;
4770  std::string ModuleDir;
4771  bool DoneWithControlBlock = false;
4772  while (!DoneWithControlBlock) {
4773  llvm::BitstreamEntry Entry = Stream.advance();
4774 
4775  switch (Entry.Kind) {
4776  case llvm::BitstreamEntry::SubBlock: {
4777  switch (Entry.ID) {
4778  case OPTIONS_BLOCK_ID: {
4779  std::string IgnoredSuggestedPredefines;
4780  if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4781  /*AllowCompatibleConfigurationMismatch*/ false,
4782  Listener, IgnoredSuggestedPredefines) != Success)
4783  return true;
4784  break;
4785  }
4786 
4787  case INPUT_FILES_BLOCK_ID:
4788  InputFilesCursor = Stream;
4789  if (Stream.SkipBlock() ||
4790  (NeedsInputFiles &&
4791  ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4792  return true;
4793  break;
4794 
4795  default:
4796  if (Stream.SkipBlock())
4797  return true;
4798  break;
4799  }
4800 
4801  continue;
4802  }
4803 
4804  case llvm::BitstreamEntry::EndBlock:
4805  DoneWithControlBlock = true;
4806  break;
4807 
4809  return true;
4810 
4811  case llvm::BitstreamEntry::Record:
4812  break;
4813  }
4814 
4815  if (DoneWithControlBlock) break;
4816 
4817  Record.clear();
4818  StringRef Blob;
4819  unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4820  switch ((ControlRecordTypes)RecCode) {
4821  case METADATA:
4822  if (Record[0] != VERSION_MAJOR)
4823  return true;
4824  if (Listener.ReadFullVersionInformation(Blob))
4825  return true;
4826  break;
4827  case MODULE_NAME:
4828  Listener.ReadModuleName(Blob);
4829  break;
4830  case MODULE_DIRECTORY:
4831  ModuleDir = Blob;
4832  break;
4833  case MODULE_MAP_FILE: {
4834  unsigned Idx = 0;
4835  auto Path = ReadString(Record, Idx);
4836  ResolveImportedPath(Path, ModuleDir);
4837  Listener.ReadModuleMapFile(Path);
4838  break;
4839  }
4840  case INPUT_FILE_OFFSETS: {
4841  if (!NeedsInputFiles)
4842  break;
4843 
4844  unsigned NumInputFiles = Record[0];
4845  unsigned NumUserFiles = Record[1];
4846  const llvm::support::unaligned_uint64_t *InputFileOffs =
4847  (const llvm::support::unaligned_uint64_t *)Blob.data();
4848  for (unsigned I = 0; I != NumInputFiles; ++I) {
4849  // Go find this input file.
4850  bool isSystemFile = I >= NumUserFiles;
4851 
4852  if (isSystemFile && !NeedsSystemInputFiles)
4853  break; // the rest are system input files
4854 
4855  BitstreamCursor &Cursor = InputFilesCursor;
4856  SavedStreamPosition SavedPosition(Cursor);
4857  Cursor.JumpToBit(InputFileOffs[I]);
4858 
4859  unsigned Code = Cursor.ReadCode();
4860  RecordData Record;
4861  StringRef Blob;
4862  bool shouldContinue = false;
4863  switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4864  case INPUT_FILE:
4865  bool Overridden = static_cast<bool>(Record[3]);
4866  std::string Filename = Blob;
4867  ResolveImportedPath(Filename, ModuleDir);
4868  shouldContinue = Listener.visitInputFile(
4869  Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
4870  break;
4871  }
4872  if (!shouldContinue)
4873  break;
4874  }
4875  break;
4876  }
4877 
4878  case IMPORTS: {
4879  if (!NeedsImports)
4880  break;
4881 
4882  unsigned Idx = 0, N = Record.size();
4883  while (Idx < N) {
4884  // Read information about the AST file.
4885  Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
4886  std::string ModuleName = ReadString(Record, Idx);
4887  std::string Filename = ReadString(Record, Idx);
4888  ResolveImportedPath(Filename, ModuleDir);
4889  Listener.visitImport(ModuleName, Filename);
4890  }
4891  break;
4892  }
4893 
4894  default:
4895  // No other validation to perform.
4896  break;
4897  }
4898  }
4899 
4900  // Look for module file extension blocks, if requested.
4901  if (FindModuleFileExtensions) {
4902  BitstreamCursor SavedStream = Stream;
4903  while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4904  bool DoneWithExtensionBlock = false;
4905  while (!DoneWithExtensionBlock) {
4906  llvm::BitstreamEntry Entry = Stream.advance();
4907 
4908  switch (Entry.Kind) {
4909  case llvm::BitstreamEntry::SubBlock:
4910  if (Stream.SkipBlock())
4911  return true;
4912 
4913  continue;
4914 
4915  case llvm::BitstreamEntry::EndBlock:
4916  DoneWithExtensionBlock = true;
4917  continue;
4918 
4920  return true;
4921 
4922  case llvm::BitstreamEntry::Record:
4923  break;
4924  }
4925 
4926  Record.clear();
4927  StringRef Blob;
4928  unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4929  switch (RecCode) {
4930  case EXTENSION_METADATA: {
4931  ModuleFileExtensionMetadata Metadata;
4932  if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4933  return true;
4934 
4935  Listener.readModuleFileExtension(Metadata);
4936  break;
4937  }
4938  }
4939  }
4940  }
4941  Stream = SavedStream;
4942  }
4943 
4944  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4945  if (readUnhashedControlBlockImpl(
4946  nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
4947  /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
4948  ValidateDiagnosticOptions) != Success)
4949  return true;
4950 
4951  return false;
4952 }
4953 
4955  const PCHContainerReader &PCHContainerRdr,
4956  const LangOptions &LangOpts,
4957  const TargetOptions &TargetOpts,
4958  const PreprocessorOptions &PPOpts,
4959  StringRef ExistingModuleCachePath) {
4960  SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4961  ExistingModuleCachePath, FileMgr);
4962  return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
4963  /*FindModuleFileExtensions=*/false,
4964  validator,
4965  /*ValidateDiagnosticOptions=*/true);
4966 }
4967 
4969 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4970  // Enter the submodule block.
4971  if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4972  Error("malformed submodule block record in AST file");
4973  return Failure;
4974  }
4975 
4976  ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4977  bool First = true;
4978  Module *CurrentModule = nullptr;
4979  RecordData Record;
4980  while (true) {
4981  llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4982 
4983  switch (Entry.Kind) {
4984  case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4986  Error("malformed block record in AST file");
4987  return Failure;
4988  case llvm::BitstreamEntry::EndBlock:
4989  return Success;
4990  case llvm::BitstreamEntry::Record:
4991  // The interesting case.
4992  break;
4993  }
4994 
4995  // Read a record.
4996  StringRef Blob;
4997  Record.clear();
4998  auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4999 
5000  if ((Kind == SUBMODULE_METADATA) != First) {
5001  Error("submodule metadata record should be at beginning of block");
5002  return Failure;
5003  }
5004  First = false;
5005 
5006  // Submodule information is only valid if we have a current module.
5007  // FIXME: Should we error on these cases?
5008  if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5010  continue;
5011 
5012  switch (Kind) {
5013  default: // Default behavior: ignore.
5014  break;
5015 
5016  case SUBMODULE_DEFINITION: {
5017  if (Record.size() < 12) {
5018  Error("malformed module definition");
5019  return Failure;
5020  }
5021 
5022  StringRef Name = Blob;
5023  unsigned Idx = 0;
5024  SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5025  SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5026  Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5027  bool IsFramework = Record[Idx++];
5028  bool IsExplicit = Record[Idx++];
5029  bool IsSystem = Record[Idx++];
5030  bool IsExternC = Record[Idx++];
5031  bool InferSubmodules = Record[Idx++];
5032  bool InferExplicitSubmodules = Record[Idx++];
5033  bool InferExportWildcard = Record[Idx++];
5034  bool ConfigMacrosExhaustive = Record[Idx++];
5035  bool ModuleMapIsPrivate = Record[Idx++];
5036 
5037  Module *ParentModule = nullptr;
5038  if (Parent)
5039  ParentModule = getSubmodule(Parent);
5040 
5041  // Retrieve this (sub)module from the module map, creating it if
5042  // necessary.
5043  CurrentModule =
5044  ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5045  .first;
5046 
5047  // FIXME: set the definition loc for CurrentModule, or call
5048  // ModMap.setInferredModuleAllowedBy()
5049 
5050  SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5051  if (GlobalIndex >= SubmodulesLoaded.size() ||
5052  SubmodulesLoaded[GlobalIndex]) {
5053  Error("too many submodules");
5054  return Failure;
5055  }
5056 
5057  if (!ParentModule) {
5058  if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5059  // Don't emit module relocation error if we have -fno-validate-pch
5060  if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5061  CurFile != F.File) {
5062  if (!Diags.isDiagnosticInFlight()) {
5063  Diag(diag::err_module_file_conflict)
5064  << CurrentModule->getTopLevelModuleName()
5065  << CurFile->getName()
5066  << F.File->getName();
5067  }
5068  return Failure;
5069  }
5070  }
5071 
5072  CurrentModule->setASTFile(F.File);
5073  CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5074  }
5075 
5076  CurrentModule->Kind = Kind;
5077  CurrentModule->Signature = F.Signature;
5078  CurrentModule->IsFromModuleFile = true;
5079  CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5080  CurrentModule->IsExternC = IsExternC;
5081  CurrentModule->InferSubmodules = InferSubmodules;
5082  CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5083  CurrentModule->InferExportWildcard = InferExportWildcard;
5084  CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5085  CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5086  if (DeserializationListener)
5087  DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5088 
5089  SubmodulesLoaded[GlobalIndex] = CurrentModule;
5090 
5091  // Clear out data that will be replaced by what is in the module file.
5092  CurrentModule->LinkLibraries.clear();
5093  CurrentModule->ConfigMacros.clear();
5094  CurrentModule->UnresolvedConflicts.clear();
5095  CurrentModule->Conflicts.clear();
5096 
5097  // The module is available unless it's missing a requirement; relevant
5098  // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5099  // Missing headers that were present when the module was built do not
5100  // make it unavailable -- if we got this far, this must be an explicitly
5101  // imported module file.
5102  CurrentModule->Requirements.clear();
5103  CurrentModule->MissingHeaders.clear();
5104  CurrentModule->IsMissingRequirement =
5105  ParentModule && ParentModule->IsMissingRequirement;
5106  CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5107  break;
5108  }
5109 
5111  std::string Filename = Blob;
5112  ResolveImportedPath(F, Filename);
5113  if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
5114  if (!CurrentModule->getUmbrellaHeader())
5115  ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
5116  else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
5117  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5118  Error("mismatched umbrella headers in submodule");
5119  return OutOfDate;
5120  }
5121  }
5122  break;
5123  }
5124 
5125  case SUBMODULE_HEADER:
5128  // We lazily associate headers with their modules via the HeaderInfo table.
5129  // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5130  // of complete filenames or remove it entirely.
5131  break;
5132 
5135  // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5136  // them here.
5137  break;
5138 
5139  case SUBMODULE_TOPHEADER:
5140  CurrentModule->addTopHeaderFilename(Blob);
5141  break;
5142 
5143  case SUBMODULE_UMBRELLA_DIR: {
5144  std::string Dirname = Blob;
5145  ResolveImportedPath(F, Dirname);
5146  if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5147  if (!CurrentModule->getUmbrellaDir())
5148  ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
5149  else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
5150  if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5151  Error("mismatched umbrella directories in submodule");
5152  return OutOfDate;
5153  }
5154  }
5155  break;
5156  }
5157 
5158  case SUBMODULE_METADATA: {
5159  F.BaseSubmoduleID = getTotalNumSubmodules();
5160  F.LocalNumSubmodules = Record[0];
5161  unsigned LocalBaseSubmoduleID = Record[1];
5162  if (F.LocalNumSubmodules > 0) {
5163  // Introduce the global -> local mapping for submodules within this
5164  // module.
5165  GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5166 
5167  // Introduce the local -> global mapping for submodules within this
5168  // module.
5170  std::make_pair(LocalBaseSubmoduleID,
5171  F.BaseSubmoduleID - LocalBaseSubmoduleID));
5172 
5173  SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5174  }
5175  break;
5176  }
5177 
5178  case SUBMODULE_IMPORTS:
5179  for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5180  UnresolvedModuleRef Unresolved;
5181  Unresolved.File = &F;
5182  Unresolved.Mod = CurrentModule;
5183  Unresolved.ID = Record[Idx];
5184  Unresolved.Kind = UnresolvedModuleRef::Import;
5185  Unresolved.IsWildcard = false;
5186  UnresolvedModuleRefs.push_back(Unresolved);
5187  }
5188  break;
5189 
5190  case SUBMODULE_EXPORTS:
5191  for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5192  UnresolvedModuleRef Unresolved;
5193  Unresolved.File = &F;
5194  Unresolved.Mod = CurrentModule;
5195  Unresolved.ID = Record[Idx];
5196  Unresolved.Kind = UnresolvedModuleRef::Export;
5197  Unresolved.IsWildcard = Record[Idx + 1];
5198  UnresolvedModuleRefs.push_back(Unresolved);
5199  }
5200 
5201  // Once we've loaded the set of exports, there's no reason to keep
5202  // the parsed, unresolved exports around.
5203  CurrentModule->UnresolvedExports.clear();
5204  break;
5205 
5206  case SUBMODULE_REQUIRES:
5207  CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5208  PP.getTargetInfo());
5209  break;
5210 
5212  ModMap.resolveLinkAsDependencies(CurrentModule);
5213  CurrentModule->LinkLibraries.push_back(
5214  Module::LinkLibrary(Blob, Record[0]));
5215  break;
5216 
5218  CurrentModule->ConfigMacros.push_back(Blob.str());
5219  break;
5220 
5221  case SUBMODULE_CONFLICT: {
5222  UnresolvedModuleRef Unresolved;
5223  Unresolved.File = &F;
5224  Unresolved.Mod = CurrentModule;
5225  Unresolved.ID = Record[0];
5226  Unresolved.Kind = UnresolvedModuleRef::Conflict;
5227  Unresolved.IsWildcard = false;
5228  Unresolved.String = Blob;
5229  UnresolvedModuleRefs.push_back(Unresolved);
5230  break;
5231  }
5232 
5233  case SUBMODULE_INITIALIZERS: {
5234  if (!ContextObj)
5235  break;
5237  for (auto &ID : Record)
5238  Inits.push_back(getGlobalDeclID(F, ID));
5239  ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5240  break;
5241  }
5242 
5243  case SUBMODULE_EXPORT_AS:
5244  CurrentModule->ExportAsModule = Blob.str();
5245  ModMap.addLinkAsDependency(CurrentModule);
5246  break;
5247  }
5248  }
5249 }
5250 
5251 /// Parse the record that corresponds to a LangOptions data
5252 /// structure.
5253 ///
5254 /// This routine parses the language options from the AST file and then gives
5255 /// them to the AST listener if one is set.
5256 ///
5257 /// \returns true if the listener deems the file unacceptable, false otherwise.
5258 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5259  bool Complain,
5260  ASTReaderListener &Listener,
5261  bool AllowCompatibleDifferences) {
5262  LangOptions LangOpts;
5263  unsigned Idx = 0;
5264 #define LANGOPT(Name, Bits, Default, Description) \
5265  LangOpts.Name = Record[Idx++];
5266 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5267  LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5268 #include "clang/Basic/LangOptions.def"
5269 #define SANITIZER(NAME, ID) \
5270  LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5271 #include "clang/Basic/Sanitizers.def"
5272 
5273  for (unsigned N = Record[Idx++]; N; --N)
5274  LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5275 
5276  ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5277  VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5278  LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5279 
5280  LangOpts.CurrentModule = ReadString(Record, Idx);
5281 
5282  // Comment options.
5283  for (unsigned N = Record[Idx++]; N; --N) {
5284  LangOpts.CommentOpts.BlockCommandNames.push_back(
5285  ReadString(Record, Idx));
5286  }
5287  LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5288 
5289  // OpenMP offloading options.
5290  for (unsigned N = Record[Idx++]; N; --N) {
5291  LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5292  }
5293 
5294  LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5295 
5296  return Listener.ReadLanguageOptions(LangOpts, Complain,
5297  AllowCompatibleDifferences);
5298 }
5299 
5300 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5301  ASTReaderListener &Listener,
5302  bool AllowCompatibleDifferences) {
5303  unsigned Idx = 0;
5304  TargetOptions TargetOpts;
5305  TargetOpts.Triple = ReadString(Record, Idx);
5306  TargetOpts.CPU = ReadString(Record, Idx);
5307  TargetOpts.ABI = ReadString(Record, Idx);
5308  for (unsigned N = Record[Idx++]; N; --N) {
5309  TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5310  }
5311  for (unsigned N = Record[Idx++]; N; --N) {
5312  TargetOpts.Features.push_back(ReadString(Record, Idx));
5313  }
5314 
5315  return Listener.ReadTargetOptions(TargetOpts, Complain,
5316  AllowCompatibleDifferences);
5317 }
5318 
5319 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5320  ASTReaderListener &Listener) {
5322  unsigned Idx = 0;
5323 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5324 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5325  DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5326 #include "clang/Basic/DiagnosticOptions.def"
5327 
5328  for (unsigned N = Record[Idx++]; N; --N)
5329  DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5330  for (unsigned N = Record[Idx++]; N; --N)
5331  DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5332 
5333  return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5334 }
5335 
5336 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5337  ASTReaderListener &Listener) {
5338  FileSystemOptions FSOpts;
5339  unsigned Idx = 0;
5340  FSOpts.WorkingDir = ReadString(Record, Idx);
5341  return Listener.ReadFileSystemOptions(FSOpts, Complain);
5342 }
5343 
5344 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5345  bool Complain,
5346  ASTReaderListener &Listener) {
5347  HeaderSearchOptions HSOpts;
5348  unsigned Idx = 0;
5349  HSOpts.Sysroot = ReadString(Record, Idx);
5350 
5351  // Include entries.
5352  for (unsigned N = Record[Idx++]; N; --N) {
5353  std::string Path = ReadString(Record, Idx);
5355  = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5356  bool IsFramework = Record[Idx++];
5357  bool IgnoreSysRoot = Record[Idx++];
5358  HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5359  IgnoreSysRoot);
5360  }
5361 
5362  // System header prefixes.
5363  for (unsigned N = Record[Idx++]; N; --N) {
5364  std::string Prefix = ReadString(Record, Idx);
5365  bool IsSystemHeader = Record[Idx++];
5366  HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5367  }
5368 
5369  HSOpts.ResourceDir = ReadString(Record, Idx);
5370  HSOpts.ModuleCachePath = ReadString(Record, Idx);
5371  HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5372  HSOpts.DisableModuleHash = Record[Idx++];
5373  HSOpts.ImplicitModuleMaps = Record[Idx++];
5374  HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5375  HSOpts.UseBuiltinIncludes = Record[Idx++];
5376  HSOpts.UseStandardSystemIncludes = Record[Idx++];
5377  HSOpts.UseStandardCXXIncludes = Record[Idx++];
5378  HSOpts.UseLibcxx = Record[Idx++];
5379  std::string SpecificModuleCachePath = ReadString(Record, Idx);
5380 
5381  return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5382  Complain);
5383 }
5384 
5385 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5386  bool Complain,
5387  ASTReaderListener &Listener,
5388  std::string &SuggestedPredefines) {
5389  PreprocessorOptions PPOpts;
5390  unsigned Idx = 0;
5391 
5392  // Macro definitions/undefs
5393  for (unsigned N = Record[Idx++]; N; --N) {
5394  std::string Macro = ReadString(Record, Idx);
5395  bool IsUndef = Record[Idx++];
5396  PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5397  }
5398 
5399  // Includes
5400  for (unsigned N = Record[Idx++]; N; --N) {
5401  PPOpts.Includes.push_back(ReadString(Record, Idx));
5402  }
5403 
5404  // Macro Includes
5405  for (unsigned N = Record[Idx++]; N; --N) {
5406  PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5407  }
5408 
5409  PPOpts.UsePredefines = Record[Idx++];
5410  PPOpts.DetailedRecord = Record[Idx++];
5411  PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5412  PPOpts.ObjCXXARCStandardLibrary =
5413  static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5414  SuggestedPredefines.clear();
5415  return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5416  SuggestedPredefines);
5417 }
5418 
5419 std::pair<ModuleFile *, unsigned>
5420 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5422  I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5423  assert(I != GlobalPreprocessedEntityMap.end() &&
5424  "Corrupted global preprocessed entity map");
5425  ModuleFile *M = I->second;
5426  unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5427  return std::make_pair(M, LocalIndex);
5428 }
5429 
5430 llvm::iterator_range<PreprocessingRecord::iterator>
5431 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5432  if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5433  return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5435 
5436  return llvm::make_range(PreprocessingRecord::iterator(),
5438 }
5439 
5440 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5442  return llvm::make_range(
5443  ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5444  ModuleDeclIterator(this, &Mod,
5445  Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5446 }
5447 
5449  auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5450  assert(I != GlobalSkippedRangeMap.end() &&
5451  "Corrupted global skipped range map");
5452  ModuleFile *M = I->second;
5453  unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5454  assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5455  PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5456  SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5457  TranslateSourceLocation(*M, RawRange.getEnd()));
5458  assert(Range.isValid());
5459  return Range;
5460 }
5461 
5463  PreprocessedEntityID PPID = Index+1;
5464  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5465  ModuleFile &M = *PPInfo.first;
5466  unsigned LocalIndex = PPInfo.second;
5467  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5468 
5469  if (!PP.getPreprocessingRecord()) {
5470  Error("no preprocessing record");
5471  return nullptr;
5472  }
5473 
5475  M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
5476 
5477  llvm::BitstreamEntry Entry =
5478  M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5479  if (Entry.Kind != llvm::BitstreamEntry::Record)
5480  return nullptr;
5481 
5482  // Read the record.
5483  SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5484  TranslateSourceLocation(M, PPOffs.getEnd()));
5485  PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5486  StringRef Blob;
5487  RecordData Record;
5490  Entry.ID, Record, &Blob);
5491  switch (RecType) {
5492  case PPD_MACRO_EXPANSION: {
5493  bool isBuiltin = Record[0];
5494  IdentifierInfo *Name = nullptr;
5495  MacroDefinitionRecord *Def = nullptr;
5496  if (isBuiltin)
5497  Name = getLocalIdentifier(M, Record[1]);
5498  else {
5499  PreprocessedEntityID GlobalID =
5500  getGlobalPreprocessedEntityID(M, Record[1]);
5501  Def = cast<MacroDefinitionRecord>(
5502  PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5503  }
5504 
5505  MacroExpansion *ME;
5506  if (isBuiltin)
5507  ME = new (PPRec) MacroExpansion(Name, Range);
5508  else
5509  ME = new (PPRec) MacroExpansion(Def, Range);
5510 
5511  return ME;
5512  }
5513 
5514  case PPD_MACRO_DEFINITION: {
5515  // Decode the identifier info and then check again; if the macro is
5516  // still defined and associated with the identifier,
5517  IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5518  MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5519 
5520  if (DeserializationListener)
5521  DeserializationListener->MacroDefinitionRead(PPID, MD);
5522 
5523  return MD;
5524  }
5525 
5526  case PPD_INCLUSION_DIRECTIVE: {
5527  const char *FullFileNameStart = Blob.data() + Record[0];
5528  StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5529  const FileEntry *File = nullptr;
5530  if (!FullFileName.empty())
5531  File = PP.getFileManager().getFile(FullFileName);
5532 
5533  // FIXME: Stable encoding
5535  = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5537  = new (PPRec) InclusionDirective(PPRec, Kind,
5538  StringRef(Blob.data(), Record[0]),
5539  Record[1], Record[3],
5540  File,
5541  Range);
5542  return ID;
5543  }
5544  }
5545 
5546  llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5547 }
5548 
5549 /// Find the next module that contains entities and return the ID
5550 /// of the first entry.
5551 ///
5552 /// \param SLocMapI points at a chunk of a module that contains no
5553 /// preprocessed entities or the entities it contains are not the ones we are
5554 /// looking for.
5555 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5556  GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5557  ++SLocMapI;
5559  EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5560  ModuleFile &M = *SLocMapI->second;
5562  return M.BasePreprocessedEntityID;
5563  }
5564 
5565  return getTotalNumPreprocessedEntities();
5566 }
5567 
5568 namespace {
5569 
5570 struct PPEntityComp {
5571  const ASTReader &Reader;
5572  ModuleFile &M;
5573 
5574  PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5575 
5576  bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5577  SourceLocation LHS = getLoc(L);
5578  SourceLocation RHS = getLoc(R);
5579  return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5580  }
5581 
5582  bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5583  SourceLocation LHS = getLoc(L);
5584  return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5585  }
5586 
5587  bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5588  SourceLocation RHS = getLoc(R);
5589  return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5590  }
5591 
5592  SourceLocation getLoc(const PPEntityOffset &PPE) const {
5593  return Reader.TranslateSourceLocation(M, PPE.getBegin());
5594  }
5595 };
5596 
5597 } // namespace
5598 
5599 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5600  bool EndsAfter) const {
5601  if (SourceMgr.isLocalSourceLocation(Loc))
5602  return getTotalNumPreprocessedEntities();
5603 
5604  GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5605  SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5606  assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5607  "Corrupted global sloc offset map");
5608 
5609  if (SLocMapI->second->NumPreprocessedEntities == 0)
5610  return findNextPreprocessedEntity(SLocMapI);
5611 
5612  ModuleFile &M = *SLocMapI->second;
5613 
5614  using pp_iterator = const PPEntityOffset *;
5615 
5616  pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5617  pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5618 
5619  size_t Count = M.NumPreprocessedEntities;
5620  size_t Half;
5621  pp_iterator First = pp_begin;
5622  pp_iterator PPI;
5623 
5624  if (EndsAfter) {
5625  PPI = std::upper_bound(pp_begin, pp_end, Loc,
5626  PPEntityComp(*this, M));
5627  } else {
5628  // Do a binary search manually instead of using std::lower_bound because
5629  // The end locations of entities may be unordered (when a macro expansion
5630  // is inside another macro argument), but for this case it is not important
5631  // whether we get the first macro expansion or its containing macro.
5632  while (Count > 0) {
5633  Half = Count / 2;
5634  PPI = First;
5635  std::advance(PPI, Half);
5636  if (SourceMgr.isBeforeInTranslationUnit(
5637  TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
5638  First = PPI;
5639  ++First;
5640  Count = Count - Half - 1;
5641  } else
5642  Count = Half;
5643  }
5644  }
5645 
5646  if (PPI == pp_end)
5647  return findNextPreprocessedEntity(SLocMapI);
5648 
5649  return M.BasePreprocessedEntityID + (PPI - pp_begin);
5650 }
5651 
5652 /// Returns a pair of [Begin, End) indices of preallocated
5653 /// preprocessed entities that \arg Range encompasses.
5654 std::pair<unsigned, unsigned>
5656  if (Range.isInvalid())
5657  return std::make_pair(0,0);
5658  assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5659 
5660  PreprocessedEntityID BeginID =
5661  findPreprocessedEntity(Range.getBegin(), false);
5662  PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
5663  return std::make_pair(BeginID, EndID);
5664 }
5665 
5666 /// Optionally returns true or false if the preallocated preprocessed
5667 /// entity with index \arg Index came from file \arg FID.
5669  FileID FID) {
5670  if (FID.isInvalid())
5671  return false;
5672 
5673  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5674  ModuleFile &M = *PPInfo.first;
5675  unsigned LocalIndex = PPInfo.second;
5676  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5677 
5678  SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
5679  if (Loc.isInvalid())
5680  return false;
5681 
5682  if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5683  return true;
5684  else
5685  return false;
5686 }
5687 
5688 namespace {
5689 
5690  /// Visitor used to search for information about a header file.
5691  class HeaderFileInfoVisitor {
5692  const FileEntry *FE;
5694 
5695  public:
5696  explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
5697 
5698  bool operator()(ModuleFile &M) {
5700  = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5701  if (!Table)
5702  return false;
5703 
5704  // Look in the on-disk hash table for an entry for this file name.
5705  HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
5706  if (Pos == Table->end())
5707  return false;
5708 
5709  HFI = *Pos;
5710  return true;
5711  }
5712 
5713  Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
5714  };
5715 
5716 } // namespace
5717 
5719  HeaderFileInfoVisitor Visitor(FE);
5720  ModuleMgr.visit(Visitor);
5721  if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
5722  return *HFI;
5723 
5724  return HeaderFileInfo();
5725 }
5726 
5728  using DiagState = DiagnosticsEngine::DiagState;
5729  SmallVector<DiagState *, 32> DiagStates;
5730 
5731  for (ModuleFile &F : ModuleMgr) {
5732  unsigned Idx = 0;
5733  auto &Record = F.PragmaDiagMappings;
5734  if (Record.empty())
5735  continue;
5736 
5737  DiagStates.clear();
5738 
5739  auto ReadDiagState =
5740  [&](const DiagState &BasedOn, SourceLocation Loc,
5741  bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
5742  unsigned BackrefID = Record[Idx++];
5743  if (BackrefID != 0)
5744  return DiagStates[BackrefID - 1];
5745 
5746  // A new DiagState was created here.
5747  Diag.DiagStates.push_back(BasedOn);
5748  DiagState *NewState = &Diag.DiagStates.back();
5749  DiagStates.push_back(NewState);
5750  unsigned Size = Record[Idx++];
5751  assert(Idx + Size * 2 <= Record.size() &&
5752  "Invalid data, not enough diag/map pairs");
5753  while (Size--) {
5754  unsigned DiagID = Record[Idx++];
5755  DiagnosticMapping NewMapping =
5756  DiagnosticMapping::deserialize(Record[Idx++]);
5757  if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
5758  continue;
5759 
5760  DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
5761 
5762  // If this mapping was specified as a warning but the severity was
5763  // upgraded due to diagnostic settings, simulate the current diagnostic
5764  // settings (and use a warning).
5765  if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
5767  NewMapping.setUpgradedFromWarning(false);
5768  }
5769 
5770  Mapping = NewMapping;
5771  }
5772  return NewState;
5773  };
5774 
5775  // Read the first state.
5776  DiagState *FirstState;
5777  if (F.Kind == MK_ImplicitModule) {
5778  // Implicitly-built modules are reused with different diagnostic
5779  // settings. Use the initial diagnostic state from Diag to simulate this
5780  // compilation's diagnostic settings.
5781  FirstState = Diag.DiagStatesByLoc.FirstDiagState;
5782  DiagStates.push_back(FirstState);
5783 
5784  // Skip the initial diagnostic state from the serialized module.
5785  assert(Record[1] == 0 &&
5786  "Invalid data, unexpected backref in initial state");
5787  Idx = 3 + Record[2] * 2;
5788  assert(Idx < Record.size() &&
5789  "Invalid data, not enough state change pairs in initial state");
5790  } else if (F.isModule()) {
5791  // For an explicit module, preserve the flags from the module build
5792  // command line (-w, -Weverything, -Werror, ...) along with any explicit
5793  // -Wblah flags.
5794  unsigned Flags = Record[Idx++];
5795  DiagState Initial;
5796  Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
5797  Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
5798  Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
5799  Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
5800  Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
5801  Initial.ExtBehavior = (diag::Severity)Flags;
5802  FirstState = ReadDiagState(Initial, SourceLocation(), true);
5803 
5804  assert(F.OriginalSourceFileID.isValid());
5805 
5806  // Set up the root buffer of the module to start with the initial
5807  // diagnostic state of the module itself, to cover files that contain no
5808  // explicit transitions (for which we did not serialize anything).
5809  Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
5810  .StateTransitions.push_back({FirstState, 0});
5811  } else {
5812  // For prefix ASTs, start with whatever the user configured on the
5813  // command line.
5814  Idx++; // Skip flags.
5815  FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
5816  SourceLocation(), false);
5817  }
5818 
5819  // Read the state transitions.
5820  unsigned NumLocations = Record[Idx++];
5821  while (NumLocations--) {
5822  assert(Idx < Record.size() &&
5823  "Invalid data, missing pragma diagnostic states");
5824  SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
5825  auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
5826  assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
5827  assert(IDAndOffset.second == 0 && "not a start location for a FileID");
5828  unsigned Transitions = Record[Idx++];
5829 
5830  // Note that we don't need to set up Parent/ParentOffset here, because
5831  // we won't be changing the diagnostic state within imported FileIDs
5832  // (other than perhaps appending to the main source file, which has no
5833  // parent).
5834  auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
5835  F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
5836  for (unsigned I = 0; I != Transitions; ++I) {
5837  unsigned Offset = Record[Idx++];
5838  auto *State =
5839  ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
5840  F.StateTransitions.push_back({State, Offset});
5841  }
5842  }
5843 
5844  // Read the final state.
5845  assert(Idx < Record.size() &&
5846  "Invalid data, missing final pragma diagnostic state");
5847  SourceLocation CurStateLoc =
5848  ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5849  auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
5850 
5851  if (!F.isModule()) {
5852  Diag.DiagStatesByLoc.CurDiagState = CurState;
5853  Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
5854 
5855  // Preserve the property that the imaginary root file describes the
5856  // current state.
5857  FileID NullFile;
5858  auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
5859  if (T.empty())
5860  T.push_back({CurState, 0});
5861  else
5862  T[0].State = CurState;
5863  }
5864 
5865  // Don't try to read these mappings again.
5866  Record.clear();
5867  }
5868 }
5869 
5870 /// Get the correct cursor and offset for loading a type.
5871 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5872  GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5873  assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5874  ModuleFile *M = I->second;
5875  return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5876 }
5877 
5878 /// Read and return the type with the given index..
5879 ///
5880 /// The index is the type ID, shifted and minus the number of predefs. This
5881 /// routine actually reads the record corresponding to the type at the given
5882 /// location. It is a helper routine for GetType, which deals with reading type
5883 /// IDs.
5884 QualType ASTReader::readTypeRecord(unsigned Index) {
5885  assert(ContextObj && "reading type with no AST context");
5886  ASTContext &Context = *ContextObj;
5887  RecordLocation Loc = TypeCursorForIndex(Index);
5888  BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5889 
5890  // Keep track of where we are in the stream, then jump back there
5891  // after reading this type.
5892  SavedStreamPosition SavedPosition(DeclsCursor);
5893 
5894  ReadingKindTracker ReadingKind(Read_Type, *this);
5895 
5896  // Note that we are loading a type record.
5897  Deserializing AType(this);
5898 
5899  unsigned Idx = 0;
5900  DeclsCursor.JumpToBit(Loc.Offset);
5901  RecordData Record;
5902  unsigned Code = DeclsCursor.ReadCode();
5903  switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5904  case TYPE_EXT_QUAL: {
5905  if (Record.size() != 2) {
5906  Error("Incorrect encoding of extended qualifier type");
5907  return QualType();
5908  }
5909  QualType Base = readType(*Loc.F, Record, Idx);
5910  Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5911  return Context.getQualifiedType(Base, Quals);
5912  }
5913 
5914  case TYPE_COMPLEX: {
5915  if (Record.size() != 1) {
5916  Error("Incorrect encoding of complex type");
5917  return QualType();
5918  }
5919  QualType ElemType = readType(*Loc.F, Record, Idx);
5920  return Context.getComplexType(ElemType);
5921  }
5922 
5923  case TYPE_POINTER: {
5924  if (Record.size() != 1) {
5925  Error("Incorrect encoding of pointer type");
5926  return QualType();
5927  }
5928  QualType PointeeType = readType(*Loc.F, Record, Idx);
5929  return Context.getPointerType(PointeeType);
5930  }
5931 
5932  case TYPE_DECAYED: {
5933  if (Record.size() != 1) {
5934  Error("Incorrect encoding of decayed type");
5935  return QualType();
5936  }
5937  QualType OriginalType = readType(*Loc.F, Record, Idx);
5938  QualType DT = Context.getAdjustedParameterType(OriginalType);
5939  if (!isa<DecayedType>(DT))
5940  Error("Decayed type does not decay");
5941  return DT;
5942  }
5943 
5944  case TYPE_ADJUSTED: {
5945  if (Record.size() != 2) {
5946  Error("Incorrect encoding of adjusted type");
5947  return QualType();
5948  }
5949  QualType OriginalTy = readType(*Loc.F, Record, Idx);
5950  QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5951  return Context.getAdjustedType(OriginalTy, AdjustedTy);
5952  }
5953 
5954  case TYPE_BLOCK_POINTER: {
5955  if (Record.size() != 1) {
5956  Error("Incorrect encoding of block pointer type");
5957  return QualType();
5958  }
5959  QualType PointeeType = readType(*Loc.F, Record, Idx);
5960  return Context.getBlockPointerType(PointeeType);
5961  }
5962 
5963  case TYPE_LVALUE_REFERENCE: {
5964  if (Record.size() != 2) {
5965  Error("Incorrect encoding of lvalue reference type");
5966  return QualType();
5967  }
5968  QualType PointeeType = readType(*Loc.F, Record, Idx);
5969  return Context.getLValueReferenceType(PointeeType, Record[1]);
5970  }
5971 
5972  case TYPE_RVALUE_REFERENCE: {
5973  if (Record.size() != 1) {
5974  Error("Incorrect encoding of rvalue reference type");
5975  return QualType();
5976  }
5977  QualType PointeeType = readType(*Loc.F, Record, Idx);
5978  return Context.getRValueReferenceType(PointeeType);
5979  }
5980 
5981  case TYPE_MEMBER_POINTER: {
5982  if (Record.size() != 2) {
5983  Error("Incorrect encoding of member pointer type");
5984  return QualType();
5985  }
5986  QualType PointeeType = readType(*Loc.F, Record, Idx);
5987  QualType ClassType = readType(*Loc.F, Record, Idx);
5988  if (PointeeType.isNull() || ClassType.isNull())
5989  return QualType();
5990 
5991  return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5992  }
5993 
5994  case TYPE_CONSTANT_ARRAY: {
5995  QualType ElementType = readType(*Loc.F, Record, Idx);
5997  unsigned IndexTypeQuals = Record[2];
5998  unsigned Idx = 3;
5999  llvm::APInt Size = ReadAPInt(Record, Idx);
6000  return Context.getConstantArrayType(ElementType, Size,
6001  ASM, IndexTypeQuals);
6002  }
6003 
6004  case TYPE_INCOMPLETE_ARRAY: {
6005  QualType ElementType = readType(*Loc.F, Record, Idx);
6007  unsigned IndexTypeQuals = Record[2];
6008  return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6009  }
6010 
6011  case TYPE_VARIABLE_ARRAY: {
6012  QualType ElementType = readType(*Loc.F, Record, Idx);
6014  unsigned IndexTypeQuals = Record[2];
6015  SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6016  SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6017  return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6018  ASM, IndexTypeQuals,
6019  SourceRange(LBLoc, RBLoc));
6020  }
6021 
6022  case TYPE_VECTOR: {
6023  if (Record.size() != 3) {
6024  Error("incorrect encoding of vector type in AST file");
6025  return QualType();
6026  }
6027 
6028  QualType ElementType = readType(*Loc.F, Record, Idx);
6029  unsigned NumElements = Record[1];
6030  unsigned VecKind = Record[2];
6031  return Context.