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