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