Bug Summary

File:tools/clang/lib/Serialization/ASTReader.cpp
Warning:line 12406, column 3
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

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