Bug Summary

File:clang/lib/Serialization/ASTReader.cpp
Warning:line 3909, column 5
Forming reference to null pointer

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