Coverage Report

Created: 2019-07-24 05:18

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