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