1//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the ASTReader class, which reads AST files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTReader.h"
15#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
17#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/Type.h"
24#include "clang/AST/TypeLocVisitor.h"
25#include "clang/Basic/DiagnosticOptions.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/SourceManagerInternals.h"
29#include "clang/Basic/TargetInfo.h"
30#include "clang/Basic/TargetOptions.h"
31#include "clang/Basic/Version.h"
32#include "clang/Basic/VersionTuple.h"
33#include "clang/Frontend/Utils.h"
34#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/Scope.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTDeserializationListener.h"
43#include "clang/Serialization/GlobalModuleIndex.h"
44#include "clang/Serialization/ModuleManager.h"
45#include "clang/Serialization/SerializationDiagnostic.h"
46#include "llvm/ADT/Hashing.h"
47#include "llvm/ADT/StringExtras.h"
48#include "llvm/Bitcode/BitstreamReader.h"
49#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/FileSystem.h"
51#include "llvm/Support/MemoryBuffer.h"
52#include "llvm/Support/Path.h"
53#include "llvm/Support/SaveAndRestore.h"
54#include "llvm/Support/raw_ostream.h"
55#include <algorithm>
56#include <cstdio>
57#include <iterator>
58#include <system_error>
59
60using namespace clang;
61using namespace clang::serialization;
62using namespace clang::serialization::reader;
63using llvm::BitstreamCursor;
64
65
66//===----------------------------------------------------------------------===//
67// ChainedASTReaderListener implementation
68//===----------------------------------------------------------------------===//
69
70bool
71ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72  return First->ReadFullVersionInformation(FullVersion) ||
73         Second->ReadFullVersionInformation(FullVersion);
74}
75void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
76  First->ReadModuleName(ModuleName);
77  Second->ReadModuleName(ModuleName);
78}
79void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
80  First->ReadModuleMapFile(ModuleMapPath);
81  Second->ReadModuleMapFile(ModuleMapPath);
82}
83bool ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
84                                                   bool Complain) {
85  return First->ReadLanguageOptions(LangOpts, Complain) ||
86         Second->ReadLanguageOptions(LangOpts, Complain);
87}
88bool
89ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
90                                            bool Complain) {
91  return First->ReadTargetOptions(TargetOpts, Complain) ||
92         Second->ReadTargetOptions(TargetOpts, Complain);
93}
94bool ChainedASTReaderListener::ReadDiagnosticOptions(
95    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
96  return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
97         Second->ReadDiagnosticOptions(DiagOpts, Complain);
98}
99bool
100ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
101                                                bool Complain) {
102  return First->ReadFileSystemOptions(FSOpts, Complain) ||
103         Second->ReadFileSystemOptions(FSOpts, Complain);
104}
105
106bool ChainedASTReaderListener::ReadHeaderSearchOptions(
107    const HeaderSearchOptions &HSOpts, bool Complain) {
108  return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
109         Second->ReadHeaderSearchOptions(HSOpts, Complain);
110}
111bool ChainedASTReaderListener::ReadPreprocessorOptions(
112    const PreprocessorOptions &PPOpts, bool Complain,
113    std::string &SuggestedPredefines) {
114  return First->ReadPreprocessorOptions(PPOpts, Complain,
115                                        SuggestedPredefines) ||
116         Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
117}
118void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
119                                           unsigned Value) {
120  First->ReadCounter(M, Value);
121  Second->ReadCounter(M, Value);
122}
123bool ChainedASTReaderListener::needsInputFileVisitation() {
124  return First->needsInputFileVisitation() ||
125         Second->needsInputFileVisitation();
126}
127bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
128  return First->needsSystemInputFileVisitation() ||
129  Second->needsSystemInputFileVisitation();
130}
131void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
132  First->visitModuleFile(Filename);
133  Second->visitModuleFile(Filename);
134}
135bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
136                                              bool isSystem,
137                                              bool isOverridden) {
138  bool Continue = false;
139  if (First->needsInputFileVisitation() &&
140      (!isSystem || First->needsSystemInputFileVisitation()))
141    Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
142  if (Second->needsInputFileVisitation() &&
143      (!isSystem || Second->needsSystemInputFileVisitation()))
144    Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
145  return Continue;
146}
147
148//===----------------------------------------------------------------------===//
149// PCH validator implementation
150//===----------------------------------------------------------------------===//
151
152ASTReaderListener::~ASTReaderListener() {}
153
154/// \brief Compare the given set of language options against an existing set of
155/// language options.
156///
157/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
158///
159/// \returns true if the languagae options mis-match, false otherwise.
160static bool checkLanguageOptions(const LangOptions &LangOpts,
161                                 const LangOptions &ExistingLangOpts,
162                                 DiagnosticsEngine *Diags) {
163#define LANGOPT(Name, Bits, Default, Description)                 \
164  if (ExistingLangOpts.Name != LangOpts.Name) {                   \
165    if (Diags)                                                    \
166      Diags->Report(diag::err_pch_langopt_mismatch)               \
167        << Description << LangOpts.Name << ExistingLangOpts.Name; \
168    return true;                                                  \
169  }
170
171#define VALUE_LANGOPT(Name, Bits, Default, Description)   \
172  if (ExistingLangOpts.Name != LangOpts.Name) {           \
173    if (Diags)                                            \
174      Diags->Report(diag::err_pch_langopt_value_mismatch) \
175        << Description;                                   \
176    return true;                                          \
177  }
178
179#define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
180  if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
181    if (Diags)                                                 \
182      Diags->Report(diag::err_pch_langopt_value_mismatch)      \
183        << Description;                                        \
184    return true;                                               \
185  }
186
187#define BENIGN_LANGOPT(Name, Bits, Default, Description)
188#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
189#include "clang/Basic/LangOptions.def"
190
191  if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
192    if (Diags)
193      Diags->Report(diag::err_pch_langopt_value_mismatch)
194      << "target Objective-C runtime";
195    return true;
196  }
197
198  if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
199      LangOpts.CommentOpts.BlockCommandNames) {
200    if (Diags)
201      Diags->Report(diag::err_pch_langopt_value_mismatch)
202        << "block command names";
203    return true;
204  }
205
206  return false;
207}
208
209/// \brief Compare the given set of target options against an existing set of
210/// target options.
211///
212/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
213///
214/// \returns true if the target options mis-match, false otherwise.
215static bool checkTargetOptions(const TargetOptions &TargetOpts,
216                               const TargetOptions &ExistingTargetOpts,
217                               DiagnosticsEngine *Diags) {
218#define CHECK_TARGET_OPT(Field, Name)                             \
219  if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
220    if (Diags)                                                    \
221      Diags->Report(diag::err_pch_targetopt_mismatch)             \
222        << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
223    return true;                                                  \
224  }
225
226  CHECK_TARGET_OPT(Triple, "target");
227  CHECK_TARGET_OPT(CPU, "target CPU");
228  CHECK_TARGET_OPT(ABI, "target ABI");
229#undef CHECK_TARGET_OPT
230
231  // Compare feature sets.
232  SmallVector<StringRef, 4> ExistingFeatures(
233                                             ExistingTargetOpts.FeaturesAsWritten.begin(),
234                                             ExistingTargetOpts.FeaturesAsWritten.end());
235  SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
236                                         TargetOpts.FeaturesAsWritten.end());
237  std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
238  std::sort(ReadFeatures.begin(), ReadFeatures.end());
239
240  unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
241  unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
242  while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
243    if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
244      ++ExistingIdx;
245      ++ReadIdx;
246      continue;
247    }
248
249    if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
250      if (Diags)
251        Diags->Report(diag::err_pch_targetopt_feature_mismatch)
252          << false << ReadFeatures[ReadIdx];
253      return true;
254    }
255
256    if (Diags)
257      Diags->Report(diag::err_pch_targetopt_feature_mismatch)
258        << true << ExistingFeatures[ExistingIdx];
259    return true;
260  }
261
262  if (ExistingIdx < ExistingN) {
263    if (Diags)
264      Diags->Report(diag::err_pch_targetopt_feature_mismatch)
265        << true << ExistingFeatures[ExistingIdx];
266    return true;
267  }
268
269  if (ReadIdx < ReadN) {
270    if (Diags)
271      Diags->Report(diag::err_pch_targetopt_feature_mismatch)
272        << false << ReadFeatures[ReadIdx];
273    return true;
274  }
275
276  return false;
277}
278
279bool
280PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
281                                  bool Complain) {
282  const LangOptions &ExistingLangOpts = PP.getLangOpts();
283  return checkLanguageOptions(LangOpts, ExistingLangOpts,
284                              Complain? &Reader.Diags : nullptr);
285}
286
287bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
288                                     bool Complain) {
289  const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
290  return checkTargetOptions(TargetOpts, ExistingTargetOpts,
291                            Complain? &Reader.Diags : nullptr);
292}
293
294namespace {
295  typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
296    MacroDefinitionsMap;
297  typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
298    DeclsMap;
299}
300
301static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
302                                         DiagnosticsEngine &Diags,
303                                         bool Complain) {
304  typedef DiagnosticsEngine::Level Level;
305
306  // Check current mappings for new -Werror mappings, and the stored mappings
307  // for cases that were explicitly mapped to *not* be errors that are now
308  // errors because of options like -Werror.
309  DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
310
311  for (DiagnosticsEngine *MappingSource : MappingSources) {
312    for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
313      diag::kind DiagID = DiagIDMappingPair.first;
314      Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
315      if (CurLevel < DiagnosticsEngine::Error)
316        continue; // not significant
317      Level StoredLevel =
318          StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
319      if (StoredLevel < DiagnosticsEngine::Error) {
320        if (Complain)
321          Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
322              Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
323        return true;
324      }
325    }
326  }
327
328  return false;
329}
330
331static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
332  diag::Severity Ext = Diags.getExtensionHandlingBehavior();
333  if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
334    return true;
335  return Ext >= diag::Severity::Error;
336}
337
338static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
339                                    DiagnosticsEngine &Diags,
340                                    bool IsSystem, bool Complain) {
341  // Top-level options
342  if (IsSystem) {
343    if (Diags.getSuppressSystemWarnings())
344      return false;
345    // If -Wsystem-headers was not enabled before, be conservative
346    if (StoredDiags.getSuppressSystemWarnings()) {
347      if (Complain)
348        Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
349      return true;
350    }
351  }
352
353  if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
354    if (Complain)
355      Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
356    return true;
357  }
358
359  if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
360      !StoredDiags.getEnableAllWarnings()) {
361    if (Complain)
362      Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
363    return true;
364  }
365
366  if (isExtHandlingFromDiagsError(Diags) &&
367      !isExtHandlingFromDiagsError(StoredDiags)) {
368    if (Complain)
369      Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
370    return true;
371  }
372
373  return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
374}
375
376bool PCHValidator::ReadDiagnosticOptions(
377    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
378  DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
379  IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
380  IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
381      new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
382  // This should never fail, because we would have processed these options
383  // before writing them to an ASTFile.
384  ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
385
386  ModuleManager &ModuleMgr = Reader.getModuleManager();
387  assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
388
389  // If the original import came from a file explicitly generated by the user,
390  // don't check the diagnostic mappings.
391  // FIXME: currently this is approximated by checking whether this is not a
392  // module import.
393  // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
394  // the transitive closure of its imports, since unrelated modules cannot be
395  // imported until after this module finishes validation.
396  ModuleFile *TopImport = *ModuleMgr.rbegin();
397  while (!TopImport->ImportedBy.empty())
398    TopImport = TopImport->ImportedBy[0];
399  if (TopImport->Kind != MK_Module)
400    return false;
401
402  StringRef ModuleName = TopImport->ModuleName;
403  assert(!ModuleName.empty() && "diagnostic options read before module name");
404
405  Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
406  assert(M && "missing module");
407
408  // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
409  // contains the union of their flags.
410  return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
411}
412
413/// \brief Collect the macro definitions provided by the given preprocessor
414/// options.
415static void
416collectMacroDefinitions(const PreprocessorOptions &PPOpts,
417                        MacroDefinitionsMap &Macros,
418                        SmallVectorImpl<StringRef> *MacroNames = nullptr) {
419  for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
420    StringRef Macro = PPOpts.Macros[I].first;
421    bool IsUndef = PPOpts.Macros[I].second;
422
423    std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
424    StringRef MacroName = MacroPair.first;
425    StringRef MacroBody = MacroPair.second;
426
427    // For an #undef'd macro, we only care about the name.
428    if (IsUndef) {
429      if (MacroNames && !Macros.count(MacroName))
430        MacroNames->push_back(MacroName);
431
432      Macros[MacroName] = std::make_pair("", true);
433      continue;
434    }
435
436    // For a #define'd macro, figure out the actual definition.
437    if (MacroName.size() == Macro.size())
438      MacroBody = "1";
439    else {
440      // Note: GCC drops anything following an end-of-line character.
441      StringRef::size_type End = MacroBody.find_first_of("\n\r");
442      MacroBody = MacroBody.substr(0, End);
443    }
444
445    if (MacroNames && !Macros.count(MacroName))
446      MacroNames->push_back(MacroName);
447    Macros[MacroName] = std::make_pair(MacroBody, false);
448  }
449}
450
451/// \brief Check the preprocessor options deserialized from the control block
452/// against the preprocessor options in an existing preprocessor.
453///
454/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
455static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
456                                     const PreprocessorOptions &ExistingPPOpts,
457                                     DiagnosticsEngine *Diags,
458                                     FileManager &FileMgr,
459                                     std::string &SuggestedPredefines,
460                                     const LangOptions &LangOpts) {
461  // Check macro definitions.
462  MacroDefinitionsMap ASTFileMacros;
463  collectMacroDefinitions(PPOpts, ASTFileMacros);
464  MacroDefinitionsMap ExistingMacros;
465  SmallVector<StringRef, 4> ExistingMacroNames;
466  collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
467
468  for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
469    // Dig out the macro definition in the existing preprocessor options.
470    StringRef MacroName = ExistingMacroNames[I];
471    std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
472
473    // Check whether we know anything about this macro name or not.
474    llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
475      = ASTFileMacros.find(MacroName);
476    if (Known == ASTFileMacros.end()) {
477      // FIXME: Check whether this identifier was referenced anywhere in the
478      // AST file. If so, we should reject the AST file. Unfortunately, this
479      // information isn't in the control block. What shall we do about it?
480
481      if (Existing.second) {
482        SuggestedPredefines += "#undef ";
483        SuggestedPredefines += MacroName.str();
484        SuggestedPredefines += '\n';
485      } else {
486        SuggestedPredefines += "#define ";
487        SuggestedPredefines += MacroName.str();
488        SuggestedPredefines += ' ';
489        SuggestedPredefines += Existing.first.str();
490        SuggestedPredefines += '\n';
491      }
492      continue;
493    }
494
495    // If the macro was defined in one but undef'd in the other, we have a
496    // conflict.
497    if (Existing.second != Known->second.second) {
498      if (Diags) {
499        Diags->Report(diag::err_pch_macro_def_undef)
500          << MacroName << Known->second.second;
501      }
502      return true;
503    }
504
505    // If the macro was #undef'd in both, or if the macro bodies are identical,
506    // it's fine.
507    if (Existing.second || Existing.first == Known->second.first)
508      continue;
509
510    // The macro bodies differ; complain.
511    if (Diags) {
512      Diags->Report(diag::err_pch_macro_def_conflict)
513        << MacroName << Known->second.first << Existing.first;
514    }
515    return true;
516  }
517
518  // Check whether we're using predefines.
519  if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
520    if (Diags) {
521      Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
522    }
523    return true;
524  }
525
526  // Detailed record is important since it is used for the module cache hash.
527  if (LangOpts.Modules &&
528      PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
529    if (Diags) {
530      Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
531    }
532    return true;
533  }
534
535  // Compute the #include and #include_macros lines we need.
536  for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
537    StringRef File = ExistingPPOpts.Includes[I];
538    if (File == ExistingPPOpts.ImplicitPCHInclude)
539      continue;
540
541    if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
542          != PPOpts.Includes.end())
543      continue;
544
545    SuggestedPredefines += "#include \"";
546    SuggestedPredefines +=
547      HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
548    SuggestedPredefines += "\"\n";
549  }
550
551  for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
552    StringRef File = ExistingPPOpts.MacroIncludes[I];
553    if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
554                  File)
555        != PPOpts.MacroIncludes.end())
556      continue;
557
558    SuggestedPredefines += "#__include_macros \"";
559    SuggestedPredefines +=
560      HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
561    SuggestedPredefines += "\"\n##\n";
562  }
563
564  return false;
565}
566
567bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
568                                           bool Complain,
569                                           std::string &SuggestedPredefines) {
570  const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
571
572  return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
573                                  Complain? &Reader.Diags : nullptr,
574                                  PP.getFileManager(),
575                                  SuggestedPredefines,
576                                  PP.getLangOpts());
577}
578
579void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
580  PP.setCounterValue(Value);
581}
582
583//===----------------------------------------------------------------------===//
584// AST reader implementation
585//===----------------------------------------------------------------------===//
586
587void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
588                                           bool TakeOwnership) {
589  DeserializationListener = Listener;
590  OwnsDeserializationListener = TakeOwnership;
591}
592
593
594
595unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
596  return serialization::ComputeHash(Sel);
597}
598
599
600std::pair<unsigned, unsigned>
601ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
602  using namespace llvm::support;
603  unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
604  unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
605  return std::make_pair(KeyLen, DataLen);
606}
607
608ASTSelectorLookupTrait::internal_key_type
609ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
610  using namespace llvm::support;
611  SelectorTable &SelTable = Reader.getContext().Selectors;
612  unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
613  IdentifierInfo *FirstII = Reader.getLocalIdentifier(
614      F, endian::readNext<uint32_t, little, unaligned>(d));
615  if (N == 0)
616    return SelTable.getNullarySelector(FirstII);
617  else if (N == 1)
618    return SelTable.getUnarySelector(FirstII);
619
620  SmallVector<IdentifierInfo *, 16> Args;
621  Args.push_back(FirstII);
622  for (unsigned I = 1; I != N; ++I)
623    Args.push_back(Reader.getLocalIdentifier(
624        F, endian::readNext<uint32_t, little, unaligned>(d)));
625
626  return SelTable.getSelector(N, Args.data());
627}
628
629ASTSelectorLookupTrait::data_type
630ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
631                                 unsigned DataLen) {
632  using namespace llvm::support;
633
634  data_type Result;
635
636  Result.ID = Reader.getGlobalSelectorID(
637      F, endian::readNext<uint32_t, little, unaligned>(d));
638  unsigned NumInstanceMethodsAndBits =
639      endian::readNext<uint16_t, little, unaligned>(d);
640  unsigned NumFactoryMethodsAndBits =
641      endian::readNext<uint16_t, little, unaligned>(d);
642  Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
643  Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
644  unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
645  unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
646
647  // Load instance methods
648  for (unsigned I = 0; I != NumInstanceMethods; ++I) {
649    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
650            F, endian::readNext<uint32_t, little, unaligned>(d)))
651      Result.Instance.push_back(Method);
652  }
653
654  // Load factory methods
655  for (unsigned I = 0; I != NumFactoryMethods; ++I) {
656    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
657            F, endian::readNext<uint32_t, little, unaligned>(d)))
658      Result.Factory.push_back(Method);
659  }
660
661  return Result;
662}
663
664unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
665  return llvm::HashString(a);
666}
667
668std::pair<unsigned, unsigned>
669ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
670  using namespace llvm::support;
671  unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
672  unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
673  return std::make_pair(KeyLen, DataLen);
674}
675
676ASTIdentifierLookupTraitBase::internal_key_type
677ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
678  assert(n >= 2 && d[n-1] == '\0');
679  return StringRef((const char*) d, n-1);
680}
681
682/// \brief Whether the given identifier is "interesting".
683static bool isInterestingIdentifier(IdentifierInfo &II) {
684  return II.isPoisoned() ||
685         II.isExtensionToken() ||
686         II.getObjCOrBuiltinID() ||
687         II.hasRevertedTokenIDToIdentifier() ||
688         II.hadMacroDefinition() ||
689         II.getFETokenInfo<void>();
690}
691
692IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
693                                                   const unsigned char* d,
694                                                   unsigned DataLen) {
695  using namespace llvm::support;
696  unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
697  bool IsInteresting = RawID & 0x01;
698
699  // Wipe out the "is interesting" bit.
700  RawID = RawID >> 1;
701
702  IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
703  if (!IsInteresting) {
704    // For uninteresting identifiers, just build the IdentifierInfo
705    // and associate it with the persistent ID.
706    IdentifierInfo *II = KnownII;
707    if (!II) {
708      II = &Reader.getIdentifierTable().getOwn(k);
709      KnownII = II;
710    }
711    Reader.SetIdentifierInfo(ID, II);
712    if (!II->isFromAST()) {
713      bool WasInteresting = isInterestingIdentifier(*II);
714      II->setIsFromAST();
715      if (WasInteresting)
716        II->setChangedSinceDeserialization();
717    }
718    Reader.markIdentifierUpToDate(II);
719    return II;
720  }
721
722  unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
723  unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
724  bool CPlusPlusOperatorKeyword = Bits & 0x01;
725  Bits >>= 1;
726  bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
727  Bits >>= 1;
728  bool Poisoned = Bits & 0x01;
729  Bits >>= 1;
730  bool ExtensionToken = Bits & 0x01;
731  Bits >>= 1;
732  bool hasSubmoduleMacros = Bits & 0x01;
733  Bits >>= 1;
734  bool hadMacroDefinition = Bits & 0x01;
735  Bits >>= 1;
736
737  assert(Bits == 0 && "Extra bits in the identifier?");
738  DataLen -= 8;
739
740  // Build the IdentifierInfo itself and link the identifier ID with
741  // the new IdentifierInfo.
742  IdentifierInfo *II = KnownII;
743  if (!II) {
744    II = &Reader.getIdentifierTable().getOwn(StringRef(k));
745    KnownII = II;
746  }
747  Reader.markIdentifierUpToDate(II);
748  if (!II->isFromAST()) {
749    bool WasInteresting = isInterestingIdentifier(*II);
750    II->setIsFromAST();
751    if (WasInteresting)
752      II->setChangedSinceDeserialization();
753  }
754
755  // Set or check the various bits in the IdentifierInfo structure.
756  // Token IDs are read-only.
757  if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
758    II->RevertTokenIDToIdentifier();
759  II->setObjCOrBuiltinID(ObjCOrBuiltinID);
760  assert(II->isExtensionToken() == ExtensionToken &&
761         "Incorrect extension token flag");
762  (void)ExtensionToken;
763  if (Poisoned)
764    II->setIsPoisoned(true);
765  assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
766         "Incorrect C++ operator keyword flag");
767  (void)CPlusPlusOperatorKeyword;
768
769  // If this identifier is a macro, deserialize the macro
770  // definition.
771  if (hadMacroDefinition) {
772    uint32_t MacroDirectivesOffset =
773        endian::readNext<uint32_t, little, unaligned>(d);
774    DataLen -= 4;
775    SmallVector<uint32_t, 8> LocalMacroIDs;
776    if (hasSubmoduleMacros) {
777      while (uint32_t LocalMacroID =
778                 endian::readNext<uint32_t, little, unaligned>(d)) {
779        DataLen -= 4;
780        LocalMacroIDs.push_back(LocalMacroID);
781      }
782      DataLen -= 4;
783    }
784
785    if (F.Kind == MK_Module) {
786      // Macro definitions are stored from newest to oldest, so reverse them
787      // before registering them.
788      llvm::SmallVector<unsigned, 8> MacroSizes;
789      for (SmallVectorImpl<uint32_t>::iterator
790             I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
791        unsigned Size = 1;
792
793        static const uint32_t HasOverridesFlag = 0x80000000U;
794        if (I + 1 != E && (I[1] & HasOverridesFlag))
795          Size += 1 + (I[1] & ~HasOverridesFlag);
796
797        MacroSizes.push_back(Size);
798        I += Size;
799      }
800
801      SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
802      for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
803                                                       SE = MacroSizes.rend();
804           SI != SE; ++SI) {
805        I -= *SI;
806
807        uint32_t LocalMacroID = *I;
808        ArrayRef<uint32_t> Overrides;
809        if (*SI != 1)
810          Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
811        Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
812      }
813      assert(I == LocalMacroIDs.begin());
814    } else {
815      Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
816    }
817  }
818
819  Reader.SetIdentifierInfo(ID, II);
820
821  // Read all of the declarations visible at global scope with this
822  // name.
823  if (DataLen > 0) {
824    SmallVector<uint32_t, 4> DeclIDs;
825    for (; DataLen > 0; DataLen -= 4)
826      DeclIDs.push_back(Reader.getGlobalDeclID(
827          F, endian::readNext<uint32_t, little, unaligned>(d)));
828    Reader.SetGloballyVisibleDecls(II, DeclIDs);
829  }
830
831  return II;
832}
833
834unsigned
835ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
836  llvm::FoldingSetNodeID ID;
837  ID.AddInteger(Key.Kind);
838
839  switch (Key.Kind) {
840  case DeclarationName::Identifier:
841  case DeclarationName::CXXLiteralOperatorName:
842    ID.AddString(((IdentifierInfo*)Key.Data)->getName());
843    break;
844  case DeclarationName::ObjCZeroArgSelector:
845  case DeclarationName::ObjCOneArgSelector:
846  case DeclarationName::ObjCMultiArgSelector:
847    ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
848    break;
849  case DeclarationName::CXXOperatorName:
850    ID.AddInteger((OverloadedOperatorKind)Key.Data);
851    break;
852  case DeclarationName::CXXConstructorName:
853  case DeclarationName::CXXDestructorName:
854  case DeclarationName::CXXConversionFunctionName:
855  case DeclarationName::CXXUsingDirective:
856    break;
857  }
858
859  return ID.ComputeHash();
860}
861
862ASTDeclContextNameLookupTrait::internal_key_type
863ASTDeclContextNameLookupTrait::GetInternalKey(
864                                          const external_key_type& Name) const {
865  DeclNameKey Key;
866  Key.Kind = Name.getNameKind();
867  switch (Name.getNameKind()) {
868  case DeclarationName::Identifier:
869    Key.Data = (uint64_t)Name.getAsIdentifierInfo();
870    break;
871  case DeclarationName::ObjCZeroArgSelector:
872  case DeclarationName::ObjCOneArgSelector:
873  case DeclarationName::ObjCMultiArgSelector:
874    Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
875    break;
876  case DeclarationName::CXXOperatorName:
877    Key.Data = Name.getCXXOverloadedOperator();
878    break;
879  case DeclarationName::CXXLiteralOperatorName:
880    Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
881    break;
882  case DeclarationName::CXXConstructorName:
883  case DeclarationName::CXXDestructorName:
884  case DeclarationName::CXXConversionFunctionName:
885  case DeclarationName::CXXUsingDirective:
886    Key.Data = 0;
887    break;
888  }
889
890  return Key;
891}
892
893std::pair<unsigned, unsigned>
894ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
895  using namespace llvm::support;
896  unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
897  unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
898  return std::make_pair(KeyLen, DataLen);
899}
900
901ASTDeclContextNameLookupTrait::internal_key_type
902ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
903  using namespace llvm::support;
904
905  DeclNameKey Key;
906  Key.Kind = (DeclarationName::NameKind)*d++;
907  switch (Key.Kind) {
908  case DeclarationName::Identifier:
909    Key.Data = (uint64_t)Reader.getLocalIdentifier(
910        F, endian::readNext<uint32_t, little, unaligned>(d));
911    break;
912  case DeclarationName::ObjCZeroArgSelector:
913  case DeclarationName::ObjCOneArgSelector:
914  case DeclarationName::ObjCMultiArgSelector:
915    Key.Data =
916        (uint64_t)Reader.getLocalSelector(
917                             F, endian::readNext<uint32_t, little, unaligned>(
918                                    d)).getAsOpaquePtr();
919    break;
920  case DeclarationName::CXXOperatorName:
921    Key.Data = *d++; // OverloadedOperatorKind
922    break;
923  case DeclarationName::CXXLiteralOperatorName:
924    Key.Data = (uint64_t)Reader.getLocalIdentifier(
925        F, endian::readNext<uint32_t, little, unaligned>(d));
926    break;
927  case DeclarationName::CXXConstructorName:
928  case DeclarationName::CXXDestructorName:
929  case DeclarationName::CXXConversionFunctionName:
930  case DeclarationName::CXXUsingDirective:
931    Key.Data = 0;
932    break;
933  }
934
935  return Key;
936}
937
938ASTDeclContextNameLookupTrait::data_type
939ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
940                                        const unsigned char* d,
941                                        unsigned DataLen) {
942  using namespace llvm::support;
943  unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
944  LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
945                        const_cast<unsigned char *>(d));
946  return std::make_pair(Start, Start + NumDecls);
947}
948
949bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
950                                       BitstreamCursor &Cursor,
951                                   const std::pair<uint64_t, uint64_t> &Offsets,
952                                       DeclContextInfo &Info) {
953  SavedStreamPosition SavedPosition(Cursor);
954  // First the lexical decls.
955  if (Offsets.first != 0) {
956    Cursor.JumpToBit(Offsets.first);
957
958    RecordData Record;
959    StringRef Blob;
960    unsigned Code = Cursor.ReadCode();
961    unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
962    if (RecCode != DECL_CONTEXT_LEXICAL) {
963      Error("Expected lexical block");
964      return true;
965    }
966
967    Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
968    Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
969  }
970
971  // Now the lookup table.
972  if (Offsets.second != 0) {
973    Cursor.JumpToBit(Offsets.second);
974
975    RecordData Record;
976    StringRef Blob;
977    unsigned Code = Cursor.ReadCode();
978    unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
979    if (RecCode != DECL_CONTEXT_VISIBLE) {
980      Error("Expected visible lookup table block");
981      return true;
982    }
983    Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
984        (const unsigned char *)Blob.data() + Record[0],
985        (const unsigned char *)Blob.data() + sizeof(uint32_t),
986        (const unsigned char *)Blob.data(),
987        ASTDeclContextNameLookupTrait(*this, M));
988  }
989
990  return false;
991}
992
993void ASTReader::Error(StringRef Msg) {
994  Error(diag::err_fe_pch_malformed, Msg);
995  if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
996    Diag(diag::note_module_cache_path)
997      << PP.getHeaderSearchInfo().getModuleCachePath();
998  }
999}
1000
1001void ASTReader::Error(unsigned DiagID,
1002                      StringRef Arg1, StringRef Arg2) {
1003  if (Diags.isDiagnosticInFlight())
1004    Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1005  else
1006    Diag(DiagID) << Arg1 << Arg2;
1007}
1008
1009//===----------------------------------------------------------------------===//
1010// Source Manager Deserialization
1011//===----------------------------------------------------------------------===//
1012
1013/// \brief Read the line table in the source manager block.
1014/// \returns true if there was an error.
1015bool ASTReader::ParseLineTable(ModuleFile &F,
1016                               SmallVectorImpl<uint64_t> &Record) {
1017  unsigned Idx = 0;
1018  LineTableInfo &LineTable = SourceMgr.getLineTable();
1019
1020  // Parse the file names
1021  std::map<int, int> FileIDs;
1022  for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1023    // Extract the file name
1024    unsigned FilenameLen = Record[Idx++];
1025    std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1026    Idx += FilenameLen;
1027    MaybeAddSystemRootToFilename(F, Filename);
1028    FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1029  }
1030
1031  // Parse the line entries
1032  std::vector<LineEntry> Entries;
1033  while (Idx < Record.size()) {
1034    int FID = Record[Idx++];
1035    assert(FID >= 0 && "Serialized line entries for non-local file.");
1036    // Remap FileID from 1-based old view.
1037    FID += F.SLocEntryBaseID - 1;
1038
1039    // Extract the line entries
1040    unsigned NumEntries = Record[Idx++];
1041    assert(NumEntries && "Numentries is 00000");
1042    Entries.clear();
1043    Entries.reserve(NumEntries);
1044    for (unsigned I = 0; I != NumEntries; ++I) {
1045      unsigned FileOffset = Record[Idx++];
1046      unsigned LineNo = Record[Idx++];
1047      int FilenameID = FileIDs[Record[Idx++]];
1048      SrcMgr::CharacteristicKind FileKind
1049        = (SrcMgr::CharacteristicKind)Record[Idx++];
1050      unsigned IncludeOffset = Record[Idx++];
1051      Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1052                                       FileKind, IncludeOffset));
1053    }
1054    LineTable.AddEntry(FileID::get(FID), Entries);
1055  }
1056
1057  return false;
1058}
1059
1060/// \brief Read a source manager block
1061bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1062  using namespace SrcMgr;
1063
1064  BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1065
1066  // Set the source-location entry cursor to the current position in
1067  // the stream. This cursor will be used to read the contents of the
1068  // source manager block initially, and then lazily read
1069  // source-location entries as needed.
1070  SLocEntryCursor = F.Stream;
1071
1072  // The stream itself is going to skip over the source manager block.
1073  if (F.Stream.SkipBlock()) {
1074    Error("malformed block record in AST file");
1075    return true;
1076  }
1077
1078  // Enter the source manager block.
1079  if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1080    Error("malformed source manager block record in AST file");
1081    return true;
1082  }
1083
1084  RecordData Record;
1085  while (true) {
1086    llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1087
1088    switch (E.Kind) {
1089    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1090    case llvm::BitstreamEntry::Error:
1091      Error("malformed block record in AST file");
1092      return true;
1093    case llvm::BitstreamEntry::EndBlock:
1094      return false;
1095    case llvm::BitstreamEntry::Record:
1096      // The interesting case.
1097      break;
1098    }
1099
1100    // Read a record.
1101    Record.clear();
1102    StringRef Blob;
1103    switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1104    default:  // Default behavior: ignore.
1105      break;
1106
1107    case SM_SLOC_FILE_ENTRY:
1108    case SM_SLOC_BUFFER_ENTRY:
1109    case SM_SLOC_EXPANSION_ENTRY:
1110      // Once we hit one of the source location entries, we're done.
1111      return false;
1112    }
1113  }
1114}
1115
1116/// \brief If a header file is not found at the path that we expect it to be
1117/// and the PCH file was moved from its original location, try to resolve the
1118/// file by assuming that header+PCH were moved together and the header is in
1119/// the same place relative to the PCH.
1120static std::string
1121resolveFileRelativeToOriginalDir(const std::string &Filename,
1122                                 const std::string &OriginalDir,
1123                                 const std::string &CurrDir) {
1124  assert(OriginalDir != CurrDir &&
1125         "No point trying to resolve the file if the PCH dir didn't change");
1126  using namespace llvm::sys;
1127  SmallString<128> filePath(Filename);
1128  fs::make_absolute(filePath);
1129  assert(path::is_absolute(OriginalDir));
1130  SmallString<128> currPCHPath(CurrDir);
1131
1132  path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1133                       fileDirE = path::end(path::parent_path(filePath));
1134  path::const_iterator origDirI = path::begin(OriginalDir),
1135                       origDirE = path::end(OriginalDir);
1136  // Skip the common path components from filePath and OriginalDir.
1137  while (fileDirI != fileDirE && origDirI != origDirE &&
1138         *fileDirI == *origDirI) {
1139    ++fileDirI;
1140    ++origDirI;
1141  }
1142  for (; origDirI != origDirE; ++origDirI)
1143    path::append(currPCHPath, "..");
1144  path::append(currPCHPath, fileDirI, fileDirE);
1145  path::append(currPCHPath, path::filename(Filename));
1146  return currPCHPath.str();
1147}
1148
1149bool ASTReader::ReadSLocEntry(int ID) {
1150  if (ID == 0)
1151    return false;
1152
1153  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1154    Error("source location entry ID out-of-range for AST file");
1155    return true;
1156  }
1157
1158  ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1159  F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1160  BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1161  unsigned BaseOffset = F->SLocEntryBaseOffset;
1162
1163  ++NumSLocEntriesRead;
1164  llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1165  if (Entry.Kind != llvm::BitstreamEntry::Record) {
1166    Error("incorrectly-formatted source location entry in AST file");
1167    return true;
1168  }
1169
1170  RecordData Record;
1171  StringRef Blob;
1172  switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1173  default:
1174    Error("incorrectly-formatted source location entry in AST file");
1175    return true;
1176
1177  case SM_SLOC_FILE_ENTRY: {
1178    // We will detect whether a file changed and return 'Failure' for it, but
1179    // we will also try to fail gracefully by setting up the SLocEntry.
1180    unsigned InputID = Record[4];
1181    InputFile IF = getInputFile(*F, InputID);
1182    const FileEntry *File = IF.getFile();
1183    bool OverriddenBuffer = IF.isOverridden();
1184
1185    // Note that we only check if a File was returned. If it was out-of-date
1186    // we have complained but we will continue creating a FileID to recover
1187    // gracefully.
1188    if (!File)
1189      return true;
1190
1191    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1192    if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1193      // This is the module's main file.
1194      IncludeLoc = getImportLocation(F);
1195    }
1196    SrcMgr::CharacteristicKind
1197      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1198    FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1199                                        ID, BaseOffset + Record[0]);
1200    SrcMgr::FileInfo &FileInfo =
1201          const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1202    FileInfo.NumCreatedFIDs = Record[5];
1203    if (Record[3])
1204      FileInfo.setHasLineDirectives();
1205
1206    const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1207    unsigned NumFileDecls = Record[7];
1208    if (NumFileDecls) {
1209      assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1210      FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1211                                                             NumFileDecls));
1212    }
1213
1214    const SrcMgr::ContentCache *ContentCache
1215      = SourceMgr.getOrCreateContentCache(File,
1216                              /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1217    if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1218        ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1219      unsigned Code = SLocEntryCursor.ReadCode();
1220      Record.clear();
1221      unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1222
1223      if (RecCode != SM_SLOC_BUFFER_BLOB) {
1224        Error("AST record has invalid code");
1225        return true;
1226      }
1227
1228      llvm::MemoryBuffer *Buffer
1229        = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
1230      SourceMgr.overrideFileContents(File, Buffer);
1231    }
1232
1233    break;
1234  }
1235
1236  case SM_SLOC_BUFFER_ENTRY: {
1237    const char *Name = Blob.data();
1238    unsigned Offset = Record[0];
1239    SrcMgr::CharacteristicKind
1240      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1241    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1242    if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1243      IncludeLoc = getImportLocation(F);
1244    }
1245    unsigned Code = SLocEntryCursor.ReadCode();
1246    Record.clear();
1247    unsigned RecCode
1248      = SLocEntryCursor.readRecord(Code, Record, &Blob);
1249
1250    if (RecCode != SM_SLOC_BUFFER_BLOB) {
1251      Error("AST record has invalid code");
1252      return true;
1253    }
1254
1255    llvm::MemoryBuffer *Buffer
1256      = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1257    SourceMgr.createFileID(Buffer, FileCharacter, ID, BaseOffset + Offset,
1258                           IncludeLoc);
1259    break;
1260  }
1261
1262  case SM_SLOC_EXPANSION_ENTRY: {
1263    SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1264    SourceMgr.createExpansionLoc(SpellingLoc,
1265                                     ReadSourceLocation(*F, Record[2]),
1266                                     ReadSourceLocation(*F, Record[3]),
1267                                     Record[4],
1268                                     ID,
1269                                     BaseOffset + Record[0]);
1270    break;
1271  }
1272  }
1273
1274  return false;
1275}
1276
1277std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1278  if (ID == 0)
1279    return std::make_pair(SourceLocation(), "");
1280
1281  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1282    Error("source location entry ID out-of-range for AST file");
1283    return std::make_pair(SourceLocation(), "");
1284  }
1285
1286  // Find which module file this entry lands in.
1287  ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1288  if (M->Kind != MK_Module)
1289    return std::make_pair(SourceLocation(), "");
1290
1291  // FIXME: Can we map this down to a particular submodule? That would be
1292  // ideal.
1293  return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1294}
1295
1296/// \brief Find the location where the module F is imported.
1297SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1298  if (F->ImportLoc.isValid())
1299    return F->ImportLoc;
1300
1301  // Otherwise we have a PCH. It's considered to be "imported" at the first
1302  // location of its includer.
1303  if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1304    // Main file is the importer.
1305    assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1306    return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1307  }
1308  return F->ImportedBy[0]->FirstLoc;
1309}
1310
1311/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1312/// specified cursor.  Read the abbreviations that are at the top of the block
1313/// and then leave the cursor pointing into the block.
1314bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1315  if (Cursor.EnterSubBlock(BlockID)) {
1316    Error("malformed block record in AST file");
1317    return Failure;
1318  }
1319
1320  while (true) {
1321    uint64_t Offset = Cursor.GetCurrentBitNo();
1322    unsigned Code = Cursor.ReadCode();
1323
1324    // We expect all abbrevs to be at the start of the block.
1325    if (Code != llvm::bitc::DEFINE_ABBREV) {
1326      Cursor.JumpToBit(Offset);
1327      return false;
1328    }
1329    Cursor.ReadAbbrevRecord();
1330  }
1331}
1332
1333Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1334                           unsigned &Idx) {
1335  Token Tok;
1336  Tok.startToken();
1337  Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1338  Tok.setLength(Record[Idx++]);
1339  if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1340    Tok.setIdentifierInfo(II);
1341  Tok.setKind((tok::TokenKind)Record[Idx++]);
1342  Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1343  return Tok;
1344}
1345
1346MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1347  BitstreamCursor &Stream = F.MacroCursor;
1348
1349  // Keep track of where we are in the stream, then jump back there
1350  // after reading this macro.
1351  SavedStreamPosition SavedPosition(Stream);
1352
1353  Stream.JumpToBit(Offset);
1354  RecordData Record;
1355  SmallVector<IdentifierInfo*, 16> MacroArgs;
1356  MacroInfo *Macro = nullptr;
1357
1358  while (true) {
1359    // Advance to the next record, but if we get to the end of the block, don't
1360    // pop it (removing all the abbreviations from the cursor) since we want to
1361    // be able to reseek within the block and read entries.
1362    unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1363    llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1364
1365    switch (Entry.Kind) {
1366    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1367    case llvm::BitstreamEntry::Error:
1368      Error("malformed block record in AST file");
1369      return Macro;
1370    case llvm::BitstreamEntry::EndBlock:
1371      return Macro;
1372    case llvm::BitstreamEntry::Record:
1373      // The interesting case.
1374      break;
1375    }
1376
1377    // Read a record.
1378    Record.clear();
1379    PreprocessorRecordTypes RecType =
1380      (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1381    switch (RecType) {
1382    case PP_MACRO_DIRECTIVE_HISTORY:
1383      return Macro;
1384
1385    case PP_MACRO_OBJECT_LIKE:
1386    case PP_MACRO_FUNCTION_LIKE: {
1387      // If we already have a macro, that means that we've hit the end
1388      // of the definition of the macro we were looking for. We're
1389      // done.
1390      if (Macro)
1391        return Macro;
1392
1393      unsigned NextIndex = 1; // Skip identifier ID.
1394      SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1395      SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1396      MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1397      MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1398      MI->setIsUsed(Record[NextIndex++]);
1399      MI->setUsedForHeaderGuard(Record[NextIndex++]);
1400
1401      if (RecType == PP_MACRO_FUNCTION_LIKE) {
1402        // Decode function-like macro info.
1403        bool isC99VarArgs = Record[NextIndex++];
1404        bool isGNUVarArgs = Record[NextIndex++];
1405        bool hasCommaPasting = Record[NextIndex++];
1406        MacroArgs.clear();
1407        unsigned NumArgs = Record[NextIndex++];
1408        for (unsigned i = 0; i != NumArgs; ++i)
1409          MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1410
1411        // Install function-like macro info.
1412        MI->setIsFunctionLike();
1413        if (isC99VarArgs) MI->setIsC99Varargs();
1414        if (isGNUVarArgs) MI->setIsGNUVarargs();
1415        if (hasCommaPasting) MI->setHasCommaPasting();
1416        MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1417                            PP.getPreprocessorAllocator());
1418      }
1419
1420      // Remember that we saw this macro last so that we add the tokens that
1421      // form its body to it.
1422      Macro = MI;
1423
1424      if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1425          Record[NextIndex]) {
1426        // We have a macro definition. Register the association
1427        PreprocessedEntityID
1428            GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1429        PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1430        PreprocessingRecord::PPEntityID
1431          PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1432        MacroDefinition *PPDef =
1433          cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1434        if (PPDef)
1435          PPRec.RegisterMacroDefinition(Macro, PPDef);
1436      }
1437
1438      ++NumMacrosRead;
1439      break;
1440    }
1441
1442    case PP_TOKEN: {
1443      // If we see a TOKEN before a PP_MACRO_*, then the file is
1444      // erroneous, just pretend we didn't see this.
1445      if (!Macro) break;
1446
1447      unsigned Idx = 0;
1448      Token Tok = ReadToken(F, Record, Idx);
1449      Macro->AddTokenToBody(Tok);
1450      break;
1451    }
1452    }
1453  }
1454}
1455
1456PreprocessedEntityID
1457ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1458  ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1459    I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1460  assert(I != M.PreprocessedEntityRemap.end()
1461         && "Invalid index into preprocessed entity index remap");
1462
1463  return LocalID + I->second;
1464}
1465
1466unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1467  return llvm::hash_combine(ikey.Size, ikey.ModTime);
1468}
1469
1470HeaderFileInfoTrait::internal_key_type
1471HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1472  internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1473                             FE->getName() };
1474  return ikey;
1475}
1476
1477bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1478  if (a.Size != b.Size || a.ModTime != b.ModTime)
1479    return false;
1480
1481  if (strcmp(a.Filename, b.Filename) == 0)
1482    return true;
1483
1484  // Determine whether the actual files are equivalent.
1485  FileManager &FileMgr = Reader.getFileManager();
1486  const FileEntry *FEA = FileMgr.getFile(a.Filename);
1487  const FileEntry *FEB = FileMgr.getFile(b.Filename);
1488  return (FEA && FEA == FEB);
1489}
1490
1491std::pair<unsigned, unsigned>
1492HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1493  using namespace llvm::support;
1494  unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1495  unsigned DataLen = (unsigned) *d++;
1496  return std::make_pair(KeyLen, DataLen);
1497}
1498
1499HeaderFileInfoTrait::internal_key_type
1500HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1501  using namespace llvm::support;
1502  internal_key_type ikey;
1503  ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1504  ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1505  ikey.Filename = (const char *)d;
1506  return ikey;
1507}
1508
1509HeaderFileInfoTrait::data_type
1510HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1511                              unsigned DataLen) {
1512  const unsigned char *End = d + DataLen;
1513  using namespace llvm::support;
1514  HeaderFileInfo HFI;
1515  unsigned Flags = *d++;
1516  HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1517                   ((Flags >> 6) & 0x03);
1518  HFI.isImport = (Flags >> 5) & 0x01;
1519  HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1520  HFI.DirInfo = (Flags >> 2) & 0x03;
1521  HFI.Resolved = (Flags >> 1) & 0x01;
1522  HFI.IndexHeaderMapHeader = Flags & 0x01;
1523  HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1524  HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1525      M, endian::readNext<uint32_t, little, unaligned>(d));
1526  if (unsigned FrameworkOffset =
1527          endian::readNext<uint32_t, little, unaligned>(d)) {
1528    // The framework offset is 1 greater than the actual offset,
1529    // since 0 is used as an indicator for "no framework name".
1530    StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1531    HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1532  }
1533
1534  if (d != End) {
1535    uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1536    if (LocalSMID) {
1537      // This header is part of a module. Associate it with the module to enable
1538      // implicit module import.
1539      SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1540      Module *Mod = Reader.getSubmodule(GlobalSMID);
1541      HFI.isModuleHeader = true;
1542      FileManager &FileMgr = Reader.getFileManager();
1543      ModuleMap &ModMap =
1544          Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1545      ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
1546    }
1547  }
1548
1549  assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1550  (void)End;
1551
1552  // This HeaderFileInfo was externally loaded.
1553  HFI.External = true;
1554  return HFI;
1555}
1556
1557void
1558ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1559                                     GlobalMacroID GMacID,
1560                                     ArrayRef<SubmoduleID> Overrides) {
1561  assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1562  SubmoduleID *OverrideData = nullptr;
1563  if (!Overrides.empty()) {
1564    OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1565    OverrideData[0] = Overrides.size();
1566    for (unsigned I = 0; I != Overrides.size(); ++I)
1567      OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1568  }
1569  PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
1570}
1571
1572void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1573                                       ModuleFile *M,
1574                                       uint64_t MacroDirectivesOffset) {
1575  assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1576  PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1577}
1578
1579void ASTReader::ReadDefinedMacros() {
1580  // Note that we are loading defined macros.
1581  Deserializing Macros(this);
1582
1583  for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1584      E = ModuleMgr.rend(); I != E; ++I) {
1585    BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1586
1587    // If there was no preprocessor block, skip this file.
1588    if (!MacroCursor.getBitStreamReader())
1589      continue;
1590
1591    BitstreamCursor Cursor = MacroCursor;
1592    Cursor.JumpToBit((*I)->MacroStartOffset);
1593
1594    RecordData Record;
1595    while (true) {
1596      llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1597
1598      switch (E.Kind) {
1599      case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1600      case llvm::BitstreamEntry::Error:
1601        Error("malformed block record in AST file");
1602        return;
1603      case llvm::BitstreamEntry::EndBlock:
1604        goto NextCursor;
1605
1606      case llvm::BitstreamEntry::Record:
1607        Record.clear();
1608        switch (Cursor.readRecord(E.ID, Record)) {
1609        default:  // Default behavior: ignore.
1610          break;
1611
1612        case PP_MACRO_OBJECT_LIKE:
1613        case PP_MACRO_FUNCTION_LIKE:
1614          getLocalIdentifier(**I, Record[0]);
1615          break;
1616
1617        case PP_TOKEN:
1618          // Ignore tokens.
1619          break;
1620        }
1621        break;
1622      }
1623    }
1624    NextCursor:  ;
1625  }
1626}
1627
1628namespace {
1629  /// \brief Visitor class used to look up identifirs in an AST file.
1630  class IdentifierLookupVisitor {
1631    StringRef Name;
1632    unsigned PriorGeneration;
1633    unsigned &NumIdentifierLookups;
1634    unsigned &NumIdentifierLookupHits;
1635    IdentifierInfo *Found;
1636
1637  public:
1638    IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1639                            unsigned &NumIdentifierLookups,
1640                            unsigned &NumIdentifierLookupHits)
1641      : Name(Name), PriorGeneration(PriorGeneration),
1642        NumIdentifierLookups(NumIdentifierLookups),
1643        NumIdentifierLookupHits(NumIdentifierLookupHits),
1644        Found()
1645    {
1646    }
1647
1648    static bool visit(ModuleFile &M, void *UserData) {
1649      IdentifierLookupVisitor *This
1650        = static_cast<IdentifierLookupVisitor *>(UserData);
1651
1652      // If we've already searched this module file, skip it now.
1653      if (M.Generation <= This->PriorGeneration)
1654        return true;
1655
1656      ASTIdentifierLookupTable *IdTable
1657        = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1658      if (!IdTable)
1659        return false;
1660
1661      ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1662                                     M, This->Found);
1663      ++This->NumIdentifierLookups;
1664      ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
1665      if (Pos == IdTable->end())
1666        return false;
1667
1668      // Dereferencing the iterator has the effect of building the
1669      // IdentifierInfo node and populating it with the various
1670      // declarations it needs.
1671      ++This->NumIdentifierLookupHits;
1672      This->Found = *Pos;
1673      return true;
1674    }
1675
1676    // \brief Retrieve the identifier info found within the module
1677    // files.
1678    IdentifierInfo *getIdentifierInfo() const { return Found; }
1679  };
1680}
1681
1682void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1683  // Note that we are loading an identifier.
1684  Deserializing AnIdentifier(this);
1685
1686  unsigned PriorGeneration = 0;
1687  if (getContext().getLangOpts().Modules)
1688    PriorGeneration = IdentifierGeneration[&II];
1689
1690  // If there is a global index, look there first to determine which modules
1691  // provably do not have any results for this identifier.
1692  GlobalModuleIndex::HitSet Hits;
1693  GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1694  if (!loadGlobalIndex()) {
1695    if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1696      HitsPtr = &Hits;
1697    }
1698  }
1699
1700  IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1701                                  NumIdentifierLookups,
1702                                  NumIdentifierLookupHits);
1703  ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1704  markIdentifierUpToDate(&II);
1705}
1706
1707void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1708  if (!II)
1709    return;
1710
1711  II->setOutOfDate(false);
1712
1713  // Update the generation for this identifier.
1714  if (getContext().getLangOpts().Modules)
1715    IdentifierGeneration[II] = getGeneration();
1716}
1717
1718struct ASTReader::ModuleMacroInfo {
1719  SubmoduleID SubModID;
1720  MacroInfo *MI;
1721  SubmoduleID *Overrides;
1722  // FIXME: Remove this.
1723  ModuleFile *F;
1724
1725  bool isDefine() const { return MI; }
1726
1727  SubmoduleID getSubmoduleID() const { return SubModID; }
1728
1729  ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1730    if (!Overrides)
1731      return None;
1732    return llvm::makeArrayRef(Overrides + 1, *Overrides);
1733  }
1734
1735  DefMacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1736    if (!MI)
1737      return nullptr;
1738    return PP.AllocateDefMacroDirective(MI, ImportLoc, /*isImported=*/true);
1739  }
1740};
1741
1742ASTReader::ModuleMacroInfo *
1743ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1744  ModuleMacroInfo Info;
1745
1746  uint32_t ID = PMInfo.ModuleMacroData.MacID;
1747  if (ID & 1) {
1748    // Macro undefinition.
1749    Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
1750    Info.MI = nullptr;
1751  } else {
1752    // Macro definition.
1753    GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1754    assert(GMacID);
1755
1756    // If this macro has already been loaded, don't do so again.
1757    // FIXME: This is highly dubious. Multiple macro definitions can have the
1758    // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1759    if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1760      return nullptr;
1761
1762    Info.MI = getMacro(GMacID);
1763    Info.SubModID = Info.MI->getOwningModuleID();
1764  }
1765  Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1766  Info.F = PMInfo.M;
1767
1768  return new (Context) ModuleMacroInfo(Info);
1769}
1770
1771void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1772                                    const PendingMacroInfo &PMInfo) {
1773  assert(II);
1774
1775  if (PMInfo.M->Kind != MK_Module) {
1776    installPCHMacroDirectives(II, *PMInfo.M,
1777                              PMInfo.PCHMacroData.MacroDirectivesOffset);
1778    return;
1779  }
1780
1781  // Module Macro.
1782
1783  ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1784  if (!MMI)
1785    return;
1786
1787  Module *Owner = getSubmodule(MMI->getSubmoduleID());
1788  if (Owner && Owner->NameVisibility == Module::Hidden) {
1789    // Macros in the owning module are hidden. Just remember this macro to
1790    // install if we make this module visible.
1791    HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1792  } else {
1793    installImportedMacro(II, MMI, Owner);
1794  }
1795}
1796
1797void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1798                                          ModuleFile &M, uint64_t Offset) {
1799  assert(M.Kind != MK_Module);
1800
1801  BitstreamCursor &Cursor = M.MacroCursor;
1802  SavedStreamPosition SavedPosition(Cursor);
1803  Cursor.JumpToBit(Offset);
1804
1805  llvm::BitstreamEntry Entry =
1806      Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1807  if (Entry.Kind != llvm::BitstreamEntry::Record) {
1808    Error("malformed block record in AST file");
1809    return;
1810  }
1811
1812  RecordData Record;
1813  PreprocessorRecordTypes RecType =
1814    (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1815  if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1816    Error("malformed block record in AST file");
1817    return;
1818  }
1819
1820  // Deserialize the macro directives history in reverse source-order.
1821  MacroDirective *Latest = nullptr, *Earliest = nullptr;
1822  unsigned Idx = 0, N = Record.size();
1823  while (Idx < N) {
1824    MacroDirective *MD = nullptr;
1825    SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1826    MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1827    switch (K) {
1828    case MacroDirective::MD_Define: {
1829      GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1830      MacroInfo *MI = getMacro(GMacID);
1831      bool isImported = Record[Idx++];
1832      bool isAmbiguous = Record[Idx++];
1833      DefMacroDirective *DefMD =
1834          PP.AllocateDefMacroDirective(MI, Loc, isImported);
1835      DefMD->setAmbiguous(isAmbiguous);
1836      MD = DefMD;
1837      break;
1838    }
1839    case MacroDirective::MD_Undefine:
1840      MD = PP.AllocateUndefMacroDirective(Loc);
1841      break;
1842    case MacroDirective::MD_Visibility: {
1843      bool isPublic = Record[Idx++];
1844      MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1845      break;
1846    }
1847    }
1848
1849    if (!Latest)
1850      Latest = MD;
1851    if (Earliest)
1852      Earliest->setPrevious(MD);
1853    Earliest = MD;
1854  }
1855
1856  PP.setLoadedMacroDirective(II, Latest);
1857}
1858
1859/// \brief For the given macro definitions, check if they are both in system
1860/// modules.
1861static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1862                                      Module *NewOwner, ASTReader &Reader) {
1863  assert(PrevMI && NewMI);
1864  Module *PrevOwner = nullptr;
1865  if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1866    PrevOwner = Reader.getSubmodule(PrevModID);
1867  SourceManager &SrcMgr = Reader.getSourceManager();
1868  bool PrevInSystem
1869    = PrevOwner? PrevOwner->IsSystem
1870               : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1871  bool NewInSystem
1872    = NewOwner? NewOwner->IsSystem
1873              : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1874  if (PrevOwner && PrevOwner == NewOwner)
1875    return false;
1876  return PrevInSystem && NewInSystem;
1877}
1878
1879void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1880                                       AmbiguousMacros &Ambig,
1881                                       ArrayRef<SubmoduleID> Overrides) {
1882  for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1883    SubmoduleID OwnerID = Overrides[OI];
1884
1885    // If this macro is not yet visible, remove it from the hidden names list.
1886    Module *Owner = getSubmodule(OwnerID);
1887    HiddenNames &Hidden = HiddenNamesMap[Owner];
1888    HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1889    if (HI != Hidden.HiddenMacros.end()) {
1890      auto SubOverrides = HI->second->getOverriddenSubmodules();
1891      Hidden.HiddenMacros.erase(HI);
1892      removeOverriddenMacros(II, Ambig, SubOverrides);
1893    }
1894
1895    // If this macro is already in our list of conflicts, remove it from there.
1896    Ambig.erase(
1897        std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1898          return MD->getInfo()->getOwningModuleID() == OwnerID;
1899        }),
1900        Ambig.end());
1901  }
1902}
1903
1904ASTReader::AmbiguousMacros *
1905ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1906                                  ArrayRef<SubmoduleID> Overrides) {
1907  MacroDirective *Prev = PP.getMacroDirective(II);
1908  if (!Prev && Overrides.empty())
1909    return nullptr;
1910
1911  DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
1912                                    : nullptr;
1913  if (PrevDef && PrevDef->isAmbiguous()) {
1914    // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1915    AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1916    Ambig.push_back(PrevDef);
1917
1918    removeOverriddenMacros(II, Ambig, Overrides);
1919
1920    if (!Ambig.empty())
1921      return &Ambig;
1922
1923    AmbiguousMacroDefs.erase(II);
1924  } else {
1925    // There's no ambiguity yet. Maybe we're introducing one.
1926    AmbiguousMacros Ambig;
1927    if (PrevDef)
1928      Ambig.push_back(PrevDef);
1929
1930    removeOverriddenMacros(II, Ambig, Overrides);
1931
1932    if (!Ambig.empty()) {
1933      AmbiguousMacros &Result = AmbiguousMacroDefs[II];
1934      std::swap(Result, Ambig);
1935      return &Result;
1936    }
1937  }
1938
1939  // We ended up with no ambiguity.
1940  return nullptr;
1941}
1942
1943void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1944                                     Module *Owner) {
1945  assert(II && Owner);
1946
1947  SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
1948  if (ImportLoc.isInvalid()) {
1949    // FIXME: If we made macros from this module visible but didn't provide a
1950    // source location for the import, we don't have a location for the macro.
1951    // Use the location at which the containing module file was first imported
1952    // for now.
1953    ImportLoc = MMI->F->DirectImportLoc;
1954    assert(ImportLoc.isValid() && "no import location for a visible macro?");
1955  }
1956
1957  AmbiguousMacros *Prev =
1958      removeOverriddenMacros(II, MMI->getOverriddenSubmodules());
1959
1960  // Create a synthetic macro definition corresponding to the import (or null
1961  // if this was an undefinition of the macro).
1962  DefMacroDirective *MD = MMI->import(PP, ImportLoc);
1963
1964  // If there's no ambiguity, just install the macro.
1965  if (!Prev) {
1966    if (MD)
1967      PP.appendMacroDirective(II, MD);
1968    else
1969      PP.appendMacroDirective(II, PP.AllocateUndefMacroDirective(ImportLoc));
1970    return;
1971  }
1972  assert(!Prev->empty());
1973
1974  if (!MD) {
1975    // We imported a #undef that didn't remove all prior definitions. The most
1976    // recent prior definition remains, and we install it in the place of the
1977    // imported directive.
1978    MacroInfo *NewMI = Prev->back()->getInfo();
1979    Prev->pop_back();
1980    MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc, /*Imported*/true);
1981  }
1982
1983  // We're introducing a macro definition that creates or adds to an ambiguity.
1984  // We can resolve that ambiguity if this macro is token-for-token identical to
1985  // all of the existing definitions.
1986  MacroInfo *NewMI = MD->getInfo();
1987  assert(NewMI && "macro definition with no MacroInfo?");
1988  while (!Prev->empty()) {
1989    MacroInfo *PrevMI = Prev->back()->getInfo();
1990    assert(PrevMI && "macro definition with no MacroInfo?");
1991
1992    // Before marking the macros as ambiguous, check if this is a case where
1993    // both macros are in system headers. If so, we trust that the system
1994    // did not get it wrong. This also handles cases where Clang's own
1995    // headers have a different spelling of certain system macros:
1996    //   #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1997    //   #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
1998    //
1999    // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2000    // overrides the system limits.h's macros, so there's no conflict here.
2001    if (NewMI != PrevMI &&
2002        !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2003        !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2004      break;
2005
2006    // The previous definition is the same as this one (or both are defined in
2007    // system modules so we can assume they're equivalent); we don't need to
2008    // track it any more.
2009    Prev->pop_back();
2010  }
2011
2012  if (!Prev->empty())
2013    MD->setAmbiguous(true);
2014
2015  PP.appendMacroDirective(II, MD);
2016}
2017
2018ASTReader::InputFileInfo
2019ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2020  // Go find this input file.
2021  BitstreamCursor &Cursor = F.InputFilesCursor;
2022  SavedStreamPosition SavedPosition(Cursor);
2023  Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2024
2025  unsigned Code = Cursor.ReadCode();
2026  RecordData Record;
2027  StringRef Blob;
2028
2029  unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2030  assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2031         "invalid record type for input file");
2032  (void)Result;
2033
2034  std::string Filename;
2035  off_t StoredSize;
2036  time_t StoredTime;
2037  bool Overridden;
2038
2039  assert(Record[0] == ID && "Bogus stored ID or offset");
2040  StoredSize = static_cast<off_t>(Record[1]);
2041  StoredTime = static_cast<time_t>(Record[2]);
2042  Overridden = static_cast<bool>(Record[3]);
2043  Filename = Blob;
2044  MaybeAddSystemRootToFilename(F, Filename);
2045
2046  InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2047  return R;
2048}
2049
2050std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
2051  return readInputFileInfo(F, ID).Filename;
2052}
2053
2054InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2055  // If this ID is bogus, just return an empty input file.
2056  if (ID == 0 || ID > F.InputFilesLoaded.size())
2057    return InputFile();
2058
2059  // If we've already loaded this input file, return it.
2060  if (F.InputFilesLoaded[ID-1].getFile())
2061    return F.InputFilesLoaded[ID-1];
2062
2063  if (F.InputFilesLoaded[ID-1].isNotFound())
2064    return InputFile();
2065
2066  // Go find this input file.
2067  BitstreamCursor &Cursor = F.InputFilesCursor;
2068  SavedStreamPosition SavedPosition(Cursor);
2069  Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2070
2071  InputFileInfo FI = readInputFileInfo(F, ID);
2072  off_t StoredSize = FI.StoredSize;
2073  time_t StoredTime = FI.StoredTime;
2074  bool Overridden = FI.Overridden;
2075  StringRef Filename = FI.Filename;
2076
2077  const FileEntry *File
2078    = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2079                : FileMgr.getFile(Filename, /*OpenFile=*/false);
2080
2081  // If we didn't find the file, resolve it relative to the
2082  // original directory from which this AST file was created.
2083  if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
2084      F.OriginalDir != CurrentDir) {
2085    std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2086                                                            F.OriginalDir,
2087                                                            CurrentDir);
2088    if (!Resolved.empty())
2089      File = FileMgr.getFile(Resolved);
2090  }
2091
2092  // For an overridden file, create a virtual file with the stored
2093  // size/timestamp.
2094  if (Overridden && File == nullptr) {
2095    File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2096  }
2097
2098  if (File == nullptr) {
2099    if (Complain) {
2100      std::string ErrorStr = "could not find file '";
2101      ErrorStr += Filename;
2102      ErrorStr += "' referenced by AST file";
2103      Error(ErrorStr.c_str());
2104    }
2105    // Record that we didn't find the file.
2106    F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2107    return InputFile();
2108  }
2109
2110  // Check if there was a request to override the contents of the file
2111  // that was part of the precompiled header. Overridding such a file
2112  // can lead to problems when lexing using the source locations from the
2113  // PCH.
2114  SourceManager &SM = getSourceManager();
2115  if (!Overridden && SM.isFileOverridden(File)) {
2116    if (Complain)
2117      Error(diag::err_fe_pch_file_overridden, Filename);
2118    // After emitting the diagnostic, recover by disabling the override so
2119    // that the original file will be used.
2120    SM.disableFileContentsOverride(File);
2121    // The FileEntry is a virtual file entry with the size of the contents
2122    // that would override the original contents. Set it to the original's
2123    // size/time.
2124    FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2125                            StoredSize, StoredTime);
2126  }
2127
2128  bool IsOutOfDate = false;
2129
2130  // For an overridden file, there is nothing to validate.
2131  if (!Overridden && (StoredSize != File->getSize()
2132#if !defined(LLVM_ON_WIN32)
2133       // In our regression testing, the Windows file system seems to
2134       // have inconsistent modification times that sometimes
2135       // erroneously trigger this error-handling path.
2136       || StoredTime != File->getModificationTime()
2137#endif
2138       )) {
2139    if (Complain) {
2140      // Build a list of the PCH imports that got us here (in reverse).
2141      SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2142      while (ImportStack.back()->ImportedBy.size() > 0)
2143        ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2144
2145      // The top-level PCH is stale.
2146      StringRef TopLevelPCHName(ImportStack.back()->FileName);
2147      Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2148
2149      // Print the import stack.
2150      if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2151        Diag(diag::note_pch_required_by)
2152          << Filename << ImportStack[0]->FileName;
2153        for (unsigned I = 1; I < ImportStack.size(); ++I)
2154          Diag(diag::note_pch_required_by)
2155            << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2156      }
2157
2158      if (!Diags.isDiagnosticInFlight())
2159        Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2160    }
2161
2162    IsOutOfDate = true;
2163  }
2164
2165  InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2166
2167  // Note that we've loaded this input file.
2168  F.InputFilesLoaded[ID-1] = IF;
2169  return IF;
2170}
2171
2172const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2173  ModuleFile &M = ModuleMgr.getPrimaryModule();
2174  std::string Filename = filenameStrRef;
2175  MaybeAddSystemRootToFilename(M, Filename);
2176  const FileEntry *File = FileMgr.getFile(Filename);
2177  if (File == nullptr && !M.OriginalDir.empty() && !CurrentDir.empty() &&
2178      M.OriginalDir != CurrentDir) {
2179    std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2180                                                            M.OriginalDir,
2181                                                            CurrentDir);
2182    if (!resolved.empty())
2183      File = FileMgr.getFile(resolved);
2184  }
2185
2186  return File;
2187}
2188
2189/// \brief If we are loading a relocatable PCH file, and the filename is
2190/// not an absolute path, add the system root to the beginning of the file
2191/// name.
2192void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2193                                             std::string &Filename) {
2194  // If this is not a relocatable PCH file, there's nothing to do.
2195  if (!M.RelocatablePCH)
2196    return;
2197
2198  if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2199    return;
2200
2201  if (isysroot.empty()) {
2202    // If no system root was given, default to '/'
2203    Filename.insert(Filename.begin(), '/');
2204    return;
2205  }
2206
2207  unsigned Length = isysroot.size();
2208  if (isysroot[Length - 1] != '/')
2209    Filename.insert(Filename.begin(), '/');
2210
2211  Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2212}
2213
2214ASTReader::ASTReadResult
2215ASTReader::ReadControlBlock(ModuleFile &F,
2216                            SmallVectorImpl<ImportedModule> &Loaded,
2217                            const ModuleFile *ImportedBy,
2218                            unsigned ClientLoadCapabilities) {
2219  BitstreamCursor &Stream = F.Stream;
2220
2221  if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2222    Error("malformed block record in AST file");
2223    return Failure;
2224  }
2225
2226  // Read all of the records and blocks in the control block.
2227  RecordData Record;
2228  while (1) {
2229    llvm::BitstreamEntry Entry = Stream.advance();
2230
2231    switch (Entry.Kind) {
2232    case llvm::BitstreamEntry::Error:
2233      Error("malformed block record in AST file");
2234      return Failure;
2235    case llvm::BitstreamEntry::EndBlock: {
2236      // Validate input files.
2237      const HeaderSearchOptions &HSOpts =
2238          PP.getHeaderSearchInfo().getHeaderSearchOpts();
2239
2240      // All user input files reside at the index range [0, Record[1]), and
2241      // system input files reside at [Record[1], Record[0]).
2242      // Record is the one from INPUT_FILE_OFFSETS.
2243      unsigned NumInputs = Record[0];
2244      unsigned NumUserInputs = Record[1];
2245
2246      if (!DisableValidation &&
2247          (ValidateSystemInputs || !HSOpts.ModulesValidateOncePerBuildSession ||
2248           F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
2249        bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2250
2251        // If we are reading a module, we will create a verification timestamp,
2252        // so we verify all input files.  Otherwise, verify only user input
2253        // files.
2254
2255        unsigned N = NumUserInputs;
2256        if (ValidateSystemInputs ||
2257            (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module))
2258          N = NumInputs;
2259
2260        for (unsigned I = 0; I < N; ++I) {
2261          InputFile IF = getInputFile(F, I+1, Complain);
2262          if (!IF.getFile() || IF.isOutOfDate())
2263            return OutOfDate;
2264        }
2265      }
2266
2267      if (Listener)
2268        Listener->visitModuleFile(F.FileName);
2269
2270      if (Listener && Listener->needsInputFileVisitation()) {
2271        unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2272                                                                : NumUserInputs;
2273        for (unsigned I = 0; I < N; ++I) {
2274          bool IsSystem = I >= NumUserInputs;
2275          InputFileInfo FI = readInputFileInfo(F, I+1);
2276          Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2277        }
2278      }
2279
2280      return Success;
2281    }
2282
2283    case llvm::BitstreamEntry::SubBlock:
2284      switch (Entry.ID) {
2285      case INPUT_FILES_BLOCK_ID:
2286        F.InputFilesCursor = Stream;
2287        if (Stream.SkipBlock() || // Skip with the main cursor
2288            // Read the abbreviations
2289            ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2290          Error("malformed block record in AST file");
2291          return Failure;
2292        }
2293        continue;
2294
2295      default:
2296        if (Stream.SkipBlock()) {
2297          Error("malformed block record in AST file");
2298          return Failure;
2299        }
2300        continue;
2301      }
2302
2303    case llvm::BitstreamEntry::Record:
2304      // The interesting case.
2305      break;
2306    }
2307
2308    // Read and process a record.
2309    Record.clear();
2310    StringRef Blob;
2311    switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2312    case METADATA: {
2313      if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2314        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2315          Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2316                                        : diag::err_pch_version_too_new);
2317        return VersionMismatch;
2318      }
2319
2320      bool hasErrors = Record[5];
2321      if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2322        Diag(diag::err_pch_with_compiler_errors);
2323        return HadErrors;
2324      }
2325
2326      F.RelocatablePCH = Record[4];
2327
2328      const std::string &CurBranch = getClangFullRepositoryVersion();
2329      StringRef ASTBranch = Blob;
2330      if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2331        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2332          Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2333        return VersionMismatch;
2334      }
2335      break;
2336    }
2337
2338    case IMPORTS: {
2339      // Load each of the imported PCH files.
2340      unsigned Idx = 0, N = Record.size();
2341      while (Idx < N) {
2342        // Read information about the AST file.
2343        ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2344        // The import location will be the local one for now; we will adjust
2345        // all import locations of module imports after the global source
2346        // location info are setup.
2347        SourceLocation ImportLoc =
2348            SourceLocation::getFromRawEncoding(Record[Idx++]);
2349        off_t StoredSize = (off_t)Record[Idx++];
2350        time_t StoredModTime = (time_t)Record[Idx++];
2351        unsigned Length = Record[Idx++];
2352        SmallString<128> ImportedFile(Record.begin() + Idx,
2353                                      Record.begin() + Idx + Length);
2354        Idx += Length;
2355
2356        // Load the AST file.
2357        switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
2358                           StoredSize, StoredModTime,
2359                           ClientLoadCapabilities)) {
2360        case Failure: return Failure;
2361          // If we have to ignore the dependency, we'll have to ignore this too.
2362        case Missing:
2363        case OutOfDate: return OutOfDate;
2364        case VersionMismatch: return VersionMismatch;
2365        case ConfigurationMismatch: return ConfigurationMismatch;
2366        case HadErrors: return HadErrors;
2367        case Success: break;
2368        }
2369      }
2370      break;
2371    }
2372
2373    case LANGUAGE_OPTIONS: {
2374      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2375      if (Listener && &F == *ModuleMgr.begin() &&
2376          ParseLanguageOptions(Record, Complain, *Listener) &&
2377          !DisableValidation && !AllowConfigurationMismatch)
2378        return ConfigurationMismatch;
2379      break;
2380    }
2381
2382    case TARGET_OPTIONS: {
2383      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2384      if (Listener && &F == *ModuleMgr.begin() &&
2385          ParseTargetOptions(Record, Complain, *Listener) &&
2386          !DisableValidation && !AllowConfigurationMismatch)
2387        return ConfigurationMismatch;
2388      break;
2389    }
2390
2391    case DIAGNOSTIC_OPTIONS: {
2392      bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
2393      if (Listener && &F == *ModuleMgr.begin() &&
2394          ParseDiagnosticOptions(Record, Complain, *Listener) &&
2395          !DisableValidation)
2396        return OutOfDate;
2397      break;
2398    }
2399
2400    case FILE_SYSTEM_OPTIONS: {
2401      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2402      if (Listener && &F == *ModuleMgr.begin() &&
2403          ParseFileSystemOptions(Record, Complain, *Listener) &&
2404          !DisableValidation && !AllowConfigurationMismatch)
2405        return ConfigurationMismatch;
2406      break;
2407    }
2408
2409    case HEADER_SEARCH_OPTIONS: {
2410      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2411      if (Listener && &F == *ModuleMgr.begin() &&
2412          ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2413          !DisableValidation && !AllowConfigurationMismatch)
2414        return ConfigurationMismatch;
2415      break;
2416    }
2417
2418    case PREPROCESSOR_OPTIONS: {
2419      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2420      if (Listener && &F == *ModuleMgr.begin() &&
2421          ParsePreprocessorOptions(Record, Complain, *Listener,
2422                                   SuggestedPredefines) &&
2423          !DisableValidation && !AllowConfigurationMismatch)
2424        return ConfigurationMismatch;
2425      break;
2426    }
2427
2428    case ORIGINAL_FILE:
2429      F.OriginalSourceFileID = FileID::get(Record[0]);
2430      F.ActualOriginalSourceFileName = Blob;
2431      F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2432      MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2433      break;
2434
2435    case ORIGINAL_FILE_ID:
2436      F.OriginalSourceFileID = FileID::get(Record[0]);
2437      break;
2438
2439    case ORIGINAL_PCH_DIR:
2440      F.OriginalDir = Blob;
2441      break;
2442
2443    case MODULE_NAME:
2444      F.ModuleName = Blob;
2445      if (Listener)
2446        Listener->ReadModuleName(F.ModuleName);
2447      break;
2448
2449    case MODULE_MAP_FILE:
2450      F.ModuleMapPath = Blob;
2451
2452      // Try to resolve ModuleName in the current header search context and
2453      // verify that it is found in the same module map file as we saved. If the
2454      // top-level AST file is a main file, skip this check because there is no
2455      // usable header search context.
2456      assert(!F.ModuleName.empty() &&
2457             "MODULE_NAME should come before MOUDLE_MAP_FILE");
2458      if (F.Kind == MK_Module &&
2459          (*ModuleMgr.begin())->Kind != MK_MainFile) {
2460        Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2461        if (!M) {
2462          assert(ImportedBy && "top-level import should be verified");
2463          if ((ClientLoadCapabilities & ARR_Missing) == 0)
2464            Diag(diag::err_imported_module_not_found)
2465              << F.ModuleName << ImportedBy->FileName;
2466          return Missing;
2467        }
2468
2469        const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
2470        if (StoredModMap == nullptr || StoredModMap != M->ModuleMap) {
2471          assert(M->ModuleMap && "found module is missing module map file");
2472          assert(M->Name == F.ModuleName && "found module with different name");
2473          assert(ImportedBy && "top-level import should be verified");
2474          if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2475            Diag(diag::err_imported_module_modmap_changed)
2476              << F.ModuleName << ImportedBy->FileName
2477              << M->ModuleMap->getName() << F.ModuleMapPath;
2478          return OutOfDate;
2479        }
2480      }
2481
2482      if (Listener)
2483        Listener->ReadModuleMapFile(F.ModuleMapPath);
2484      break;
2485
2486    case INPUT_FILE_OFFSETS:
2487      F.InputFileOffsets = (const uint32_t *)Blob.data();
2488      F.InputFilesLoaded.resize(Record[0]);
2489      break;
2490    }
2491  }
2492}
2493
2494ASTReader::ASTReadResult
2495ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2496  BitstreamCursor &Stream = F.Stream;
2497
2498  if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2499    Error("malformed block record in AST file");
2500    return Failure;
2501  }
2502
2503  // Read all of the records and blocks for the AST file.
2504  RecordData Record;
2505  while (1) {
2506    llvm::BitstreamEntry Entry = Stream.advance();
2507
2508    switch (Entry.Kind) {
2509    case llvm::BitstreamEntry::Error:
2510      Error("error at end of module block in AST file");
2511      return Failure;
2512    case llvm::BitstreamEntry::EndBlock: {
2513      // Outside of C++, we do not store a lookup map for the translation unit.
2514      // Instead, mark it as needing a lookup map to be built if this module
2515      // contains any declarations lexically within it (which it always does!).
2516      // This usually has no cost, since we very rarely need the lookup map for
2517      // the translation unit outside C++.
2518      DeclContext *DC = Context.getTranslationUnitDecl();
2519      if (DC->hasExternalLexicalStorage() &&
2520          !getContext().getLangOpts().CPlusPlus)
2521        DC->setMustBuildLookupTable();
2522
2523      return Success;
2524    }
2525    case llvm::BitstreamEntry::SubBlock:
2526      switch (Entry.ID) {
2527      case DECLTYPES_BLOCK_ID:
2528        // We lazily load the decls block, but we want to set up the
2529        // DeclsCursor cursor to point into it.  Clone our current bitcode
2530        // cursor to it, enter the block and read the abbrevs in that block.
2531        // With the main cursor, we just skip over it.
2532        F.DeclsCursor = Stream;
2533        if (Stream.SkipBlock() ||  // Skip with the main cursor.
2534            // Read the abbrevs.
2535            ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2536          Error("malformed block record in AST file");
2537          return Failure;
2538        }
2539        break;
2540
2541      case PREPROCESSOR_BLOCK_ID:
2542        F.MacroCursor = Stream;
2543        if (!PP.getExternalSource())
2544          PP.setExternalSource(this);
2545
2546        if (Stream.SkipBlock() ||
2547            ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2548          Error("malformed block record in AST file");
2549          return Failure;
2550        }
2551        F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2552        break;
2553
2554      case PREPROCESSOR_DETAIL_BLOCK_ID:
2555        F.PreprocessorDetailCursor = Stream;
2556        if (Stream.SkipBlock() ||
2557            ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2558                             PREPROCESSOR_DETAIL_BLOCK_ID)) {
2559              Error("malformed preprocessor detail record in AST file");
2560              return Failure;
2561            }
2562        F.PreprocessorDetailStartOffset
2563        = F.PreprocessorDetailCursor.GetCurrentBitNo();
2564
2565        if (!PP.getPreprocessingRecord())
2566          PP.createPreprocessingRecord();
2567        if (!PP.getPreprocessingRecord()->getExternalSource())
2568          PP.getPreprocessingRecord()->SetExternalSource(*this);
2569        break;
2570
2571      case SOURCE_MANAGER_BLOCK_ID:
2572        if (ReadSourceManagerBlock(F))
2573          return Failure;
2574        break;
2575
2576      case SUBMODULE_BLOCK_ID:
2577        if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2578          return Result;
2579        break;
2580
2581      case COMMENTS_BLOCK_ID: {
2582        BitstreamCursor C = Stream;
2583        if (Stream.SkipBlock() ||
2584            ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2585          Error("malformed comments block in AST file");
2586          return Failure;
2587        }
2588        CommentsCursors.push_back(std::make_pair(C, &F));
2589        break;
2590      }
2591
2592      default:
2593        if (Stream.SkipBlock()) {
2594          Error("malformed block record in AST file");
2595          return Failure;
2596        }
2597        break;
2598      }
2599      continue;
2600
2601    case llvm::BitstreamEntry::Record:
2602      // The interesting case.
2603      break;
2604    }
2605
2606    // Read and process a record.
2607    Record.clear();
2608    StringRef Blob;
2609    switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2610    default:  // Default behavior: ignore.
2611      break;
2612
2613    case TYPE_OFFSET: {
2614      if (F.LocalNumTypes != 0) {
2615        Error("duplicate TYPE_OFFSET record in AST file");
2616        return Failure;
2617      }
2618      F.TypeOffsets = (const uint32_t *)Blob.data();
2619      F.LocalNumTypes = Record[0];
2620      unsigned LocalBaseTypeIndex = Record[1];
2621      F.BaseTypeIndex = getTotalNumTypes();
2622
2623      if (F.LocalNumTypes > 0) {
2624        // Introduce the global -> local mapping for types within this module.
2625        GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2626
2627        // Introduce the local -> global mapping for types within this module.
2628        F.TypeRemap.insertOrReplace(
2629          std::make_pair(LocalBaseTypeIndex,
2630                         F.BaseTypeIndex - LocalBaseTypeIndex));
2631
2632        TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2633      }
2634      break;
2635    }
2636
2637    case DECL_OFFSET: {
2638      if (F.LocalNumDecls != 0) {
2639        Error("duplicate DECL_OFFSET record in AST file");
2640        return Failure;
2641      }
2642      F.DeclOffsets = (const DeclOffset *)Blob.data();
2643      F.LocalNumDecls = Record[0];
2644      unsigned LocalBaseDeclID = Record[1];
2645      F.BaseDeclID = getTotalNumDecls();
2646
2647      if (F.LocalNumDecls > 0) {
2648        // Introduce the global -> local mapping for declarations within this
2649        // module.
2650        GlobalDeclMap.insert(
2651          std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2652
2653        // Introduce the local -> global mapping for declarations within this
2654        // module.
2655        F.DeclRemap.insertOrReplace(
2656          std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2657
2658        // Introduce the global -> local mapping for declarations within this
2659        // module.
2660        F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2661
2662        DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2663      }
2664      break;
2665    }
2666
2667    case TU_UPDATE_LEXICAL: {
2668      DeclContext *TU = Context.getTranslationUnitDecl();
2669      DeclContextInfo &Info = F.DeclContextInfos[TU];
2670      Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2671      Info.NumLexicalDecls
2672        = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2673      TU->setHasExternalLexicalStorage(true);
2674      break;
2675    }
2676
2677    case UPDATE_VISIBLE: {
2678      unsigned Idx = 0;
2679      serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2680      ASTDeclContextNameLookupTable *Table =
2681          ASTDeclContextNameLookupTable::Create(
2682              (const unsigned char *)Blob.data() + Record[Idx++],
2683              (const unsigned char *)Blob.data() + sizeof(uint32_t),
2684              (const unsigned char *)Blob.data(),
2685              ASTDeclContextNameLookupTrait(*this, F));
2686      if (Decl *D = GetExistingDecl(ID)) {
2687        auto *DC = cast<DeclContext>(D);
2688        DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
2689        auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2690        // FIXME: There should never be an existing lookup table.
2691        delete LookupTable;
2692        LookupTable = Table;
2693      } else
2694        PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2695      break;
2696    }
2697
2698    case IDENTIFIER_TABLE:
2699      F.IdentifierTableData = Blob.data();
2700      if (Record[0]) {
2701        F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2702            (const unsigned char *)F.IdentifierTableData + Record[0],
2703            (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2704            (const unsigned char *)F.IdentifierTableData,
2705            ASTIdentifierLookupTrait(*this, F));
2706
2707        PP.getIdentifierTable().setExternalIdentifierLookup(this);
2708      }
2709      break;
2710
2711    case IDENTIFIER_OFFSET: {
2712      if (F.LocalNumIdentifiers != 0) {
2713        Error("duplicate IDENTIFIER_OFFSET record in AST file");
2714        return Failure;
2715      }
2716      F.IdentifierOffsets = (const uint32_t *)Blob.data();
2717      F.LocalNumIdentifiers = Record[0];
2718      unsigned LocalBaseIdentifierID = Record[1];
2719      F.BaseIdentifierID = getTotalNumIdentifiers();
2720
2721      if (F.LocalNumIdentifiers > 0) {
2722        // Introduce the global -> local mapping for identifiers within this
2723        // module.
2724        GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2725                                                  &F));
2726
2727        // Introduce the local -> global mapping for identifiers within this
2728        // module.
2729        F.IdentifierRemap.insertOrReplace(
2730          std::make_pair(LocalBaseIdentifierID,
2731                         F.BaseIdentifierID - LocalBaseIdentifierID));
2732
2733        IdentifiersLoaded.resize(IdentifiersLoaded.size()
2734                                 + F.LocalNumIdentifiers);
2735      }
2736      break;
2737    }
2738
2739    case EAGERLY_DESERIALIZED_DECLS:
2740      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2741        EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2742      break;
2743
2744    case SPECIAL_TYPES:
2745      if (SpecialTypes.empty()) {
2746        for (unsigned I = 0, N = Record.size(); I != N; ++I)
2747          SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2748        break;
2749      }
2750
2751      if (SpecialTypes.size() != Record.size()) {
2752        Error("invalid special-types record");
2753        return Failure;
2754      }
2755
2756      for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2757        serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2758        if (!SpecialTypes[I])
2759          SpecialTypes[I] = ID;
2760        // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2761        // merge step?
2762      }
2763      break;
2764
2765    case STATISTICS:
2766      TotalNumStatements += Record[0];
2767      TotalNumMacros += Record[1];
2768      TotalLexicalDeclContexts += Record[2];
2769      TotalVisibleDeclContexts += Record[3];
2770      break;
2771
2772    case UNUSED_FILESCOPED_DECLS:
2773      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2774        UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2775      break;
2776
2777    case DELEGATING_CTORS:
2778      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2779        DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2780      break;
2781
2782    case WEAK_UNDECLARED_IDENTIFIERS:
2783      if (Record.size() % 4 != 0) {
2784        Error("invalid weak identifiers record");
2785        return Failure;
2786      }
2787
2788      // FIXME: Ignore weak undeclared identifiers from non-original PCH
2789      // files. This isn't the way to do it :)
2790      WeakUndeclaredIdentifiers.clear();
2791
2792      // Translate the weak, undeclared identifiers into global IDs.
2793      for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2794        WeakUndeclaredIdentifiers.push_back(
2795          getGlobalIdentifierID(F, Record[I++]));
2796        WeakUndeclaredIdentifiers.push_back(
2797          getGlobalIdentifierID(F, Record[I++]));
2798        WeakUndeclaredIdentifiers.push_back(
2799          ReadSourceLocation(F, Record, I).getRawEncoding());
2800        WeakUndeclaredIdentifiers.push_back(Record[I++]);
2801      }
2802      break;
2803
2804    case LOCALLY_SCOPED_EXTERN_C_DECLS:
2805      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2806        LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
2807      break;
2808
2809    case SELECTOR_OFFSETS: {
2810      F.SelectorOffsets = (const uint32_t *)Blob.data();
2811      F.LocalNumSelectors = Record[0];
2812      unsigned LocalBaseSelectorID = Record[1];
2813      F.BaseSelectorID = getTotalNumSelectors();
2814
2815      if (F.LocalNumSelectors > 0) {
2816        // Introduce the global -> local mapping for selectors within this
2817        // module.
2818        GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2819
2820        // Introduce the local -> global mapping for selectors within this
2821        // module.
2822        F.SelectorRemap.insertOrReplace(
2823          std::make_pair(LocalBaseSelectorID,
2824                         F.BaseSelectorID - LocalBaseSelectorID));
2825
2826        SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2827      }
2828      break;
2829    }
2830
2831    case METHOD_POOL:
2832      F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2833      if (Record[0])
2834        F.SelectorLookupTable
2835          = ASTSelectorLookupTable::Create(
2836                        F.SelectorLookupTableData + Record[0],
2837                        F.SelectorLookupTableData,
2838                        ASTSelectorLookupTrait(*this, F));
2839      TotalNumMethodPoolEntries += Record[1];
2840      break;
2841
2842    case REFERENCED_SELECTOR_POOL:
2843      if (!Record.empty()) {
2844        for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2845          ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2846                                                                Record[Idx++]));
2847          ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2848                                              getRawEncoding());
2849        }
2850      }
2851      break;
2852
2853    case PP_COUNTER_VALUE:
2854      if (!Record.empty() && Listener)
2855        Listener->ReadCounter(F, Record[0]);
2856      break;
2857
2858    case FILE_SORTED_DECLS:
2859      F.FileSortedDecls = (const DeclID *)Blob.data();
2860      F.NumFileSortedDecls = Record[0];
2861      break;
2862
2863    case SOURCE_LOCATION_OFFSETS: {
2864      F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2865      F.LocalNumSLocEntries = Record[0];
2866      unsigned SLocSpaceSize = Record[1];
2867      std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2868          SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2869                                              SLocSpaceSize);
2870      // Make our entry in the range map. BaseID is negative and growing, so
2871      // we invert it. Because we invert it, though, we need the other end of
2872      // the range.
2873      unsigned RangeStart =
2874          unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2875      GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2876      F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2877
2878      // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2879      assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2880      GlobalSLocOffsetMap.insert(
2881          std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2882                           - SLocSpaceSize,&F));
2883
2884      // Initialize the remapping table.
2885      // Invalid stays invalid.
2886      F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
2887      // This module. Base was 2 when being compiled.
2888      F.SLocRemap.insertOrReplace(std::make_pair(2U,
2889                                  static_cast<int>(F.SLocEntryBaseOffset - 2)));
2890
2891      TotalNumSLocEntries += F.LocalNumSLocEntries;
2892      break;
2893    }
2894
2895    case MODULE_OFFSET_MAP: {
2896      // Additional remapping information.
2897      const unsigned char *Data = (const unsigned char*)Blob.data();
2898      const unsigned char *DataEnd = Data + Blob.size();
2899
2900      // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2901      if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2902        F.SLocRemap.insert(std::make_pair(0U, 0));
2903        F.SLocRemap.insert(std::make_pair(2U, 1));
2904      }
2905
2906      // Continuous range maps we may be updating in our module.
2907      ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2908      ContinuousRangeMap<uint32_t, int, 2>::Builder
2909        IdentifierRemap(F.IdentifierRemap);
2910      ContinuousRangeMap<uint32_t, int, 2>::Builder
2911        MacroRemap(F.MacroRemap);
2912      ContinuousRangeMap<uint32_t, int, 2>::Builder
2913        PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2914      ContinuousRangeMap<uint32_t, int, 2>::Builder
2915        SubmoduleRemap(F.SubmoduleRemap);
2916      ContinuousRangeMap<uint32_t, int, 2>::Builder
2917        SelectorRemap(F.SelectorRemap);
2918      ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2919      ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2920
2921      while(Data < DataEnd) {
2922        using namespace llvm::support;
2923        uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
2924        StringRef Name = StringRef((const char*)Data, Len);
2925        Data += Len;
2926        ModuleFile *OM = ModuleMgr.lookup(Name);
2927        if (!OM) {
2928          Error("SourceLocation remap refers to unknown module");
2929          return Failure;
2930        }
2931
2932        uint32_t SLocOffset =
2933            endian::readNext<uint32_t, little, unaligned>(Data);
2934        uint32_t IdentifierIDOffset =
2935            endian::readNext<uint32_t, little, unaligned>(Data);
2936        uint32_t MacroIDOffset =
2937            endian::readNext<uint32_t, little, unaligned>(Data);
2938        uint32_t PreprocessedEntityIDOffset =
2939            endian::readNext<uint32_t, little, unaligned>(Data);
2940        uint32_t SubmoduleIDOffset =
2941            endian::readNext<uint32_t, little, unaligned>(Data);
2942        uint32_t SelectorIDOffset =
2943            endian::readNext<uint32_t, little, unaligned>(Data);
2944        uint32_t DeclIDOffset =
2945            endian::readNext<uint32_t, little, unaligned>(Data);
2946        uint32_t TypeIndexOffset =
2947            endian::readNext<uint32_t, little, unaligned>(Data);
2948
2949        // Source location offset is mapped to OM->SLocEntryBaseOffset.
2950        SLocRemap.insert(std::make_pair(SLocOffset,
2951          static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2952        IdentifierRemap.insert(
2953          std::make_pair(IdentifierIDOffset,
2954                         OM->BaseIdentifierID - IdentifierIDOffset));
2955        MacroRemap.insert(std::make_pair(MacroIDOffset,
2956                                         OM->BaseMacroID - MacroIDOffset));
2957        PreprocessedEntityRemap.insert(
2958          std::make_pair(PreprocessedEntityIDOffset,
2959            OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2960        SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2961                                      OM->BaseSubmoduleID - SubmoduleIDOffset));
2962        SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2963                               OM->BaseSelectorID - SelectorIDOffset));
2964        DeclRemap.insert(std::make_pair(DeclIDOffset,
2965                                        OM->BaseDeclID - DeclIDOffset));
2966
2967        TypeRemap.insert(std::make_pair(TypeIndexOffset,
2968                                    OM->BaseTypeIndex - TypeIndexOffset));
2969
2970        // Global -> local mappings.
2971        F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2972      }
2973      break;
2974    }
2975
2976    case SOURCE_MANAGER_LINE_TABLE:
2977      if (ParseLineTable(F, Record))
2978        return Failure;
2979      break;
2980
2981    case SOURCE_LOCATION_PRELOADS: {
2982      // Need to transform from the local view (1-based IDs) to the global view,
2983      // which is based off F.SLocEntryBaseID.
2984      if (!F.PreloadSLocEntries.empty()) {
2985        Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2986        return Failure;
2987      }
2988
2989      F.PreloadSLocEntries.swap(Record);
2990      break;
2991    }
2992
2993    case EXT_VECTOR_DECLS:
2994      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2995        ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2996      break;
2997
2998    case VTABLE_USES:
2999      if (Record.size() % 3 != 0) {
3000        Error("Invalid VTABLE_USES record");
3001        return Failure;
3002      }
3003
3004      // Later tables overwrite earlier ones.
3005      // FIXME: Modules will have some trouble with this. This is clearly not
3006      // the right way to do this.
3007      VTableUses.clear();
3008
3009      for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3010        VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3011        VTableUses.push_back(
3012          ReadSourceLocation(F, Record, Idx).getRawEncoding());
3013        VTableUses.push_back(Record[Idx++]);
3014      }
3015      break;
3016
3017    case DYNAMIC_CLASSES:
3018      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3019        DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
3020      break;
3021
3022    case PENDING_IMPLICIT_INSTANTIATIONS:
3023      if (PendingInstantiations.size() % 2 != 0) {
3024        Error("Invalid existing PendingInstantiations");
3025        return Failure;
3026      }
3027
3028      if (Record.size() % 2 != 0) {
3029        Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3030        return Failure;
3031      }
3032
3033      for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3034        PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3035        PendingInstantiations.push_back(
3036          ReadSourceLocation(F, Record, I).getRawEncoding());
3037      }
3038      break;
3039
3040    case SEMA_DECL_REFS:
3041      if (Record.size() != 2) {
3042        Error("Invalid SEMA_DECL_REFS block");
3043        return Failure;
3044      }
3045      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3046        SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3047      break;
3048
3049    case PPD_ENTITIES_OFFSETS: {
3050      F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3051      assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3052      F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3053
3054      unsigned LocalBasePreprocessedEntityID = Record[0];
3055
3056      unsigned StartingID;
3057      if (!PP.getPreprocessingRecord())
3058        PP.createPreprocessingRecord();
3059      if (!PP.getPreprocessingRecord()->getExternalSource())
3060        PP.getPreprocessingRecord()->SetExternalSource(*this);
3061      StartingID
3062        = PP.getPreprocessingRecord()
3063            ->allocateLoadedEntities(F.NumPreprocessedEntities);
3064      F.BasePreprocessedEntityID = StartingID;
3065
3066      if (F.NumPreprocessedEntities > 0) {
3067        // Introduce the global -> local mapping for preprocessed entities in
3068        // this module.
3069        GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3070
3071        // Introduce the local -> global mapping for preprocessed entities in
3072        // this module.
3073        F.PreprocessedEntityRemap.insertOrReplace(
3074          std::make_pair(LocalBasePreprocessedEntityID,
3075            F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3076      }
3077
3078      break;
3079    }
3080
3081    case DECL_UPDATE_OFFSETS: {
3082      if (Record.size() % 2 != 0) {
3083        Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3084        return Failure;
3085      }
3086      for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3087        GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3088        DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3089
3090        // If we've already loaded the decl, perform the updates when we finish
3091        // loading this block.
3092        if (Decl *D = GetExistingDecl(ID))
3093          PendingUpdateRecords.push_back(std::make_pair(ID, D));
3094      }
3095      break;
3096    }
3097
3098    case DECL_REPLACEMENTS: {
3099      if (Record.size() % 3 != 0) {
3100        Error("invalid DECL_REPLACEMENTS block in AST file");
3101        return Failure;
3102      }
3103      for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3104        ReplacedDecls[getGlobalDeclID(F, Record[I])]
3105          = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3106      break;
3107    }
3108
3109    case OBJC_CATEGORIES_MAP: {
3110      if (F.LocalNumObjCCategoriesInMap != 0) {
3111        Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3112        return Failure;
3113      }
3114
3115      F.LocalNumObjCCategoriesInMap = Record[0];
3116      F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3117      break;
3118    }
3119
3120    case OBJC_CATEGORIES:
3121      F.ObjCCategories.swap(Record);
3122      break;
3123
3124    case CXX_BASE_SPECIFIER_OFFSETS: {
3125      if (F.LocalNumCXXBaseSpecifiers != 0) {
3126        Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
3127        return Failure;
3128      }
3129
3130      F.LocalNumCXXBaseSpecifiers = Record[0];
3131      F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
3132      NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
3133      break;
3134    }
3135
3136    case DIAG_PRAGMA_MAPPINGS:
3137      if (F.PragmaDiagMappings.empty())
3138        F.PragmaDiagMappings.swap(Record);
3139      else
3140        F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3141                                    Record.begin(), Record.end());
3142      break;
3143
3144    case CUDA_SPECIAL_DECL_REFS:
3145      // Later tables overwrite earlier ones.
3146      // FIXME: Modules will have trouble with this.
3147      CUDASpecialDeclRefs.clear();
3148      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3149        CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3150      break;
3151
3152    case HEADER_SEARCH_TABLE: {
3153      F.HeaderFileInfoTableData = Blob.data();
3154      F.LocalNumHeaderFileInfos = Record[1];
3155      if (Record[0]) {
3156        F.HeaderFileInfoTable
3157          = HeaderFileInfoLookupTable::Create(
3158                   (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3159                   (const unsigned char *)F.HeaderFileInfoTableData,
3160                   HeaderFileInfoTrait(*this, F,
3161                                       &PP.getHeaderSearchInfo(),
3162                                       Blob.data() + Record[2]));
3163
3164        PP.getHeaderSearchInfo().SetExternalSource(this);
3165        if (!PP.getHeaderSearchInfo().getExternalLookup())
3166          PP.getHeaderSearchInfo().SetExternalLookup(this);
3167      }
3168      break;
3169    }
3170
3171    case FP_PRAGMA_OPTIONS:
3172      // Later tables overwrite earlier ones.
3173      FPPragmaOptions.swap(Record);
3174      break;
3175
3176    case OPENCL_EXTENSIONS:
3177      // Later tables overwrite earlier ones.
3178      OpenCLExtensions.swap(Record);
3179      break;
3180
3181    case TENTATIVE_DEFINITIONS:
3182      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3183        TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3184      break;
3185
3186    case KNOWN_NAMESPACES:
3187      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3188        KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3189      break;
3190
3191    case UNDEFINED_BUT_USED:
3192      if (UndefinedButUsed.size() % 2 != 0) {
3193        Error("Invalid existing UndefinedButUsed");
3194        return Failure;
3195      }
3196
3197      if (Record.size() % 2 != 0) {
3198        Error("invalid undefined-but-used record");
3199        return Failure;
3200      }
3201      for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3202        UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3203        UndefinedButUsed.push_back(
3204            ReadSourceLocation(F, Record, I).getRawEncoding());
3205      }
3206      break;
3207
3208    case IMPORTED_MODULES: {
3209      if (F.Kind != MK_Module) {
3210        // If we aren't loading a module (which has its own exports), make
3211        // all of the imported modules visible.
3212        // FIXME: Deal with macros-only imports.
3213        for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3214          unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3215          SourceLocation Loc = ReadSourceLocation(F, Record, I);
3216          if (GlobalID)
3217            ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3218        }
3219      }
3220      break;
3221    }
3222
3223    case LOCAL_REDECLARATIONS: {
3224      F.RedeclarationChains.swap(Record);
3225      break;
3226    }
3227
3228    case LOCAL_REDECLARATIONS_MAP: {
3229      if (F.LocalNumRedeclarationsInMap != 0) {
3230        Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3231        return Failure;
3232      }
3233
3234      F.LocalNumRedeclarationsInMap = Record[0];
3235      F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
3236      break;
3237    }
3238
3239    case MERGED_DECLARATIONS: {
3240      for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3241        GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3242        SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3243        for (unsigned N = Record[Idx++]; N > 0; --N)
3244          Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3245      }
3246      break;
3247    }
3248
3249    case MACRO_OFFSET: {
3250      if (F.LocalNumMacros != 0) {
3251        Error("duplicate MACRO_OFFSET record in AST file");
3252        return Failure;
3253      }
3254      F.MacroOffsets = (const uint32_t *)Blob.data();
3255      F.LocalNumMacros = Record[0];
3256      unsigned LocalBaseMacroID = Record[1];
3257      F.BaseMacroID = getTotalNumMacros();
3258
3259      if (F.LocalNumMacros > 0) {
3260        // Introduce the global -> local mapping for macros within this module.
3261        GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3262
3263        // Introduce the local -> global mapping for macros within this module.
3264        F.MacroRemap.insertOrReplace(
3265          std::make_pair(LocalBaseMacroID,
3266                         F.BaseMacroID - LocalBaseMacroID));
3267
3268        MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3269      }
3270      break;
3271    }
3272
3273    case MACRO_TABLE: {
3274      // FIXME: Not used yet.
3275      break;
3276    }
3277
3278    case LATE_PARSED_TEMPLATE: {
3279      LateParsedTemplates.append(Record.begin(), Record.end());
3280      break;
3281    }
3282
3283    case OPTIMIZE_PRAGMA_OPTIONS:
3284      if (Record.size() != 1) {
3285        Error("invalid pragma optimize record");
3286        return Failure;
3287      }
3288      OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3289      break;
3290    }
3291  }
3292}
3293
3294/// \brief Move the given method to the back of the global list of methods.
3295static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3296  // Find the entry for this selector in the method pool.
3297  Sema::GlobalMethodPool::iterator Known
3298    = S.MethodPool.find(Method->getSelector());
3299  if (Known == S.MethodPool.end())
3300    return;
3301
3302  // Retrieve the appropriate method list.
3303  ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3304                                                    : Known->second.second;
3305  bool Found = false;
3306  for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3307    if (!Found) {
3308      if (List->Method == Method) {
3309        Found = true;
3310      } else {
3311        // Keep searching.
3312        continue;
3313      }
3314    }
3315
3316    if (List->getNext())
3317      List->Method = List->getNext()->Method;
3318    else
3319      List->Method = Method;
3320  }
3321}
3322
3323void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3324  for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) {
3325    Decl *D = Names.HiddenDecls[I];
3326    bool wasHidden = D->Hidden;
3327    D->Hidden = false;
3328
3329    if (wasHidden && SemaObj) {
3330      if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3331        moveMethodToBackOfGlobalList(*SemaObj, Method);
3332      }
3333    }
3334  }
3335
3336  for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(),
3337                                       E = Names.HiddenMacros.end();
3338       I != E; ++I)
3339    installImportedMacro(I->first, I->second, Owner);
3340}
3341
3342void ASTReader::makeModuleVisible(Module *Mod,
3343                                  Module::NameVisibilityKind NameVisibility,
3344                                  SourceLocation ImportLoc,
3345                                  bool Complain) {
3346  llvm::SmallPtrSet<Module *, 4> Visited;
3347  SmallVector<Module *, 4> Stack;
3348  Stack.push_back(Mod);
3349  while (!Stack.empty()) {
3350    Mod = Stack.pop_back_val();
3351
3352    if (NameVisibility <= Mod->NameVisibility) {
3353      // This module already has this level of visibility (or greater), so
3354      // there is nothing more to do.
3355      continue;
3356    }
3357
3358    if (!Mod->isAvailable()) {
3359      // Modules that aren't available cannot be made visible.
3360      continue;
3361    }
3362
3363    // Update the module's name visibility.
3364    if (NameVisibility >= Module::MacrosVisible &&
3365        Mod->NameVisibility < Module::MacrosVisible)
3366      Mod->MacroVisibilityLoc = ImportLoc;
3367    Mod->NameVisibility = NameVisibility;
3368
3369    // If we've already deserialized any names from this module,
3370    // mark them as visible.
3371    HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3372    if (Hidden != HiddenNamesMap.end()) {
3373      makeNamesVisible(Hidden->second, Hidden->first);
3374      HiddenNamesMap.erase(Hidden);
3375    }
3376
3377    // Push any exported modules onto the stack to be marked as visible.
3378    SmallVector<Module *, 16> Exports;
3379    Mod->getExportedModules(Exports);
3380    for (SmallVectorImpl<Module *>::iterator
3381           I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3382      Module *Exported = *I;
3383      if (Visited.insert(Exported))
3384        Stack.push_back(Exported);
3385    }
3386
3387    // Detect any conflicts.
3388    if (Complain) {
3389      assert(ImportLoc.isValid() && "Missing import location");
3390      for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3391        if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3392          Diag(ImportLoc, diag::warn_module_conflict)
3393            << Mod->getFullModuleName()
3394            << Mod->Conflicts[I].Other->getFullModuleName()
3395            << Mod->Conflicts[I].Message;
3396          // FIXME: Need note where the other module was imported.
3397        }
3398      }
3399    }
3400  }
3401}
3402
3403bool ASTReader::loadGlobalIndex() {
3404  if (GlobalIndex)
3405    return false;
3406
3407  if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3408      !Context.getLangOpts().Modules)
3409    return true;
3410
3411  // Try to load the global index.
3412  TriedLoadingGlobalIndex = true;
3413  StringRef ModuleCachePath
3414    = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3415  std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3416    = GlobalModuleIndex::readIndex(ModuleCachePath);
3417  if (!Result.first)
3418    return true;
3419
3420  GlobalIndex.reset(Result.first);
3421  ModuleMgr.setGlobalIndex(GlobalIndex.get());
3422  return false;
3423}
3424
3425bool ASTReader::isGlobalIndexUnavailable() const {
3426  return Context.getLangOpts().Modules && UseGlobalIndex &&
3427         !hasGlobalIndex() && TriedLoadingGlobalIndex;
3428}
3429
3430static void updateModuleTimestamp(ModuleFile &MF) {
3431  // Overwrite the timestamp file contents so that file's mtime changes.
3432  std::string TimestampFilename = MF.getTimestampFilename();
3433  std::string ErrorInfo;
3434  llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo,
3435                          llvm::sys::fs::F_Text);
3436  if (!ErrorInfo.empty())
3437    return;
3438  OS << "Timestamp file\n";
3439}
3440
3441ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3442                                            ModuleKind Type,
3443                                            SourceLocation ImportLoc,
3444                                            unsigned ClientLoadCapabilities) {
3445  llvm::SaveAndRestore<SourceLocation>
3446    SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3447
3448  // Defer any pending actions until we get to the end of reading the AST file.
3449  Deserializing AnASTFile(this);
3450
3451  // Bump the generation number.
3452  unsigned PreviousGeneration = incrementGeneration(Context);
3453
3454  unsigned NumModules = ModuleMgr.size();
3455  SmallVector<ImportedModule, 4> Loaded;
3456  switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3457                                                /*ImportedBy=*/nullptr, Loaded,
3458                                                0, 0,
3459                                                ClientLoadCapabilities)) {
3460  case Failure:
3461  case Missing:
3462  case OutOfDate:
3463  case VersionMismatch:
3464  case ConfigurationMismatch:
3465  case HadErrors: {
3466    llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3467    for (const ImportedModule &IM : Loaded)
3468      LoadedSet.insert(IM.Mod);
3469
3470    ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3471                            LoadedSet,
3472                            Context.getLangOpts().Modules
3473                              ? &PP.getHeaderSearchInfo().getModuleMap()
3474                              : nullptr);
3475
3476    // If we find that any modules are unusable, the global index is going
3477    // to be out-of-date. Just remove it.
3478    GlobalIndex.reset();
3479    ModuleMgr.setGlobalIndex(nullptr);
3480    return ReadResult;
3481  }
3482  case Success:
3483    break;
3484  }
3485
3486  // Here comes stuff that we only do once the entire chain is loaded.
3487
3488  // Load the AST blocks of all of the modules that we loaded.
3489  for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3490                                              MEnd = Loaded.end();
3491       M != MEnd; ++M) {
3492    ModuleFile &F = *M->Mod;
3493
3494    // Read the AST block.
3495    if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3496      return Result;
3497
3498    // Once read, set the ModuleFile bit base offset and update the size in
3499    // bits of all files we've seen.
3500    F.GlobalBitOffset = TotalModulesSizeInBits;
3501    TotalModulesSizeInBits += F.SizeInBits;
3502    GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3503
3504    // Preload SLocEntries.
3505    for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3506      int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3507      // Load it through the SourceManager and don't call ReadSLocEntry()
3508      // directly because the entry may have already been loaded in which case
3509      // calling ReadSLocEntry() directly would trigger an assertion in
3510      // SourceManager.
3511      SourceMgr.getLoadedSLocEntryByID(Index);
3512    }
3513  }
3514
3515  // Setup the import locations and notify the module manager that we've
3516  // committed to these module files.
3517  for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3518                                              MEnd = Loaded.end();
3519       M != MEnd; ++M) {
3520    ModuleFile &F = *M->Mod;
3521
3522    ModuleMgr.moduleFileAccepted(&F);
3523
3524    // Set the import location.
3525    F.DirectImportLoc = ImportLoc;
3526    if (!M->ImportedBy)
3527      F.ImportLoc = M->ImportLoc;
3528    else
3529      F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3530                                       M->ImportLoc.getRawEncoding());
3531  }
3532
3533  // Mark all of the identifiers in the identifier table as being out of date,
3534  // so that various accessors know to check the loaded modules when the
3535  // identifier is used.
3536  for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3537                              IdEnd = PP.getIdentifierTable().end();
3538       Id != IdEnd; ++Id)
3539    Id->second->setOutOfDate(true);
3540
3541  // Resolve any unresolved module exports.
3542  for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3543    UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
3544    SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3545    Module *ResolvedMod = getSubmodule(GlobalID);
3546
3547    switch (Unresolved.Kind) {
3548    case UnresolvedModuleRef::Conflict:
3549      if (ResolvedMod) {
3550        Module::Conflict Conflict;
3551        Conflict.Other = ResolvedMod;
3552        Conflict.Message = Unresolved.String.str();
3553        Unresolved.Mod->Conflicts.push_back(Conflict);
3554      }
3555      continue;
3556
3557    case UnresolvedModuleRef::Import:
3558      if (ResolvedMod)
3559        Unresolved.Mod->Imports.push_back(ResolvedMod);
3560      continue;
3561
3562    case UnresolvedModuleRef::Export:
3563      if (ResolvedMod || Unresolved.IsWildcard)
3564        Unresolved.Mod->Exports.push_back(
3565          Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3566      continue;
3567    }
3568  }
3569  UnresolvedModuleRefs.clear();
3570
3571  // FIXME: How do we load the 'use'd modules? They may not be submodules.
3572  // Might be unnecessary as use declarations are only used to build the
3573  // module itself.
3574
3575  InitializeContext();
3576
3577  if (SemaObj)
3578    UpdateSema();
3579
3580  if (DeserializationListener)
3581    DeserializationListener->ReaderInitialized(this);
3582
3583  ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3584  if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3585    PrimaryModule.OriginalSourceFileID
3586      = FileID::get(PrimaryModule.SLocEntryBaseID
3587                    + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3588
3589    // If this AST file is a precompiled preamble, then set the
3590    // preamble file ID of the source manager to the file source file
3591    // from which the preamble was built.
3592    if (Type == MK_Preamble) {
3593      SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3594    } else if (Type == MK_MainFile) {
3595      SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3596    }
3597  }
3598
3599  // For any Objective-C class definitions we have already loaded, make sure
3600  // that we load any additional categories.
3601  for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3602    loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3603                       ObjCClassesLoaded[I],
3604                       PreviousGeneration);
3605  }
3606
3607  if (PP.getHeaderSearchInfo()
3608          .getHeaderSearchOpts()
3609          .ModulesValidateOncePerBuildSession) {
3610    // Now we are certain that the module and all modules it depends on are
3611    // up to date.  Create or update timestamp files for modules that are
3612    // located in the module cache (not for PCH files that could be anywhere
3613    // in the filesystem).
3614    for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3615      ImportedModule &M = Loaded[I];
3616      if (M.Mod->Kind == MK_Module) {
3617        updateModuleTimestamp(*M.Mod);
3618      }
3619    }
3620  }
3621
3622  return Success;
3623}
3624
3625ASTReader::ASTReadResult
3626ASTReader::ReadASTCore(StringRef FileName,
3627                       ModuleKind Type,
3628                       SourceLocation ImportLoc,
3629                       ModuleFile *ImportedBy,
3630                       SmallVectorImpl<ImportedModule> &Loaded,
3631                       off_t ExpectedSize, time_t ExpectedModTime,
3632                       unsigned ClientLoadCapabilities) {
3633  ModuleFile *M;
3634  std::string ErrorStr;
3635  ModuleManager::AddModuleResult AddResult
3636    = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3637                          getGeneration(), ExpectedSize, ExpectedModTime,
3638                          M, ErrorStr);
3639
3640  switch (AddResult) {
3641  case ModuleManager::AlreadyLoaded:
3642    return Success;
3643
3644  case ModuleManager::NewlyLoaded:
3645    // Load module file below.
3646    break;
3647
3648  case ModuleManager::Missing:
3649    // The module file was missing; if the client handle handle, that, return
3650    // it.
3651    if (ClientLoadCapabilities & ARR_Missing)
3652      return Missing;
3653
3654    // Otherwise, return an error.
3655    {
3656      std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3657                      + ErrorStr;
3658      Error(Msg);
3659    }
3660    return Failure;
3661
3662  case ModuleManager::OutOfDate:
3663    // We couldn't load the module file because it is out-of-date. If the
3664    // client can handle out-of-date, return it.
3665    if (ClientLoadCapabilities & ARR_OutOfDate)
3666      return OutOfDate;
3667
3668    // Otherwise, return an error.
3669    {
3670      std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3671                      + ErrorStr;
3672      Error(Msg);
3673    }
3674    return Failure;
3675  }
3676
3677  assert(M && "Missing module file");
3678
3679  // FIXME: This seems rather a hack. Should CurrentDir be part of the
3680  // module?
3681  if (FileName != "-") {
3682    CurrentDir = llvm::sys::path::parent_path(FileName);
3683    if (CurrentDir.empty()) CurrentDir = ".";
3684  }
3685
3686  ModuleFile &F = *M;
3687  BitstreamCursor &Stream = F.Stream;
3688  Stream.init(F.StreamFile);
3689  F.SizeInBits = F.Buffer->getBufferSize() * 8;
3690
3691  // Sniff for the signature.
3692  if (Stream.Read(8) != 'C' ||
3693      Stream.Read(8) != 'P' ||
3694      Stream.Read(8) != 'C' ||
3695      Stream.Read(8) != 'H') {
3696    Diag(diag::err_not_a_pch_file) << FileName;
3697    return Failure;
3698  }
3699
3700  // This is used for compatibility with older PCH formats.
3701  bool HaveReadControlBlock = false;
3702
3703  while (1) {
3704    llvm::BitstreamEntry Entry = Stream.advance();
3705
3706    switch (Entry.Kind) {
3707    case llvm::BitstreamEntry::Error:
3708    case llvm::BitstreamEntry::EndBlock:
3709    case llvm::BitstreamEntry::Record:
3710      Error("invalid record at top-level of AST file");
3711      return Failure;
3712
3713    case llvm::BitstreamEntry::SubBlock:
3714      break;
3715    }
3716
3717    // We only know the control subblock ID.
3718    switch (Entry.ID) {
3719    case llvm::bitc::BLOCKINFO_BLOCK_ID:
3720      if (Stream.ReadBlockInfoBlock()) {
3721        Error("malformed BlockInfoBlock in AST file");
3722        return Failure;
3723      }
3724      break;
3725    case CONTROL_BLOCK_ID:
3726      HaveReadControlBlock = true;
3727      switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
3728      case Success:
3729        break;
3730
3731      case Failure: return Failure;
3732      case Missing: return Missing;
3733      case OutOfDate: return OutOfDate;
3734      case VersionMismatch: return VersionMismatch;
3735      case ConfigurationMismatch: return ConfigurationMismatch;
3736      case HadErrors: return HadErrors;
3737      }
3738      break;
3739    case AST_BLOCK_ID:
3740      if (!HaveReadControlBlock) {
3741        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3742          Diag(diag::err_pch_version_too_old);
3743        return VersionMismatch;
3744      }
3745
3746      // Record that we've loaded this module.
3747      Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3748      return Success;
3749
3750    default:
3751      if (Stream.SkipBlock()) {
3752        Error("malformed block record in AST file");
3753        return Failure;
3754      }
3755      break;
3756    }
3757  }
3758
3759  return Success;
3760}
3761
3762void ASTReader::InitializeContext() {
3763  // If there's a listener, notify them that we "read" the translation unit.
3764  if (DeserializationListener)
3765    DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3766                                      Context.getTranslationUnitDecl());
3767
3768  // FIXME: Find a better way to deal with collisions between these
3769  // built-in types. Right now, we just ignore the problem.
3770
3771  // Load the special types.
3772  if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3773    if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3774      if (!Context.CFConstantStringTypeDecl)
3775        Context.setCFConstantStringType(GetType(String));
3776    }
3777
3778    if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3779      QualType FileType = GetType(File);
3780      if (FileType.isNull()) {
3781        Error("FILE type is NULL");
3782        return;
3783      }
3784
3785      if (!Context.FILEDecl) {
3786        if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3787          Context.setFILEDecl(Typedef->getDecl());
3788        else {
3789          const TagType *Tag = FileType->getAs<TagType>();
3790          if (!Tag) {
3791            Error("Invalid FILE type in AST file");
3792            return;
3793          }
3794          Context.setFILEDecl(Tag->getDecl());
3795        }
3796      }
3797    }
3798
3799    if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3800      QualType Jmp_bufType = GetType(Jmp_buf);
3801      if (Jmp_bufType.isNull()) {
3802        Error("jmp_buf type is NULL");
3803        return;
3804      }
3805
3806      if (!Context.jmp_bufDecl) {
3807        if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3808          Context.setjmp_bufDecl(Typedef->getDecl());
3809        else {
3810          const TagType *Tag = Jmp_bufType->getAs<TagType>();
3811          if (!Tag) {
3812            Error("Invalid jmp_buf type in AST file");
3813            return;
3814          }
3815          Context.setjmp_bufDecl(Tag->getDecl());
3816        }
3817      }
3818    }
3819
3820    if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3821      QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3822      if (Sigjmp_bufType.isNull()) {
3823        Error("sigjmp_buf type is NULL");
3824        return;
3825      }
3826
3827      if (!Context.sigjmp_bufDecl) {
3828        if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3829          Context.setsigjmp_bufDecl(Typedef->getDecl());
3830        else {
3831          const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3832          assert(Tag && "Invalid sigjmp_buf type in AST file");
3833          Context.setsigjmp_bufDecl(Tag->getDecl());
3834        }
3835      }
3836    }
3837
3838    if (unsigned ObjCIdRedef
3839          = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3840      if (Context.ObjCIdRedefinitionType.isNull())
3841        Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3842    }
3843
3844    if (unsigned ObjCClassRedef
3845          = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3846      if (Context.ObjCClassRedefinitionType.isNull())
3847        Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3848    }
3849
3850    if (unsigned ObjCSelRedef
3851          = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3852      if (Context.ObjCSelRedefinitionType.isNull())
3853        Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3854    }
3855
3856    if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3857      QualType Ucontext_tType = GetType(Ucontext_t);
3858      if (Ucontext_tType.isNull()) {
3859        Error("ucontext_t type is NULL");
3860        return;
3861      }
3862
3863      if (!Context.ucontext_tDecl) {
3864        if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3865          Context.setucontext_tDecl(Typedef->getDecl());
3866        else {
3867          const TagType *Tag = Ucontext_tType->getAs<TagType>();
3868          assert(Tag && "Invalid ucontext_t type in AST file");
3869          Context.setucontext_tDecl(Tag->getDecl());
3870        }
3871      }
3872    }
3873  }
3874
3875  ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3876
3877  // If there were any CUDA special declarations, deserialize them.
3878  if (!CUDASpecialDeclRefs.empty()) {
3879    assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3880    Context.setcudaConfigureCallDecl(
3881                           cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3882  }
3883
3884  // Re-export any modules that were imported by a non-module AST file.
3885  // FIXME: This does not make macro-only imports visible again. It also doesn't
3886  // make #includes mapped to module imports visible.
3887  for (auto &Import : ImportedModules) {
3888    if (Module *Imported = getSubmodule(Import.ID))
3889      makeModuleVisible(Imported, Module::AllVisible,
3890                        /*ImportLoc=*/Import.ImportLoc,
3891                        /*Complain=*/false);
3892  }
3893  ImportedModules.clear();
3894}
3895
3896void ASTReader::finalizeForWriting() {
3897  for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3898                                 HiddenEnd = HiddenNamesMap.end();
3899       Hidden != HiddenEnd; ++Hidden) {
3900    makeNamesVisible(Hidden->second, Hidden->first);
3901  }
3902  HiddenNamesMap.clear();
3903}
3904
3905/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3906/// cursor into the start of the given block ID, returning false on success and
3907/// true on failure.
3908static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3909  while (1) {
3910    llvm::BitstreamEntry Entry = Cursor.advance();
3911    switch (Entry.Kind) {
3912    case llvm::BitstreamEntry::Error:
3913    case llvm::BitstreamEntry::EndBlock:
3914      return true;
3915
3916    case llvm::BitstreamEntry::Record:
3917      // Ignore top-level records.
3918      Cursor.skipRecord(Entry.ID);
3919      break;
3920
3921    case llvm::BitstreamEntry::SubBlock:
3922      if (Entry.ID == BlockID) {
3923        if (Cursor.EnterSubBlock(BlockID))
3924          return true;
3925        // Found it!
3926        return false;
3927      }
3928
3929      if (Cursor.SkipBlock())
3930        return true;
3931    }
3932  }
3933}
3934
3935/// \brief Retrieve the name of the original source file name
3936/// directly from the AST file, without actually loading the AST
3937/// file.
3938std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3939                                             FileManager &FileMgr,
3940                                             DiagnosticsEngine &Diags) {
3941  // Open the AST file.
3942  std::string ErrStr;
3943  std::unique_ptr<llvm::MemoryBuffer> Buffer;
3944  Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3945  if (!Buffer) {
3946    Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3947    return std::string();
3948  }
3949
3950  // Initialize the stream
3951  llvm::BitstreamReader StreamFile;
3952  BitstreamCursor Stream;
3953  StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3954                  (const unsigned char *)Buffer->getBufferEnd());
3955  Stream.init(StreamFile);
3956
3957  // Sniff for the signature.
3958  if (Stream.Read(8) != 'C' ||
3959      Stream.Read(8) != 'P' ||
3960      Stream.Read(8) != 'C' ||
3961      Stream.Read(8) != 'H') {
3962    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3963    return std::string();
3964  }
3965
3966  // Scan for the CONTROL_BLOCK_ID block.
3967  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
3968    Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3969    return std::string();
3970  }
3971
3972  // Scan for ORIGINAL_FILE inside the control block.
3973  RecordData Record;
3974  while (1) {
3975    llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3976    if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3977      return std::string();
3978
3979    if (Entry.Kind != llvm::BitstreamEntry::Record) {
3980      Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3981      return std::string();
3982    }
3983
3984    Record.clear();
3985    StringRef Blob;
3986    if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3987      return Blob.str();
3988  }
3989}
3990
3991namespace {
3992  class SimplePCHValidator : public ASTReaderListener {
3993    const LangOptions &ExistingLangOpts;
3994    const TargetOptions &ExistingTargetOpts;
3995    const PreprocessorOptions &ExistingPPOpts;
3996    FileManager &FileMgr;
3997
3998  public:
3999    SimplePCHValidator(const LangOptions &ExistingLangOpts,
4000                       const TargetOptions &ExistingTargetOpts,
4001                       const PreprocessorOptions &ExistingPPOpts,
4002                       FileManager &FileMgr)
4003      : ExistingLangOpts(ExistingLangOpts),
4004        ExistingTargetOpts(ExistingTargetOpts),
4005        ExistingPPOpts(ExistingPPOpts),
4006        FileMgr(FileMgr)
4007    {
4008    }
4009
4010    bool ReadLanguageOptions(const LangOptions &LangOpts,
4011                             bool Complain) override {
4012      return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr);
4013    }
4014    bool ReadTargetOptions(const TargetOptions &TargetOpts,
4015                           bool Complain) override {
4016      return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr);
4017    }
4018    bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4019                                 bool Complain,
4020                                 std::string &SuggestedPredefines) override {
4021      return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4022                                      SuggestedPredefines, ExistingLangOpts);
4023    }
4024  };
4025}
4026
4027bool ASTReader::readASTFileControlBlock(StringRef Filename,
4028                                        FileManager &FileMgr,
4029                                        ASTReaderListener &Listener) {
4030  // Open the AST file.
4031  std::string ErrStr;
4032  std::unique_ptr<llvm::MemoryBuffer> Buffer;
4033  Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
4034  if (!Buffer) {
4035    return true;
4036  }
4037
4038  // Initialize the stream
4039  llvm::BitstreamReader StreamFile;
4040  BitstreamCursor Stream;
4041  StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
4042                  (const unsigned char *)Buffer->getBufferEnd());
4043  Stream.init(StreamFile);
4044
4045  // Sniff for the signature.
4046  if (Stream.Read(8) != 'C' ||
4047      Stream.Read(8) != 'P' ||
4048      Stream.Read(8) != 'C' ||
4049      Stream.Read(8) != 'H') {
4050    return true;
4051  }
4052
4053  // Scan for the CONTROL_BLOCK_ID block.
4054  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4055    return true;
4056
4057  bool NeedsInputFiles = Listener.needsInputFileVisitation();
4058  bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4059  BitstreamCursor InputFilesCursor;
4060  if (NeedsInputFiles) {
4061    InputFilesCursor = Stream;
4062    if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4063      return true;
4064
4065    // Read the abbreviations
4066    while (true) {
4067      uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4068      unsigned Code = InputFilesCursor.ReadCode();
4069
4070      // We expect all abbrevs to be at the start of the block.
4071      if (Code != llvm::bitc::DEFINE_ABBREV) {
4072        InputFilesCursor.JumpToBit(Offset);
4073        break;
4074      }
4075      InputFilesCursor.ReadAbbrevRecord();
4076    }
4077  }
4078
4079  // Scan for ORIGINAL_FILE inside the control block.
4080  RecordData Record;
4081  while (1) {
4082    llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4083    if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4084      return false;
4085
4086    if (Entry.Kind != llvm::BitstreamEntry::Record)
4087      return true;
4088
4089    Record.clear();
4090    StringRef Blob;
4091    unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4092    switch ((ControlRecordTypes)RecCode) {
4093    case METADATA: {
4094      if (Record[0] != VERSION_MAJOR)
4095        return true;
4096
4097      if (Listener.ReadFullVersionInformation(Blob))
4098        return true;
4099
4100      break;
4101    }
4102    case MODULE_NAME:
4103      Listener.ReadModuleName(Blob);
4104      break;
4105    case MODULE_MAP_FILE:
4106      Listener.ReadModuleMapFile(Blob);
4107      break;
4108    case LANGUAGE_OPTIONS:
4109      if (ParseLanguageOptions(Record, false, Listener))
4110        return true;
4111      break;
4112
4113    case TARGET_OPTIONS:
4114      if (ParseTargetOptions(Record, false, Listener))
4115        return true;
4116      break;
4117
4118    case DIAGNOSTIC_OPTIONS:
4119      if (ParseDiagnosticOptions(Record, false, Listener))
4120        return true;
4121      break;
4122
4123    case FILE_SYSTEM_OPTIONS:
4124      if (ParseFileSystemOptions(Record, false, Listener))
4125        return true;
4126      break;
4127
4128    case HEADER_SEARCH_OPTIONS:
4129      if (ParseHeaderSearchOptions(Record, false, Listener))
4130        return true;
4131      break;
4132
4133    case PREPROCESSOR_OPTIONS: {
4134      std::string IgnoredSuggestedPredefines;
4135      if (ParsePreprocessorOptions(Record, false, Listener,
4136                                   IgnoredSuggestedPredefines))
4137        return true;
4138      break;
4139    }
4140
4141    case INPUT_FILE_OFFSETS: {
4142      if (!NeedsInputFiles)
4143        break;
4144
4145      unsigned NumInputFiles = Record[0];
4146      unsigned NumUserFiles = Record[1];
4147      const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
4148      for (unsigned I = 0; I != NumInputFiles; ++I) {
4149        // Go find this input file.
4150        bool isSystemFile = I >= NumUserFiles;
4151
4152        if (isSystemFile && !NeedsSystemInputFiles)
4153          break; // the rest are system input files
4154
4155        BitstreamCursor &Cursor = InputFilesCursor;
4156        SavedStreamPosition SavedPosition(Cursor);
4157        Cursor.JumpToBit(InputFileOffs[I]);
4158
4159        unsigned Code = Cursor.ReadCode();
4160        RecordData Record;
4161        StringRef Blob;
4162        bool shouldContinue = false;
4163        switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4164        case INPUT_FILE:
4165          bool Overridden = static_cast<bool>(Record[3]);
4166          shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden);
4167          break;
4168        }
4169        if (!shouldContinue)
4170          break;
4171      }
4172      break;
4173    }
4174
4175    default:
4176      // No other validation to perform.
4177      break;
4178    }
4179  }
4180}
4181
4182
4183bool ASTReader::isAcceptableASTFile(StringRef Filename,
4184                                    FileManager &FileMgr,
4185                                    const LangOptions &LangOpts,
4186                                    const TargetOptions &TargetOpts,
4187                                    const PreprocessorOptions &PPOpts) {
4188  SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
4189  return !readASTFileControlBlock(Filename, FileMgr, validator);
4190}
4191
4192ASTReader::ASTReadResult
4193ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4194  // Enter the submodule block.
4195  if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4196    Error("malformed submodule block record in AST file");
4197    return Failure;
4198  }
4199
4200  ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4201  bool First = true;
4202  Module *CurrentModule = nullptr;
4203  RecordData Record;
4204  while (true) {
4205    llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4206
4207    switch (Entry.Kind) {
4208    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4209    case llvm::BitstreamEntry::Error:
4210      Error("malformed block record in AST file");
4211      return Failure;
4212    case llvm::BitstreamEntry::EndBlock:
4213      return Success;
4214    case llvm::BitstreamEntry::Record:
4215      // The interesting case.
4216      break;
4217    }
4218
4219    // Read a record.
4220    StringRef Blob;
4221    Record.clear();
4222    switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
4223    default:  // Default behavior: ignore.
4224      break;
4225
4226    case SUBMODULE_DEFINITION: {
4227      if (First) {
4228        Error("missing submodule metadata record at beginning of block");
4229        return Failure;
4230      }
4231
4232      if (Record.size() < 8) {
4233        Error("malformed module definition");
4234        return Failure;
4235      }
4236
4237      StringRef Name = Blob;
4238      unsigned Idx = 0;
4239      SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4240      SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4241      bool IsFramework = Record[Idx++];
4242      bool IsExplicit = Record[Idx++];
4243      bool IsSystem = Record[Idx++];
4244      bool IsExternC = Record[Idx++];
4245      bool InferSubmodules = Record[Idx++];
4246      bool InferExplicitSubmodules = Record[Idx++];
4247      bool InferExportWildcard = Record[Idx++];
4248      bool ConfigMacrosExhaustive = Record[Idx++];
4249
4250      Module *ParentModule = nullptr;
4251      const FileEntry *ModuleMap = nullptr;
4252      if (Parent) {
4253        ParentModule = getSubmodule(Parent);
4254        ModuleMap = ParentModule->ModuleMap;
4255      }
4256
4257      if (!F.ModuleMapPath.empty())
4258        ModuleMap = FileMgr.getFile(F.ModuleMapPath);
4259
4260      // Retrieve this (sub)module from the module map, creating it if
4261      // necessary.
4262      CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, ModuleMap,
4263                                                IsFramework,
4264                                                IsExplicit).first;
4265      SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4266      if (GlobalIndex >= SubmodulesLoaded.size() ||
4267          SubmodulesLoaded[GlobalIndex]) {
4268        Error("too many submodules");
4269        return Failure;
4270      }
4271
4272      if (!ParentModule) {
4273        if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4274          if (CurFile != F.File) {
4275            if (!Diags.isDiagnosticInFlight()) {
4276              Diag(diag::err_module_file_conflict)
4277                << CurrentModule->getTopLevelModuleName()
4278                << CurFile->getName()
4279                << F.File->getName();
4280            }
4281            return Failure;
4282          }
4283        }
4284
4285        CurrentModule->setASTFile(F.File);
4286      }
4287
4288      CurrentModule->IsFromModuleFile = true;
4289      CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
4290      CurrentModule->IsExternC = IsExternC;
4291      CurrentModule->InferSubmodules = InferSubmodules;
4292      CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4293      CurrentModule->InferExportWildcard = InferExportWildcard;
4294      CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
4295      if (DeserializationListener)
4296        DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4297
4298      SubmodulesLoaded[GlobalIndex] = CurrentModule;
4299
4300      // Clear out data that will be replaced by what is the module file.
4301      CurrentModule->LinkLibraries.clear();
4302      CurrentModule->ConfigMacros.clear();
4303      CurrentModule->UnresolvedConflicts.clear();
4304      CurrentModule->Conflicts.clear();
4305      break;
4306    }
4307
4308    case SUBMODULE_UMBRELLA_HEADER: {
4309      if (First) {
4310        Error("missing submodule metadata record at beginning of block");
4311        return Failure;
4312      }
4313
4314      if (!CurrentModule)
4315        break;
4316
4317      if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
4318        if (!CurrentModule->getUmbrellaHeader())
4319          ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4320        else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
4321          if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4322            Error("mismatched umbrella headers in submodule");
4323          return OutOfDate;
4324        }
4325      }
4326      break;
4327    }
4328
4329    case SUBMODULE_HEADER: {
4330      if (First) {
4331        Error("missing submodule metadata record at beginning of block");
4332        return Failure;
4333      }
4334
4335      if (!CurrentModule)
4336        break;
4337
4338      // We lazily associate headers with their modules via the HeaderInfoTable.
4339      // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4340      // of complete filenames or remove it entirely.
4341      break;
4342    }
4343
4344    case SUBMODULE_EXCLUDED_HEADER: {
4345      if (First) {
4346        Error("missing submodule metadata record at beginning of block");
4347        return Failure;
4348      }
4349
4350      if (!CurrentModule)
4351        break;
4352
4353      // We lazily associate headers with their modules via the HeaderInfoTable.
4354      // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4355      // of complete filenames or remove it entirely.
4356      break;
4357    }
4358
4359    case SUBMODULE_PRIVATE_HEADER: {
4360      if (First) {
4361        Error("missing submodule metadata record at beginning of block");
4362        return Failure;
4363      }
4364
4365      if (!CurrentModule)
4366        break;
4367
4368      // We lazily associate headers with their modules via the HeaderInfoTable.
4369      // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4370      // of complete filenames or remove it entirely.
4371      break;
4372    }
4373
4374    case SUBMODULE_TOPHEADER: {
4375      if (First) {
4376        Error("missing submodule metadata record at beginning of block");
4377        return Failure;
4378      }
4379
4380      if (!CurrentModule)
4381        break;
4382
4383      CurrentModule->addTopHeaderFilename(Blob);
4384      break;
4385    }
4386
4387    case SUBMODULE_UMBRELLA_DIR: {
4388      if (First) {
4389        Error("missing submodule metadata record at beginning of block");
4390        return Failure;
4391      }
4392
4393      if (!CurrentModule)
4394        break;
4395
4396      if (const DirectoryEntry *Umbrella
4397                                  = PP.getFileManager().getDirectory(Blob)) {
4398        if (!CurrentModule->getUmbrellaDir())
4399          ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4400        else if (CurrentModule->getUmbrellaDir() != Umbrella) {
4401          if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4402            Error("mismatched umbrella directories in submodule");
4403          return OutOfDate;
4404        }
4405      }
4406      break;
4407    }
4408
4409    case SUBMODULE_METADATA: {
4410      if (!First) {
4411        Error("submodule metadata record not at beginning of block");
4412        return Failure;
4413      }
4414      First = false;
4415
4416      F.BaseSubmoduleID = getTotalNumSubmodules();
4417      F.LocalNumSubmodules = Record[0];
4418      unsigned LocalBaseSubmoduleID = Record[1];
4419      if (F.LocalNumSubmodules > 0) {
4420        // Introduce the global -> local mapping for submodules within this
4421        // module.
4422        GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4423
4424        // Introduce the local -> global mapping for submodules within this
4425        // module.
4426        F.SubmoduleRemap.insertOrReplace(
4427          std::make_pair(LocalBaseSubmoduleID,
4428                         F.BaseSubmoduleID - LocalBaseSubmoduleID));
4429
4430        SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4431      }
4432      break;
4433    }
4434
4435    case SUBMODULE_IMPORTS: {
4436      if (First) {
4437        Error("missing submodule metadata record at beginning of block");
4438        return Failure;
4439      }
4440
4441      if (!CurrentModule)
4442        break;
4443
4444      for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
4445        UnresolvedModuleRef Unresolved;
4446        Unresolved.File = &F;
4447        Unresolved.Mod = CurrentModule;
4448        Unresolved.ID = Record[Idx];
4449        Unresolved.Kind = UnresolvedModuleRef::Import;
4450        Unresolved.IsWildcard = false;
4451        UnresolvedModuleRefs.push_back(Unresolved);
4452      }
4453      break;
4454    }
4455
4456    case SUBMODULE_EXPORTS: {
4457      if (First) {
4458        Error("missing submodule metadata record at beginning of block");
4459        return Failure;
4460      }
4461
4462      if (!CurrentModule)
4463        break;
4464
4465      for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
4466        UnresolvedModuleRef Unresolved;
4467        Unresolved.File = &F;
4468        Unresolved.Mod = CurrentModule;
4469        Unresolved.ID = Record[Idx];
4470        Unresolved.Kind = UnresolvedModuleRef::Export;
4471        Unresolved.IsWildcard = Record[Idx + 1];
4472        UnresolvedModuleRefs.push_back(Unresolved);
4473      }
4474
4475      // Once we've loaded the set of exports, there's no reason to keep
4476      // the parsed, unresolved exports around.
4477      CurrentModule->UnresolvedExports.clear();
4478      break;
4479    }
4480    case SUBMODULE_REQUIRES: {
4481      if (First) {
4482        Error("missing submodule metadata record at beginning of block");
4483        return Failure;
4484      }
4485
4486      if (!CurrentModule)
4487        break;
4488
4489      CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
4490                                    Context.getTargetInfo());
4491      break;
4492    }
4493
4494    case SUBMODULE_LINK_LIBRARY:
4495      if (First) {
4496        Error("missing submodule metadata record at beginning of block");
4497        return Failure;
4498      }
4499
4500      if (!CurrentModule)
4501        break;
4502
4503      CurrentModule->LinkLibraries.push_back(
4504                                         Module::LinkLibrary(Blob, Record[0]));
4505      break;
4506
4507    case SUBMODULE_CONFIG_MACRO:
4508      if (First) {
4509        Error("missing submodule metadata record at beginning of block");
4510        return Failure;
4511      }
4512
4513      if (!CurrentModule)
4514        break;
4515
4516      CurrentModule->ConfigMacros.push_back(Blob.str());
4517      break;
4518
4519    case SUBMODULE_CONFLICT: {
4520      if (First) {
4521        Error("missing submodule metadata record at beginning of block");
4522        return Failure;
4523      }
4524
4525      if (!CurrentModule)
4526        break;
4527
4528      UnresolvedModuleRef Unresolved;
4529      Unresolved.File = &F;
4530      Unresolved.Mod = CurrentModule;
4531      Unresolved.ID = Record[0];
4532      Unresolved.Kind = UnresolvedModuleRef::Conflict;
4533      Unresolved.IsWildcard = false;
4534      Unresolved.String = Blob;
4535      UnresolvedModuleRefs.push_back(Unresolved);
4536      break;
4537    }
4538    }
4539  }
4540}
4541
4542/// \brief Parse the record that corresponds to a LangOptions data
4543/// structure.
4544///
4545/// This routine parses the language options from the AST file and then gives
4546/// them to the AST listener if one is set.
4547///
4548/// \returns true if the listener deems the file unacceptable, false otherwise.
4549bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4550                                     bool Complain,
4551                                     ASTReaderListener &Listener) {
4552  LangOptions LangOpts;
4553  unsigned Idx = 0;
4554#define LANGOPT(Name, Bits, Default, Description) \
4555  LangOpts.Name = Record[Idx++];
4556#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4557  LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4558#include "clang/Basic/LangOptions.def"
4559#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4560#include "clang/Basic/Sanitizers.def"
4561
4562  ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4563  VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4564  LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4565
4566  unsigned Length = Record[Idx++];
4567  LangOpts.CurrentModule.assign(Record.begin() + Idx,
4568                                Record.begin() + Idx + Length);
4569
4570  Idx += Length;
4571
4572  // Comment options.
4573  for (unsigned N = Record[Idx++]; N; --N) {
4574    LangOpts.CommentOpts.BlockCommandNames.push_back(
4575      ReadString(Record, Idx));
4576  }
4577  LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
4578
4579  return Listener.ReadLanguageOptions(LangOpts, Complain);
4580}
4581
4582bool ASTReader::ParseTargetOptions(const RecordData &Record,
4583                                   bool Complain,
4584                                   ASTReaderListener &Listener) {
4585  unsigned Idx = 0;
4586  TargetOptions TargetOpts;
4587  TargetOpts.Triple = ReadString(Record, Idx);
4588  TargetOpts.CPU = ReadString(Record, Idx);
4589  TargetOpts.ABI = ReadString(Record, Idx);
4590  for (unsigned N = Record[Idx++]; N; --N) {
4591    TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4592  }
4593  for (unsigned N = Record[Idx++]; N; --N) {
4594    TargetOpts.Features.push_back(ReadString(Record, Idx));
4595  }
4596
4597  return Listener.ReadTargetOptions(TargetOpts, Complain);
4598}
4599
4600bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4601                                       ASTReaderListener &Listener) {
4602  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
4603  unsigned Idx = 0;
4604#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
4605#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4606  DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
4607#include "clang/Basic/DiagnosticOptions.def"
4608
4609  for (unsigned N = Record[Idx++]; N; --N) {
4610    DiagOpts->Warnings.push_back(ReadString(Record, Idx));
4611  }
4612
4613  return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4614}
4615
4616bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4617                                       ASTReaderListener &Listener) {
4618  FileSystemOptions FSOpts;
4619  unsigned Idx = 0;
4620  FSOpts.WorkingDir = ReadString(Record, Idx);
4621  return Listener.ReadFileSystemOptions(FSOpts, Complain);
4622}
4623
4624bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4625                                         bool Complain,
4626                                         ASTReaderListener &Listener) {
4627  HeaderSearchOptions HSOpts;
4628  unsigned Idx = 0;
4629  HSOpts.Sysroot = ReadString(Record, Idx);
4630
4631  // Include entries.
4632  for (unsigned N = Record[Idx++]; N; --N) {
4633    std::string Path = ReadString(Record, Idx);
4634    frontend::IncludeDirGroup Group
4635      = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4636    bool IsFramework = Record[Idx++];
4637    bool IgnoreSysRoot = Record[Idx++];
4638    HSOpts.UserEntries.push_back(
4639      HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
4640  }
4641
4642  // System header prefixes.
4643  for (unsigned N = Record[Idx++]; N; --N) {
4644    std::string Prefix = ReadString(Record, Idx);
4645    bool IsSystemHeader = Record[Idx++];
4646    HSOpts.SystemHeaderPrefixes.push_back(
4647      HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4648  }
4649
4650  HSOpts.ResourceDir = ReadString(Record, Idx);
4651  HSOpts.ModuleCachePath = ReadString(Record, Idx);
4652  HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
4653  HSOpts.DisableModuleHash = Record[Idx++];
4654  HSOpts.UseBuiltinIncludes = Record[Idx++];
4655  HSOpts.UseStandardSystemIncludes = Record[Idx++];
4656  HSOpts.UseStandardCXXIncludes = Record[Idx++];
4657  HSOpts.UseLibcxx = Record[Idx++];
4658
4659  return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4660}
4661
4662bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4663                                         bool Complain,
4664                                         ASTReaderListener &Listener,
4665                                         std::string &SuggestedPredefines) {
4666  PreprocessorOptions PPOpts;
4667  unsigned Idx = 0;
4668
4669  // Macro definitions/undefs
4670  for (unsigned N = Record[Idx++]; N; --N) {
4671    std::string Macro = ReadString(Record, Idx);
4672    bool IsUndef = Record[Idx++];
4673    PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4674  }
4675
4676  // Includes
4677  for (unsigned N = Record[Idx++]; N; --N) {
4678    PPOpts.Includes.push_back(ReadString(Record, Idx));
4679  }
4680
4681  // Macro Includes
4682  for (unsigned N = Record[Idx++]; N; --N) {
4683    PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4684  }
4685
4686  PPOpts.UsePredefines = Record[Idx++];
4687  PPOpts.DetailedRecord = Record[Idx++];
4688  PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4689  PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4690  PPOpts.ObjCXXARCStandardLibrary =
4691    static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4692  SuggestedPredefines.clear();
4693  return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4694                                          SuggestedPredefines);
4695}
4696
4697std::pair<ModuleFile *, unsigned>
4698ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4699  GlobalPreprocessedEntityMapType::iterator
4700  I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4701  assert(I != GlobalPreprocessedEntityMap.end() &&
4702         "Corrupted global preprocessed entity map");
4703  ModuleFile *M = I->second;
4704  unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4705  return std::make_pair(M, LocalIndex);
4706}
4707
4708std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4709ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4710  if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4711    return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4712                                             Mod.NumPreprocessedEntities);
4713
4714  return std::make_pair(PreprocessingRecord::iterator(),
4715                        PreprocessingRecord::iterator());
4716}
4717
4718std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4719ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4720  return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4721                        ModuleDeclIterator(this, &Mod,
4722                                 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4723}
4724
4725PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4726  PreprocessedEntityID PPID = Index+1;
4727  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4728  ModuleFile &M = *PPInfo.first;
4729  unsigned LocalIndex = PPInfo.second;
4730  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4731
4732  if (!PP.getPreprocessingRecord()) {
4733    Error("no preprocessing record");
4734    return nullptr;
4735  }
4736
4737  SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4738  M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4739
4740  llvm::BitstreamEntry Entry =
4741    M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4742  if (Entry.Kind != llvm::BitstreamEntry::Record)
4743    return nullptr;
4744
4745  // Read the record.
4746  SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4747                    ReadSourceLocation(M, PPOffs.End));
4748  PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4749  StringRef Blob;
4750  RecordData Record;
4751  PreprocessorDetailRecordTypes RecType =
4752    (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4753                                          Entry.ID, Record, &Blob);
4754  switch (RecType) {
4755  case PPD_MACRO_EXPANSION: {
4756    bool isBuiltin = Record[0];
4757    IdentifierInfo *Name = nullptr;
4758    MacroDefinition *Def = nullptr;
4759    if (isBuiltin)
4760      Name = getLocalIdentifier(M, Record[1]);
4761    else {
4762      PreprocessedEntityID
4763          GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4764      Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4765    }
4766
4767    MacroExpansion *ME;
4768    if (isBuiltin)
4769      ME = new (PPRec) MacroExpansion(Name, Range);
4770    else
4771      ME = new (PPRec) MacroExpansion(Def, Range);
4772
4773    return ME;
4774  }
4775
4776  case PPD_MACRO_DEFINITION: {
4777    // Decode the identifier info and then check again; if the macro is
4778    // still defined and associated with the identifier,
4779    IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4780    MacroDefinition *MD
4781      = new (PPRec) MacroDefinition(II, Range);
4782
4783    if (DeserializationListener)
4784      DeserializationListener->MacroDefinitionRead(PPID, MD);
4785
4786    return MD;
4787  }
4788
4789  case PPD_INCLUSION_DIRECTIVE: {
4790    const char *FullFileNameStart = Blob.data() + Record[0];
4791    StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4792    const FileEntry *File = nullptr;
4793    if (!FullFileName.empty())
4794      File = PP.getFileManager().getFile(FullFileName);
4795
4796    // FIXME: Stable encoding
4797    InclusionDirective::InclusionKind Kind
4798      = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4799    InclusionDirective *ID
4800      = new (PPRec) InclusionDirective(PPRec, Kind,
4801                                       StringRef(Blob.data(), Record[0]),
4802                                       Record[1], Record[3],
4803                                       File,
4804                                       Range);
4805    return ID;
4806  }
4807  }
4808
4809  llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4810}
4811
4812/// \brief \arg SLocMapI points at a chunk of a module that contains no
4813/// preprocessed entities or the entities it contains are not the ones we are
4814/// looking for. Find the next module that contains entities and return the ID
4815/// of the first entry.
4816PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4817                       GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4818  ++SLocMapI;
4819  for (GlobalSLocOffsetMapType::const_iterator
4820         EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4821    ModuleFile &M = *SLocMapI->second;
4822    if (M.NumPreprocessedEntities)
4823      return M.BasePreprocessedEntityID;
4824  }
4825
4826  return getTotalNumPreprocessedEntities();
4827}
4828
4829namespace {
4830
4831template <unsigned PPEntityOffset::*PPLoc>
4832struct PPEntityComp {
4833  const ASTReader &Reader;
4834  ModuleFile &M;
4835
4836  PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4837
4838  bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4839    SourceLocation LHS = getLoc(L);
4840    SourceLocation RHS = getLoc(R);
4841    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4842  }
4843
4844  bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4845    SourceLocation LHS = getLoc(L);
4846    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4847  }
4848
4849  bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4850    SourceLocation RHS = getLoc(R);
4851    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4852  }
4853
4854  SourceLocation getLoc(const PPEntityOffset &PPE) const {
4855    return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4856  }
4857};
4858
4859}
4860
4861PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4862                                                       bool EndsAfter) const {
4863  if (SourceMgr.isLocalSourceLocation(Loc))
4864    return getTotalNumPreprocessedEntities();
4865
4866  GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4867      SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
4868  assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4869         "Corrupted global sloc offset map");
4870
4871  if (SLocMapI->second->NumPreprocessedEntities == 0)
4872    return findNextPreprocessedEntity(SLocMapI);
4873
4874  ModuleFile &M = *SLocMapI->second;
4875  typedef const PPEntityOffset *pp_iterator;
4876  pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4877  pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4878
4879  size_t Count = M.NumPreprocessedEntities;
4880  size_t Half;
4881  pp_iterator First = pp_begin;
4882  pp_iterator PPI;
4883
4884  if (EndsAfter) {
4885    PPI = std::upper_bound(pp_begin, pp_end, Loc,
4886                           PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4887  } else {
4888    // Do a binary search manually instead of using std::lower_bound because
4889    // The end locations of entities may be unordered (when a macro expansion
4890    // is inside another macro argument), but for this case it is not important
4891    // whether we get the first macro expansion or its containing macro.
4892    while (Count > 0) {
4893      Half = Count / 2;
4894      PPI = First;
4895      std::advance(PPI, Half);
4896      if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4897                                              Loc)) {
4898        First = PPI;
4899        ++First;
4900        Count = Count - Half - 1;
4901      } else
4902        Count = Half;
4903    }
4904  }
4905
4906  if (PPI == pp_end)
4907    return findNextPreprocessedEntity(SLocMapI);
4908
4909  return M.BasePreprocessedEntityID + (PPI - pp_begin);
4910}
4911
4912/// \brief Returns a pair of [Begin, End) indices of preallocated
4913/// preprocessed entities that \arg Range encompasses.
4914std::pair<unsigned, unsigned>
4915    ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4916  if (Range.isInvalid())
4917    return std::make_pair(0,0);
4918  assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4919
4920  PreprocessedEntityID BeginID =
4921      findPreprocessedEntity(Range.getBegin(), false);
4922  PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
4923  return std::make_pair(BeginID, EndID);
4924}
4925
4926/// \brief Optionally returns true or false if the preallocated preprocessed
4927/// entity with index \arg Index came from file \arg FID.
4928Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4929                                                             FileID FID) {
4930  if (FID.isInvalid())
4931    return false;
4932
4933  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4934  ModuleFile &M = *PPInfo.first;
4935  unsigned LocalIndex = PPInfo.second;
4936  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4937
4938  SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4939  if (Loc.isInvalid())
4940    return false;
4941
4942  if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4943    return true;
4944  else
4945    return false;
4946}
4947
4948namespace {
4949  /// \brief Visitor used to search for information about a header file.
4950  class HeaderFileInfoVisitor {
4951    const FileEntry *FE;
4952
4953    Optional<HeaderFileInfo> HFI;
4954
4955  public:
4956    explicit HeaderFileInfoVisitor(const FileEntry *FE)
4957      : FE(FE) { }
4958
4959    static bool visit(ModuleFile &M, void *UserData) {
4960      HeaderFileInfoVisitor *This
4961        = static_cast<HeaderFileInfoVisitor *>(UserData);
4962
4963      HeaderFileInfoLookupTable *Table
4964        = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4965      if (!Table)
4966        return false;
4967
4968      // Look in the on-disk hash table for an entry for this file name.
4969      HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
4970      if (Pos == Table->end())
4971        return false;
4972
4973      This->HFI = *Pos;
4974      return true;
4975    }
4976
4977    Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4978  };
4979}
4980
4981HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4982  HeaderFileInfoVisitor Visitor(FE);
4983  ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4984  if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
4985    return *HFI;
4986
4987  return HeaderFileInfo();
4988}
4989
4990void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4991  // FIXME: Make it work properly with modules.
4992  SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
4993  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4994    ModuleFile &F = *(*I);
4995    unsigned Idx = 0;
4996    DiagStates.clear();
4997    assert(!Diag.DiagStates.empty());
4998    DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4999    while (Idx < F.PragmaDiagMappings.size()) {
5000      SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5001      unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5002      if (DiagStateID != 0) {
5003        Diag.DiagStatePoints.push_back(
5004                    DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5005                    FullSourceLoc(Loc, SourceMgr)));
5006        continue;
5007      }
5008
5009      assert(DiagStateID == 0);
5010      // A new DiagState was created here.
5011      Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5012      DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5013      DiagStates.push_back(NewState);
5014      Diag.DiagStatePoints.push_back(
5015          DiagnosticsEngine::DiagStatePoint(NewState,
5016                                            FullSourceLoc(Loc, SourceMgr)));
5017      while (1) {
5018        assert(Idx < F.PragmaDiagMappings.size() &&
5019               "Invalid data, didn't find '-1' marking end of diag/map pairs");
5020        if (Idx >= F.PragmaDiagMappings.size()) {
5021          break; // Something is messed up but at least avoid infinite loop in
5022                 // release build.
5023        }
5024        unsigned DiagID = F.PragmaDiagMappings[Idx++];
5025        if (DiagID == (unsigned)-1) {
5026          break; // no more diag/map pairs for this location.
5027        }
5028        diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5029        DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5030        Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
5031      }
5032    }
5033  }
5034}
5035
5036/// \brief Get the correct cursor and offset for loading a type.
5037ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5038  GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5039  assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5040  ModuleFile *M = I->second;
5041  return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5042}
5043
5044/// \brief Read and return the type with the given index..
5045///
5046/// The index is the type ID, shifted and minus the number of predefs. This
5047/// routine actually reads the record corresponding to the type at the given
5048/// location. It is a helper routine for GetType, which deals with reading type
5049/// IDs.
5050QualType ASTReader::readTypeRecord(unsigned Index) {
5051  RecordLocation Loc = TypeCursorForIndex(Index);
5052  BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5053
5054  // Keep track of where we are in the stream, then jump back there
5055  // after reading this type.
5056  SavedStreamPosition SavedPosition(DeclsCursor);
5057
5058  ReadingKindTracker ReadingKind(Read_Type, *this);
5059
5060  // Note that we are loading a type record.
5061  Deserializing AType(this);
5062
5063  unsigned Idx = 0;
5064  DeclsCursor.JumpToBit(Loc.Offset);
5065  RecordData Record;
5066  unsigned Code = DeclsCursor.ReadCode();
5067  switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5068  case TYPE_EXT_QUAL: {
5069    if (Record.size() != 2) {
5070      Error("Incorrect encoding of extended qualifier type");
5071      return QualType();
5072    }
5073    QualType Base = readType(*Loc.F, Record, Idx);
5074    Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5075    return Context.getQualifiedType(Base, Quals);
5076  }
5077
5078  case TYPE_COMPLEX: {
5079    if (Record.size() != 1) {
5080      Error("Incorrect encoding of complex type");
5081      return QualType();
5082    }
5083    QualType ElemType = readType(*Loc.F, Record, Idx);
5084    return Context.getComplexType(ElemType);
5085  }
5086
5087  case TYPE_POINTER: {
5088    if (Record.size() != 1) {
5089      Error("Incorrect encoding of pointer type");
5090      return QualType();
5091    }
5092    QualType PointeeType = readType(*Loc.F, Record, Idx);
5093    return Context.getPointerType(PointeeType);
5094  }
5095
5096  case TYPE_DECAYED: {
5097    if (Record.size() != 1) {
5098      Error("Incorrect encoding of decayed type");
5099      return QualType();
5100    }
5101    QualType OriginalType = readType(*Loc.F, Record, Idx);
5102    QualType DT = Context.getAdjustedParameterType(OriginalType);
5103    if (!isa<DecayedType>(DT))
5104      Error("Decayed type does not decay");
5105    return DT;
5106  }
5107
5108  case TYPE_ADJUSTED: {
5109    if (Record.size() != 2) {
5110      Error("Incorrect encoding of adjusted type");
5111      return QualType();
5112    }
5113    QualType OriginalTy = readType(*Loc.F, Record, Idx);
5114    QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5115    return Context.getAdjustedType(OriginalTy, AdjustedTy);
5116  }
5117
5118  case TYPE_BLOCK_POINTER: {
5119    if (Record.size() != 1) {
5120      Error("Incorrect encoding of block pointer type");
5121      return QualType();
5122    }
5123    QualType PointeeType = readType(*Loc.F, Record, Idx);
5124    return Context.getBlockPointerType(PointeeType);
5125  }
5126
5127  case TYPE_LVALUE_REFERENCE: {
5128    if (Record.size() != 2) {
5129      Error("Incorrect encoding of lvalue reference type");
5130      return QualType();
5131    }
5132    QualType PointeeType = readType(*Loc.F, Record, Idx);
5133    return Context.getLValueReferenceType(PointeeType, Record[1]);
5134  }
5135
5136  case TYPE_RVALUE_REFERENCE: {
5137    if (Record.size() != 1) {
5138      Error("Incorrect encoding of rvalue reference type");
5139      return QualType();
5140    }
5141    QualType PointeeType = readType(*Loc.F, Record, Idx);
5142    return Context.getRValueReferenceType(PointeeType);
5143  }
5144
5145  case TYPE_MEMBER_POINTER: {
5146    if (Record.size() != 2) {
5147      Error("Incorrect encoding of member pointer type");
5148      return QualType();
5149    }
5150    QualType PointeeType = readType(*Loc.F, Record, Idx);
5151    QualType ClassType = readType(*Loc.F, Record, Idx);
5152    if (PointeeType.isNull() || ClassType.isNull())
5153      return QualType();
5154
5155    return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5156  }
5157
5158  case TYPE_CONSTANT_ARRAY: {
5159    QualType ElementType = readType(*Loc.F, Record, Idx);
5160    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5161    unsigned IndexTypeQuals = Record[2];
5162    unsigned Idx = 3;
5163    llvm::APInt Size = ReadAPInt(Record, Idx);
5164    return Context.getConstantArrayType(ElementType, Size,
5165                                         ASM, IndexTypeQuals);
5166  }
5167
5168  case TYPE_INCOMPLETE_ARRAY: {
5169    QualType ElementType = readType(*Loc.F, Record, Idx);
5170    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5171    unsigned IndexTypeQuals = Record[2];
5172    return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5173  }
5174
5175  case TYPE_VARIABLE_ARRAY: {
5176    QualType ElementType = readType(*Loc.F, Record, Idx);
5177    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5178    unsigned IndexTypeQuals = Record[2];
5179    SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5180    SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5181    return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5182                                         ASM, IndexTypeQuals,
5183                                         SourceRange(LBLoc, RBLoc));
5184  }
5185
5186  case TYPE_VECTOR: {
5187    if (Record.size() != 3) {
5188      Error("incorrect encoding of vector type in AST file");
5189      return QualType();
5190    }
5191
5192    QualType ElementType = readType(*Loc.F, Record, Idx);
5193    unsigned NumElements = Record[1];
5194    unsigned VecKind = Record[2];
5195    return Context.getVectorType(ElementType, NumElements,
5196                                  (VectorType::VectorKind)VecKind);
5197  }
5198
5199  case TYPE_EXT_VECTOR: {
5200    if (Record.size() != 3) {
5201      Error("incorrect encoding of extended vector type in AST file");
5202      return QualType();
5203    }
5204
5205    QualType ElementType = readType(*Loc.F, Record, Idx);
5206    unsigned NumElements = Record[1];
5207    return Context.getExtVectorType(ElementType, NumElements);
5208  }
5209
5210  case TYPE_FUNCTION_NO_PROTO: {
5211    if (Record.size() != 6) {
5212      Error("incorrect encoding of no-proto function type");
5213      return QualType();
5214    }
5215    QualType ResultType = readType(*Loc.F, Record, Idx);
5216    FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5217                               (CallingConv)Record[4], Record[5]);
5218    return Context.getFunctionNoProtoType(ResultType, Info);
5219  }
5220
5221  case TYPE_FUNCTION_PROTO: {
5222    QualType ResultType = readType(*Loc.F, Record, Idx);
5223
5224    FunctionProtoType::ExtProtoInfo EPI;
5225    EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5226                                        /*hasregparm*/ Record[2],
5227                                        /*regparm*/ Record[3],
5228                                        static_cast<CallingConv>(Record[4]),
5229                                        /*produces*/ Record[5]);
5230
5231    unsigned Idx = 6;
5232    unsigned NumParams = Record[Idx++];
5233    SmallVector<QualType, 16> ParamTypes;
5234    for (unsigned I = 0; I != NumParams; ++I)
5235      ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5236
5237    EPI.Variadic = Record[Idx++];
5238    EPI.HasTrailingReturn = Record[Idx++];
5239    EPI.TypeQuals = Record[Idx++];
5240    EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5241    SmallVector<QualType, 8> ExceptionStorage;
5242    readExceptionSpec(*Loc.F, ExceptionStorage, EPI, Record, Idx);
5243    return Context.getFunctionType(ResultType, ParamTypes, EPI);
5244  }
5245
5246  case TYPE_UNRESOLVED_USING: {
5247    unsigned Idx = 0;
5248    return Context.getTypeDeclType(
5249                  ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5250  }
5251
5252  case TYPE_TYPEDEF: {
5253    if (Record.size() != 2) {
5254      Error("incorrect encoding of typedef type");
5255      return QualType();
5256    }
5257    unsigned Idx = 0;
5258    TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5259    QualType Canonical = readType(*Loc.F, Record, Idx);
5260    if (!Canonical.isNull())
5261      Canonical = Context.getCanonicalType(Canonical);
5262    return Context.getTypedefType(Decl, Canonical);
5263  }
5264
5265  case TYPE_TYPEOF_EXPR:
5266    return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5267
5268  case TYPE_TYPEOF: {
5269    if (Record.size() != 1) {
5270      Error("incorrect encoding of typeof(type) in AST file");
5271      return QualType();
5272    }
5273    QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5274    return Context.getTypeOfType(UnderlyingType);
5275  }
5276
5277  case TYPE_DECLTYPE: {
5278    QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5279    return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5280  }
5281
5282  case TYPE_UNARY_TRANSFORM: {
5283    QualType BaseType = readType(*Loc.F, Record, Idx);
5284    QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5285    UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5286    return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5287  }
5288
5289  case TYPE_AUTO: {
5290    QualType Deduced = readType(*Loc.F, Record, Idx);
5291    bool IsDecltypeAuto = Record[Idx++];
5292    bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
5293    return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
5294  }
5295
5296  case TYPE_RECORD: {
5297    if (Record.size() != 2) {
5298      Error("incorrect encoding of record type");
5299      return QualType();
5300    }
5301    unsigned Idx = 0;
5302    bool IsDependent = Record[Idx++];
5303    RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5304    RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5305    QualType T = Context.getRecordType(RD);
5306    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5307    return T;
5308  }
5309
5310  case TYPE_ENUM: {
5311    if (Record.size() != 2) {
5312      Error("incorrect encoding of enum type");
5313      return QualType();
5314    }
5315    unsigned Idx = 0;
5316    bool IsDependent = Record[Idx++];
5317    QualType T
5318      = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5319    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5320    return T;
5321  }
5322
5323  case TYPE_ATTRIBUTED: {
5324    if (Record.size() != 3) {
5325      Error("incorrect encoding of attributed type");
5326      return QualType();
5327    }
5328    QualType modifiedType = readType(*Loc.F, Record, Idx);
5329    QualType equivalentType = readType(*Loc.F, Record, Idx);
5330    AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5331    return Context.getAttributedType(kind, modifiedType, equivalentType);
5332  }
5333
5334  case TYPE_PAREN: {
5335    if (Record.size() != 1) {
5336      Error("incorrect encoding of paren type");
5337      return QualType();
5338    }
5339    QualType InnerType = readType(*Loc.F, Record, Idx);
5340    return Context.getParenType(InnerType);
5341  }
5342
5343  case TYPE_PACK_EXPANSION: {
5344    if (Record.size() != 2) {
5345      Error("incorrect encoding of pack expansion type");
5346      return QualType();
5347    }
5348    QualType Pattern = readType(*Loc.F, Record, Idx);
5349    if (Pattern.isNull())
5350      return QualType();
5351    Optional<unsigned> NumExpansions;
5352    if (Record[1])
5353      NumExpansions = Record[1] - 1;
5354    return Context.getPackExpansionType(Pattern, NumExpansions);
5355  }
5356
5357  case TYPE_ELABORATED: {
5358    unsigned Idx = 0;
5359    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5360    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5361    QualType NamedType = readType(*Loc.F, Record, Idx);
5362    return Context.getElaboratedType(Keyword, NNS, NamedType);
5363  }
5364
5365  case TYPE_OBJC_INTERFACE: {
5366    unsigned Idx = 0;
5367    ObjCInterfaceDecl *ItfD
5368      = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5369    return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5370  }
5371
5372  case TYPE_OBJC_OBJECT: {
5373    unsigned Idx = 0;
5374    QualType Base = readType(*Loc.F, Record, Idx);
5375    unsigned NumProtos = Record[Idx++];
5376    SmallVector<ObjCProtocolDecl*, 4> Protos;
5377    for (unsigned I = 0; I != NumProtos; ++I)
5378      Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5379    return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5380  }
5381
5382  case TYPE_OBJC_OBJECT_POINTER: {
5383    unsigned Idx = 0;
5384    QualType Pointee = readType(*Loc.F, Record, Idx);
5385    return Context.getObjCObjectPointerType(Pointee);
5386  }
5387
5388  case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5389    unsigned Idx = 0;
5390    QualType Parm = readType(*Loc.F, Record, Idx);
5391    QualType Replacement = readType(*Loc.F, Record, Idx);
5392    return Context.getSubstTemplateTypeParmType(
5393        cast<TemplateTypeParmType>(Parm),
5394        Context.getCanonicalType(Replacement));
5395  }
5396
5397  case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5398    unsigned Idx = 0;
5399    QualType Parm =