1 //===--- ASTReader.cpp - AST File Reader ----------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ASTReader class, which reads AST files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Serialization/ASTReader.h" 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/ASTConsumer.h" 18 #include "clang/AST/ASTContext.h" 19 #include "clang/AST/DeclTemplate.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/ExprCXX.h" 22 #include "clang/AST/NestedNameSpecifier.h" 23 #include "clang/AST/Type.h" 24 #include "clang/AST/TypeLocVisitor.h" 25 #include "clang/Basic/FileManager.h" 26 #include "clang/Basic/SourceManager.h" 27 #include "clang/Basic/SourceManagerInternals.h" 28 #include "clang/Basic/TargetInfo.h" 29 #include "clang/Basic/TargetOptions.h" 30 #include "clang/Basic/Version.h" 31 #include "clang/Basic/VersionTuple.h" 32 #include "clang/Lex/HeaderSearch.h" 33 #include "clang/Lex/HeaderSearchOptions.h" 34 #include "clang/Lex/MacroInfo.h" 35 #include "clang/Lex/PreprocessingRecord.h" 36 #include "clang/Lex/Preprocessor.h" 37 #include "clang/Lex/PreprocessorOptions.h" 38 #include "clang/Sema/Scope.h" 39 #include "clang/Sema/Sema.h" 40 #include "clang/Serialization/ASTDeserializationListener.h" 41 #include "clang/Serialization/GlobalModuleIndex.h" 42 #include "clang/Serialization/ModuleManager.h" 43 #include "clang/Serialization/SerializationDiagnostic.h" 44 #include "llvm/ADT/Hashing.h" 45 #include "llvm/ADT/StringExtras.h" 46 #include "llvm/Bitcode/BitstreamReader.h" 47 #include "llvm/Support/ErrorHandling.h" 48 #include "llvm/Support/FileSystem.h" 49 #include "llvm/Support/MemoryBuffer.h" 50 #include "llvm/Support/Path.h" 51 #include "llvm/Support/SaveAndRestore.h" 52 #include "llvm/Support/system_error.h" 53 #include <algorithm> 54 #include <cstdio> 55 #include <iterator> 56 57 using namespace clang; 58 using namespace clang::serialization; 59 using namespace clang::serialization::reader; 60 using llvm::BitstreamCursor; 61 62 //===----------------------------------------------------------------------===// 63 // PCH validator implementation 64 //===----------------------------------------------------------------------===// 65 66 ASTReaderListener::~ASTReaderListener() {} 67 68 /// \brief Compare the given set of language options against an existing set of 69 /// language options. 70 /// 71 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 72 /// 73 /// \returns true if the languagae options mis-match, false otherwise. 74 static bool checkLanguageOptions(const LangOptions &LangOpts, 75 const LangOptions &ExistingLangOpts, 76 DiagnosticsEngine *Diags) { 77 #define LANGOPT(Name, Bits, Default, Description) \ 78 if (ExistingLangOpts.Name != LangOpts.Name) { \ 79 if (Diags) \ 80 Diags->Report(diag::err_pch_langopt_mismatch) \ 81 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 82 return true; \ 83 } 84 85 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 86 if (ExistingLangOpts.Name != LangOpts.Name) { \ 87 if (Diags) \ 88 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 89 << Description; \ 90 return true; \ 91 } 92 93 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 95 if (Diags) \ 96 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 97 << Description; \ 98 return true; \ 99 } 100 101 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 102 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 103 #include "clang/Basic/LangOptions.def" 104 105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 106 if (Diags) 107 Diags->Report(diag::err_pch_langopt_value_mismatch) 108 << "target Objective-C runtime"; 109 return true; 110 } 111 112 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 113 LangOpts.CommentOpts.BlockCommandNames) { 114 if (Diags) 115 Diags->Report(diag::err_pch_langopt_value_mismatch) 116 << "block command names"; 117 return true; 118 } 119 120 return false; 121 } 122 123 /// \brief Compare the given set of target options against an existing set of 124 /// target options. 125 /// 126 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 127 /// 128 /// \returns true if the target options mis-match, false otherwise. 129 static bool checkTargetOptions(const TargetOptions &TargetOpts, 130 const TargetOptions &ExistingTargetOpts, 131 DiagnosticsEngine *Diags) { 132 #define CHECK_TARGET_OPT(Field, Name) \ 133 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 134 if (Diags) \ 135 Diags->Report(diag::err_pch_targetopt_mismatch) \ 136 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 137 return true; \ 138 } 139 140 CHECK_TARGET_OPT(Triple, "target"); 141 CHECK_TARGET_OPT(CPU, "target CPU"); 142 CHECK_TARGET_OPT(ABI, "target ABI"); 143 CHECK_TARGET_OPT(CXXABI, "target C++ ABI"); 144 CHECK_TARGET_OPT(LinkerVersion, "target linker version"); 145 #undef CHECK_TARGET_OPT 146 147 // Compare feature sets. 148 SmallVector<StringRef, 4> ExistingFeatures( 149 ExistingTargetOpts.FeaturesAsWritten.begin(), 150 ExistingTargetOpts.FeaturesAsWritten.end()); 151 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 152 TargetOpts.FeaturesAsWritten.end()); 153 std::sort(ExistingFeatures.begin(), ExistingFeatures.end()); 154 std::sort(ReadFeatures.begin(), ReadFeatures.end()); 155 156 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size(); 157 unsigned ReadIdx = 0, ReadN = ReadFeatures.size(); 158 while (ExistingIdx < ExistingN && ReadIdx < ReadN) { 159 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) { 160 ++ExistingIdx; 161 ++ReadIdx; 162 continue; 163 } 164 165 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) { 166 if (Diags) 167 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 168 << false << ReadFeatures[ReadIdx]; 169 return true; 170 } 171 172 if (Diags) 173 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 174 << true << ExistingFeatures[ExistingIdx]; 175 return true; 176 } 177 178 if (ExistingIdx < ExistingN) { 179 if (Diags) 180 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 181 << true << ExistingFeatures[ExistingIdx]; 182 return true; 183 } 184 185 if (ReadIdx < ReadN) { 186 if (Diags) 187 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 188 << false << ReadFeatures[ReadIdx]; 189 return true; 190 } 191 192 return false; 193 } 194 195 bool 196 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 197 bool Complain) { 198 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 199 return checkLanguageOptions(LangOpts, ExistingLangOpts, 200 Complain? &Reader.Diags : 0); 201 } 202 203 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 204 bool Complain) { 205 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 206 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 207 Complain? &Reader.Diags : 0); 208 } 209 210 namespace { 211 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> > 212 MacroDefinitionsMap; 213 } 214 215 /// \brief Collect the macro definitions provided by the given preprocessor 216 /// options. 217 static void collectMacroDefinitions(const PreprocessorOptions &PPOpts, 218 MacroDefinitionsMap &Macros, 219 SmallVectorImpl<StringRef> *MacroNames = 0){ 220 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 221 StringRef Macro = PPOpts.Macros[I].first; 222 bool IsUndef = PPOpts.Macros[I].second; 223 224 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 225 StringRef MacroName = MacroPair.first; 226 StringRef MacroBody = MacroPair.second; 227 228 // For an #undef'd macro, we only care about the name. 229 if (IsUndef) { 230 if (MacroNames && !Macros.count(MacroName)) 231 MacroNames->push_back(MacroName); 232 233 Macros[MacroName] = std::make_pair("", true); 234 continue; 235 } 236 237 // For a #define'd macro, figure out the actual definition. 238 if (MacroName.size() == Macro.size()) 239 MacroBody = "1"; 240 else { 241 // Note: GCC drops anything following an end-of-line character. 242 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 243 MacroBody = MacroBody.substr(0, End); 244 } 245 246 if (MacroNames && !Macros.count(MacroName)) 247 MacroNames->push_back(MacroName); 248 Macros[MacroName] = std::make_pair(MacroBody, false); 249 } 250 } 251 252 /// \brief Check the preprocessor options deserialized from the control block 253 /// against the preprocessor options in an existing preprocessor. 254 /// 255 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 256 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 257 const PreprocessorOptions &ExistingPPOpts, 258 DiagnosticsEngine *Diags, 259 FileManager &FileMgr, 260 std::string &SuggestedPredefines) { 261 // Check macro definitions. 262 MacroDefinitionsMap ASTFileMacros; 263 collectMacroDefinitions(PPOpts, ASTFileMacros); 264 MacroDefinitionsMap ExistingMacros; 265 SmallVector<StringRef, 4> ExistingMacroNames; 266 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 267 268 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 269 // Dig out the macro definition in the existing preprocessor options. 270 StringRef MacroName = ExistingMacroNames[I]; 271 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 272 273 // Check whether we know anything about this macro name or not. 274 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known 275 = ASTFileMacros.find(MacroName); 276 if (Known == ASTFileMacros.end()) { 277 // FIXME: Check whether this identifier was referenced anywhere in the 278 // AST file. If so, we should reject the AST file. Unfortunately, this 279 // information isn't in the control block. What shall we do about it? 280 281 if (Existing.second) { 282 SuggestedPredefines += "#undef "; 283 SuggestedPredefines += MacroName.str(); 284 SuggestedPredefines += '\n'; 285 } else { 286 SuggestedPredefines += "#define "; 287 SuggestedPredefines += MacroName.str(); 288 SuggestedPredefines += ' '; 289 SuggestedPredefines += Existing.first.str(); 290 SuggestedPredefines += '\n'; 291 } 292 continue; 293 } 294 295 // If the macro was defined in one but undef'd in the other, we have a 296 // conflict. 297 if (Existing.second != Known->second.second) { 298 if (Diags) { 299 Diags->Report(diag::err_pch_macro_def_undef) 300 << MacroName << Known->second.second; 301 } 302 return true; 303 } 304 305 // If the macro was #undef'd in both, or if the macro bodies are identical, 306 // it's fine. 307 if (Existing.second || Existing.first == Known->second.first) 308 continue; 309 310 // The macro bodies differ; complain. 311 if (Diags) { 312 Diags->Report(diag::err_pch_macro_def_conflict) 313 << MacroName << Known->second.first << Existing.first; 314 } 315 return true; 316 } 317 318 // Check whether we're using predefines. 319 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) { 320 if (Diags) { 321 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 322 } 323 return true; 324 } 325 326 // Compute the #include and #include_macros lines we need. 327 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 328 StringRef File = ExistingPPOpts.Includes[I]; 329 if (File == ExistingPPOpts.ImplicitPCHInclude) 330 continue; 331 332 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 333 != PPOpts.Includes.end()) 334 continue; 335 336 SuggestedPredefines += "#include \""; 337 SuggestedPredefines += 338 HeaderSearch::NormalizeDashIncludePath(File, FileMgr); 339 SuggestedPredefines += "\"\n"; 340 } 341 342 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 343 StringRef File = ExistingPPOpts.MacroIncludes[I]; 344 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 345 File) 346 != PPOpts.MacroIncludes.end()) 347 continue; 348 349 SuggestedPredefines += "#__include_macros \""; 350 SuggestedPredefines += 351 HeaderSearch::NormalizeDashIncludePath(File, FileMgr); 352 SuggestedPredefines += "\"\n##\n"; 353 } 354 355 return false; 356 } 357 358 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 359 bool Complain, 360 std::string &SuggestedPredefines) { 361 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 362 363 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 364 Complain? &Reader.Diags : 0, 365 PP.getFileManager(), 366 SuggestedPredefines); 367 } 368 369 void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI, 370 unsigned ID) { 371 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID); 372 ++NumHeaderInfos; 373 } 374 375 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 376 PP.setCounterValue(Value); 377 } 378 379 //===----------------------------------------------------------------------===// 380 // AST reader implementation 381 //===----------------------------------------------------------------------===// 382 383 void 384 ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) { 385 DeserializationListener = Listener; 386 } 387 388 389 390 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 391 return serialization::ComputeHash(Sel); 392 } 393 394 395 std::pair<unsigned, unsigned> 396 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 397 using namespace clang::io; 398 unsigned KeyLen = ReadUnalignedLE16(d); 399 unsigned DataLen = ReadUnalignedLE16(d); 400 return std::make_pair(KeyLen, DataLen); 401 } 402 403 ASTSelectorLookupTrait::internal_key_type 404 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 405 using namespace clang::io; 406 SelectorTable &SelTable = Reader.getContext().Selectors; 407 unsigned N = ReadUnalignedLE16(d); 408 IdentifierInfo *FirstII 409 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 410 if (N == 0) 411 return SelTable.getNullarySelector(FirstII); 412 else if (N == 1) 413 return SelTable.getUnarySelector(FirstII); 414 415 SmallVector<IdentifierInfo *, 16> Args; 416 Args.push_back(FirstII); 417 for (unsigned I = 1; I != N; ++I) 418 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d))); 419 420 return SelTable.getSelector(N, Args.data()); 421 } 422 423 ASTSelectorLookupTrait::data_type 424 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 425 unsigned DataLen) { 426 using namespace clang::io; 427 428 data_type Result; 429 430 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d)); 431 unsigned NumInstanceMethods = ReadUnalignedLE16(d); 432 unsigned NumFactoryMethods = ReadUnalignedLE16(d); 433 434 // Load instance methods 435 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 436 if (ObjCMethodDecl *Method 437 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 438 Result.Instance.push_back(Method); 439 } 440 441 // Load factory methods 442 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 443 if (ObjCMethodDecl *Method 444 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 445 Result.Factory.push_back(Method); 446 } 447 448 return Result; 449 } 450 451 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 452 return llvm::HashString(a); 453 } 454 455 std::pair<unsigned, unsigned> 456 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 457 using namespace clang::io; 458 unsigned DataLen = ReadUnalignedLE16(d); 459 unsigned KeyLen = ReadUnalignedLE16(d); 460 return std::make_pair(KeyLen, DataLen); 461 } 462 463 ASTIdentifierLookupTraitBase::internal_key_type 464 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 465 assert(n >= 2 && d[n-1] == '\0'); 466 return StringRef((const char*) d, n-1); 467 } 468 469 /// \brief Whether the given identifier is "interesting". 470 static bool isInterestingIdentifier(IdentifierInfo &II) { 471 return II.isPoisoned() || 472 II.isExtensionToken() || 473 II.getObjCOrBuiltinID() || 474 II.hasRevertedTokenIDToIdentifier() || 475 II.hadMacroDefinition() || 476 II.getFETokenInfo<void>(); 477 } 478 479 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 480 const unsigned char* d, 481 unsigned DataLen) { 482 using namespace clang::io; 483 unsigned RawID = ReadUnalignedLE32(d); 484 bool IsInteresting = RawID & 0x01; 485 486 // Wipe out the "is interesting" bit. 487 RawID = RawID >> 1; 488 489 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 490 if (!IsInteresting) { 491 // For uninteresting identifiers, just build the IdentifierInfo 492 // and associate it with the persistent ID. 493 IdentifierInfo *II = KnownII; 494 if (!II) { 495 II = &Reader.getIdentifierTable().getOwn(k); 496 KnownII = II; 497 } 498 Reader.SetIdentifierInfo(ID, II); 499 if (!II->isFromAST()) { 500 bool WasInteresting = isInterestingIdentifier(*II); 501 II->setIsFromAST(); 502 if (WasInteresting) 503 II->setChangedSinceDeserialization(); 504 } 505 Reader.markIdentifierUpToDate(II); 506 return II; 507 } 508 509 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d); 510 unsigned Bits = ReadUnalignedLE16(d); 511 bool CPlusPlusOperatorKeyword = Bits & 0x01; 512 Bits >>= 1; 513 bool HasRevertedTokenIDToIdentifier = Bits & 0x01; 514 Bits >>= 1; 515 bool Poisoned = Bits & 0x01; 516 Bits >>= 1; 517 bool ExtensionToken = Bits & 0x01; 518 Bits >>= 1; 519 bool hadMacroDefinition = Bits & 0x01; 520 Bits >>= 1; 521 522 assert(Bits == 0 && "Extra bits in the identifier?"); 523 DataLen -= 8; 524 525 // Build the IdentifierInfo itself and link the identifier ID with 526 // the new IdentifierInfo. 527 IdentifierInfo *II = KnownII; 528 if (!II) { 529 II = &Reader.getIdentifierTable().getOwn(StringRef(k)); 530 KnownII = II; 531 } 532 Reader.markIdentifierUpToDate(II); 533 if (!II->isFromAST()) { 534 bool WasInteresting = isInterestingIdentifier(*II); 535 II->setIsFromAST(); 536 if (WasInteresting) 537 II->setChangedSinceDeserialization(); 538 } 539 540 // Set or check the various bits in the IdentifierInfo structure. 541 // Token IDs are read-only. 542 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 543 II->RevertTokenIDToIdentifier(); 544 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 545 assert(II->isExtensionToken() == ExtensionToken && 546 "Incorrect extension token flag"); 547 (void)ExtensionToken; 548 if (Poisoned) 549 II->setIsPoisoned(true); 550 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 551 "Incorrect C++ operator keyword flag"); 552 (void)CPlusPlusOperatorKeyword; 553 554 // If this identifier is a macro, deserialize the macro 555 // definition. 556 if (hadMacroDefinition) { 557 SmallVector<MacroID, 4> MacroIDs; 558 while (uint32_t LocalID = ReadUnalignedLE32(d)) { 559 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID)); 560 DataLen -= 4; 561 } 562 DataLen -= 4; 563 Reader.setIdentifierIsMacro(II, MacroIDs); 564 } 565 566 Reader.SetIdentifierInfo(ID, II); 567 568 // Read all of the declarations visible at global scope with this 569 // name. 570 if (DataLen > 0) { 571 SmallVector<uint32_t, 4> DeclIDs; 572 for (; DataLen > 0; DataLen -= 4) 573 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d))); 574 Reader.SetGloballyVisibleDecls(II, DeclIDs); 575 } 576 577 return II; 578 } 579 580 unsigned 581 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const { 582 llvm::FoldingSetNodeID ID; 583 ID.AddInteger(Key.Kind); 584 585 switch (Key.Kind) { 586 case DeclarationName::Identifier: 587 case DeclarationName::CXXLiteralOperatorName: 588 ID.AddString(((IdentifierInfo*)Key.Data)->getName()); 589 break; 590 case DeclarationName::ObjCZeroArgSelector: 591 case DeclarationName::ObjCOneArgSelector: 592 case DeclarationName::ObjCMultiArgSelector: 593 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data))); 594 break; 595 case DeclarationName::CXXOperatorName: 596 ID.AddInteger((OverloadedOperatorKind)Key.Data); 597 break; 598 case DeclarationName::CXXConstructorName: 599 case DeclarationName::CXXDestructorName: 600 case DeclarationName::CXXConversionFunctionName: 601 case DeclarationName::CXXUsingDirective: 602 break; 603 } 604 605 return ID.ComputeHash(); 606 } 607 608 ASTDeclContextNameLookupTrait::internal_key_type 609 ASTDeclContextNameLookupTrait::GetInternalKey( 610 const external_key_type& Name) const { 611 DeclNameKey Key; 612 Key.Kind = Name.getNameKind(); 613 switch (Name.getNameKind()) { 614 case DeclarationName::Identifier: 615 Key.Data = (uint64_t)Name.getAsIdentifierInfo(); 616 break; 617 case DeclarationName::ObjCZeroArgSelector: 618 case DeclarationName::ObjCOneArgSelector: 619 case DeclarationName::ObjCMultiArgSelector: 620 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 621 break; 622 case DeclarationName::CXXOperatorName: 623 Key.Data = Name.getCXXOverloadedOperator(); 624 break; 625 case DeclarationName::CXXLiteralOperatorName: 626 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier(); 627 break; 628 case DeclarationName::CXXConstructorName: 629 case DeclarationName::CXXDestructorName: 630 case DeclarationName::CXXConversionFunctionName: 631 case DeclarationName::CXXUsingDirective: 632 Key.Data = 0; 633 break; 634 } 635 636 return Key; 637 } 638 639 std::pair<unsigned, unsigned> 640 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 641 using namespace clang::io; 642 unsigned KeyLen = ReadUnalignedLE16(d); 643 unsigned DataLen = ReadUnalignedLE16(d); 644 return std::make_pair(KeyLen, DataLen); 645 } 646 647 ASTDeclContextNameLookupTrait::internal_key_type 648 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) { 649 using namespace clang::io; 650 651 DeclNameKey Key; 652 Key.Kind = (DeclarationName::NameKind)*d++; 653 switch (Key.Kind) { 654 case DeclarationName::Identifier: 655 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 656 break; 657 case DeclarationName::ObjCZeroArgSelector: 658 case DeclarationName::ObjCOneArgSelector: 659 case DeclarationName::ObjCMultiArgSelector: 660 Key.Data = 661 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d)) 662 .getAsOpaquePtr(); 663 break; 664 case DeclarationName::CXXOperatorName: 665 Key.Data = *d++; // OverloadedOperatorKind 666 break; 667 case DeclarationName::CXXLiteralOperatorName: 668 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 669 break; 670 case DeclarationName::CXXConstructorName: 671 case DeclarationName::CXXDestructorName: 672 case DeclarationName::CXXConversionFunctionName: 673 case DeclarationName::CXXUsingDirective: 674 Key.Data = 0; 675 break; 676 } 677 678 return Key; 679 } 680 681 ASTDeclContextNameLookupTrait::data_type 682 ASTDeclContextNameLookupTrait::ReadData(internal_key_type, 683 const unsigned char* d, 684 unsigned DataLen) { 685 using namespace clang::io; 686 unsigned NumDecls = ReadUnalignedLE16(d); 687 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>( 688 const_cast<unsigned char *>(d)); 689 return std::make_pair(Start, Start + NumDecls); 690 } 691 692 bool ASTReader::ReadDeclContextStorage(ModuleFile &M, 693 BitstreamCursor &Cursor, 694 const std::pair<uint64_t, uint64_t> &Offsets, 695 DeclContextInfo &Info) { 696 SavedStreamPosition SavedPosition(Cursor); 697 // First the lexical decls. 698 if (Offsets.first != 0) { 699 Cursor.JumpToBit(Offsets.first); 700 701 RecordData Record; 702 StringRef Blob; 703 unsigned Code = Cursor.ReadCode(); 704 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 705 if (RecCode != DECL_CONTEXT_LEXICAL) { 706 Error("Expected lexical block"); 707 return true; 708 } 709 710 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data()); 711 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair); 712 } 713 714 // Now the lookup table. 715 if (Offsets.second != 0) { 716 Cursor.JumpToBit(Offsets.second); 717 718 RecordData Record; 719 StringRef Blob; 720 unsigned Code = Cursor.ReadCode(); 721 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 722 if (RecCode != DECL_CONTEXT_VISIBLE) { 723 Error("Expected visible lookup table block"); 724 return true; 725 } 726 Info.NameLookupTableData 727 = ASTDeclContextNameLookupTable::Create( 728 (const unsigned char *)Blob.data() + Record[0], 729 (const unsigned char *)Blob.data(), 730 ASTDeclContextNameLookupTrait(*this, M)); 731 } 732 733 return false; 734 } 735 736 void ASTReader::Error(StringRef Msg) { 737 Error(diag::err_fe_pch_malformed, Msg); 738 } 739 740 void ASTReader::Error(unsigned DiagID, 741 StringRef Arg1, StringRef Arg2) { 742 if (Diags.isDiagnosticInFlight()) 743 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 744 else 745 Diag(DiagID) << Arg1 << Arg2; 746 } 747 748 //===----------------------------------------------------------------------===// 749 // Source Manager Deserialization 750 //===----------------------------------------------------------------------===// 751 752 /// \brief Read the line table in the source manager block. 753 /// \returns true if there was an error. 754 bool ASTReader::ParseLineTable(ModuleFile &F, 755 SmallVectorImpl<uint64_t> &Record) { 756 unsigned Idx = 0; 757 LineTableInfo &LineTable = SourceMgr.getLineTable(); 758 759 // Parse the file names 760 std::map<int, int> FileIDs; 761 for (int I = 0, N = Record[Idx++]; I != N; ++I) { 762 // Extract the file name 763 unsigned FilenameLen = Record[Idx++]; 764 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); 765 Idx += FilenameLen; 766 MaybeAddSystemRootToFilename(F, Filename); 767 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 768 } 769 770 // Parse the line entries 771 std::vector<LineEntry> Entries; 772 while (Idx < Record.size()) { 773 int FID = Record[Idx++]; 774 assert(FID >= 0 && "Serialized line entries for non-local file."); 775 // Remap FileID from 1-based old view. 776 FID += F.SLocEntryBaseID - 1; 777 778 // Extract the line entries 779 unsigned NumEntries = Record[Idx++]; 780 assert(NumEntries && "Numentries is 00000"); 781 Entries.clear(); 782 Entries.reserve(NumEntries); 783 for (unsigned I = 0; I != NumEntries; ++I) { 784 unsigned FileOffset = Record[Idx++]; 785 unsigned LineNo = Record[Idx++]; 786 int FilenameID = FileIDs[Record[Idx++]]; 787 SrcMgr::CharacteristicKind FileKind 788 = (SrcMgr::CharacteristicKind)Record[Idx++]; 789 unsigned IncludeOffset = Record[Idx++]; 790 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 791 FileKind, IncludeOffset)); 792 } 793 LineTable.AddEntry(FileID::get(FID), Entries); 794 } 795 796 return false; 797 } 798 799 /// \brief Read a source manager block 800 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 801 using namespace SrcMgr; 802 803 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 804 805 // Set the source-location entry cursor to the current position in 806 // the stream. This cursor will be used to read the contents of the 807 // source manager block initially, and then lazily read 808 // source-location entries as needed. 809 SLocEntryCursor = F.Stream; 810 811 // The stream itself is going to skip over the source manager block. 812 if (F.Stream.SkipBlock()) { 813 Error("malformed block record in AST file"); 814 return true; 815 } 816 817 // Enter the source manager block. 818 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 819 Error("malformed source manager block record in AST file"); 820 return true; 821 } 822 823 RecordData Record; 824 while (true) { 825 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks(); 826 827 switch (E.Kind) { 828 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 829 case llvm::BitstreamEntry::Error: 830 Error("malformed block record in AST file"); 831 return true; 832 case llvm::BitstreamEntry::EndBlock: 833 return false; 834 case llvm::BitstreamEntry::Record: 835 // The interesting case. 836 break; 837 } 838 839 // Read a record. 840 Record.clear(); 841 StringRef Blob; 842 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) { 843 default: // Default behavior: ignore. 844 break; 845 846 case SM_SLOC_FILE_ENTRY: 847 case SM_SLOC_BUFFER_ENTRY: 848 case SM_SLOC_EXPANSION_ENTRY: 849 // Once we hit one of the source location entries, we're done. 850 return false; 851 } 852 } 853 } 854 855 /// \brief If a header file is not found at the path that we expect it to be 856 /// and the PCH file was moved from its original location, try to resolve the 857 /// file by assuming that header+PCH were moved together and the header is in 858 /// the same place relative to the PCH. 859 static std::string 860 resolveFileRelativeToOriginalDir(const std::string &Filename, 861 const std::string &OriginalDir, 862 const std::string &CurrDir) { 863 assert(OriginalDir != CurrDir && 864 "No point trying to resolve the file if the PCH dir didn't change"); 865 using namespace llvm::sys; 866 SmallString<128> filePath(Filename); 867 fs::make_absolute(filePath); 868 assert(path::is_absolute(OriginalDir)); 869 SmallString<128> currPCHPath(CurrDir); 870 871 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 872 fileDirE = path::end(path::parent_path(filePath)); 873 path::const_iterator origDirI = path::begin(OriginalDir), 874 origDirE = path::end(OriginalDir); 875 // Skip the common path components from filePath and OriginalDir. 876 while (fileDirI != fileDirE && origDirI != origDirE && 877 *fileDirI == *origDirI) { 878 ++fileDirI; 879 ++origDirI; 880 } 881 for (; origDirI != origDirE; ++origDirI) 882 path::append(currPCHPath, ".."); 883 path::append(currPCHPath, fileDirI, fileDirE); 884 path::append(currPCHPath, path::filename(Filename)); 885 return currPCHPath.str(); 886 } 887 888 bool ASTReader::ReadSLocEntry(int ID) { 889 if (ID == 0) 890 return false; 891 892 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 893 Error("source location entry ID out-of-range for AST file"); 894 return true; 895 } 896 897 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 898 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 899 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 900 unsigned BaseOffset = F->SLocEntryBaseOffset; 901 902 ++NumSLocEntriesRead; 903 llvm::BitstreamEntry Entry = SLocEntryCursor.advance(); 904 if (Entry.Kind != llvm::BitstreamEntry::Record) { 905 Error("incorrectly-formatted source location entry in AST file"); 906 return true; 907 } 908 909 RecordData Record; 910 StringRef Blob; 911 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) { 912 default: 913 Error("incorrectly-formatted source location entry in AST file"); 914 return true; 915 916 case SM_SLOC_FILE_ENTRY: { 917 // We will detect whether a file changed and return 'Failure' for it, but 918 // we will also try to fail gracefully by setting up the SLocEntry. 919 unsigned InputID = Record[4]; 920 InputFile IF = getInputFile(*F, InputID); 921 const FileEntry *File = IF.getFile(); 922 bool OverriddenBuffer = IF.isOverridden(); 923 924 // Note that we only check if a File was returned. If it was out-of-date 925 // we have complained but we will continue creating a FileID to recover 926 // gracefully. 927 if (!File) 928 return true; 929 930 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 931 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 932 // This is the module's main file. 933 IncludeLoc = getImportLocation(F); 934 } 935 SrcMgr::CharacteristicKind 936 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 937 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 938 ID, BaseOffset + Record[0]); 939 SrcMgr::FileInfo &FileInfo = 940 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 941 FileInfo.NumCreatedFIDs = Record[5]; 942 if (Record[3]) 943 FileInfo.setHasLineDirectives(); 944 945 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 946 unsigned NumFileDecls = Record[7]; 947 if (NumFileDecls) { 948 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 949 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 950 NumFileDecls)); 951 } 952 953 const SrcMgr::ContentCache *ContentCache 954 = SourceMgr.getOrCreateContentCache(File, 955 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 956 if (OverriddenBuffer && !ContentCache->BufferOverridden && 957 ContentCache->ContentsEntry == ContentCache->OrigEntry) { 958 unsigned Code = SLocEntryCursor.ReadCode(); 959 Record.clear(); 960 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob); 961 962 if (RecCode != SM_SLOC_BUFFER_BLOB) { 963 Error("AST record has invalid code"); 964 return true; 965 } 966 967 llvm::MemoryBuffer *Buffer 968 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName()); 969 SourceMgr.overrideFileContents(File, Buffer); 970 } 971 972 break; 973 } 974 975 case SM_SLOC_BUFFER_ENTRY: { 976 const char *Name = Blob.data(); 977 unsigned Offset = Record[0]; 978 SrcMgr::CharacteristicKind 979 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 980 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 981 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) { 982 IncludeLoc = getImportLocation(F); 983 } 984 unsigned Code = SLocEntryCursor.ReadCode(); 985 Record.clear(); 986 unsigned RecCode 987 = SLocEntryCursor.readRecord(Code, Record, &Blob); 988 989 if (RecCode != SM_SLOC_BUFFER_BLOB) { 990 Error("AST record has invalid code"); 991 return true; 992 } 993 994 llvm::MemoryBuffer *Buffer 995 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name); 996 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID, 997 BaseOffset + Offset, IncludeLoc); 998 break; 999 } 1000 1001 case SM_SLOC_EXPANSION_ENTRY: { 1002 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1003 SourceMgr.createExpansionLoc(SpellingLoc, 1004 ReadSourceLocation(*F, Record[2]), 1005 ReadSourceLocation(*F, Record[3]), 1006 Record[4], 1007 ID, 1008 BaseOffset + Record[0]); 1009 break; 1010 } 1011 } 1012 1013 return false; 1014 } 1015 1016 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1017 if (ID == 0) 1018 return std::make_pair(SourceLocation(), ""); 1019 1020 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1021 Error("source location entry ID out-of-range for AST file"); 1022 return std::make_pair(SourceLocation(), ""); 1023 } 1024 1025 // Find which module file this entry lands in. 1026 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1027 if (M->Kind != MK_Module) 1028 return std::make_pair(SourceLocation(), ""); 1029 1030 // FIXME: Can we map this down to a particular submodule? That would be 1031 // ideal. 1032 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName)); 1033 } 1034 1035 /// \brief Find the location where the module F is imported. 1036 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1037 if (F->ImportLoc.isValid()) 1038 return F->ImportLoc; 1039 1040 // Otherwise we have a PCH. It's considered to be "imported" at the first 1041 // location of its includer. 1042 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1043 // Main file is the importer. We assume that it is the first entry in the 1044 // entry table. We can't ask the manager, because at the time of PCH loading 1045 // the main file entry doesn't exist yet. 1046 // The very first entry is the invalid instantiation loc, which takes up 1047 // offsets 0 and 1. 1048 return SourceLocation::getFromRawEncoding(2U); 1049 } 1050 //return F->Loaders[0]->FirstLoc; 1051 return F->ImportedBy[0]->FirstLoc; 1052 } 1053 1054 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1055 /// specified cursor. Read the abbreviations that are at the top of the block 1056 /// and then leave the cursor pointing into the block. 1057 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { 1058 if (Cursor.EnterSubBlock(BlockID)) { 1059 Error("malformed block record in AST file"); 1060 return Failure; 1061 } 1062 1063 while (true) { 1064 uint64_t Offset = Cursor.GetCurrentBitNo(); 1065 unsigned Code = Cursor.ReadCode(); 1066 1067 // We expect all abbrevs to be at the start of the block. 1068 if (Code != llvm::bitc::DEFINE_ABBREV) { 1069 Cursor.JumpToBit(Offset); 1070 return false; 1071 } 1072 Cursor.ReadAbbrevRecord(); 1073 } 1074 } 1075 1076 void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, 1077 MacroDirective *Hint) { 1078 BitstreamCursor &Stream = F.MacroCursor; 1079 1080 // Keep track of where we are in the stream, then jump back there 1081 // after reading this macro. 1082 SavedStreamPosition SavedPosition(Stream); 1083 1084 Stream.JumpToBit(Offset); 1085 RecordData Record; 1086 SmallVector<IdentifierInfo*, 16> MacroArgs; 1087 MacroInfo *Macro = 0; 1088 1089 // RAII object to add the loaded macro information once we're done 1090 // adding tokens. 1091 struct AddLoadedMacroInfoRAII { 1092 Preprocessor &PP; 1093 MacroDirective *Hint; 1094 MacroDirective *MD; 1095 IdentifierInfo *II; 1096 1097 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroDirective *Hint) 1098 : PP(PP), Hint(Hint), MD(), II() { } 1099 ~AddLoadedMacroInfoRAII( ) { 1100 if (MD) { 1101 // Finally, install the macro. 1102 PP.addLoadedMacroInfo(II, MD, Hint); 1103 } 1104 } 1105 } AddLoadedMacroInfo(PP, Hint); 1106 1107 while (true) { 1108 // Advance to the next record, but if we get to the end of the block, don't 1109 // pop it (removing all the abbreviations from the cursor) since we want to 1110 // be able to reseek within the block and read entries. 1111 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1112 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags); 1113 1114 switch (Entry.Kind) { 1115 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1116 case llvm::BitstreamEntry::Error: 1117 Error("malformed block record in AST file"); 1118 return; 1119 case llvm::BitstreamEntry::EndBlock: 1120 return; 1121 case llvm::BitstreamEntry::Record: 1122 // The interesting case. 1123 break; 1124 } 1125 1126 // Read a record. 1127 Record.clear(); 1128 PreprocessorRecordTypes RecType = 1129 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record); 1130 switch (RecType) { 1131 case PP_MACRO_OBJECT_LIKE: 1132 case PP_MACRO_FUNCTION_LIKE: { 1133 // If we already have a macro, that means that we've hit the end 1134 // of the definition of the macro we were looking for. We're 1135 // done. 1136 if (Macro) 1137 return; 1138 1139 IdentifierInfo *II = getLocalIdentifier(F, Record[0]); 1140 if (II == 0) { 1141 Error("macro must have a name in AST file"); 1142 return; 1143 } 1144 1145 unsigned GlobalID = getGlobalMacroID(F, Record[1]); 1146 1147 // If this macro has already been loaded, don't do so again. 1148 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS]) 1149 return; 1150 1151 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]); 1152 unsigned NextIndex = 3; 1153 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1154 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1155 // FIXME: Location should be import location in case of module. 1156 MacroDirective *MD = PP.AllocateMacroDirective(MI, Loc, 1157 /*isImported=*/true); 1158 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1159 1160 // Record this macro. 1161 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MD; 1162 1163 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex); 1164 if (UndefLoc.isValid()) 1165 MD->setUndefLoc(UndefLoc); 1166 1167 MI->setIsUsed(Record[NextIndex++]); 1168 1169 bool IsPublic = Record[NextIndex++]; 1170 MD->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex)); 1171 1172 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1173 // Decode function-like macro info. 1174 bool isC99VarArgs = Record[NextIndex++]; 1175 bool isGNUVarArgs = Record[NextIndex++]; 1176 bool hasCommaPasting = Record[NextIndex++]; 1177 MacroArgs.clear(); 1178 unsigned NumArgs = Record[NextIndex++]; 1179 for (unsigned i = 0; i != NumArgs; ++i) 1180 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1181 1182 // Install function-like macro info. 1183 MI->setIsFunctionLike(); 1184 if (isC99VarArgs) MI->setIsC99Varargs(); 1185 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1186 if (hasCommaPasting) MI->setHasCommaPasting(); 1187 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), 1188 PP.getPreprocessorAllocator()); 1189 } 1190 1191 if (DeserializationListener) 1192 DeserializationListener->MacroRead(GlobalID, MD); 1193 1194 // If an update record marked this as undefined, do so now. 1195 // FIXME: Only if the submodule this update came from is visible? 1196 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID); 1197 if (Update != MacroUpdates.end()) { 1198 if (MD->getUndefLoc().isInvalid()) { 1199 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) { 1200 bool Hidden = false; 1201 if (unsigned SubmoduleID = Update->second[I].first) { 1202 if (Module *Owner = getSubmodule(SubmoduleID)) { 1203 if (Owner->NameVisibility == Module::Hidden) { 1204 // Note that this #undef is hidden. 1205 Hidden = true; 1206 1207 // Record this hiding for later. 1208 HiddenNamesMap[Owner].push_back( 1209 HiddenName(II, MD, Update->second[I].second.UndefLoc)); 1210 } 1211 } 1212 } 1213 1214 if (!Hidden) { 1215 MD->setUndefLoc(Update->second[I].second.UndefLoc); 1216 if (PPMutationListener *Listener = PP.getPPMutationListener()) 1217 Listener->UndefinedMacro(MD); 1218 break; 1219 } 1220 } 1221 } 1222 MacroUpdates.erase(Update); 1223 } 1224 1225 // Determine whether this macro definition is visible. 1226 bool Hidden = !MD->isPublic(); 1227 if (!Hidden && GlobalSubmoduleID) { 1228 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) { 1229 if (Owner->NameVisibility == Module::Hidden) { 1230 // The owning module is not visible, and this macro definition 1231 // should not be, either. 1232 Hidden = true; 1233 1234 // Note that this macro definition was hidden because its owning 1235 // module is not yet visible. 1236 HiddenNamesMap[Owner].push_back(HiddenName(II, MD)); 1237 } 1238 } 1239 } 1240 MD->setHidden(Hidden); 1241 1242 // Make sure we install the macro once we're done. 1243 AddLoadedMacroInfo.MD = MD; 1244 AddLoadedMacroInfo.II = II; 1245 1246 // Remember that we saw this macro last so that we add the tokens that 1247 // form its body to it. 1248 Macro = MI; 1249 1250 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1251 Record[NextIndex]) { 1252 // We have a macro definition. Register the association 1253 PreprocessedEntityID 1254 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1255 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1256 PreprocessingRecord::PPEntityID 1257 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true); 1258 MacroDefinition *PPDef = 1259 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID)); 1260 if (PPDef) 1261 PPRec.RegisterMacroDefinition(Macro, PPDef); 1262 } 1263 1264 ++NumMacrosRead; 1265 break; 1266 } 1267 1268 case PP_TOKEN: { 1269 // If we see a TOKEN before a PP_MACRO_*, then the file is 1270 // erroneous, just pretend we didn't see this. 1271 if (Macro == 0) break; 1272 1273 Token Tok; 1274 Tok.startToken(); 1275 Tok.setLocation(ReadSourceLocation(F, Record[0])); 1276 Tok.setLength(Record[1]); 1277 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2])) 1278 Tok.setIdentifierInfo(II); 1279 Tok.setKind((tok::TokenKind)Record[3]); 1280 Tok.setFlag((Token::TokenFlags)Record[4]); 1281 Macro->AddTokenToBody(Tok); 1282 break; 1283 } 1284 } 1285 } 1286 } 1287 1288 PreprocessedEntityID 1289 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const { 1290 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1291 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1292 assert(I != M.PreprocessedEntityRemap.end() 1293 && "Invalid index into preprocessed entity index remap"); 1294 1295 return LocalID + I->second; 1296 } 1297 1298 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1299 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1300 } 1301 1302 HeaderFileInfoTrait::internal_key_type 1303 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1304 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(), 1305 FE->getName() }; 1306 return ikey; 1307 } 1308 1309 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1310 if (a.Size != b.Size || a.ModTime != b.ModTime) 1311 return false; 1312 1313 if (strcmp(a.Filename, b.Filename) == 0) 1314 return true; 1315 1316 // Determine whether the actual files are equivalent. 1317 FileManager &FileMgr = Reader.getFileManager(); 1318 const FileEntry *FEA = FileMgr.getFile(a.Filename); 1319 const FileEntry *FEB = FileMgr.getFile(b.Filename); 1320 return (FEA && FEA == FEB); 1321 } 1322 1323 std::pair<unsigned, unsigned> 1324 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1325 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 1326 unsigned DataLen = (unsigned) *d++; 1327 return std::make_pair(KeyLen, DataLen); 1328 } 1329 1330 HeaderFileInfoTrait::internal_key_type 1331 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1332 internal_key_type ikey; 1333 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d)); 1334 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d)); 1335 ikey.Filename = (const char *)d; 1336 return ikey; 1337 } 1338 1339 HeaderFileInfoTrait::data_type 1340 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1341 unsigned DataLen) { 1342 const unsigned char *End = d + DataLen; 1343 using namespace clang::io; 1344 HeaderFileInfo HFI; 1345 unsigned Flags = *d++; 1346 HFI.isImport = (Flags >> 5) & 0x01; 1347 HFI.isPragmaOnce = (Flags >> 4) & 0x01; 1348 HFI.DirInfo = (Flags >> 2) & 0x03; 1349 HFI.Resolved = (Flags >> 1) & 0x01; 1350 HFI.IndexHeaderMapHeader = Flags & 0x01; 1351 HFI.NumIncludes = ReadUnalignedLE16(d); 1352 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M, 1353 ReadUnalignedLE32(d)); 1354 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) { 1355 // The framework offset is 1 greater than the actual offset, 1356 // since 0 is used as an indicator for "no framework name". 1357 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1358 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1359 } 1360 1361 if (d != End) { 1362 uint32_t LocalSMID = ReadUnalignedLE32(d); 1363 if (LocalSMID) { 1364 // This header is part of a module. Associate it with the module to enable 1365 // implicit module import. 1366 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1367 Module *Mod = Reader.getSubmodule(GlobalSMID); 1368 HFI.isModuleHeader = true; 1369 FileManager &FileMgr = Reader.getFileManager(); 1370 ModuleMap &ModMap = 1371 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1372 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false); 1373 } 1374 } 1375 1376 assert(End == d && "Wrong data length in HeaderFileInfo deserialization"); 1377 (void)End; 1378 1379 // This HeaderFileInfo was externally loaded. 1380 HFI.External = true; 1381 return HFI; 1382 } 1383 1384 void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){ 1385 II->setHadMacroDefinition(true); 1386 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1387 PendingMacroIDs[II].append(IDs.begin(), IDs.end()); 1388 } 1389 1390 void ASTReader::ReadDefinedMacros() { 1391 // Note that we are loading defined macros. 1392 Deserializing Macros(this); 1393 1394 for (ModuleReverseIterator I = ModuleMgr.rbegin(), 1395 E = ModuleMgr.rend(); I != E; ++I) { 1396 BitstreamCursor &MacroCursor = (*I)->MacroCursor; 1397 1398 // If there was no preprocessor block, skip this file. 1399 if (!MacroCursor.getBitStreamReader()) 1400 continue; 1401 1402 BitstreamCursor Cursor = MacroCursor; 1403 Cursor.JumpToBit((*I)->MacroStartOffset); 1404 1405 RecordData Record; 1406 while (true) { 1407 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks(); 1408 1409 switch (E.Kind) { 1410 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1411 case llvm::BitstreamEntry::Error: 1412 Error("malformed block record in AST file"); 1413 return; 1414 case llvm::BitstreamEntry::EndBlock: 1415 goto NextCursor; 1416 1417 case llvm::BitstreamEntry::Record: 1418 Record.clear(); 1419 switch (Cursor.readRecord(E.ID, Record)) { 1420 default: // Default behavior: ignore. 1421 break; 1422 1423 case PP_MACRO_OBJECT_LIKE: 1424 case PP_MACRO_FUNCTION_LIKE: 1425 getLocalIdentifier(**I, Record[0]); 1426 break; 1427 1428 case PP_TOKEN: 1429 // Ignore tokens. 1430 break; 1431 } 1432 break; 1433 } 1434 } 1435 NextCursor: ; 1436 } 1437 } 1438 1439 namespace { 1440 /// \brief Visitor class used to look up identifirs in an AST file. 1441 class IdentifierLookupVisitor { 1442 StringRef Name; 1443 unsigned PriorGeneration; 1444 unsigned &NumIdentifierLookups; 1445 unsigned &NumIdentifierLookupHits; 1446 IdentifierInfo *Found; 1447 1448 public: 1449 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 1450 unsigned &NumIdentifierLookups, 1451 unsigned &NumIdentifierLookupHits) 1452 : Name(Name), PriorGeneration(PriorGeneration), 1453 NumIdentifierLookups(NumIdentifierLookups), 1454 NumIdentifierLookupHits(NumIdentifierLookupHits), 1455 Found() 1456 { 1457 } 1458 1459 static bool visit(ModuleFile &M, void *UserData) { 1460 IdentifierLookupVisitor *This 1461 = static_cast<IdentifierLookupVisitor *>(UserData); 1462 1463 // If we've already searched this module file, skip it now. 1464 if (M.Generation <= This->PriorGeneration) 1465 return true; 1466 1467 ASTIdentifierLookupTable *IdTable 1468 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 1469 if (!IdTable) 1470 return false; 1471 1472 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), 1473 M, This->Found); 1474 ++This->NumIdentifierLookups; 1475 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait); 1476 if (Pos == IdTable->end()) 1477 return false; 1478 1479 // Dereferencing the iterator has the effect of building the 1480 // IdentifierInfo node and populating it with the various 1481 // declarations it needs. 1482 ++This->NumIdentifierLookupHits; 1483 This->Found = *Pos; 1484 return true; 1485 } 1486 1487 // \brief Retrieve the identifier info found within the module 1488 // files. 1489 IdentifierInfo *getIdentifierInfo() const { return Found; } 1490 }; 1491 } 1492 1493 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 1494 // Note that we are loading an identifier. 1495 Deserializing AnIdentifier(this); 1496 1497 unsigned PriorGeneration = 0; 1498 if (getContext().getLangOpts().Modules) 1499 PriorGeneration = IdentifierGeneration[&II]; 1500 1501 // If there is a global index, look there first to determine which modules 1502 // provably do not have any results for this identifier. 1503 GlobalModuleIndex::HitSet Hits; 1504 GlobalModuleIndex::HitSet *HitsPtr = 0; 1505 if (!loadGlobalIndex()) { 1506 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 1507 HitsPtr = &Hits; 1508 } 1509 } 1510 1511 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 1512 NumIdentifierLookups, 1513 NumIdentifierLookupHits); 1514 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); 1515 markIdentifierUpToDate(&II); 1516 } 1517 1518 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 1519 if (!II) 1520 return; 1521 1522 II->setOutOfDate(false); 1523 1524 // Update the generation for this identifier. 1525 if (getContext().getLangOpts().Modules) 1526 IdentifierGeneration[II] = CurrentGeneration; 1527 } 1528 1529 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 1530 // If this ID is bogus, just return an empty input file. 1531 if (ID == 0 || ID > F.InputFilesLoaded.size()) 1532 return InputFile(); 1533 1534 // If we've already loaded this input file, return it. 1535 if (F.InputFilesLoaded[ID-1].getFile()) 1536 return F.InputFilesLoaded[ID-1]; 1537 1538 // Go find this input file. 1539 BitstreamCursor &Cursor = F.InputFilesCursor; 1540 SavedStreamPosition SavedPosition(Cursor); 1541 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 1542 1543 unsigned Code = Cursor.ReadCode(); 1544 RecordData Record; 1545 StringRef Blob; 1546 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) { 1547 case INPUT_FILE: { 1548 unsigned StoredID = Record[0]; 1549 assert(ID == StoredID && "Bogus stored ID or offset"); 1550 (void)StoredID; 1551 off_t StoredSize = (off_t)Record[1]; 1552 time_t StoredTime = (time_t)Record[2]; 1553 bool Overridden = (bool)Record[3]; 1554 1555 // Get the file entry for this input file. 1556 StringRef OrigFilename = Blob; 1557 std::string Filename = OrigFilename; 1558 MaybeAddSystemRootToFilename(F, Filename); 1559 const FileEntry *File 1560 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime) 1561 : FileMgr.getFile(Filename, /*OpenFile=*/false); 1562 1563 // If we didn't find the file, resolve it relative to the 1564 // original directory from which this AST file was created. 1565 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() && 1566 F.OriginalDir != CurrentDir) { 1567 std::string Resolved = resolveFileRelativeToOriginalDir(Filename, 1568 F.OriginalDir, 1569 CurrentDir); 1570 if (!Resolved.empty()) 1571 File = FileMgr.getFile(Resolved); 1572 } 1573 1574 // For an overridden file, create a virtual file with the stored 1575 // size/timestamp. 1576 if (Overridden && File == 0) { 1577 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 1578 } 1579 1580 if (File == 0) { 1581 if (Complain) { 1582 std::string ErrorStr = "could not find file '"; 1583 ErrorStr += Filename; 1584 ErrorStr += "' referenced by AST file"; 1585 Error(ErrorStr.c_str()); 1586 } 1587 return InputFile(); 1588 } 1589 1590 // Check if there was a request to override the contents of the file 1591 // that was part of the precompiled header. Overridding such a file 1592 // can lead to problems when lexing using the source locations from the 1593 // PCH. 1594 SourceManager &SM = getSourceManager(); 1595 if (!Overridden && SM.isFileOverridden(File)) { 1596 if (Complain) 1597 Error(diag::err_fe_pch_file_overridden, Filename); 1598 // After emitting the diagnostic, recover by disabling the override so 1599 // that the original file will be used. 1600 SM.disableFileContentsOverride(File); 1601 // The FileEntry is a virtual file entry with the size of the contents 1602 // that would override the original contents. Set it to the original's 1603 // size/time. 1604 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 1605 StoredSize, StoredTime); 1606 } 1607 1608 bool IsOutOfDate = false; 1609 1610 // For an overridden file, there is nothing to validate. 1611 if (!Overridden && (StoredSize != File->getSize() 1612 #if !defined(LLVM_ON_WIN32) 1613 // In our regression testing, the Windows file system seems to 1614 // have inconsistent modification times that sometimes 1615 // erroneously trigger this error-handling path. 1616 || StoredTime != File->getModificationTime() 1617 #endif 1618 )) { 1619 if (Complain) 1620 Error(diag::err_fe_pch_file_modified, Filename, F.FileName); 1621 IsOutOfDate = true; 1622 } 1623 1624 InputFile IF = InputFile(File, Overridden, IsOutOfDate); 1625 1626 // Note that we've loaded this input file. 1627 F.InputFilesLoaded[ID-1] = IF; 1628 return IF; 1629 } 1630 } 1631 1632 return InputFile(); 1633 } 1634 1635 const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) { 1636 ModuleFile &M = ModuleMgr.getPrimaryModule(); 1637 std::string Filename = filenameStrRef; 1638 MaybeAddSystemRootToFilename(M, Filename); 1639 const FileEntry *File = FileMgr.getFile(Filename); 1640 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() && 1641 M.OriginalDir != CurrentDir) { 1642 std::string resolved = resolveFileRelativeToOriginalDir(Filename, 1643 M.OriginalDir, 1644 CurrentDir); 1645 if (!resolved.empty()) 1646 File = FileMgr.getFile(resolved); 1647 } 1648 1649 return File; 1650 } 1651 1652 /// \brief If we are loading a relocatable PCH file, and the filename is 1653 /// not an absolute path, add the system root to the beginning of the file 1654 /// name. 1655 void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M, 1656 std::string &Filename) { 1657 // If this is not a relocatable PCH file, there's nothing to do. 1658 if (!M.RelocatablePCH) 1659 return; 1660 1661 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 1662 return; 1663 1664 if (isysroot.empty()) { 1665 // If no system root was given, default to '/' 1666 Filename.insert(Filename.begin(), '/'); 1667 return; 1668 } 1669 1670 unsigned Length = isysroot.size(); 1671 if (isysroot[Length - 1] != '/') 1672 Filename.insert(Filename.begin(), '/'); 1673 1674 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end()); 1675 } 1676 1677 ASTReader::ASTReadResult 1678 ASTReader::ReadControlBlock(ModuleFile &F, 1679 SmallVectorImpl<ImportedModule> &Loaded, 1680 unsigned ClientLoadCapabilities) { 1681 BitstreamCursor &Stream = F.Stream; 1682 1683 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 1684 Error("malformed block record in AST file"); 1685 return Failure; 1686 } 1687 1688 // Read all of the records and blocks in the control block. 1689 RecordData Record; 1690 while (1) { 1691 llvm::BitstreamEntry Entry = Stream.advance(); 1692 1693 switch (Entry.Kind) { 1694 case llvm::BitstreamEntry::Error: 1695 Error("malformed block record in AST file"); 1696 return Failure; 1697 case llvm::BitstreamEntry::EndBlock: 1698 // Validate all of the non-system input files. 1699 if (!DisableValidation) { 1700 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 1701 // All user input files reside at the index range [0, Record[1]). 1702 // Record is the one from INPUT_FILE_OFFSETS. 1703 for (unsigned I = 0, N = Record[1]; I < N; ++I) { 1704 InputFile IF = getInputFile(F, I+1, Complain); 1705 if (!IF.getFile() || IF.isOutOfDate()) 1706 return OutOfDate; 1707 } 1708 } 1709 return Success; 1710 1711 case llvm::BitstreamEntry::SubBlock: 1712 switch (Entry.ID) { 1713 case INPUT_FILES_BLOCK_ID: 1714 F.InputFilesCursor = Stream; 1715 if (Stream.SkipBlock() || // Skip with the main cursor 1716 // Read the abbreviations 1717 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 1718 Error("malformed block record in AST file"); 1719 return Failure; 1720 } 1721 continue; 1722 1723 default: 1724 if (Stream.SkipBlock()) { 1725 Error("malformed block record in AST file"); 1726 return Failure; 1727 } 1728 continue; 1729 } 1730 1731 case llvm::BitstreamEntry::Record: 1732 // The interesting case. 1733 break; 1734 } 1735 1736 // Read and process a record. 1737 Record.clear(); 1738 StringRef Blob; 1739 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 1740 case METADATA: { 1741 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1742 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 1743 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1744 : diag::warn_pch_version_too_new); 1745 return VersionMismatch; 1746 } 1747 1748 bool hasErrors = Record[5]; 1749 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 1750 Diag(diag::err_pch_with_compiler_errors); 1751 return HadErrors; 1752 } 1753 1754 F.RelocatablePCH = Record[4]; 1755 1756 const std::string &CurBranch = getClangFullRepositoryVersion(); 1757 StringRef ASTBranch = Blob; 1758 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 1759 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 1760 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 1761 return VersionMismatch; 1762 } 1763 break; 1764 } 1765 1766 case IMPORTS: { 1767 // Load each of the imported PCH files. 1768 unsigned Idx = 0, N = Record.size(); 1769 while (Idx < N) { 1770 // Read information about the AST file. 1771 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 1772 // The import location will be the local one for now; we will adjust 1773 // all import locations of module imports after the global source 1774 // location info are setup. 1775 SourceLocation ImportLoc = 1776 SourceLocation::getFromRawEncoding(Record[Idx++]); 1777 unsigned Length = Record[Idx++]; 1778 SmallString<128> ImportedFile(Record.begin() + Idx, 1779 Record.begin() + Idx + Length); 1780 Idx += Length; 1781 1782 // Load the AST file. 1783 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded, 1784 ClientLoadCapabilities)) { 1785 case Failure: return Failure; 1786 // If we have to ignore the dependency, we'll have to ignore this too. 1787 case OutOfDate: return OutOfDate; 1788 case VersionMismatch: return VersionMismatch; 1789 case ConfigurationMismatch: return ConfigurationMismatch; 1790 case HadErrors: return HadErrors; 1791 case Success: break; 1792 } 1793 } 1794 break; 1795 } 1796 1797 case LANGUAGE_OPTIONS: { 1798 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 1799 if (Listener && &F == *ModuleMgr.begin() && 1800 ParseLanguageOptions(Record, Complain, *Listener) && 1801 !DisableValidation) 1802 return ConfigurationMismatch; 1803 break; 1804 } 1805 1806 case TARGET_OPTIONS: { 1807 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1808 if (Listener && &F == *ModuleMgr.begin() && 1809 ParseTargetOptions(Record, Complain, *Listener) && 1810 !DisableValidation) 1811 return ConfigurationMismatch; 1812 break; 1813 } 1814 1815 case DIAGNOSTIC_OPTIONS: { 1816 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1817 if (Listener && &F == *ModuleMgr.begin() && 1818 ParseDiagnosticOptions(Record, Complain, *Listener) && 1819 !DisableValidation) 1820 return ConfigurationMismatch; 1821 break; 1822 } 1823 1824 case FILE_SYSTEM_OPTIONS: { 1825 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1826 if (Listener && &F == *ModuleMgr.begin() && 1827 ParseFileSystemOptions(Record, Complain, *Listener) && 1828 !DisableValidation) 1829 return ConfigurationMismatch; 1830 break; 1831 } 1832 1833 case HEADER_SEARCH_OPTIONS: { 1834 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1835 if (Listener && &F == *ModuleMgr.begin() && 1836 ParseHeaderSearchOptions(Record, Complain, *Listener) && 1837 !DisableValidation) 1838 return ConfigurationMismatch; 1839 break; 1840 } 1841 1842 case PREPROCESSOR_OPTIONS: { 1843 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1844 if (Listener && &F == *ModuleMgr.begin() && 1845 ParsePreprocessorOptions(Record, Complain, *Listener, 1846 SuggestedPredefines) && 1847 !DisableValidation) 1848 return ConfigurationMismatch; 1849 break; 1850 } 1851 1852 case ORIGINAL_FILE: 1853 F.OriginalSourceFileID = FileID::get(Record[0]); 1854 F.ActualOriginalSourceFileName = Blob; 1855 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 1856 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName); 1857 break; 1858 1859 case ORIGINAL_FILE_ID: 1860 F.OriginalSourceFileID = FileID::get(Record[0]); 1861 break; 1862 1863 case ORIGINAL_PCH_DIR: 1864 F.OriginalDir = Blob; 1865 break; 1866 1867 case INPUT_FILE_OFFSETS: 1868 F.InputFileOffsets = (const uint32_t *)Blob.data(); 1869 F.InputFilesLoaded.resize(Record[0]); 1870 break; 1871 } 1872 } 1873 } 1874 1875 bool ASTReader::ReadASTBlock(ModuleFile &F) { 1876 BitstreamCursor &Stream = F.Stream; 1877 1878 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 1879 Error("malformed block record in AST file"); 1880 return true; 1881 } 1882 1883 // Read all of the records and blocks for the AST file. 1884 RecordData Record; 1885 while (1) { 1886 llvm::BitstreamEntry Entry = Stream.advance(); 1887 1888 switch (Entry.Kind) { 1889 case llvm::BitstreamEntry::Error: 1890 Error("error at end of module block in AST file"); 1891 return true; 1892 case llvm::BitstreamEntry::EndBlock: { 1893 DeclContext *DC = Context.getTranslationUnitDecl(); 1894 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage()) 1895 DC->setMustBuildLookupTable(); 1896 1897 return false; 1898 } 1899 case llvm::BitstreamEntry::SubBlock: 1900 switch (Entry.ID) { 1901 case DECLTYPES_BLOCK_ID: 1902 // We lazily load the decls block, but we want to set up the 1903 // DeclsCursor cursor to point into it. Clone our current bitcode 1904 // cursor to it, enter the block and read the abbrevs in that block. 1905 // With the main cursor, we just skip over it. 1906 F.DeclsCursor = Stream; 1907 if (Stream.SkipBlock() || // Skip with the main cursor. 1908 // Read the abbrevs. 1909 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 1910 Error("malformed block record in AST file"); 1911 return true; 1912 } 1913 break; 1914 1915 case DECL_UPDATES_BLOCK_ID: 1916 if (Stream.SkipBlock()) { 1917 Error("malformed block record in AST file"); 1918 return true; 1919 } 1920 break; 1921 1922 case PREPROCESSOR_BLOCK_ID: 1923 F.MacroCursor = Stream; 1924 if (!PP.getExternalSource()) 1925 PP.setExternalSource(this); 1926 1927 if (Stream.SkipBlock() || 1928 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 1929 Error("malformed block record in AST file"); 1930 return true; 1931 } 1932 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 1933 break; 1934 1935 case PREPROCESSOR_DETAIL_BLOCK_ID: 1936 F.PreprocessorDetailCursor = Stream; 1937 if (Stream.SkipBlock() || 1938 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 1939 PREPROCESSOR_DETAIL_BLOCK_ID)) { 1940 Error("malformed preprocessor detail record in AST file"); 1941 return true; 1942 } 1943 F.PreprocessorDetailStartOffset 1944 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 1945 1946 if (!PP.getPreprocessingRecord()) 1947 PP.createPreprocessingRecord(); 1948 if (!PP.getPreprocessingRecord()->getExternalSource()) 1949 PP.getPreprocessingRecord()->SetExternalSource(*this); 1950 break; 1951 1952 case SOURCE_MANAGER_BLOCK_ID: 1953 if (ReadSourceManagerBlock(F)) 1954 return true; 1955 break; 1956 1957 case SUBMODULE_BLOCK_ID: 1958 if (ReadSubmoduleBlock(F)) 1959 return true; 1960 break; 1961 1962 case COMMENTS_BLOCK_ID: { 1963 BitstreamCursor C = Stream; 1964 if (Stream.SkipBlock() || 1965 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 1966 Error("malformed comments block in AST file"); 1967 return true; 1968 } 1969 CommentsCursors.push_back(std::make_pair(C, &F)); 1970 break; 1971 } 1972 1973 default: 1974 if (Stream.SkipBlock()) { 1975 Error("malformed block record in AST file"); 1976 return true; 1977 } 1978 break; 1979 } 1980 continue; 1981 1982 case llvm::BitstreamEntry::Record: 1983 // The interesting case. 1984 break; 1985 } 1986 1987 // Read and process a record. 1988 Record.clear(); 1989 StringRef Blob; 1990 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 1991 default: // Default behavior: ignore. 1992 break; 1993 1994 case TYPE_OFFSET: { 1995 if (F.LocalNumTypes != 0) { 1996 Error("duplicate TYPE_OFFSET record in AST file"); 1997 return true; 1998 } 1999 F.TypeOffsets = (const uint32_t *)Blob.data(); 2000 F.LocalNumTypes = Record[0]; 2001 unsigned LocalBaseTypeIndex = Record[1]; 2002 F.BaseTypeIndex = getTotalNumTypes(); 2003 2004 if (F.LocalNumTypes > 0) { 2005 // Introduce the global -> local mapping for types within this module. 2006 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 2007 2008 // Introduce the local -> global mapping for types within this module. 2009 F.TypeRemap.insertOrReplace( 2010 std::make_pair(LocalBaseTypeIndex, 2011 F.BaseTypeIndex - LocalBaseTypeIndex)); 2012 2013 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 2014 } 2015 break; 2016 } 2017 2018 case DECL_OFFSET: { 2019 if (F.LocalNumDecls != 0) { 2020 Error("duplicate DECL_OFFSET record in AST file"); 2021 return true; 2022 } 2023 F.DeclOffsets = (const DeclOffset *)Blob.data(); 2024 F.LocalNumDecls = Record[0]; 2025 unsigned LocalBaseDeclID = Record[1]; 2026 F.BaseDeclID = getTotalNumDecls(); 2027 2028 if (F.LocalNumDecls > 0) { 2029 // Introduce the global -> local mapping for declarations within this 2030 // module. 2031 GlobalDeclMap.insert( 2032 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 2033 2034 // Introduce the local -> global mapping for declarations within this 2035 // module. 2036 F.DeclRemap.insertOrReplace( 2037 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 2038 2039 // Introduce the global -> local mapping for declarations within this 2040 // module. 2041 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 2042 2043 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 2044 } 2045 break; 2046 } 2047 2048 case TU_UPDATE_LEXICAL: { 2049 DeclContext *TU = Context.getTranslationUnitDecl(); 2050 DeclContextInfo &Info = F.DeclContextInfos[TU]; 2051 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data()); 2052 Info.NumLexicalDecls 2053 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair)); 2054 TU->setHasExternalLexicalStorage(true); 2055 break; 2056 } 2057 2058 case UPDATE_VISIBLE: { 2059 unsigned Idx = 0; 2060 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 2061 ASTDeclContextNameLookupTable *Table = 2062 ASTDeclContextNameLookupTable::Create( 2063 (const unsigned char *)Blob.data() + Record[Idx++], 2064 (const unsigned char *)Blob.data(), 2065 ASTDeclContextNameLookupTrait(*this, F)); 2066 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU? 2067 DeclContext *TU = Context.getTranslationUnitDecl(); 2068 F.DeclContextInfos[TU].NameLookupTableData = Table; 2069 TU->setHasExternalVisibleStorage(true); 2070 } else 2071 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F)); 2072 break; 2073 } 2074 2075 case IDENTIFIER_TABLE: 2076 F.IdentifierTableData = Blob.data(); 2077 if (Record[0]) { 2078 F.IdentifierLookupTable 2079 = ASTIdentifierLookupTable::Create( 2080 (const unsigned char *)F.IdentifierTableData + Record[0], 2081 (const unsigned char *)F.IdentifierTableData, 2082 ASTIdentifierLookupTrait(*this, F)); 2083 2084 PP.getIdentifierTable().setExternalIdentifierLookup(this); 2085 } 2086 break; 2087 2088 case IDENTIFIER_OFFSET: { 2089 if (F.LocalNumIdentifiers != 0) { 2090 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2091 return true; 2092 } 2093 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 2094 F.LocalNumIdentifiers = Record[0]; 2095 unsigned LocalBaseIdentifierID = Record[1]; 2096 F.BaseIdentifierID = getTotalNumIdentifiers(); 2097 2098 if (F.LocalNumIdentifiers > 0) { 2099 // Introduce the global -> local mapping for identifiers within this 2100 // module. 2101 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 2102 &F)); 2103 2104 // Introduce the local -> global mapping for identifiers within this 2105 // module. 2106 F.IdentifierRemap.insertOrReplace( 2107 std::make_pair(LocalBaseIdentifierID, 2108 F.BaseIdentifierID - LocalBaseIdentifierID)); 2109 2110 IdentifiersLoaded.resize(IdentifiersLoaded.size() 2111 + F.LocalNumIdentifiers); 2112 } 2113 break; 2114 } 2115 2116 case EXTERNAL_DEFINITIONS: 2117 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2118 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2119 break; 2120 2121 case SPECIAL_TYPES: 2122 if (SpecialTypes.empty()) { 2123 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2124 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 2125 break; 2126 } 2127 2128 if (SpecialTypes.size() != Record.size()) { 2129 Error("invalid special-types record"); 2130 return true; 2131 } 2132 2133 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2134 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 2135 if (!SpecialTypes[I]) 2136 SpecialTypes[I] = ID; 2137 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 2138 // merge step? 2139 } 2140 break; 2141 2142 case STATISTICS: 2143 TotalNumStatements += Record[0]; 2144 TotalNumMacros += Record[1]; 2145 TotalLexicalDeclContexts += Record[2]; 2146 TotalVisibleDeclContexts += Record[3]; 2147 break; 2148 2149 case UNUSED_FILESCOPED_DECLS: 2150 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2151 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 2152 break; 2153 2154 case DELEGATING_CTORS: 2155 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2156 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 2157 break; 2158 2159 case WEAK_UNDECLARED_IDENTIFIERS: 2160 if (Record.size() % 4 != 0) { 2161 Error("invalid weak identifiers record"); 2162 return true; 2163 } 2164 2165 // FIXME: Ignore weak undeclared identifiers from non-original PCH 2166 // files. This isn't the way to do it :) 2167 WeakUndeclaredIdentifiers.clear(); 2168 2169 // Translate the weak, undeclared identifiers into global IDs. 2170 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 2171 WeakUndeclaredIdentifiers.push_back( 2172 getGlobalIdentifierID(F, Record[I++])); 2173 WeakUndeclaredIdentifiers.push_back( 2174 getGlobalIdentifierID(F, Record[I++])); 2175 WeakUndeclaredIdentifiers.push_back( 2176 ReadSourceLocation(F, Record, I).getRawEncoding()); 2177 WeakUndeclaredIdentifiers.push_back(Record[I++]); 2178 } 2179 break; 2180 2181 case LOCALLY_SCOPED_EXTERN_C_DECLS: 2182 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2183 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I])); 2184 break; 2185 2186 case SELECTOR_OFFSETS: { 2187 F.SelectorOffsets = (const uint32_t *)Blob.data(); 2188 F.LocalNumSelectors = Record[0]; 2189 unsigned LocalBaseSelectorID = Record[1]; 2190 F.BaseSelectorID = getTotalNumSelectors(); 2191 2192 if (F.LocalNumSelectors > 0) { 2193 // Introduce the global -> local mapping for selectors within this 2194 // module. 2195 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 2196 2197 // Introduce the local -> global mapping for selectors within this 2198 // module. 2199 F.SelectorRemap.insertOrReplace( 2200 std::make_pair(LocalBaseSelectorID, 2201 F.BaseSelectorID - LocalBaseSelectorID)); 2202 2203 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 2204 } 2205 break; 2206 } 2207 2208 case METHOD_POOL: 2209 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 2210 if (Record[0]) 2211 F.SelectorLookupTable 2212 = ASTSelectorLookupTable::Create( 2213 F.SelectorLookupTableData + Record[0], 2214 F.SelectorLookupTableData, 2215 ASTSelectorLookupTrait(*this, F)); 2216 TotalNumMethodPoolEntries += Record[1]; 2217 break; 2218 2219 case REFERENCED_SELECTOR_POOL: 2220 if (!Record.empty()) { 2221 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 2222 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 2223 Record[Idx++])); 2224 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 2225 getRawEncoding()); 2226 } 2227 } 2228 break; 2229 2230 case PP_COUNTER_VALUE: 2231 if (!Record.empty() && Listener) 2232 Listener->ReadCounter(F, Record[0]); 2233 break; 2234 2235 case FILE_SORTED_DECLS: 2236 F.FileSortedDecls = (const DeclID *)Blob.data(); 2237 F.NumFileSortedDecls = Record[0]; 2238 break; 2239 2240 case SOURCE_LOCATION_OFFSETS: { 2241 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 2242 F.LocalNumSLocEntries = Record[0]; 2243 unsigned SLocSpaceSize = Record[1]; 2244 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2245 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2246 SLocSpaceSize); 2247 // Make our entry in the range map. BaseID is negative and growing, so 2248 // we invert it. Because we invert it, though, we need the other end of 2249 // the range. 2250 unsigned RangeStart = 2251 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2252 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2253 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2254 2255 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2256 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2257 GlobalSLocOffsetMap.insert( 2258 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2259 - SLocSpaceSize,&F)); 2260 2261 // Initialize the remapping table. 2262 // Invalid stays invalid. 2263 F.SLocRemap.insert(std::make_pair(0U, 0)); 2264 // This module. Base was 2 when being compiled. 2265 F.SLocRemap.insert(std::make_pair(2U, 2266 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2267 2268 TotalNumSLocEntries += F.LocalNumSLocEntries; 2269 break; 2270 } 2271 2272 case MODULE_OFFSET_MAP: { 2273 // Additional remapping information. 2274 const unsigned char *Data = (const unsigned char*)Blob.data(); 2275 const unsigned char *DataEnd = Data + Blob.size(); 2276 2277 // Continuous range maps we may be updating in our module. 2278 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap); 2279 ContinuousRangeMap<uint32_t, int, 2>::Builder 2280 IdentifierRemap(F.IdentifierRemap); 2281 ContinuousRangeMap<uint32_t, int, 2>::Builder 2282 MacroRemap(F.MacroRemap); 2283 ContinuousRangeMap<uint32_t, int, 2>::Builder 2284 PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2285 ContinuousRangeMap<uint32_t, int, 2>::Builder 2286 SubmoduleRemap(F.SubmoduleRemap); 2287 ContinuousRangeMap<uint32_t, int, 2>::Builder 2288 SelectorRemap(F.SelectorRemap); 2289 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap); 2290 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap); 2291 2292 while(Data < DataEnd) { 2293 uint16_t Len = io::ReadUnalignedLE16(Data); 2294 StringRef Name = StringRef((const char*)Data, Len); 2295 Data += Len; 2296 ModuleFile *OM = ModuleMgr.lookup(Name); 2297 if (!OM) { 2298 Error("SourceLocation remap refers to unknown module"); 2299 return true; 2300 } 2301 2302 uint32_t SLocOffset = io::ReadUnalignedLE32(Data); 2303 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data); 2304 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data); 2305 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data); 2306 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data); 2307 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data); 2308 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data); 2309 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data); 2310 2311 // Source location offset is mapped to OM->SLocEntryBaseOffset. 2312 SLocRemap.insert(std::make_pair(SLocOffset, 2313 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset))); 2314 IdentifierRemap.insert( 2315 std::make_pair(IdentifierIDOffset, 2316 OM->BaseIdentifierID - IdentifierIDOffset)); 2317 MacroRemap.insert(std::make_pair(MacroIDOffset, 2318 OM->BaseMacroID - MacroIDOffset)); 2319 PreprocessedEntityRemap.insert( 2320 std::make_pair(PreprocessedEntityIDOffset, 2321 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset)); 2322 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset, 2323 OM->BaseSubmoduleID - SubmoduleIDOffset)); 2324 SelectorRemap.insert(std::make_pair(SelectorIDOffset, 2325 OM->BaseSelectorID - SelectorIDOffset)); 2326 DeclRemap.insert(std::make_pair(DeclIDOffset, 2327 OM->BaseDeclID - DeclIDOffset)); 2328 2329 TypeRemap.insert(std::make_pair(TypeIndexOffset, 2330 OM->BaseTypeIndex - TypeIndexOffset)); 2331 2332 // Global -> local mappings. 2333 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 2334 } 2335 break; 2336 } 2337 2338 case SOURCE_MANAGER_LINE_TABLE: 2339 if (ParseLineTable(F, Record)) 2340 return true; 2341 break; 2342 2343 case SOURCE_LOCATION_PRELOADS: { 2344 // Need to transform from the local view (1-based IDs) to the global view, 2345 // which is based off F.SLocEntryBaseID. 2346 if (!F.PreloadSLocEntries.empty()) { 2347 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2348 return true; 2349 } 2350 2351 F.PreloadSLocEntries.swap(Record); 2352 break; 2353 } 2354 2355 case EXT_VECTOR_DECLS: 2356 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2357 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2358 break; 2359 2360 case VTABLE_USES: 2361 if (Record.size() % 3 != 0) { 2362 Error("Invalid VTABLE_USES record"); 2363 return true; 2364 } 2365 2366 // Later tables overwrite earlier ones. 2367 // FIXME: Modules will have some trouble with this. This is clearly not 2368 // the right way to do this. 2369 VTableUses.clear(); 2370 2371 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 2372 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 2373 VTableUses.push_back( 2374 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 2375 VTableUses.push_back(Record[Idx++]); 2376 } 2377 break; 2378 2379 case DYNAMIC_CLASSES: 2380 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2381 DynamicClasses.push_back(getGlobalDeclID(F, Record[I])); 2382 break; 2383 2384 case PENDING_IMPLICIT_INSTANTIATIONS: 2385 if (PendingInstantiations.size() % 2 != 0) { 2386 Error("Invalid existing PendingInstantiations"); 2387 return true; 2388 } 2389 2390 if (Record.size() % 2 != 0) { 2391 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 2392 return true; 2393 } 2394 2395 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2396 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 2397 PendingInstantiations.push_back( 2398 ReadSourceLocation(F, Record, I).getRawEncoding()); 2399 } 2400 break; 2401 2402 case SEMA_DECL_REFS: 2403 // Later tables overwrite earlier ones. 2404 // FIXME: Modules will have some trouble with this. 2405 SemaDeclRefs.clear(); 2406 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2407 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2408 break; 2409 2410 case PPD_ENTITIES_OFFSETS: { 2411 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 2412 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 2413 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 2414 2415 unsigned LocalBasePreprocessedEntityID = Record[0]; 2416 2417 unsigned StartingID; 2418 if (!PP.getPreprocessingRecord()) 2419 PP.createPreprocessingRecord(); 2420 if (!PP.getPreprocessingRecord()->getExternalSource()) 2421 PP.getPreprocessingRecord()->SetExternalSource(*this); 2422 StartingID 2423 = PP.getPreprocessingRecord() 2424 ->allocateLoadedEntities(F.NumPreprocessedEntities); 2425 F.BasePreprocessedEntityID = StartingID; 2426 2427 if (F.NumPreprocessedEntities > 0) { 2428 // Introduce the global -> local mapping for preprocessed entities in 2429 // this module. 2430 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 2431 2432 // Introduce the local -> global mapping for preprocessed entities in 2433 // this module. 2434 F.PreprocessedEntityRemap.insertOrReplace( 2435 std::make_pair(LocalBasePreprocessedEntityID, 2436 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 2437 } 2438 2439 break; 2440 } 2441 2442 case DECL_UPDATE_OFFSETS: { 2443 if (Record.size() % 2 != 0) { 2444 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2445 return true; 2446 } 2447 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2448 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])] 2449 .push_back(std::make_pair(&F, Record[I+1])); 2450 break; 2451 } 2452 2453 case DECL_REPLACEMENTS: { 2454 if (Record.size() % 3 != 0) { 2455 Error("invalid DECL_REPLACEMENTS block in AST file"); 2456 return true; 2457 } 2458 for (unsigned I = 0, N = Record.size(); I != N; I += 3) 2459 ReplacedDecls[getGlobalDeclID(F, Record[I])] 2460 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]); 2461 break; 2462 } 2463 2464 case OBJC_CATEGORIES_MAP: { 2465 if (F.LocalNumObjCCategoriesInMap != 0) { 2466 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 2467 return true; 2468 } 2469 2470 F.LocalNumObjCCategoriesInMap = Record[0]; 2471 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 2472 break; 2473 } 2474 2475 case OBJC_CATEGORIES: 2476 F.ObjCCategories.swap(Record); 2477 break; 2478 2479 case CXX_BASE_SPECIFIER_OFFSETS: { 2480 if (F.LocalNumCXXBaseSpecifiers != 0) { 2481 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2482 return true; 2483 } 2484 2485 F.LocalNumCXXBaseSpecifiers = Record[0]; 2486 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data(); 2487 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers; 2488 break; 2489 } 2490 2491 case DIAG_PRAGMA_MAPPINGS: 2492 if (F.PragmaDiagMappings.empty()) 2493 F.PragmaDiagMappings.swap(Record); 2494 else 2495 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 2496 Record.begin(), Record.end()); 2497 break; 2498 2499 case CUDA_SPECIAL_DECL_REFS: 2500 // Later tables overwrite earlier ones. 2501 // FIXME: Modules will have trouble with this. 2502 CUDASpecialDeclRefs.clear(); 2503 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2504 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2505 break; 2506 2507 case HEADER_SEARCH_TABLE: { 2508 F.HeaderFileInfoTableData = Blob.data(); 2509 F.LocalNumHeaderFileInfos = Record[1]; 2510 if (Record[0]) { 2511 F.HeaderFileInfoTable 2512 = HeaderFileInfoLookupTable::Create( 2513 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2514 (const unsigned char *)F.HeaderFileInfoTableData, 2515 HeaderFileInfoTrait(*this, F, 2516 &PP.getHeaderSearchInfo(), 2517 Blob.data() + Record[2])); 2518 2519 PP.getHeaderSearchInfo().SetExternalSource(this); 2520 if (!PP.getHeaderSearchInfo().getExternalLookup()) 2521 PP.getHeaderSearchInfo().SetExternalLookup(this); 2522 } 2523 break; 2524 } 2525 2526 case FP_PRAGMA_OPTIONS: 2527 // Later tables overwrite earlier ones. 2528 FPPragmaOptions.swap(Record); 2529 break; 2530 2531 case OPENCL_EXTENSIONS: 2532 // Later tables overwrite earlier ones. 2533 OpenCLExtensions.swap(Record); 2534 break; 2535 2536 case TENTATIVE_DEFINITIONS: 2537 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2538 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2539 break; 2540 2541 case KNOWN_NAMESPACES: 2542 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2543 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 2544 break; 2545 2546 case UNDEFINED_BUT_USED: 2547 if (UndefinedButUsed.size() % 2 != 0) { 2548 Error("Invalid existing UndefinedButUsed"); 2549 return true; 2550 } 2551 2552 if (Record.size() % 2 != 0) { 2553 Error("invalid undefined-but-used record"); 2554 return true; 2555 } 2556 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2557 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 2558 UndefinedButUsed.push_back( 2559 ReadSourceLocation(F, Record, I).getRawEncoding()); 2560 } 2561 break; 2562 2563 case IMPORTED_MODULES: { 2564 if (F.Kind != MK_Module) { 2565 // If we aren't loading a module (which has its own exports), make 2566 // all of the imported modules visible. 2567 // FIXME: Deal with macros-only imports. 2568 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2569 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I])) 2570 ImportedModules.push_back(GlobalID); 2571 } 2572 } 2573 break; 2574 } 2575 2576 case LOCAL_REDECLARATIONS: { 2577 F.RedeclarationChains.swap(Record); 2578 break; 2579 } 2580 2581 case LOCAL_REDECLARATIONS_MAP: { 2582 if (F.LocalNumRedeclarationsInMap != 0) { 2583 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file"); 2584 return true; 2585 } 2586 2587 F.LocalNumRedeclarationsInMap = Record[0]; 2588 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data(); 2589 break; 2590 } 2591 2592 case MERGED_DECLARATIONS: { 2593 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) { 2594 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]); 2595 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID]; 2596 for (unsigned N = Record[Idx++]; N > 0; --N) 2597 Decls.push_back(getGlobalDeclID(F, Record[Idx++])); 2598 } 2599 break; 2600 } 2601 2602 case MACRO_OFFSET: { 2603 if (F.LocalNumMacros != 0) { 2604 Error("duplicate MACRO_OFFSET record in AST file"); 2605 return true; 2606 } 2607 F.MacroOffsets = (const uint32_t *)Blob.data(); 2608 F.LocalNumMacros = Record[0]; 2609 unsigned LocalBaseMacroID = Record[1]; 2610 F.BaseMacroID = getTotalNumMacros(); 2611 2612 if (F.LocalNumMacros > 0) { 2613 // Introduce the global -> local mapping for macros within this module. 2614 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 2615 2616 // Introduce the local -> global mapping for macros within this module. 2617 F.MacroRemap.insertOrReplace( 2618 std::make_pair(LocalBaseMacroID, 2619 F.BaseMacroID - LocalBaseMacroID)); 2620 2621 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 2622 } 2623 break; 2624 } 2625 2626 case MACRO_UPDATES: { 2627 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2628 MacroID ID = getGlobalMacroID(F, Record[I++]); 2629 if (I == N) 2630 break; 2631 2632 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I); 2633 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);; 2634 MacroUpdate Update; 2635 Update.UndefLoc = UndefLoc; 2636 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update)); 2637 } 2638 break; 2639 } 2640 } 2641 } 2642 } 2643 2644 /// \brief Move the given method to the back of the global list of methods. 2645 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 2646 // Find the entry for this selector in the method pool. 2647 Sema::GlobalMethodPool::iterator Known 2648 = S.MethodPool.find(Method->getSelector()); 2649 if (Known == S.MethodPool.end()) 2650 return; 2651 2652 // Retrieve the appropriate method list. 2653 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 2654 : Known->second.second; 2655 bool Found = false; 2656 for (ObjCMethodList *List = &Start; List; List = List->Next) { 2657 if (!Found) { 2658 if (List->Method == Method) { 2659 Found = true; 2660 } else { 2661 // Keep searching. 2662 continue; 2663 } 2664 } 2665 2666 if (List->Next) 2667 List->Method = List->Next->Method; 2668 else 2669 List->Method = Method; 2670 } 2671 } 2672 2673 void ASTReader::makeNamesVisible(const HiddenNames &Names) { 2674 for (unsigned I = 0, N = Names.size(); I != N; ++I) { 2675 switch (Names[I].getKind()) { 2676 case HiddenName::Declaration: { 2677 Decl *D = Names[I].getDecl(); 2678 bool wasHidden = D->Hidden; 2679 D->Hidden = false; 2680 2681 if (wasHidden && SemaObj) { 2682 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 2683 moveMethodToBackOfGlobalList(*SemaObj, Method); 2684 } 2685 } 2686 break; 2687 } 2688 case HiddenName::MacroVisibility: { 2689 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro(); 2690 Macro.second->setHidden(!Macro.second->isPublic()); 2691 if (Macro.second->isDefined()) { 2692 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2693 } 2694 break; 2695 } 2696 2697 case HiddenName::MacroUndef: { 2698 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro(); 2699 if (Macro.second->isDefined()) { 2700 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc()); 2701 if (PPMutationListener *Listener = PP.getPPMutationListener()) 2702 Listener->UndefinedMacro(Macro.second); 2703 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2704 } 2705 break; 2706 } 2707 } 2708 } 2709 } 2710 2711 void ASTReader::makeModuleVisible(Module *Mod, 2712 Module::NameVisibilityKind NameVisibility, 2713 SourceLocation ImportLoc) { 2714 llvm::SmallPtrSet<Module *, 4> Visited; 2715 SmallVector<Module *, 4> Stack; 2716 Stack.push_back(Mod); 2717 while (!Stack.empty()) { 2718 Mod = Stack.back(); 2719 Stack.pop_back(); 2720 2721 if (NameVisibility <= Mod->NameVisibility) { 2722 // This module already has this level of visibility (or greater), so 2723 // there is nothing more to do. 2724 continue; 2725 } 2726 2727 if (!Mod->isAvailable()) { 2728 // Modules that aren't available cannot be made visible. 2729 continue; 2730 } 2731 2732 // Update the module's name visibility. 2733 Mod->NameVisibility = NameVisibility; 2734 2735 // If we've already deserialized any names from this module, 2736 // mark them as visible. 2737 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 2738 if (Hidden != HiddenNamesMap.end()) { 2739 makeNamesVisible(Hidden->second); 2740 HiddenNamesMap.erase(Hidden); 2741 } 2742 2743 // Push any non-explicit submodules onto the stack to be marked as 2744 // visible. 2745 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2746 SubEnd = Mod->submodule_end(); 2747 Sub != SubEnd; ++Sub) { 2748 if (!(*Sub)->IsExplicit && Visited.insert(*Sub)) 2749 Stack.push_back(*Sub); 2750 } 2751 2752 // Push any exported modules onto the stack to be marked as visible. 2753 SmallVector<Module *, 16> Exports; 2754 Mod->getExportedModules(Exports); 2755 for (SmallVectorImpl<Module *>::iterator 2756 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 2757 Module *Exported = *I; 2758 if (Visited.insert(Exported)) 2759 Stack.push_back(Exported); 2760 } 2761 } 2762 } 2763 2764 bool ASTReader::loadGlobalIndex() { 2765 if (GlobalIndex) 2766 return false; 2767 2768 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 2769 !Context.getLangOpts().Modules) 2770 return true; 2771 2772 // Try to load the global index. 2773 TriedLoadingGlobalIndex = true; 2774 StringRef ModuleCachePath 2775 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 2776 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result 2777 = GlobalModuleIndex::readIndex(FileMgr, ModuleCachePath); 2778 if (!Result.first) 2779 return true; 2780 2781 GlobalIndex.reset(Result.first); 2782 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 2783 return false; 2784 } 2785 2786 bool ASTReader::isGlobalIndexUnavailable() const { 2787 return Context.getLangOpts().Modules && UseGlobalIndex && 2788 !hasGlobalIndex() && TriedLoadingGlobalIndex; 2789 } 2790 2791 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2792 ModuleKind Type, 2793 SourceLocation ImportLoc, 2794 unsigned ClientLoadCapabilities) { 2795 // Bump the generation number. 2796 unsigned PreviousGeneration = CurrentGeneration++; 2797 2798 unsigned NumModules = ModuleMgr.size(); 2799 SmallVector<ImportedModule, 4> Loaded; 2800 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc, 2801 /*ImportedBy=*/0, Loaded, 2802 ClientLoadCapabilities)) { 2803 case Failure: 2804 case OutOfDate: 2805 case VersionMismatch: 2806 case ConfigurationMismatch: 2807 case HadErrors: 2808 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end()); 2809 2810 // If we find that any modules are unusable, the global index is going 2811 // to be out-of-date. Just remove it. 2812 GlobalIndex.reset(); 2813 ModuleMgr.setGlobalIndex(0); 2814 return ReadResult; 2815 2816 case Success: 2817 break; 2818 } 2819 2820 // Here comes stuff that we only do once the entire chain is loaded. 2821 2822 // Load the AST blocks of all of the modules that we loaded. 2823 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2824 MEnd = Loaded.end(); 2825 M != MEnd; ++M) { 2826 ModuleFile &F = *M->Mod; 2827 2828 // Read the AST block. 2829 if (ReadASTBlock(F)) 2830 return Failure; 2831 2832 // Once read, set the ModuleFile bit base offset and update the size in 2833 // bits of all files we've seen. 2834 F.GlobalBitOffset = TotalModulesSizeInBits; 2835 TotalModulesSizeInBits += F.SizeInBits; 2836 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 2837 2838 // Preload SLocEntries. 2839 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 2840 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 2841 // Load it through the SourceManager and don't call ReadSLocEntry() 2842 // directly because the entry may have already been loaded in which case 2843 // calling ReadSLocEntry() directly would trigger an assertion in 2844 // SourceManager. 2845 SourceMgr.getLoadedSLocEntryByID(Index); 2846 } 2847 } 2848 2849 // Setup the import locations. 2850 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2851 MEnd = Loaded.end(); 2852 M != MEnd; ++M) { 2853 ModuleFile &F = *M->Mod; 2854 F.DirectImportLoc = ImportLoc; 2855 if (!M->ImportedBy) 2856 F.ImportLoc = M->ImportLoc; 2857 else 2858 F.ImportLoc = ReadSourceLocation(*M->ImportedBy, 2859 M->ImportLoc.getRawEncoding()); 2860 } 2861 2862 // Mark all of the identifiers in the identifier table as being out of date, 2863 // so that various accessors know to check the loaded modules when the 2864 // identifier is used. 2865 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 2866 IdEnd = PP.getIdentifierTable().end(); 2867 Id != IdEnd; ++Id) 2868 Id->second->setOutOfDate(true); 2869 2870 // Resolve any unresolved module exports. 2871 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) { 2872 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I]; 2873 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 2874 Module *ResolvedMod = getSubmodule(GlobalID); 2875 2876 if (Unresolved.IsImport) { 2877 if (ResolvedMod) 2878 Unresolved.Mod->Imports.push_back(ResolvedMod); 2879 continue; 2880 } 2881 2882 if (ResolvedMod || Unresolved.IsWildcard) 2883 Unresolved.Mod->Exports.push_back( 2884 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 2885 } 2886 UnresolvedModuleImportExports.clear(); 2887 2888 InitializeContext(); 2889 2890 if (DeserializationListener) 2891 DeserializationListener->ReaderInitialized(this); 2892 2893 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 2894 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) { 2895 PrimaryModule.OriginalSourceFileID 2896 = FileID::get(PrimaryModule.SLocEntryBaseID 2897 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1); 2898 2899 // If this AST file is a precompiled preamble, then set the 2900 // preamble file ID of the source manager to the file source file 2901 // from which the preamble was built. 2902 if (Type == MK_Preamble) { 2903 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 2904 } else if (Type == MK_MainFile) { 2905 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 2906 } 2907 } 2908 2909 // For any Objective-C class definitions we have already loaded, make sure 2910 // that we load any additional categories. 2911 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 2912 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 2913 ObjCClassesLoaded[I], 2914 PreviousGeneration); 2915 } 2916 2917 return Success; 2918 } 2919 2920 ASTReader::ASTReadResult 2921 ASTReader::ReadASTCore(StringRef FileName, 2922 ModuleKind Type, 2923 SourceLocation ImportLoc, 2924 ModuleFile *ImportedBy, 2925 SmallVectorImpl<ImportedModule> &Loaded, 2926 unsigned ClientLoadCapabilities) { 2927 ModuleFile *M; 2928 bool NewModule; 2929 std::string ErrorStr; 2930 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportLoc, 2931 ImportedBy, CurrentGeneration, 2932 ErrorStr); 2933 2934 if (!M) { 2935 // We couldn't load the module. 2936 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 2937 + ErrorStr; 2938 Error(Msg); 2939 return Failure; 2940 } 2941 2942 if (!NewModule) { 2943 // We've already loaded this module. 2944 return Success; 2945 } 2946 2947 // FIXME: This seems rather a hack. Should CurrentDir be part of the 2948 // module? 2949 if (FileName != "-") { 2950 CurrentDir = llvm::sys::path::parent_path(FileName); 2951 if (CurrentDir.empty()) CurrentDir = "."; 2952 } 2953 2954 ModuleFile &F = *M; 2955 BitstreamCursor &Stream = F.Stream; 2956 Stream.init(F.StreamFile); 2957 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2958 2959 // Sniff for the signature. 2960 if (Stream.Read(8) != 'C' || 2961 Stream.Read(8) != 'P' || 2962 Stream.Read(8) != 'C' || 2963 Stream.Read(8) != 'H') { 2964 Diag(diag::err_not_a_pch_file) << FileName; 2965 return Failure; 2966 } 2967 2968 // This is used for compatibility with older PCH formats. 2969 bool HaveReadControlBlock = false; 2970 2971 while (1) { 2972 llvm::BitstreamEntry Entry = Stream.advance(); 2973 2974 switch (Entry.Kind) { 2975 case llvm::BitstreamEntry::Error: 2976 case llvm::BitstreamEntry::EndBlock: 2977 case llvm::BitstreamEntry::Record: 2978 Error("invalid record at top-level of AST file"); 2979 return Failure; 2980 2981 case llvm::BitstreamEntry::SubBlock: 2982 break; 2983 } 2984 2985 // We only know the control subblock ID. 2986 switch (Entry.ID) { 2987 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2988 if (Stream.ReadBlockInfoBlock()) { 2989 Error("malformed BlockInfoBlock in AST file"); 2990 return Failure; 2991 } 2992 break; 2993 case CONTROL_BLOCK_ID: 2994 HaveReadControlBlock = true; 2995 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) { 2996 case Success: 2997 break; 2998 2999 case Failure: return Failure; 3000 case OutOfDate: return OutOfDate; 3001 case VersionMismatch: return VersionMismatch; 3002 case ConfigurationMismatch: return ConfigurationMismatch; 3003 case HadErrors: return HadErrors; 3004 } 3005 break; 3006 case AST_BLOCK_ID: 3007 if (!HaveReadControlBlock) { 3008 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 3009 Diag(diag::warn_pch_version_too_old); 3010 return VersionMismatch; 3011 } 3012 3013 // Record that we've loaded this module. 3014 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 3015 return Success; 3016 3017 default: 3018 if (Stream.SkipBlock()) { 3019 Error("malformed block record in AST file"); 3020 return Failure; 3021 } 3022 break; 3023 } 3024 } 3025 3026 return Success; 3027 } 3028 3029 void ASTReader::InitializeContext() { 3030 // If there's a listener, notify them that we "read" the translation unit. 3031 if (DeserializationListener) 3032 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 3033 Context.getTranslationUnitDecl()); 3034 3035 // Make sure we load the declaration update records for the translation unit, 3036 // if there are any. 3037 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, 3038 Context.getTranslationUnitDecl()); 3039 3040 // FIXME: Find a better way to deal with collisions between these 3041 // built-in types. Right now, we just ignore the problem. 3042 3043 // Load the special types. 3044 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 3045 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 3046 if (!Context.CFConstantStringTypeDecl) 3047 Context.setCFConstantStringType(GetType(String)); 3048 } 3049 3050 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 3051 QualType FileType = GetType(File); 3052 if (FileType.isNull()) { 3053 Error("FILE type is NULL"); 3054 return; 3055 } 3056 3057 if (!Context.FILEDecl) { 3058 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 3059 Context.setFILEDecl(Typedef->getDecl()); 3060 else { 3061 const TagType *Tag = FileType->getAs<TagType>(); 3062 if (!Tag) { 3063 Error("Invalid FILE type in AST file"); 3064 return; 3065 } 3066 Context.setFILEDecl(Tag->getDecl()); 3067 } 3068 } 3069 } 3070 3071 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 3072 QualType Jmp_bufType = GetType(Jmp_buf); 3073 if (Jmp_bufType.isNull()) { 3074 Error("jmp_buf type is NULL"); 3075 return; 3076 } 3077 3078 if (!Context.jmp_bufDecl) { 3079 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 3080 Context.setjmp_bufDecl(Typedef->getDecl()); 3081 else { 3082 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 3083 if (!Tag) { 3084 Error("Invalid jmp_buf type in AST file"); 3085 return; 3086 } 3087 Context.setjmp_bufDecl(Tag->getDecl()); 3088 } 3089 } 3090 } 3091 3092 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 3093 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 3094 if (Sigjmp_bufType.isNull()) { 3095 Error("sigjmp_buf type is NULL"); 3096 return; 3097 } 3098 3099 if (!Context.sigjmp_bufDecl) { 3100 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 3101 Context.setsigjmp_bufDecl(Typedef->getDecl()); 3102 else { 3103 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 3104 assert(Tag && "Invalid sigjmp_buf type in AST file"); 3105 Context.setsigjmp_bufDecl(Tag->getDecl()); 3106 } 3107 } 3108 } 3109 3110 if (unsigned ObjCIdRedef 3111 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 3112 if (Context.ObjCIdRedefinitionType.isNull()) 3113 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 3114 } 3115 3116 if (unsigned ObjCClassRedef 3117 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 3118 if (Context.ObjCClassRedefinitionType.isNull()) 3119 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 3120 } 3121 3122 if (unsigned ObjCSelRedef 3123 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 3124 if (Context.ObjCSelRedefinitionType.isNull()) 3125 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 3126 } 3127 3128 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 3129 QualType Ucontext_tType = GetType(Ucontext_t); 3130 if (Ucontext_tType.isNull()) { 3131 Error("ucontext_t type is NULL"); 3132 return; 3133 } 3134 3135 if (!Context.ucontext_tDecl) { 3136 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 3137 Context.setucontext_tDecl(Typedef->getDecl()); 3138 else { 3139 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 3140 assert(Tag && "Invalid ucontext_t type in AST file"); 3141 Context.setucontext_tDecl(Tag->getDecl()); 3142 } 3143 } 3144 } 3145 } 3146 3147 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 3148 3149 // If there were any CUDA special declarations, deserialize them. 3150 if (!CUDASpecialDeclRefs.empty()) { 3151 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 3152 Context.setcudaConfigureCallDecl( 3153 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 3154 } 3155 3156 // Re-export any modules that were imported by a non-module AST file. 3157 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) { 3158 if (Module *Imported = getSubmodule(ImportedModules[I])) 3159 makeModuleVisible(Imported, Module::AllVisible, 3160 /*ImportLoc=*/SourceLocation()); 3161 } 3162 ImportedModules.clear(); 3163 } 3164 3165 void ASTReader::finalizeForWriting() { 3166 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(), 3167 HiddenEnd = HiddenNamesMap.end(); 3168 Hidden != HiddenEnd; ++Hidden) { 3169 makeNamesVisible(Hidden->second); 3170 } 3171 HiddenNamesMap.clear(); 3172 } 3173 3174 /// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan 3175 /// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning 3176 /// false on success and true on failure. 3177 static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) { 3178 while (1) { 3179 llvm::BitstreamEntry Entry = Cursor.advance(); 3180 switch (Entry.Kind) { 3181 case llvm::BitstreamEntry::Error: 3182 case llvm::BitstreamEntry::EndBlock: 3183 return true; 3184 3185 case llvm::BitstreamEntry::Record: 3186 // Ignore top-level records. 3187 Cursor.skipRecord(Entry.ID); 3188 break; 3189 3190 case llvm::BitstreamEntry::SubBlock: 3191 if (Entry.ID == CONTROL_BLOCK_ID) { 3192 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID)) 3193 return true; 3194 // Found it! 3195 return false; 3196 } 3197 3198 if (Cursor.SkipBlock()) 3199 return true; 3200 } 3201 } 3202 } 3203 3204 /// \brief Retrieve the name of the original source file name 3205 /// directly from the AST file, without actually loading the AST 3206 /// file. 3207 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 3208 FileManager &FileMgr, 3209 DiagnosticsEngine &Diags) { 3210 // Open the AST file. 3211 std::string ErrStr; 3212 OwningPtr<llvm::MemoryBuffer> Buffer; 3213 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 3214 if (!Buffer) { 3215 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; 3216 return std::string(); 3217 } 3218 3219 // Initialize the stream 3220 llvm::BitstreamReader StreamFile; 3221 BitstreamCursor Stream; 3222 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3223 (const unsigned char *)Buffer->getBufferEnd()); 3224 Stream.init(StreamFile); 3225 3226 // Sniff for the signature. 3227 if (Stream.Read(8) != 'C' || 3228 Stream.Read(8) != 'P' || 3229 Stream.Read(8) != 'C' || 3230 Stream.Read(8) != 'H') { 3231 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 3232 return std::string(); 3233 } 3234 3235 // Scan for the CONTROL_BLOCK_ID block. 3236 if (SkipCursorToControlBlock(Stream)) { 3237 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3238 return std::string(); 3239 } 3240 3241 // Scan for ORIGINAL_FILE inside the control block. 3242 RecordData Record; 3243 while (1) { 3244 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 3245 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3246 return std::string(); 3247 3248 if (Entry.Kind != llvm::BitstreamEntry::Record) { 3249 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3250 return std::string(); 3251 } 3252 3253 Record.clear(); 3254 StringRef Blob; 3255 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE) 3256 return Blob.str(); 3257 } 3258 } 3259 3260 namespace { 3261 class SimplePCHValidator : public ASTReaderListener { 3262 const LangOptions &ExistingLangOpts; 3263 const TargetOptions &ExistingTargetOpts; 3264 const PreprocessorOptions &ExistingPPOpts; 3265 FileManager &FileMgr; 3266 3267 public: 3268 SimplePCHValidator(const LangOptions &ExistingLangOpts, 3269 const TargetOptions &ExistingTargetOpts, 3270 const PreprocessorOptions &ExistingPPOpts, 3271 FileManager &FileMgr) 3272 : ExistingLangOpts(ExistingLangOpts), 3273 ExistingTargetOpts(ExistingTargetOpts), 3274 ExistingPPOpts(ExistingPPOpts), 3275 FileMgr(FileMgr) 3276 { 3277 } 3278 3279 virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 3280 bool Complain) { 3281 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0); 3282 } 3283 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, 3284 bool Complain) { 3285 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0); 3286 } 3287 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 3288 bool Complain, 3289 std::string &SuggestedPredefines) { 3290 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr, 3291 SuggestedPredefines); 3292 } 3293 }; 3294 } 3295 3296 bool ASTReader::readASTFileControlBlock(StringRef Filename, 3297 FileManager &FileMgr, 3298 ASTReaderListener &Listener) { 3299 // Open the AST file. 3300 std::string ErrStr; 3301 OwningPtr<llvm::MemoryBuffer> Buffer; 3302 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr)); 3303 if (!Buffer) { 3304 return true; 3305 } 3306 3307 // Initialize the stream 3308 llvm::BitstreamReader StreamFile; 3309 BitstreamCursor Stream; 3310 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3311 (const unsigned char *)Buffer->getBufferEnd()); 3312 Stream.init(StreamFile); 3313 3314 // Sniff for the signature. 3315 if (Stream.Read(8) != 'C' || 3316 Stream.Read(8) != 'P' || 3317 Stream.Read(8) != 'C' || 3318 Stream.Read(8) != 'H') { 3319 return true; 3320 } 3321 3322 // Scan for the CONTROL_BLOCK_ID block. 3323 if (SkipCursorToControlBlock(Stream)) 3324 return true; 3325 3326 // Scan for ORIGINAL_FILE inside the control block. 3327 RecordData Record; 3328 while (1) { 3329 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 3330 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3331 return false; 3332 3333 if (Entry.Kind != llvm::BitstreamEntry::Record) 3334 return true; 3335 3336 Record.clear(); 3337 StringRef Blob; 3338 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 3339 switch ((ControlRecordTypes)RecCode) { 3340 case METADATA: { 3341 if (Record[0] != VERSION_MAJOR) 3342 return true; 3343 3344 const std::string &CurBranch = getClangFullRepositoryVersion(); 3345 if (StringRef(CurBranch) != Blob) 3346 return true; 3347 3348 break; 3349 } 3350 case LANGUAGE_OPTIONS: 3351 if (ParseLanguageOptions(Record, false, Listener)) 3352 return true; 3353 break; 3354 3355 case TARGET_OPTIONS: 3356 if (ParseTargetOptions(Record, false, Listener)) 3357 return true; 3358 break; 3359 3360 case DIAGNOSTIC_OPTIONS: 3361 if (ParseDiagnosticOptions(Record, false, Listener)) 3362 return true; 3363 break; 3364 3365 case FILE_SYSTEM_OPTIONS: 3366 if (ParseFileSystemOptions(Record, false, Listener)) 3367 return true; 3368 break; 3369 3370 case HEADER_SEARCH_OPTIONS: 3371 if (ParseHeaderSearchOptions(Record, false, Listener)) 3372 return true; 3373 break; 3374 3375 case PREPROCESSOR_OPTIONS: { 3376 std::string IgnoredSuggestedPredefines; 3377 if (ParsePreprocessorOptions(Record, false, Listener, 3378 IgnoredSuggestedPredefines)) 3379 return true; 3380 break; 3381 } 3382 3383 default: 3384 // No other validation to perform. 3385 break; 3386 } 3387 } 3388 } 3389 3390 3391 bool ASTReader::isAcceptableASTFile(StringRef Filename, 3392 FileManager &FileMgr, 3393 const LangOptions &LangOpts, 3394 const TargetOptions &TargetOpts, 3395 const PreprocessorOptions &PPOpts) { 3396 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr); 3397 return !readASTFileControlBlock(Filename, FileMgr, validator); 3398 } 3399 3400 bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) { 3401 // Enter the submodule block. 3402 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 3403 Error("malformed submodule block record in AST file"); 3404 return true; 3405 } 3406 3407 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 3408 bool First = true; 3409 Module *CurrentModule = 0; 3410 RecordData Record; 3411 while (true) { 3412 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks(); 3413 3414 switch (Entry.Kind) { 3415 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 3416 case llvm::BitstreamEntry::Error: 3417 Error("malformed block record in AST file"); 3418 return true; 3419 case llvm::BitstreamEntry::EndBlock: 3420 return false; 3421 case llvm::BitstreamEntry::Record: 3422 // The interesting case. 3423 break; 3424 } 3425 3426 // Read a record. 3427 StringRef Blob; 3428 Record.clear(); 3429 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) { 3430 default: // Default behavior: ignore. 3431 break; 3432 3433 case SUBMODULE_DEFINITION: { 3434 if (First) { 3435 Error("missing submodule metadata record at beginning of block"); 3436 return true; 3437 } 3438 3439 if (Record.size() < 7) { 3440 Error("malformed module definition"); 3441 return true; 3442 } 3443 3444 StringRef Name = Blob; 3445 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]); 3446 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]); 3447 bool IsFramework = Record[2]; 3448 bool IsExplicit = Record[3]; 3449 bool IsSystem = Record[4]; 3450 bool InferSubmodules = Record[5]; 3451 bool InferExplicitSubmodules = Record[6]; 3452 bool InferExportWildcard = Record[7]; 3453 3454 Module *ParentModule = 0; 3455 if (Parent) 3456 ParentModule = getSubmodule(Parent); 3457 3458 // Retrieve this (sub)module from the module map, creating it if 3459 // necessary. 3460 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, 3461 IsFramework, 3462 IsExplicit).first; 3463 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 3464 if (GlobalIndex >= SubmodulesLoaded.size() || 3465 SubmodulesLoaded[GlobalIndex]) { 3466 Error("too many submodules"); 3467 return true; 3468 } 3469 3470 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 3471 if (CurFile != F.File) { 3472 if (!Diags.isDiagnosticInFlight()) { 3473 Diag(diag::err_module_file_conflict) 3474 << CurrentModule->getTopLevelModuleName() 3475 << CurFile->getName() 3476 << F.File->getName(); 3477 } 3478 return true; 3479 } 3480 } 3481 CurrentModule->setASTFile(F.File); 3482 CurrentModule->IsFromModuleFile = true; 3483 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 3484 CurrentModule->InferSubmodules = InferSubmodules; 3485 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 3486 CurrentModule->InferExportWildcard = InferExportWildcard; 3487 if (DeserializationListener) 3488 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 3489 3490 SubmodulesLoaded[GlobalIndex] = CurrentModule; 3491 3492 // Clear out link libraries; the module file has them. 3493 CurrentModule->LinkLibraries.clear(); 3494 break; 3495 } 3496 3497 case SUBMODULE_UMBRELLA_HEADER: { 3498 if (First) { 3499 Error("missing submodule metadata record at beginning of block"); 3500 return true; 3501 } 3502 3503 if (!CurrentModule) 3504 break; 3505 3506 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) { 3507 if (!CurrentModule->getUmbrellaHeader()) 3508 ModMap.setUmbrellaHeader(CurrentModule, Umbrella); 3509 else if (CurrentModule->getUmbrellaHeader() != Umbrella) { 3510 Error("mismatched umbrella headers in submodule"); 3511 return true; 3512 } 3513 } 3514 break; 3515 } 3516 3517 case SUBMODULE_HEADER: { 3518 if (First) { 3519 Error("missing submodule metadata record at beginning of block"); 3520 return true; 3521 } 3522 3523 if (!CurrentModule) 3524 break; 3525 3526 // We lazily associate headers with their modules via the HeaderInfoTable. 3527 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 3528 // of complete filenames or remove it entirely. 3529 break; 3530 } 3531 3532 case SUBMODULE_EXCLUDED_HEADER: { 3533 if (First) { 3534 Error("missing submodule metadata record at beginning of block"); 3535 return true; 3536 } 3537 3538 if (!CurrentModule) 3539 break; 3540 3541 // We lazily associate headers with their modules via the HeaderInfoTable. 3542 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 3543 // of complete filenames or remove it entirely. 3544 break; 3545 } 3546 3547 case SUBMODULE_TOPHEADER: { 3548 if (First) { 3549 Error("missing submodule metadata record at beginning of block"); 3550 return true; 3551 } 3552 3553 if (!CurrentModule) 3554 break; 3555 3556 CurrentModule->addTopHeaderFilename(Blob); 3557 break; 3558 } 3559 3560 case SUBMODULE_UMBRELLA_DIR: { 3561 if (First) { 3562 Error("missing submodule metadata record at beginning of block"); 3563 return true; 3564 } 3565 3566 if (!CurrentModule) 3567 break; 3568 3569 if (const DirectoryEntry *Umbrella 3570 = PP.getFileManager().getDirectory(Blob)) { 3571 if (!CurrentModule->getUmbrellaDir()) 3572 ModMap.setUmbrellaDir(CurrentModule, Umbrella); 3573 else if (CurrentModule->getUmbrellaDir() != Umbrella) { 3574 Error("mismatched umbrella directories in submodule"); 3575 return true; 3576 } 3577 } 3578 break; 3579 } 3580 3581 case SUBMODULE_METADATA: { 3582 if (!First) { 3583 Error("submodule metadata record not at beginning of block"); 3584 return true; 3585 } 3586 First = false; 3587 3588 F.BaseSubmoduleID = getTotalNumSubmodules(); 3589 F.LocalNumSubmodules = Record[0]; 3590 unsigned LocalBaseSubmoduleID = Record[1]; 3591 if (F.LocalNumSubmodules > 0) { 3592 // Introduce the global -> local mapping for submodules within this 3593 // module. 3594 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 3595 3596 // Introduce the local -> global mapping for submodules within this 3597 // module. 3598 F.SubmoduleRemap.insertOrReplace( 3599 std::make_pair(LocalBaseSubmoduleID, 3600 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 3601 3602 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 3603 } 3604 break; 3605 } 3606 3607 case SUBMODULE_IMPORTS: { 3608 if (First) { 3609 Error("missing submodule metadata record at beginning of block"); 3610 return true; 3611 } 3612 3613 if (!CurrentModule) 3614 break; 3615 3616 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 3617 UnresolvedModuleImportExport Unresolved; 3618 Unresolved.File = &F; 3619 Unresolved.Mod = CurrentModule; 3620 Unresolved.ID = Record[Idx]; 3621 Unresolved.IsImport = true; 3622 Unresolved.IsWildcard = false; 3623 UnresolvedModuleImportExports.push_back(Unresolved); 3624 } 3625 break; 3626 } 3627 3628 case SUBMODULE_EXPORTS: { 3629 if (First) { 3630 Error("missing submodule metadata record at beginning of block"); 3631 return true; 3632 } 3633 3634 if (!CurrentModule) 3635 break; 3636 3637 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 3638 UnresolvedModuleImportExport Unresolved; 3639 Unresolved.File = &F; 3640 Unresolved.Mod = CurrentModule; 3641 Unresolved.ID = Record[Idx]; 3642 Unresolved.IsImport = false; 3643 Unresolved.IsWildcard = Record[Idx + 1]; 3644 UnresolvedModuleImportExports.push_back(Unresolved); 3645 } 3646 3647 // Once we've loaded the set of exports, there's no reason to keep 3648 // the parsed, unresolved exports around. 3649 CurrentModule->UnresolvedExports.clear(); 3650 break; 3651 } 3652 case SUBMODULE_REQUIRES: { 3653 if (First) { 3654 Error("missing submodule metadata record at beginning of block"); 3655 return true; 3656 } 3657 3658 if (!CurrentModule) 3659 break; 3660 3661 CurrentModule->addRequirement(Blob, Context.getLangOpts(), 3662 Context.getTargetInfo()); 3663 break; 3664 } 3665 3666 case SUBMODULE_LINK_LIBRARY: 3667 if (First) { 3668 Error("missing submodule metadata record at beginning of block"); 3669 return true; 3670 } 3671 3672 if (!CurrentModule) 3673 break; 3674 3675 CurrentModule->LinkLibraries.push_back( 3676 Module::LinkLibrary(Blob, Record[0])); 3677 break; 3678 } 3679 } 3680 } 3681 3682 /// \brief Parse the record that corresponds to a LangOptions data 3683 /// structure. 3684 /// 3685 /// This routine parses the language options from the AST file and then gives 3686 /// them to the AST listener if one is set. 3687 /// 3688 /// \returns true if the listener deems the file unacceptable, false otherwise. 3689 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 3690 bool Complain, 3691 ASTReaderListener &Listener) { 3692 LangOptions LangOpts; 3693 unsigned Idx = 0; 3694 #define LANGOPT(Name, Bits, Default, Description) \ 3695 LangOpts.Name = Record[Idx++]; 3696 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 3697 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 3698 #include "clang/Basic/LangOptions.def" 3699 #define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++]; 3700 #include "clang/Basic/Sanitizers.def" 3701 3702 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 3703 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 3704 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 3705 3706 unsigned Length = Record[Idx++]; 3707 LangOpts.CurrentModule.assign(Record.begin() + Idx, 3708 Record.begin() + Idx + Length); 3709 3710 Idx += Length; 3711 3712 // Comment options. 3713 for (unsigned N = Record[Idx++]; N; --N) { 3714 LangOpts.CommentOpts.BlockCommandNames.push_back( 3715 ReadString(Record, Idx)); 3716 } 3717 3718 return Listener.ReadLanguageOptions(LangOpts, Complain); 3719 } 3720 3721 bool ASTReader::ParseTargetOptions(const RecordData &Record, 3722 bool Complain, 3723 ASTReaderListener &Listener) { 3724 unsigned Idx = 0; 3725 TargetOptions TargetOpts; 3726 TargetOpts.Triple = ReadString(Record, Idx); 3727 TargetOpts.CPU = ReadString(Record, Idx); 3728 TargetOpts.ABI = ReadString(Record, Idx); 3729 TargetOpts.CXXABI = ReadString(Record, Idx); 3730 TargetOpts.LinkerVersion = ReadString(Record, Idx); 3731 for (unsigned N = Record[Idx++]; N; --N) { 3732 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 3733 } 3734 for (unsigned N = Record[Idx++]; N; --N) { 3735 TargetOpts.Features.push_back(ReadString(Record, Idx)); 3736 } 3737 3738 return Listener.ReadTargetOptions(TargetOpts, Complain); 3739 } 3740 3741 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 3742 ASTReaderListener &Listener) { 3743 DiagnosticOptions DiagOpts; 3744 unsigned Idx = 0; 3745 #define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++]; 3746 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 3747 DiagOpts.set##Name(static_cast<Type>(Record[Idx++])); 3748 #include "clang/Basic/DiagnosticOptions.def" 3749 3750 for (unsigned N = Record[Idx++]; N; --N) { 3751 DiagOpts.Warnings.push_back(ReadString(Record, Idx)); 3752 } 3753 3754 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 3755 } 3756 3757 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 3758 ASTReaderListener &Listener) { 3759 FileSystemOptions FSOpts; 3760 unsigned Idx = 0; 3761 FSOpts.WorkingDir = ReadString(Record, Idx); 3762 return Listener.ReadFileSystemOptions(FSOpts, Complain); 3763 } 3764 3765 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 3766 bool Complain, 3767 ASTReaderListener &Listener) { 3768 HeaderSearchOptions HSOpts; 3769 unsigned Idx = 0; 3770 HSOpts.Sysroot = ReadString(Record, Idx); 3771 3772 // Include entries. 3773 for (unsigned N = Record[Idx++]; N; --N) { 3774 std::string Path = ReadString(Record, Idx); 3775 frontend::IncludeDirGroup Group 3776 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 3777 bool IsFramework = Record[Idx++]; 3778 bool IgnoreSysRoot = Record[Idx++]; 3779 HSOpts.UserEntries.push_back( 3780 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot)); 3781 } 3782 3783 // System header prefixes. 3784 for (unsigned N = Record[Idx++]; N; --N) { 3785 std::string Prefix = ReadString(Record, Idx); 3786 bool IsSystemHeader = Record[Idx++]; 3787 HSOpts.SystemHeaderPrefixes.push_back( 3788 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader)); 3789 } 3790 3791 HSOpts.ResourceDir = ReadString(Record, Idx); 3792 HSOpts.ModuleCachePath = ReadString(Record, Idx); 3793 HSOpts.DisableModuleHash = Record[Idx++]; 3794 HSOpts.UseBuiltinIncludes = Record[Idx++]; 3795 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 3796 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 3797 HSOpts.UseLibcxx = Record[Idx++]; 3798 3799 return Listener.ReadHeaderSearchOptions(HSOpts, Complain); 3800 } 3801 3802 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 3803 bool Complain, 3804 ASTReaderListener &Listener, 3805 std::string &SuggestedPredefines) { 3806 PreprocessorOptions PPOpts; 3807 unsigned Idx = 0; 3808 3809 // Macro definitions/undefs 3810 for (unsigned N = Record[Idx++]; N; --N) { 3811 std::string Macro = ReadString(Record, Idx); 3812 bool IsUndef = Record[Idx++]; 3813 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 3814 } 3815 3816 // Includes 3817 for (unsigned N = Record[Idx++]; N; --N) { 3818 PPOpts.Includes.push_back(ReadString(Record, Idx)); 3819 } 3820 3821 // Macro Includes 3822 for (unsigned N = Record[Idx++]; N; --N) { 3823 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 3824 } 3825 3826 PPOpts.UsePredefines = Record[Idx++]; 3827 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 3828 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx); 3829 PPOpts.ObjCXXARCStandardLibrary = 3830 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 3831 SuggestedPredefines.clear(); 3832 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 3833 SuggestedPredefines); 3834 } 3835 3836 std::pair<ModuleFile *, unsigned> 3837 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 3838 GlobalPreprocessedEntityMapType::iterator 3839 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 3840 assert(I != GlobalPreprocessedEntityMap.end() && 3841 "Corrupted global preprocessed entity map"); 3842 ModuleFile *M = I->second; 3843 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 3844 return std::make_pair(M, LocalIndex); 3845 } 3846 3847 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 3848 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 3849 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 3850 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 3851 Mod.NumPreprocessedEntities); 3852 3853 return std::make_pair(PreprocessingRecord::iterator(), 3854 PreprocessingRecord::iterator()); 3855 } 3856 3857 std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator> 3858 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 3859 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 3860 ModuleDeclIterator(this, &Mod, 3861 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 3862 } 3863 3864 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 3865 PreprocessedEntityID PPID = Index+1; 3866 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 3867 ModuleFile &M = *PPInfo.first; 3868 unsigned LocalIndex = PPInfo.second; 3869 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 3870 3871 if (!PP.getPreprocessingRecord()) { 3872 Error("no preprocessing record"); 3873 return 0; 3874 } 3875 3876 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 3877 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 3878 3879 llvm::BitstreamEntry Entry = 3880 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 3881 if (Entry.Kind != llvm::BitstreamEntry::Record) 3882 return 0; 3883 3884 // Read the record. 3885 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin), 3886 ReadSourceLocation(M, PPOffs.End)); 3887 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 3888 StringRef Blob; 3889 RecordData Record; 3890 PreprocessorDetailRecordTypes RecType = 3891 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord( 3892 Entry.ID, Record, &Blob); 3893 switch (RecType) { 3894 case PPD_MACRO_EXPANSION: { 3895 bool isBuiltin = Record[0]; 3896 IdentifierInfo *Name = 0; 3897 MacroDefinition *Def = 0; 3898 if (isBuiltin) 3899 Name = getLocalIdentifier(M, Record[1]); 3900 else { 3901 PreprocessedEntityID 3902 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]); 3903 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)); 3904 } 3905 3906 MacroExpansion *ME; 3907 if (isBuiltin) 3908 ME = new (PPRec) MacroExpansion(Name, Range); 3909 else 3910 ME = new (PPRec) MacroExpansion(Def, Range); 3911 3912 return ME; 3913 } 3914 3915 case PPD_MACRO_DEFINITION: { 3916 // Decode the identifier info and then check again; if the macro is 3917 // still defined and associated with the identifier, 3918 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 3919 MacroDefinition *MD 3920 = new (PPRec) MacroDefinition(II, Range); 3921 3922 if (DeserializationListener) 3923 DeserializationListener->MacroDefinitionRead(PPID, MD); 3924 3925 return MD; 3926 } 3927 3928 case PPD_INCLUSION_DIRECTIVE: { 3929 const char *FullFileNameStart = Blob.data() + Record[0]; 3930 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 3931 const FileEntry *File = 0; 3932 if (!FullFileName.empty()) 3933 File = PP.getFileManager().getFile(FullFileName); 3934 3935 // FIXME: Stable encoding 3936 InclusionDirective::InclusionKind Kind 3937 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 3938 InclusionDirective *ID 3939 = new (PPRec) InclusionDirective(PPRec, Kind, 3940 StringRef(Blob.data(), Record[0]), 3941 Record[1], Record[3], 3942 File, 3943 Range); 3944 return ID; 3945 } 3946 } 3947 3948 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 3949 } 3950 3951 /// \brief \arg SLocMapI points at a chunk of a module that contains no 3952 /// preprocessed entities or the entities it contains are not the ones we are 3953 /// looking for. Find the next module that contains entities and return the ID 3954 /// of the first entry. 3955 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 3956 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 3957 ++SLocMapI; 3958 for (GlobalSLocOffsetMapType::const_iterator 3959 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 3960 ModuleFile &M = *SLocMapI->second; 3961 if (M.NumPreprocessedEntities) 3962 return M.BasePreprocessedEntityID; 3963 } 3964 3965 return getTotalNumPreprocessedEntities(); 3966 } 3967 3968 namespace { 3969 3970 template <unsigned PPEntityOffset::*PPLoc> 3971 struct PPEntityComp { 3972 const ASTReader &Reader; 3973 ModuleFile &M; 3974 3975 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { } 3976 3977 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 3978 SourceLocation LHS = getLoc(L); 3979 SourceLocation RHS = getLoc(R); 3980 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3981 } 3982 3983 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 3984 SourceLocation LHS = getLoc(L); 3985 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3986 } 3987 3988 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 3989 SourceLocation RHS = getLoc(R); 3990 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3991 } 3992 3993 SourceLocation getLoc(const PPEntityOffset &PPE) const { 3994 return Reader.ReadSourceLocation(M, PPE.*PPLoc); 3995 } 3996 }; 3997 3998 } 3999 4000 /// \brief Returns the first preprocessed entity ID that ends after \arg BLoc. 4001 PreprocessedEntityID 4002 ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const { 4003 if (SourceMgr.isLocalSourceLocation(BLoc)) 4004 return getTotalNumPreprocessedEntities(); 4005 4006 GlobalSLocOffsetMapType::const_iterator 4007 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 4008 BLoc.getOffset() - 1); 4009 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4010 "Corrupted global sloc offset map"); 4011 4012 if (SLocMapI->second->NumPreprocessedEntities == 0) 4013 return findNextPreprocessedEntity(SLocMapI); 4014 4015 ModuleFile &M = *SLocMapI->second; 4016 typedef const PPEntityOffset *pp_iterator; 4017 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4018 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4019 4020 size_t Count = M.NumPreprocessedEntities; 4021 size_t Half; 4022 pp_iterator First = pp_begin; 4023 pp_iterator PPI; 4024 4025 // Do a binary search manually instead of using std::lower_bound because 4026 // The end locations of entities may be unordered (when a macro expansion 4027 // is inside another macro argument), but for this case it is not important 4028 // whether we get the first macro expansion or its containing macro. 4029 while (Count > 0) { 4030 Half = Count/2; 4031 PPI = First; 4032 std::advance(PPI, Half); 4033 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End), 4034 BLoc)){ 4035 First = PPI; 4036 ++First; 4037 Count = Count - Half - 1; 4038 } else 4039 Count = Half; 4040 } 4041 4042 if (PPI == pp_end) 4043 return findNextPreprocessedEntity(SLocMapI); 4044 4045 return M.BasePreprocessedEntityID + (PPI - pp_begin); 4046 } 4047 4048 /// \brief Returns the first preprocessed entity ID that begins after \arg ELoc. 4049 PreprocessedEntityID 4050 ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const { 4051 if (SourceMgr.isLocalSourceLocation(ELoc)) 4052 return getTotalNumPreprocessedEntities(); 4053 4054 GlobalSLocOffsetMapType::const_iterator 4055 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 4056 ELoc.getOffset() - 1); 4057 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4058 "Corrupted global sloc offset map"); 4059 4060 if (SLocMapI->second->NumPreprocessedEntities == 0) 4061 return findNextPreprocessedEntity(SLocMapI); 4062 4063 ModuleFile &M = *SLocMapI->second; 4064 typedef const PPEntityOffset *pp_iterator; 4065 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4066 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4067 pp_iterator PPI = 4068 std::upper_bound(pp_begin, pp_end, ELoc, 4069 PPEntityComp<&PPEntityOffset::Begin>(*this, M)); 4070 4071 if (PPI == pp_end) 4072 return findNextPreprocessedEntity(SLocMapI); 4073 4074 return M.BasePreprocessedEntityID + (PPI - pp_begin); 4075 } 4076 4077 /// \brief Returns a pair of [Begin, End) indices of preallocated 4078 /// preprocessed entities that \arg Range encompasses. 4079 std::pair<unsigned, unsigned> 4080 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 4081 if (Range.isInvalid()) 4082 return std::make_pair(0,0); 4083 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 4084 4085 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin()); 4086 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd()); 4087 return std::make_pair(BeginID, EndID); 4088 } 4089 4090 /// \brief Optionally returns true or false if the preallocated preprocessed 4091 /// entity with index \arg Index came from file \arg FID. 4092 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 4093 FileID FID) { 4094 if (FID.isInvalid()) 4095 return false; 4096 4097 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4098 ModuleFile &M = *PPInfo.first; 4099 unsigned LocalIndex = PPInfo.second; 4100 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4101 4102 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin); 4103 if (Loc.isInvalid()) 4104 return false; 4105 4106 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 4107 return true; 4108 else 4109 return false; 4110 } 4111 4112 namespace { 4113 /// \brief Visitor used to search for information about a header file. 4114 class HeaderFileInfoVisitor { 4115 const FileEntry *FE; 4116 4117 Optional<HeaderFileInfo> HFI; 4118 4119 public: 4120 explicit HeaderFileInfoVisitor(const FileEntry *FE) 4121 : FE(FE) { } 4122 4123 static bool visit(ModuleFile &M, void *UserData) { 4124 HeaderFileInfoVisitor *This 4125 = static_cast<HeaderFileInfoVisitor *>(UserData); 4126 4127 HeaderFileInfoLookupTable *Table 4128 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 4129 if (!Table) 4130 return false; 4131 4132 // Look in the on-disk hash table for an entry for this file name. 4133 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE); 4134 if (Pos == Table->end()) 4135 return false; 4136 4137 This->HFI = *Pos; 4138 return true; 4139 } 4140 4141 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 4142 }; 4143 } 4144 4145 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 4146 HeaderFileInfoVisitor Visitor(FE); 4147 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); 4148 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) { 4149 if (Listener) 4150 Listener->ReadHeaderFileInfo(*HFI, FE->getUID()); 4151 return *HFI; 4152 } 4153 4154 return HeaderFileInfo(); 4155 } 4156 4157 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 4158 // FIXME: Make it work properly with modules. 4159 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates; 4160 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 4161 ModuleFile &F = *(*I); 4162 unsigned Idx = 0; 4163 DiagStates.clear(); 4164 assert(!Diag.DiagStates.empty()); 4165 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one. 4166 while (Idx < F.PragmaDiagMappings.size()) { 4167 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 4168 unsigned DiagStateID = F.PragmaDiagMappings[Idx++]; 4169 if (DiagStateID != 0) { 4170 Diag.DiagStatePoints.push_back( 4171 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1], 4172 FullSourceLoc(Loc, SourceMgr))); 4173 continue; 4174 } 4175 4176 assert(DiagStateID == 0); 4177 // A new DiagState was created here. 4178 Diag.DiagStates.push_back(*Diag.GetCurDiagState()); 4179 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back(); 4180 DiagStates.push_back(NewState); 4181 Diag.DiagStatePoints.push_back( 4182 DiagnosticsEngine::DiagStatePoint(NewState, 4183 FullSourceLoc(Loc, SourceMgr))); 4184 while (1) { 4185 assert(Idx < F.PragmaDiagMappings.size() && 4186 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 4187 if (Idx >= F.PragmaDiagMappings.size()) { 4188 break; // Something is messed up but at least avoid infinite loop in 4189 // release build. 4190 } 4191 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 4192 if (DiagID == (unsigned)-1) { 4193 break; // no more diag/map pairs for this location. 4194 } 4195 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++]; 4196 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc); 4197 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo); 4198 } 4199 } 4200 } 4201 } 4202 4203 /// \brief Get the correct cursor and offset for loading a type. 4204 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 4205 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 4206 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 4207 ModuleFile *M = I->second; 4208 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 4209 } 4210 4211 /// \brief Read and return the type with the given index.. 4212 /// 4213 /// The index is the type ID, shifted and minus the number of predefs. This 4214 /// routine actually reads the record corresponding to the type at the given 4215 /// location. It is a helper routine for GetType, which deals with reading type 4216 /// IDs. 4217 QualType ASTReader::readTypeRecord(unsigned Index) { 4218 RecordLocation Loc = TypeCursorForIndex(Index); 4219 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 4220 4221 // Keep track of where we are in the stream, then jump back there 4222 // after reading this type. 4223 SavedStreamPosition SavedPosition(DeclsCursor); 4224 4225 ReadingKindTracker ReadingKind(Read_Type, *this); 4226 4227 // Note that we are loading a type record. 4228 Deserializing AType(this); 4229 4230 unsigned Idx = 0; 4231 DeclsCursor.JumpToBit(Loc.Offset); 4232 RecordData Record; 4233 unsigned Code = DeclsCursor.ReadCode(); 4234 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) { 4235 case TYPE_EXT_QUAL: { 4236 if (Record.size() != 2) { 4237 Error("Incorrect encoding of extended qualifier type"); 4238 return QualType(); 4239 } 4240 QualType Base = readType(*Loc.F, Record, Idx); 4241 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 4242 return Context.getQualifiedType(Base, Quals); 4243 } 4244 4245 case TYPE_COMPLEX: { 4246 if (Record.size() != 1) { 4247 Error("Incorrect encoding of complex type"); 4248 return QualType(); 4249 } 4250 QualType ElemType = readType(*Loc.F, Record, Idx); 4251 return Context.getComplexType(ElemType); 4252 } 4253 4254 case TYPE_POINTER: { 4255 if (Record.size() != 1) { 4256 Error("Incorrect encoding of pointer type"); 4257 return QualType(); 4258 } 4259 QualType PointeeType = readType(*Loc.F, Record, Idx); 4260 return Context.getPointerType(PointeeType); 4261 } 4262 4263 case TYPE_BLOCK_POINTER: { 4264 if (Record.size() != 1) { 4265 Error("Incorrect encoding of block pointer type"); 4266 return QualType(); 4267 } 4268 QualType PointeeType = readType(*Loc.F, Record, Idx); 4269 return Context.getBlockPointerType(PointeeType); 4270 } 4271 4272 case TYPE_LVALUE_REFERENCE: { 4273 if (Record.size() != 2) { 4274 Error("Incorrect encoding of lvalue reference type"); 4275 return QualType(); 4276 } 4277 QualType PointeeType = readType(*Loc.F, Record, Idx); 4278 return Context.getLValueReferenceType(PointeeType, Record[1]); 4279 } 4280 4281 case TYPE_RVALUE_REFERENCE: { 4282 if (Record.size() != 1) { 4283 Error("Incorrect encoding of rvalue reference type"); 4284 return QualType(); 4285 } 4286 QualType PointeeType = readType(*Loc.F, Record, Idx); 4287 return Context.getRValueReferenceType(PointeeType); 4288 } 4289 4290 case TYPE_MEMBER_POINTER: { 4291 if (Record.size() != 2) { 4292 Error("Incorrect encoding of member pointer type"); 4293 return QualType(); 4294 } 4295 QualType PointeeType = readType(*Loc.F, Record, Idx); 4296 QualType ClassType = readType(*Loc.F, Record, Idx); 4297 if (PointeeType.isNull() || ClassType.isNull()) 4298 return QualType(); 4299 4300 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 4301 } 4302 4303 case TYPE_CONSTANT_ARRAY: { 4304 QualType ElementType = readType(*Loc.F, Record, Idx); 4305 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4306 unsigned IndexTypeQuals = Record[2]; 4307 unsigned Idx = 3; 4308 llvm::APInt Size = ReadAPInt(Record, Idx); 4309 return Context.getConstantArrayType(ElementType, Size, 4310 ASM, IndexTypeQuals); 4311 } 4312 4313 case TYPE_INCOMPLETE_ARRAY: { 4314 QualType ElementType = readType(*Loc.F, Record, Idx); 4315 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4316 unsigned IndexTypeQuals = Record[2]; 4317 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 4318 } 4319 4320 case TYPE_VARIABLE_ARRAY: { 4321 QualType ElementType = readType(*Loc.F, Record, Idx); 4322 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4323 unsigned IndexTypeQuals = Record[2]; 4324 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 4325 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 4326 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 4327 ASM, IndexTypeQuals, 4328 SourceRange(LBLoc, RBLoc)); 4329 } 4330 4331 case TYPE_VECTOR: { 4332 if (Record.size() != 3) { 4333 Error("incorrect encoding of vector type in AST file"); 4334 return QualType(); 4335 } 4336 4337 QualType ElementType = readType(*Loc.F, Record, Idx); 4338 unsigned NumElements = Record[1]; 4339 unsigned VecKind = Record[2]; 4340 return Context.getVectorType(ElementType, NumElements, 4341 (VectorType::VectorKind)VecKind); 4342 } 4343 4344 case TYPE_EXT_VECTOR: { 4345 if (Record.size() != 3) { 4346 Error("incorrect encoding of extended vector type in AST file"); 4347 return QualType(); 4348 } 4349 4350 QualType ElementType = readType(*Loc.F, Record, Idx); 4351 unsigned NumElements = Record[1]; 4352 return Context.getExtVectorType(ElementType, NumElements); 4353 } 4354 4355 case TYPE_FUNCTION_NO_PROTO: { 4356 if (Record.size() != 6) { 4357 Error("incorrect encoding of no-proto function type"); 4358 return QualType(); 4359 } 4360 QualType ResultType = readType(*Loc.F, Record, Idx); 4361 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 4362 (CallingConv)Record[4], Record[5]); 4363 return Context.getFunctionNoProtoType(ResultType, Info); 4364 } 4365 4366 case TYPE_FUNCTION_PROTO: { 4367 QualType ResultType = readType(*Loc.F, Record, Idx); 4368 4369 FunctionProtoType::ExtProtoInfo EPI; 4370 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 4371 /*hasregparm*/ Record[2], 4372 /*regparm*/ Record[3], 4373 static_cast<CallingConv>(Record[4]), 4374 /*produces*/ Record[5]); 4375 4376 unsigned Idx = 6; 4377 unsigned NumParams = Record[Idx++]; 4378 SmallVector<QualType, 16> ParamTypes; 4379 for (unsigned I = 0; I != NumParams; ++I) 4380 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 4381 4382 EPI.Variadic = Record[Idx++]; 4383 EPI.HasTrailingReturn = Record[Idx++]; 4384 EPI.TypeQuals = Record[Idx++]; 4385 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 4386 ExceptionSpecificationType EST = 4387 static_cast<ExceptionSpecificationType>(Record[Idx++]); 4388 EPI.ExceptionSpecType = EST; 4389 SmallVector<QualType, 2> Exceptions; 4390 if (EST == EST_Dynamic) { 4391 EPI.NumExceptions = Record[Idx++]; 4392 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 4393 Exceptions.push_back(readType(*Loc.F, Record, Idx)); 4394 EPI.Exceptions = Exceptions.data(); 4395 } else if (EST == EST_ComputedNoexcept) { 4396 EPI.NoexceptExpr = ReadExpr(*Loc.F); 4397 } else if (EST == EST_Uninstantiated) { 4398 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4399 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4400 } else if (EST == EST_Unevaluated) { 4401 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4402 } 4403 return Context.getFunctionType(ResultType, ParamTypes, EPI); 4404 } 4405 4406 case TYPE_UNRESOLVED_USING: { 4407 unsigned Idx = 0; 4408 return Context.getTypeDeclType( 4409 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 4410 } 4411 4412 case TYPE_TYPEDEF: { 4413 if (Record.size() != 2) { 4414 Error("incorrect encoding of typedef type"); 4415 return QualType(); 4416 } 4417 unsigned Idx = 0; 4418 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 4419 QualType Canonical = readType(*Loc.F, Record, Idx); 4420 if (!Canonical.isNull()) 4421 Canonical = Context.getCanonicalType(Canonical); 4422 return Context.getTypedefType(Decl, Canonical); 4423 } 4424 4425 case TYPE_TYPEOF_EXPR: 4426 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 4427 4428 case TYPE_TYPEOF: { 4429 if (Record.size() != 1) { 4430 Error("incorrect encoding of typeof(type) in AST file"); 4431 return QualType(); 4432 } 4433 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4434 return Context.getTypeOfType(UnderlyingType); 4435 } 4436 4437 case TYPE_DECLTYPE: { 4438 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4439 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 4440 } 4441 4442 case TYPE_UNARY_TRANSFORM: { 4443 QualType BaseType = readType(*Loc.F, Record, Idx); 4444 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4445 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 4446 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 4447 } 4448 4449 case TYPE_AUTO: 4450 return Context.getAutoType(readType(*Loc.F, Record, Idx)); 4451 4452 case TYPE_RECORD: { 4453 if (Record.size() != 2) { 4454 Error("incorrect encoding of record type"); 4455 return QualType(); 4456 } 4457 unsigned Idx = 0; 4458 bool IsDependent = Record[Idx++]; 4459 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 4460 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 4461 QualType T = Context.getRecordType(RD); 4462 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4463 return T; 4464 } 4465 4466 case TYPE_ENUM: { 4467 if (Record.size() != 2) { 4468 Error("incorrect encoding of enum type"); 4469 return QualType(); 4470 } 4471 unsigned Idx = 0; 4472 bool IsDependent = Record[Idx++]; 4473 QualType T 4474 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 4475 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4476 return T; 4477 } 4478 4479 case TYPE_ATTRIBUTED: { 4480 if (Record.size() != 3) { 4481 Error("incorrect encoding of attributed type"); 4482 return QualType(); 4483 } 4484 QualType modifiedType = readType(*Loc.F, Record, Idx); 4485 QualType equivalentType = readType(*Loc.F, Record, Idx); 4486 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 4487 return Context.getAttributedType(kind, modifiedType, equivalentType); 4488 } 4489 4490 case TYPE_PAREN: { 4491 if (Record.size() != 1) { 4492 Error("incorrect encoding of paren type"); 4493 return QualType(); 4494 } 4495 QualType InnerType = readType(*Loc.F, Record, Idx); 4496 return Context.getParenType(InnerType); 4497 } 4498 4499 case TYPE_PACK_EXPANSION: { 4500 if (Record.size() != 2) { 4501 Error("incorrect encoding of pack expansion type"); 4502 return QualType(); 4503 } 4504 QualType Pattern = readType(*Loc.F, Record, Idx); 4505 if (Pattern.isNull()) 4506 return QualType(); 4507 Optional<unsigned> NumExpansions; 4508 if (Record[1]) 4509 NumExpansions = Record[1] - 1; 4510 return Context.getPackExpansionType(Pattern, NumExpansions); 4511 } 4512 4513 case TYPE_ELABORATED: { 4514 unsigned Idx = 0; 4515 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4516 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4517 QualType NamedType = readType(*Loc.F, Record, Idx); 4518 return Context.getElaboratedType(Keyword, NNS, NamedType); 4519 } 4520 4521 case TYPE_OBJC_INTERFACE: { 4522 unsigned Idx = 0; 4523 ObjCInterfaceDecl *ItfD 4524 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 4525 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 4526 } 4527 4528 case TYPE_OBJC_OBJECT: { 4529 unsigned Idx = 0; 4530 QualType Base = readType(*Loc.F, Record, Idx); 4531 unsigned NumProtos = Record[Idx++]; 4532 SmallVector<ObjCProtocolDecl*, 4> Protos; 4533 for (unsigned I = 0; I != NumProtos; ++I) 4534 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 4535 return Context.getObjCObjectType(Base, Protos.data(), NumProtos); 4536 } 4537 4538 case TYPE_OBJC_OBJECT_POINTER: { 4539 unsigned Idx = 0; 4540 QualType Pointee = readType(*Loc.F, Record, Idx); 4541 return Context.getObjCObjectPointerType(Pointee); 4542 } 4543 4544 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 4545 unsigned Idx = 0; 4546 QualType Parm = readType(*Loc.F, Record, Idx); 4547 QualType Replacement = readType(*Loc.F, Record, Idx); 4548 return 4549 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 4550 Replacement); 4551 } 4552 4553 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 4554 unsigned Idx = 0; 4555 QualType Parm = readType(*Loc.F, Record, Idx); 4556 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 4557 return Context.getSubstTemplateTypeParmPackType( 4558 cast<TemplateTypeParmType>(Parm), 4559 ArgPack); 4560 } 4561 4562 case TYPE_INJECTED_CLASS_NAME: { 4563 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 4564 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 4565 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 4566 // for AST reading, too much interdependencies. 4567 return 4568 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 4569 } 4570 4571 case TYPE_TEMPLATE_TYPE_PARM: { 4572 unsigned Idx = 0; 4573 unsigned Depth = Record[Idx++]; 4574 unsigned Index = Record[Idx++]; 4575 bool Pack = Record[Idx++]; 4576 TemplateTypeParmDecl *D 4577 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 4578 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 4579 } 4580 4581 case TYPE_DEPENDENT_NAME: { 4582 unsigned Idx = 0; 4583 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4584 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4585 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4586 QualType Canon = readType(*Loc.F, Record, Idx); 4587 if (!Canon.isNull()) 4588 Canon = Context.getCanonicalType(Canon); 4589 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 4590 } 4591 4592 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 4593 unsigned Idx = 0; 4594 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4595 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4596 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4597 unsigned NumArgs = Record[Idx++]; 4598 SmallVector<TemplateArgument, 8> Args; 4599 Args.reserve(NumArgs); 4600 while (NumArgs--) 4601 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 4602 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 4603 Args.size(), Args.data()); 4604 } 4605 4606 case TYPE_DEPENDENT_SIZED_ARRAY: { 4607 unsigned Idx = 0; 4608 4609 // ArrayType 4610 QualType ElementType = readType(*Loc.F, Record, Idx); 4611 ArrayType::ArraySizeModifier ASM 4612 = (ArrayType::ArraySizeModifier)Record[Idx++]; 4613 unsigned IndexTypeQuals = Record[Idx++]; 4614 4615 // DependentSizedArrayType 4616 Expr *NumElts = ReadExpr(*Loc.F); 4617 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 4618 4619 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 4620 IndexTypeQuals, Brackets); 4621 } 4622 4623 case TYPE_TEMPLATE_SPECIALIZATION: { 4624 unsigned Idx = 0; 4625 bool IsDependent = Record[Idx++]; 4626 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 4627 SmallVector<TemplateArgument, 8> Args; 4628 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 4629 QualType Underlying = readType(*Loc.F, Record, Idx); 4630 QualType T; 4631 if (Underlying.isNull()) 4632 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(), 4633 Args.size()); 4634 else 4635 T = Context.getTemplateSpecializationType(Name, Args.data(), 4636 Args.size(), Underlying); 4637 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4638 return T; 4639 } 4640 4641 case TYPE_ATOMIC: { 4642 if (Record.size() != 1) { 4643 Error("Incorrect encoding of atomic type"); 4644 return QualType(); 4645 } 4646 QualType ValueType = readType(*Loc.F, Record, Idx); 4647 return Context.getAtomicType(ValueType); 4648 } 4649 } 4650 llvm_unreachable("Invalid TypeCode!"); 4651 } 4652 4653 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 4654 ASTReader &Reader; 4655 ModuleFile &F; 4656 const ASTReader::RecordData &Record; 4657 unsigned &Idx; 4658 4659 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 4660 unsigned &I) { 4661 return Reader.ReadSourceLocation(F, R, I); 4662 } 4663 4664 template<typename T> 4665 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 4666 return Reader.ReadDeclAs<T>(F, Record, Idx); 4667 } 4668 4669 public: 4670 TypeLocReader(ASTReader &Reader, ModuleFile &F, 4671 const ASTReader::RecordData &Record, unsigned &Idx) 4672 : Reader(Reader), F(F), Record(Record), Idx(Idx) 4673 { } 4674 4675 // We want compile-time assurance that we've enumerated all of 4676 // these, so unfortunately we have to declare them first, then 4677 // define them out-of-line. 4678 #define ABSTRACT_TYPELOC(CLASS, PARENT) 4679 #define TYPELOC(CLASS, PARENT) \ 4680 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 4681 #include "clang/AST/TypeLocNodes.def" 4682 4683 void VisitFunctionTypeLoc(FunctionTypeLoc); 4684 void VisitArrayTypeLoc(ArrayTypeLoc); 4685 }; 4686 4687 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 4688 // nothing to do 4689 } 4690 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 4691 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 4692 if (TL.needsExtraLocalData()) { 4693 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 4694 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 4695 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 4696 TL.setModeAttr(Record[Idx++]); 4697 } 4698 } 4699 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 4700 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4701 } 4702 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 4703 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4704 } 4705 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 4706 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 4707 } 4708 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 4709 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 4710 } 4711 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 4712 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 4713 } 4714 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 4715 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4716 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4717 } 4718 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 4719 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 4720 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 4721 if (Record[Idx++]) 4722 TL.setSizeExpr(Reader.ReadExpr(F)); 4723 else 4724 TL.setSizeExpr(0); 4725 } 4726 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 4727 VisitArrayTypeLoc(TL); 4728 } 4729 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 4730 VisitArrayTypeLoc(TL); 4731 } 4732 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 4733 VisitArrayTypeLoc(TL); 4734 } 4735 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 4736 DependentSizedArrayTypeLoc TL) { 4737 VisitArrayTypeLoc(TL); 4738 } 4739 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 4740 DependentSizedExtVectorTypeLoc TL) { 4741 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4742 } 4743 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 4744 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4745 } 4746 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 4747 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4748 } 4749 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 4750 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 4751 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4752 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4753 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 4754 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 4755 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 4756 } 4757 } 4758 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 4759 VisitFunctionTypeLoc(TL); 4760 } 4761 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 4762 VisitFunctionTypeLoc(TL); 4763 } 4764 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 4765 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4766 } 4767 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 4768 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4769 } 4770 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 4771 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4772 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4773 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4774 } 4775 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 4776 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4777 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4778 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4779 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4780 } 4781 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 4782 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4783 } 4784 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 4785 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 4786 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4787 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4788 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4789 } 4790 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 4791 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4792 } 4793 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 4794 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4795 } 4796 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 4797 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4798 } 4799 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 4800 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 4801 if (TL.hasAttrOperand()) { 4802 SourceRange range; 4803 range.setBegin(ReadSourceLocation(Record, Idx)); 4804 range.setEnd(ReadSourceLocation(Record, Idx)); 4805 TL.setAttrOperandParensRange(range); 4806 } 4807 if (TL.hasAttrExprOperand()) { 4808 if (Record[Idx++]) 4809 TL.setAttrExprOperand(Reader.ReadExpr(F)); 4810 else 4811 TL.setAttrExprOperand(0); 4812 } else if (TL.hasAttrEnumOperand()) 4813 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 4814 } 4815 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 4816 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4817 } 4818 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 4819 SubstTemplateTypeParmTypeLoc TL) { 4820 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4821 } 4822 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 4823 SubstTemplateTypeParmPackTypeLoc TL) { 4824 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4825 } 4826 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 4827 TemplateSpecializationTypeLoc TL) { 4828 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 4829 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 4830 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4831 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4832 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 4833 TL.setArgLocInfo(i, 4834 Reader.GetTemplateArgumentLocInfo(F, 4835 TL.getTypePtr()->getArg(i).getKind(), 4836 Record, Idx)); 4837 } 4838 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 4839 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4840 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4841 } 4842 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 4843 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4844 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4845 } 4846 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 4847 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4848 } 4849 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 4850 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4851 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4852 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4853 } 4854 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 4855 DependentTemplateSpecializationTypeLoc TL) { 4856 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4857 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4858 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 4859 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 4860 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4861 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4862 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 4863 TL.setArgLocInfo(I, 4864 Reader.GetTemplateArgumentLocInfo(F, 4865 TL.getTypePtr()->getArg(I).getKind(), 4866 Record, Idx)); 4867 } 4868 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 4869 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 4870 } 4871 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 4872 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4873 } 4874 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 4875 TL.setHasBaseTypeAsWritten(Record[Idx++]); 4876 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4877 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4878 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 4879 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 4880 } 4881 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 4882 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4883 } 4884 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 4885 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 4886 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4887 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4888 } 4889 4890 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F, 4891 const RecordData &Record, 4892 unsigned &Idx) { 4893 QualType InfoTy = readType(F, Record, Idx); 4894 if (InfoTy.isNull()) 4895 return 0; 4896 4897 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 4898 TypeLocReader TLR(*this, F, Record, Idx); 4899 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 4900 TLR.Visit(TL); 4901 return TInfo; 4902 } 4903 4904 QualType ASTReader::GetType(TypeID ID) { 4905 unsigned FastQuals = ID & Qualifiers::FastMask; 4906 unsigned Index = ID >> Qualifiers::FastWidth; 4907 4908 if (Index < NUM_PREDEF_TYPE_IDS) { 4909 QualType T; 4910 switch ((PredefinedTypeIDs)Index) { 4911 case PREDEF_TYPE_NULL_ID: return QualType(); 4912 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; 4913 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; 4914 4915 case PREDEF_TYPE_CHAR_U_ID: 4916 case PREDEF_TYPE_CHAR_S_ID: 4917 // FIXME: Check that the signedness of CharTy is correct! 4918 T = Context.CharTy; 4919 break; 4920 4921 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; 4922 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; 4923 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; 4924 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; 4925 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; 4926 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break; 4927 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; 4928 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; 4929 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; 4930 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break; 4931 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; 4932 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; 4933 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break; 4934 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break; 4935 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; 4936 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; 4937 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; 4938 case PREDEF_TY