Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Serialization/ASTReader.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ASTReader.cpp - AST File Reader ------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the ASTReader class, which reads AST files.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "ASTCommon.h"
14
#include "ASTReaderInternals.h"
15
#include "clang/AST/ASTConsumer.h"
16
#include "clang/AST/ASTContext.h"
17
#include "clang/AST/ASTMutationListener.h"
18
#include "clang/AST/ASTUnresolvedSet.h"
19
#include "clang/AST/AbstractTypeReader.h"
20
#include "clang/AST/Decl.h"
21
#include "clang/AST/DeclBase.h"
22
#include "clang/AST/DeclCXX.h"
23
#include "clang/AST/DeclFriend.h"
24
#include "clang/AST/DeclGroup.h"
25
#include "clang/AST/DeclObjC.h"
26
#include "clang/AST/DeclTemplate.h"
27
#include "clang/AST/DeclarationName.h"
28
#include "clang/AST/Expr.h"
29
#include "clang/AST/ExprCXX.h"
30
#include "clang/AST/ExternalASTSource.h"
31
#include "clang/AST/NestedNameSpecifier.h"
32
#include "clang/AST/ODRHash.h"
33
#include "clang/AST/OpenMPClause.h"
34
#include "clang/AST/RawCommentList.h"
35
#include "clang/AST/TemplateBase.h"
36
#include "clang/AST/TemplateName.h"
37
#include "clang/AST/Type.h"
38
#include "clang/AST/TypeLoc.h"
39
#include "clang/AST/TypeLocVisitor.h"
40
#include "clang/AST/UnresolvedSet.h"
41
#include "clang/Basic/CommentOptions.h"
42
#include "clang/Basic/Diagnostic.h"
43
#include "clang/Basic/DiagnosticError.h"
44
#include "clang/Basic/DiagnosticOptions.h"
45
#include "clang/Basic/ExceptionSpecificationType.h"
46
#include "clang/Basic/FileManager.h"
47
#include "clang/Basic/FileSystemOptions.h"
48
#include "clang/Basic/IdentifierTable.h"
49
#include "clang/Basic/LLVM.h"
50
#include "clang/Basic/LangOptions.h"
51
#include "clang/Basic/Module.h"
52
#include "clang/Basic/ObjCRuntime.h"
53
#include "clang/Basic/OpenMPKinds.h"
54
#include "clang/Basic/OperatorKinds.h"
55
#include "clang/Basic/PragmaKinds.h"
56
#include "clang/Basic/Sanitizers.h"
57
#include "clang/Basic/SourceLocation.h"
58
#include "clang/Basic/SourceManager.h"
59
#include "clang/Basic/SourceManagerInternals.h"
60
#include "clang/Basic/Specifiers.h"
61
#include "clang/Basic/TargetInfo.h"
62
#include "clang/Basic/TargetOptions.h"
63
#include "clang/Basic/TokenKinds.h"
64
#include "clang/Basic/Version.h"
65
#include "clang/Lex/HeaderSearch.h"
66
#include "clang/Lex/HeaderSearchOptions.h"
67
#include "clang/Lex/MacroInfo.h"
68
#include "clang/Lex/ModuleMap.h"
69
#include "clang/Lex/PreprocessingRecord.h"
70
#include "clang/Lex/Preprocessor.h"
71
#include "clang/Lex/PreprocessorOptions.h"
72
#include "clang/Lex/Token.h"
73
#include "clang/Sema/ObjCMethodList.h"
74
#include "clang/Sema/Scope.h"
75
#include "clang/Sema/Sema.h"
76
#include "clang/Sema/Weak.h"
77
#include "clang/Serialization/ASTBitCodes.h"
78
#include "clang/Serialization/ASTDeserializationListener.h"
79
#include "clang/Serialization/ASTRecordReader.h"
80
#include "clang/Serialization/ContinuousRangeMap.h"
81
#include "clang/Serialization/GlobalModuleIndex.h"
82
#include "clang/Serialization/InMemoryModuleCache.h"
83
#include "clang/Serialization/ModuleFile.h"
84
#include "clang/Serialization/ModuleFileExtension.h"
85
#include "clang/Serialization/ModuleManager.h"
86
#include "clang/Serialization/PCHContainerOperations.h"
87
#include "clang/Serialization/SerializationDiagnostic.h"
88
#include "llvm/ADT/APFloat.h"
89
#include "llvm/ADT/APInt.h"
90
#include "llvm/ADT/APSInt.h"
91
#include "llvm/ADT/ArrayRef.h"
92
#include "llvm/ADT/DenseMap.h"
93
#include "llvm/ADT/FloatingPointMode.h"
94
#include "llvm/ADT/FoldingSet.h"
95
#include "llvm/ADT/Hashing.h"
96
#include "llvm/ADT/IntrusiveRefCntPtr.h"
97
#include "llvm/ADT/None.h"
98
#include "llvm/ADT/Optional.h"
99
#include "llvm/ADT/STLExtras.h"
100
#include "llvm/ADT/ScopeExit.h"
101
#include "llvm/ADT/SmallPtrSet.h"
102
#include "llvm/ADT/SmallString.h"
103
#include "llvm/ADT/SmallVector.h"
104
#include "llvm/ADT/StringExtras.h"
105
#include "llvm/ADT/StringMap.h"
106
#include "llvm/ADT/StringRef.h"
107
#include "llvm/ADT/Triple.h"
108
#include "llvm/ADT/iterator_range.h"
109
#include "llvm/Bitstream/BitstreamReader.h"
110
#include "llvm/Support/Casting.h"
111
#include "llvm/Support/Compiler.h"
112
#include "llvm/Support/Compression.h"
113
#include "llvm/Support/DJB.h"
114
#include "llvm/Support/Endian.h"
115
#include "llvm/Support/Error.h"
116
#include "llvm/Support/ErrorHandling.h"
117
#include "llvm/Support/FileSystem.h"
118
#include "llvm/Support/LEB128.h"
119
#include "llvm/Support/MemoryBuffer.h"
120
#include "llvm/Support/Path.h"
121
#include "llvm/Support/SaveAndRestore.h"
122
#include "llvm/Support/Timer.h"
123
#include "llvm/Support/VersionTuple.h"
124
#include "llvm/Support/raw_ostream.h"
125
#include <algorithm>
126
#include <cassert>
127
#include <cstddef>
128
#include <cstdint>
129
#include <cstdio>
130
#include <ctime>
131
#include <iterator>
132
#include <limits>
133
#include <map>
134
#include <memory>
135
#include <string>
136
#include <system_error>
137
#include <tuple>
138
#include <utility>
139
#include <vector>
140
141
using namespace clang;
142
using namespace clang::serialization;
143
using namespace clang::serialization::reader;
144
using llvm::BitstreamCursor;
145
146
//===----------------------------------------------------------------------===//
147
// ChainedASTReaderListener implementation
148
//===----------------------------------------------------------------------===//
149
150
bool
151
0
ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
152
0
  return First->ReadFullVersionInformation(FullVersion) ||
153
0
         Second->ReadFullVersionInformation(FullVersion);
154
0
}
155
156
5.31k
void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
157
5.31k
  First->ReadModuleName(ModuleName);
158
5.31k
  Second->ReadModuleName(ModuleName);
159
5.31k
}
160
161
5.21k
void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
162
5.21k
  First->ReadModuleMapFile(ModuleMapPath);
163
5.21k
  Second->ReadModuleMapFile(ModuleMapPath);
164
5.21k
}
165
166
bool
167
ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168
                                              bool Complain,
169
4.88k
                                              bool AllowCompatibleDifferences) {
170
4.88k
  return First->ReadLanguageOptions(LangOpts, Complain,
171
4.88k
                                    AllowCompatibleDifferences) ||
172
4.88k
         Second->ReadLanguageOptions(LangOpts, Complain,
173
4.88k
                                     AllowCompatibleDifferences);
174
4.88k
}
175
176
bool ChainedASTReaderListener::ReadTargetOptions(
177
    const TargetOptions &TargetOpts, bool Complain,
178
4.88k
    bool AllowCompatibleDifferences) {
179
4.88k
  return First->ReadTargetOptions(TargetOpts, Complain,
180
4.88k
                                  AllowCompatibleDifferences) ||
181
4.88k
         Second->ReadTargetOptions(TargetOpts, Complain,
182
4.88k
                                   AllowCompatibleDifferences);
183
4.88k
}
184
185
bool ChainedASTReaderListener::ReadDiagnosticOptions(
186
4.45k
    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
187
4.45k
  return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
188
4.45k
         
Second->ReadDiagnosticOptions(DiagOpts, Complain)4.45k
;
189
4.45k
}
190
191
bool
192
ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193
4.45k
                                                bool Complain) {
194
4.45k
  return First->ReadFileSystemOptions(FSOpts, Complain) ||
195
4.45k
         Second->ReadFileSystemOptions(FSOpts, Complain);
196
4.45k
}
197
198
bool ChainedASTReaderListener::ReadHeaderSearchOptions(
199
    const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200
4.45k
    bool Complain) {
201
4.45k
  return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202
4.45k
                                        Complain) ||
203
4.45k
         Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204
4.45k
                                         Complain);
205
4.45k
}
206
207
bool ChainedASTReaderListener::ReadPreprocessorOptions(
208
    const PreprocessorOptions &PPOpts, bool Complain,
209
4.45k
    std::string &SuggestedPredefines) {
210
4.45k
  return First->ReadPreprocessorOptions(PPOpts, Complain,
211
4.45k
                                        SuggestedPredefines) ||
212
4.45k
         Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
213
4.45k
}
214
215
void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216
6
                                           unsigned Value) {
217
6
  First->ReadCounter(M, Value);
218
6
  Second->ReadCounter(M, Value);
219
6
}
220
221
9.12k
bool ChainedASTReaderListener::needsInputFileVisitation() {
222
9.12k
  return First->needsInputFileVisitation() ||
223
9.12k
         
Second->needsInputFileVisitation()5.06k
;
224
9.12k
}
225
226
4.01k
bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
227
4.01k
  return First->needsSystemInputFileVisitation() ||
228
4.01k
  
Second->needsSystemInputFileVisitation()41
;
229
4.01k
}
230
231
void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232
9.05k
                                               ModuleKind Kind) {
233
9.05k
  First->visitModuleFile(Filename, Kind);
234
9.05k
  Second->visitModuleFile(Filename, Kind);
235
9.05k
}
236
237
bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
238
                                              bool isSystem,
239
                                              bool isOverridden,
240
514k
                                              bool isExplicitModule) {
241
514k
  bool Continue = false;
242
514k
  if (First->needsInputFileVisitation() &&
243
514k
      
(514k
!isSystem514k
||
First->needsSystemInputFileVisitation()514k
))
244
514k
    Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245
514k
                                      isExplicitModule);
246
514k
  if (Second->needsInputFileVisitation() &&
247
514k
      
(71
!isSystem71
||
Second->needsSystemInputFileVisitation()0
))
248
71
    Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
249
71
                                       isExplicitModule);
250
514k
  return Continue;
251
514k
}
252
253
void ChainedASTReaderListener::readModuleFileExtension(
254
0
       const ModuleFileExtensionMetadata &Metadata) {
255
0
  First->readModuleFileExtension(Metadata);
256
0
  Second->readModuleFileExtension(Metadata);
257
0
}
258
259
//===----------------------------------------------------------------------===//
260
// PCH validator implementation
261
//===----------------------------------------------------------------------===//
262
263
21.3k
ASTReaderListener::~ASTReaderListener() = default;
264
265
/// Compare the given set of language options against an existing set of
266
/// language options.
267
///
268
/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
269
/// \param AllowCompatibleDifferences If true, differences between compatible
270
///        language options will be permitted.
271
///
272
/// \returns true if the languagae options mis-match, false otherwise.
273
static bool checkLanguageOptions(const LangOptions &LangOpts,
274
                                 const LangOptions &ExistingLangOpts,
275
                                 DiagnosticsEngine *Diags,
276
7.76k
                                 bool AllowCompatibleDifferences = true) {
277
7.76k
#define LANGOPT(Name, Bits, Default, Description)                 \
278
1.28M
  if (ExistingLangOpts.Name != LangOpts.Name) {                   \
279
21
    if (Diags)                                                    \
280
21
      Diags->Report(diag::err_pch_langopt_mismatch)               \
281
7
        << Description << LangOpts.Name << ExistingLangOpts.Name; \
282
21
    return true;                                                  \
283
21
  }
284
285
7.76k
#define VALUE_LANGOPT(Name, Bits, Default, Description)   \
286
105k
  if (ExistingLangOpts.Name != LangOpts.Name) {           \
287
2
    if (Diags)                                            \
288
2
      Diags->Report(diag::err_pch_langopt_value_mismatch) \
289
0
        << Description;                                   \
290
2
    return true;                                          \
291
2
  }
292
293
7.76k
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
294
178k
  if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
295
0
    if (Diags)                                                 \
296
0
      Diags->Report(diag::err_pch_langopt_value_mismatch)      \
297
0
        << Description;                                        \
298
0
    return true;                                               \
299
0
  }
300
301
7.76k
#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
302
154k
  if (!AllowCompatibleDifferences)                            \
303
154k
    
LANGOPT144k
(Name, Bits, Default, Description)
304
305
7.76k
#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
306
7.76k
  if (!AllowCompatibleDifferences)                                 \
307
7.76k
    ENUM_LANGOPT(Name, Bits, Default, Description)
308
309
7.76k
#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
310
38.7k
  if (!AllowCompatibleDifferences)                                 \
311
38.7k
    
VALUE_LANGOPT36.0k
(Name, Bits, Default, Description)
312
313
7.76k
#define BENIGN_LANGOPT(Name, Bits, Default, Description)
314
7.76k
#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
315
7.76k
#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
316
7.76k
#include "clang/Basic/LangOptions.def"
317
318
7.74k
  if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319
1
    if (Diags)
320
1
      Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
321
1
    return true;
322
1
  }
323
324
7.74k
  if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325
0
    if (Diags)
326
0
      Diags->Report(diag::err_pch_langopt_value_mismatch)
327
0
      << "target Objective-C runtime";
328
0
    return true;
329
0
  }
330
331
7.74k
  if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
332
7.74k
      LangOpts.CommentOpts.BlockCommandNames) {
333
0
    if (Diags)
334
0
      Diags->Report(diag::err_pch_langopt_value_mismatch)
335
0
        << "block command names";
336
0
    return true;
337
0
  }
338
339
  // Sanitizer feature mismatches are treated as compatible differences. If
340
  // compatible differences aren't allowed, we still only want to check for
341
  // mismatches of non-modular sanitizers (the only ones which can affect AST
342
  // generation).
343
7.74k
  if (!AllowCompatibleDifferences) {
344
7.20k
    SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
345
7.20k
    SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
346
7.20k
    SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
347
7.20k
    ExistingSanitizers.clear(ModularSanitizers);
348
7.20k
    ImportedSanitizers.clear(ModularSanitizers);
349
7.20k
    if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
350
1
      const std::string Flag = "-fsanitize=";
351
1
      if (Diags) {
352
1
#define SANITIZER(NAME, ID)                                                    \
353
55
  {                                                                            \
354
55
    bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
355
55
    bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
356
55
    if (InExistingModule != InImportedModule)                                  \
357
55
      Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
358
1
          << InExistingModule << (Flag + NAME);                                \
359
55
  }
360
1
#include "clang/Basic/Sanitizers.def"
361
1
      }
362
1
      return true;
363
1
    }
364
7.20k
  }
365
366
7.74k
  return false;
367
7.74k
}
368
369
/// Compare the given set of target options against an existing set of
370
/// target options.
371
///
372
/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373
///
374
/// \returns true if the target options mis-match, false otherwise.
375
static bool checkTargetOptions(const TargetOptions &TargetOpts,
376
                               const TargetOptions &ExistingTargetOpts,
377
                               DiagnosticsEngine *Diags,
378
7.76k
                               bool AllowCompatibleDifferences = true) {
379
7.76k
#define CHECK_TARGET_OPT(Field, Name)                             \
380
29.9k
  if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
381
5
    if (Diags)                                                    \
382
5
      Diags->Report(diag::err_pch_targetopt_mismatch)             \
383
2
        << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
384
5
    return true;                                                  \
385
5
  }
386
387
  // The triple and ABI must match exactly.
388
7.76k
  CHECK_TARGET_OPT(Triple, "target");
389
7.76k
  CHECK_TARGET_OPT(ABI, "target ABI");
390
391
  // We can tolerate different CPUs in many cases, notably when one CPU
392
  // supports a strict superset of another. When allowing compatible
393
  // differences skip this check.
394
7.76k
  if (!AllowCompatibleDifferences) {
395
7.22k
    CHECK_TARGET_OPT(CPU, "target CPU");
396
7.22k
    CHECK_TARGET_OPT(TuneCPU, "tune CPU");
397
7.22k
  }
398
399
7.76k
#undef CHECK_TARGET_OPT
400
401
  // Compare feature sets.
402
7.76k
  SmallVector<StringRef, 4> ExistingFeatures(
403
7.76k
                                             ExistingTargetOpts.FeaturesAsWritten.begin(),
404
7.76k
                                             ExistingTargetOpts.FeaturesAsWritten.end());
405
7.76k
  SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
406
7.76k
                                         TargetOpts.FeaturesAsWritten.end());
407
7.76k
  llvm::sort(ExistingFeatures);
408
7.76k
  llvm::sort(ReadFeatures);
409
410
  // We compute the set difference in both directions explicitly so that we can
411
  // diagnose the differences differently.
412
7.76k
  SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
413
7.76k
  std::set_difference(
414
7.76k
      ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
415
7.76k
      ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
416
7.76k
  std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
417
7.76k
                      ExistingFeatures.begin(), ExistingFeatures.end(),
418
7.76k
                      std::back_inserter(UnmatchedReadFeatures));
419
420
  // If we are allowing compatible differences and the read feature set is
421
  // a strict subset of the existing feature set, there is nothing to diagnose.
422
7.76k
  if (AllowCompatibleDifferences && 
UnmatchedReadFeatures.empty()540
)
423
538
    return false;
424
425
7.22k
  if (Diags) {
426
7.20k
    for (StringRef Feature : UnmatchedReadFeatures)
427
2
      Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428
2
          << /* is-existing-feature */ false << Feature;
429
7.20k
    for (StringRef Feature : UnmatchedExistingFeatures)
430
1
      Diags->Report(diag::err_pch_targetopt_feature_mismatch)
431
1
          << /* is-existing-feature */ true << Feature;
432
7.20k
  }
433
434
7.22k
  return !UnmatchedReadFeatures.empty() || 
!UnmatchedExistingFeatures.empty()7.22k
;
435
7.76k
}
436
437
bool
438
PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
439
                                  bool Complain,
440
7.75k
                                  bool AllowCompatibleDifferences) {
441
7.75k
  const LangOptions &ExistingLangOpts = PP.getLangOpts();
442
7.75k
  return checkLanguageOptions(LangOpts, ExistingLangOpts,
443
7.75k
                              Complain ? 
&Reader.Diags7.66k
:
nullptr91
,
444
7.75k
                              AllowCompatibleDifferences);
445
7.75k
}
446
447
bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
448
                                     bool Complain,
449
7.75k
                                     bool AllowCompatibleDifferences) {
450
7.75k
  const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
451
7.75k
  return checkTargetOptions(TargetOpts, ExistingTargetOpts,
452
7.75k
                            Complain ? 
&Reader.Diags7.66k
:
nullptr90
,
453
7.75k
                            AllowCompatibleDifferences);
454
7.75k
}
455
456
namespace {
457
458
using MacroDefinitionsMap =
459
    llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
460
using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461
462
} // namespace
463
464
static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
465
                                         DiagnosticsEngine &Diags,
466
1.80k
                                         bool Complain) {
467
1.80k
  using Level = DiagnosticsEngine::Level;
468
469
  // Check current mappings for new -Werror mappings, and the stored mappings
470
  // for cases that were explicitly mapped to *not* be errors that are now
471
  // errors because of options like -Werror.
472
1.80k
  DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
473
474
3.59k
  for (DiagnosticsEngine *MappingSource : MappingSources) {
475
34.5k
    for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
476
34.5k
      diag::kind DiagID = DiagIDMappingPair.first;
477
34.5k
      Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
478
34.5k
      if (CurLevel < DiagnosticsEngine::Error)
479
31.7k
        continue; // not significant
480
2.76k
      Level StoredLevel =
481
2.76k
          StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
482
2.76k
      if (StoredLevel < DiagnosticsEngine::Error) {
483
5
        if (Complain)
484
0
          Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
485
0
              Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486
5
        return true;
487
5
      }
488
2.76k
    }
489
3.59k
  }
490
491
1.79k
  return false;
492
1.80k
}
493
494
1.80k
static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
495
1.80k
  diag::Severity Ext = Diags.getExtensionHandlingBehavior();
496
1.80k
  if (Ext == diag::Severity::Warning && 
Diags.getWarningsAsErrors()7
)
497
5
    return true;
498
1.79k
  return Ext >= diag::Severity::Error;
499
1.80k
}
500
501
static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
502
                                    DiagnosticsEngine &Diags,
503
3.93k
                                    bool IsSystem, bool Complain) {
504
  // Top-level options
505
3.93k
  if (IsSystem) {
506
2.13k
    if (Diags.getSuppressSystemWarnings())
507
2.12k
      return false;
508
    // If -Wsystem-headers was not enabled before, be conservative
509
4
    if (StoredDiags.getSuppressSystemWarnings()) {
510
2
      if (Complain)
511
0
        Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
512
2
      return true;
513
2
    }
514
4
  }
515
516
1.80k
  if (Diags.getWarningsAsErrors() && 
!StoredDiags.getWarningsAsErrors()48
) {
517
5
    if (Complain)
518
0
      Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519
5
    return true;
520
5
  }
521
522
1.80k
  if (Diags.getWarningsAsErrors() && 
Diags.getEnableAllWarnings()43
&&
523
1.80k
      
!StoredDiags.getEnableAllWarnings()0
) {
524
0
    if (Complain)
525
0
      Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526
0
    return true;
527
0
  }
528
529
1.80k
  if (isExtHandlingFromDiagsError(Diags) &&
530
1.80k
      
!isExtHandlingFromDiagsError(StoredDiags)4
) {
531
1
    if (Complain)
532
0
      Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533
1
    return true;
534
1
  }
535
536
1.80k
  return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
537
1.80k
}
538
539
/// Return the top import module if it is implicit, nullptr otherwise.
540
static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
541
7.24k
                                          Preprocessor &PP) {
542
  // If the original import came from a file explicitly generated by the user,
543
  // don't check the diagnostic mappings.
544
  // FIXME: currently this is approximated by checking whether this is not a
545
  // module import of an implicitly-loaded module file.
546
  // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
547
  // the transitive closure of its imports, since unrelated modules cannot be
548
  // imported until after this module finishes validation.
549
7.24k
  ModuleFile *TopImport = &*ModuleMgr.rbegin();
550
7.24k
  while (!TopImport->ImportedBy.empty())
551
0
    TopImport = TopImport->ImportedBy[0];
552
7.24k
  if (TopImport->Kind != MK_ImplicitModule)
553
3.30k
    return nullptr;
554
555
3.93k
  StringRef ModuleName = TopImport->ModuleName;
556
3.93k
  assert(!ModuleName.empty() && "diagnostic options read before module name");
557
558
0
  Module *M =
559
3.93k
      PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
560
3.93k
  assert(M && "missing module");
561
0
  return M;
562
7.24k
}
563
564
bool PCHValidator::ReadDiagnosticOptions(
565
7.24k
    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
566
7.24k
  DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
567
7.24k
  IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
568
7.24k
  IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
569
7.24k
      new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
570
  // This should never fail, because we would have processed these options
571
  // before writing them to an ASTFile.
572
7.24k
  ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
573
574
7.24k
  ModuleManager &ModuleMgr = Reader.getModuleManager();
575
7.24k
  assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
576
577
0
  Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
578
7.24k
  if (!TopM)
579
3.30k
    return false;
580
581
  // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
582
  // contains the union of their flags.
583
3.93k
  return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
584
3.93k
                                 Complain);
585
7.24k
}
586
587
/// Collect the macro definitions provided by the given preprocessor
588
/// options.
589
static void
590
collectMacroDefinitions(const PreprocessorOptions &PPOpts,
591
                        MacroDefinitionsMap &Macros,
592
15.2k
                        SmallVectorImpl<StringRef> *MacroNames = nullptr) {
593
25.5k
  for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; 
++I10.2k
) {
594
10.2k
    StringRef Macro = PPOpts.Macros[I].first;
595
10.2k
    bool IsUndef = PPOpts.Macros[I].second;
596
597
10.2k
    std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
598
10.2k
    StringRef MacroName = MacroPair.first;
599
10.2k
    StringRef MacroBody = MacroPair.second;
600
601
    // For an #undef'd macro, we only care about the name.
602
10.2k
    if (IsUndef) {
603
12
      if (MacroNames && 
!Macros.count(MacroName)8
)
604
8
        MacroNames->push_back(MacroName);
605
606
12
      Macros[MacroName] = std::make_pair("", true);
607
12
      continue;
608
12
    }
609
610
    // For a #define'd macro, figure out the actual definition.
611
10.2k
    if (MacroName.size() == Macro.size())
612
6.08k
      MacroBody = "1";
613
4.15k
    else {
614
      // Note: GCC drops anything following an end-of-line character.
615
4.15k
      StringRef::size_type End = MacroBody.find_first_of("\n\r");
616
4.15k
      MacroBody = MacroBody.substr(0, End);
617
4.15k
    }
618
619
10.2k
    if (MacroNames && 
!Macros.count(MacroName)4.83k
)
620
4.83k
      MacroNames->push_back(MacroName);
621
10.2k
    Macros[MacroName] = std::make_pair(MacroBody, false);
622
10.2k
  }
623
15.2k
}
624
625
/// Check the preprocessor options deserialized from the control block
626
/// against the preprocessor options in an existing preprocessor.
627
///
628
/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
629
/// \param Validate If true, validate preprocessor options. If false, allow
630
///        macros defined by \p ExistingPPOpts to override those defined by
631
///        \p PPOpts in SuggestedPredefines.
632
static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
633
                                     const PreprocessorOptions &ExistingPPOpts,
634
                                     DiagnosticsEngine *Diags,
635
                                     FileManager &FileMgr,
636
                                     std::string &SuggestedPredefines,
637
                                     const LangOptions &LangOpts,
638
7.62k
                                     bool Validate = true) {
639
  // Check macro definitions.
640
7.62k
  MacroDefinitionsMap ASTFileMacros;
641
7.62k
  collectMacroDefinitions(PPOpts, ASTFileMacros);
642
7.62k
  MacroDefinitionsMap ExistingMacros;
643
7.62k
  SmallVector<StringRef, 4> ExistingMacroNames;
644
7.62k
  collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
645
646
12.4k
  for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; 
++I4.82k
) {
647
    // Dig out the macro definition in the existing preprocessor options.
648
4.83k
    StringRef MacroName = ExistingMacroNames[I];
649
4.83k
    std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
650
651
    // Check whether we know anything about this macro name or not.
652
4.83k
    llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
653
4.83k
        ASTFileMacros.find(MacroName);
654
4.83k
    if (!Validate || 
Known == ASTFileMacros.end()4.50k
) {
655
      // FIXME: Check whether this identifier was referenced anywhere in the
656
      // AST file. If so, we should reject the AST file. Unfortunately, this
657
      // information isn't in the control block. What shall we do about it?
658
659
409
      if (Existing.second) {
660
2
        SuggestedPredefines += "#undef ";
661
2
        SuggestedPredefines += MacroName.str();
662
2
        SuggestedPredefines += '\n';
663
407
      } else {
664
407
        SuggestedPredefines += "#define ";
665
407
        SuggestedPredefines += MacroName.str();
666
407
        SuggestedPredefines += ' ';
667
407
        SuggestedPredefines += Existing.first.str();
668
407
        SuggestedPredefines += '\n';
669
407
      }
670
409
      continue;
671
409
    }
672
673
    // If the macro was defined in one but undef'd in the other, we have a
674
    // conflict.
675
4.42k
    if (Existing.second != Known->second.second) {
676
2
      if (Diags) {
677
2
        Diags->Report(diag::err_pch_macro_def_undef)
678
2
          << MacroName << Known->second.second;
679
2
      }
680
2
      return true;
681
2
    }
682
683
    // If the macro was #undef'd in both, or if the macro bodies are identical,
684
    // it's fine.
685
4.42k
    if (Existing.second || 
Existing.first == Known->second.first4.42k
)
686
4.41k
      continue;
687
688
    // The macro bodies differ; complain.
689
6
    if (Diags) {
690
3
      Diags->Report(diag::err_pch_macro_def_conflict)
691
3
        << MacroName << Known->second.first << Existing.first;
692
3
    }
693
6
    return true;
694
4.42k
  }
695
696
  // Check whether we're using predefines.
697
7.62k
  if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && 
Validate1
) {
698
1
    if (Diags) {
699
1
      Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
700
1
    }
701
1
    return true;
702
1
  }
703
704
  // Detailed record is important since it is used for the module cache hash.
705
7.61k
  if (LangOpts.Modules &&
706
7.61k
      
PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord4.02k
&&
Validate0
) {
707
0
    if (Diags) {
708
0
      Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
709
0
    }
710
0
    return true;
711
0
  }
712
713
  // Compute the #include and #include_macros lines we need.
714
8.35k
  
for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); 7.61k
I != N;
++I740
) {
715
740
    StringRef File = ExistingPPOpts.Includes[I];
716
717
740
    if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
718
740
        
!ExistingPPOpts.PCHThroughHeader.empty()17
) {
719
      // In case the through header is an include, we must add all the includes
720
      // to the predefines so the start point can be determined.
721
13
      SuggestedPredefines += "#include \"";
722
13
      SuggestedPredefines += File;
723
13
      SuggestedPredefines += "\"\n";
724
13
      continue;
725
13
    }
726
727
727
    if (File == ExistingPPOpts.ImplicitPCHInclude)
728
0
      continue;
729
730
727
    if (llvm::is_contained(PPOpts.Includes, File))
731
2
      continue;
732
733
725
    SuggestedPredefines += "#include \"";
734
725
    SuggestedPredefines += File;
735
725
    SuggestedPredefines += "\"\n";
736
725
  }
737
738
7.61k
  for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; 
++I0
) {
739
0
    StringRef File = ExistingPPOpts.MacroIncludes[I];
740
0
    if (llvm::is_contained(PPOpts.MacroIncludes, File))
741
0
      continue;
742
743
0
    SuggestedPredefines += "#__include_macros \"";
744
0
    SuggestedPredefines += File;
745
0
    SuggestedPredefines += "\"\n##\n";
746
0
  }
747
748
7.61k
  return false;
749
7.61k
}
750
751
bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
752
                                           bool Complain,
753
7.21k
                                           std::string &SuggestedPredefines) {
754
7.21k
  const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
755
756
7.21k
  return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
757
7.21k
                                  Complain? 
&Reader.Diags7.20k
:
nullptr6
,
758
7.21k
                                  PP.getFileManager(),
759
7.21k
                                  SuggestedPredefines,
760
7.21k
                                  PP.getLangOpts());
761
7.21k
}
762
763
bool SimpleASTReaderListener::ReadPreprocessorOptions(
764
                                  const PreprocessorOptions &PPOpts,
765
                                  bool Complain,
766
403
                                  std::string &SuggestedPredefines) {
767
403
  return checkPreprocessorOptions(PPOpts,
768
403
                                  PP.getPreprocessorOpts(),
769
403
                                  nullptr,
770
403
                                  PP.getFileManager(),
771
403
                                  SuggestedPredefines,
772
403
                                  PP.getLangOpts(),
773
403
                                  false);
774
403
}
775
776
/// Check the header search options deserialized from the control block
777
/// against the header search options in an existing preprocessor.
778
///
779
/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
780
static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
781
                                     StringRef SpecificModuleCachePath,
782
                                     StringRef ExistingModuleCachePath,
783
                                     DiagnosticsEngine *Diags,
784
                                     const LangOptions &LangOpts,
785
7.22k
                                     const PreprocessorOptions &PPOpts) {
786
7.22k
  if (LangOpts.Modules) {
787
3.99k
    if (SpecificModuleCachePath != ExistingModuleCachePath &&
788
3.99k
        
!PPOpts.AllowPCHWithDifferentModulesCachePath9
) {
789
3
      if (Diags)
790
3
        Diags->Report(diag::err_pch_modulecache_mismatch)
791
3
          << SpecificModuleCachePath << ExistingModuleCachePath;
792
3
      return true;
793
3
    }
794
3.99k
  }
795
796
7.22k
  return false;
797
7.22k
}
798
799
bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
800
                                           StringRef SpecificModuleCachePath,
801
7.21k
                                           bool Complain) {
802
7.21k
  return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
803
7.21k
                                  PP.getHeaderSearchInfo().getModuleCachePath(),
804
7.21k
                                  Complain ? 
&Reader.Diags7.20k
:
nullptr6
,
805
7.21k
                                  PP.getLangOpts(), PP.getPreprocessorOpts());
806
7.21k
}
807
808
6
void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
809
6
  PP.setCounterValue(Value);
810
6
}
811
812
//===----------------------------------------------------------------------===//
813
// AST reader implementation
814
//===----------------------------------------------------------------------===//
815
816
89.8M
static uint64_t readULEB(const unsigned char *&P) {
817
89.8M
  unsigned Length = 0;
818
89.8M
  const char *Error = nullptr;
819
820
89.8M
  uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
821
89.8M
  if (Error)
822
0
    llvm::report_fatal_error(Error);
823
89.8M
  P += Length;
824
89.8M
  return Val;
825
89.8M
}
826
827
/// Read ULEB-encoded key length and data length.
828
static std::pair<unsigned, unsigned>
829
44.9M
readULEBKeyDataLength(const unsigned char *&P) {
830
44.9M
  unsigned KeyLen = readULEB(P);
831
44.9M
  if ((unsigned)KeyLen != KeyLen)
832
0
    llvm::report_fatal_error("key too large");
833
834
44.9M
  unsigned DataLen = readULEB(P);
835
44.9M
  if ((unsigned)DataLen != DataLen)
836
0
    llvm::report_fatal_error("data too large");
837
838
44.9M
  return std::make_pair(KeyLen, DataLen);
839
44.9M
}
840
841
void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
842
13.5k
                                           bool TakeOwnership) {
843
13.5k
  DeserializationListener = Listener;
844
13.5k
  OwnsDeserializationListener = TakeOwnership;
845
13.5k
}
846
847
144k
unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
848
144k
  return serialization::ComputeHash(Sel);
849
144k
}
850
851
std::pair<unsigned, unsigned>
852
80.8k
ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
853
80.8k
  return readULEBKeyDataLength(d);
854
80.8k
}
855
856
ASTSelectorLookupTrait::internal_key_type
857
24.9k
ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
858
24.9k
  using namespace llvm::support;
859
860
24.9k
  SelectorTable &SelTable = Reader.getContext().Selectors;
861
24.9k
  unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
862
24.9k
  IdentifierInfo *FirstII = Reader.getLocalIdentifier(
863
24.9k
      F, endian::readNext<uint32_t, little, unaligned>(d));
864
24.9k
  if (N == 0)
865
8.31k
    return SelTable.getNullarySelector(FirstII);
866
16.6k
  else if (N == 1)
867
12.3k
    return SelTable.getUnarySelector(FirstII);
868
869
4.36k
  SmallVector<IdentifierInfo *, 16> Args;
870
4.36k
  Args.push_back(FirstII);
871
11.7k
  for (unsigned I = 1; I != N; 
++I7.34k
)
872
7.34k
    Args.push_back(Reader.getLocalIdentifier(
873
7.34k
        F, endian::readNext<uint32_t, little, unaligned>(d)));
874
875
4.36k
  return SelTable.getSelector(N, Args.data());
876
24.9k
}
877
878
ASTSelectorLookupTrait::data_type
879
ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
880
1.87k
                                 unsigned DataLen) {
881
1.87k
  using namespace llvm::support;
882
883
1.87k
  data_type Result;
884
885
1.87k
  Result.ID = Reader.getGlobalSelectorID(
886
1.87k
      F, endian::readNext<uint32_t, little, unaligned>(d));
887
1.87k
  unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
888
1.87k
  unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
889
1.87k
  Result.InstanceBits = FullInstanceBits & 0x3;
890
1.87k
  Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
891
1.87k
  Result.FactoryBits = FullFactoryBits & 0x3;
892
1.87k
  Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
893
1.87k
  unsigned NumInstanceMethods = FullInstanceBits >> 3;
894
1.87k
  unsigned NumFactoryMethods = FullFactoryBits >> 3;
895
896
  // Load instance methods
897
7.78k
  for (unsigned I = 0; I != NumInstanceMethods; 
++I5.90k
) {
898
5.90k
    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
899
5.90k
            F, endian::readNext<uint32_t, little, unaligned>(d)))
900
5.90k
      Result.Instance.push_back(Method);
901
5.90k
  }
902
903
  // Load factory methods
904
2.11k
  for (unsigned I = 0; I != NumFactoryMethods; 
++I244
) {
905
244
    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
906
244
            F, endian::readNext<uint32_t, little, unaligned>(d)))
907
244
      Result.Factory.push_back(Method);
908
244
  }
909
910
1.87k
  return Result;
911
1.87k
}
912
913
11.2M
unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
914
11.2M
  return llvm::djbHash(a);
915
11.2M
}
916
917
std::pair<unsigned, unsigned>
918
38.6M
ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
919
38.6M
  return readULEBKeyDataLength(d);
920
38.6M
}
921
922
ASTIdentifierLookupTraitBase::internal_key_type
923
21.0M
ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
924
21.0M
  assert(n >= 2 && d[n-1] == '\0');
925
0
  return StringRef((const char*) d, n-1);
926
21.0M
}
927
928
/// Whether the given identifier is "interesting".
929
static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
930
16.7M
                                    bool IsModule) {
931
16.7M
  return II.hadMacroDefinition() || 
II.isPoisoned()16.6M
||
932
16.7M
         
(16.6M
!IsModule16.6M
&&
II.getObjCOrBuiltinID()14.8M
) ||
933
16.7M
         
II.hasRevertedTokenIDToIdentifier()16.6M
||
934
16.7M
         
(16.6M
!(16.6M
IsModule16.6M
&&
Reader.getPreprocessor().getLangOpts().CPlusPlus1.85M
) &&
935
16.6M
          
II.getFETokenInfo()14.8M
);
936
16.7M
}
937
938
11.7M
static bool readBit(unsigned &Bits) {
939
11.7M
  bool Value = Bits & 0x1;
940
11.7M
  Bits >>= 1;
941
11.7M
  return Value;
942
11.7M
}
943
944
14.5M
IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
945
14.5M
  using namespace llvm::support;
946
947
14.5M
  unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
948
14.5M
  return Reader.getGlobalIdentifierID(F, RawID >> 1);
949
14.5M
}
950
951
18.5M
static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
952
18.5M
  if (!II.isFromAST()) {
953
16.7M
    II.setIsFromAST();
954
16.7M
    bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
955
16.7M
    if (isInterestingIdentifier(Reader, II, IsModule))
956
81.7k
      II.setChangedSinceDeserialization();
957
16.7M
  }
958
18.5M
}
959
960
IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
961
                                                   const unsigned char* d,
962
2.44M
                                                   unsigned DataLen) {
963
2.44M
  using namespace llvm::support;
964
965
2.44M
  unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
966
2.44M
  bool IsInteresting = RawID & 0x01;
967
968
  // Wipe out the "is interesting" bit.
969
2.44M
  RawID = RawID >> 1;
970
971
  // Build the IdentifierInfo and link the identifier ID with it.
972
2.44M
  IdentifierInfo *II = KnownII;
973
2.44M
  if (!II) {
974
2.44M
    II = &Reader.getIdentifierTable().getOwn(k);
975
2.44M
    KnownII = II;
976
2.44M
  }
977
2.44M
  markIdentifierFromAST(Reader, *II);
978
2.44M
  Reader.markIdentifierUpToDate(II);
979
980
2.44M
  IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
981
2.44M
  if (!IsInteresting) {
982
    // For uninteresting identifiers, there's nothing else to do. Just notify
983
    // the reader that we've finished loading this identifier.
984
94.6k
    Reader.SetIdentifierInfo(ID, II);
985
94.6k
    return II;
986
94.6k
  }
987
988
2.35M
  unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
989
2.35M
  unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
990
2.35M
  bool CPlusPlusOperatorKeyword = readBit(Bits);
991
2.35M
  bool HasRevertedTokenIDToIdentifier = readBit(Bits);
992
2.35M
  bool Poisoned = readBit(Bits);
993
2.35M
  bool ExtensionToken = readBit(Bits);
994
2.35M
  bool HadMacroDefinition = readBit(Bits);
995
996
2.35M
  assert(Bits == 0 && "Extra bits in the identifier?");
997
0
  DataLen -= 8;
998
999
  // Set or check the various bits in the IdentifierInfo structure.
1000
  // Token IDs are read-only.
1001
2.35M
  if (HasRevertedTokenIDToIdentifier && 
II->getTokenID() != tok::identifier51
)
1002
51
    II->revertTokenIDToIdentifier();
1003
2.35M
  if (!F.isModule())
1004
614k
    II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1005
2.35M
  assert(II->isExtensionToken() == ExtensionToken &&
1006
2.35M
         "Incorrect extension token flag");
1007
0
  (void)ExtensionToken;
1008
2.35M
  if (Poisoned)
1009
1.14k
    II->setIsPoisoned(true);
1010
2.35M
  assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1011
2.35M
         "Incorrect C++ operator keyword flag");
1012
0
  (void)CPlusPlusOperatorKeyword;
1013
1014
  // If this identifier is a macro, deserialize the macro
1015
  // definition.
1016
2.35M
  if (HadMacroDefinition) {
1017
1.71M
    uint32_t MacroDirectivesOffset =
1018
1.71M
        endian::readNext<uint32_t, little, unaligned>(d);
1019
1.71M
    DataLen -= 4;
1020
1021
1.71M
    Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1022
1.71M
  }
1023
1024
2.35M
  Reader.SetIdentifierInfo(ID, II);
1025
1026
  // Read all of the declarations visible at global scope with this
1027
  // name.
1028
2.35M
  if (DataLen > 0) {
1029
103k
    SmallVector<uint32_t, 4> DeclIDs;
1030
210k
    for (; DataLen > 0; 
DataLen -= 4106k
)
1031
106k
      DeclIDs.push_back(Reader.getGlobalDeclID(
1032
106k
          F, endian::readNext<uint32_t, little, unaligned>(d)));
1033
103k
    Reader.SetGloballyVisibleDecls(II, DeclIDs);
1034
103k
  }
1035
1036
2.35M
  return II;
1037
2.44M
}
1038
1039
DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1040
2.29M
    : Kind(Name.getNameKind()) {
1041
2.29M
  switch (Kind) {
1042
1.81M
  case DeclarationName::Identifier:
1043
1.81M
    Data = (uint64_t)Name.getAsIdentifierInfo();
1044
1.81M
    break;
1045
208k
  case DeclarationName::ObjCZeroArgSelector:
1046
341k
  case DeclarationName::ObjCOneArgSelector:
1047
361k
  case DeclarationName::ObjCMultiArgSelector:
1048
361k
    Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1049
361k
    break;
1050
48.2k
  case DeclarationName::CXXOperatorName:
1051
48.2k
    Data = Name.getCXXOverloadedOperator();
1052
48.2k
    break;
1053
157
  case DeclarationName::CXXLiteralOperatorName:
1054
157
    Data = (uint64_t)Name.getCXXLiteralIdentifier();
1055
157
    break;
1056
185
  case DeclarationName::CXXDeductionGuideName:
1057
185
    Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1058
185
               ->getDeclName().getAsIdentifierInfo();
1059
185
    break;
1060
33.4k
  case DeclarationName::CXXConstructorName:
1061
57.6k
  case DeclarationName::CXXDestructorName:
1062
61.8k
  case DeclarationName::CXXConversionFunctionName:
1063
66.1k
  case DeclarationName::CXXUsingDirective:
1064
66.1k
    Data = 0;
1065
66.1k
    break;
1066
2.29M
  }
1067
2.29M
}
1068
1069
5.96M
unsigned DeclarationNameKey::getHash() const {
1070
5.96M
  llvm::FoldingSetNodeID ID;
1071
5.96M
  ID.AddInteger(Kind);
1072
1073
5.96M
  switch (Kind) {
1074
5.49M
  case DeclarationName::Identifier:
1075
5.49M
  case DeclarationName::CXXLiteralOperatorName:
1076
5.49M
  case DeclarationName::CXXDeductionGuideName:
1077
5.49M
    ID.AddString(((IdentifierInfo*)Data)->getName());
1078
5.49M
    break;
1079
208k
  case DeclarationName::ObjCZeroArgSelector:
1080
341k
  case DeclarationName::ObjCOneArgSelector:
1081
361k
  case DeclarationName::ObjCMultiArgSelector:
1082
361k
    ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1083
361k
    break;
1084
48.5k
  case DeclarationName::CXXOperatorName:
1085
48.5k
    ID.AddInteger((OverloadedOperatorKind)Data);
1086
48.5k
    break;
1087
33.4k
  case DeclarationName::CXXConstructorName:
1088
57.7k
  case DeclarationName::CXXDestructorName:
1089
61.8k
  case DeclarationName::CXXConversionFunctionName:
1090
66.2k
  case DeclarationName::CXXUsingDirective:
1091
66.2k
    break;
1092
5.96M
  }
1093
1094
5.96M
  return ID.ComputeHash();
1095
5.96M
}
1096
1097
ModuleFile *
1098
8.88k
ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1099
8.88k
  using namespace llvm::support;
1100
1101
8.88k
  uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1102
8.88k
  return Reader.getLocalModuleFile(F, ModuleFileID);
1103
8.88k
}
1104
1105
std::pair<unsigned, unsigned>
1106
3.56M
ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1107
3.56M
  return readULEBKeyDataLength(d);
1108
3.56M
}
1109
1110
ASTDeclContextNameLookupTrait::internal_key_type
1111
1.59M
ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1112
1.59M
  using namespace llvm::support;
1113
1114
1.59M
  auto Kind = (DeclarationName::NameKind)*d++;
1115
1.59M
  uint64_t Data;
1116
1.59M
  switch (Kind) {
1117
1.52M
  case DeclarationName::Identifier:
1118
1.52M
  case DeclarationName::CXXLiteralOperatorName:
1119
1.52M
  case DeclarationName::CXXDeductionGuideName:
1120
1.52M
    Data = (uint64_t)Reader.getLocalIdentifier(
1121
1.52M
        F, endian::readNext<uint32_t, little, unaligned>(d));
1122
1.52M
    break;
1123
2.25k
  case DeclarationName::ObjCZeroArgSelector:
1124
2.90k
  case DeclarationName::ObjCOneArgSelector:
1125
3.12k
  case DeclarationName::ObjCMultiArgSelector:
1126
3.12k
    Data =
1127
3.12k
        (uint64_t)Reader.getLocalSelector(
1128
3.12k
                             F, endian::readNext<uint32_t, little, unaligned>(
1129
3.12k
                                    d)).getAsOpaquePtr();
1130
3.12k
    break;
1131
29.9k
  case DeclarationName::CXXOperatorName:
1132
29.9k
    Data = *d++; // OverloadedOperatorKind
1133
29.9k
    break;
1134
19.5k
  case DeclarationName::CXXConstructorName:
1135
33.0k
  case DeclarationName::CXXDestructorName:
1136
35.6k
  case DeclarationName::CXXConversionFunctionName:
1137
36.4k
  case DeclarationName::CXXUsingDirective:
1138
36.4k
    Data = 0;
1139
36.4k
    break;
1140
1.59M
  }
1141
1142
1.59M
  return DeclarationNameKey(Kind, Data);
1143
1.59M
}
1144
1145
void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1146
                                                 const unsigned char *d,
1147
                                                 unsigned DataLen,
1148
1.59M
                                                 data_type_builder &Val) {
1149
1.59M
  using namespace llvm::support;
1150
1151
3.85M
  for (unsigned NumDecls = DataLen / 4; NumDecls; 
--NumDecls2.26M
) {
1152
2.26M
    uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1153
2.26M
    Val.insert(Reader.getGlobalDeclID(F, LocalID));
1154
2.26M
  }
1155
1.59M
}
1156
1157
bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1158
                                              BitstreamCursor &Cursor,
1159
                                              uint64_t Offset,
1160
2.42M
                                              DeclContext *DC) {
1161
2.42M
  assert(Offset != 0);
1162
1163
0
  SavedStreamPosition SavedPosition(Cursor);
1164
2.42M
  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1165
0
    Error(std::move(Err));
1166
0
    return true;
1167
0
  }
1168
1169
2.42M
  RecordData Record;
1170
2.42M
  StringRef Blob;
1171
2.42M
  Expected<unsigned> MaybeCode = Cursor.ReadCode();
1172
2.42M
  if (!MaybeCode) {
1173
0
    Error(MaybeCode.takeError());
1174
0
    return true;
1175
0
  }
1176
2.42M
  unsigned Code = MaybeCode.get();
1177
1178
2.42M
  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1179
2.42M
  if (!MaybeRecCode) {
1180
0
    Error(MaybeRecCode.takeError());
1181
0
    return true;
1182
0
  }
1183
2.42M
  unsigned RecCode = MaybeRecCode.get();
1184
2.42M
  if (RecCode != DECL_CONTEXT_LEXICAL) {
1185
0
    Error("Expected lexical block");
1186
0
    return true;
1187
0
  }
1188
1189
2.42M
  assert(!isa<TranslationUnitDecl>(DC) &&
1190
2.42M
         "expected a TU_UPDATE_LEXICAL record for TU");
1191
  // If we are handling a C++ class template instantiation, we can see multiple
1192
  // lexical updates for the same record. It's important that we select only one
1193
  // of them, so that field numbering works properly. Just pick the first one we
1194
  // see.
1195
0
  auto &Lex = LexicalDecls[DC];
1196
2.42M
  if (!Lex.first) {
1197
2.42M
    Lex = std::make_pair(
1198
2.42M
        &M, llvm::makeArrayRef(
1199
2.42M
                reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1200
2.42M
                    Blob.data()),
1201
2.42M
                Blob.size() / 4));
1202
2.42M
  }
1203
2.42M
  DC->setHasExternalLexicalStorage(true);
1204
2.42M
  return false;
1205
2.42M
}
1206
1207
bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1208
                                              BitstreamCursor &Cursor,
1209
                                              uint64_t Offset,
1210
1.17M
                                              DeclID ID) {
1211
1.17M
  assert(Offset != 0);
1212
1213
0
  SavedStreamPosition SavedPosition(Cursor);
1214
1.17M
  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1215
0
    Error(std::move(Err));
1216
0
    return true;
1217
0
  }
1218
1219
1.17M
  RecordData Record;
1220
1.17M
  StringRef Blob;
1221
1.17M
  Expected<unsigned> MaybeCode = Cursor.ReadCode();
1222
1.17M
  if (!MaybeCode) {
1223
0
    Error(MaybeCode.takeError());
1224
0
    return true;
1225
0
  }
1226
1.17M
  unsigned Code = MaybeCode.get();
1227
1228
1.17M
  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1229
1.17M
  if (!MaybeRecCode) {
1230
0
    Error(MaybeRecCode.takeError());
1231
0
    return true;
1232
0
  }
1233
1.17M
  unsigned RecCode = MaybeRecCode.get();
1234
1.17M
  if (RecCode != DECL_CONTEXT_VISIBLE) {
1235
0
    Error("Expected visible lookup table block");
1236
0
    return true;
1237
0
  }
1238
1239
  // We can't safely determine the primary context yet, so delay attaching the
1240
  // lookup table until we're done with recursive deserialization.
1241
1.17M
  auto *Data = (const unsigned char*)Blob.data();
1242
1.17M
  PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1243
1.17M
  return false;
1244
1.17M
}
1245
1246
3
void ASTReader::Error(StringRef Msg) const {
1247
3
  Error(diag::err_fe_pch_malformed, Msg);
1248
3
  if (PP.getLangOpts().Modules && 
!Diags.isDiagnosticInFlight()2
&&
1249
3
      
!PP.getHeaderSearchInfo().getModuleCachePath().empty()1
) {
1250
0
    Diag(diag::note_module_cache_path)
1251
0
      << PP.getHeaderSearchInfo().getModuleCachePath();
1252
0
  }
1253
3
}
1254
1255
void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256
8
                      StringRef Arg3) const {
1257
8
  if (Diags.isDiagnosticInFlight())
1258
1
    Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1259
7
  else
1260
7
    Diag(DiagID) << Arg1 << Arg2 << Arg3;
1261
8
}
1262
1263
4
void ASTReader::Error(llvm::Error &&Err) const {
1264
4
  llvm::Error RemainingErr =
1265
4
      handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1266
4
        auto Diag = E.getDiagnostic().second;
1267
1268
        // Ideally we'd just emit it, but have to handle a possible in-flight
1269
        // diagnostic. Note that the location is currently ignored as well.
1270
4
        auto NumArgs = Diag.getStorage()->NumDiagArgs;
1271
4
        assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1272
0
        StringRef Arg1, Arg2, Arg3;
1273
4
        switch (NumArgs) {
1274
4
        case 3:
1275
4
          Arg3 = Diag.getStringArg(2);
1276
4
          LLVM_FALLTHROUGH;
1277
4
        case 2:
1278
4
          Arg2 = Diag.getStringArg(1);
1279
4
          LLVM_FALLTHROUGH;
1280
4
        case 1:
1281
4
          Arg1 = Diag.getStringArg(0);
1282
4
        }
1283
4
        Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1284
4
      });
1285
4
  if (RemainingErr)
1286
0
    Error(toString(std::move(RemainingErr)));
1287
4
}
1288
1289
//===----------------------------------------------------------------------===//
1290
// Source Manager Deserialization
1291
//===----------------------------------------------------------------------===//
1292
1293
/// Read the line table in the source manager block.
1294
17.9k
void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1295
17.9k
  unsigned Idx = 0;
1296
17.9k
  LineTableInfo &LineTable = SourceMgr.getLineTable();
1297
1298
  // Parse the file names
1299
17.9k
  std::map<int, int> FileIDs;
1300
17.9k
  FileIDs[-1] = -1; // For unspecified filenames.
1301
561k
  for (unsigned I = 0; Record[Idx]; 
++I543k
) {
1302
    // Extract the file name
1303
543k
    auto Filename = ReadPath(F, Record, Idx);
1304
543k
    FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1305
543k
  }
1306
17.9k
  ++Idx;
1307
1308
  // Parse the line entries
1309
17.9k
  std::vector<LineEntry> Entries;
1310
544k
  while (Idx < Record.size()) {
1311
526k
    int FID = Record[Idx++];
1312
526k
    assert(FID >= 0 && "Serialized line entries for non-local file.");
1313
    // Remap FileID from 1-based old view.
1314
0
    FID += F.SLocEntryBaseID - 1;
1315
1316
    // Extract the line entries
1317
526k
    unsigned NumEntries = Record[Idx++];
1318
526k
    assert(NumEntries && "no line entries for file ID");
1319
0
    Entries.clear();
1320
526k
    Entries.reserve(NumEntries);
1321
1.08M
    for (unsigned I = 0; I != NumEntries; 
++I562k
) {
1322
562k
      unsigned FileOffset = Record[Idx++];
1323
562k
      unsigned LineNo = Record[Idx++];
1324
562k
      int FilenameID = FileIDs[Record[Idx++]];
1325
562k
      SrcMgr::CharacteristicKind FileKind
1326
562k
        = (SrcMgr::CharacteristicKind)Record[Idx++];
1327
562k
      unsigned IncludeOffset = Record[Idx++];
1328
562k
      Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1329
562k
                                       FileKind, IncludeOffset));
1330
562k
    }
1331
526k
    LineTable.AddEntry(FileID::get(FID), Entries);
1332
526k
  }
1333
17.9k
}
1334
1335
/// Read a source manager block
1336
17.9k
llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1337
17.9k
  using namespace SrcMgr;
1338
1339
17.9k
  BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1340
1341
  // Set the source-location entry cursor to the current position in
1342
  // the stream. This cursor will be used to read the contents of the
1343
  // source manager block initially, and then lazily read
1344
  // source-location entries as needed.
1345
17.9k
  SLocEntryCursor = F.Stream;
1346
1347
  // The stream itself is going to skip over the source manager block.
1348
17.9k
  if (llvm::Error Err = F.Stream.SkipBlock())
1349
0
    return Err;
1350
1351
  // Enter the source manager block.
1352
17.9k
  if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1353
0
    return Err;
1354
17.9k
  F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1355
1356
17.9k
  RecordData Record;
1357
17.9k
  while (true) {
1358
17.9k
    Expected<llvm::BitstreamEntry> MaybeE =
1359
17.9k
        SLocEntryCursor.advanceSkippingSubblocks();
1360
17.9k
    if (!MaybeE)
1361
0
      return MaybeE.takeError();
1362
17.9k
    llvm::BitstreamEntry E = MaybeE.get();
1363
1364
17.9k
    switch (E.Kind) {
1365
0
    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1366
0
    case llvm::BitstreamEntry::Error:
1367
0
      return llvm::createStringError(std::errc::illegal_byte_sequence,
1368
0
                                     "malformed block record in AST file");
1369
0
    case llvm::BitstreamEntry::EndBlock:
1370
0
      return llvm::Error::success();
1371
17.9k
    case llvm::BitstreamEntry::Record:
1372
      // The interesting case.
1373
17.9k
      break;
1374
17.9k
    }
1375
1376
    // Read a record.
1377
17.9k
    Record.clear();
1378
17.9k
    StringRef Blob;
1379
17.9k
    Expected<unsigned> MaybeRecord =
1380
17.9k
        SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1381
17.9k
    if (!MaybeRecord)
1382
0
      return MaybeRecord.takeError();
1383
17.9k
    switch (MaybeRecord.get()) {
1384
0
    default:  // Default behavior: ignore.
1385
0
      break;
1386
1387
17.9k
    case SM_SLOC_FILE_ENTRY:
1388
17.9k
    case SM_SLOC_BUFFER_ENTRY:
1389
17.9k
    case SM_SLOC_EXPANSION_ENTRY:
1390
      // Once we hit one of the source location entries, we're done.
1391
17.9k
      return llvm::Error::success();
1392
17.9k
    }
1393
17.9k
  }
1394
17.9k
}
1395
1396
/// If a header file is not found at the path that we expect it to be
1397
/// and the PCH file was moved from its original location, try to resolve the
1398
/// file by assuming that header+PCH were moved together and the header is in
1399
/// the same place relative to the PCH.
1400
static std::string
1401
resolveFileRelativeToOriginalDir(const std::string &Filename,
1402
                                 const std::string &OriginalDir,
1403
230
                                 const std::string &CurrDir) {
1404
230
  assert(OriginalDir != CurrDir &&
1405
230
         "No point trying to resolve the file if the PCH dir didn't change");
1406
1407
0
  using namespace llvm::sys;
1408
1409
230
  SmallString<128> filePath(Filename);
1410
230
  fs::make_absolute(filePath);
1411
230
  assert(path::is_absolute(OriginalDir));
1412
0
  SmallString<128> currPCHPath(CurrDir);
1413
1414
230
  path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1415
230
                       fileDirE = path::end(path::parent_path(filePath));
1416
230
  path::const_iterator origDirI = path::begin(OriginalDir),
1417
230
                       origDirE = path::end(OriginalDir);
1418
  // Skip the common path components from filePath and OriginalDir.
1419
615
  while (fileDirI != fileDirE && origDirI != origDirE &&
1420
615
         *fileDirI == *origDirI) {
1421
385
    ++fileDirI;
1422
385
    ++origDirI;
1423
385
  }
1424
2.34k
  for (; origDirI != origDirE; 
++origDirI2.11k
)
1425
2.11k
    path::append(currPCHPath, "..");
1426
230
  path::append(currPCHPath, fileDirI, fileDirE);
1427
230
  path::append(currPCHPath, path::filename(Filename));
1428
230
  return std::string(currPCHPath.str());
1429
230
}
1430
1431
9.56M
bool ASTReader::ReadSLocEntry(int ID) {
1432
9.56M
  if (ID == 0)
1433
0
    return false;
1434
1435
9.56M
  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1436
0
    Error("source location entry ID out-of-range for AST file");
1437
0
    return true;
1438
0
  }
1439
1440
  // Local helper to read the (possibly-compressed) buffer data following the
1441
  // entry record.
1442
9.56M
  auto ReadBuffer = [this](
1443
9.56M
      BitstreamCursor &SLocEntryCursor,
1444
9.56M
      StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1445
31.9k
    RecordData Record;
1446
31.9k
    StringRef Blob;
1447
31.9k
    Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1448
31.9k
    if (!MaybeCode) {
1449
0
      Error(MaybeCode.takeError());
1450
0
      return nullptr;
1451
0
    }
1452
31.9k
    unsigned Code = MaybeCode.get();
1453
1454
31.9k
    Expected<unsigned> MaybeRecCode =
1455
31.9k
        SLocEntryCursor.readRecord(Code, Record, &Blob);
1456
31.9k
    if (!MaybeRecCode) {
1457
0
      Error(MaybeRecCode.takeError());
1458
0
      return nullptr;
1459
0
    }
1460
31.9k
    unsigned RecCode = MaybeRecCode.get();
1461
1462
31.9k
    if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1463
31.9k
      if (!llvm::zlib::isAvailable()) {
1464
0
        Error("zlib is not available");
1465
0
        return nullptr;
1466
0
      }
1467
31.9k
      SmallString<0> Uncompressed;
1468
31.9k
      if (llvm::Error E =
1469
31.9k
              llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1470
0
        Error("could not decompress embedded file contents: " +
1471
0
              llvm::toString(std::move(E)));
1472
0
        return nullptr;
1473
0
      }
1474
31.9k
      return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1475
31.9k
    } else 
if (0
RecCode == SM_SLOC_BUFFER_BLOB0
) {
1476
0
      return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1477
0
    } else {
1478
0
      Error("AST record has invalid code");
1479
0
      return nullptr;
1480
0
    }
1481
31.9k
  };
1482
1483
9.56M
  ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1484
9.56M
  if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1485
9.56M
          F->SLocEntryOffsetsBase +
1486
9.56M
          F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1487
0
    Error(std::move(Err));
1488
0
    return true;
1489
0
  }
1490
1491
9.56M
  BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1492
9.56M
  SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1493
1494
9.56M
  ++NumSLocEntriesRead;
1495
9.56M
  Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1496
9.56M
  if (!MaybeEntry) {
1497
0
    Error(MaybeEntry.takeError());
1498
0
    return true;
1499
0
  }
1500
9.56M
  llvm::BitstreamEntry Entry = MaybeEntry.get();
1501
1502
9.56M
  if (Entry.Kind != llvm::BitstreamEntry::Record) {
1503
0
    Error("incorrectly-formatted source location entry in AST file");
1504
0
    return true;
1505
0
  }
1506
1507
9.56M
  RecordData Record;
1508
9.56M
  StringRef Blob;
1509
9.56M
  Expected<unsigned> MaybeSLOC =
1510
9.56M
      SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1511
9.56M
  if (!MaybeSLOC) {
1512
0
    Error(MaybeSLOC.takeError());
1513
0
    return true;
1514
0
  }
1515
9.56M
  switch (MaybeSLOC.get()) {
1516
0
  default:
1517
0
    Error("incorrectly-formatted source location entry in AST file");
1518
0
    return true;
1519
1520
635k
  case SM_SLOC_FILE_ENTRY: {
1521
    // We will detect whether a file changed and return 'Failure' for it, but
1522
    // we will also try to fail gracefully by setting up the SLocEntry.
1523
635k
    unsigned InputID = Record[4];
1524
635k
    InputFile IF = getInputFile(*F, InputID);
1525
635k
    Optional<FileEntryRef> File = IF.getFile();
1526
635k
    bool OverriddenBuffer = IF.isOverridden();
1527
1528
    // Note that we only check if a File was returned. If it was out-of-date
1529
    // we have complained but we will continue creating a FileID to recover
1530
    // gracefully.
1531
635k
    if (!File)
1532
94
      return true;
1533
1534
635k
    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1535
635k
    if (IncludeLoc.isInvalid() && 
F->Kind != MK_MainFile20.9k
) {
1536
      // This is the module's main file.
1537
20.8k
      IncludeLoc = getImportLocation(F);
1538
20.8k
    }
1539
635k
    SrcMgr::CharacteristicKind
1540
635k
      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1541
635k
    FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1542
635k
                                        BaseOffset + Record[0]);
1543
635k
    SrcMgr::FileInfo &FileInfo =
1544
635k
          const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1545
635k
    FileInfo.NumCreatedFIDs = Record[5];
1546
635k
    if (Record[3])
1547
124k
      FileInfo.setHasLineDirectives();
1548
1549
635k
    unsigned NumFileDecls = Record[7];
1550
635k
    if (NumFileDecls && 
ContextObj610k
) {
1551
610k
      const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1552
610k
      assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1553
0
      FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1554
610k
                                                             NumFileDecls));
1555
610k
    }
1556
1557
0
    const SrcMgr::ContentCache &ContentCache =
1558
635k
        SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1559
635k
    if (OverriddenBuffer && 
!ContentCache.BufferOverridden304
&&
1560
635k
        
ContentCache.ContentsEntry == ContentCache.OrigEntry92
&&
1561
635k
        
!ContentCache.getBufferIfLoaded()92
) {
1562
90
      auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1563
90
      if (!Buffer)
1564
0
        return true;
1565
90
      SourceMgr.overrideFileContents(*File, std::move(Buffer));
1566
90
    }
1567
1568
635k
    break;
1569
635k
  }
1570
1571
635k
  case SM_SLOC_BUFFER_ENTRY: {
1572
31.8k
    const char *Name = Blob.data();
1573
31.8k
    unsigned Offset = Record[0];
1574
31.8k
    SrcMgr::CharacteristicKind
1575
31.8k
      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1576
31.8k
    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1577
31.8k
    if (IncludeLoc.isInvalid() && F->isModule()) {
1578
27.8k
      IncludeLoc = getImportLocation(F);
1579
27.8k
    }
1580
1581
31.8k
    auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1582
31.8k
    if (!Buffer)
1583
0
      return true;
1584
31.8k
    SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1585
31.8k
                           BaseOffset + Offset, IncludeLoc);
1586
31.8k
    break;
1587
31.8k
  }
1588
1589
8.89M
  case SM_SLOC_EXPANSION_ENTRY: {
1590
8.89M
    SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1591
8.89M
    SourceMgr.createExpansionLoc(SpellingLoc,
1592
8.89M
                                     ReadSourceLocation(*F, Record[2]),
1593
8.89M
                                     ReadSourceLocation(*F, Record[3]),
1594
8.89M
                                     Record[5],
1595
8.89M
                                     Record[4],
1596
8.89M
                                     ID,
1597
8.89M
                                     BaseOffset + Record[0]);
1598
8.89M
    break;
1599
31.8k
  }
1600
9.56M
  }
1601
1602
9.56M
  return false;
1603
9.56M
}
1604
1605
42
std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1606
42
  if (ID == 0)
1607
0
    return std::make_pair(SourceLocation(), "");
1608
1609
42
  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1610
0
    Error("source location entry ID out-of-range for AST file");
1611
0
    return std::make_pair(SourceLocation(), "");
1612
0
  }
1613
1614
  // Find which module file this entry lands in.
1615
42
  ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1616
42
  if (!M->isModule())
1617
14
    return std::make_pair(SourceLocation(), "");
1618
1619
  // FIXME: Can we map this down to a particular submodule? That would be
1620
  // ideal.
1621
28
  return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1622
42
}
1623
1624
/// Find the location where the module F is imported.
1625
48.7k
SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1626
48.7k
  if (F->ImportLoc.isValid())
1627
44.3k
    return F->ImportLoc;
1628
1629
  // Otherwise we have a PCH. It's considered to be "imported" at the first
1630
  // location of its includer.
1631
4.37k
  if (F->ImportedBy.empty() || 
!F->ImportedBy[0]1.25k
) {
1632
    // Main file is the importer.
1633
3.12k
    assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1634
0
    return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1635
3.12k
  }
1636
1.25k
  return F->ImportedBy[0]->FirstLoc;
1637
4.37k
}
1638
1639
/// Enter a subblock of the specified BlockID with the specified cursor. Read
1640
/// the abbreviations that are at the top of the block and then leave the cursor
1641
/// pointing into the block.
1642
llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1643
                                        unsigned BlockID,
1644
72.6k
                                        uint64_t *StartOfBlockOffset) {
1645
72.6k
  if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1646
0
    return Err;
1647
1648
72.6k
  if (StartOfBlockOffset)
1649
17.9k
    *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1650
1651
396k
  while (true) {
1652
396k
    uint64_t Offset = Cursor.GetCurrentBitNo();
1653
396k
    Expected<unsigned> MaybeCode = Cursor.ReadCode();
1654
396k
    if (!MaybeCode)
1655
0
      return MaybeCode.takeError();
1656
396k
    unsigned Code = MaybeCode.get();
1657
1658
    // We expect all abbrevs to be at the start of the block.
1659
396k
    if (Code != llvm::bitc::DEFINE_ABBREV) {
1660
72.6k
      if (llvm::Error Err = Cursor.JumpToBit(Offset))
1661
0
        return Err;
1662
72.6k
      return llvm::Error::success();
1663
72.6k
    }
1664
324k
    if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1665
0
      return Err;
1666
324k
  }
1667
72.6k
}
1668
1669
Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1670
10.9M
                           unsigned &Idx) {
1671
10.9M
  Token Tok;
1672
10.9M
  Tok.startToken();
1673
10.9M
  Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1674
10.9M
  Tok.setLength(Record[Idx++]);
1675
10.9M
  if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1676
3.00M
    Tok.setIdentifierInfo(II);
1677
10.9M
  Tok.setKind((tok::TokenKind)Record[Idx++]);
1678
10.9M
  Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1679
10.9M
  return Tok;
1680
10.9M
}
1681
1682
1.72M
MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1683
1.72M
  BitstreamCursor &Stream = F.MacroCursor;
1684
1685
  // Keep track of where we are in the stream, then jump back there
1686
  // after reading this macro.
1687
1.72M
  SavedStreamPosition SavedPosition(Stream);
1688
1689
1.72M
  if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1690
    // FIXME this drops errors on the floor.
1691
0
    consumeError(std::move(Err));
1692
0
    return nullptr;
1693
0
  }
1694
1.72M
  RecordData Record;
1695
1.72M
  SmallVector<IdentifierInfo*, 16> MacroParams;
1696
1.72M
  MacroInfo *Macro = nullptr;
1697
1698
14.4M
  while (true) {
1699
    // Advance to the next record, but if we get to the end of the block, don't
1700
    // pop it (removing all the abbreviations from the cursor) since we want to
1701
    // be able to reseek within the block and read entries.
1702
14.4M
    unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1703
14.4M
    Expected<llvm::BitstreamEntry> MaybeEntry =
1704
14.4M
        Stream.advanceSkippingSubblocks(Flags);
1705
14.4M
    if (!MaybeEntry) {
1706
0
      Error(MaybeEntry.takeError());
1707
0
      return Macro;
1708
0
    }
1709
14.4M
    llvm::BitstreamEntry Entry = MaybeEntry.get();
1710
1711
14.4M
    switch (Entry.Kind) {
1712
0
    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1713
0
    case llvm::BitstreamEntry::Error:
1714
0
      Error("malformed block record in AST file");
1715
0
      return Macro;
1716
2.43k
    case llvm::BitstreamEntry::EndBlock:
1717
2.43k
      return Macro;
1718
14.4M
    case llvm::BitstreamEntry::Record:
1719
      // The interesting case.
1720
14.4M
      break;
1721
14.4M
    }
1722
1723
    // Read a record.
1724
14.4M
    Record.clear();
1725
14.4M
    PreprocessorRecordTypes RecType;
1726
14.4M
    if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1727
14.4M
      RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1728
0
    else {
1729
0
      Error(MaybeRecType.takeError());
1730
0
      return Macro;
1731
0
    }
1732
14.4M
    switch (RecType) {
1733
0
    case PP_MODULE_MACRO:
1734
0
    case PP_MACRO_DIRECTIVE_HISTORY:
1735
0
      return Macro;
1736
1737
2.85M
    case PP_MACRO_OBJECT_LIKE:
1738
3.45M
    case PP_MACRO_FUNCTION_LIKE: {
1739
      // If we already have a macro, that means that we've hit the end
1740
      // of the definition of the macro we were looking for. We're
1741
      // done.
1742
3.45M
      if (Macro)
1743
1.72M
        return Macro;
1744
1745
1.72M
      unsigned NextIndex = 1; // Skip identifier ID.
1746
1.72M
      SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1747
1.72M
      MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1748
1.72M
      MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1749
1.72M
      MI->setIsUsed(Record[NextIndex++]);
1750
1.72M
      MI->setUsedForHeaderGuard(Record[NextIndex++]);
1751
1752
1.72M
      if (RecType == PP_MACRO_FUNCTION_LIKE) {
1753
        // Decode function-like macro info.
1754
299k
        bool isC99VarArgs = Record[NextIndex++];
1755
299k
        bool isGNUVarArgs = Record[NextIndex++];
1756
299k
        bool hasCommaPasting = Record[NextIndex++];
1757
299k
        MacroParams.clear();
1758
299k
        unsigned NumArgs = Record[NextIndex++];
1759
1.01M
        for (unsigned i = 0; i != NumArgs; 
++i715k
)
1760
715k
          MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1761
1762
        // Install function-like macro info.
1763
299k
        MI->setIsFunctionLike();
1764
299k
        if (isC99VarArgs) 
MI->setIsC99Varargs()9.71k
;
1765
299k
        if (isGNUVarArgs) 
MI->setIsGNUVarargs()67
;
1766
299k
        if (hasCommaPasting) 
MI->setHasCommaPasting()204
;
1767
299k
        MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1768
299k
      }
1769
1770
      // Remember that we saw this macro last so that we add the tokens that
1771
      // form its body to it.
1772
1.72M
      Macro = MI;
1773
1774
1.72M
      if (NextIndex + 1 == Record.size() && 
PP.getPreprocessingRecord()40.6k
&&
1775
1.72M
          
Record[NextIndex]40.6k
) {
1776
        // We have a macro definition. Register the association
1777
40.6k
        PreprocessedEntityID
1778
40.6k
            GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1779
40.6k
        PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1780
40.6k
        PreprocessingRecord::PPEntityID PPID =
1781
40.6k
            PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1782
40.6k
        MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1783
40.6k
            PPRec.getPreprocessedEntity(PPID));
1784
40.6k
        if (PPDef)
1785
40.6k
          PPRec.RegisterMacroDefinition(Macro, PPDef);
1786
40.6k
      }
1787
1788
1.72M
      ++NumMacrosRead;
1789
1.72M
      break;
1790
3.45M
    }
1791
1792
10.9M
    case PP_TOKEN: {
1793
      // If we see a TOKEN before a PP_MACRO_*, then the file is
1794
      // erroneous, just pretend we didn't see this.
1795
10.9M
      if (!Macro) 
break0
;
1796
1797
10.9M
      unsigned Idx = 0;
1798
10.9M
      Token Tok = ReadToken(F, Record, Idx);
1799
10.9M
      Macro->AddTokenToBody(Tok);
1800
10.9M
      break;
1801
10.9M
    }
1802
14.4M
    }
1803
14.4M
  }
1804
1.72M
}
1805
1806
PreprocessedEntityID
1807
ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1808
40.6k
                                         unsigned LocalID) const {
1809
40.6k
  if (!M.ModuleOffsetMap.empty())
1810
0
    ReadModuleOffsetMap(M);
1811
1812
40.6k
  ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1813
40.6k
    I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1814
40.6k
  assert(I != M.PreprocessedEntityRemap.end()
1815
40.6k
         && "Invalid index into preprocessed entity index remap");
1816
1817
0
  return LocalID + I->second;
1818
40.6k
}
1819
1820
3.05M
unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1821
3.05M
  return llvm::hash_combine(ikey.Size, ikey.ModTime);
1822
3.05M
}
1823
1824
HeaderFileInfoTrait::internal_key_type
1825
3.05M
HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1826
3.05M
  internal_key_type ikey = {FE->getSize(),
1827
3.05M
                            M.HasTimestamps ? 
FE->getModificationTime()2.99M
:
055.5k
,
1828
3.05M
                            FE->getName(), /*Imported*/ false};
1829
3.05M
  return ikey;
1830
3.05M
}
1831
1832
133k
bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1833
133k
  if (a.Size != b.Size || (a.ModTime && 
b.ModTime116k
&&
a.ModTime != b.ModTime116k
))
1834
0
    return false;
1835
1836
133k
  if (llvm::sys::path::is_absolute(a.Filename) && 
a.Filename == b.Filename34.3k
)
1837
4.73k
    return true;
1838
1839
  // Determine whether the actual files are equivalent.
1840
129k
  FileManager &FileMgr = Reader.getFileManager();
1841
258k
  auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1842
258k
    if (!Key.Imported) {
1843
129k
      if (auto File = FileMgr.getFile(Key.Filename))
1844
129k
        return *File;
1845
0
      return nullptr;
1846
129k
    }
1847
1848
129k
    std::string Resolved = std::string(Key.Filename);
1849
129k
    Reader.ResolveImportedPath(M, Resolved);
1850
129k
    if (auto File = FileMgr.getFile(Resolved))
1851
129k
      return *File;
1852
0
    return nullptr;
1853
129k
  };
1854
1855
129k
  const FileEntry *FEA = GetFile(a);
1856
129k
  const FileEntry *FEB = GetFile(b);
1857
129k
  return 
FEA129k
&& FEA == FEB;
1858
133k
}
1859
1860
std::pair<unsigned, unsigned>
1861
2.62M
HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1862
2.62M
  return readULEBKeyDataLength(d);
1863
2.62M
}
1864
1865
HeaderFileInfoTrait::internal_key_type
1866
133k
HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1867
133k
  using namespace llvm::support;
1868
1869
133k
  internal_key_type ikey;
1870
133k
  ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1871
133k
  ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1872
133k
  ikey.Filename = (const char *)d;
1873
133k
  ikey.Imported = true;
1874
133k
  return ikey;
1875
133k
}
1876
1877
HeaderFileInfoTrait::data_type
1878
HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1879
86.4k
                              unsigned DataLen) {
1880
86.4k
  using namespace llvm::support;
1881
1882
86.4k
  const unsigned char *End = d + DataLen;
1883
86.4k
  HeaderFileInfo HFI;
1884
86.4k
  unsigned Flags = *d++;
1885
  // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1886
86.4k
  HFI.isImport |= (Flags >> 5) & 0x01;
1887
86.4k
  HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1888
86.4k
  HFI.DirInfo = (Flags >> 1) & 0x07;
1889
86.4k
  HFI.IndexHeaderMapHeader = Flags & 0x01;
1890
  // FIXME: Find a better way to handle this. Maybe just store a
1891
  // "has been included" flag?
1892
86.4k
  HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1893
86.4k
                             HFI.NumIncludes);
1894
86.4k
  HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1895
86.4k
      M, endian::readNext<uint32_t, little, unaligned>(d));
1896
86.4k
  if (unsigned FrameworkOffset =
1897
86.4k
          endian::readNext<uint32_t, little, unaligned>(d)) {
1898
    // The framework offset is 1 greater than the actual offset,
1899
    // since 0 is used as an indicator for "no framework name".
1900
0
    StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1901
0
    HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1902
0
  }
1903
1904
86.4k
  assert((End - d) % 4 == 0 &&
1905
86.4k
         "Wrong data length in HeaderFileInfo deserialization");
1906
169k
  while (d != End) {
1907
82.9k
    uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1908
82.9k
    auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1909
82.9k
    LocalSMID >>= 2;
1910
1911
    // This header is part of a module. Associate it with the module to enable
1912
    // implicit module import.
1913
82.9k
    SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1914
82.9k
    Module *Mod = Reader.getSubmodule(GlobalSMID);
1915
82.9k
    FileManager &FileMgr = Reader.getFileManager();
1916
82.9k
    ModuleMap &ModMap =
1917
82.9k
        Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1918
1919
82.9k
    std::string Filename = std::string(key.Filename);
1920
82.9k
    if (key.Imported)
1921
82.9k
      Reader.ResolveImportedPath(M, Filename);
1922
    // FIXME: NameAsWritten
1923
82.9k
    Module::Header H = {std::string(key.Filename), "",
1924
82.9k
                        *FileMgr.getFile(Filename)};
1925
82.9k
    ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1926
82.9k
    HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1927
82.9k
  }
1928
1929
  // This HeaderFileInfo was externally loaded.
1930
86.4k
  HFI.External = true;
1931
86.4k
  HFI.IsValid = true;
1932
86.4k
  return HFI;
1933
86.4k
}
1934
1935
void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1936
1.71M
                                uint32_t MacroDirectivesOffset) {
1937
1.71M
  assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1938
0
  PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1939
1.71M
}
1940
1941
543
void ASTReader::ReadDefinedMacros() {
1942
  // Note that we are loading defined macros.
1943
543
  Deserializing Macros(this);
1944
1945
6.39k
  for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1946
6.39k
    BitstreamCursor &MacroCursor = I.MacroCursor;
1947
1948
    // If there was no preprocessor block, skip this file.
1949
6.39k
    if (MacroCursor.getBitcodeBytes().empty())
1950
0
      continue;
1951
1952
6.39k
    BitstreamCursor Cursor = MacroCursor;
1953
6.39k
    if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1954
0
      Error(std::move(Err));
1955
0
      return;
1956
0
    }
1957
1958
6.39k
    RecordData Record;
1959
62.6M
    while (true) {
1960
62.6M
      Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1961
62.6M
      if (!MaybeE) {
1962
0
        Error(MaybeE.takeError());
1963
0
        return;
1964
0
      }
1965
62.6M
      llvm::BitstreamEntry E = MaybeE.get();
1966
1967
62.6M
      switch (E.Kind) {
1968
0
      case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1969
0
      case llvm::BitstreamEntry::Error:
1970
0
        Error("malformed block record in AST file");
1971
0
        return;
1972
6.39k
      case llvm::BitstreamEntry::EndBlock:
1973
6.39k
        goto NextCursor;
1974
1975
62.6M
      case llvm::BitstreamEntry::Record: {
1976
62.6M
        Record.clear();
1977
62.6M
        Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1978
62.6M
        if (!MaybeRecord) {
1979
0
          Error(MaybeRecord.takeError());
1980
0
          return;
1981
0
        }
1982
62.6M
        switch (MaybeRecord.get()) {
1983
13.2M
        default:  // Default behavior: ignore.
1984
13.2M
          break;
1985
1986
13.2M
        case PP_MACRO_OBJECT_LIKE:
1987
6.62M
        case PP_MACRO_FUNCTION_LIKE: {
1988
6.62M
          IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1989
6.62M
          if (II->isOutOfDate())
1990
1.65M
            updateOutOfDateIdentifier(*II);
1991
6.62M
          break;
1992
5.46M
        }
1993
1994
42.8M
        case PP_TOKEN:
1995
          // Ignore tokens.
1996
42.8M
          break;
1997
62.6M
        }
1998
62.6M
        break;
1999
62.6M
      }
2000
62.6M
      }
2001
62.6M
    }
2002
6.39k
    NextCursor:  ;
2003
6.39k
  }
2004
543
}
2005
2006
namespace {
2007
2008
  /// Visitor class used to look up identifirs in an AST file.
2009
  class IdentifierLookupVisitor {
2010
    StringRef Name;
2011
    unsigned NameHash;
2012
    unsigned PriorGeneration;
2013
    unsigned &NumIdentifierLookups;
2014
    unsigned &NumIdentifierLookupHits;
2015
    IdentifierInfo *Found = nullptr;
2016
2017
  public:
2018
    IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2019
                            unsigned &NumIdentifierLookups,
2020
                            unsigned &NumIdentifierLookupHits)
2021
      : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2022
        PriorGeneration(PriorGeneration),
2023
        NumIdentifierLookups(NumIdentifierLookups),
2024
11.2M
        NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2025
2026
29.8M
    bool operator()(ModuleFile &M) {
2027
      // If we've already searched this module file, skip it now.
2028
29.8M
      if (M.Generation <= PriorGeneration)
2029
25.1k
        return true;
2030
2031
29.8M
      ASTIdentifierLookupTable *IdTable
2032
29.8M
        = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2033
29.8M
      if (!IdTable)
2034
0
        return false;
2035
2036
29.8M
      ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2037
29.8M
                                     Found);
2038
29.8M
      ++NumIdentifierLookups;
2039
29.8M
      ASTIdentifierLookupTable::iterator Pos =
2040
29.8M
          IdTable->find_hashed(Name, NameHash, &Trait);
2041
29.8M
      if (Pos == IdTable->end())
2042
27.3M
        return false;
2043
2044
      // Dereferencing the iterator has the effect of building the
2045
      // IdentifierInfo node and populating it with the various
2046
      // declarations it needs.
2047
2.44M
      ++NumIdentifierLookupHits;
2048
2.44M
      Found = *Pos;
2049
2.44M
      return true;
2050
29.8M
    }
2051
2052
    // Retrieve the identifier info found within the module
2053
    // files.
2054
8.15M
    IdentifierInfo *getIdentifierInfo() const { return Found; }
2055
  };
2056
2057
} // namespace
2058
2059
3.08M
void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2060
  // Note that we are loading an identifier.
2061
3.08M
  Deserializing AnIdentifier(this);
2062
2063
3.08M
  unsigned PriorGeneration = 0;
2064
3.08M
  if (getContext().getLangOpts().Modules)
2065
3.06M
    PriorGeneration = IdentifierGeneration[&II];
2066
2067
  // If there is a global index, look there first to determine which modules
2068
  // provably do not have any results for this identifier.
2069
3.08M
  GlobalModuleIndex::HitSet Hits;
2070
3.08M
  GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2071
3.08M
  if (!loadGlobalIndex()) {
2072
302k
    if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2073
38.2k
      HitsPtr = &Hits;
2074
38.2k
    }
2075
302k
  }
2076
2077
3.08M
  IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2078
3.08M
                                  NumIdentifierLookups,
2079
3.08M
                                  NumIdentifierLookupHits);
2080
3.08M
  ModuleMgr.visit(Visitor, HitsPtr);
2081
3.08M
  markIdentifierUpToDate(&II);
2082
3.08M
}
2083
2084
13.6M
void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2085
13.6M
  if (!II)
2086
7.44M
    return;
2087
2088
6.24M
  II->setOutOfDate(false);
2089
2090
  // Update the generation for this identifier.
2091
6.24M
  if (getContext().getLangOpts().Modules)
2092
5.60M
    IdentifierGeneration[II] = getGeneration();
2093
6.24M
}
2094
2095
void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2096
1.71M
                                    const PendingMacroInfo &PMInfo) {
2097
1.71M
  ModuleFile &M = *PMInfo.M;
2098
2099
1.71M
  BitstreamCursor &Cursor = M.MacroCursor;
2100
1.71M
  SavedStreamPosition SavedPosition(Cursor);
2101
1.71M
  if (llvm::Error Err =
2102
1.71M
          Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2103
0
    Error(std::move(Err));
2104
0
    return;
2105
0
  }
2106
2107
1.71M
  struct ModuleMacroRecord {
2108
1.71M
    SubmoduleID SubModID;
2109
1.71M
    MacroInfo *MI;
2110
1.71M
    SmallVector<SubmoduleID, 8> Overrides;
2111
1.71M
  };
2112
1.71M
  llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2113
2114
  // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2115
  // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2116
  // macro histroy.
2117
1.71M
  RecordData Record;
2118
3.40M
  while (true) {
2119
3.40M
    Expected<llvm::BitstreamEntry> MaybeEntry =
2120
3.40M
        Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2121
3.40M
    if (!MaybeEntry) {
2122
0
      Error(MaybeEntry.takeError());
2123
0
      return;
2124
0
    }
2125
3.40M
    llvm::BitstreamEntry Entry = MaybeEntry.get();
2126
2127
3.40M
    if (Entry.Kind != llvm::BitstreamEntry::Record) {
2128
0
      Error("malformed block record in AST file");
2129
0
      return;
2130
0
    }
2131
2132
3.40M
    Record.clear();
2133
3.40M
    Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2134
3.40M
    if (!MaybePP) {
2135
0
      Error(MaybePP.takeError());
2136
0
      return;
2137
0
    }
2138
3.40M
    switch ((PreprocessorRecordTypes)MaybePP.get()) {
2139
1.71M
    case PP_MACRO_DIRECTIVE_HISTORY:
2140
1.71M
      break;
2141
2142
1.68M
    case PP_MODULE_MACRO: {
2143
1.68M
      ModuleMacros.push_back(ModuleMacroRecord());
2144
1.68M
      auto &Info = ModuleMacros.back();
2145
1.68M
      Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2146
1.68M
      Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2147
1.68M
      for (int I = 2, N = Record.size(); I != N; 
++I6.60k
)
2148
6.60k
        Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2149
1.68M
      continue;
2150
0
    }
2151
2152
0
    default:
2153
0
      Error("malformed block record in AST file");
2154
0
      return;
2155
3.40M
    }
2156
2157
    // We found the macro directive history; that's the last record
2158
    // for this macro.
2159
1.71M
    break;
2160
3.40M
  }
2161
2162
  // Module macros are listed in reverse dependency order.
2163
1.71M
  {
2164
1.71M
    std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2165
1.71M
    llvm::SmallVector<ModuleMacro*, 8> Overrides;
2166
1.71M
    for (auto &MMR : ModuleMacros) {
2167
1.68M
      Overrides.clear();
2168
1.68M
      for (unsigned ModID : MMR.Overrides) {
2169
6.60k
        Module *Mod = getSubmodule(ModID);
2170
6.60k
        auto *Macro = PP.getModuleMacro(Mod, II);
2171
6.60k
        assert(Macro && "missing definition for overridden macro");
2172
0
        Overrides.push_back(Macro);
2173
6.60k
      }
2174
2175
1.68M
      bool Inserted = false;
2176
1.68M
      Module *Owner = getSubmodule(MMR.SubModID);
2177
1.68M
      PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2178
1.68M
    }
2179
1.71M
  }
2180
2181
  // Don't read the directive history for a module; we don't have anywhere
2182
  // to put it.
2183
1.71M
  if (M.isModule())
2184
1.67M
    return;
2185
2186
  // Deserialize the macro directives history in reverse source-order.
2187
45.1k
  MacroDirective *Latest = nullptr, *Earliest = nullptr;
2188
45.1k
  unsigned Idx = 0, N = Record.size();
2189
90.2k
  while (Idx < N) {
2190
45.1k
    MacroDirective *MD = nullptr;
2191
45.1k
    SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2192
45.1k
    MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2193
45.1k
    switch (K) {
2194
45.1k
    case MacroDirective::MD_Define: {
2195
45.1k
      MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2196
45.1k
      MD = PP.AllocateDefMacroDirective(MI, Loc);
2197
45.1k
      break;
2198
0
    }
2199
31
    case MacroDirective::MD_Undefine:
2200
31
      MD = PP.AllocateUndefMacroDirective(Loc);
2201
31
      break;
2202
4
    case MacroDirective::MD_Visibility:
2203
4
      bool isPublic = Record[Idx++];
2204
4
      MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2205
4
      break;
2206
45.1k
    }
2207
2208
45.1k
    if (!Latest)
2209
45.1k
      Latest = MD;
2210
45.1k
    if (Earliest)
2211
46
      Earliest->setPrevious(MD);
2212
45.1k
    Earliest = MD;
2213
45.1k
  }
2214
2215
45.1k
  if (Latest)
2216
45.1k
    PP.setLoadedMacroDirective(II, Earliest, Latest);
2217
45.1k
}
2218
2219
bool ASTReader::shouldDisableValidationForFile(
2220
989k
    const serialization::ModuleFile &M) const {
2221
989k
  if (DisableValidationKind == DisableValidationForModuleKind::None)
2222
987k
    return false;
2223
2224
  // If a PCH is loaded and validation is disabled for PCH then disable
2225
  // validation for the PCH and the modules it loads.
2226
1.53k
  ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2227
2228
1.53k
  switch (K) {
2229
0
  case MK_MainFile:
2230
1.09k
  case MK_Preamble:
2231
1.48k
  case MK_PCH:
2232
1.48k
    return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2233
56
  case MK_ImplicitModule:
2234
58
  case MK_ExplicitModule:
2235
58
  case MK_PrebuiltModule:
2236
58
    return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2237
1.53k
  }
2238
2239
0
  return false;
2240
1.53k
}
2241
2242
ASTReader::InputFileInfo
2243
1.46M
ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2244
  // Go find this input file.
2245
1.46M
  BitstreamCursor &Cursor = F.InputFilesCursor;
2246
1.46M
  SavedStreamPosition SavedPosition(Cursor);
2247
1.46M
  if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2248
    // FIXME this drops errors on the floor.
2249
0
    consumeError(std::move(Err));
2250
0
  }
2251
2252
1.46M
  Expected<unsigned> MaybeCode = Cursor.ReadCode();
2253
1.46M
  if (!MaybeCode) {
2254
    // FIXME this drops errors on the floor.
2255
0
    consumeError(MaybeCode.takeError());
2256
0
  }
2257
1.46M
  unsigned Code = MaybeCode.get();
2258
1.46M
  RecordData Record;
2259
1.46M
  StringRef Blob;
2260
2261
1.46M
  if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2262
1.46M
    assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2263
1.46M
           "invalid record type for input file");
2264
0
  else {
2265
    // FIXME this drops errors on the floor.
2266
0
    consumeError(Maybe.takeError());
2267
0
  }
2268
2269
0
  assert(Record[0] == ID && "Bogus stored ID or offset");
2270
0
  InputFileInfo R;
2271
1.46M
  R.StoredSize = static_cast<off_t>(Record[1]);
2272
1.46M
  R.StoredTime = static_cast<time_t>(Record[2]);
2273
1.46M
  R.Overridden = static_cast<bool>(Record[3]);
2274
1.46M
  R.Transient = static_cast<bool>(Record[4]);
2275
1.46M
  R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2276
1.46M
  R.Filename = std::string(Blob);
2277
1.46M
  ResolveImportedPath(F, R.Filename);
2278
2279
1.46M
  Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2280
1.46M
  if (!MaybeEntry) // FIXME this drops errors on the floor.
2281
0
    consumeError(MaybeEntry.takeError());
2282
1.46M
  llvm::BitstreamEntry Entry = MaybeEntry.get();
2283
1.46M
  assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2284
1.46M
         "expected record type for input file hash");
2285
2286
0
  Record.clear();
2287
1.46M
  if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2288
1.46M
    assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2289
1.46M
           "invalid record type for input file hash");
2290
0
  else {
2291
    // FIXME this drops errors on the floor.
2292
0
    consumeError(Maybe.takeError());
2293
0
  }
2294
0
  R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2295
1.46M
                  static_cast<uint64_t>(Record[0]);
2296
1.46M
  return R;
2297
1.46M
}
2298
2299
static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2300
1.43M
InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2301
  // If this ID is bogus, just return an empty input file.
2302
1.43M
  if (ID == 0 || ID > F.InputFilesLoaded.size())
2303
0
    return InputFile();
2304
2305
  // If we've already loaded this input file, return it.
2306
1.43M
  if (F.InputFilesLoaded[ID-1].getFile())
2307
481k
    return F.InputFilesLoaded[ID-1];
2308
2309
953k
  if (F.InputFilesLoaded[ID-1].isNotFound())
2310
92
    return InputFile();
2311
2312
  // Go find this input file.
2313
953k
  BitstreamCursor &Cursor = F.InputFilesCursor;
2314
953k
  SavedStreamPosition SavedPosition(Cursor);
2315
953k
  if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2316
    // FIXME this drops errors on the floor.
2317
0
    consumeError(std::move(Err));
2318
0
  }
2319
2320
953k
  InputFileInfo FI = readInputFileInfo(F, ID);
2321
953k
  off_t StoredSize = FI.StoredSize;
2322
953k
  time_t StoredTime = FI.StoredTime;
2323
953k
  bool Overridden = FI.Overridden;
2324
953k
  bool Transient = FI.Transient;
2325
953k
  StringRef Filename = FI.Filename;
2326
953k
  uint64_t StoredContentHash = FI.ContentHash;
2327
2328
953k
  OptionalFileEntryRefDegradesToFileEntryPtr File =
2329
953k
      expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2330
2331
  // If we didn't find the file, resolve it relative to the
2332
  // original directory from which this AST file was created.
2333
953k
  if (!File && 
!F.OriginalDir.empty()241
&&
!F.BaseDirectory.empty()241
&&
2334
953k
      
F.OriginalDir != F.BaseDirectory234
) {
2335
230
    std::string Resolved = resolveFileRelativeToOriginalDir(
2336
230
        std::string(Filename), F.OriginalDir, F.BaseDirectory);
2337
230
    if (!Resolved.empty())
2338
230
      File = expectedToOptional(FileMgr.getFileRef(Resolved));
2339
230
  }
2340
2341
  // For an overridden file, create a virtual file with the stored
2342
  // size/timestamp.
2343
953k
  if ((Overridden || 
Transient952k
) &&
!File597
)
2344
238
    File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2345
2346
953k
  if (!File) {
2347
3
    if (Complain) {
2348
3
      std::string ErrorStr = "could not find file '";
2349
3
      ErrorStr += Filename;
2350
3
      ErrorStr += "' referenced by AST file '";
2351
3
      ErrorStr += F.FileName;
2352
3
      ErrorStr += "'";
2353
3
      Error(ErrorStr);
2354
3
    }
2355
    // Record that we didn't find the file.
2356
3
    F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2357
3
    return InputFile();
2358
3
  }
2359
2360
  // Check if there was a request to override the contents of the file
2361
  // that was part of the precompiled header. Overriding such a file
2362
  // can lead to problems when lexing using the source locations from the
2363
  // PCH.
2364
953k
  SourceManager &SM = getSourceManager();
2365
  // FIXME: Reject if the overrides are different.
2366
953k
  if ((!Overridden && 
!Transient952k
) &&
SM.isFileOverridden(File)952k
) {
2367
1
    if (Complain)
2368
1
      Error(diag::err_fe_pch_file_overridden, Filename);
2369
2370
    // After emitting the diagnostic, bypass the overriding file to recover
2371
    // (this creates a separate FileEntry).
2372
1
    File = SM.bypassFileContentsOverride(*File);
2373
1
    if (!File) {
2374
0
      F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2375
0
      return InputFile();
2376
0
    }
2377
1
  }
2378
2379
953k
  struct Change {
2380
953k
    enum ModificationKind {
2381
953k
      Size,
2382
953k
      ModTime,
2383
953k
      Content,
2384
953k
      None,
2385
953k
    } Kind;
2386
953k
    llvm::Optional<int64_t> Old = llvm::None;
2387
953k
    llvm::Optional<int64_t> New = llvm::None;
2388
953k
  };
2389
953k
  auto HasInputFileChanged = [&]() {
2390
953k
    if (StoredSize != File->getSize())
2391
228
      return Change{Change::Size, StoredSize, File->getSize()};
2392
953k
    if (!shouldDisableValidationForFile(F) && 
StoredTime952k
&&
2393
953k
        
StoredTime != File->getModificationTime()951k
) {
2394
5
      Change MTimeChange = {Change::ModTime, StoredTime,
2395
5
                            File->getModificationTime()};
2396
2397
      // In case the modification time changes but not the content,
2398
      // accept the cached file as legit.
2399
5
      if (ValidateASTInputFilesContent &&
2400
5
          
StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))4
) {
2401
4
        auto MemBuffOrError = FileMgr.getBufferForFile(File);
2402
4
        if (!MemBuffOrError) {
2403
0
          if (!Complain)
2404
0
            return MTimeChange;
2405
0
          std::string ErrorStr = "could not get buffer for file '";
2406
0
          ErrorStr += File->getName();
2407
0
          ErrorStr += "'";
2408
0
          Error(ErrorStr);
2409
0
          return MTimeChange;
2410
0
        }
2411
2412
        // FIXME: hash_value is not guaranteed to be stable!
2413
4
        auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2414
4
        if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2415
2
          return Change{Change::None};
2416
2417
2
        return Change{Change::Content};
2418
4
      }
2419
1
      return MTimeChange;
2420
5
    }
2421
953k
    return Change{Change::None};
2422
953k
  };
2423
2424
953k
  bool IsOutOfDate = false;
2425
953k
  auto FileChange = HasInputFileChanged();
2426
  // For an overridden file, there is nothing to validate.
2427
953k
  if (!Overridden && 
FileChange.Kind != Change::None952k
) {
2428
39
    if (Complain && 
!Diags.isDiagnosticInFlight()10
) {
2429
      // Build a list of the PCH imports that got us here (in reverse).
2430
10
      SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2431
12
      while (!ImportStack.back()->ImportedBy.empty())
2432
2
        ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2433
2434
      // The top-level PCH is stale.
2435
10
      StringRef TopLevelPCHName(ImportStack.back()->FileName);
2436
10
      Diag(diag::err_fe_ast_file_modified)
2437
10
          << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2438
10
          << TopLevelPCHName << FileChange.Kind
2439
10
          << (FileChange.Old && 
FileChange.New8
)
2440
10
          << llvm::itostr(FileChange.Old.getValueOr(0))
2441
10
          << llvm::itostr(FileChange.New.getValueOr(0));
2442
2443
      // Print the import stack.
2444
10
      if (ImportStack.size() > 1) {
2445
2
        Diag(diag::note_pch_required_by)
2446
2
          << Filename << ImportStack[0]->FileName;
2447
4
        for (unsigned I = 1; I < ImportStack.size(); 
++I2
)
2448
2
          Diag(diag::note_pch_required_by)
2449
2
            << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2450
2
      }
2451
2452
10
      Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2453
10
    }
2454
2455
39
    IsOutOfDate = true;
2456
39
  }
2457
  // FIXME: If the file is overridden and we've already opened it,
2458
  // issue an error (or split it into a separate FileEntry).
2459
2460
953k
  InputFile IF = InputFile(*File, Overridden || 
Transient952k
, IsOutOfDate);
2461
2462
  // Note that we've loaded this input file.
2463
953k
  F.InputFilesLoaded[ID-1] = IF;
2464
953k
  return IF;
2465
953k
}
2466
2467
/// If we are loading a relocatable PCH or module file, and the filename
2468
/// is not an absolute path, add the system or module root to the beginning of
2469
/// the file name.
2470
2.26M
void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2471
  // Resolve relative to the base directory, if we have one.
2472
2.26M
  if (!M.BaseDirectory.empty())
2473
2.21M
    return ResolveImportedPath(Filename, M.BaseDirectory);
2474
2.26M
}
2475
2476
2.23M
void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2477
2.23M
  if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2478
173k
    return;
2479
2480
2.06M
  SmallString<128> Buffer;
2481
2.06M
  llvm::sys::path::append(Buffer, Prefix, Filename);
2482
2.06M
  Filename.assign(Buffer.begin(), Buffer.end());
2483
2.06M
}
2484
2485
23.7k
static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2486
23.7k
  switch (ARR) {
2487
5
  case ASTReader::Failure: return true;
2488
1
  case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2489
43
  case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2490
0
  case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2491
0
  case ASTReader::ConfigurationMismatch:
2492
0
    return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2493
0
  case ASTReader::HadErrors: return true;
2494
23.7k
  case ASTReader::Success: return false;
2495
23.7k
  }
2496
2497
0
  llvm_unreachable("unknown ASTReadResult");
2498
0
}
2499
2500
ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2501
    BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2502
    bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2503
8.33k
    std::string &SuggestedPredefines) {
2504
8.33k
  if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2505
    // FIXME this drops errors on the floor.
2506
0
    consumeError(std::move(Err));
2507
0
    return Failure;
2508
0
  }
2509
2510
  // Read all of the records in the options block.
2511
8.33k
  RecordData Record;
2512
8.33k
  ASTReadResult Result = Success;
2513
50.0k
  while (true) {
2514
50.0k
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2515
50.0k
    if (!MaybeEntry) {
2516
      // FIXME this drops errors on the floor.
2517
0
      consumeError(MaybeEntry.takeError());
2518
0
      return Failure;
2519
0
    }
2520
50.0k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
2521
2522
50.0k
    switch (Entry.Kind) {
2523
0
    case llvm::BitstreamEntry::Error:
2524
0
    case llvm::BitstreamEntry::SubBlock:
2525
0
      return Failure;
2526
2527
8.34k
    case llvm::BitstreamEntry::EndBlock:
2528
8.34k
      return Result;
2529
2530
41.6k
    case llvm::BitstreamEntry::Record:
2531
      // The interesting case.
2532
41.6k
      break;
2533
50.0k
    }
2534
2535
    // Read and process a record.
2536
41.6k
    Record.clear();
2537
41.6k
    Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2538
41.6k
    if (!MaybeRecordType) {
2539
      // FIXME this drops errors on the floor.
2540
0
      consumeError(MaybeRecordType.takeError());
2541
0
      return Failure;
2542
0
    }
2543
41.6k
    switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2544
8.34k
    case LANGUAGE_OPTIONS: {
2545
8.34k
      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546
8.34k
      if (ParseLanguageOptions(Record, Complain, Listener,
2547
8.34k
                               AllowCompatibleConfigurationMismatch))
2548
25
        Result = ConfigurationMismatch;
2549
8.34k
      break;
2550
0
    }
2551
2552
8.34k
    case TARGET_OPTIONS: {
2553
8.34k
      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554
8.34k
      if (ParseTargetOptions(Record, Complain, Listener,
2555
8.34k
                             AllowCompatibleConfigurationMismatch))
2556
7
        Result = ConfigurationMismatch;
2557
8.34k
      break;
2558
0
    }
2559
2560
8.34k
    case FILE_SYSTEM_OPTIONS: {
2561
8.34k
      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2562
8.34k
      if (!AllowCompatibleConfigurationMismatch &&
2563
8.34k
          
ParseFileSystemOptions(Record, Complain, Listener)7.79k
)
2564
0
        Result = ConfigurationMismatch;
2565
8.34k
      break;
2566
0
    }
2567
2568
8.34k
    case HEADER_SEARCH_OPTIONS: {
2569
8.34k
      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2570
8.34k
      if (!AllowCompatibleConfigurationMismatch &&
2571
8.34k
          
ParseHeaderSearchOptions(Record, Complain, Listener)7.79k
)
2572
3
        Result = ConfigurationMismatch;
2573
8.34k
      break;
2574
0
    }
2575
2576
8.34k
    case PREPROCESSOR_OPTIONS:
2577
8.34k
      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2578
8.34k
      if (!AllowCompatibleConfigurationMismatch &&
2579
8.34k
          ParsePreprocessorOptions(Record, Complain, Listener,
2580
7.79k
                                   SuggestedPredefines))
2581
9
        Result = ConfigurationMismatch;
2582
8.34k
      break;
2583
41.6k
    }
2584
41.6k
  }
2585
8.33k
}
2586
2587
ASTReader::ASTReadResult
2588
ASTReader::ReadControlBlock(ModuleFile &F,
2589
                            SmallVectorImpl<ImportedModule> &Loaded,
2590
                            const ModuleFile *ImportedBy,
2591
18.1k
                            unsigned ClientLoadCapabilities) {
2592
18.1k
  BitstreamCursor &Stream = F.Stream;
2593
2594
18.1k
  if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2595
0
    Error(std::move(Err));
2596
0
    return Failure;
2597
0
  }
2598
2599
  // Lambda to read the unhashed control block the first time it's called.
2600
  //
2601
  // For PCM files, the unhashed control block cannot be read until after the
2602
  // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2603
  // need to look ahead before reading the IMPORTS record.  For consistency,
2604
  // this block is always read somehow (see BitstreamEntry::EndBlock).
2605
18.1k
  bool HasReadUnhashedControlBlock = false;
2606
46.5k
  auto readUnhashedControlBlockOnce = [&]() {
2607
46.5k
    if (!HasReadUnhashedControlBlock) {
2608
18.0k
      HasReadUnhashedControlBlock = true;
2609
18.0k
      if (ASTReadResult Result =
2610
18.0k
              readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2611
11
        return Result;
2612
18.0k
    }
2613
46.4k
    return Success;
2614
46.5k
  };
2615
2616
18.1k
  bool DisableValidation = shouldDisableValidationForFile(F);
2617
2618
  // Read all of the records and blocks in the control block.
2619
18.1k
  RecordData Record;
2620
18.1k
  unsigned NumInputs = 0;
2621
18.1k
  unsigned NumUserInputs = 0;
2622
18.1k
  StringRef BaseDirectoryAsWritten;
2623
186k
  while (true) {
2624
186k
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2625
186k
    if (!MaybeEntry) {
2626
0
      Error(MaybeEntry.takeError());
2627
0
      return Failure;
2628
0
    }
2629
186k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
2630
2631
186k
    switch (Entry.Kind) {
2632
0
    case llvm::BitstreamEntry::Error:
2633
0
      Error("malformed block record in AST file");
2634
0
      return Failure;
2635
18.0k
    case llvm::BitstreamEntry::EndBlock: {
2636
      // Validate the module before returning.  This call catches an AST with
2637
      // no module name and no imports.
2638
18.0k
      if (ASTReadResult Result = readUnhashedControlBlockOnce())
2639
0
        return Result;
2640
2641
      // Validate input files.
2642
18.0k
      const HeaderSearchOptions &HSOpts =
2643
18.0k
          PP.getHeaderSearchInfo().getHeaderSearchOpts();
2644
2645
      // All user input files reside at the index range [0, NumUserInputs), and
2646
      // system input files reside at [NumUserInputs, NumInputs). For explicitly
2647
      // loaded module files, ignore missing inputs.
2648
18.0k
      if (!DisableValidation && 
F.Kind != MK_ExplicitModule17.4k
&&
2649
18.0k
          
F.Kind != MK_PrebuiltModule16.1k
) {
2650
16.0k
        bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2651
2652
        // If we are reading a module, we will create a verification timestamp,
2653
        // so we verify all input files.  Otherwise, verify only user input
2654
        // files.
2655
2656
16.0k
        unsigned N = NumUserInputs;
2657
16.0k
        if (ValidateSystemInputs ||
2658
16.0k
            
(8.76k
HSOpts.ModulesValidateOncePerBuildSession8.76k
&&
2659
8.76k
             
F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp20
&&
2660
8.76k
             
F.Kind == MK_ImplicitModule9
))
2661
7.33k
          N = NumInputs;
2662
2663
815k
        for (unsigned I = 0; I < N; 
++I798k
) {
2664
799k
          InputFile IF = getInputFile(F, I+1, Complain);
2665
799k
          if (!IF.getFile() || 
IF.isOutOfDate()799k
)
2666
39
            return OutOfDate;
2667
799k
        }
2668
16.0k
      }
2669
2670
17.9k
      if (Listener)
2671
17.9k
        Listener->visitModuleFile(F.FileName, F.Kind);
2672
2673
17.9k
      if (Listener && Listener->needsInputFileVisitation()) {
2674
3.98k
        unsigned N = Listener->needsSystemInputFileVisitation() ? 
NumInputs3.97k
2675
3.98k
                                                                : 
NumUserInputs13
;
2676
518k
        for (unsigned I = 0; I < N; 
++I514k
) {
2677
514k
          bool IsSystem = I >= NumUserInputs;
2678
514k
          InputFileInfo FI = readInputFileInfo(F, I+1);
2679
514k
          Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2680
514k
                                   F.Kind == MK_ExplicitModule ||
2681
514k
                                   
F.Kind == MK_PrebuiltModule514k
);
2682
514k
        }
2683
3.98k
      }
2684
2685
17.9k
      return Success;
2686
18.0k
    }
2687
2688
36.0k
    case llvm::BitstreamEntry::SubBlock:
2689
36.0k
      switch (Entry.ID) {
2690
18.0k
      case INPUT_FILES_BLOCK_ID:
2691
18.0k
        F.InputFilesCursor = Stream;
2692
18.0k
        if (llvm::Error Err = Stream.SkipBlock()) {
2693
0
          Error(std::move(Err));
2694
0
          return Failure;
2695
0
        }
2696
18.0k
        if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2697
0
          Error("malformed block record in AST file");
2698
0
          return Failure;
2699
0
        }
2700
18.0k
        continue;
2701
2702
18.0k
      case OPTIONS_BLOCK_ID:
2703
        // If we're reading the first module for this group, check its options
2704
        // are compatible with ours. For modules it imports, no further checking
2705
        // is required, because we checked them when we built it.
2706
18.0k
        if (
Listener18.0k
&& !ImportedBy) {
2707
          // Should we allow the configuration of the module file to differ from
2708
          // the configuration of the current translation unit in a compatible
2709
          // way?
2710
          //
2711
          // FIXME: Allow this for files explicitly specified with -include-pch.
2712
8.30k
          bool AllowCompatibleConfigurationMismatch =
2713
8.30k
              F.Kind == MK_ExplicitModule || 
F.Kind == MK_PrebuiltModule7.80k
;
2714
2715
8.30k
          ASTReadResult Result =
2716
8.30k
              ReadOptionsBlock(Stream, ClientLoadCapabilities,
2717
8.30k
                               AllowCompatibleConfigurationMismatch, *Listener,
2718
8.30k
                               SuggestedPredefines);
2719
8.30k
          if (Result == Failure) {
2720
0
            Error("malformed block record in AST file");
2721
0
            return Result;
2722
0
          }
2723
2724
8.30k
          if (DisableValidation ||
2725
8.30k
              
(7.90k
AllowConfigurationMismatch7.90k
&&
Result == ConfigurationMismatch6
))
2726
405
            Result = Success;
2727
2728
          // If we can't load the module, exit early since we likely
2729
          // will rebuild the module anyway. The stream may be in the
2730
          // middle of a block.
2731
8.30k
          if (Result != Success)
2732
24
            return Result;
2733
9.71k
        } else if (llvm::Error Err = Stream.SkipBlock()) {
2734
0
          Error(std::move(Err));
2735
0
          return Failure;
2736
0
        }
2737
18.0k
        continue;
2738
2739
18.0k
      default:
2740
0
        if (llvm::Error Err = Stream.SkipBlock()) {
2741
0
          Error(std::move(Err));
2742
0
          return Failure;
2743
0
        }
2744
0
        continue;
2745
36.0k
      }
2746
2747
132k
    case llvm::BitstreamEntry::Record:
2748
      // The interesting case.
2749
132k
      break;
2750
186k
    }
2751
2752
    // Read and process a record.
2753
132k
    Record.clear();
2754
132k
    StringRef Blob;
2755
132k
    Expected<unsigned> MaybeRecordType =
2756
132k
        Stream.readRecord(Entry.ID, Record, &Blob);
2757
132k
    if (!MaybeRecordType) {
2758
0
      Error(MaybeRecordType.takeError());
2759
0
      return Failure;
2760
0
    }
2761
132k
    switch ((ControlRecordTypes)MaybeRecordType.get()) {
2762
18.1k
    case METADATA: {
2763
18.1k
      if (Record[0] != VERSION_MAJOR && 
!DisableValidation0
) {
2764
0
        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2765
0
          Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2766
0
                                        : diag::err_pch_version_too_new);
2767
0
        return VersionMismatch;
2768
0
      }
2769
2770
18.1k
      bool hasErrors = Record[6];
2771
18.1k
      if (hasErrors && 
!DisableValidation120
) {
2772
        // If requested by the caller and the module hasn't already been read
2773
        // or compiled, mark modules on error as out-of-date.
2774
84
        if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2775
84
            
canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)21
)
2776
15
          return OutOfDate;
2777
2778
69
        if (!AllowASTWithCompilerErrors) {
2779
4
          Diag(diag::err_pch_with_compiler_errors);
2780
4
          return HadErrors;
2781
4
        }
2782
69
      }
2783
18.0k
      if (hasErrors) {
2784
101
        Diags.ErrorOccurred = true;
2785
101
        Diags.UncompilableErrorOccurred = true;
2786
101
        Diags.UnrecoverableErrorOccurred = true;
2787
101
      }
2788
2789
18.0k
      F.RelocatablePCH = Record[4];
2790
      // Relative paths in a relocatable PCH are relative to our sysroot.
2791
18.0k
      if (F.RelocatablePCH)
2792
2
        F.BaseDirectory = isysroot.empty() ? 
"/"0
: isysroot;
2793
2794
18.0k
      F.HasTimestamps = Record[5];
2795
2796
18.0k
      const std::string &CurBranch = getClangFullRepositoryVersion();
2797
18.0k
      StringRef ASTBranch = Blob;
2798
18.0k
      if (StringRef(CurBranch) != ASTBranch && 
!DisableValidation0
) {
2799
0
        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2800
0
          Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2801
0
        return VersionMismatch;
2802
0
      }
2803
18.0k
      break;
2804
18.0k
    }
2805
2806
18.0k
    case IMPORTS: {
2807
      // Validate the AST before processing any imports (otherwise, untangling
2808
      // them can be error-prone and expensive).  A module will have a name and
2809
      // will already have been validated, but this catches the PCH case.
2810
14.3k
      if (ASTReadResult Result = readUnhashedControlBlockOnce())
2811
0
        return Result;
2812
2813
      // Load each of the imported PCH files.
2814
14.3k
      unsigned Idx = 0, N = Record.size();
2815
38.0k
      while (Idx < N) {
2816
        // Read information about the AST file.
2817
23.7k
        ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2818
        // The import location will be the local one for now; we will adjust
2819
        // all import locations of module imports after the global source
2820
        // location info are setup, in ReadAST.
2821
23.7k
        SourceLocation ImportLoc =
2822
23.7k
            ReadUntranslatedSourceLocation(Record[Idx++]);
2823
23.7k
        off_t StoredSize = (off_t)Record[Idx++];
2824
23.7k
        time_t StoredModTime = (time_t)Record[Idx++];
2825
23.7k
        auto FirstSignatureByte = Record.begin() + Idx;
2826
23.7k
        ASTFileSignature StoredSignature = ASTFileSignature::create(
2827
23.7k
            FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2828
23.7k
        Idx += ASTFileSignature::size;
2829
2830
23.7k
        std::string ImportedName = ReadString(Record, Idx);
2831
23.7k
        std::string ImportedFile;
2832
2833
        // For prebuilt and explicit modules first consult the file map for
2834
        // an override. Note that here we don't search prebuilt module
2835
        // directories, only the explicit name to file mappings. Also, we will
2836
        // still verify the size/signature making sure it is essentially the
2837
        // same file but perhaps in a different location.
2838
23.7k
        if (ImportedKind == MK_PrebuiltModule || 
ImportedKind == MK_ExplicitModule23.7k
)
2839
1.75k
          ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2840
1.75k
            ImportedName, /*FileMapOnly*/ true);
2841
2842
23.7k
        if (ImportedFile.empty())
2843
          // Use BaseDirectoryAsWritten to ensure we use the same path in the
2844
          // ModuleCache as when writing.
2845
23.7k
          ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2846
21
        else
2847
21
          SkipPath(Record, Idx);
2848
2849
        // If our client can't cope with us being out of date, we can't cope with
2850
        // our dependency being missing.
2851
23.7k
        unsigned Capabilities = ClientLoadCapabilities;
2852
23.7k
        if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2853
4.58k
          Capabilities &= ~ARR_Missing;
2854
2855
        // Load the AST file.
2856
23.7k
        auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2857
23.7k
                                  Loaded, StoredSize, StoredModTime,
2858
23.7k
                                  StoredSignature, Capabilities);
2859
2860
        // If we diagnosed a problem, produce a backtrace.
2861
23.7k
        bool recompilingFinalized =
2862
23.7k
            Result == OutOfDate && 
(Capabilities & ARR_OutOfDate)43
&&
2863
23.7k
            
getModuleManager().getModuleCache().isPCMFinal(F.FileName)33
;
2864
23.7k
        if (isDiagnosedResult(Result, Capabilities) || 
recompilingFinalized23.7k
)
2865
17
          Diag(diag::note_module_file_imported_by)
2866
17
              << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2867
23.7k
        if (recompilingFinalized)
2868
2
          Diag(diag::note_module_file_conflict);
2869
2870
23.7k
        switch (Result) {
2871
5
        case Failure: return Failure;
2872
          // If we have to ignore the dependency, we'll have to ignore this too.
2873
1
        case Missing:
2874
44
        case OutOfDate: return OutOfDate;
2875
0
        case VersionMismatch: return VersionMismatch;
2876
0
        case ConfigurationMismatch: return ConfigurationMismatch;
2877
0
        case HadErrors: return HadErrors;
2878
23.7k
        case Success: break;
2879
23.7k
        }
2880
23.7k
      }
2881
14.3k
      break;
2882
14.3k
    }
2883
2884
14.3k
    case ORIGINAL_FILE:
2885
4.17k
      F.OriginalSourceFileID = FileID::get(Record[0]);
2886
4.17k
      F.ActualOriginalSourceFileName = std::string(Blob);
2887
4.17k
      F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2888
4.17k
      ResolveImportedPath(F, F.OriginalSourceFileName);
2889
4.17k
      break;
2890
2891
18.0k
    case ORIGINAL_FILE_ID:
2892
18.0k
      F.OriginalSourceFileID = FileID::get(Record[0]);
2893
18.0k
      break;
2894
2895
17.3k
    case ORIGINAL_PCH_DIR:
2896
17.3k
      F.OriginalDir = std::string(Blob);
2897
17.3k
      break;
2898
2899
14.1k
    case MODULE_NAME:
2900
14.1k
      F.ModuleName = std::string(Blob);
2901
14.1k
      Diag(diag::remark_module_import)
2902
14.1k
          << F.ModuleName << F.FileName << (ImportedBy ? 
true9.59k
:
false4.52k
)
2903
14.1k
          << (ImportedBy ? 
StringRef(ImportedBy->ModuleName)9.59k
:
StringRef()4.52k
);
2904
14.1k
      if (Listener)
2905
14.1k
        Listener->ReadModuleName(F.ModuleName);
2906
2907
      // Validate the AST as soon as we have a name so we can exit early on
2908
      // failure.
2909
14.1k
      if (ASTReadResult Result = readUnhashedControlBlockOnce())
2910
11
        return Result;
2911
2912
14.1k
      break;
2913
2914
14.1k
    case MODULE_DIRECTORY: {
2915
      // Save the BaseDirectory as written in the PCM for computing the module
2916
      // filename for the ModuleCache.
2917
13.9k
      BaseDirectoryAsWritten = Blob;
2918
13.9k
      assert(!F.ModuleName.empty() &&
2919
13.9k
             "MODULE_DIRECTORY found before MODULE_NAME");
2920
      // If we've already loaded a module map file covering this module, we may
2921
      // have a better path for it (relative to the current build).
2922
0
      Module *M = PP.getHeaderSearchInfo().lookupModule(
2923
13.9k
          F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
2924
13.9k
          /*AllowExtraModuleMapSearch*/ true);
2925
13.9k
      if (M && 
M->Directory13.6k
) {
2926
        // If we're implicitly loading a module, the base directory can't
2927
        // change between the build and use.
2928
        // Don't emit module relocation error if we have -fno-validate-pch
2929
13.6k
        if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2930
13.6k
                  DisableValidationForModuleKind::Module) &&
2931
13.6k
            
F.Kind != MK_ExplicitModule13.6k
&&
F.Kind != MK_PrebuiltModule12.6k
) {
2932
12.6k
          auto BuildDir = PP.getFileManager().getDirectory(Blob);
2933
12.6k
          if (!BuildDir || *BuildDir != M->Directory) {
2934
5
            if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2935
4
              Diag(diag::err_imported_module_relocated)
2936
4
                  << F.ModuleName << Blob << M->Directory->getName();
2937
5
            return OutOfDate;
2938
5
          }
2939
12.6k
        }
2940
13.6k
        F.BaseDirectory = std::string(M->Directory->getName());
2941
13.6k
      } else {
2942
269
        F.BaseDirectory = std::string(Blob);
2943
269
      }
2944
13.9k
      break;
2945
13.9k
    }
2946
2947
13.9k
    case MODULE_MAP_FILE:
2948
13.9k
      if (ASTReadResult Result =
2949
13.9k
              ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2950
6
        return Result;
2951
13.9k
      break;
2952
2953
18.0k
    case INPUT_FILE_OFFSETS:
2954
18.0k
      NumInputs = Record[0];
2955
18.0k
      NumUserInputs = Record[1];
2956
18.0k
      F.InputFileOffsets =
2957
18.0k
          (const llvm::support::unaligned_uint64_t *)Blob.data();
2958
18.0k
      F.InputFilesLoaded.resize(NumInputs);
2959
18.0k
      F.NumUserInputFiles = NumUserInputs;
2960
18.0k
      break;
2961
132k
    }
2962
132k
  }
2963
18.1k
}
2964
2965
llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
2966
17.9k
                                    unsigned ClientLoadCapabilities) {
2967
17.9k
  BitstreamCursor &Stream = F.Stream;
2968
2969
17.9k
  if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
2970
0
    return Err;
2971
17.9k
  F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2972
2973
  // Read all of the records and blocks for the AST file.
2974
17.9k
  RecordData Record;
2975
502k
  while (true) {
2976
502k
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2977
502k
    if (!MaybeEntry)
2978
0
      return MaybeEntry.takeError();
2979
502k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
2980
2981
502k
    switch (Entry.Kind) {
2982
0
    case llvm::BitstreamEntry::Error:
2983
0
      return llvm::createStringError(
2984
0
          std::errc::illegal_byte_sequence,
2985
0
          "error at end of module block in AST file");
2986
17.9k
    case llvm::BitstreamEntry::EndBlock:
2987
      // Outside of C++, we do not store a lookup map for the translation unit.
2988
      // Instead, mark it as needing a lookup map to be built if this module
2989
      // contains any declarations lexically within it (which it always does!).
2990
      // This usually has no cost, since we very rarely need the lookup map for
2991
      // the translation unit outside C++.
2992
17.9k
      if (ASTContext *Ctx = ContextObj) {
2993
17.9k
        DeclContext *DC = Ctx->getTranslationUnitDecl();
2994
17.9k
        if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2995
5.12k
          DC->setMustBuildLookupTable();
2996
17.9k
      }
2997
2998
17.9k
      return llvm::Error::success();
2999
86.5k
    case llvm::BitstreamEntry::SubBlock:
3000
86.5k
      switch (Entry.ID) {
3001
17.9k
      case DECLTYPES_BLOCK_ID:
3002
        // We lazily load the decls block, but we want to set up the
3003
        // DeclsCursor cursor to point into it.  Clone our current bitcode
3004
        // cursor to it, enter the block and read the abbrevs in that block.
3005
        // With the main cursor, we just skip over it.
3006
17.9k
        F.DeclsCursor = Stream;
3007
17.9k
        if (llvm::Error Err = Stream.SkipBlock())
3008
0
          return Err;
3009
17.9k
        if (llvm::Error Err = ReadBlockAbbrevs(
3010
17.9k
                F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3011
0
          return Err;
3012
17.9k
        break;
3013
3014
17.9k
      case PREPROCESSOR_BLOCK_ID:
3015
17.9k
        F.MacroCursor = Stream;
3016
17.9k
        if (!PP.getExternalSource())
3017
7.24k
          PP.setExternalSource(this);
3018
3019
17.9k
        if (llvm::Error Err = Stream.SkipBlock())
3020
0
          return Err;
3021
17.9k
        if (llvm::Error Err =
3022
17.9k
                ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3023
0
          return Err;
3024
17.9k
        F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3025
17.9k
        break;
3026
3027
721
      case PREPROCESSOR_DETAIL_BLOCK_ID:
3028
721
        F.PreprocessorDetailCursor = Stream;
3029
3030
721
        if (llvm::Error Err = Stream.SkipBlock()) {
3031
0
          return Err;
3032
0
        }
3033
721
        if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3034
721
                                               PREPROCESSOR_DETAIL_BLOCK_ID))
3035
0
          return Err;
3036
721
        F.PreprocessorDetailStartOffset
3037
721
        = F.PreprocessorDetailCursor.GetCurrentBitNo();
3038
3039
721
        if (!PP.getPreprocessingRecord())
3040
119
          PP.createPreprocessingRecord();
3041
721
        if (!PP.getPreprocessingRecord()->getExternalSource())
3042
569
          PP.getPreprocessingRecord()->SetExternalSource(*this);
3043
721
        break;
3044
3045
17.9k
      case SOURCE_MANAGER_BLOCK_ID:
3046
17.9k
        if (llvm::Error Err = ReadSourceManagerBlock(F))
3047
0
          return Err;
3048
17.9k
        break;
3049
3050
17.9k
      case SUBMODULE_BLOCK_ID:
3051
14.0k
        if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3052
4
          return Err;
3053
14.0k
        break;
3054
3055
17.9k
      case COMMENTS_BLOCK_ID: {
3056
17.9k
        BitstreamCursor C = Stream;
3057
3058
17.9k
        if (llvm::Error Err = Stream.SkipBlock())
3059
0
          return Err;
3060
17.9k
        if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3061
0
          return Err;
3062
17.9k
        CommentsCursors.push_back(std::make_pair(C, &F));
3063
17.9k
        break;
3064
17.9k
      }
3065
3066
0
      default:
3067
0
        if (llvm::Error Err = Stream.SkipBlock())
3068
0
          return Err;
3069
0
        break;
3070
86.5k
      }
3071
86.5k
      continue;
3072
3073
397k
    case llvm::BitstreamEntry::Record:
3074
      // The interesting case.
3075
397k
      break;
3076
502k
    }
3077
3078
    // Read and process a record.
3079
397k
    Record.clear();
3080
397k
    StringRef Blob;
3081
397k
    Expected<unsigned> MaybeRecordType =
3082
397k
        Stream.readRecord(Entry.ID, Record, &Blob);
3083
397k
    if (!MaybeRecordType)
3084
0
      return MaybeRecordType.takeError();
3085
397k
    ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3086
3087
    // If we're not loading an AST context, we don't care about most records.
3088
397k
    if (!ContextObj) {
3089
481
      switch (RecordType) {
3090
25
      case IDENTIFIER_TABLE:
3091
50
      case IDENTIFIER_OFFSET:
3092
66
      case INTERESTING_IDENTIFIERS:
3093
91
      case STATISTICS:
3094
91
      case PP_CONDITIONAL_STACK:
3095
91
      case PP_COUNTER_VALUE:
3096
116
      case SOURCE_LOCATION_OFFSETS:
3097
141
      case MODULE_OFFSET_MAP:
3098
166
      case SOURCE_MANAGER_LINE_TABLE:
3099
191
      case SOURCE_LOCATION_PRELOADS:
3100
191
      case PPD_ENTITIES_OFFSETS:
3101
216
      case HEADER_SEARCH_TABLE:
3102
216
      case IMPORTED_MODULES:
3103
241
      case MACRO_OFFSET:
3104
241
        break;
3105
240
      default:
3106
240
        continue;
3107
481
      }
3108
481
    }
3109
3110
397k
    switch (RecordType) {
3111
17.9k
    default:  // Default behavior: ignore.
3112
17.9k
      break;
3113
3114
17.9k
    case TYPE_OFFSET: {
3115
17.9k
      if (F.LocalNumTypes != 0)
3116
0
        return llvm::createStringError(
3117
0
            std::errc::illegal_byte_sequence,
3118
0
            "duplicate TYPE_OFFSET record in AST file");
3119
17.9k
      F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3120
17.9k
      F.LocalNumTypes = Record[0];
3121
17.9k
      unsigned LocalBaseTypeIndex = Record[1];
3122
17.9k
      F.BaseTypeIndex = getTotalNumTypes();
3123
3124
17.9k
      if (F.LocalNumTypes > 0) {
3125
        // Introduce the global -> local mapping for types within this module.
3126
17.7k
        GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3127
3128
        // Introduce the local -> global mapping for types within this module.
3129
17.7k
        F.TypeRemap.insertOrReplace(
3130
17.7k
          std::make_pair(LocalBaseTypeIndex,
3131
17.7k
                         F.BaseTypeIndex - LocalBaseTypeIndex));
3132
3133
17.7k
        TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3134
17.7k
      }
3135
17.9k
      break;
3136
17.9k
    }
3137
3138
17.9k
    case DECL_OFFSET: {
3139
17.9k
      if (F.LocalNumDecls != 0)
3140
0
        return llvm::createStringError(
3141
0
            std::errc::illegal_byte_sequence,
3142
0
            "duplicate DECL_OFFSET record in AST file");
3143
17.9k
      F.DeclOffsets = (const DeclOffset *)Blob.data();
3144
17.9k
      F.LocalNumDecls = Record[0];
3145
17.9k
      unsigned LocalBaseDeclID = Record[1];
3146
17.9k
      F.BaseDeclID = getTotalNumDecls();
3147
3148
17.9k
      if (F.LocalNumDecls > 0) {
3149
        // Introduce the global -> local mapping for declarations within this
3150
        // module.
3151
17.6k
        GlobalDeclMap.insert(
3152
17.6k
          std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3153
3154
        // Introduce the local -> global mapping for declarations within this
3155
        // module.
3156
17.6k
        F.DeclRemap.insertOrReplace(
3157
17.6k
          std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3158
3159
        // Introduce the global -> local mapping for declarations within this
3160
        // module.
3161
17.6k
        F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3162
3163
17.6k
        DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3164
17.6k
      }
3165
17.9k
      break;
3166
17.9k
    }
3167
3168
17.9k
    case TU_UPDATE_LEXICAL: {
3169
17.9k
      DeclContext *TU = ContextObj->getTranslationUnitDecl();
3170
17.9k
      LexicalContents Contents(
3171
17.9k
          reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3172
17.9k
              Blob.data()),
3173
17.9k
          static_cast<unsigned int>(Blob.size() / 4));
3174
17.9k
      TULexicalDecls.push_back(std::make_pair(&F, Contents));
3175
17.9k
      TU->setHasExternalLexicalStorage(true);
3176
17.9k
      break;
3177
17.9k
    }
3178
3179
27.0k
    case UPDATE_VISIBLE: {
3180
27.0k
      unsigned Idx = 0;
3181
27.0k
      serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3182
27.0k
      auto *Data = (const unsigned char*)Blob.data();
3183
27.0k
      PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3184
      // If we've already loaded the decl, perform the updates when we finish
3185
      // loading this block.
3186
27.0k
      if (Decl *D = GetExistingDecl(ID))
3187
16.8k
        PendingUpdateRecords.push_back(
3188
16.8k
            PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3189
27.0k
      break;
3190
17.9k
    }
3191
3192
17.9k
    case IDENTIFIER_TABLE:
3193
17.9k
      F.IdentifierTableData =
3194
17.9k
          reinterpret_cast<const unsigned char *>(Blob.data());
3195
17.9k
      if (Record[0]) {
3196
17.9k
        F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3197
17.9k
            F.IdentifierTableData + Record[0],
3198
17.9k
            F.IdentifierTableData + sizeof(uint32_t),
3199
17.9k
            F.IdentifierTableData,
3200
17.9k
            ASTIdentifierLookupTrait(*this, F));
3201
3202
17.9k
        PP.getIdentifierTable().setExternalIdentifierLookup(this);
3203
17.9k
      }
3204
17.9k
      break;
3205
3206
17.9k
    case IDENTIFIER_OFFSET: {
3207
17.9k
      if (F.LocalNumIdentifiers != 0)
3208
0
        return llvm::createStringError(
3209
0
            std::errc::illegal_byte_sequence,
3210
0
            "duplicate IDENTIFIER_OFFSET record in AST file");
3211
17.9k
      F.IdentifierOffsets = (const uint32_t *)Blob.data();
3212
17.9k
      F.LocalNumIdentifiers = Record[0];
3213
17.9k
      unsigned LocalBaseIdentifierID = Record[1];
3214
17.9k
      F.BaseIdentifierID = getTotalNumIdentifiers();
3215
3216
17.9k
      if (F.LocalNumIdentifiers > 0) {
3217
        // Introduce the global -> local mapping for identifiers within this
3218
        // module.
3219
16.8k
        GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3220
16.8k
                                                  &F));
3221
3222
        // Introduce the local -> global mapping for identifiers within this
3223
        // module.
3224
16.8k
        F.IdentifierRemap.insertOrReplace(
3225
16.8k
          std::make_pair(LocalBaseIdentifierID,
3226
16.8k
                         F.BaseIdentifierID - LocalBaseIdentifierID));
3227
3228
16.8k
        IdentifiersLoaded.resize(IdentifiersLoaded.size()
3229
16.8k
                                 + F.LocalNumIdentifiers);
3230
16.8k
      }
3231
17.9k
      break;
3232
17.9k
    }
3233
3234
9.70k
    case INTERESTING_IDENTIFIERS:
3235
9.70k
      F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3236
9.70k
      break;
3237
3238
3.43k
    case EAGERLY_DESERIALIZED_DECLS:
3239
      // FIXME: Skip reading this record if our ASTConsumer doesn't care
3240
      // about "interesting" decls (for instance, if we're building a module).
3241
14.1k
      for (unsigned I = 0, N = Record.size(); I != N; 
++I10.7k
)
3242
10.7k
        EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3243
3.43k
      break;
3244
3245
110
    case MODULAR_CODEGEN_DECLS:
3246
      // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3247
      // them (ie: if we're not codegenerating this module).
3248
110
      if (F.Kind == MK_MainFile ||
3249
110
          
getContext().getLangOpts().BuildingPCHWithObjectFile97
)
3250
50
        
for (unsigned I = 0, N = Record.size(); 14
I != N;
++I36
)
3251
36
          EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3252
110
      break;
3253
3254
17.9k
    case SPECIAL_TYPES:
3255
17.9k
      if (SpecialTypes.empty()) {
3256
65.0k
        for (unsigned I = 0, N = Record.size(); I != N; 
++I57.8k
)
3257
57.8k
          SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3258
7.23k
        break;
3259
7.23k
      }
3260
3261
10.6k
      if (SpecialTypes.size() != Record.size())
3262
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3263
0
                                       "invalid special-types record");
3264
3265
96.2k
      
for (unsigned I = 0, N = Record.size(); 10.6k
I != N;
++I85.5k
) {
3266
85.5k
        serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3267
85.5k
        if (!SpecialTypes[I])
3268
40.2k
          SpecialTypes[I] = ID;
3269
        // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3270
        // merge step?
3271
85.5k
      }
3272
10.6k
      break;
3273
3274
17.9k
    case STATISTICS:
3275
17.9k
      TotalNumStatements += Record[0];
3276
17.9k
      TotalNumMacros += Record[1];
3277
17.9k
      TotalLexicalDeclContexts += Record[2];
3278
17.9k
      TotalVisibleDeclContexts += Record[3];
3279
17.9k
      break;
3280
3281
52
    case UNUSED_FILESCOPED_DECLS:
3282
257
      for (unsigned I = 0, N = Record.size(); I != N; 
++I205
)
3283
205
        UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3284
52
      break;
3285
3286
5
    case DELEGATING_CTORS:
3287
12
      for (unsigned I = 0, N = Record.size(); I != N; 
++I7
)
3288
7
        DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3289
5
      break;
3290
3291
30
    case WEAK_UNDECLARED_IDENTIFIERS:
3292
30
      if (Record.size() % 4 != 0)
3293
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3294
0
                                       "invalid weak identifiers record");
3295
3296
      // FIXME: Ignore weak undeclared identifiers from non-original PCH
3297
      // files. This isn't the way to do it :)
3298
30
      WeakUndeclaredIdentifiers.clear();
3299
3300
      // Translate the weak, undeclared identifiers into global IDs.
3301
196
      for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3302
166
        WeakUndeclaredIdentifiers.push_back(
3303
166
          getGlobalIdentifierID(F, Record[I++]));
3304
166
        WeakUndeclaredIdentifiers.push_back(
3305
166
          getGlobalIdentifierID(F, Record[I++]));
3306
166
        WeakUndeclaredIdentifiers.push_back(
3307
166
          ReadSourceLocation(F, Record, I).getRawEncoding());
3308
166
        WeakUndeclaredIdentifiers.push_back(Record[I++]);
3309
166
      }
3310
30
      break;
3311
3312
922
    case SELECTOR_OFFSETS: {
3313
922
      F.SelectorOffsets = (const uint32_t *)Blob.data();
3314
922
      F.LocalNumSelectors = Record[0];
3315
922
      unsigned LocalBaseSelectorID = Record[1];
3316
922
      F.BaseSelectorID = getTotalNumSelectors();
3317
3318
922
      if (F.LocalNumSelectors > 0) {
3319
        // Introduce the global -> local mapping for selectors within this
3320
        // module.
3321
911
        GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3322
3323
        // Introduce the local -> global mapping for selectors within this
3324
        // module.
3325
911
        F.SelectorRemap.insertOrReplace(
3326
911
          std::make_pair(LocalBaseSelectorID,
3327
911
                         F.BaseSelectorID - LocalBaseSelectorID));
3328
3329
911
        SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3330
911
      }
3331
922
      break;
3332
30
    }
3333
3334
922
    case METHOD_POOL:
3335
922
      F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3336
922
      if (Record[0])
3337
922
        F.SelectorLookupTable
3338
922
          = ASTSelectorLookupTable::Create(
3339
922
                        F.SelectorLookupTableData + Record[0],
3340
922
                        F.SelectorLookupTableData,
3341
922
                        ASTSelectorLookupTrait(*this, F));
3342
922
      TotalNumMethodPoolEntries += Record[1];
3343
922
      break;
3344
3345
3
    case REFERENCED_SELECTOR_POOL:
3346
3
      if (!Record.empty()) {
3347
6
        for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3348
3
          ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3349
3
                                                                Record[Idx++]));
3350
3
          ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3351
3
                                              getRawEncoding());
3352
3
        }
3353
3
      }
3354
3
      break;
3355
3356
28
    case PP_CONDITIONAL_STACK:
3357
28
      if (!Record.empty()) {
3358
28
        unsigned Idx = 0, End = Record.size() - 1;
3359
28
        bool ReachedEOFWhileSkipping = Record[Idx++];
3360
28
        llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3361
28
        if (ReachedEOFWhileSkipping) {
3362
18
          SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3363
18
          SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3364
18
          bool FoundNonSkipPortion = Record[Idx++];
3365
18
          bool FoundElse = Record[Idx++];
3366
18
          SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3367
18
          SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3368
18
                           FoundElse, ElseLoc);
3369
18
        }
3370
28
        SmallVector<PPConditionalInfo, 4> ConditionalStack;
3371
56
        while (Idx < End) {
3372
28
          auto Loc = ReadSourceLocation(F, Record, Idx);
3373
28
          bool WasSkipping = Record[Idx++];
3374
28
          bool FoundNonSkip = Record[Idx++];
3375
28
          bool FoundElse = Record[Idx++];
3376
28
          ConditionalStack.push_back(
3377
28
              {Loc, WasSkipping, FoundNonSkip, FoundElse});
3378
28
        }
3379
28
        PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3380
28
      }
3381
28
      break;
3382
3383
6
    case PP_COUNTER_VALUE:
3384
6
      if (!Record.empty() && Listener)
3385
6
        Listener->ReadCounter(F, Record[0]);
3386
6
      break;
3387
3388
17.9k
    case FILE_SORTED_DECLS:
3389
17.9k
      F.FileSortedDecls = (const DeclID *)Blob.data();
3390
17.9k
      F.NumFileSortedDecls = Record[0];
3391
17.9k
      break;
3392
3393
17.9k
    case SOURCE_LOCATION_OFFSETS: {
3394
17.9k
      F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3395
17.9k
      F.LocalNumSLocEntries = Record[0];
3396
17.9k
      SourceLocation::UIntTy SLocSpaceSize = Record[1];
3397
17.9k
      F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3398
17.9k
      std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3399
17.9k
          SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3400
17.9k
                                              SLocSpaceSize);
3401
17.9k
      if (!F.SLocEntryBaseID)
3402
0
        return llvm::createStringError(std::errc::invalid_argument,
3403
0
                                       "ran out of source locations");
3404
      // Make our entry in the range map. BaseID is negative and growing, so
3405
      // we invert it. Because we invert it, though, we need the other end of
3406
      // the range.
3407
17.9k
      unsigned RangeStart =
3408
17.9k
          unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3409
17.9k
      GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3410
17.9k
      F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3411
3412
      // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3413
17.9k
      assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3414
0
      GlobalSLocOffsetMap.insert(
3415
17.9k
          std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3416
17.9k
                           - SLocSpaceSize,&F));
3417
3418
      // Initialize the remapping table.
3419
      // Invalid stays invalid.
3420
17.9k
      F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3421
      // This module. Base was 2 when being compiled.
3422
17.9k
      F.SLocRemap.insertOrReplace(std::make_pair(
3423
17.9k
          2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3424
3425
17.9k
      TotalNumSLocEntries += F.LocalNumSLocEntries;
3426
17.9k
      break;
3427
17.9k
    }
3428
3429
14.2k
    case MODULE_OFFSET_MAP:
3430
14.2k
      F.ModuleOffsetMap = Blob;
3431
14.2k
      break;
3432
3433
17.9k
    case SOURCE_MANAGER_LINE_TABLE:
3434
17.9k
      ParseLineTable(F, Record);
3435
17.9k
      break;
3436
3437
17.9k
    case SOURCE_LOCATION_PRELOADS: {
3438
      // Need to transform from the local view (1-based IDs) to the global view,
3439
      // which is based off F.SLocEntryBaseID.
3440
17.9k
      if (!F.PreloadSLocEntries.empty())
3441
0
        return llvm::createStringError(
3442
0
            std::errc::illegal_byte_sequence,
3443
0
            "Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3444
3445
17.9k
      F.PreloadSLocEntries.swap(Record);
3446
17.9k
      break;
3447
17.9k
    }
3448
3449
34
    case EXT_VECTOR_DECLS:
3450
884
      for (unsigned I = 0, N = Record.size(); I != N; 
++I850
)
3451
850
        ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3452
34
      break;
3453
3454
11
    case VTABLE_USES:
3455
11
      if (Record.size() % 3 != 0)
3456
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3457
0
                                       "Invalid VTABLE_USES record");
3458
3459
      // Later tables overwrite earlier ones.
3460
      // FIXME: Modules will have some trouble with this. This is clearly not
3461
      // the right way to do this.
3462
11
      VTableUses.clear();
3463
3464
39
      for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3465
28
        VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3466
28
        VTableUses.push_back(
3467
28
          ReadSourceLocation(F, Record, Idx).getRawEncoding());
3468
28
        VTableUses.push_back(Record[Idx++]);
3469
28
      }
3470
11
      break;
3471
3472
1.48k
    case PENDING_IMPLICIT_INSTANTIATIONS:
3473
1.48k
      if (PendingInstantiations.size() % 2 != 0)
3474
0
        return llvm::createStringError(
3475
0
            std::errc::illegal_byte_sequence,
3476
0
            "Invalid existing PendingInstantiations");
3477
3478
1.48k
      if (Record.size() % 2 != 0)
3479
0
        return llvm::createStringError(
3480
0
            std::errc::illegal_byte_sequence,
3481
0
            "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3482
3483
5.16k
      
for (unsigned I = 0, N = Record.size(); 1.48k
I != N; /* in loop */) {
3484
3.68k
        PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3485
3.68k
        PendingInstantiations.push_back(
3486
3.68k
          ReadSourceLocation(F, Record, I).getRawEncoding());
3487
3.68k
      }
3488
1.48k
      break;
3489
3490
3.96k
    case SEMA_DECL_REFS:
3491
3.96k
      if (Record.size() != 3)
3492
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3493
0
                                       "Invalid SEMA_DECL_REFS block");
3494
15.8k
      
for (unsigned I = 0, N = Record.size(); 3.96k
I != N;
++I11.9k
)
3495
11.9k
        SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3496
3.96k
      break;
3497
3498
721
    case PPD_ENTITIES_OFFSETS: {
3499
721
      F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3500
721
      assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3501
0
      F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3502
3503
721
      unsigned LocalBasePreprocessedEntityID = Record[0];
3504
3505
721
      unsigned StartingID;
3506
721
      if (!PP.getPreprocessingRecord())
3507
0
        PP.createPreprocessingRecord();
3508
721
      if (!PP.getPreprocessingRecord()->getExternalSource())
3509
0
        PP.getPreprocessingRecord()->SetExternalSource(*this);
3510
721
      StartingID
3511
721
        = PP.getPreprocessingRecord()
3512
721
            ->allocateLoadedEntities(F.NumPreprocessedEntities);
3513
721
      F.BasePreprocessedEntityID = StartingID;
3514
3515
721
      if (F.NumPreprocessedEntities > 0) {
3516
        // Introduce the global -> local mapping for preprocessed entities in
3517
        // this module.
3518
721
        GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3519
3520
        // Introduce the local -> global mapping for preprocessed entities in
3521
        // this module.
3522
721
        F.PreprocessedEntityRemap.insertOrReplace(
3523
721
          std::make_pair(LocalBasePreprocessedEntityID,
3524
721
            F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3525
721
      }
3526
3527
721
      break;
3528
3.96k
    }
3529
3530
83
    case PPD_SKIPPED_RANGES: {
3531
83
      F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3532
83
      assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3533
0
      F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3534
3535
83
      if (!PP.getPreprocessingRecord())
3536
0
        PP.createPreprocessingRecord();
3537
83
      if (!PP.getPreprocessingRecord()->getExternalSource())
3538
0
        PP.getPreprocessingRecord()->SetExternalSource(*this);
3539
83
      F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3540
83
          ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3541
3542
83
      if (F.NumPreprocessedSkippedRanges > 0)
3543
83
        GlobalSkippedRangeMap.insert(
3544
83
            std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3545
83
      break;
3546
3.96k
    }
3547
3548
4.43k
    case DECL_UPDATE_OFFSETS:
3549
4.43k
      if (Record.size() % 2 != 0)
3550
0
        return llvm::createStringError(
3551
0
            std::errc::illegal_byte_sequence,
3552
0
            "invalid DECL_UPDATE_OFFSETS block in AST file");
3553
265k
      
for (unsigned I = 0, N = Record.size(); 4.43k
I != N;
I += 2260k
) {
3554
260k
        GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3555
260k
        DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3556
3557
        // If we've already loaded the decl, perform the updates when we finish
3558
        // loading this block.
3559
260k
        if (Decl *D = GetExistingDecl(ID))
3560
171
          PendingUpdateRecords.push_back(
3561
171
              PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3562
260k
      }
3563
4.43k
      break;
3564
3565
17.9k
    case OBJC_CATEGORIES_MAP:
3566
17.9k
      if (F.LocalNumObjCCategoriesInMap != 0)
3567
0
        return llvm::createStringError(
3568
0
            std::errc::illegal_byte_sequence,
3569
0
            "duplicate OBJC_CATEGORIES_MAP record in AST file");
3570
3571
17.9k
      F.LocalNumObjCCategoriesInMap = Record[0];
3572
17.9k
      F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3573
17.9k
      break;
3574
3575
17.9k
    case OBJC_CATEGORIES:
3576
17.9k
      F.ObjCCategories.swap(Record);
3577
17.9k
      break;
3578
3579
1
    case CUDA_SPECIAL_DECL_REFS:
3580
      // Later tables overwrite earlier ones.
3581
      // FIXME: Modules will have trouble with this.
3582
1
      CUDASpecialDeclRefs.clear();
3583
2
      for (unsigned I = 0, N = Record.size(); I != N; 
++I1
)
3584
1
        CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3585
1
      break;
3586
3587
17.9k
    case HEADER_SEARCH_TABLE:
3588
17.9k
      F.HeaderFileInfoTableData = Blob.data();
3589
17.9k
      F.LocalNumHeaderFileInfos = Record[1];
3590
17.9k
      if (Record[0]) {
3591
17.9k
        F.HeaderFileInfoTable
3592
17.9k
          = HeaderFileInfoLookupTable::Create(
3593
17.9k
                   (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3594
17.9k
                   (const unsigned char *)F.HeaderFileInfoTableData,
3595
17.9k
                   HeaderFileInfoTrait(*this, F,
3596
17.9k
                                       &PP.getHeaderSearchInfo(),
3597
17.9k
                                       Blob.data() + Record[2]));
3598
3599
17.9k
        PP.getHeaderSearchInfo().SetExternalSource(this);
3600
17.9k
        if (!PP.getHeaderSearchInfo().getExternalLookup())
3601
7.24k
          PP.getHeaderSearchInfo().SetExternalLookup(this);
3602
17.9k
      }
3603
17.9k
      break;
3604
3605
17.9k
    case FP_PRAGMA_OPTIONS:
3606
      // Later tables overwrite earlier ones.
3607
17.9k
      FPPragmaOptions.swap(Record);
3608
17.9k
      break;
3609
3610
23
    case OPENCL_EXTENSIONS:
3611
1.00k
      for (unsigned I = 0, E = Record.size(); I != E; ) {
3612
979
        auto Name = ReadString(Record, I);
3613
979
        auto &OptInfo = OpenCLExtensions.OptMap[Name];
3614
979
        OptInfo.Supported = Record[I++] != 0;
3615
979
        OptInfo.Enabled = Record[I++] != 0;
3616
979
        OptInfo.WithPragma = Record[I++] != 0;
3617
979
        OptInfo.Avail = Record[I++];
3618
979
        OptInfo.Core = Record[I++];
3619
979
        OptInfo.Opt = Record[I++];
3620
979
      }
3621
23
      break;
3622
3623
275
    case TENTATIVE_DEFINITIONS:
3624
1.72k
      for (unsigned I = 0, N = Record.size(); I != N; 
++I1.45k
)
3625
1.45k
        TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3626
275
      break;
3627
3628
2.64k
    case KNOWN_NAMESPACES:
3629
18.6k
      for (unsigned I = 0, N = Record.size(); I != N; 
++I16.0k
)
3630
16.0k
        KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3631
2.64k
      break;
3632
3633
725
    case UNDEFINED_BUT_USED:
3634
725
      if (UndefinedButUsed.size() % 2 != 0)
3635
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3636
0
                                       "Invalid existing UndefinedButUsed");
3637
3638
725
      if (Record.size() % 2 != 0)
3639
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3640
0
                                       "invalid undefined-but-used record");
3641
3.01k
      
for (unsigned I = 0, N = Record.size(); 725
I != N; /* in loop */) {
3642
2.29k
        UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3643
2.29k
        UndefinedButUsed.push_back(
3644
2.29k
            ReadSourceLocation(F, Record, I).getRawEncoding());
3645
2.29k
      }
3646
725
      break;
3647
3648
1
    case DELETE_EXPRS_TO_ANALYZE:
3649
2
      for (unsigned I = 0, N = Record.size(); I != N;) {
3650
1
        DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3651
1
        const uint64_t Count = Record[I++];
3652
1
        DelayedDeleteExprs.push_back(Count);
3653
2
        for (uint64_t C = 0; C < Count; 
++C1
) {
3654
1
          DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3655
1
          bool IsArrayForm = Record[I++] == 1;
3656
1
          DelayedDeleteExprs.push_back(IsArrayForm);
3657
1
        }
3658
1
      }
3659
1
      break;
3660
3661
64
    case IMPORTED_MODULES:
3662
64
      if (!F.isModule()) {
3663
        // If we aren't loading a module (which has its own exports), make
3664
        // all of the imported modules visible.
3665
        // FIXME: Deal with macros-only imports.
3666
142
        for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3667
78
          unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3668
78
          SourceLocation Loc = ReadSourceLocation(F, Record, I);
3669
78
          if (GlobalID) {
3670
78
            ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3671
78
            if (DeserializationListener)
3672
2
              DeserializationListener->ModuleImportRead(GlobalID, Loc);
3673
78
          }
3674
78
        }
3675
64
      }
3676
64
      break;
3677
3678
17.9k
    case MACRO_OFFSET: {
3679
17.9k
      if (F.LocalNumMacros != 0)
3680
0
        return llvm::createStringError(
3681
0
            std::errc::illegal_byte_sequence,
3682
0
            "duplicate MACRO_OFFSET record in AST file");
3683
17.9k
      F.MacroOffsets = (const uint32_t *)Blob.data();
3684
17.9k
      F.LocalNumMacros = Record[0];
3685
17.9k
      unsigned LocalBaseMacroID = Record[1];
3686
17.9k
      F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3687
17.9k
      F.BaseMacroID = getTotalNumMacros();
3688
3689
17.9k
      if (F.LocalNumMacros > 0) {
3690
        // Introduce the global -> local mapping for macros within this module.
3691
15.0k
        GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3692
3693
        // Introduce the local -> global mapping for macros within this module.
3694
15.0k
        F.MacroRemap.insertOrReplace(
3695
15.0k
          std::make_pair(LocalBaseMacroID,
3696
15.0k
                         F.BaseMacroID - LocalBaseMacroID));
3697
3698
15.0k
        MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3699
15.0k
      }
3700
17.9k
      break;
3701
17.9k
    }
3702
3703
12
    case LATE_PARSED_TEMPLATE:
3704
12
      LateParsedTemplates.emplace_back(
3705
12
          std::piecewise_construct, std::forward_as_tuple(&F),
3706
12
          std::forward_as_tuple(Record.begin(), Record.end()));
3707
12
      break;
3708
3709
3.93k
    case OPTIMIZE_PRAGMA_OPTIONS:
3710
3.93k
      if (Record.size() != 1)
3711
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3712
0
                                       "invalid pragma optimize record");
3713
3.93k
      OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3714
3.93k
      break;
3715
3716
3.93k
    case MSSTRUCT_PRAGMA_OPTIONS:
3717
3.93k
      if (Record.size() != 1)
3718
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3719
0
                                       "invalid pragma ms_struct record");
3720
3.93k
      PragmaMSStructState = Record[0];
3721
3.93k
      break;
3722
3723
3.93k
    case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3724
3.93k
      if (Record.size() != 2)
3725
0
        return llvm::createStringError(
3726
0
            std::errc::illegal_byte_sequence,
3727
0
            "invalid pragma pointers to members record");
3728
3.93k
      PragmaMSPointersToMembersState = Record[0];
3729
3.93k
      PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3730
3.93k
      break;
3731
3732
4
    case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3733
8
      for (unsigned I = 0, N = Record.size(); I != N; 
++I4
)
3734
4
        UnusedLocalTypedefNameCandidates.push_back(
3735
4
            getGlobalDeclID(F, Record[I]));
3736
4
      break;
3737
3738
1
    case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3739
1
      if (Record.size() != 1)
3740
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3741
0
                                       "invalid cuda pragma options record");
3742
1
      ForceCUDAHostDeviceDepth = Record[0];
3743
1
      break;
3744
3745
3.93k
    case ALIGN_PACK_PRAGMA_OPTIONS: {
3746
3.93k
      if (Record.size() < 3)
3747
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3748
0
                                       "invalid pragma pack record");
3749
3.93k
      PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3750
3.93k
      PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3751
3.93k
      unsigned NumStackEntries = Record[2];
3752
3.93k
      unsigned Idx = 3;
3753
      // Reset the stack when importing a new module.
3754
3.93k
      PragmaAlignPackStack.clear();
3755
3.95k
      for (unsigned I = 0; I < NumStackEntries; 
++I14
) {
3756
14
        PragmaAlignPackStackEntry Entry;
3757
14
        Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3758
14
        Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3759
14
        Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3760
14
        PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3761
14
        Entry.SlotLabel = PragmaAlignPackStrings.back();
3762
14
        PragmaAlignPackStack.push_back(Entry);
3763
14
      }
3764
3.93k
      break;
3765
3.93k
    }
3766
3767
3.93k
    case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3768
3.93k
      if (Record.size() < 3)
3769
0
        return llvm::createStringError(std::errc::illegal_byte_sequence,
3770
0
                                       "invalid pragma float control record");
3771
3.93k
      FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3772
3.93k
      FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3773
3.93k
      unsigned NumStackEntries = Record[2];
3774
3.93k
      unsigned Idx = 3;
3775
      // Reset the stack when importing a new module.
3776
3.93k
      FpPragmaStack.clear();
3777
3.93k
      for (unsigned I = 0; I < NumStackEntries; 
++I2
) {
3778
2
        FpPragmaStackEntry Entry;
3779
2
        Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3780
2
        Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3781
2
        Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3782
2
        FpPragmaStrings.push_back(ReadString(Record, Idx));
3783
2
        Entry.SlotLabel = FpPragmaStrings.back();
3784
2
        FpPragmaStack.push_back(Entry);
3785
2
      }
3786
3.93k
      break;
3787
3.93k
    }
3788
3789
1.97k
    case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3790
9.28k
      for (unsigned I = 0, N = Record.size(); I != N; 
++I7.30k
)
3791
7.30k
        DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3792
1.97k
      break;
3793
397k
    }
3794
397k
  }
3795
17.9k
}
3796
3797
9.22k
void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3798
9.22k
  assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3799
3800
  // Additional remapping information.
3801
0
  const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3802
9.22k
  const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3803
9.22k
  F.ModuleOffsetMap = StringRef();
3804
3805
  // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3806
9.22k
  if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3807
4.39k
    F.SLocRemap.insert(std::make_pair(0U, 0));
3808
4.39k
    F.SLocRemap.insert(std::make_pair(2U, 1));
3809
4.39k
  }
3810
3811
  // Continuous range maps we may be updating in our module.
3812
9.22k
  using SLocRemapBuilder =
3813
9.22k
      ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
3814
9.22k
                         2>::Builder;
3815
9.22k
  using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3816
9.22k
  SLocRemapBuilder SLocRemap(F.SLocRemap);
3817
9.22k
  RemapBuilder IdentifierRemap(F.IdentifierRemap);
3818
9.22k
  RemapBuilder MacroRemap(F.MacroRemap);
3819
9.22k
  RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3820
9.22k
  RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3821
9.22k
  RemapBuilder SelectorRemap(F.SelectorRemap);
3822
9.22k
  RemapBuilder DeclRemap(F.DeclRemap);
3823
9.22k
  RemapBuilder TypeRemap(F.TypeRemap);
3824
3825
63.7k
  while (Data < DataEnd) {
3826
    // FIXME: Looking up dependency modules by filename is horrible. Let's
3827
    // start fixing this with prebuilt, explicit and implicit modules and see
3828
    // how it goes...
3829
54.4k
    using namespace llvm::support;
3830
54.4k
    ModuleKind Kind = static_cast<ModuleKind>(
3831
54.4k
      endian::readNext<uint8_t, little, unaligned>(Data));
3832
54.4k
    uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3833
54.4k
    StringRef Name = StringRef((const char*)Data, Len);
3834
54.4k
    Data += Len;
3835
54.4k
    ModuleFile *OM = (Kind == MK_PrebuiltModule || 
Kind == MK_ExplicitModule54.4k
||
3836
54.4k
                              
Kind == MK_ImplicitModule42.8k
3837
54.4k
                          ? 
ModuleMgr.lookupByModuleName(Name)54.3k
3838
54.4k
                          : 
ModuleMgr.lookupByFileName(Name)145
);
3839
54.4k
    if (!OM) {
3840
0
      std::string Msg =
3841
0
          "SourceLocation remap refers to unknown module, cannot find ";
3842
0
      Msg.append(std::string(Name));
3843
0
      Error(Msg);
3844
0
      return;
3845
0
    }
3846
3847
54.4k
    SourceLocation::UIntTy SLocOffset =
3848
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3849
54.4k
    uint32_t IdentifierIDOffset =
3850
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3851
54.4k
    uint32_t MacroIDOffset =
3852
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3853
54.4k
    uint32_t PreprocessedEntityIDOffset =
3854
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3855
54.4k
    uint32_t SubmoduleIDOffset =
3856
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3857
54.4k
    uint32_t SelectorIDOffset =
3858
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3859
54.4k
    uint32_t DeclIDOffset =
3860
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3861
54.4k
    uint32_t TypeIndexOffset =
3862
54.4k
        endian::readNext<uint32_t, little, unaligned>(Data);
3863
3864
54.4k
    auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3865
381k
                         RemapBuilder &Remap) {
3866
381k
      constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3867
381k
      if (Offset != None)
3868
255k
        Remap.insert(std::make_pair(Offset,
3869
255k
                                    static_cast<int>(BaseOffset - Offset)));
3870
381k
    };
3871
3872
54.4k
    constexpr SourceLocation::UIntTy SLocNone =
3873
54.4k
        std::numeric_limits<SourceLocation::UIntTy>::max();
3874
54.4k
    if (SLocOffset != SLocNone)
3875
54.4k
      SLocRemap.insert(std::make_pair(
3876
54.4k
          SLocOffset, static_cast<SourceLocation::IntTy>(
3877
54.4k
                          OM->SLocEntryBaseOffset - SLocOffset)));
3878
3879
54.4k
    mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3880
54.4k
    mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3881
54.4k
    mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3882
54.4k
              PreprocessedEntityRemap);
3883
54.4k
    mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3884
54.4k
    mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3885
54.4k
    mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3886
54.4k
    mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3887
3888
    // Global -> local mappings.
3889
54.4k
    F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3890
54.4k
  }
3891
9.22k
}
3892
3893
ASTReader::ASTReadResult
3894
ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3895
                                  const ModuleFile *ImportedBy,
3896
13.9k
                                  unsigned ClientLoadCapabilities) {
3897
13.9k
  unsigned Idx = 0;
3898
13.9k
  F.ModuleMapPath = ReadPath(F, Record, Idx);
3899
3900
  // Try to resolve ModuleName in the current header search context and
3901
  // verify that it is found in the same module map file as we saved. If the
3902
  // top-level AST file is a main file, skip this check because there is no
3903
  // usable header search context.
3904
13.9k
  assert(!F.ModuleName.empty() &&
3905
13.9k
         "MODULE_NAME should come before MODULE_MAP_FILE");
3906
13.9k
  if (F.Kind == MK_ImplicitModule && 
ModuleMgr.begin()->Kind != MK_MainFile12.6k
) {
3907
    // An implicitly-loaded module file should have its module listed in some
3908
    // module map file that we've already loaded.
3909
12.6k
    Module *M =
3910
12.6k
        PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
3911
12.6k
    auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3912
12.6k
    const FileEntry *ModMap = M ? 
Map.getModuleMapFileForUniquing(M)12.6k
:
nullptr2
;
3913
    // Don't emit module relocation error if we have -fno-validate-pch
3914
12.6k
    if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3915
12.6k
              DisableValidationForModuleKind::Module) &&
3916
12.6k
        
!ModMap12.6k
) {
3917
4
      if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3918
4
        if (auto ASTFE = M ? M->getASTFile() : None) {
3919
          // This module was defined by an imported (explicit) module.
3920
1
          Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3921
1
                                               << ASTFE->getName();
3922
3
        } else {
3923
          // This module was built with a different module map.
3924
3
          Diag(diag::err_imported_module_not_found)
3925
3
              << F.ModuleName << F.FileName
3926
3
              << (ImportedBy ? ImportedBy->FileName : 
""0
) << F.ModuleMapPath
3927
3
              << !ImportedBy;
3928
          // In case it was imported by a PCH, there's a chance the user is
3929
          // just missing to include the search path to the directory containing
3930
          // the modulemap.
3931
3
          if (ImportedBy && ImportedBy->Kind == MK_PCH)
3932
2
            Diag(diag::note_imported_by_pch_module_not_found)
3933
2
                << llvm::sys::path::parent_path(F.ModuleMapPath);
3934
3
        }
3935
4
      }
3936
4
      return OutOfDate;
3937
4
    }
3938
3939
12.6k
    assert(M && M->Name == F.ModuleName && "found module with different name");
3940
3941
    // Check the primary module map file.
3942
0
    auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3943
12.6k
    if (
!StoredModMap12.6k
|| *StoredModMap != ModMap) {
3944
0
      assert(ModMap && "found module is missing module map file");
3945
0
      assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3946
0
             "top-level import should be verified");
3947
0
      bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3948
0
      if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3949
0
        Diag(diag::err_imported_module_modmap_changed)
3950
0
            << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3951
0
            << ModMap->getName() << F.ModuleMapPath << NotImported;
3952
0
      return OutOfDate;
3953
0
    }
3954
3955
12.6k
    llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3956
12.6k
    for (unsigned I = 0, N = Record[Idx++]; I < N; 
++I54
) {
3957
      // FIXME: we should use input files rather than storing names.
3958
55
      std::string Filename = ReadPath(F, Record, Idx);
3959
55
      auto SF = FileMgr.getFile(Filename, false, false);
3960
55
      if (!SF) {
3961
1
        if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3962
0
          Error("could not find file '" + Filename +"' referenced by AST file");
3963
1
        return OutOfDate;
3964
1
      }
3965
54
      AdditionalStoredMaps.insert(*SF);
3966
54
    }
3967
3968
    // Check any additional module map files (e.g. module.private.modulemap)
3969
    // that are not in the pcm.
3970
12.6k
    if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3971
55
      for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3972
        // Remove files that match
3973
        // Note: SmallPtrSet::erase is really remove
3974
55
        if (!AdditionalStoredMaps.erase(ModMap)) {
3975
1
          if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3976
0
            Diag(diag::err_module_different_modmap)
3977
0
              << F.ModuleName << /*new*/0 << ModMap->getName();
3978
1
          return OutOfDate;
3979
1
        }
3980
55
      }
3981
55
    }
3982
3983
    // Check any additional module map files that are in the pcm, but not
3984
    // found in header search. Cases that match are already removed.
3985
12.6k
    for (const FileEntry *ModMap : AdditionalStoredMaps) {
3986
0
      if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3987
0
        Diag(diag::err_module_different_modmap)
3988
0
          << F.ModuleName << /*not new*/1 << ModMap->getName();
3989
0
      return OutOfDate;
3990
0
    }
3991
12.6k
  }
3992
3993
13.9k
  if (Listener)
3994
13.9k
    Listener->ReadModuleMapFile(F.ModuleMapPath);
3995
13.9k
  return Success;
3996
13.9k
}
3997
3998
/// Move the given method to the back of the global list of methods.
3999
31
static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4000
  // Find the entry for this selector in the method pool.
4001
31
  Sema::GlobalMethodPool::iterator Known
4002
31
    = S.MethodPool.find(Method->getSelector());
4003
31
  if (Known == S.MethodPool.end())
4004
4
    return;
4005
4006
  // Retrieve the appropriate method list.
4007
27
  ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4008
27
                                                    : 
Known->second.second0
;
4009
27
  bool Found = false;
4010
290
  for (ObjCMethodList *List = &Start; List; 
List = List->getNext()263
) {
4011
263
    if (!Found) {
4012
138
      if (List->getMethod() == Method) {
4013
27
        Found = true;
4014
111
      } else {
4015
        // Keep searching.
4016
111
        continue;
4017
111
      }
4018
138
    }
4019
4020
152
    if (List->getNext())
4021
125
      List->setMethod(List->getNext()->getMethod());
4022
27
    else
4023
27
      List->setMethod(Method);
4024
152
  }
4025
27
}
4026
4027
9.24k
void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4028
9.24k
  assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4029
22.8k
  for (Decl *D : Names) {
4030
22.8k
    bool wasHidden = !D->isUnconditionallyVisible();
4031
22.8k
    D->setVisibleDespiteOwningModule();
4032
4033
22.8k
    if (wasHidden && 
SemaObj22.7k
) {
4034
22.7k
      if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4035
31
        moveMethodToBackOfGlobalList(*SemaObj, Method);
4036
31
      }
4037
22.7k
    }
4038
22.8k
  }
4039
9.24k
}
4040
4041
void ASTReader::makeModuleVisible(Module *Mod,
4042
                                  Module::NameVisibilityKind NameVisibility,
4043
120k
                                  SourceLocation ImportLoc) {
4044
120k
  llvm::SmallPtrSet<Module *, 4> Visited;
4045
120k
  SmallVector<Module *, 4> Stack;
4046
120k
  Stack.push_back(Mod);
4047
1.09M
  while (!Stack.empty()) {
4048
977k
    Mod = Stack.pop_back_val();
4049
4050
977k
    if (NameVisibility <= Mod->NameVisibility) {
4051
      // This module already has this level of visibility (or greater), so
4052
      // there is nothing more to do.
4053
133k
      continue;
4054
133k
    }
4055
4056
843k
    if (Mod->isUnimportable()) {
4057
      // Modules that aren't importable cannot be made visible.
4058
3.85k
      continue;
4059
3.85k
    }
4060
4061
    // Update the module's name visibility.
4062
839k
    Mod->NameVisibility = NameVisibility;
4063
4064
    // If we've already deserialized any names from this module,
4065
    // mark them as visible.
4066
839k
    HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4067
839k
    if (Hidden != HiddenNamesMap.end()) {
4068
9.24k
      auto HiddenNames = std::move(*Hidden);
4069
9.24k
      HiddenNamesMap.erase(Hidden);
4070
9.24k
      makeNamesVisible(HiddenNames.second, HiddenNames.first);
4071
9.24k
      assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4072
9.24k
             "making names visible added hidden names");
4073
9.24k
    }
4074
4075
    // Push any exported modules onto the stack to be marked as visible.
4076
0
    SmallVector<Module *, 16> Exports;
4077
839k
    Mod->getExportedModules(Exports);
4078
839k
    for (SmallVectorImpl<Module *>::iterator
4079
3.62M
           I = Exports.begin(), E = Exports.end(); I != E; 
++I2.78M
) {
4080
2.78M
      Module *Exported = *I;
4081
2.78M
      if (Visited.insert(Exported).second)
4082
856k
        Stack.push_back(Exported);
4083
2.78M
    }
4084
839k
  }
4085
120k
}
4086
4087
/// We've merged the definition \p MergedDef into the existing definition
4088
/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4089
/// visible.
4090
void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4091
1.59k
                                          NamedDecl *MergedDef) {
4092
1.59k
  if (!Def->isUnconditionallyVisible()) {
4093
    // If MergedDef is visible or becomes visible, make the definition visible.
4094
326
    if (MergedDef->isUnconditionallyVisible())
4095
52
      Def->setVisibleDespiteOwningModule();
4096
274
    else {
4097
274
      getContext().mergeDefinitionIntoModule(
4098
274
          Def, MergedDef->getImportedOwningModule(),
4099
274
          /*NotifyListeners*/ false);
4100
274
      PendingMergedDefinitionsToDeduplicate.insert(Def);
4101
274
    }
4102
326
  }
4103
1.59k
}
4104
4105
3.92M
bool ASTReader::loadGlobalIndex() {
4106
3.92M
  if (GlobalIndex)
4107
556k
    return false;
4108
4109
3.37M
  if (TriedLoadingGlobalIndex || 
!UseGlobalIndex225k
||
4110
3.37M
      
!PP.getLangOpts().Modules222k
)
4111
3.36M
    return true;
4112
4113
  // Try to load the global index.
4114
1.79k
  TriedLoadingGlobalIndex = true;
4115
1.79k
  StringRef ModuleCachePath
4116
1.79k
    = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4117
1.79k
  std::pair<GlobalModuleIndex *, llvm::Error> Result =
4118
1.79k
      GlobalModuleIndex::readIndex(ModuleCachePath);
4119
1.79k
  if (llvm::Error Err = std::move(Result.second)) {
4120
1.15k
    assert(!Result.first);
4121
0
    consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4122
1.15k
    return true;
4123
1.15k
  }
4124
4125
644
  GlobalIndex.reset(Result.first);
4126
644
  ModuleMgr.setGlobalIndex(GlobalIndex.get());
4127
644
  return false;
4128
1.79k
}
4129
4130
10.5k
bool ASTReader::isGlobalIndexUnavailable() const {
4131
10.5k
  return PP.getLangOpts().Modules && 
UseGlobalIndex6.95k
&&
4132
10.5k
         
!hasGlobalIndex()6.95k
&&
TriedLoadingGlobalIndex6.39k
;
4133
10.5k
}
4134
4135
17
static void updateModuleTimestamp(ModuleFile &MF) {
4136
  // Overwrite the timestamp file contents so that file's mtime changes.
4137
17
  std::string TimestampFilename = MF.getTimestampFilename();
4138
17
  std::error_code EC;
4139
17
  llvm::raw_fd_ostream OS(TimestampFilename, EC,
4140
17
                          llvm::sys::fs::OF_TextWithCRLF);
4141
17
  if (EC)
4142
0
    return;
4143
17
  OS << "Timestamp file\n";
4144
17
  OS.close();
4145
17
  OS.clear_error(); // Avoid triggering a fatal error.
4146
17
}
4147
4148
/// Given a cursor at the start of an AST file, scan ahead and drop the
4149
/// cursor into the start of the given block ID, returning false on success and
4150
/// true on failure.
4151
48.5k
static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4152
150k
  while (true) {
4153
150k
    Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4154
150k
    if (!MaybeEntry) {
4155
      // FIXME this drops errors on the floor.
4156
0
      consumeError(MaybeEntry.takeError());
4157
0
      return true;
4158
0
    }
4159
150k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
4160
4161
150k
    switch (Entry.Kind) {
4162
17.9k
    case llvm::BitstreamEntry::Error:
4163
17.9k
    case llvm::BitstreamEntry::EndBlock:
4164
17.9k
      return true;
4165
4166
0
    case llvm::BitstreamEntry::Record:
4167
      // Ignore top-level records.
4168
0
      if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4169
0
        break;
4170
0
      else {
4171
        // FIXME this drops errors on the floor.
4172
0
        consumeError(Skipped.takeError());
4173
0
        return true;
4174
0
      }
4175
4176
132k
    case llvm::BitstreamEntry::SubBlock:
4177
132k
      if (Entry.ID == BlockID) {
4178
30.5k
        if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4179
          // FIXME this drops the error on the floor.
4180
0
          consumeError(std::move(Err));
4181
0
          return true;
4182
0
        }
4183
        // Found it!
4184
30.5k
        return false;
4185
30.5k
      }
4186
4187
102k
      if (llvm::Error Err = Cursor.SkipBlock()) {
4188
        // FIXME this drops the error on the floor.
4189
0
        consumeError(std::move(Err));
4190
0
        return true;
4191
0
      }
4192
150k
    }
4193
150k
  }
4194
48.5k
}
4195
4196
ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4197
                                            ModuleKind Type,
4198
                                            SourceLocation ImportLoc,
4199
                                            unsigned ClientLoadCapabilities,
4200
10.2k
                                            SmallVectorImpl<ImportedSubmodule> *Imported) {
4201
10.2k
  llvm::SaveAndRestore<SourceLocation>
4202
10.2k
    SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4203
10.2k
  llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4204
10.2k
      CurrentDeserializingModuleKind, Type);
4205
4206
  // Defer any pending actions until we get to the end of reading the AST file.
4207
10.2k
  Deserializing AnASTFile(this);
4208
4209
  // Bump the generation number.
4210
10.2k
  unsigned PreviousGeneration = 0;
4211
10.2k
  if (ContextObj)
4212
10.2k
    PreviousGeneration = incrementGeneration(*ContextObj);
4213
4214
10.2k
  unsigned NumModules = ModuleMgr.size();
4215
10.2k
  SmallVector<ImportedModule, 4> Loaded;
4216
10.2k
  if (ASTReadResult ReadResult =
4217
10.2k
          ReadASTCore(FileName, Type, ImportLoc,
4218
10.2k
                      /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4219
10.2k
                      ClientLoadCapabilities)) {
4220
1.73k
    ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4221
1.73k
                            PP.getLangOpts().Modules
4222
1.73k
                                ? 
&PP.getHeaderSearchInfo().getModuleMap()1.71k
4223
1.73k
                                : 
nullptr19
);
4224
4225
    // If we find that any modules are unusable, the global index is going
4226
    // to be out-of-date. Just remove it.
4227
1.73k
    GlobalIndex.reset();
4228
1.73k
    ModuleMgr.setGlobalIndex(nullptr);
4229
1.73k
    return ReadResult;
4230
1.73k
  }
4231
4232
  // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4233
  // remove modules from this point. Various fields are updated during reading
4234
  // the AST block and removing the modules would result in dangling pointers.
4235
  // They are generally only incidentally dereferenced, ie. a binary search
4236
  // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4237
  // be dereferenced but it wouldn't actually be used.
4238
4239
  // Load the AST blocks of all of the modules that we loaded. We can still
4240
  // hit errors parsing the ASTs at this point.
4241
17.9k
  
for (ImportedModule &M : Loaded)8.56k
{
4242
17.9k
    ModuleFile &F = *M.Mod;
4243
4244
    // Read the AST block.
4245
17.9k
    if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4246
4
      Error(std::move(Err));
4247
4
      return Failure;
4248
4
    }
4249
4250
    // The AST block should always have a definition for the main module.
4251
17.9k
    if (F.isModule() && 
!F.DidReadTopLevelSubmodule13.9k
) {
4252
0
      Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4253
0
      return Failure;
4254
0
    }
4255
4256
    // Read the extension blocks.
4257
17.9k
    
while (17.9k
!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4258
18
      if (llvm::Error Err = ReadExtensionBlock(F)) {
4259
0
        Error(std::move(Err));
4260
0
        return Failure;
4261
0
      }
4262
18
    }
4263
4264
    // Once read, set the ModuleFile bit base offset and update the size in
4265
    // bits of all files we've seen.
4266
17.9k
    F.GlobalBitOffset = TotalModulesSizeInBits;
4267
17.9k
    TotalModulesSizeInBits += F.SizeInBits;
4268
17.9k
    GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4269
17.9k
  }
4270
4271
  // Preload source locations and interesting indentifiers.
4272
17.9k
  
for (ImportedModule &M : Loaded)8.56k
{
4273
17.9k
    ModuleFile &F = *M.Mod;
4274
4275
    // Preload SLocEntries.
4276
35.9k
    for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; 
++I17.9k
) {
4277
17.9k
      int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4278
      // Load it through the SourceManager and don't call ReadSLocEntry()
4279
      // directly because the entry may have already been loaded in which case
4280
      // calling ReadSLocEntry() directly would trigger an assertion in
4281
      // SourceManager.
4282
17.9k
      SourceMgr.getLoadedSLocEntryByID(Index);
4283
17.9k
    }
4284
4285
    // Map the original source file ID into the ID space of the current
4286
    // compilation.
4287
17.9k
    if (F.OriginalSourceFileID.isValid()) {
4288
17.9k
      F.OriginalSourceFileID = FileID::get(
4289
17.9k
          F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4290
17.9k
    }
4291
4292
    // Preload all the pending interesting identifiers by marking them out of
4293
    // date.
4294
14.5M
    for (auto Offset : F.PreloadIdentifierOffsets) {
4295
14.5M
      const unsigned char *Data = F.IdentifierTableData + Offset;
4296
4297
14.5M
      ASTIdentifierLookupTrait Trait(*this, F);
4298
14.5M
      auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4299
14.5M
      auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4300
14.5M
      auto &II = PP.getIdentifierTable().getOwn(Key);
4301
14.5M
      II.setOutOfDate(true);
4302
4303
      // Mark this identifier as being from an AST file so that we can track
4304
      // whether we need to serialize it.
4305
14.5M
      markIdentifierFromAST(*this, II);
4306
4307
      // Associate the ID with the identifier so that the writer can reuse it.
4308
14.5M
      auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4309
14.5M
      SetIdentifierInfo(ID, &II);
4310
14.5M
    }
4311
17.9k
  }
4312
4313
  // Setup the import locations and notify the module manager that we've
4314
  // committed to these module files.
4315
17.9k
  for (ImportedModule &M : Loaded) {
4316
17.9k
    ModuleFile &F = *M.Mod;
4317
4318
17.9k
    ModuleMgr.moduleFileAccepted(&F);
4319
4320
    // Set the import location.
4321
17.9k
    F.DirectImportLoc = ImportLoc;
4322
    // FIXME: We assume that locations from PCH / preamble do not need
4323
    // any translation.
4324
17.9k
    if (!M.ImportedBy)
4325
8.25k
      F.ImportLoc = M.ImportLoc;
4326
9.69k
    else
4327
9.69k
      F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4328
17.9k
  }
4329
4330
8.56k
  if (!PP.getLangOpts().CPlusPlus ||
4331
8.56k
      
(6.34k
Type != MK_ImplicitModule6.34k
&&
Type != MK_ExplicitModule3.58k
&&
4332
6.34k
       
Type != MK_PrebuiltModule3.11k
)) {
4333
    // Mark all of the identifiers in the identifier table as being out of date,
4334
    // so that various accessors know to check the loaded modules when the
4335
    // identifier is used.
4336
    //
4337
    // For C++ modules, we don't need information on many identifiers (just
4338
    // those that provide macros or are poisoned), so we mark all of
4339
    // the interesting ones via PreloadIdentifierOffsets.
4340
5.30k
    for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4341
5.30k
                                IdEnd = PP.getIdentifierTable().end();
4342
6.49M
         Id != IdEnd; 
++Id6.48M
)
4343
6.48M
      Id->second->setOutOfDate(true);
4344
5.30k
  }
4345
  // Mark selectors as out of date.
4346
8.56k
  for (auto Sel : SelectorGeneration)
4347
96.2k
    SelectorOutOfDate[Sel.first] = true;
4348
4349
  // Resolve any unresolved module exports.
4350
6.92M
  for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; 
++I6.91M
) {
4351
6.91M
    UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4352
6.91M
    SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4353
6.91M
    Module *ResolvedMod = getSubmodule(GlobalID);
4354
4355
6.91M
    switch (Unresolved.Kind) {
4356
1
    case UnresolvedModuleRef::Conflict:
4357
1
      if (ResolvedMod) {
4358
1
        Module::Conflict Conflict;
4359
1
        Conflict.Other = ResolvedMod;
4360
1
        Conflict.Message = Unresolved.String.str();
4361
1
        Unresolved.Mod->Conflicts.push_back(Conflict);
4362
1
      }
4363
1
      continue;
4364
4365
5.88M
    case UnresolvedModuleRef::Import:
4366
5.88M
      if (ResolvedMod)
4367
5.88M
        Unresolved.Mod->Imports.insert(ResolvedMod);
4368
5.88M
      continue;
4369
4370
1.03M
    case UnresolvedModuleRef::Export:
4371
1.03M
      if (ResolvedMod || 
Unresolved.IsWildcard961k
)
4372
1.03M
        Unresolved.Mod->Exports.push_back(
4373
1.03M
          Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4374
1.03M
      continue;
4375
6.91M
    }
4376
6.91M
  }
4377
8.56k
  UnresolvedModuleRefs.clear();
4378
4379
8.56k
  if (Imported)
4380
0
    Imported->append(ImportedModules.begin(),
4381
0
                     ImportedModules.end());
4382
4383
  // FIXME: How do we load the 'use'd modules? They may not be submodules.
4384
  // Might be unnecessary as use declarations are only used to build the
4385
  // module itself.
4386
4387
8.56k
  if (ContextObj)
4388
8.54k
    InitializeContext();
4389
4390
8.56k
  if (SemaObj)
4391
4.16k
    UpdateSema();
4392
4393
8.56k
  if (DeserializationListener)
4394
1.56k
    DeserializationListener->ReaderInitialized(this);
4395
4396
8.56k
  ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4397
8.56k
  if (PrimaryModule.OriginalSourceFileID.isValid()) {
4398
    // If this AST file is a precompiled preamble, then set the
4399
    // preamble file ID of the source manager to the file source file
4400
    // from which the preamble was built.
4401
8.55k
    if (Type == MK_Preamble) {
4402
334
      SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4403
8.22k
    } else if (Type == MK_MainFile) {
4404
148
      SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4405
148
    }
4406
8.55k
  }
4407
4408
  // For any Objective-C class definitions we have already loaded, make sure
4409
  // that we load any additional categories.
4410
8.56k
  if (ContextObj) {
4411
11.8k
    for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; 
++I3.32k
) {
4412
3.32k
      loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4413
3.32k
                         ObjCClassesLoaded[I],
4414
3.32k
                         PreviousGeneration);
4415
3.32k
    }
4416
8.54k
  }
4417
4418
8.56k
  if (PP.getHeaderSearchInfo()
4419
8.56k
          .getHeaderSearchOpts()
4420
8.56k
          .ModulesValidateOncePerBuildSession) {
4421
    // Now we are certain that the module and all modules it depends on are
4422
    // up to date.  Create or update timestamp files for modules that are
4423
    // located in the module cache (not for PCH files that could be anywhere
4424
    // in the filesystem).
4425
34
    for (unsigned I = 0, N = Loaded.size(); I != N; 
++I17
) {
4426
17
      ImportedModule &M = Loaded[I];
4427
17
      if (M.Mod->Kind == MK_ImplicitModule) {
4428
17
        updateModuleTimestamp(*M.Mod);
4429
17
      }
4430
17
    }
4431
17
  }
4432
4433
8.56k
  return Success;
4434
8.56k
}
4435
4436
static ASTFileSignature readASTFileSignature(StringRef PCH);
4437
4438
/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4439
48.6k
static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4440
  // FIXME checking magic headers is done in other places such as
4441
  // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4442
  // always done the same. Unify it all with a helper.
4443
48.6k
  if (!Stream.canSkipToPos(4))
4444
7
    return llvm::createStringError(std::errc::illegal_byte_sequence,
4445
7
                                   "file too small to contain AST file magic");
4446
48.6k
  for (unsigned C : {'C', 'P', 'C', 'H'})
4447
194k
    if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4448
194k
      if (Res.get() != C)
4449
0
        return llvm::createStringError(
4450
0
            std::errc::illegal_byte_sequence,
4451
0
            "file doesn't start with AST file magic");
4452
194k
    } else
4453
6
      return Res.takeError();
4454
48.6k
  return llvm::Error::success();
4455
48.6k
}
4456
4457
20
static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4458
20
  switch (Kind) {
4459
9
  case MK_PCH:
4460
9
    return 0; // PCH
4461
5
  case MK_ImplicitModule:
4462
10
  case MK_ExplicitModule:
4463
10
  case MK_PrebuiltModule:
4464
10
    return 1; // module
4465
1
  case MK_MainFile:
4466
1
  case MK_Preamble:
4467
1
    return 2; // main source file
4468
20
  }
4469
0
  llvm_unreachable("unknown module kind");
4470
0
}
4471
4472
ASTReader::ASTReadResult
4473
ASTReader::ReadASTCore(StringRef FileName,
4474
                       ModuleKind Type,
4475
                       SourceLocation ImportLoc,
4476
                       ModuleFile *ImportedBy,
4477
                       SmallVectorImpl<ImportedModule> &Loaded,
4478
                       off_t ExpectedSize, time_t ExpectedModTime,
4479
                       ASTFileSignature ExpectedSignature,
4480
34.0k
                       unsigned ClientLoadCapabilities) {
4481
34.0k
  ModuleFile *M;
4482
34.0k
  std::string ErrorStr;
4483
34.0k
  ModuleManager::AddModuleResult AddResult
4484
34.0k
    = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4485
34.0k
                          getGeneration(), ExpectedSize, ExpectedModTime,
4486
34.0k
                          ExpectedSignature, readASTFileSignature,
4487
34.0k
                          M, ErrorStr);
4488
4489
34.0k
  switch (AddResult) {
4490
14.3k
  case ModuleManager::AlreadyLoaded:
4491
14.3k
    Diag(diag::remark_module_import)
4492
14.3k
        << M->ModuleName << M->FileName << (ImportedBy ? 
true14.0k
:
false303
)
4493
14.3k
        << (ImportedBy ? 
StringRef(ImportedBy->ModuleName)14.0k
:
StringRef()303
);
4494
14.3k
    return Success;
4495
4496
18.1k
  case ModuleManager::NewlyLoaded:
4497
    // Load module file below.
4498
18.1k
    break;
4499
4500
1.58k
  case ModuleManager::Missing:
4501
    // The module file was missing; if the client can handle that, return
4502
    // it.
4503
1.58k
    if (ClientLoadCapabilities & ARR_Missing)
4504
1.58k
      return Missing;
4505
4506
    // Otherwise, return an error.
4507
3
    Diag(diag::err_ast_file_not_found)
4508
3
        << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4509
3
        << ErrorStr;
4510
3
    return Failure;
4511
4512
32
  case ModuleManager::OutOfDate:
4513
    // We couldn't load the module file because it is out-of-date. If the
4514
    // client can handle out-of-date, return it.
4515
32
    if (ClientLoadCapabilities & ARR_OutOfDate)
4516
31
      return OutOfDate;
4517
4518
    // Otherwise, return an error.
4519
1
    Diag(diag::err_ast_file_out_of_date)
4520
1
        << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4521
1
        << ErrorStr;
4522
1
    return Failure;
4523
34.0k
  }
4524
4525
18.1k
  assert(M && "Missing module file");
4526
4527
0
  bool ShouldFinalizePCM = false;
4528
18.1k
  auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4529
18.1k
    auto &MC = getModuleManager().getModuleCache();
4530
18.1k
    if (ShouldFinalizePCM)
4531
17.9k
      MC.finalizePCM(FileName);
4532
159
    else
4533
159
      MC.tryToDropPCM(FileName);
4534
18.1k
  });
4535
18.1k
  ModuleFile &F = *M;
4536
18.1k
  BitstreamCursor &Stream = F.Stream;
4537
18.1k
  Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4538
18.1k
  F.SizeInBits = F.Buffer->getBufferSize() * 8;
4539
4540
  // Sniff for the signature.
4541
18.1k
  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4542
5
    Diag(diag::err_ast_file_invalid)
4543
5
        << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4544
5
    return Failure;
4545
5
  }
4546
4547
  // This is used for compatibility with older PCH formats.
4548
18.1k
  bool HaveReadControlBlock = false;
4549
54.1k
  while (true) {
4550
54.1k
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4551
54.1k
    if (!MaybeEntry) {
4552
0
      Error(MaybeEntry.takeError());
4553
0
      return Failure;
4554
0
    }
4555
54.1k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
4556
4557
54.1k
    switch (Entry.Kind) {
4558
0
    case llvm::BitstreamEntry::Error:
4559
0
    case llvm::BitstreamEntry::Record:
4560
0
    case llvm::BitstreamEntry::EndBlock:
4561
0
      Error("invalid record at top-level of AST file");
4562
0
      return Failure;
4563
4564
54.1k
    case llvm::BitstreamEntry::SubBlock:
4565
54.1k
      break;
4566
54.1k
    }
4567
4568
54.1k
    switch (Entry.ID) {
4569
18.1k
    case CONTROL_BLOCK_ID:
4570
18.1k
      HaveReadControlBlock = true;
4571
18.1k
      switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4572
17.9k
      case Success:
4573
        // Check that we didn't try to load a non-module AST file as a module.
4574
        //
4575
        // FIXME: Should we also perform the converse check? Loading a module as
4576
        // a PCH file sort of works, but it's a bit wonky.
4577
17.9k
        if ((Type == MK_ImplicitModule || 
Type == MK_ExplicitModule5.37k
||
4578
17.9k
             
Type == MK_PrebuiltModule4.02k
) &&
4579
17.9k
            
F.ModuleName.empty()13.9k
) {
4580
1
          auto Result = (Type == MK_ImplicitModule) ? 
OutOfDate0
: Failure;
4581
1
          if (Result != OutOfDate ||
4582
1
              
(ClientLoadCapabilities & ARR_OutOfDate) == 00
)
4583
1
            Diag(diag::err_module_file_not_module) << FileName;
4584
1
          return Result;
4585
1
        }
4586
17.9k
        break;
4587
4588
17.9k
      
case Failure: return Failure5
;
4589
0
      case Missing: return Missing;
4590
120
      case OutOfDate: return OutOfDate;
4591
0
      case VersionMismatch: return VersionMismatch;
4592
24
      case ConfigurationMismatch: return ConfigurationMismatch;
4593
4
      case HadErrors: return HadErrors;
4594
18.1k
      }
4595
17.9k
      break;
4596
4597
17.9k
    case AST_BLOCK_ID:
4598
17.9k
      if (!HaveReadControlBlock) {
4599
0
        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4600
0
          Diag(diag::err_pch_version_too_old);
4601
0
        return VersionMismatch;
4602
0
      }
4603
4604
      // Record that we've loaded this module.
4605
17.9k
      Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4606
17.9k
      ShouldFinalizePCM = true;
4607
17.9k
      return Success;
4608
4609
0
    case UNHASHED_CONTROL_BLOCK_ID:
4610
      // This block is handled using look-ahead during ReadControlBlock.  We
4611
      // shouldn't get here!
4612
0
      Error("malformed block record in AST file");
4613
0
      return Failure;
4614
4615
18.1k
    default:
4616
18.1k
      if (llvm::Error Err = Stream.SkipBlock()) {
4617
0
        Error(std::move(Err));
4618
0
        return Failure;
4619
0
      }
4620
18.1k
      break;
4621
54.1k
    }
4622
54.1k
  }
4623
4624
1
  llvm_unreachable("unexpected break; expected return");
4625
1
}
4626
4627
ASTReader::ASTReadResult
4628
ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4629
18.0k
                                    unsigned ClientLoadCapabilities) {
4630
18.0k
  const HeaderSearchOptions &HSOpts =
4631
18.0k
      PP.getHeaderSearchInfo().getHeaderSearchOpts();
4632
18.0k
  bool AllowCompatibleConfigurationMismatch =
4633
18.0k
      F.Kind == MK_ExplicitModule || 
F.Kind == MK_PrebuiltModule16.7k
;
4634
18.0k
  bool DisableValidation = shouldDisableValidationForFile(F);
4635
4636
18.0k
  ASTReadResult Result = readUnhashedControlBlockImpl(
4637
18.0k
      &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4638
18.0k
      Listener.get(),
4639
18.0k
      WasImportedBy ? 
false9.74k
:
HSOpts.ModulesValidateDiagnosticOptions8.34k
);
4640
4641
  // If F was directly imported by another module, it's implicitly validated by
4642
  // the importing module.
4643
18.0k
  if (DisableValidation || 
WasImportedBy17.5k
||
4644
18.0k
      
(7.94k
AllowConfigurationMismatch7.94k
&&
Result == ConfigurationMismatch7
))
4645
10.1k
    return Success;
4646
4647
7.94k
  if (Result == Failure) {
4648
0
    Error("malformed block record in AST file");
4649
0
    return Failure;
4650
0
  }
4651
4652
7.94k
  if (Result == OutOfDate && 
F.Kind == MK_ImplicitModule13
) {
4653
    // If this module has already been finalized in the ModuleCache, we're stuck
4654
    // with it; we can only load a single version of each module.
4655
    //
4656
    // This can happen when a module is imported in two contexts: in one, as a
4657
    // user module; in another, as a system module (due to an import from
4658
    // another module marked with the [system] flag).  It usually indicates a
4659
    // bug in the module map: this module should also be marked with [system].
4660
    //
4661
    // If -Wno-system-headers (the default), and the first import is as a
4662
    // system module, then validation will fail during the as-user import,
4663
    // since -Werror flags won't have been validated.  However, it's reasonable
4664
    // to treat this consistently as a system module.
4665
    //
4666
    // If -Wsystem-headers, the PCM on disk was built with
4667
    // -Wno-system-headers, and the first import is as a user module, then
4668
    // validation will fail during the as-system import since the PCM on disk
4669
    // doesn't guarantee that -Werror was respected.  However, the -Werror
4670
    // flags were checked during the initial as-user import.
4671
13
    if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4672
2
      Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4673
2
      return Success;
4674
2
    }
4675
13
  }
4676
4677
7.94k
  return Result;
4678
7.94k
}
4679
4680
ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4681
    ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4682
    bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4683
18.1k
    bool ValidateDiagnosticOptions) {
4684
  // Initialize a stream.
4685
18.1k
  BitstreamCursor Stream(StreamData);
4686
4687
  // Sniff for the signature.
4688
18.1k
  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4689
    // FIXME this drops the error on the floor.
4690
0
    consumeError(std::move(Err));
4691
0
    return Failure;
4692
0
  }
4693
4694
  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4695
18.1k
  if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4696
0
    return Failure;
4697
4698
  // Read all of the records in the options block.
4699
18.1k
  RecordData Record;
4700
18.1k
  ASTReadResult Result = Success;
4701
97.9k
  while (true) {
4702
97.9k
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4703
97.9k
    if (!MaybeEntry) {
4704
      // FIXME this drops the error on the floor.
4705
0
      consumeError(MaybeEntry.takeError());
4706
0
      return Failure;
4707
0
    }
4708
97.9k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
4709
4710
97.9k
    switch (Entry.Kind) {
4711
0
    case llvm::BitstreamEntry::Error:
4712
0
    case llvm::BitstreamEntry::SubBlock:
4713
0
      return Failure;
4714
4715
18.1k
    case llvm::BitstreamEntry::EndBlock:
4716
18.1k
      return Result;
4717
4718
79.8k
    case llvm::BitstreamEntry::Record:
4719
      // The interesting case.
4720
79.8k
      break;
4721
97.9k
    }
4722
4723
    // Read and process a record.
4724
79.8k
    Record.clear();
4725
79.8k
    StringRef Blob;
4726
79.8k
    Expected<unsigned> MaybeRecordType =
4727
79.8k
        Stream.readRecord(Entry.ID, Record, &Blob);
4728
79.8k
    if (!MaybeRecordType) {
4729
      // FIXME this drops the error.
4730
0
      return Failure;
4731
0
    }
4732
79.8k
    switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4733
12.7k
    case SIGNATURE:
4734
12.7k
      if (F)
4735
12.7k
        F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4736
12.7k
      break;
4737
12.7k
    case AST_BLOCK_HASH:
4738
12.7k
      if (F)
4739
12.7k
        F->ASTBlockHash =
4740
12.7k
            ASTFileSignature::create(Record.begin(), Record.end());
4741
12.7k
      break;
4742
18.1k
    case DIAGNOSTIC_OPTIONS: {
4743
18.1k
      bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4744
18.1k
      if (Listener && ValidateDiagnosticOptions &&
4745
18.1k
          
!AllowCompatibleConfigurationMismatch8.35k
&&
4746
18.1k
          
ParseDiagnosticOptions(Record, Complain, *Listener)7.80k
)
4747
13
        Result = OutOfDate; // Don't return early.  Read the signature.
4748
18.1k
      break;
4749
0
    }
4750
18.1k
    case DIAG_PRAGMA_MAPPINGS:
4751
18.1k
      if (!F)
4752
24
        break;
4753
18.0k
      if (F->PragmaDiagMappings.empty())
4754
18.0k
        F->PragmaDiagMappings.swap(Record);
4755
0
      else
4756
0
        F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4757
0
                                     Record.begin(), Record.end());
4758
18.0k
      break;
4759
18.1k
    case HEADER_SEARCH_ENTRY_USAGE:
4760
18.1k
      if (!F)
4761
24
        break;
4762
18.0k
      unsigned Count = Record[0];
4763
18.0k
      const char *Byte = Blob.data();
4764
18.0k
      F->SearchPathUsage = llvm::BitVector(Count, false);
4765
36.1k
      for (unsigned I = 0; I < Count; 
++Byte18.0k
)
4766
82.3k
        
for (unsigned Bit = 0; 18.0k
Bit < 8 &&
I < Count82.3k
;
++Bit, ++I64.2k
)
4767
64.2k
          if (*Byte & (1 << Bit))
4768
18.7k
            F->SearchPathUsage[I] = true;
4769
18.0k
      break;
4770
79.8k
    }
4771
79.8k
  }
4772
18.1k
}
4773
4774
/// Parse a record and blob containing module file extension metadata.
4775
static bool parseModuleFileExtensionMetadata(
4776
              const SmallVectorImpl<uint64_t> &Record,
4777
              StringRef Blob,
4778
20
              ModuleFileExtensionMetadata &Metadata) {
4779
20
  if (Record.size() < 4) 
return true0
;
4780
4781
20
  Metadata.MajorVersion = Record[0];
4782
20
  Metadata.MinorVersion = Record[1];
4783
4784
20
  unsigned BlockNameLen = Record[2];
4785
20
  unsigned UserInfoLen = Record[3];
4786
4787
20
  if (BlockNameLen + UserInfoLen > Blob.size()) 
return true0
;
4788
4789
20
  Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4790
20
  Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4791
20
                                  Blob.data() + BlockNameLen + UserInfoLen);
4792
20
  return false;
4793
20
}
4794
4795
18
llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
4796
18
  BitstreamCursor &Stream = F.Stream;
4797
4798
18
  RecordData Record;
4799
54
  while (true) {
4800
54
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4801
54
    if (!MaybeEntry)
4802
0
      return MaybeEntry.takeError();
4803
54
    llvm::BitstreamEntry Entry = MaybeEntry.get();
4804
4805
54
    switch (Entry.Kind) {
4806
0
    case llvm::BitstreamEntry::SubBlock:
4807
0
      if (llvm::Error Err = Stream.SkipBlock())
4808
0
        return Err;
4809
0
      continue;
4810
18
    case llvm::BitstreamEntry::EndBlock:
4811
18
      return llvm::Error::success();
4812
0
    case llvm::BitstreamEntry::Error:
4813
0
      return llvm::createStringError(std::errc::illegal_byte_sequence,
4814
0
                                     "malformed block record in AST file");
4815
36
    case llvm::BitstreamEntry::Record:
4816
36
      break;
4817
54
    }
4818
4819
36
    Record.clear();
4820
36
    StringRef Blob;
4821
36
    Expected<unsigned> MaybeRecCode =
4822
36
        Stream.readRecord(Entry.ID, Record, &Blob);
4823
36
    if (!MaybeRecCode)
4824
0
      return MaybeRecCode.takeError();
4825
36
    switch (MaybeRecCode.get()) {
4826
18
    case EXTENSION_METADATA: {
4827
18
      ModuleFileExtensionMetadata Metadata;
4828
18
      if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4829
0
        return llvm::createStringError(
4830
0
            std::errc::illegal_byte_sequence,
4831
0
            "malformed EXTENSION_METADATA in AST file");
4832
4833
      // Find a module file extension with this block name.
4834
18
      auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4835
18
      if (Known == ModuleFileExtensions.end()) 
break3
;
4836
4837
      // Form a reader.
4838
15
      if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4839
15
                                                             F, Stream)) {
4840
13
        F.ExtensionReaders.push_back(std::move(Reader));
4841
13
      }
4842
4843
15
      break;
4844
18
    }
4845
36
    }
4846
36
  }
4847
4848
0
  return llvm::Error::success();
4849
18
}
4850
4851
8.54k
void ASTReader::InitializeContext() {
4852
8.54k
  assert(ContextObj && "no context to initialize");
4853
0
  ASTContext &Context = *ContextObj;
4854
4855
  // If there's a listener, notify them that we "read" the translation unit.
4856
8.54k
  if (DeserializationListener)
4857
1.56k
    DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4858
1.56k
                                      Context.getTranslationUnitDecl());
4859
4860
  // FIXME: Find a better way to deal with collisions between these
4861
  // built-in types. Right now, we just ignore the problem.
4862
4863
  // Load the special types.
4864
8.54k
  if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4865
8.54k
    if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4866
8.54k
      if (!Context.CFConstantStringTypeDecl)
4867
4.19k
        Context.setCFConstantStringType(GetType(String));
4868
8.54k
    }
4869
4870
8.54k
    if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4871
2.31k
      QualType FileType = GetType(File);
4872
2.31k
      if (FileType.isNull()) {
4873
0
        Error("FILE type is NULL");
4874
0
        return;
4875
0
      }
4876
4877
2.31k
      if (!Context.FILEDecl) {
4878
1.75k
        if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4879
1.75k
          Context.setFILEDecl(Typedef->getDecl());
4880
0
        else {
4881
0
          const TagType *Tag = FileType->getAs<TagType>();
4882
0
          if (!Tag) {
4883
0
            Error("Invalid FILE type in AST file");
4884
0
            return;
4885
0
          }
4886
0
          Context.setFILEDecl(Tag->getDecl());
4887
0
        }
4888
1.75k
      }
4889
2.31k
    }
4890
4891
8.54k
    if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4892
2.11k
      QualType Jmp_bufType = GetType(Jmp_buf);
4893
2.11k
      if (Jmp_bufType.isNull()) {
4894
0
        Error("jmp_buf type is NULL");
4895
0
        return;
4896
0
      }
4897
4898
2.11k
      if (!Context.jmp_bufDecl) {
4899
1.58k
        if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4900
1.58k
          Context.setjmp_bufDecl(Typedef->getDecl());
4901
0
        else {
4902
0
          const TagType *Tag = Jmp_bufType->getAs<TagType>();
4903
0
          if (!Tag) {
4904
0
            Error("Invalid jmp_buf type in AST file");
4905
0
            return;
4906
0
          }
4907
0
          Context.setjmp_bufDecl(Tag->getDecl());
4908
0
        }
4909
1.58k
      }
4910
2.11k
    }
4911
4912
8.54k
    if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4913
2.11k
      QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4914
2.11k
      if (Sigjmp_bufType.isNull()) {
4915
0
        Error("sigjmp_buf type is NULL");
4916
0
        return;
4917
0
      }
4918
4919
2.11k
      if (!Context.sigjmp_bufDecl) {
4920
1.58k
        if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4921
1.58k
          Context.setsigjmp_bufDecl(Typedef->getDecl());
4922
0
        else {
4923
0
          const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4924
0
          assert(Tag && "Invalid sigjmp_buf type in AST file");
4925
0
          Context.setsigjmp_bufDecl(Tag->getDecl());
4926
0
        }
4927
1.58k
      }
4928
2.11k
    }
4929
4930
8.54k
    if (unsigned ObjCIdRedef
4931
8.54k
          = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4932
592
      if (Context.ObjCIdRedefinitionType.isNull())
4933
295
        Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4934
592
    }
4935
4936
8.54k
    if (unsigned ObjCClassRedef
4937
8.54k
          = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4938
594
      if (Context.ObjCClassRedefinitionType.isNull())
4939
297
        Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4940
594
    }
4941
4942
8.54k
    if (unsigned ObjCSelRedef
4943
8.54k
          = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4944
592
      if (Context.ObjCSelRedefinitionType.isNull())
4945
295
        Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4946
592
    }
4947
4948
8.54k
    if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4949
2.11k
      QualType Ucontext_tType = GetType(Ucontext_t);
4950
2.11k
      if (Ucontext_tType.isNull()) {
4951
0
        Error("ucontext_t type is NULL");
4952
0
        return;
4953
0
      }
4954
4955
2.11k
      if (!Context.ucontext_tDecl) {
4956
1.58k
        if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4957
1.58k
          Context.setucontext_tDecl(Typedef->getDecl());
4958
0
        else {
4959
0
          const TagType *Tag = Ucontext_tType->getAs<TagType>();
4960
0
          assert(Tag && "Invalid ucontext_t type in AST file");
4961
0
          Context.setucontext_tDecl(Tag->getDecl());
4962
0
        }
4963
1.58k
      }
4964
2.11k
    }
4965
8.54k
  }
4966
4967
8.54k
  ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4968
4969
  // If there were any CUDA special declarations, deserialize them.
4970
8.54k
  if (!CUDASpecialDeclRefs.empty()) {
4971
1
    assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4972
0
    Context.setcudaConfigureCallDecl(
4973
1
                           cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4974
1
  }
4975
4976
  // Re-export any modules that were imported by a non-module AST file.
4977
  // FIXME: This does not make macro-only imports visible again.
4978
100
  for (auto &Import : ImportedModules) {
4979
100
    if (Module *Imported = getSubmodule(Import.ID)) {
4980
100
      makeModuleVisible(Imported, Module::AllVisible,
4981
100
                        /*ImportLoc=*/Import.ImportLoc);
4982
100
      if (Import.ImportLoc.isValid())
4983
98
        PP.makeModuleVisible(Imported, Import.ImportLoc);
4984
      // This updates visibility for Preprocessor only. For Sema, which can be
4985
      // nullptr here, we do the same later, in UpdateSema().
4986
100
    }
4987
100
  }
4988
8.54k
}
4989
4990
2.13k
void ASTReader::finalizeForWriting() {
4991
  // Nothing to do for now.
4992
2.13k
}
4993
4994
/// Reads and return the signature record from \p PCH's control block, or
4995
/// else returns 0.
4996
8.73k
static ASTFileSignature readASTFileSignature(StringRef PCH) {
4997
8.73k
  BitstreamCursor Stream(PCH);
4998
8.73k
  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4999
    // FIXME this drops the error on the floor.
5000
0
    consumeError(std::move(Err));
5001
0
    return ASTFileSignature();
5002
0
  }
5003
5004
  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5005
8.73k
  if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5006
0
    return ASTFileSignature();
5007
5008
  // Scan for SIGNATURE inside the diagnostic options block.
5009
8.73k
  ASTReader::RecordData Record;
5010
17.4k
  while (true) {
5011
17.4k
    Expected<llvm::BitstreamEntry> MaybeEntry =
5012
17.4k
        Stream.advanceSkippingSubblocks();
5013
17.4k
    if (!MaybeEntry) {
5014
      // FIXME this drops the error on the floor.
5015
0
      consumeError(MaybeEntry.takeError());
5016
0
      return ASTFileSignature();
5017
0
    }
5018
17.4k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
5019
5020
17.4k
    if (Entry.Kind != llvm::BitstreamEntry::Record)
5021
0
      return ASTFileSignature();
5022
5023
17.4k
    Record.clear();
5024
17.4k
    StringRef Blob;
5025
17.4k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5026
17.4k
    if (!MaybeRecord) {
5027
      // FIXME this drops the error on the floor.
5028
0
      consumeError(MaybeRecord.takeError());
5029
0
      return ASTFileSignature();
5030
0
    }
5031
17.4k
    if (SIGNATURE == MaybeRecord.get())
5032
8.73k
      return ASTFileSignature::create(Record.begin(),
5033
8.73k
                                      Record.begin() + ASTFileSignature::size);
5034
17.4k
  }
5035
8.73k
}
5036
5037
/// Retrieve the name of the original source file name
5038
/// directly from the AST file, without actually loading the AST
5039
/// file.
5040
std::string ASTReader::getOriginalSourceFile(
5041
    const std::string &ASTFileName, FileManager &FileMgr,
5042
3.66k
    const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5043
  // Open the AST file.
5044
3.66k
  auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5045
3.66k
  if (!Buffer) {
5046
0
    Diags.Report(diag::err_fe_unable_to_read_pch_file)
5047
0
        << ASTFileName << Buffer.getError().message();
5048
0
    return std::string();
5049
0
  }
5050
5051
  // Initialize the stream
5052
3.66k
  BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5053
5054
  // Sniff for the signature.
5055
3.66k
  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5056
1
    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5057
1
    return std::string();
5058
1
  }
5059
5060
  // Scan for the CONTROL_BLOCK_ID block.
5061
3.66k
  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5062
0
    Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5063
0
    return std::string();
5064
0
  }
5065
5066
  // Scan for ORIGINAL_FILE inside the control block.
5067
3.66k
  RecordData Record;
5068
7.57k
  while (true) {
5069
7.57k
    Expected<llvm::BitstreamEntry> MaybeEntry =
5070
7.57k
        Stream.advanceSkippingSubblocks();
5071
7.57k
    if (!MaybeEntry) {
5072
      // FIXME this drops errors on the floor.
5073
0
      consumeError(MaybeEntry.takeError());
5074
0
      return std::string();
5075
0
    }
5076
7.57k
    llvm::BitstreamEntry Entry = MaybeEntry.get();
5077
5078
7.57k
    if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5079
0
      return std::string();
5080
5081
7.57k
    if (Entry.Kind != llvm::BitstreamEntry::Record) {
5082
0
      Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5083
0
      return std::string();
5084
0
    }
5085
5086
7.57k
    Record.clear();
5087
7.57k
    StringRef Blob;
5088
7.57k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5089
7.57k
    if (!MaybeRecord) {
5090
      // FIXME this drops the errors on the floor.
5091
0
      consumeError(MaybeRecord.takeError());
5092
0
      return std::string();
5093
0
    }
5094
7.57k
    if (ORIGINAL_FILE == MaybeRecord.get())
5095
3.66k
      return Blob.str();
5096
7.57k
  }
5097
3.66k
}
5098
5099
namespace {
5100
5101
  class SimplePCHValidator : public ASTReaderListener {
5102
    const LangOptions &ExistingLangOpts;
5103
    const TargetOptions &ExistingTargetOpts;
5104
    const PreprocessorOptions &ExistingPPOpts;
5105
    std::string ExistingModuleCachePath;
5106
    FileManager &FileMgr;
5107
5108
  public:
5109
    SimplePCHValidator(const LangOptions &ExistingLangOpts,
5110
                       const TargetOptions &ExistingTargetOpts,
5111
                       const PreprocessorOptions &ExistingPPOpts,
5112
                       StringRef ExistingModuleCachePath, FileManager &FileMgr)
5113
        : ExistingLangOpts(ExistingLangOpts),
5114
          ExistingTargetOpts(ExistingTargetOpts),
5115
          ExistingPPOpts(ExistingPPOpts),
5116
13
          ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5117
5118
    bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5119
12
                             bool AllowCompatibleDifferences) override {
5120
12
      return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5121
12
                                  AllowCompatibleDifferences);
5122
12
    }
5123
5124
    bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5125
12
                           bool AllowCompatibleDifferences) override {
5126
12
      return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5127
12
                                AllowCompatibleDifferences);
5128
12
    }
5129
5130
    bool ReadHeade