ASTReader.cpp revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org//===--- ASTReader.cpp - AST File Reader ----------------------------------===// 275a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// 375a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// The LLVM Compiler Infrastructure 475a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// 575a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// This file is distributed under the University of Illinois Open Source 675a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// License. See LICENSE.TXT for details. 775a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// 875a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org//===----------------------------------------------------------------------===// 975a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// 1075a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// This file defines the ASTReader class, which reads AST files. 1175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// 1275a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org//===----------------------------------------------------------------------===// 1375a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org 1475a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "clang/Serialization/ASTReader.h" 1575a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "ASTCommon.h" 1675a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "ASTReaderInternals.h" 1775a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "clang/AST/ASTConsumer.h" 1875a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "clang/AST/ASTContext.h" 1990e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/AST/DeclTemplate.h" 2075a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "clang/AST/Expr.h" 2175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "clang/AST/ExprCXX.h" 2290e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/AST/NestedNameSpecifier.h" 2390e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/AST/Type.h" 2490e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/AST/TypeLocVisitor.h" 2590e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/DiagnosticOptions.h" 2690e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/FileManager.h" 2790e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/SourceManager.h" 2890e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/SourceManagerInternals.h" 2990e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/TargetInfo.h" 3090e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/TargetOptions.h" 3190e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/Version.h" 3290e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Basic/VersionTuple.h" 3390e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Frontend/Utils.h" 3490e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Lex/HeaderSearch.h" 3590e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Lex/HeaderSearchOptions.h" 3690e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Lex/MacroInfo.h" 3790e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Lex/PreprocessingRecord.h" 3890e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Lex/Preprocessor.h" 3990e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Lex/PreprocessorOptions.h" 4090e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Sema/Scope.h" 4190e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Sema/Sema.h" 4290e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Serialization/ASTDeserializationListener.h" 4390e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Serialization/GlobalModuleIndex.h" 4490e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Serialization/ModuleManager.h" 4590e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "clang/Serialization/SerializationDiagnostic.h" 4690e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/ADT/Hashing.h" 4790e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/ADT/StringExtras.h" 4890e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/Bitcode/BitstreamReader.h" 4990e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/Support/ErrorHandling.h" 5090e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/Support/FileSystem.h" 5190e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/Support/MemoryBuffer.h" 5290e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/Support/Path.h" 5390e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/Support/SaveAndRestore.h" 5490e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com#include "llvm/Support/raw_ostream.h" 5575a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include "llvm/Support/system_error.h" 5675a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include <algorithm> 5775a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include <cstdio> 5875a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org#include <iterator> 5990e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com 6090e624a56973856b6b81112fc1cd815a4224aa96roubert@google.comusing namespace clang; 6175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgusing namespace clang::serialization; 6290e624a56973856b6b81112fc1cd815a4224aa96roubert@google.comusing namespace clang::serialization::reader; 6375a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgusing llvm::BitstreamCursor; 6475a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org 6575a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org 6675a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org//===----------------------------------------------------------------------===// 6775a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org// ChainedASTReaderListener implementation 6875a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org//===----------------------------------------------------------------------===// 6975a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org 7075a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgbool 7175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 7275a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org return First->ReadFullVersionInformation(FullVersion) || 7375a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org Second->ReadFullVersionInformation(FullVersion); 7490e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com} 7590e624a56973856b6b81112fc1cd815a4224aa96roubert@google.comvoid ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 7690e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com First->ReadModuleName(ModuleName); 7790e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com Second->ReadModuleName(ModuleName); 7890e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com} 7990e624a56973856b6b81112fc1cd815a4224aa96roubert@google.comvoid ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 8075a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org First->ReadModuleMapFile(ModuleMapPath); 8175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org Second->ReadModuleMapFile(ModuleMapPath); 8290e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com} 8375a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgbool ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 8475a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org bool Complain) { 8575a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org return First->ReadLanguageOptions(LangOpts, Complain) || 8675a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org Second->ReadLanguageOptions(LangOpts, Complain); 8775a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org} 8875a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgbool 8975a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts, 9075a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org bool Complain) { 9175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org return First->ReadTargetOptions(TargetOpts, Complain) || 9275a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org Second->ReadTargetOptions(TargetOpts, Complain); 9375a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org} 9475a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.orgbool ChainedASTReaderListener::ReadDiagnosticOptions( 9575a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 9690e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com return First->ReadDiagnosticOptions(DiagOpts, Complain) || 9790e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com Second->ReadDiagnosticOptions(DiagOpts, Complain); 9890e624a56973856b6b81112fc1cd815a4224aa96roubert@google.com} 9990e624a56973856b6b81112fc1cd815a4224aa96roubert@google.combool 10090e624a56973856b6b81112fc1cd815a4224aa96roubert@google.comChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 10175a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org bool Complain) { 10275a0cfdaa6edd47c9917172fa57701b5d22e83efrouslan@chromium.org return First->ReadFileSystemOptions(FSOpts, Complain) || 103 Second->ReadFileSystemOptions(FSOpts, Complain); 104} 105 106bool ChainedASTReaderListener::ReadHeaderSearchOptions( 107 const HeaderSearchOptions &HSOpts, bool Complain) { 108 return First->ReadHeaderSearchOptions(HSOpts, Complain) || 109 Second->ReadHeaderSearchOptions(HSOpts, Complain); 110} 111bool ChainedASTReaderListener::ReadPreprocessorOptions( 112 const PreprocessorOptions &PPOpts, bool Complain, 113 std::string &SuggestedPredefines) { 114 return First->ReadPreprocessorOptions(PPOpts, Complain, 115 SuggestedPredefines) || 116 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 117} 118void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 119 unsigned Value) { 120 First->ReadCounter(M, Value); 121 Second->ReadCounter(M, Value); 122} 123bool ChainedASTReaderListener::needsInputFileVisitation() { 124 return First->needsInputFileVisitation() || 125 Second->needsInputFileVisitation(); 126} 127bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 128 return First->needsSystemInputFileVisitation() || 129 Second->needsSystemInputFileVisitation(); 130} 131void ChainedASTReaderListener::visitModuleFile(StringRef Filename) { 132 First->visitModuleFile(Filename); 133 Second->visitModuleFile(Filename); 134} 135bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 136 bool isSystem, 137 bool isOverridden) { 138 bool Continue = false; 139 if (First->needsInputFileVisitation() && 140 (!isSystem || First->needsSystemInputFileVisitation())) 141 Continue |= First->visitInputFile(Filename, isSystem, isOverridden); 142 if (Second->needsInputFileVisitation() && 143 (!isSystem || Second->needsSystemInputFileVisitation())) 144 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden); 145 return Continue; 146} 147 148//===----------------------------------------------------------------------===// 149// PCH validator implementation 150//===----------------------------------------------------------------------===// 151 152ASTReaderListener::~ASTReaderListener() {} 153 154/// \brief Compare the given set of language options against an existing set of 155/// language options. 156/// 157/// \param Diags If non-NULL, diagnostics will be emitted via this engine. 158/// 159/// \returns true if the languagae options mis-match, false otherwise. 160static bool checkLanguageOptions(const LangOptions &LangOpts, 161 const LangOptions &ExistingLangOpts, 162 DiagnosticsEngine *Diags) { 163#define LANGOPT(Name, Bits, Default, Description) \ 164 if (ExistingLangOpts.Name != LangOpts.Name) { \ 165 if (Diags) \ 166 Diags->Report(diag::err_pch_langopt_mismatch) \ 167 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 168 return true; \ 169 } 170 171#define VALUE_LANGOPT(Name, Bits, Default, Description) \ 172 if (ExistingLangOpts.Name != LangOpts.Name) { \ 173 if (Diags) \ 174 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 175 << Description; \ 176 return true; \ 177 } 178 179#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 180 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 181 if (Diags) \ 182 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 183 << Description; \ 184 return true; \ 185 } 186 187#define BENIGN_LANGOPT(Name, Bits, Default, Description) 188#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 189#include "clang/Basic/LangOptions.def" 190 191 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 192 if (Diags) 193 Diags->Report(diag::err_pch_langopt_value_mismatch) 194 << "target Objective-C runtime"; 195 return true; 196 } 197 198 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 199 LangOpts.CommentOpts.BlockCommandNames) { 200 if (Diags) 201 Diags->Report(diag::err_pch_langopt_value_mismatch) 202 << "block command names"; 203 return true; 204 } 205 206 return false; 207} 208 209/// \brief Compare the given set of target options against an existing set of 210/// target options. 211/// 212/// \param Diags If non-NULL, diagnostics will be emitted via this engine. 213/// 214/// \returns true if the target options mis-match, false otherwise. 215static bool checkTargetOptions(const TargetOptions &TargetOpts, 216 const TargetOptions &ExistingTargetOpts, 217 DiagnosticsEngine *Diags) { 218#define CHECK_TARGET_OPT(Field, Name) \ 219 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 220 if (Diags) \ 221 Diags->Report(diag::err_pch_targetopt_mismatch) \ 222 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 223 return true; \ 224 } 225 226 CHECK_TARGET_OPT(Triple, "target"); 227 CHECK_TARGET_OPT(CPU, "target CPU"); 228 CHECK_TARGET_OPT(ABI, "target ABI"); 229#undef CHECK_TARGET_OPT 230 231 // Compare feature sets. 232 SmallVector<StringRef, 4> ExistingFeatures( 233 ExistingTargetOpts.FeaturesAsWritten.begin(), 234 ExistingTargetOpts.FeaturesAsWritten.end()); 235 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 236 TargetOpts.FeaturesAsWritten.end()); 237 std::sort(ExistingFeatures.begin(), ExistingFeatures.end()); 238 std::sort(ReadFeatures.begin(), ReadFeatures.end()); 239 240 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size(); 241 unsigned ReadIdx = 0, ReadN = ReadFeatures.size(); 242 while (ExistingIdx < ExistingN && ReadIdx < ReadN) { 243 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) { 244 ++ExistingIdx; 245 ++ReadIdx; 246 continue; 247 } 248 249 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) { 250 if (Diags) 251 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 252 << false << ReadFeatures[ReadIdx]; 253 return true; 254 } 255 256 if (Diags) 257 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 258 << true << ExistingFeatures[ExistingIdx]; 259 return true; 260 } 261 262 if (ExistingIdx < ExistingN) { 263 if (Diags) 264 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 265 << true << ExistingFeatures[ExistingIdx]; 266 return true; 267 } 268 269 if (ReadIdx < ReadN) { 270 if (Diags) 271 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 272 << false << ReadFeatures[ReadIdx]; 273 return true; 274 } 275 276 return false; 277} 278 279bool 280PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 281 bool Complain) { 282 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 283 return checkLanguageOptions(LangOpts, ExistingLangOpts, 284 Complain? &Reader.Diags : nullptr); 285} 286 287bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 288 bool Complain) { 289 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 290 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 291 Complain? &Reader.Diags : nullptr); 292} 293 294namespace { 295 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> > 296 MacroDefinitionsMap; 297 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > 298 DeclsMap; 299} 300 301static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 302 DiagnosticsEngine &Diags, 303 bool Complain) { 304 typedef DiagnosticsEngine::Level Level; 305 306 // Check current mappings for new -Werror mappings, and the stored mappings 307 // for cases that were explicitly mapped to *not* be errors that are now 308 // errors because of options like -Werror. 309 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 310 311 for (DiagnosticsEngine *MappingSource : MappingSources) { 312 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 313 diag::kind DiagID = DiagIDMappingPair.first; 314 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 315 if (CurLevel < DiagnosticsEngine::Error) 316 continue; // not significant 317 Level StoredLevel = 318 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 319 if (StoredLevel < DiagnosticsEngine::Error) { 320 if (Complain) 321 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 322 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 323 return true; 324 } 325 } 326 } 327 328 return false; 329} 330 331static DiagnosticsEngine::ExtensionHandling 332isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 333 DiagnosticsEngine::ExtensionHandling Ext = 334 Diags.getExtensionHandlingBehavior(); 335 if (Ext == DiagnosticsEngine::Ext_Warn && Diags.getWarningsAsErrors()) 336 Ext = DiagnosticsEngine::Ext_Error; 337 return Ext; 338} 339 340static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 341 DiagnosticsEngine &Diags, 342 bool IsSystem, bool Complain) { 343 // Top-level options 344 if (IsSystem) { 345 if (Diags.getSuppressSystemWarnings()) 346 return false; 347 // If -Wsystem-headers was not enabled before, be conservative 348 if (StoredDiags.getSuppressSystemWarnings()) { 349 if (Complain) 350 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 351 return true; 352 } 353 } 354 355 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 356 if (Complain) 357 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 358 return true; 359 } 360 361 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 362 !StoredDiags.getEnableAllWarnings()) { 363 if (Complain) 364 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 365 return true; 366 } 367 368 if (isExtHandlingFromDiagsError(Diags) && 369 !isExtHandlingFromDiagsError(StoredDiags)) { 370 if (Complain) 371 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 372 return true; 373 } 374 375 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 376} 377 378bool PCHValidator::ReadDiagnosticOptions( 379 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 380 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 381 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 382 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 383 new DiagnosticsEngine(DiagIDs, DiagOpts.getPtr())); 384 // This should never fail, because we would have processed these options 385 // before writing them to an ASTFile. 386 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 387 388 ModuleManager &ModuleMgr = Reader.getModuleManager(); 389 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 390 391 // If the original import came from a file explicitly generated by the user, 392 // don't check the diagnostic mappings. 393 // FIXME: currently this is approximated by checking whether this is not a 394 // module import. 395 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 396 // the transitive closure of its imports, since unrelated modules cannot be 397 // imported until after this module finishes validation. 398 ModuleFile *TopImport = *ModuleMgr.rbegin(); 399 while (!TopImport->ImportedBy.empty()) 400 TopImport = TopImport->ImportedBy[0]; 401 if (TopImport->Kind != MK_Module) 402 return false; 403 404 StringRef ModuleName = TopImport->ModuleName; 405 assert(!ModuleName.empty() && "diagnostic options read before module name"); 406 407 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 408 assert(M && "missing module"); 409 410 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 411 // contains the union of their flags. 412 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain); 413} 414 415/// \brief Collect the macro definitions provided by the given preprocessor 416/// options. 417static void 418collectMacroDefinitions(const PreprocessorOptions &PPOpts, 419 MacroDefinitionsMap &Macros, 420 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 421 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 422 StringRef Macro = PPOpts.Macros[I].first; 423 bool IsUndef = PPOpts.Macros[I].second; 424 425 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 426 StringRef MacroName = MacroPair.first; 427 StringRef MacroBody = MacroPair.second; 428 429 // For an #undef'd macro, we only care about the name. 430 if (IsUndef) { 431 if (MacroNames && !Macros.count(MacroName)) 432 MacroNames->push_back(MacroName); 433 434 Macros[MacroName] = std::make_pair("", true); 435 continue; 436 } 437 438 // For a #define'd macro, figure out the actual definition. 439 if (MacroName.size() == Macro.size()) 440 MacroBody = "1"; 441 else { 442 // Note: GCC drops anything following an end-of-line character. 443 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 444 MacroBody = MacroBody.substr(0, End); 445 } 446 447 if (MacroNames && !Macros.count(MacroName)) 448 MacroNames->push_back(MacroName); 449 Macros[MacroName] = std::make_pair(MacroBody, false); 450 } 451} 452 453/// \brief Check the preprocessor options deserialized from the control block 454/// against the preprocessor options in an existing preprocessor. 455/// 456/// \param Diags If non-null, produce diagnostics for any mismatches incurred. 457static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 458 const PreprocessorOptions &ExistingPPOpts, 459 DiagnosticsEngine *Diags, 460 FileManager &FileMgr, 461 std::string &SuggestedPredefines, 462 const LangOptions &LangOpts) { 463 // Check macro definitions. 464 MacroDefinitionsMap ASTFileMacros; 465 collectMacroDefinitions(PPOpts, ASTFileMacros); 466 MacroDefinitionsMap ExistingMacros; 467 SmallVector<StringRef, 4> ExistingMacroNames; 468 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 469 470 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 471 // Dig out the macro definition in the existing preprocessor options. 472 StringRef MacroName = ExistingMacroNames[I]; 473 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 474 475 // Check whether we know anything about this macro name or not. 476 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known 477 = ASTFileMacros.find(MacroName); 478 if (Known == ASTFileMacros.end()) { 479 // FIXME: Check whether this identifier was referenced anywhere in the 480 // AST file. If so, we should reject the AST file. Unfortunately, this 481 // information isn't in the control block. What shall we do about it? 482 483 if (Existing.second) { 484 SuggestedPredefines += "#undef "; 485 SuggestedPredefines += MacroName.str(); 486 SuggestedPredefines += '\n'; 487 } else { 488 SuggestedPredefines += "#define "; 489 SuggestedPredefines += MacroName.str(); 490 SuggestedPredefines += ' '; 491 SuggestedPredefines += Existing.first.str(); 492 SuggestedPredefines += '\n'; 493 } 494 continue; 495 } 496 497 // If the macro was defined in one but undef'd in the other, we have a 498 // conflict. 499 if (Existing.second != Known->second.second) { 500 if (Diags) { 501 Diags->Report(diag::err_pch_macro_def_undef) 502 << MacroName << Known->second.second; 503 } 504 return true; 505 } 506 507 // If the macro was #undef'd in both, or if the macro bodies are identical, 508 // it's fine. 509 if (Existing.second || Existing.first == Known->second.first) 510 continue; 511 512 // The macro bodies differ; complain. 513 if (Diags) { 514 Diags->Report(diag::err_pch_macro_def_conflict) 515 << MacroName << Known->second.first << Existing.first; 516 } 517 return true; 518 } 519 520 // Check whether we're using predefines. 521 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) { 522 if (Diags) { 523 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 524 } 525 return true; 526 } 527 528 // Detailed record is important since it is used for the module cache hash. 529 if (LangOpts.Modules && 530 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) { 531 if (Diags) { 532 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 533 } 534 return true; 535 } 536 537 // Compute the #include and #include_macros lines we need. 538 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 539 StringRef File = ExistingPPOpts.Includes[I]; 540 if (File == ExistingPPOpts.ImplicitPCHInclude) 541 continue; 542 543 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 544 != PPOpts.Includes.end()) 545 continue; 546 547 SuggestedPredefines += "#include \""; 548 SuggestedPredefines += 549 HeaderSearch::NormalizeDashIncludePath(File, FileMgr); 550 SuggestedPredefines += "\"\n"; 551 } 552 553 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 554 StringRef File = ExistingPPOpts.MacroIncludes[I]; 555 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 556 File) 557 != PPOpts.MacroIncludes.end()) 558 continue; 559 560 SuggestedPredefines += "#__include_macros \""; 561 SuggestedPredefines += 562 HeaderSearch::NormalizeDashIncludePath(File, FileMgr); 563 SuggestedPredefines += "\"\n##\n"; 564 } 565 566 return false; 567} 568 569bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 570 bool Complain, 571 std::string &SuggestedPredefines) { 572 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 573 574 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 575 Complain? &Reader.Diags : nullptr, 576 PP.getFileManager(), 577 SuggestedPredefines, 578 PP.getLangOpts()); 579} 580 581void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 582 PP.setCounterValue(Value); 583} 584 585//===----------------------------------------------------------------------===// 586// AST reader implementation 587//===----------------------------------------------------------------------===// 588 589void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 590 bool TakeOwnership) { 591 DeserializationListener = Listener; 592 OwnsDeserializationListener = TakeOwnership; 593} 594 595 596 597unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 598 return serialization::ComputeHash(Sel); 599} 600 601 602std::pair<unsigned, unsigned> 603ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 604 using namespace llvm::support; 605 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 606 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 607 return std::make_pair(KeyLen, DataLen); 608} 609 610ASTSelectorLookupTrait::internal_key_type 611ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 612 using namespace llvm::support; 613 SelectorTable &SelTable = Reader.getContext().Selectors; 614 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 615 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 616 F, endian::readNext<uint32_t, little, unaligned>(d)); 617 if (N == 0) 618 return SelTable.getNullarySelector(FirstII); 619 else if (N == 1) 620 return SelTable.getUnarySelector(FirstII); 621 622 SmallVector<IdentifierInfo *, 16> Args; 623 Args.push_back(FirstII); 624 for (unsigned I = 1; I != N; ++I) 625 Args.push_back(Reader.getLocalIdentifier( 626 F, endian::readNext<uint32_t, little, unaligned>(d))); 627 628 return SelTable.getSelector(N, Args.data()); 629} 630 631ASTSelectorLookupTrait::data_type 632ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 633 unsigned DataLen) { 634 using namespace llvm::support; 635 636 data_type Result; 637 638 Result.ID = Reader.getGlobalSelectorID( 639 F, endian::readNext<uint32_t, little, unaligned>(d)); 640 unsigned NumInstanceMethodsAndBits = 641 endian::readNext<uint16_t, little, unaligned>(d); 642 unsigned NumFactoryMethodsAndBits = 643 endian::readNext<uint16_t, little, unaligned>(d); 644 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3; 645 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3; 646 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2; 647 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2; 648 649 // Load instance methods 650 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 651 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 652 F, endian::readNext<uint32_t, little, unaligned>(d))) 653 Result.Instance.push_back(Method); 654 } 655 656 // Load factory methods 657 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 658 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 659 F, endian::readNext<uint32_t, little, unaligned>(d))) 660 Result.Factory.push_back(Method); 661 } 662 663 return Result; 664} 665 666unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 667 return llvm::HashString(a); 668} 669 670std::pair<unsigned, unsigned> 671ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 672 using namespace llvm::support; 673 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 674 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 675 return std::make_pair(KeyLen, DataLen); 676} 677 678ASTIdentifierLookupTraitBase::internal_key_type 679ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 680 assert(n >= 2 && d[n-1] == '\0'); 681 return StringRef((const char*) d, n-1); 682} 683 684/// \brief Whether the given identifier is "interesting". 685static bool isInterestingIdentifier(IdentifierInfo &II) { 686 return II.isPoisoned() || 687 II.isExtensionToken() || 688 II.getObjCOrBuiltinID() || 689 II.hasRevertedTokenIDToIdentifier() || 690 II.hadMacroDefinition() || 691 II.getFETokenInfo<void>(); 692} 693 694IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 695 const unsigned char* d, 696 unsigned DataLen) { 697 using namespace llvm::support; 698 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 699 bool IsInteresting = RawID & 0x01; 700 701 // Wipe out the "is interesting" bit. 702 RawID = RawID >> 1; 703 704 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 705 if (!IsInteresting) { 706 // For uninteresting identifiers, just build the IdentifierInfo 707 // and associate it with the persistent ID. 708 IdentifierInfo *II = KnownII; 709 if (!II) { 710 II = &Reader.getIdentifierTable().getOwn(k); 711 KnownII = II; 712 } 713 Reader.SetIdentifierInfo(ID, II); 714 if (!II->isFromAST()) { 715 bool WasInteresting = isInterestingIdentifier(*II); 716 II->setIsFromAST(); 717 if (WasInteresting) 718 II->setChangedSinceDeserialization(); 719 } 720 Reader.markIdentifierUpToDate(II); 721 return II; 722 } 723 724 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 725 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 726 bool CPlusPlusOperatorKeyword = Bits & 0x01; 727 Bits >>= 1; 728 bool HasRevertedTokenIDToIdentifier = Bits & 0x01; 729 Bits >>= 1; 730 bool Poisoned = Bits & 0x01; 731 Bits >>= 1; 732 bool ExtensionToken = Bits & 0x01; 733 Bits >>= 1; 734 bool hasSubmoduleMacros = Bits & 0x01; 735 Bits >>= 1; 736 bool hadMacroDefinition = Bits & 0x01; 737 Bits >>= 1; 738 739 assert(Bits == 0 && "Extra bits in the identifier?"); 740 DataLen -= 8; 741 742 // Build the IdentifierInfo itself and link the identifier ID with 743 // the new IdentifierInfo. 744 IdentifierInfo *II = KnownII; 745 if (!II) { 746 II = &Reader.getIdentifierTable().getOwn(StringRef(k)); 747 KnownII = II; 748 } 749 Reader.markIdentifierUpToDate(II); 750 if (!II->isFromAST()) { 751 bool WasInteresting = isInterestingIdentifier(*II); 752 II->setIsFromAST(); 753 if (WasInteresting) 754 II->setChangedSinceDeserialization(); 755 } 756 757 // Set or check the various bits in the IdentifierInfo structure. 758 // Token IDs are read-only. 759 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 760 II->RevertTokenIDToIdentifier(); 761 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 762 assert(II->isExtensionToken() == ExtensionToken && 763 "Incorrect extension token flag"); 764 (void)ExtensionToken; 765 if (Poisoned) 766 II->setIsPoisoned(true); 767 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 768 "Incorrect C++ operator keyword flag"); 769 (void)CPlusPlusOperatorKeyword; 770 771 // If this identifier is a macro, deserialize the macro 772 // definition. 773 if (hadMacroDefinition) { 774 uint32_t MacroDirectivesOffset = 775 endian::readNext<uint32_t, little, unaligned>(d); 776 DataLen -= 4; 777 SmallVector<uint32_t, 8> LocalMacroIDs; 778 if (hasSubmoduleMacros) { 779 while (uint32_t LocalMacroID = 780 endian::readNext<uint32_t, little, unaligned>(d)) { 781 DataLen -= 4; 782 LocalMacroIDs.push_back(LocalMacroID); 783 } 784 DataLen -= 4; 785 } 786 787 if (F.Kind == MK_Module) { 788 // Macro definitions are stored from newest to oldest, so reverse them 789 // before registering them. 790 llvm::SmallVector<unsigned, 8> MacroSizes; 791 for (SmallVectorImpl<uint32_t>::iterator 792 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) { 793 unsigned Size = 1; 794 795 static const uint32_t HasOverridesFlag = 0x80000000U; 796 if (I + 1 != E && (I[1] & HasOverridesFlag)) 797 Size += 1 + (I[1] & ~HasOverridesFlag); 798 799 MacroSizes.push_back(Size); 800 I += Size; 801 } 802 803 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end(); 804 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(), 805 SE = MacroSizes.rend(); 806 SI != SE; ++SI) { 807 I -= *SI; 808 809 uint32_t LocalMacroID = *I; 810 llvm::ArrayRef<uint32_t> Overrides; 811 if (*SI != 1) 812 Overrides = llvm::makeArrayRef(&I[2], *SI - 2); 813 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides); 814 } 815 assert(I == LocalMacroIDs.begin()); 816 } else { 817 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset); 818 } 819 } 820 821 Reader.SetIdentifierInfo(ID, II); 822 823 // Read all of the declarations visible at global scope with this 824 // name. 825 if (DataLen > 0) { 826 SmallVector<uint32_t, 4> DeclIDs; 827 for (; DataLen > 0; DataLen -= 4) 828 DeclIDs.push_back(Reader.getGlobalDeclID( 829 F, endian::readNext<uint32_t, little, unaligned>(d))); 830 Reader.SetGloballyVisibleDecls(II, DeclIDs); 831 } 832 833 return II; 834} 835 836unsigned 837ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const { 838 llvm::FoldingSetNodeID ID; 839 ID.AddInteger(Key.Kind); 840 841 switch (Key.Kind) { 842 case DeclarationName::Identifier: 843 case DeclarationName::CXXLiteralOperatorName: 844 ID.AddString(((IdentifierInfo*)Key.Data)->getName()); 845 break; 846 case DeclarationName::ObjCZeroArgSelector: 847 case DeclarationName::ObjCOneArgSelector: 848 case DeclarationName::ObjCMultiArgSelector: 849 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data))); 850 break; 851 case DeclarationName::CXXOperatorName: 852 ID.AddInteger((OverloadedOperatorKind)Key.Data); 853 break; 854 case DeclarationName::CXXConstructorName: 855 case DeclarationName::CXXDestructorName: 856 case DeclarationName::CXXConversionFunctionName: 857 case DeclarationName::CXXUsingDirective: 858 break; 859 } 860 861 return ID.ComputeHash(); 862} 863 864ASTDeclContextNameLookupTrait::internal_key_type 865ASTDeclContextNameLookupTrait::GetInternalKey( 866 const external_key_type& Name) const { 867 DeclNameKey Key; 868 Key.Kind = Name.getNameKind(); 869 switch (Name.getNameKind()) { 870 case DeclarationName::Identifier: 871 Key.Data = (uint64_t)Name.getAsIdentifierInfo(); 872 break; 873 case DeclarationName::ObjCZeroArgSelector: 874 case DeclarationName::ObjCOneArgSelector: 875 case DeclarationName::ObjCMultiArgSelector: 876 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 877 break; 878 case DeclarationName::CXXOperatorName: 879 Key.Data = Name.getCXXOverloadedOperator(); 880 break; 881 case DeclarationName::CXXLiteralOperatorName: 882 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier(); 883 break; 884 case DeclarationName::CXXConstructorName: 885 case DeclarationName::CXXDestructorName: 886 case DeclarationName::CXXConversionFunctionName: 887 case DeclarationName::CXXUsingDirective: 888 Key.Data = 0; 889 break; 890 } 891 892 return Key; 893} 894 895std::pair<unsigned, unsigned> 896ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 897 using namespace llvm::support; 898 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 900 return std::make_pair(KeyLen, DataLen); 901} 902 903ASTDeclContextNameLookupTrait::internal_key_type 904ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) { 905 using namespace llvm::support; 906 907 DeclNameKey Key; 908 Key.Kind = (DeclarationName::NameKind)*d++; 909 switch (Key.Kind) { 910 case DeclarationName::Identifier: 911 Key.Data = (uint64_t)Reader.getLocalIdentifier( 912 F, endian::readNext<uint32_t, little, unaligned>(d)); 913 break; 914 case DeclarationName::ObjCZeroArgSelector: 915 case DeclarationName::ObjCOneArgSelector: 916 case DeclarationName::ObjCMultiArgSelector: 917 Key.Data = 918 (uint64_t)Reader.getLocalSelector( 919 F, endian::readNext<uint32_t, little, unaligned>( 920 d)).getAsOpaquePtr(); 921 break; 922 case DeclarationName::CXXOperatorName: 923 Key.Data = *d++; // OverloadedOperatorKind 924 break; 925 case DeclarationName::CXXLiteralOperatorName: 926 Key.Data = (uint64_t)Reader.getLocalIdentifier( 927 F, endian::readNext<uint32_t, little, unaligned>(d)); 928 break; 929 case DeclarationName::CXXConstructorName: 930 case DeclarationName::CXXDestructorName: 931 case DeclarationName::CXXConversionFunctionName: 932 case DeclarationName::CXXUsingDirective: 933 Key.Data = 0; 934 break; 935 } 936 937 return Key; 938} 939 940ASTDeclContextNameLookupTrait::data_type 941ASTDeclContextNameLookupTrait::ReadData(internal_key_type, 942 const unsigned char* d, 943 unsigned DataLen) { 944 using namespace llvm::support; 945 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d); 946 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>( 947 const_cast<unsigned char *>(d)); 948 return std::make_pair(Start, Start + NumDecls); 949} 950 951bool ASTReader::ReadDeclContextStorage(ModuleFile &M, 952 BitstreamCursor &Cursor, 953 const std::pair<uint64_t, uint64_t> &Offsets, 954 DeclContextInfo &Info) { 955 SavedStreamPosition SavedPosition(Cursor); 956 // First the lexical decls. 957 if (Offsets.first != 0) { 958 Cursor.JumpToBit(Offsets.first); 959 960 RecordData Record; 961 StringRef Blob; 962 unsigned Code = Cursor.ReadCode(); 963 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 964 if (RecCode != DECL_CONTEXT_LEXICAL) { 965 Error("Expected lexical block"); 966 return true; 967 } 968 969 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data()); 970 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair); 971 } 972 973 // Now the lookup table. 974 if (Offsets.second != 0) { 975 Cursor.JumpToBit(Offsets.second); 976 977 RecordData Record; 978 StringRef Blob; 979 unsigned Code = Cursor.ReadCode(); 980 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 981 if (RecCode != DECL_CONTEXT_VISIBLE) { 982 Error("Expected visible lookup table block"); 983 return true; 984 } 985 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create( 986 (const unsigned char *)Blob.data() + Record[0], 987 (const unsigned char *)Blob.data() + sizeof(uint32_t), 988 (const unsigned char *)Blob.data(), 989 ASTDeclContextNameLookupTrait(*this, M)); 990 } 991 992 return false; 993} 994 995void ASTReader::Error(StringRef Msg) { 996 Error(diag::err_fe_pch_malformed, Msg); 997 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) { 998 Diag(diag::note_module_cache_path) 999 << PP.getHeaderSearchInfo().getModuleCachePath(); 1000 } 1001} 1002 1003void ASTReader::Error(unsigned DiagID, 1004 StringRef Arg1, StringRef Arg2) { 1005 if (Diags.isDiagnosticInFlight()) 1006 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 1007 else 1008 Diag(DiagID) << Arg1 << Arg2; 1009} 1010 1011//===----------------------------------------------------------------------===// 1012// Source Manager Deserialization 1013//===----------------------------------------------------------------------===// 1014 1015/// \brief Read the line table in the source manager block. 1016/// \returns true if there was an error. 1017bool ASTReader::ParseLineTable(ModuleFile &F, 1018 SmallVectorImpl<uint64_t> &Record) { 1019 unsigned Idx = 0; 1020 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1021 1022 // Parse the file names 1023 std::map<int, int> FileIDs; 1024 for (int I = 0, N = Record[Idx++]; I != N; ++I) { 1025 // Extract the file name 1026 unsigned FilenameLen = Record[Idx++]; 1027 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); 1028 Idx += FilenameLen; 1029 MaybeAddSystemRootToFilename(F, Filename); 1030 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1031 } 1032 1033 // Parse the line entries 1034 std::vector<LineEntry> Entries; 1035 while (Idx < Record.size()) { 1036 int FID = Record[Idx++]; 1037 assert(FID >= 0 && "Serialized line entries for non-local file."); 1038 // Remap FileID from 1-based old view. 1039 FID += F.SLocEntryBaseID - 1; 1040 1041 // Extract the line entries 1042 unsigned NumEntries = Record[Idx++]; 1043 assert(NumEntries && "Numentries is 00000"); 1044 Entries.clear(); 1045 Entries.reserve(NumEntries); 1046 for (unsigned I = 0; I != NumEntries; ++I) { 1047 unsigned FileOffset = Record[Idx++]; 1048 unsigned LineNo = Record[Idx++]; 1049 int FilenameID = FileIDs[Record[Idx++]]; 1050 SrcMgr::CharacteristicKind FileKind 1051 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1052 unsigned IncludeOffset = Record[Idx++]; 1053 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1054 FileKind, IncludeOffset)); 1055 } 1056 LineTable.AddEntry(FileID::get(FID), Entries); 1057 } 1058 1059 return false; 1060} 1061 1062/// \brief Read a source manager block 1063bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1064 using namespace SrcMgr; 1065 1066 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1067 1068 // Set the source-location entry cursor to the current position in 1069 // the stream. This cursor will be used to read the contents of the 1070 // source manager block initially, and then lazily read 1071 // source-location entries as needed. 1072 SLocEntryCursor = F.Stream; 1073 1074 // The stream itself is going to skip over the source manager block. 1075 if (F.Stream.SkipBlock()) { 1076 Error("malformed block record in AST file"); 1077 return true; 1078 } 1079 1080 // Enter the source manager block. 1081 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1082 Error("malformed source manager block record in AST file"); 1083 return true; 1084 } 1085 1086 RecordData Record; 1087 while (true) { 1088 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks(); 1089 1090 switch (E.Kind) { 1091 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1092 case llvm::BitstreamEntry::Error: 1093 Error("malformed block record in AST file"); 1094 return true; 1095 case llvm::BitstreamEntry::EndBlock: 1096 return false; 1097 case llvm::BitstreamEntry::Record: 1098 // The interesting case. 1099 break; 1100 } 1101 1102 // Read a record. 1103 Record.clear(); 1104 StringRef Blob; 1105 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) { 1106 default: // Default behavior: ignore. 1107 break; 1108 1109 case SM_SLOC_FILE_ENTRY: 1110 case SM_SLOC_BUFFER_ENTRY: 1111 case SM_SLOC_EXPANSION_ENTRY: 1112 // Once we hit one of the source location entries, we're done. 1113 return false; 1114 } 1115 } 1116} 1117 1118/// \brief If a header file is not found at the path that we expect it to be 1119/// and the PCH file was moved from its original location, try to resolve the 1120/// file by assuming that header+PCH were moved together and the header is in 1121/// the same place relative to the PCH. 1122static std::string 1123resolveFileRelativeToOriginalDir(const std::string &Filename, 1124 const std::string &OriginalDir, 1125 const std::string &CurrDir) { 1126 assert(OriginalDir != CurrDir && 1127 "No point trying to resolve the file if the PCH dir didn't change"); 1128 using namespace llvm::sys; 1129 SmallString<128> filePath(Filename); 1130 fs::make_absolute(filePath); 1131 assert(path::is_absolute(OriginalDir)); 1132 SmallString<128> currPCHPath(CurrDir); 1133 1134 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1135 fileDirE = path::end(path::parent_path(filePath)); 1136 path::const_iterator origDirI = path::begin(OriginalDir), 1137 origDirE = path::end(OriginalDir); 1138 // Skip the common path components from filePath and OriginalDir. 1139 while (fileDirI != fileDirE && origDirI != origDirE && 1140 *fileDirI == *origDirI) { 1141 ++fileDirI; 1142 ++origDirI; 1143 } 1144 for (; origDirI != origDirE; ++origDirI) 1145 path::append(currPCHPath, ".."); 1146 path::append(currPCHPath, fileDirI, fileDirE); 1147 path::append(currPCHPath, path::filename(Filename)); 1148 return currPCHPath.str(); 1149} 1150 1151bool ASTReader::ReadSLocEntry(int ID) { 1152 if (ID == 0) 1153 return false; 1154 1155 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1156 Error("source location entry ID out-of-range for AST file"); 1157 return true; 1158 } 1159 1160 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1161 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 1162 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1163 unsigned BaseOffset = F->SLocEntryBaseOffset; 1164 1165 ++NumSLocEntriesRead; 1166 llvm::BitstreamEntry Entry = SLocEntryCursor.advance(); 1167 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1168 Error("incorrectly-formatted source location entry in AST file"); 1169 return true; 1170 } 1171 1172 RecordData Record; 1173 StringRef Blob; 1174 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) { 1175 default: 1176 Error("incorrectly-formatted source location entry in AST file"); 1177 return true; 1178 1179 case SM_SLOC_FILE_ENTRY: { 1180 // We will detect whether a file changed and return 'Failure' for it, but 1181 // we will also try to fail gracefully by setting up the SLocEntry. 1182 unsigned InputID = Record[4]; 1183 InputFile IF = getInputFile(*F, InputID); 1184 const FileEntry *File = IF.getFile(); 1185 bool OverriddenBuffer = IF.isOverridden(); 1186 1187 // Note that we only check if a File was returned. If it was out-of-date 1188 // we have complained but we will continue creating a FileID to recover 1189 // gracefully. 1190 if (!File) 1191 return true; 1192 1193 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1194 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1195 // This is the module's main file. 1196 IncludeLoc = getImportLocation(F); 1197 } 1198 SrcMgr::CharacteristicKind 1199 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1200 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1201 ID, BaseOffset + Record[0]); 1202 SrcMgr::FileInfo &FileInfo = 1203 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1204 FileInfo.NumCreatedFIDs = Record[5]; 1205 if (Record[3]) 1206 FileInfo.setHasLineDirectives(); 1207 1208 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1209 unsigned NumFileDecls = Record[7]; 1210 if (NumFileDecls) { 1211 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1212 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1213 NumFileDecls)); 1214 } 1215 1216 const SrcMgr::ContentCache *ContentCache 1217 = SourceMgr.getOrCreateContentCache(File, 1218 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 1219 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1220 ContentCache->ContentsEntry == ContentCache->OrigEntry) { 1221 unsigned Code = SLocEntryCursor.ReadCode(); 1222 Record.clear(); 1223 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob); 1224 1225 if (RecCode != SM_SLOC_BUFFER_BLOB) { 1226 Error("AST record has invalid code"); 1227 return true; 1228 } 1229 1230 llvm::MemoryBuffer *Buffer 1231 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName()); 1232 SourceMgr.overrideFileContents(File, Buffer); 1233 } 1234 1235 break; 1236 } 1237 1238 case SM_SLOC_BUFFER_ENTRY: { 1239 const char *Name = Blob.data(); 1240 unsigned Offset = Record[0]; 1241 SrcMgr::CharacteristicKind 1242 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1243 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1244 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) { 1245 IncludeLoc = getImportLocation(F); 1246 } 1247 unsigned Code = SLocEntryCursor.ReadCode(); 1248 Record.clear(); 1249 unsigned RecCode 1250 = SLocEntryCursor.readRecord(Code, Record, &Blob); 1251 1252 if (RecCode != SM_SLOC_BUFFER_BLOB) { 1253 Error("AST record has invalid code"); 1254 return true; 1255 } 1256 1257 llvm::MemoryBuffer *Buffer 1258 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name); 1259 SourceMgr.createFileID(Buffer, FileCharacter, ID, BaseOffset + Offset, 1260 IncludeLoc); 1261 break; 1262 } 1263 1264 case SM_SLOC_EXPANSION_ENTRY: { 1265 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1266 SourceMgr.createExpansionLoc(SpellingLoc, 1267 ReadSourceLocation(*F, Record[2]), 1268 ReadSourceLocation(*F, Record[3]), 1269 Record[4], 1270 ID, 1271 BaseOffset + Record[0]); 1272 break; 1273 } 1274 } 1275 1276 return false; 1277} 1278 1279std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1280 if (ID == 0) 1281 return std::make_pair(SourceLocation(), ""); 1282 1283 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1284 Error("source location entry ID out-of-range for AST file"); 1285 return std::make_pair(SourceLocation(), ""); 1286 } 1287 1288 // Find which module file this entry lands in. 1289 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1290 if (M->Kind != MK_Module) 1291 return std::make_pair(SourceLocation(), ""); 1292 1293 // FIXME: Can we map this down to a particular submodule? That would be 1294 // ideal. 1295 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1296} 1297 1298/// \brief Find the location where the module F is imported. 1299SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1300 if (F->ImportLoc.isValid()) 1301 return F->ImportLoc; 1302 1303 // Otherwise we have a PCH. It's considered to be "imported" at the first 1304 // location of its includer. 1305 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1306 // Main file is the importer. 1307 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file"); 1308 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1309 } 1310 return F->ImportedBy[0]->FirstLoc; 1311} 1312 1313/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1314/// specified cursor. Read the abbreviations that are at the top of the block 1315/// and then leave the cursor pointing into the block. 1316bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { 1317 if (Cursor.EnterSubBlock(BlockID)) { 1318 Error("malformed block record in AST file"); 1319 return Failure; 1320 } 1321 1322 while (true) { 1323 uint64_t Offset = Cursor.GetCurrentBitNo(); 1324 unsigned Code = Cursor.ReadCode(); 1325 1326 // We expect all abbrevs to be at the start of the block. 1327 if (Code != llvm::bitc::DEFINE_ABBREV) { 1328 Cursor.JumpToBit(Offset); 1329 return false; 1330 } 1331 Cursor.ReadAbbrevRecord(); 1332 } 1333} 1334 1335Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1336 unsigned &Idx) { 1337 Token Tok; 1338 Tok.startToken(); 1339 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1340 Tok.setLength(Record[Idx++]); 1341 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1342 Tok.setIdentifierInfo(II); 1343 Tok.setKind((tok::TokenKind)Record[Idx++]); 1344 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1345 return Tok; 1346} 1347 1348MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1349 BitstreamCursor &Stream = F.MacroCursor; 1350 1351 // Keep track of where we are in the stream, then jump back there 1352 // after reading this macro. 1353 SavedStreamPosition SavedPosition(Stream); 1354 1355 Stream.JumpToBit(Offset); 1356 RecordData Record; 1357 SmallVector<IdentifierInfo*, 16> MacroArgs; 1358 MacroInfo *Macro = nullptr; 1359 1360 while (true) { 1361 // Advance to the next record, but if we get to the end of the block, don't 1362 // pop it (removing all the abbreviations from the cursor) since we want to 1363 // be able to reseek within the block and read entries. 1364 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1365 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags); 1366 1367 switch (Entry.Kind) { 1368 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1369 case llvm::BitstreamEntry::Error: 1370 Error("malformed block record in AST file"); 1371 return Macro; 1372 case llvm::BitstreamEntry::EndBlock: 1373 return Macro; 1374 case llvm::BitstreamEntry::Record: 1375 // The interesting case. 1376 break; 1377 } 1378 1379 // Read a record. 1380 Record.clear(); 1381 PreprocessorRecordTypes RecType = 1382 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record); 1383 switch (RecType) { 1384 case PP_MACRO_DIRECTIVE_HISTORY: 1385 return Macro; 1386 1387 case PP_MACRO_OBJECT_LIKE: 1388 case PP_MACRO_FUNCTION_LIKE: { 1389 // If we already have a macro, that means that we've hit the end 1390 // of the definition of the macro we were looking for. We're 1391 // done. 1392 if (Macro) 1393 return Macro; 1394 1395 unsigned NextIndex = 1; // Skip identifier ID. 1396 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]); 1397 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1398 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID); 1399 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1400 MI->setIsUsed(Record[NextIndex++]); 1401 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1402 1403 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1404 // Decode function-like macro info. 1405 bool isC99VarArgs = Record[NextIndex++]; 1406 bool isGNUVarArgs = Record[NextIndex++]; 1407 bool hasCommaPasting = Record[NextIndex++]; 1408 MacroArgs.clear(); 1409 unsigned NumArgs = Record[NextIndex++]; 1410 for (unsigned i = 0; i != NumArgs; ++i) 1411 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1412 1413 // Install function-like macro info. 1414 MI->setIsFunctionLike(); 1415 if (isC99VarArgs) MI->setIsC99Varargs(); 1416 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1417 if (hasCommaPasting) MI->setHasCommaPasting(); 1418 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), 1419 PP.getPreprocessorAllocator()); 1420 } 1421 1422 // Remember that we saw this macro last so that we add the tokens that 1423 // form its body to it. 1424 Macro = MI; 1425 1426 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1427 Record[NextIndex]) { 1428 // We have a macro definition. Register the association 1429 PreprocessedEntityID 1430 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1431 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1432 PreprocessingRecord::PPEntityID 1433 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true); 1434 MacroDefinition *PPDef = 1435 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID)); 1436 if (PPDef) 1437 PPRec.RegisterMacroDefinition(Macro, PPDef); 1438 } 1439 1440 ++NumMacrosRead; 1441 break; 1442 } 1443 1444 case PP_TOKEN: { 1445 // If we see a TOKEN before a PP_MACRO_*, then the file is 1446 // erroneous, just pretend we didn't see this. 1447 if (!Macro) break; 1448 1449 unsigned Idx = 0; 1450 Token Tok = ReadToken(F, Record, Idx); 1451 Macro->AddTokenToBody(Tok); 1452 break; 1453 } 1454 } 1455 } 1456} 1457 1458PreprocessedEntityID 1459ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const { 1460 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1461 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1462 assert(I != M.PreprocessedEntityRemap.end() 1463 && "Invalid index into preprocessed entity index remap"); 1464 1465 return LocalID + I->second; 1466} 1467 1468unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1469 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1470} 1471 1472HeaderFileInfoTrait::internal_key_type 1473HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1474 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(), 1475 FE->getName() }; 1476 return ikey; 1477} 1478 1479bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1480 if (a.Size != b.Size || a.ModTime != b.ModTime) 1481 return false; 1482 1483 if (strcmp(a.Filename, b.Filename) == 0) 1484 return true; 1485 1486 // Determine whether the actual files are equivalent. 1487 FileManager &FileMgr = Reader.getFileManager(); 1488 const FileEntry *FEA = FileMgr.getFile(a.Filename); 1489 const FileEntry *FEB = FileMgr.getFile(b.Filename); 1490 return (FEA && FEA == FEB); 1491} 1492 1493std::pair<unsigned, unsigned> 1494HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1495 using namespace llvm::support; 1496 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1497 unsigned DataLen = (unsigned) *d++; 1498 return std::make_pair(KeyLen, DataLen); 1499} 1500 1501HeaderFileInfoTrait::internal_key_type 1502HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1503 using namespace llvm::support; 1504 internal_key_type ikey; 1505 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1506 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1507 ikey.Filename = (const char *)d; 1508 return ikey; 1509} 1510 1511HeaderFileInfoTrait::data_type 1512HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1513 unsigned DataLen) { 1514 const unsigned char *End = d + DataLen; 1515 using namespace llvm::support; 1516 HeaderFileInfo HFI; 1517 unsigned Flags = *d++; 1518 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole> 1519 ((Flags >> 6) & 0x03); 1520 HFI.isImport = (Flags >> 5) & 0x01; 1521 HFI.isPragmaOnce = (Flags >> 4) & 0x01; 1522 HFI.DirInfo = (Flags >> 2) & 0x03; 1523 HFI.Resolved = (Flags >> 1) & 0x01; 1524 HFI.IndexHeaderMapHeader = Flags & 0x01; 1525 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d); 1526 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1527 M, endian::readNext<uint32_t, little, unaligned>(d)); 1528 if (unsigned FrameworkOffset = 1529 endian::readNext<uint32_t, little, unaligned>(d)) { 1530 // The framework offset is 1 greater than the actual offset, 1531 // since 0 is used as an indicator for "no framework name". 1532 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1533 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1534 } 1535 1536 if (d != End) { 1537 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1538 if (LocalSMID) { 1539 // This header is part of a module. Associate it with the module to enable 1540 // implicit module import. 1541 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1542 Module *Mod = Reader.getSubmodule(GlobalSMID); 1543 HFI.isModuleHeader = true; 1544 FileManager &FileMgr = Reader.getFileManager(); 1545 ModuleMap &ModMap = 1546 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1547 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole()); 1548 } 1549 } 1550 1551 assert(End == d && "Wrong data length in HeaderFileInfo deserialization"); 1552 (void)End; 1553 1554 // This HeaderFileInfo was externally loaded. 1555 HFI.External = true; 1556 return HFI; 1557} 1558 1559void 1560ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M, 1561 GlobalMacroID GMacID, 1562 llvm::ArrayRef<SubmoduleID> Overrides) { 1563 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1564 SubmoduleID *OverrideData = nullptr; 1565 if (!Overrides.empty()) { 1566 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1]; 1567 OverrideData[0] = Overrides.size(); 1568 for (unsigned I = 0; I != Overrides.size(); ++I) 1569 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]); 1570 } 1571 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData)); 1572} 1573 1574void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II, 1575 ModuleFile *M, 1576 uint64_t MacroDirectivesOffset) { 1577 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1578 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1579} 1580 1581void ASTReader::ReadDefinedMacros() { 1582 // Note that we are loading defined macros. 1583 Deserializing Macros(this); 1584 1585 for (ModuleReverseIterator I = ModuleMgr.rbegin(), 1586 E = ModuleMgr.rend(); I != E; ++I) { 1587 BitstreamCursor &MacroCursor = (*I)->MacroCursor; 1588 1589 // If there was no preprocessor block, skip this file. 1590 if (!MacroCursor.getBitStreamReader()) 1591 continue; 1592 1593 BitstreamCursor Cursor = MacroCursor; 1594 Cursor.JumpToBit((*I)->MacroStartOffset); 1595 1596 RecordData Record; 1597 while (true) { 1598 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks(); 1599 1600 switch (E.Kind) { 1601 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1602 case llvm::BitstreamEntry::Error: 1603 Error("malformed block record in AST file"); 1604 return; 1605 case llvm::BitstreamEntry::EndBlock: 1606 goto NextCursor; 1607 1608 case llvm::BitstreamEntry::Record: 1609 Record.clear(); 1610 switch (Cursor.readRecord(E.ID, Record)) { 1611 default: // Default behavior: ignore. 1612 break; 1613 1614 case PP_MACRO_OBJECT_LIKE: 1615 case PP_MACRO_FUNCTION_LIKE: 1616 getLocalIdentifier(**I, Record[0]); 1617 break; 1618 1619 case PP_TOKEN: 1620 // Ignore tokens. 1621 break; 1622 } 1623 break; 1624 } 1625 } 1626 NextCursor: ; 1627 } 1628} 1629 1630namespace { 1631 /// \brief Visitor class used to look up identifirs in an AST file. 1632 class IdentifierLookupVisitor { 1633 StringRef Name; 1634 unsigned PriorGeneration; 1635 unsigned &NumIdentifierLookups; 1636 unsigned &NumIdentifierLookupHits; 1637 IdentifierInfo *Found; 1638 1639 public: 1640 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 1641 unsigned &NumIdentifierLookups, 1642 unsigned &NumIdentifierLookupHits) 1643 : Name(Name), PriorGeneration(PriorGeneration), 1644 NumIdentifierLookups(NumIdentifierLookups), 1645 NumIdentifierLookupHits(NumIdentifierLookupHits), 1646 Found() 1647 { 1648 } 1649 1650 static bool visit(ModuleFile &M, void *UserData) { 1651 IdentifierLookupVisitor *This 1652 = static_cast<IdentifierLookupVisitor *>(UserData); 1653 1654 // If we've already searched this module file, skip it now. 1655 if (M.Generation <= This->PriorGeneration) 1656 return true; 1657 1658 ASTIdentifierLookupTable *IdTable 1659 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 1660 if (!IdTable) 1661 return false; 1662 1663 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), 1664 M, This->Found); 1665 ++This->NumIdentifierLookups; 1666 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait); 1667 if (Pos == IdTable->end()) 1668 return false; 1669 1670 // Dereferencing the iterator has the effect of building the 1671 // IdentifierInfo node and populating it with the various 1672 // declarations it needs. 1673 ++This->NumIdentifierLookupHits; 1674 This->Found = *Pos; 1675 return true; 1676 } 1677 1678 // \brief Retrieve the identifier info found within the module 1679 // files. 1680 IdentifierInfo *getIdentifierInfo() const { return Found; } 1681 }; 1682} 1683 1684void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 1685 // Note that we are loading an identifier. 1686 Deserializing AnIdentifier(this); 1687 1688 unsigned PriorGeneration = 0; 1689 if (getContext().getLangOpts().Modules) 1690 PriorGeneration = IdentifierGeneration[&II]; 1691 1692 // If there is a global index, look there first to determine which modules 1693 // provably do not have any results for this identifier. 1694 GlobalModuleIndex::HitSet Hits; 1695 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 1696 if (!loadGlobalIndex()) { 1697 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 1698 HitsPtr = &Hits; 1699 } 1700 } 1701 1702 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 1703 NumIdentifierLookups, 1704 NumIdentifierLookupHits); 1705 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); 1706 markIdentifierUpToDate(&II); 1707} 1708 1709void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 1710 if (!II) 1711 return; 1712 1713 II->setOutOfDate(false); 1714 1715 // Update the generation for this identifier. 1716 if (getContext().getLangOpts().Modules) 1717 IdentifierGeneration[II] = getGeneration(); 1718} 1719 1720struct ASTReader::ModuleMacroInfo { 1721 SubmoduleID SubModID; 1722 MacroInfo *MI; 1723 SubmoduleID *Overrides; 1724 // FIXME: Remove this. 1725 ModuleFile *F; 1726 1727 bool isDefine() const { return MI; } 1728 1729 SubmoduleID getSubmoduleID() const { return SubModID; } 1730 1731 llvm::ArrayRef<SubmoduleID> getOverriddenSubmodules() const { 1732 if (!Overrides) 1733 return llvm::ArrayRef<SubmoduleID>(); 1734 return llvm::makeArrayRef(Overrides + 1, *Overrides); 1735 } 1736 1737 DefMacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const { 1738 if (!MI) 1739 return nullptr; 1740 return PP.AllocateDefMacroDirective(MI, ImportLoc, /*isImported=*/true); 1741 } 1742}; 1743 1744ASTReader::ModuleMacroInfo * 1745ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) { 1746 ModuleMacroInfo Info; 1747 1748 uint32_t ID = PMInfo.ModuleMacroData.MacID; 1749 if (ID & 1) { 1750 // Macro undefinition. 1751 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1); 1752 Info.MI = nullptr; 1753 } else { 1754 // Macro definition. 1755 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1); 1756 assert(GMacID); 1757 1758 // If this macro has already been loaded, don't do so again. 1759 // FIXME: This is highly dubious. Multiple macro definitions can have the 1760 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc. 1761 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS]) 1762 return nullptr; 1763 1764 Info.MI = getMacro(GMacID); 1765 Info.SubModID = Info.MI->getOwningModuleID(); 1766 } 1767 Info.Overrides = PMInfo.ModuleMacroData.Overrides; 1768 Info.F = PMInfo.M; 1769 1770 return new (Context) ModuleMacroInfo(Info); 1771} 1772 1773void ASTReader::resolvePendingMacro(IdentifierInfo *II, 1774 const PendingMacroInfo &PMInfo) { 1775 assert(II); 1776 1777 if (PMInfo.M->Kind != MK_Module) { 1778 installPCHMacroDirectives(II, *PMInfo.M, 1779 PMInfo.PCHMacroData.MacroDirectivesOffset); 1780 return; 1781 } 1782 1783 // Module Macro. 1784 1785 ModuleMacroInfo *MMI = getModuleMacro(PMInfo); 1786 if (!MMI) 1787 return; 1788 1789 Module *Owner = getSubmodule(MMI->getSubmoduleID()); 1790 if (Owner && Owner->NameVisibility == Module::Hidden) { 1791 // Macros in the owning module are hidden. Just remember this macro to 1792 // install if we make this module visible. 1793 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI)); 1794 } else { 1795 installImportedMacro(II, MMI, Owner); 1796 } 1797} 1798 1799void ASTReader::installPCHMacroDirectives(IdentifierInfo *II, 1800 ModuleFile &M, uint64_t Offset) { 1801 assert(M.Kind != MK_Module); 1802 1803 BitstreamCursor &Cursor = M.MacroCursor; 1804 SavedStreamPosition SavedPosition(Cursor); 1805 Cursor.JumpToBit(Offset); 1806 1807 llvm::BitstreamEntry Entry = 1808 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 1809 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1810 Error("malformed block record in AST file"); 1811 return; 1812 } 1813 1814 RecordData Record; 1815 PreprocessorRecordTypes RecType = 1816 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record); 1817 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) { 1818 Error("malformed block record in AST file"); 1819 return; 1820 } 1821 1822 // Deserialize the macro directives history in reverse source-order. 1823 MacroDirective *Latest = nullptr, *Earliest = nullptr; 1824 unsigned Idx = 0, N = Record.size(); 1825 while (Idx < N) { 1826 MacroDirective *MD = nullptr; 1827 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 1828 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 1829 switch (K) { 1830 case MacroDirective::MD_Define: { 1831 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]); 1832 MacroInfo *MI = getMacro(GMacID); 1833 bool isImported = Record[Idx++]; 1834 bool isAmbiguous = Record[Idx++]; 1835 DefMacroDirective *DefMD = 1836 PP.AllocateDefMacroDirective(MI, Loc, isImported); 1837 DefMD->setAmbiguous(isAmbiguous); 1838 MD = DefMD; 1839 break; 1840 } 1841 case MacroDirective::MD_Undefine: 1842 MD = PP.AllocateUndefMacroDirective(Loc); 1843 break; 1844 case MacroDirective::MD_Visibility: { 1845 bool isPublic = Record[Idx++]; 1846 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 1847 break; 1848 } 1849 } 1850 1851 if (!Latest) 1852 Latest = MD; 1853 if (Earliest) 1854 Earliest->setPrevious(MD); 1855 Earliest = MD; 1856 } 1857 1858 PP.setLoadedMacroDirective(II, Latest); 1859} 1860 1861/// \brief For the given macro definitions, check if they are both in system 1862/// modules. 1863static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI, 1864 Module *NewOwner, ASTReader &Reader) { 1865 assert(PrevMI && NewMI); 1866 Module *PrevOwner = nullptr; 1867 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID()) 1868 PrevOwner = Reader.getSubmodule(PrevModID); 1869 SourceManager &SrcMgr = Reader.getSourceManager(); 1870 bool PrevInSystem 1871 = PrevOwner? PrevOwner->IsSystem 1872 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc()); 1873 bool NewInSystem 1874 = NewOwner? NewOwner->IsSystem 1875 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc()); 1876 if (PrevOwner && PrevOwner == NewOwner) 1877 return false; 1878 return PrevInSystem && NewInSystem; 1879} 1880 1881void ASTReader::removeOverriddenMacros(IdentifierInfo *II, 1882 AmbiguousMacros &Ambig, 1883 llvm::ArrayRef<SubmoduleID> Overrides) { 1884 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) { 1885 SubmoduleID OwnerID = Overrides[OI]; 1886 1887 // If this macro is not yet visible, remove it from the hidden names list. 1888 Module *Owner = getSubmodule(OwnerID); 1889 HiddenNames &Hidden = HiddenNamesMap[Owner]; 1890 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II); 1891 if (HI != Hidden.HiddenMacros.end()) { 1892 auto SubOverrides = HI->second->getOverriddenSubmodules(); 1893 Hidden.HiddenMacros.erase(HI); 1894 removeOverriddenMacros(II, Ambig, SubOverrides); 1895 } 1896 1897 // If this macro is already in our list of conflicts, remove it from there. 1898 Ambig.erase( 1899 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) { 1900 return MD->getInfo()->getOwningModuleID() == OwnerID; 1901 }), 1902 Ambig.end()); 1903 } 1904} 1905 1906ASTReader::AmbiguousMacros * 1907ASTReader::removeOverriddenMacros(IdentifierInfo *II, 1908 llvm::ArrayRef<SubmoduleID> Overrides) { 1909 MacroDirective *Prev = PP.getMacroDirective(II); 1910 if (!Prev && Overrides.empty()) 1911 return nullptr; 1912 1913 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective() 1914 : nullptr; 1915 if (PrevDef && PrevDef->isAmbiguous()) { 1916 // We had a prior ambiguity. Check whether we resolve it (or make it worse). 1917 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II]; 1918 Ambig.push_back(PrevDef); 1919 1920 removeOverriddenMacros(II, Ambig, Overrides); 1921 1922 if (!Ambig.empty()) 1923 return &Ambig; 1924 1925 AmbiguousMacroDefs.erase(II); 1926 } else { 1927 // There's no ambiguity yet. Maybe we're introducing one. 1928 AmbiguousMacros Ambig; 1929 if (PrevDef) 1930 Ambig.push_back(PrevDef); 1931 1932 removeOverriddenMacros(II, Ambig, Overrides); 1933 1934 if (!Ambig.empty()) { 1935 AmbiguousMacros &Result = AmbiguousMacroDefs[II]; 1936 std::swap(Result, Ambig); 1937 return &Result; 1938 } 1939 } 1940 1941 // We ended up with no ambiguity. 1942 return nullptr; 1943} 1944 1945void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI, 1946 Module *Owner) { 1947 assert(II && Owner); 1948 1949 SourceLocation ImportLoc = Owner->MacroVisibilityLoc; 1950 if (ImportLoc.isInvalid()) { 1951 // FIXME: If we made macros from this module visible but didn't provide a 1952 // source location for the import, we don't have a location for the macro. 1953 // Use the location at which the containing module file was first imported 1954 // for now. 1955 ImportLoc = MMI->F->DirectImportLoc; 1956 assert(ImportLoc.isValid() && "no import location for a visible macro?"); 1957 } 1958 1959 AmbiguousMacros *Prev = 1960 removeOverriddenMacros(II, MMI->getOverriddenSubmodules()); 1961 1962 // Create a synthetic macro definition corresponding to the import (or null 1963 // if this was an undefinition of the macro). 1964 DefMacroDirective *MD = MMI->import(PP, ImportLoc); 1965 1966 // If there's no ambiguity, just install the macro. 1967 if (!Prev) { 1968 if (MD) 1969 PP.appendMacroDirective(II, MD); 1970 else 1971 PP.appendMacroDirective(II, PP.AllocateUndefMacroDirective(ImportLoc)); 1972 return; 1973 } 1974 assert(!Prev->empty()); 1975 1976 if (!MD) { 1977 // We imported a #undef that didn't remove all prior definitions. The most 1978 // recent prior definition remains, and we install it in the place of the 1979 // imported directive. 1980 MacroInfo *NewMI = Prev->back()->getInfo(); 1981 Prev->pop_back(); 1982 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc, /*Imported*/true); 1983 } 1984 1985 // We're introducing a macro definition that creates or adds to an ambiguity. 1986 // We can resolve that ambiguity if this macro is token-for-token identical to 1987 // all of the existing definitions. 1988 MacroInfo *NewMI = MD->getInfo(); 1989 assert(NewMI && "macro definition with no MacroInfo?"); 1990 while (!Prev->empty()) { 1991 MacroInfo *PrevMI = Prev->back()->getInfo(); 1992 assert(PrevMI && "macro definition with no MacroInfo?"); 1993 1994 // Before marking the macros as ambiguous, check if this is a case where 1995 // both macros are in system headers. If so, we trust that the system 1996 // did not get it wrong. This also handles cases where Clang's own 1997 // headers have a different spelling of certain system macros: 1998 // #define LONG_MAX __LONG_MAX__ (clang's limits.h) 1999 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h) 2000 // 2001 // FIXME: Remove the defined-in-system-headers check. clang's limits.h 2002 // overrides the system limits.h's macros, so there's no conflict here. 2003 if (NewMI != PrevMI && 2004 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) && 2005 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) 2006 break; 2007 2008 // The previous definition is the same as this one (or both are defined in 2009 // system modules so we can assume they're equivalent); we don't need to 2010 // track it any more. 2011 Prev->pop_back(); 2012 } 2013 2014 if (!Prev->empty()) 2015 MD->setAmbiguous(true); 2016 2017 PP.appendMacroDirective(II, MD); 2018} 2019 2020ASTReader::InputFileInfo 2021ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2022 // Go find this input file. 2023 BitstreamCursor &Cursor = F.InputFilesCursor; 2024 SavedStreamPosition SavedPosition(Cursor); 2025 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 2026 2027 unsigned Code = Cursor.ReadCode(); 2028 RecordData Record; 2029 StringRef Blob; 2030 2031 unsigned Result = Cursor.readRecord(Code, Record, &Blob); 2032 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE && 2033 "invalid record type for input file"); 2034 (void)Result; 2035 2036 std::string Filename; 2037 off_t StoredSize; 2038 time_t StoredTime; 2039 bool Overridden; 2040 2041 assert(Record[0] == ID && "Bogus stored ID or offset"); 2042 StoredSize = static_cast<off_t>(Record[1]); 2043 StoredTime = static_cast<time_t>(Record[2]); 2044 Overridden = static_cast<bool>(Record[3]); 2045 Filename = Blob; 2046 MaybeAddSystemRootToFilename(F, Filename); 2047 2048 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden }; 2049 return R; 2050} 2051 2052std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) { 2053 return readInputFileInfo(F, ID).Filename; 2054} 2055 2056InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2057 // If this ID is bogus, just return an empty input file. 2058 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2059 return InputFile(); 2060 2061 // If we've already loaded this input file, return it. 2062 if (F.InputFilesLoaded[ID-1].getFile()) 2063 return F.InputFilesLoaded[ID-1]; 2064 2065 if (F.InputFilesLoaded[ID-1].isNotFound()) 2066 return InputFile(); 2067 2068 // Go find this input file. 2069 BitstreamCursor &Cursor = F.InputFilesCursor; 2070 SavedStreamPosition SavedPosition(Cursor); 2071 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 2072 2073 InputFileInfo FI = readInputFileInfo(F, ID); 2074 off_t StoredSize = FI.StoredSize; 2075 time_t StoredTime = FI.StoredTime; 2076 bool Overridden = FI.Overridden; 2077 StringRef Filename = FI.Filename; 2078 2079 const FileEntry *File 2080 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime) 2081 : FileMgr.getFile(Filename, /*OpenFile=*/false); 2082 2083 // If we didn't find the file, resolve it relative to the 2084 // original directory from which this AST file was created. 2085 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() && 2086 F.OriginalDir != CurrentDir) { 2087 std::string Resolved = resolveFileRelativeToOriginalDir(Filename, 2088 F.OriginalDir, 2089 CurrentDir); 2090 if (!Resolved.empty()) 2091 File = FileMgr.getFile(Resolved); 2092 } 2093 2094 // For an overridden file, create a virtual file with the stored 2095 // size/timestamp. 2096 if (Overridden && File == nullptr) { 2097 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 2098 } 2099 2100 if (File == nullptr) { 2101 if (Complain) { 2102 std::string ErrorStr = "could not find file '"; 2103 ErrorStr += Filename; 2104 ErrorStr += "' referenced by AST file"; 2105 Error(ErrorStr.c_str()); 2106 } 2107 // Record that we didn't find the file. 2108 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2109 return InputFile(); 2110 } 2111 2112 // Check if there was a request to override the contents of the file 2113 // that was part of the precompiled header. Overridding such a file 2114 // can lead to problems when lexing using the source locations from the 2115 // PCH. 2116 SourceManager &SM = getSourceManager(); 2117 if (!Overridden && SM.isFileOverridden(File)) { 2118 if (Complain) 2119 Error(diag::err_fe_pch_file_overridden, Filename); 2120 // After emitting the diagnostic, recover by disabling the override so 2121 // that the original file will be used. 2122 SM.disableFileContentsOverride(File); 2123 // The FileEntry is a virtual file entry with the size of the contents 2124 // that would override the original contents. Set it to the original's 2125 // size/time. 2126 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 2127 StoredSize, StoredTime); 2128 } 2129 2130 bool IsOutOfDate = false; 2131 2132 // For an overridden file, there is nothing to validate. 2133 if (!Overridden && (StoredSize != File->getSize() 2134#if !defined(LLVM_ON_WIN32) 2135 // In our regression testing, the Windows file system seems to 2136 // have inconsistent modification times that sometimes 2137 // erroneously trigger this error-handling path. 2138 || StoredTime != File->getModificationTime() 2139#endif 2140 )) { 2141 if (Complain) { 2142 // Build a list of the PCH imports that got us here (in reverse). 2143 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2144 while (ImportStack.back()->ImportedBy.size() > 0) 2145 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2146 2147 // The top-level PCH is stale. 2148 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2149 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName); 2150 2151 // Print the import stack. 2152 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2153 Diag(diag::note_pch_required_by) 2154 << Filename << ImportStack[0]->FileName; 2155 for (unsigned I = 1; I < ImportStack.size(); ++I) 2156 Diag(diag::note_pch_required_by) 2157 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2158 } 2159 2160 if (!Diags.isDiagnosticInFlight()) 2161 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2162 } 2163 2164 IsOutOfDate = true; 2165 } 2166 2167 InputFile IF = InputFile(File, Overridden, IsOutOfDate); 2168 2169 // Note that we've loaded this input file. 2170 F.InputFilesLoaded[ID-1] = IF; 2171 return IF; 2172} 2173 2174const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) { 2175 ModuleFile &M = ModuleMgr.getPrimaryModule(); 2176 std::string Filename = filenameStrRef; 2177 MaybeAddSystemRootToFilename(M, Filename); 2178 const FileEntry *File = FileMgr.getFile(Filename); 2179 if (File == nullptr && !M.OriginalDir.empty() && !CurrentDir.empty() && 2180 M.OriginalDir != CurrentDir) { 2181 std::string resolved = resolveFileRelativeToOriginalDir(Filename, 2182 M.OriginalDir, 2183 CurrentDir); 2184 if (!resolved.empty()) 2185 File = FileMgr.getFile(resolved); 2186 } 2187 2188 return File; 2189} 2190 2191/// \brief If we are loading a relocatable PCH file, and the filename is 2192/// not an absolute path, add the system root to the beginning of the file 2193/// name. 2194void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M, 2195 std::string &Filename) { 2196 // If this is not a relocatable PCH file, there's nothing to do. 2197 if (!M.RelocatablePCH) 2198 return; 2199 2200 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2201 return; 2202 2203 if (isysroot.empty()) { 2204 // If no system root was given, default to '/' 2205 Filename.insert(Filename.begin(), '/'); 2206 return; 2207 } 2208 2209 unsigned Length = isysroot.size(); 2210 if (isysroot[Length - 1] != '/') 2211 Filename.insert(Filename.begin(), '/'); 2212 2213 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end()); 2214} 2215 2216ASTReader::ASTReadResult 2217ASTReader::ReadControlBlock(ModuleFile &F, 2218 SmallVectorImpl<ImportedModule> &Loaded, 2219 const ModuleFile *ImportedBy, 2220 unsigned ClientLoadCapabilities) { 2221 BitstreamCursor &Stream = F.Stream; 2222 2223 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2224 Error("malformed block record in AST file"); 2225 return Failure; 2226 } 2227 2228 // Read all of the records and blocks in the control block. 2229 RecordData Record; 2230 while (1) { 2231 llvm::BitstreamEntry Entry = Stream.advance(); 2232 2233 switch (Entry.Kind) { 2234 case llvm::BitstreamEntry::Error: 2235 Error("malformed block record in AST file"); 2236 return Failure; 2237 case llvm::BitstreamEntry::EndBlock: { 2238 // Validate input files. 2239 const HeaderSearchOptions &HSOpts = 2240 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2241 2242 // All user input files reside at the index range [0, Record[1]), and 2243 // system input files reside at [Record[1], Record[0]). 2244 // Record is the one from INPUT_FILE_OFFSETS. 2245 unsigned NumInputs = Record[0]; 2246 unsigned NumUserInputs = Record[1]; 2247 2248 if (!DisableValidation && 2249 (ValidateSystemInputs || !HSOpts.ModulesValidateOncePerBuildSession || 2250 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) { 2251 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2252 2253 // If we are reading a module, we will create a verification timestamp, 2254 // so we verify all input files. Otherwise, verify only user input 2255 // files. 2256 2257 unsigned N = NumUserInputs; 2258 if (ValidateSystemInputs || 2259 (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module)) 2260 N = NumInputs; 2261 2262 for (unsigned I = 0; I < N; ++I) { 2263 InputFile IF = getInputFile(F, I+1, Complain); 2264 if (!IF.getFile() || IF.isOutOfDate()) 2265 return OutOfDate; 2266 } 2267 } 2268 2269 if (Listener) 2270 Listener->visitModuleFile(F.FileName); 2271 2272 if (Listener && Listener->needsInputFileVisitation()) { 2273 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2274 : NumUserInputs; 2275 for (unsigned I = 0; I < N; ++I) { 2276 bool IsSystem = I >= NumUserInputs; 2277 InputFileInfo FI = readInputFileInfo(F, I+1); 2278 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden); 2279 } 2280 } 2281 2282 return Success; 2283 } 2284 2285 case llvm::BitstreamEntry::SubBlock: 2286 switch (Entry.ID) { 2287 case INPUT_FILES_BLOCK_ID: 2288 F.InputFilesCursor = Stream; 2289 if (Stream.SkipBlock() || // Skip with the main cursor 2290 // Read the abbreviations 2291 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2292 Error("malformed block record in AST file"); 2293 return Failure; 2294 } 2295 continue; 2296 2297 default: 2298 if (Stream.SkipBlock()) { 2299 Error("malformed block record in AST file"); 2300 return Failure; 2301 } 2302 continue; 2303 } 2304 2305 case llvm::BitstreamEntry::Record: 2306 // The interesting case. 2307 break; 2308 } 2309 2310 // Read and process a record. 2311 Record.clear(); 2312 StringRef Blob; 2313 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 2314 case METADATA: { 2315 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2316 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2317 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2318 : diag::err_pch_version_too_new); 2319 return VersionMismatch; 2320 } 2321 2322 bool hasErrors = Record[5]; 2323 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2324 Diag(diag::err_pch_with_compiler_errors); 2325 return HadErrors; 2326 } 2327 2328 F.RelocatablePCH = Record[4]; 2329 2330 const std::string &CurBranch = getClangFullRepositoryVersion(); 2331 StringRef ASTBranch = Blob; 2332 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2333 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2334 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2335 return VersionMismatch; 2336 } 2337 break; 2338 } 2339 2340 case IMPORTS: { 2341 // Load each of the imported PCH files. 2342 unsigned Idx = 0, N = Record.size(); 2343 while (Idx < N) { 2344 // Read information about the AST file. 2345 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2346 // The import location will be the local one for now; we will adjust 2347 // all import locations of module imports after the global source 2348 // location info are setup. 2349 SourceLocation ImportLoc = 2350 SourceLocation::getFromRawEncoding(Record[Idx++]); 2351 off_t StoredSize = (off_t)Record[Idx++]; 2352 time_t StoredModTime = (time_t)Record[Idx++]; 2353 unsigned Length = Record[Idx++]; 2354 SmallString<128> ImportedFile(Record.begin() + Idx, 2355 Record.begin() + Idx + Length); 2356 Idx += Length; 2357 2358 // Load the AST file. 2359 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded, 2360 StoredSize, StoredModTime, 2361 ClientLoadCapabilities)) { 2362 case Failure: return Failure; 2363 // If we have to ignore the dependency, we'll have to ignore this too. 2364 case Missing: 2365 case OutOfDate: return OutOfDate; 2366 case VersionMismatch: return VersionMismatch; 2367 case ConfigurationMismatch: return ConfigurationMismatch; 2368 case HadErrors: return HadErrors; 2369 case Success: break; 2370 } 2371 } 2372 break; 2373 } 2374 2375 case LANGUAGE_OPTIONS: { 2376 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2377 if (Listener && &F == *ModuleMgr.begin() && 2378 ParseLanguageOptions(Record, Complain, *Listener) && 2379 !DisableValidation && !AllowConfigurationMismatch) 2380 return ConfigurationMismatch; 2381 break; 2382 } 2383 2384 case TARGET_OPTIONS: { 2385 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2386 if (Listener && &F == *ModuleMgr.begin() && 2387 ParseTargetOptions(Record, Complain, *Listener) && 2388 !DisableValidation && !AllowConfigurationMismatch) 2389 return ConfigurationMismatch; 2390 break; 2391 } 2392 2393 case DIAGNOSTIC_OPTIONS: { 2394 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0; 2395 if (Listener && &F == *ModuleMgr.begin() && 2396 ParseDiagnosticOptions(Record, Complain, *Listener) && 2397 !DisableValidation) 2398 return OutOfDate; 2399 break; 2400 } 2401 2402 case FILE_SYSTEM_OPTIONS: { 2403 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2404 if (Listener && &F == *ModuleMgr.begin() && 2405 ParseFileSystemOptions(Record, Complain, *Listener) && 2406 !DisableValidation && !AllowConfigurationMismatch) 2407 return ConfigurationMismatch; 2408 break; 2409 } 2410 2411 case HEADER_SEARCH_OPTIONS: { 2412 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2413 if (Listener && &F == *ModuleMgr.begin() && 2414 ParseHeaderSearchOptions(Record, Complain, *Listener) && 2415 !DisableValidation && !AllowConfigurationMismatch) 2416 return ConfigurationMismatch; 2417 break; 2418 } 2419 2420 case PREPROCESSOR_OPTIONS: { 2421 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2422 if (Listener && &F == *ModuleMgr.begin() && 2423 ParsePreprocessorOptions(Record, Complain, *Listener, 2424 SuggestedPredefines) && 2425 !DisableValidation && !AllowConfigurationMismatch) 2426 return ConfigurationMismatch; 2427 break; 2428 } 2429 2430 case ORIGINAL_FILE: 2431 F.OriginalSourceFileID = FileID::get(Record[0]); 2432 F.ActualOriginalSourceFileName = Blob; 2433 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2434 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName); 2435 break; 2436 2437 case ORIGINAL_FILE_ID: 2438 F.OriginalSourceFileID = FileID::get(Record[0]); 2439 break; 2440 2441 case ORIGINAL_PCH_DIR: 2442 F.OriginalDir = Blob; 2443 break; 2444 2445 case MODULE_NAME: 2446 F.ModuleName = Blob; 2447 if (Listener) 2448 Listener->ReadModuleName(F.ModuleName); 2449 break; 2450 2451 case MODULE_MAP_FILE: 2452 F.ModuleMapPath = Blob; 2453 2454 // Try to resolve ModuleName in the current header search context and 2455 // verify that it is found in the same module map file as we saved. If the 2456 // top-level AST file is a main file, skip this check because there is no 2457 // usable header search context. 2458 assert(!F.ModuleName.empty() && 2459 "MODULE_NAME should come before MOUDLE_MAP_FILE"); 2460 if (F.Kind == MK_Module && 2461 (*ModuleMgr.begin())->Kind != MK_MainFile) { 2462 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 2463 if (!M) { 2464 assert(ImportedBy && "top-level import should be verified"); 2465 if ((ClientLoadCapabilities & ARR_Missing) == 0) 2466 Diag(diag::err_imported_module_not_found) 2467 << F.ModuleName << ImportedBy->FileName; 2468 return Missing; 2469 } 2470 2471 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath); 2472 if (StoredModMap == nullptr || StoredModMap != M->ModuleMap) { 2473 assert(M->ModuleMap && "found module is missing module map file"); 2474 assert(M->Name == F.ModuleName && "found module with different name"); 2475 assert(ImportedBy && "top-level import should be verified"); 2476 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2477 Diag(diag::err_imported_module_modmap_changed) 2478 << F.ModuleName << ImportedBy->FileName 2479 << M->ModuleMap->getName() << F.ModuleMapPath; 2480 return OutOfDate; 2481 } 2482 } 2483 2484 if (Listener) 2485 Listener->ReadModuleMapFile(F.ModuleMapPath); 2486 break; 2487 2488 case INPUT_FILE_OFFSETS: 2489 F.InputFileOffsets = (const uint32_t *)Blob.data(); 2490 F.InputFilesLoaded.resize(Record[0]); 2491 break; 2492 } 2493 } 2494} 2495 2496ASTReader::ASTReadResult 2497ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2498 BitstreamCursor &Stream = F.Stream; 2499 2500 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2501 Error("malformed block record in AST file"); 2502 return Failure; 2503 } 2504 2505 // Read all of the records and blocks for the AST file. 2506 RecordData Record; 2507 while (1) { 2508 llvm::BitstreamEntry Entry = Stream.advance(); 2509 2510 switch (Entry.Kind) { 2511 case llvm::BitstreamEntry::Error: 2512 Error("error at end of module block in AST file"); 2513 return Failure; 2514 case llvm::BitstreamEntry::EndBlock: { 2515 // Outside of C++, we do not store a lookup map for the translation unit. 2516 // Instead, mark it as needing a lookup map to be built if this module 2517 // contains any declarations lexically within it (which it always does!). 2518 // This usually has no cost, since we very rarely need the lookup map for 2519 // the translation unit outside C++. 2520 DeclContext *DC = Context.getTranslationUnitDecl(); 2521 if (DC->hasExternalLexicalStorage() && 2522 !getContext().getLangOpts().CPlusPlus) 2523 DC->setMustBuildLookupTable(); 2524 2525 return Success; 2526 } 2527 case llvm::BitstreamEntry::SubBlock: 2528 switch (Entry.ID) { 2529 case DECLTYPES_BLOCK_ID: 2530 // We lazily load the decls block, but we want to set up the 2531 // DeclsCursor cursor to point into it. Clone our current bitcode 2532 // cursor to it, enter the block and read the abbrevs in that block. 2533 // With the main cursor, we just skip over it. 2534 F.DeclsCursor = Stream; 2535 if (Stream.SkipBlock() || // Skip with the main cursor. 2536 // Read the abbrevs. 2537 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 2538 Error("malformed block record in AST file"); 2539 return Failure; 2540 } 2541 break; 2542 2543 case PREPROCESSOR_BLOCK_ID: 2544 F.MacroCursor = Stream; 2545 if (!PP.getExternalSource()) 2546 PP.setExternalSource(this); 2547 2548 if (Stream.SkipBlock() || 2549 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2550 Error("malformed block record in AST file"); 2551 return Failure; 2552 } 2553 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2554 break; 2555 2556 case PREPROCESSOR_DETAIL_BLOCK_ID: 2557 F.PreprocessorDetailCursor = Stream; 2558 if (Stream.SkipBlock() || 2559 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2560 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2561 Error("malformed preprocessor detail record in AST file"); 2562 return Failure; 2563 } 2564 F.PreprocessorDetailStartOffset 2565 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2566 2567 if (!PP.getPreprocessingRecord()) 2568 PP.createPreprocessingRecord(); 2569 if (!PP.getPreprocessingRecord()->getExternalSource()) 2570 PP.getPreprocessingRecord()->SetExternalSource(*this); 2571 break; 2572 2573 case SOURCE_MANAGER_BLOCK_ID: 2574 if (ReadSourceManagerBlock(F)) 2575 return Failure; 2576 break; 2577 2578 case SUBMODULE_BLOCK_ID: 2579 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 2580 return Result; 2581 break; 2582 2583 case COMMENTS_BLOCK_ID: { 2584 BitstreamCursor C = Stream; 2585 if (Stream.SkipBlock() || 2586 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 2587 Error("malformed comments block in AST file"); 2588 return Failure; 2589 } 2590 CommentsCursors.push_back(std::make_pair(C, &F)); 2591 break; 2592 } 2593 2594 default: 2595 if (Stream.SkipBlock()) { 2596 Error("malformed block record in AST file"); 2597 return Failure; 2598 } 2599 break; 2600 } 2601 continue; 2602 2603 case llvm::BitstreamEntry::Record: 2604 // The interesting case. 2605 break; 2606 } 2607 2608 // Read and process a record. 2609 Record.clear(); 2610 StringRef Blob; 2611 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 2612 default: // Default behavior: ignore. 2613 break; 2614 2615 case TYPE_OFFSET: { 2616 if (F.LocalNumTypes != 0) { 2617 Error("duplicate TYPE_OFFSET record in AST file"); 2618 return Failure; 2619 } 2620 F.TypeOffsets = (const uint32_t *)Blob.data(); 2621 F.LocalNumTypes = Record[0]; 2622 unsigned LocalBaseTypeIndex = Record[1]; 2623 F.BaseTypeIndex = getTotalNumTypes(); 2624 2625 if (F.LocalNumTypes > 0) { 2626 // Introduce the global -> local mapping for types within this module. 2627 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 2628 2629 // Introduce the local -> global mapping for types within this module. 2630 F.TypeRemap.insertOrReplace( 2631 std::make_pair(LocalBaseTypeIndex, 2632 F.BaseTypeIndex - LocalBaseTypeIndex)); 2633 2634 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 2635 } 2636 break; 2637 } 2638 2639 case DECL_OFFSET: { 2640 if (F.LocalNumDecls != 0) { 2641 Error("duplicate DECL_OFFSET record in AST file"); 2642 return Failure; 2643 } 2644 F.DeclOffsets = (const DeclOffset *)Blob.data(); 2645 F.LocalNumDecls = Record[0]; 2646 unsigned LocalBaseDeclID = Record[1]; 2647 F.BaseDeclID = getTotalNumDecls(); 2648 2649 if (F.LocalNumDecls > 0) { 2650 // Introduce the global -> local mapping for declarations within this 2651 // module. 2652 GlobalDeclMap.insert( 2653 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 2654 2655 // Introduce the local -> global mapping for declarations within this 2656 // module. 2657 F.DeclRemap.insertOrReplace( 2658 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 2659 2660 // Introduce the global -> local mapping for declarations within this 2661 // module. 2662 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 2663 2664 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 2665 } 2666 break; 2667 } 2668 2669 case TU_UPDATE_LEXICAL: { 2670 DeclContext *TU = Context.getTranslationUnitDecl(); 2671 DeclContextInfo &Info = F.DeclContextInfos[TU]; 2672 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data()); 2673 Info.NumLexicalDecls 2674 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair)); 2675 TU->setHasExternalLexicalStorage(true); 2676 break; 2677 } 2678 2679 case UPDATE_VISIBLE: { 2680 unsigned Idx = 0; 2681 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 2682 ASTDeclContextNameLookupTable *Table = 2683 ASTDeclContextNameLookupTable::Create( 2684 (const unsigned char *)Blob.data() + Record[Idx++], 2685 (const unsigned char *)Blob.data() + sizeof(uint32_t), 2686 (const unsigned char *)Blob.data(), 2687 ASTDeclContextNameLookupTrait(*this, F)); 2688 if (Decl *D = GetExistingDecl(ID)) { 2689 auto *DC = cast<DeclContext>(D); 2690 DC->getPrimaryContext()->setHasExternalVisibleStorage(true); 2691 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData; 2692 // FIXME: There should never be an existing lookup table. 2693 delete LookupTable; 2694 LookupTable = Table; 2695 } else 2696 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F)); 2697 break; 2698 } 2699 2700 case IDENTIFIER_TABLE: 2701 F.IdentifierTableData = Blob.data(); 2702 if (Record[0]) { 2703 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 2704 (const unsigned char *)F.IdentifierTableData + Record[0], 2705 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 2706 (const unsigned char *)F.IdentifierTableData, 2707 ASTIdentifierLookupTrait(*this, F)); 2708 2709 PP.getIdentifierTable().setExternalIdentifierLookup(this); 2710 } 2711 break; 2712 2713 case IDENTIFIER_OFFSET: { 2714 if (F.LocalNumIdentifiers != 0) { 2715 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2716 return Failure; 2717 } 2718 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 2719 F.LocalNumIdentifiers = Record[0]; 2720 unsigned LocalBaseIdentifierID = Record[1]; 2721 F.BaseIdentifierID = getTotalNumIdentifiers(); 2722 2723 if (F.LocalNumIdentifiers > 0) { 2724 // Introduce the global -> local mapping for identifiers within this 2725 // module. 2726 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 2727 &F)); 2728 2729 // Introduce the local -> global mapping for identifiers within this 2730 // module. 2731 F.IdentifierRemap.insertOrReplace( 2732 std::make_pair(LocalBaseIdentifierID, 2733 F.BaseIdentifierID - LocalBaseIdentifierID)); 2734 2735 IdentifiersLoaded.resize(IdentifiersLoaded.size() 2736 + F.LocalNumIdentifiers); 2737 } 2738 break; 2739 } 2740 2741 case EAGERLY_DESERIALIZED_DECLS: 2742 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2743 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 2744 break; 2745 2746 case SPECIAL_TYPES: 2747 if (SpecialTypes.empty()) { 2748 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2749 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 2750 break; 2751 } 2752 2753 if (SpecialTypes.size() != Record.size()) { 2754 Error("invalid special-types record"); 2755 return Failure; 2756 } 2757 2758 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2759 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 2760 if (!SpecialTypes[I]) 2761 SpecialTypes[I] = ID; 2762 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 2763 // merge step? 2764 } 2765 break; 2766 2767 case STATISTICS: 2768 TotalNumStatements += Record[0]; 2769 TotalNumMacros += Record[1]; 2770 TotalLexicalDeclContexts += Record[2]; 2771 TotalVisibleDeclContexts += Record[3]; 2772 break; 2773 2774 case UNUSED_FILESCOPED_DECLS: 2775 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2776 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 2777 break; 2778 2779 case DELEGATING_CTORS: 2780 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2781 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 2782 break; 2783 2784 case WEAK_UNDECLARED_IDENTIFIERS: 2785 if (Record.size() % 4 != 0) { 2786 Error("invalid weak identifiers record"); 2787 return Failure; 2788 } 2789 2790 // FIXME: Ignore weak undeclared identifiers from non-original PCH 2791 // files. This isn't the way to do it :) 2792 WeakUndeclaredIdentifiers.clear(); 2793 2794 // Translate the weak, undeclared identifiers into global IDs. 2795 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 2796 WeakUndeclaredIdentifiers.push_back( 2797 getGlobalIdentifierID(F, Record[I++])); 2798 WeakUndeclaredIdentifiers.push_back( 2799 getGlobalIdentifierID(F, Record[I++])); 2800 WeakUndeclaredIdentifiers.push_back( 2801 ReadSourceLocation(F, Record, I).getRawEncoding()); 2802 WeakUndeclaredIdentifiers.push_back(Record[I++]); 2803 } 2804 break; 2805 2806 case LOCALLY_SCOPED_EXTERN_C_DECLS: 2807 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2808 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I])); 2809 break; 2810 2811 case SELECTOR_OFFSETS: { 2812 F.SelectorOffsets = (const uint32_t *)Blob.data(); 2813 F.LocalNumSelectors = Record[0]; 2814 unsigned LocalBaseSelectorID = Record[1]; 2815 F.BaseSelectorID = getTotalNumSelectors(); 2816 2817 if (F.LocalNumSelectors > 0) { 2818 // Introduce the global -> local mapping for selectors within this 2819 // module. 2820 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 2821 2822 // Introduce the local -> global mapping for selectors within this 2823 // module. 2824 F.SelectorRemap.insertOrReplace( 2825 std::make_pair(LocalBaseSelectorID, 2826 F.BaseSelectorID - LocalBaseSelectorID)); 2827 2828 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 2829 } 2830 break; 2831 } 2832 2833 case METHOD_POOL: 2834 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 2835 if (Record[0]) 2836 F.SelectorLookupTable 2837 = ASTSelectorLookupTable::Create( 2838 F.SelectorLookupTableData + Record[0], 2839 F.SelectorLookupTableData, 2840 ASTSelectorLookupTrait(*this, F)); 2841 TotalNumMethodPoolEntries += Record[1]; 2842 break; 2843 2844 case REFERENCED_SELECTOR_POOL: 2845 if (!Record.empty()) { 2846 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 2847 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 2848 Record[Idx++])); 2849 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 2850 getRawEncoding()); 2851 } 2852 } 2853 break; 2854 2855 case PP_COUNTER_VALUE: 2856 if (!Record.empty() && Listener) 2857 Listener->ReadCounter(F, Record[0]); 2858 break; 2859 2860 case FILE_SORTED_DECLS: 2861 F.FileSortedDecls = (const DeclID *)Blob.data(); 2862 F.NumFileSortedDecls = Record[0]; 2863 break; 2864 2865 case SOURCE_LOCATION_OFFSETS: { 2866 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 2867 F.LocalNumSLocEntries = Record[0]; 2868 unsigned SLocSpaceSize = Record[1]; 2869 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2870 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2871 SLocSpaceSize); 2872 // Make our entry in the range map. BaseID is negative and growing, so 2873 // we invert it. Because we invert it, though, we need the other end of 2874 // the range. 2875 unsigned RangeStart = 2876 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2877 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2878 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2879 2880 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2881 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2882 GlobalSLocOffsetMap.insert( 2883 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2884 - SLocSpaceSize,&F)); 2885 2886 // Initialize the remapping table. 2887 // Invalid stays invalid. 2888 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 2889 // This module. Base was 2 when being compiled. 2890 F.SLocRemap.insertOrReplace(std::make_pair(2U, 2891 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2892 2893 TotalNumSLocEntries += F.LocalNumSLocEntries; 2894 break; 2895 } 2896 2897 case MODULE_OFFSET_MAP: { 2898 // Additional remapping information. 2899 const unsigned char *Data = (const unsigned char*)Blob.data(); 2900 const unsigned char *DataEnd = Data + Blob.size(); 2901 2902 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 2903 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 2904 F.SLocRemap.insert(std::make_pair(0U, 0)); 2905 F.SLocRemap.insert(std::make_pair(2U, 1)); 2906 } 2907 2908 // Continuous range maps we may be updating in our module. 2909 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap); 2910 ContinuousRangeMap<uint32_t, int, 2>::Builder 2911 IdentifierRemap(F.IdentifierRemap); 2912 ContinuousRangeMap<uint32_t, int, 2>::Builder 2913 MacroRemap(F.MacroRemap); 2914 ContinuousRangeMap<uint32_t, int, 2>::Builder 2915 PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2916 ContinuousRangeMap<uint32_t, int, 2>::Builder 2917 SubmoduleRemap(F.SubmoduleRemap); 2918 ContinuousRangeMap<uint32_t, int, 2>::Builder 2919 SelectorRemap(F.SelectorRemap); 2920 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap); 2921 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap); 2922 2923 while(Data < DataEnd) { 2924 using namespace llvm::support; 2925 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 2926 StringRef Name = StringRef((const char*)Data, Len); 2927 Data += Len; 2928 ModuleFile *OM = ModuleMgr.lookup(Name); 2929 if (!OM) { 2930 Error("SourceLocation remap refers to unknown module"); 2931 return Failure; 2932 } 2933 2934 uint32_t SLocOffset = 2935 endian::readNext<uint32_t, little, unaligned>(Data); 2936 uint32_t IdentifierIDOffset = 2937 endian::readNext<uint32_t, little, unaligned>(Data); 2938 uint32_t MacroIDOffset = 2939 endian::readNext<uint32_t, little, unaligned>(Data); 2940 uint32_t PreprocessedEntityIDOffset = 2941 endian::readNext<uint32_t, little, unaligned>(Data); 2942 uint32_t SubmoduleIDOffset = 2943 endian::readNext<uint32_t, little, unaligned>(Data); 2944 uint32_t SelectorIDOffset = 2945 endian::readNext<uint32_t, little, unaligned>(Data); 2946 uint32_t DeclIDOffset = 2947 endian::readNext<uint32_t, little, unaligned>(Data); 2948 uint32_t TypeIndexOffset = 2949 endian::readNext<uint32_t, little, unaligned>(Data); 2950 2951 // Source location offset is mapped to OM->SLocEntryBaseOffset. 2952 SLocRemap.insert(std::make_pair(SLocOffset, 2953 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset))); 2954 IdentifierRemap.insert( 2955 std::make_pair(IdentifierIDOffset, 2956 OM->BaseIdentifierID - IdentifierIDOffset)); 2957 MacroRemap.insert(std::make_pair(MacroIDOffset, 2958 OM->BaseMacroID - MacroIDOffset)); 2959 PreprocessedEntityRemap.insert( 2960 std::make_pair(PreprocessedEntityIDOffset, 2961 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset)); 2962 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset, 2963 OM->BaseSubmoduleID - SubmoduleIDOffset)); 2964 SelectorRemap.insert(std::make_pair(SelectorIDOffset, 2965 OM->BaseSelectorID - SelectorIDOffset)); 2966 DeclRemap.insert(std::make_pair(DeclIDOffset, 2967 OM->BaseDeclID - DeclIDOffset)); 2968 2969 TypeRemap.insert(std::make_pair(TypeIndexOffset, 2970 OM->BaseTypeIndex - TypeIndexOffset)); 2971 2972 // Global -> local mappings. 2973 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 2974 } 2975 break; 2976 } 2977 2978 case SOURCE_MANAGER_LINE_TABLE: 2979 if (ParseLineTable(F, Record)) 2980 return Failure; 2981 break; 2982 2983 case SOURCE_LOCATION_PRELOADS: { 2984 // Need to transform from the local view (1-based IDs) to the global view, 2985 // which is based off F.SLocEntryBaseID. 2986 if (!F.PreloadSLocEntries.empty()) { 2987 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2988 return Failure; 2989 } 2990 2991 F.PreloadSLocEntries.swap(Record); 2992 break; 2993 } 2994 2995 case EXT_VECTOR_DECLS: 2996 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2997 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2998 break; 2999 3000 case VTABLE_USES: 3001 if (Record.size() % 3 != 0) { 3002 Error("Invalid VTABLE_USES record"); 3003 return Failure; 3004 } 3005 3006 // Later tables overwrite earlier ones. 3007 // FIXME: Modules will have some trouble with this. This is clearly not 3008 // the right way to do this. 3009 VTableUses.clear(); 3010 3011 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3012 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3013 VTableUses.push_back( 3014 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3015 VTableUses.push_back(Record[Idx++]); 3016 } 3017 break; 3018 3019 case DYNAMIC_CLASSES: 3020 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3021 DynamicClasses.push_back(getGlobalDeclID(F, Record[I])); 3022 break; 3023 3024 case PENDING_IMPLICIT_INSTANTIATIONS: 3025 if (PendingInstantiations.size() % 2 != 0) { 3026 Error("Invalid existing PendingInstantiations"); 3027 return Failure; 3028 } 3029 3030 if (Record.size() % 2 != 0) { 3031 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3032 return Failure; 3033 } 3034 3035 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3036 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3037 PendingInstantiations.push_back( 3038 ReadSourceLocation(F, Record, I).getRawEncoding()); 3039 } 3040 break; 3041 3042 case SEMA_DECL_REFS: 3043 if (Record.size() != 2) { 3044 Error("Invalid SEMA_DECL_REFS block"); 3045 return Failure; 3046 } 3047 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3048 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3049 break; 3050 3051 case PPD_ENTITIES_OFFSETS: { 3052 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3053 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3054 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3055 3056 unsigned LocalBasePreprocessedEntityID = Record[0]; 3057 3058 unsigned StartingID; 3059 if (!PP.getPreprocessingRecord()) 3060 PP.createPreprocessingRecord(); 3061 if (!PP.getPreprocessingRecord()->getExternalSource()) 3062 PP.getPreprocessingRecord()->SetExternalSource(*this); 3063 StartingID 3064 = PP.getPreprocessingRecord() 3065 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3066 F.BasePreprocessedEntityID = StartingID; 3067 3068 if (F.NumPreprocessedEntities > 0) { 3069 // Introduce the global -> local mapping for preprocessed entities in 3070 // this module. 3071 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3072 3073 // Introduce the local -> global mapping for preprocessed entities in 3074 // this module. 3075 F.PreprocessedEntityRemap.insertOrReplace( 3076 std::make_pair(LocalBasePreprocessedEntityID, 3077 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3078 } 3079 3080 break; 3081 } 3082 3083 case DECL_UPDATE_OFFSETS: { 3084 if (Record.size() % 2 != 0) { 3085 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3086 return Failure; 3087 } 3088 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3089 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3090 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3091 3092 // If we've already loaded the decl, perform the updates when we finish 3093 // loading this block. 3094 if (Decl *D = GetExistingDecl(ID)) 3095 PendingUpdateRecords.push_back(std::make_pair(ID, D)); 3096 } 3097 break; 3098 } 3099 3100 case DECL_REPLACEMENTS: { 3101 if (Record.size() % 3 != 0) { 3102 Error("invalid DECL_REPLACEMENTS block in AST file"); 3103 return Failure; 3104 } 3105 for (unsigned I = 0, N = Record.size(); I != N; I += 3) 3106 ReplacedDecls[getGlobalDeclID(F, Record[I])] 3107 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]); 3108 break; 3109 } 3110 3111 case OBJC_CATEGORIES_MAP: { 3112 if (F.LocalNumObjCCategoriesInMap != 0) { 3113 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3114 return Failure; 3115 } 3116 3117 F.LocalNumObjCCategoriesInMap = Record[0]; 3118 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3119 break; 3120 } 3121 3122 case OBJC_CATEGORIES: 3123 F.ObjCCategories.swap(Record); 3124 break; 3125 3126 case CXX_BASE_SPECIFIER_OFFSETS: { 3127 if (F.LocalNumCXXBaseSpecifiers != 0) { 3128 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 3129 return Failure; 3130 } 3131 3132 F.LocalNumCXXBaseSpecifiers = Record[0]; 3133 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data(); 3134 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers; 3135 break; 3136 } 3137 3138 case DIAG_PRAGMA_MAPPINGS: 3139 if (F.PragmaDiagMappings.empty()) 3140 F.PragmaDiagMappings.swap(Record); 3141 else 3142 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 3143 Record.begin(), Record.end()); 3144 break; 3145 3146 case CUDA_SPECIAL_DECL_REFS: 3147 // Later tables overwrite earlier ones. 3148 // FIXME: Modules will have trouble with this. 3149 CUDASpecialDeclRefs.clear(); 3150 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3151 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3152 break; 3153 3154 case HEADER_SEARCH_TABLE: { 3155 F.HeaderFileInfoTableData = Blob.data(); 3156 F.LocalNumHeaderFileInfos = Record[1]; 3157 if (Record[0]) { 3158 F.HeaderFileInfoTable 3159 = HeaderFileInfoLookupTable::Create( 3160 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3161 (const unsigned char *)F.HeaderFileInfoTableData, 3162 HeaderFileInfoTrait(*this, F, 3163 &PP.getHeaderSearchInfo(), 3164 Blob.data() + Record[2])); 3165 3166 PP.getHeaderSearchInfo().SetExternalSource(this); 3167 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3168 PP.getHeaderSearchInfo().SetExternalLookup(this); 3169 } 3170 break; 3171 } 3172 3173 case FP_PRAGMA_OPTIONS: 3174 // Later tables overwrite earlier ones. 3175 FPPragmaOptions.swap(Record); 3176 break; 3177 3178 case OPENCL_EXTENSIONS: 3179 // Later tables overwrite earlier ones. 3180 OpenCLExtensions.swap(Record); 3181 break; 3182 3183 case TENTATIVE_DEFINITIONS: 3184 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3185 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3186 break; 3187 3188 case KNOWN_NAMESPACES: 3189 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3190 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3191 break; 3192 3193 case UNDEFINED_BUT_USED: 3194 if (UndefinedButUsed.size() % 2 != 0) { 3195 Error("Invalid existing UndefinedButUsed"); 3196 return Failure; 3197 } 3198 3199 if (Record.size() % 2 != 0) { 3200 Error("invalid undefined-but-used record"); 3201 return Failure; 3202 } 3203 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3204 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3205 UndefinedButUsed.push_back( 3206 ReadSourceLocation(F, Record, I).getRawEncoding()); 3207 } 3208 break; 3209 3210 case IMPORTED_MODULES: { 3211 if (F.Kind != MK_Module) { 3212 // If we aren't loading a module (which has its own exports), make 3213 // all of the imported modules visible. 3214 // FIXME: Deal with macros-only imports. 3215 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3216 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3217 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3218 if (GlobalID) 3219 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3220 } 3221 } 3222 break; 3223 } 3224 3225 case LOCAL_REDECLARATIONS: { 3226 F.RedeclarationChains.swap(Record); 3227 break; 3228 } 3229 3230 case LOCAL_REDECLARATIONS_MAP: { 3231 if (F.LocalNumRedeclarationsInMap != 0) { 3232 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file"); 3233 return Failure; 3234 } 3235 3236 F.LocalNumRedeclarationsInMap = Record[0]; 3237 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data(); 3238 break; 3239 } 3240 3241 case MERGED_DECLARATIONS: { 3242 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) { 3243 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]); 3244 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID]; 3245 for (unsigned N = Record[Idx++]; N > 0; --N) 3246 Decls.push_back(getGlobalDeclID(F, Record[Idx++])); 3247 } 3248 break; 3249 } 3250 3251 case MACRO_OFFSET: { 3252 if (F.LocalNumMacros != 0) { 3253 Error("duplicate MACRO_OFFSET record in AST file"); 3254 return Failure; 3255 } 3256 F.MacroOffsets = (const uint32_t *)Blob.data(); 3257 F.LocalNumMacros = Record[0]; 3258 unsigned LocalBaseMacroID = Record[1]; 3259 F.BaseMacroID = getTotalNumMacros(); 3260 3261 if (F.LocalNumMacros > 0) { 3262 // Introduce the global -> local mapping for macros within this module. 3263 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3264 3265 // Introduce the local -> global mapping for macros within this module. 3266 F.MacroRemap.insertOrReplace( 3267 std::make_pair(LocalBaseMacroID, 3268 F.BaseMacroID - LocalBaseMacroID)); 3269 3270 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3271 } 3272 break; 3273 } 3274 3275 case MACRO_TABLE: { 3276 // FIXME: Not used yet. 3277 break; 3278 } 3279 3280 case LATE_PARSED_TEMPLATE: { 3281 LateParsedTemplates.append(Record.begin(), Record.end()); 3282 break; 3283 } 3284 3285 case OPTIMIZE_PRAGMA_OPTIONS: 3286 if (Record.size() != 1) { 3287 Error("invalid pragma optimize record"); 3288 return Failure; 3289 } 3290 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3291 break; 3292 } 3293 } 3294} 3295 3296/// \brief Move the given method to the back of the global list of methods. 3297static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 3298 // Find the entry for this selector in the method pool. 3299 Sema::GlobalMethodPool::iterator Known 3300 = S.MethodPool.find(Method->getSelector()); 3301 if (Known == S.MethodPool.end()) 3302 return; 3303 3304 // Retrieve the appropriate method list. 3305 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 3306 : Known->second.second; 3307 bool Found = false; 3308 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 3309 if (!Found) { 3310 if (List->Method == Method) { 3311 Found = true; 3312 } else { 3313 // Keep searching. 3314 continue; 3315 } 3316 } 3317 3318 if (List->getNext()) 3319 List->Method = List->getNext()->Method; 3320 else 3321 List->Method = Method; 3322 } 3323} 3324 3325void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 3326 for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) { 3327 Decl *D = Names.HiddenDecls[I]; 3328 bool wasHidden = D->Hidden; 3329 D->Hidden = false; 3330 3331 if (wasHidden && SemaObj) { 3332 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 3333 moveMethodToBackOfGlobalList(*SemaObj, Method); 3334 } 3335 } 3336 } 3337 3338 for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(), 3339 E = Names.HiddenMacros.end(); 3340 I != E; ++I) 3341 installImportedMacro(I->first, I->second, Owner); 3342} 3343 3344void ASTReader::makeModuleVisible(Module *Mod, 3345 Module::NameVisibilityKind NameVisibility, 3346 SourceLocation ImportLoc, 3347 bool Complain) { 3348 llvm::SmallPtrSet<Module *, 4> Visited; 3349 SmallVector<Module *, 4> Stack; 3350 Stack.push_back(Mod); 3351 while (!Stack.empty()) { 3352 Mod = Stack.pop_back_val(); 3353 3354 if (NameVisibility <= Mod->NameVisibility) { 3355 // This module already has this level of visibility (or greater), so 3356 // there is nothing more to do. 3357 continue; 3358 } 3359 3360 if (!Mod->isAvailable()) { 3361 // Modules that aren't available cannot be made visible. 3362 continue; 3363 } 3364 3365 // Update the module's name visibility. 3366 if (NameVisibility >= Module::MacrosVisible && 3367 Mod->NameVisibility < Module::MacrosVisible) 3368 Mod->MacroVisibilityLoc = ImportLoc; 3369 Mod->NameVisibility = NameVisibility; 3370 3371 // If we've already deserialized any names from this module, 3372 // mark them as visible. 3373 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 3374 if (Hidden != HiddenNamesMap.end()) { 3375 makeNamesVisible(Hidden->second, Hidden->first); 3376 HiddenNamesMap.erase(Hidden); 3377 } 3378 3379 // Push any exported modules onto the stack to be marked as visible. 3380 SmallVector<Module *, 16> Exports; 3381 Mod->getExportedModules(Exports); 3382 for (SmallVectorImpl<Module *>::iterator 3383 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 3384 Module *Exported = *I; 3385 if (Visited.insert(Exported)) 3386 Stack.push_back(Exported); 3387 } 3388 3389 // Detect any conflicts. 3390 if (Complain) { 3391 assert(ImportLoc.isValid() && "Missing import location"); 3392 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) { 3393 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) { 3394 Diag(ImportLoc, diag::warn_module_conflict) 3395 << Mod->getFullModuleName() 3396 << Mod->Conflicts[I].Other->getFullModuleName() 3397 << Mod->Conflicts[I].Message; 3398 // FIXME: Need note where the other module was imported. 3399 } 3400 } 3401 } 3402 } 3403} 3404 3405bool ASTReader::loadGlobalIndex() { 3406 if (GlobalIndex) 3407 return false; 3408 3409 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 3410 !Context.getLangOpts().Modules) 3411 return true; 3412 3413 // Try to load the global index. 3414 TriedLoadingGlobalIndex = true; 3415 StringRef ModuleCachePath 3416 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 3417 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result 3418 = GlobalModuleIndex::readIndex(ModuleCachePath); 3419 if (!Result.first) 3420 return true; 3421 3422 GlobalIndex.reset(Result.first); 3423 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 3424 return false; 3425} 3426 3427bool ASTReader::isGlobalIndexUnavailable() const { 3428 return Context.getLangOpts().Modules && UseGlobalIndex && 3429 !hasGlobalIndex() && TriedLoadingGlobalIndex; 3430} 3431 3432static void updateModuleTimestamp(ModuleFile &MF) { 3433 // Overwrite the timestamp file contents so that file's mtime changes. 3434 std::string TimestampFilename = MF.getTimestampFilename(); 3435 std::string ErrorInfo; 3436 llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo, 3437 llvm::sys::fs::F_Text); 3438 if (!ErrorInfo.empty()) 3439 return; 3440 OS << "Timestamp file\n"; 3441} 3442 3443ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 3444 ModuleKind Type, 3445 SourceLocation ImportLoc, 3446 unsigned ClientLoadCapabilities) { 3447 llvm::SaveAndRestore<SourceLocation> 3448 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 3449 3450 // Defer any pending actions until we get to the end of reading the AST file. 3451 Deserializing AnASTFile(this); 3452 3453 // Bump the generation number. 3454 unsigned PreviousGeneration = incrementGeneration(Context); 3455 3456 unsigned NumModules = ModuleMgr.size(); 3457 SmallVector<ImportedModule, 4> Loaded; 3458 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc, 3459 /*ImportedBy=*/nullptr, Loaded, 3460 0, 0, 3461 ClientLoadCapabilities)) { 3462 case Failure: 3463 case Missing: 3464 case OutOfDate: 3465 case VersionMismatch: 3466 case ConfigurationMismatch: 3467 case HadErrors: 3468 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(), 3469 Context.getLangOpts().Modules 3470 ? &PP.getHeaderSearchInfo().getModuleMap() 3471 : nullptr); 3472 3473 // If we find that any modules are unusable, the global index is going 3474 // to be out-of-date. Just remove it. 3475 GlobalIndex.reset(); 3476 ModuleMgr.setGlobalIndex(nullptr); 3477 return ReadResult; 3478 3479 case Success: 3480 break; 3481 } 3482 3483 // Here comes stuff that we only do once the entire chain is loaded. 3484 3485 // Load the AST blocks of all of the modules that we loaded. 3486 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 3487 MEnd = Loaded.end(); 3488 M != MEnd; ++M) { 3489 ModuleFile &F = *M->Mod; 3490 3491 // Read the AST block. 3492 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 3493 return Result; 3494 3495 // Once read, set the ModuleFile bit base offset and update the size in 3496 // bits of all files we've seen. 3497 F.GlobalBitOffset = TotalModulesSizeInBits; 3498 TotalModulesSizeInBits += F.SizeInBits; 3499 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 3500 3501 // Preload SLocEntries. 3502 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 3503 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 3504 // Load it through the SourceManager and don't call ReadSLocEntry() 3505 // directly because the entry may have already been loaded in which case 3506 // calling ReadSLocEntry() directly would trigger an assertion in 3507 // SourceManager. 3508 SourceMgr.getLoadedSLocEntryByID(Index); 3509 } 3510 } 3511 3512 // Setup the import locations and notify the module manager that we've 3513 // committed to these module files. 3514 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 3515 MEnd = Loaded.end(); 3516 M != MEnd; ++M) { 3517 ModuleFile &F = *M->Mod; 3518 3519 ModuleMgr.moduleFileAccepted(&F); 3520 3521 // Set the import location. 3522 F.DirectImportLoc = ImportLoc; 3523 if (!M->ImportedBy) 3524 F.ImportLoc = M->ImportLoc; 3525 else 3526 F.ImportLoc = ReadSourceLocation(*M->ImportedBy, 3527 M->ImportLoc.getRawEncoding()); 3528 } 3529 3530 // Mark all of the identifiers in the identifier table as being out of date, 3531 // so that various accessors know to check the loaded modules when the 3532 // identifier is used. 3533 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 3534 IdEnd = PP.getIdentifierTable().end(); 3535 Id != IdEnd; ++Id) 3536 Id->second->setOutOfDate(true); 3537 3538 // Resolve any unresolved module exports. 3539 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 3540 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 3541 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 3542 Module *ResolvedMod = getSubmodule(GlobalID); 3543 3544 switch (Unresolved.Kind) { 3545 case UnresolvedModuleRef::Conflict: 3546 if (ResolvedMod) { 3547 Module::Conflict Conflict; 3548 Conflict.Other = ResolvedMod; 3549 Conflict.Message = Unresolved.String.str(); 3550 Unresolved.Mod->Conflicts.push_back(Conflict); 3551 } 3552 continue; 3553 3554 case UnresolvedModuleRef::Import: 3555 if (ResolvedMod) 3556 Unresolved.Mod->Imports.push_back(ResolvedMod); 3557 continue; 3558 3559 case UnresolvedModuleRef::Export: 3560 if (ResolvedMod || Unresolved.IsWildcard) 3561 Unresolved.Mod->Exports.push_back( 3562 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 3563 continue; 3564 } 3565 } 3566 UnresolvedModuleRefs.clear(); 3567 3568 // FIXME: How do we load the 'use'd modules? They may not be submodules. 3569 // Might be unnecessary as use declarations are only used to build the 3570 // module itself. 3571 3572 InitializeContext(); 3573 3574 if (SemaObj) 3575 UpdateSema(); 3576 3577 if (DeserializationListener) 3578 DeserializationListener->ReaderInitialized(this); 3579 3580 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 3581 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) { 3582 PrimaryModule.OriginalSourceFileID 3583 = FileID::get(PrimaryModule.SLocEntryBaseID 3584 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1); 3585 3586 // If this AST file is a precompiled preamble, then set the 3587 // preamble file ID of the source manager to the file source file 3588 // from which the preamble was built. 3589 if (Type == MK_Preamble) { 3590 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 3591 } else if (Type == MK_MainFile) { 3592 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 3593 } 3594 } 3595 3596 // For any Objective-C class definitions we have already loaded, make sure 3597 // that we load any additional categories. 3598 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 3599 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 3600 ObjCClassesLoaded[I], 3601 PreviousGeneration); 3602 } 3603 3604 if (PP.getHeaderSearchInfo() 3605 .getHeaderSearchOpts() 3606 .ModulesValidateOncePerBuildSession) { 3607 // Now we are certain that the module and all modules it depends on are 3608 // up to date. Create or update timestamp files for modules that are 3609 // located in the module cache (not for PCH files that could be anywhere 3610 // in the filesystem). 3611 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 3612 ImportedModule &M = Loaded[I]; 3613 if (M.Mod->Kind == MK_Module) { 3614 updateModuleTimestamp(*M.Mod); 3615 } 3616 } 3617 } 3618 3619 return Success; 3620} 3621 3622ASTReader::ASTReadResult 3623ASTReader::ReadASTCore(StringRef FileName, 3624 ModuleKind Type, 3625 SourceLocation ImportLoc, 3626 ModuleFile *ImportedBy, 3627 SmallVectorImpl<ImportedModule> &Loaded, 3628 off_t ExpectedSize, time_t ExpectedModTime, 3629 unsigned ClientLoadCapabilities) { 3630 ModuleFile *M; 3631 std::string ErrorStr; 3632 ModuleManager::AddModuleResult AddResult 3633 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 3634 getGeneration(), ExpectedSize, ExpectedModTime, 3635 M, ErrorStr); 3636 3637 switch (AddResult) { 3638 case ModuleManager::AlreadyLoaded: 3639 return Success; 3640 3641 case ModuleManager::NewlyLoaded: 3642 // Load module file below. 3643 break; 3644 3645 case ModuleManager::Missing: 3646 // The module file was missing; if the client handle handle, that, return 3647 // it. 3648 if (ClientLoadCapabilities & ARR_Missing) 3649 return Missing; 3650 3651 // Otherwise, return an error. 3652 { 3653 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 3654 + ErrorStr; 3655 Error(Msg); 3656 } 3657 return Failure; 3658 3659 case ModuleManager::OutOfDate: 3660 // We couldn't load the module file because it is out-of-date. If the 3661 // client can handle out-of-date, return it. 3662 if (ClientLoadCapabilities & ARR_OutOfDate) 3663 return OutOfDate; 3664 3665 // Otherwise, return an error. 3666 { 3667 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 3668 + ErrorStr; 3669 Error(Msg); 3670 } 3671 return Failure; 3672 } 3673 3674 assert(M && "Missing module file"); 3675 3676 // FIXME: This seems rather a hack. Should CurrentDir be part of the 3677 // module? 3678 if (FileName != "-") { 3679 CurrentDir = llvm::sys::path::parent_path(FileName); 3680 if (CurrentDir.empty()) CurrentDir = "."; 3681 } 3682 3683 ModuleFile &F = *M; 3684 BitstreamCursor &Stream = F.Stream; 3685 Stream.init(F.StreamFile); 3686 F.SizeInBits = F.Buffer->getBufferSize() * 8; 3687 3688 // Sniff for the signature. 3689 if (Stream.Read(8) != 'C' || 3690 Stream.Read(8) != 'P' || 3691 Stream.Read(8) != 'C' || 3692 Stream.Read(8) != 'H') { 3693 Diag(diag::err_not_a_pch_file) << FileName; 3694 return Failure; 3695 } 3696 3697 // This is used for compatibility with older PCH formats. 3698 bool HaveReadControlBlock = false; 3699 3700 while (1) { 3701 llvm::BitstreamEntry Entry = Stream.advance(); 3702 3703 switch (Entry.Kind) { 3704 case llvm::BitstreamEntry::Error: 3705 case llvm::BitstreamEntry::EndBlock: 3706 case llvm::BitstreamEntry::Record: 3707 Error("invalid record at top-level of AST file"); 3708 return Failure; 3709 3710 case llvm::BitstreamEntry::SubBlock: 3711 break; 3712 } 3713 3714 // We only know the control subblock ID. 3715 switch (Entry.ID) { 3716 case llvm::bitc::BLOCKINFO_BLOCK_ID: 3717 if (Stream.ReadBlockInfoBlock()) { 3718 Error("malformed BlockInfoBlock in AST file"); 3719 return Failure; 3720 } 3721 break; 3722 case CONTROL_BLOCK_ID: 3723 HaveReadControlBlock = true; 3724 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 3725 case Success: 3726 break; 3727 3728 case Failure: return Failure; 3729 case Missing: return Missing; 3730 case OutOfDate: return OutOfDate; 3731 case VersionMismatch: return VersionMismatch; 3732 case ConfigurationMismatch: return ConfigurationMismatch; 3733 case HadErrors: return HadErrors; 3734 } 3735 break; 3736 case AST_BLOCK_ID: 3737 if (!HaveReadControlBlock) { 3738 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 3739 Diag(diag::err_pch_version_too_old); 3740 return VersionMismatch; 3741 } 3742 3743 // Record that we've loaded this module. 3744 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 3745 return Success; 3746 3747 default: 3748 if (Stream.SkipBlock()) { 3749 Error("malformed block record in AST file"); 3750 return Failure; 3751 } 3752 break; 3753 } 3754 } 3755 3756 return Success; 3757} 3758 3759void ASTReader::InitializeContext() { 3760 // If there's a listener, notify them that we "read" the translation unit. 3761 if (DeserializationListener) 3762 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 3763 Context.getTranslationUnitDecl()); 3764 3765 // FIXME: Find a better way to deal with collisions between these 3766 // built-in types. Right now, we just ignore the problem. 3767 3768 // Load the special types. 3769 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 3770 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 3771 if (!Context.CFConstantStringTypeDecl) 3772 Context.setCFConstantStringType(GetType(String)); 3773 } 3774 3775 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 3776 QualType FileType = GetType(File); 3777 if (FileType.isNull()) { 3778 Error("FILE type is NULL"); 3779 return; 3780 } 3781 3782 if (!Context.FILEDecl) { 3783 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 3784 Context.setFILEDecl(Typedef->getDecl()); 3785 else { 3786 const TagType *Tag = FileType->getAs<TagType>(); 3787 if (!Tag) { 3788 Error("Invalid FILE type in AST file"); 3789 return; 3790 } 3791 Context.setFILEDecl(Tag->getDecl()); 3792 } 3793 } 3794 } 3795 3796 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 3797 QualType Jmp_bufType = GetType(Jmp_buf); 3798 if (Jmp_bufType.isNull()) { 3799 Error("jmp_buf type is NULL"); 3800 return; 3801 } 3802 3803 if (!Context.jmp_bufDecl) { 3804 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 3805 Context.setjmp_bufDecl(Typedef->getDecl()); 3806 else { 3807 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 3808 if (!Tag) { 3809 Error("Invalid jmp_buf type in AST file"); 3810 return; 3811 } 3812 Context.setjmp_bufDecl(Tag->getDecl()); 3813 } 3814 } 3815 } 3816 3817 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 3818 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 3819 if (Sigjmp_bufType.isNull()) { 3820 Error("sigjmp_buf type is NULL"); 3821 return; 3822 } 3823 3824 if (!Context.sigjmp_bufDecl) { 3825 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 3826 Context.setsigjmp_bufDecl(Typedef->getDecl()); 3827 else { 3828 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 3829 assert(Tag && "Invalid sigjmp_buf type in AST file"); 3830 Context.setsigjmp_bufDecl(Tag->getDecl()); 3831 } 3832 } 3833 } 3834 3835 if (unsigned ObjCIdRedef 3836 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 3837 if (Context.ObjCIdRedefinitionType.isNull()) 3838 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 3839 } 3840 3841 if (unsigned ObjCClassRedef 3842 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 3843 if (Context.ObjCClassRedefinitionType.isNull()) 3844 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 3845 } 3846 3847 if (unsigned ObjCSelRedef 3848 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 3849 if (Context.ObjCSelRedefinitionType.isNull()) 3850 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 3851 } 3852 3853 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 3854 QualType Ucontext_tType = GetType(Ucontext_t); 3855 if (Ucontext_tType.isNull()) { 3856 Error("ucontext_t type is NULL"); 3857 return; 3858 } 3859 3860 if (!Context.ucontext_tDecl) { 3861 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 3862 Context.setucontext_tDecl(Typedef->getDecl()); 3863 else { 3864 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 3865 assert(Tag && "Invalid ucontext_t type in AST file"); 3866 Context.setucontext_tDecl(Tag->getDecl()); 3867 } 3868 } 3869 } 3870 } 3871 3872 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 3873 3874 // If there were any CUDA special declarations, deserialize them. 3875 if (!CUDASpecialDeclRefs.empty()) { 3876 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 3877 Context.setcudaConfigureCallDecl( 3878 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 3879 } 3880 3881 // Re-export any modules that were imported by a non-module AST file. 3882 // FIXME: This does not make macro-only imports visible again. It also doesn't 3883 // make #includes mapped to module imports visible. 3884 for (auto &Import : ImportedModules) { 3885 if (Module *Imported = getSubmodule(Import.ID)) 3886 makeModuleVisible(Imported, Module::AllVisible, 3887 /*ImportLoc=*/Import.ImportLoc, 3888 /*Complain=*/false); 3889 } 3890 ImportedModules.clear(); 3891} 3892 3893void ASTReader::finalizeForWriting() { 3894 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(), 3895 HiddenEnd = HiddenNamesMap.end(); 3896 Hidden != HiddenEnd; ++Hidden) { 3897 makeNamesVisible(Hidden->second, Hidden->first); 3898 } 3899 HiddenNamesMap.clear(); 3900} 3901 3902/// \brief Given a cursor at the start of an AST file, scan ahead and drop the 3903/// cursor into the start of the given block ID, returning false on success and 3904/// true on failure. 3905static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 3906 while (1) { 3907 llvm::BitstreamEntry Entry = Cursor.advance(); 3908 switch (Entry.Kind) { 3909 case llvm::BitstreamEntry::Error: 3910 case llvm::BitstreamEntry::EndBlock: 3911 return true; 3912 3913 case llvm::BitstreamEntry::Record: 3914 // Ignore top-level records. 3915 Cursor.skipRecord(Entry.ID); 3916 break; 3917 3918 case llvm::BitstreamEntry::SubBlock: 3919 if (Entry.ID == BlockID) { 3920 if (Cursor.EnterSubBlock(BlockID)) 3921 return true; 3922 // Found it! 3923 return false; 3924 } 3925 3926 if (Cursor.SkipBlock()) 3927 return true; 3928 } 3929 } 3930} 3931 3932/// \brief Retrieve the name of the original source file name 3933/// directly from the AST file, without actually loading the AST 3934/// file. 3935std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 3936 FileManager &FileMgr, 3937 DiagnosticsEngine &Diags) { 3938 // Open the AST file. 3939 std::string ErrStr; 3940 std::unique_ptr<llvm::MemoryBuffer> Buffer; 3941 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 3942 if (!Buffer) { 3943 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; 3944 return std::string(); 3945 } 3946 3947 // Initialize the stream 3948 llvm::BitstreamReader StreamFile; 3949 BitstreamCursor Stream; 3950 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3951 (const unsigned char *)Buffer->getBufferEnd()); 3952 Stream.init(StreamFile); 3953 3954 // Sniff for the signature. 3955 if (Stream.Read(8) != 'C' || 3956 Stream.Read(8) != 'P' || 3957 Stream.Read(8) != 'C' || 3958 Stream.Read(8) != 'H') { 3959 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 3960 return std::string(); 3961 } 3962 3963 // Scan for the CONTROL_BLOCK_ID block. 3964 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 3965 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3966 return std::string(); 3967 } 3968 3969 // Scan for ORIGINAL_FILE inside the control block. 3970 RecordData Record; 3971 while (1) { 3972 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 3973 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3974 return std::string(); 3975 3976 if (Entry.Kind != llvm::BitstreamEntry::Record) { 3977 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3978 return std::string(); 3979 } 3980 3981 Record.clear(); 3982 StringRef Blob; 3983 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE) 3984 return Blob.str(); 3985 } 3986} 3987 3988namespace { 3989 class SimplePCHValidator : public ASTReaderListener { 3990 const LangOptions &ExistingLangOpts; 3991 const TargetOptions &ExistingTargetOpts; 3992 const PreprocessorOptions &ExistingPPOpts; 3993 FileManager &FileMgr; 3994 3995 public: 3996 SimplePCHValidator(const LangOptions &ExistingLangOpts, 3997 const TargetOptions &ExistingTargetOpts, 3998 const PreprocessorOptions &ExistingPPOpts, 3999 FileManager &FileMgr) 4000 : ExistingLangOpts(ExistingLangOpts), 4001 ExistingTargetOpts(ExistingTargetOpts), 4002 ExistingPPOpts(ExistingPPOpts), 4003 FileMgr(FileMgr) 4004 { 4005 } 4006 4007 bool ReadLanguageOptions(const LangOptions &LangOpts, 4008 bool Complain) override { 4009 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr); 4010 } 4011 bool ReadTargetOptions(const TargetOptions &TargetOpts, 4012 bool Complain) override { 4013 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr); 4014 } 4015 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 4016 bool Complain, 4017 std::string &SuggestedPredefines) override { 4018 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 4019 SuggestedPredefines, ExistingLangOpts); 4020 } 4021 }; 4022} 4023 4024bool ASTReader::readASTFileControlBlock(StringRef Filename, 4025 FileManager &FileMgr, 4026 ASTReaderListener &Listener) { 4027 // Open the AST file. 4028 std::string ErrStr; 4029 std::unique_ptr<llvm::MemoryBuffer> Buffer; 4030 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr)); 4031 if (!Buffer) { 4032 return true; 4033 } 4034 4035 // Initialize the stream 4036 llvm::BitstreamReader StreamFile; 4037 BitstreamCursor Stream; 4038 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 4039 (const unsigned char *)Buffer->getBufferEnd()); 4040 Stream.init(StreamFile); 4041 4042 // Sniff for the signature. 4043 if (Stream.Read(8) != 'C' || 4044 Stream.Read(8) != 'P' || 4045 Stream.Read(8) != 'C' || 4046 Stream.Read(8) != 'H') { 4047 return true; 4048 } 4049 4050 // Scan for the CONTROL_BLOCK_ID block. 4051 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 4052 return true; 4053 4054 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 4055 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 4056 BitstreamCursor InputFilesCursor; 4057 if (NeedsInputFiles) { 4058 InputFilesCursor = Stream; 4059 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 4060 return true; 4061 4062 // Read the abbreviations 4063 while (true) { 4064 uint64_t Offset = InputFilesCursor.GetCurrentBitNo(); 4065 unsigned Code = InputFilesCursor.ReadCode(); 4066 4067 // We expect all abbrevs to be at the start of the block. 4068 if (Code != llvm::bitc::DEFINE_ABBREV) { 4069 InputFilesCursor.JumpToBit(Offset); 4070 break; 4071 } 4072 InputFilesCursor.ReadAbbrevRecord(); 4073 } 4074 } 4075 4076 // Scan for ORIGINAL_FILE inside the control block. 4077 RecordData Record; 4078 while (1) { 4079 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4080 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 4081 return false; 4082 4083 if (Entry.Kind != llvm::BitstreamEntry::Record) 4084 return true; 4085 4086 Record.clear(); 4087 StringRef Blob; 4088 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 4089 switch ((ControlRecordTypes)RecCode) { 4090 case METADATA: { 4091 if (Record[0] != VERSION_MAJOR) 4092 return true; 4093 4094 if (Listener.ReadFullVersionInformation(Blob)) 4095 return true; 4096 4097 break; 4098 } 4099 case MODULE_NAME: 4100 Listener.ReadModuleName(Blob); 4101 break; 4102 case MODULE_MAP_FILE: 4103 Listener.ReadModuleMapFile(Blob); 4104 break; 4105 case LANGUAGE_OPTIONS: 4106 if (ParseLanguageOptions(Record, false, Listener)) 4107 return true; 4108 break; 4109 4110 case TARGET_OPTIONS: 4111 if (ParseTargetOptions(Record, false, Listener)) 4112 return true; 4113 break; 4114 4115 case DIAGNOSTIC_OPTIONS: 4116 if (ParseDiagnosticOptions(Record, false, Listener)) 4117 return true; 4118 break; 4119 4120 case FILE_SYSTEM_OPTIONS: 4121 if (ParseFileSystemOptions(Record, false, Listener)) 4122 return true; 4123 break; 4124 4125 case HEADER_SEARCH_OPTIONS: 4126 if (ParseHeaderSearchOptions(Record, false, Listener)) 4127 return true; 4128 break; 4129 4130 case PREPROCESSOR_OPTIONS: { 4131 std::string IgnoredSuggestedPredefines; 4132 if (ParsePreprocessorOptions(Record, false, Listener, 4133 IgnoredSuggestedPredefines)) 4134 return true; 4135 break; 4136 } 4137 4138 case INPUT_FILE_OFFSETS: { 4139 if (!NeedsInputFiles) 4140 break; 4141 4142 unsigned NumInputFiles = Record[0]; 4143 unsigned NumUserFiles = Record[1]; 4144 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data(); 4145 for (unsigned I = 0; I != NumInputFiles; ++I) { 4146 // Go find this input file. 4147 bool isSystemFile = I >= NumUserFiles; 4148 4149 if (isSystemFile && !NeedsSystemInputFiles) 4150 break; // the rest are system input files 4151 4152 BitstreamCursor &Cursor = InputFilesCursor; 4153 SavedStreamPosition SavedPosition(Cursor); 4154 Cursor.JumpToBit(InputFileOffs[I]); 4155 4156 unsigned Code = Cursor.ReadCode(); 4157 RecordData Record; 4158 StringRef Blob; 4159 bool shouldContinue = false; 4160 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) { 4161 case INPUT_FILE: 4162 bool Overridden = static_cast<bool>(Record[3]); 4163 shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden); 4164 break; 4165 } 4166 if (!shouldContinue) 4167 break; 4168 } 4169 break; 4170 } 4171 4172 default: 4173 // No other validation to perform. 4174 break; 4175 } 4176 } 4177} 4178 4179 4180bool ASTReader::isAcceptableASTFile(StringRef Filename, 4181 FileManager &FileMgr, 4182 const LangOptions &LangOpts, 4183 const TargetOptions &TargetOpts, 4184 const PreprocessorOptions &PPOpts) { 4185 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr); 4186 return !readASTFileControlBlock(Filename, FileMgr, validator); 4187} 4188 4189ASTReader::ASTReadResult 4190ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 4191 // Enter the submodule block. 4192 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 4193 Error("malformed submodule block record in AST file"); 4194 return Failure; 4195 } 4196 4197 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 4198 bool First = true; 4199 Module *CurrentModule = nullptr; 4200 RecordData Record; 4201 while (true) { 4202 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks(); 4203 4204 switch (Entry.Kind) { 4205 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 4206 case llvm::BitstreamEntry::Error: 4207 Error("malformed block record in AST file"); 4208 return Failure; 4209 case llvm::BitstreamEntry::EndBlock: 4210 return Success; 4211 case llvm::BitstreamEntry::Record: 4212 // The interesting case. 4213 break; 4214 } 4215 4216 // Read a record. 4217 StringRef Blob; 4218 Record.clear(); 4219 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) { 4220 default: // Default behavior: ignore. 4221 break; 4222 4223 case SUBMODULE_DEFINITION: { 4224 if (First) { 4225 Error("missing submodule metadata record at beginning of block"); 4226 return Failure; 4227 } 4228 4229 if (Record.size() < 8) { 4230 Error("malformed module definition"); 4231 return Failure; 4232 } 4233 4234 StringRef Name = Blob; 4235 unsigned Idx = 0; 4236 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 4237 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 4238 bool IsFramework = Record[Idx++]; 4239 bool IsExplicit = Record[Idx++]; 4240 bool IsSystem = Record[Idx++]; 4241 bool IsExternC = Record[Idx++]; 4242 bool InferSubmodules = Record[Idx++]; 4243 bool InferExplicitSubmodules = Record[Idx++]; 4244 bool InferExportWildcard = Record[Idx++]; 4245 bool ConfigMacrosExhaustive = Record[Idx++]; 4246 4247 Module *ParentModule = nullptr; 4248 const FileEntry *ModuleMap = nullptr; 4249 if (Parent) { 4250 ParentModule = getSubmodule(Parent); 4251 ModuleMap = ParentModule->ModuleMap; 4252 } 4253 4254 if (!F.ModuleMapPath.empty()) 4255 ModuleMap = FileMgr.getFile(F.ModuleMapPath); 4256 4257 // Retrieve this (sub)module from the module map, creating it if 4258 // necessary. 4259 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, ModuleMap, 4260 IsFramework, 4261 IsExplicit).first; 4262 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 4263 if (GlobalIndex >= SubmodulesLoaded.size() || 4264 SubmodulesLoaded[GlobalIndex]) { 4265 Error("too many submodules"); 4266 return Failure; 4267 } 4268 4269 if (!ParentModule) { 4270 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 4271 if (CurFile != F.File) { 4272 if (!Diags.isDiagnosticInFlight()) { 4273 Diag(diag::err_module_file_conflict) 4274 << CurrentModule->getTopLevelModuleName() 4275 << CurFile->getName() 4276 << F.File->getName(); 4277 } 4278 return Failure; 4279 } 4280 } 4281 4282 CurrentModule->setASTFile(F.File); 4283 } 4284 4285 CurrentModule->IsFromModuleFile = true; 4286 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 4287 CurrentModule->IsExternC = IsExternC; 4288 CurrentModule->InferSubmodules = InferSubmodules; 4289 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 4290 CurrentModule->InferExportWildcard = InferExportWildcard; 4291 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 4292 if (DeserializationListener) 4293 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 4294 4295 SubmodulesLoaded[GlobalIndex] = CurrentModule; 4296 4297 // Clear out data that will be replaced by what is the module file. 4298 CurrentModule->LinkLibraries.clear(); 4299 CurrentModule->ConfigMacros.clear(); 4300 CurrentModule->UnresolvedConflicts.clear(); 4301 CurrentModule->Conflicts.clear(); 4302 break; 4303 } 4304 4305 case SUBMODULE_UMBRELLA_HEADER: { 4306 if (First) { 4307 Error("missing submodule metadata record at beginning of block"); 4308 return Failure; 4309 } 4310 4311 if (!CurrentModule) 4312 break; 4313 4314 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) { 4315 if (!CurrentModule->getUmbrellaHeader()) 4316 ModMap.setUmbrellaHeader(CurrentModule, Umbrella); 4317 else if (CurrentModule->getUmbrellaHeader() != Umbrella) { 4318 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 4319 Error("mismatched umbrella headers in submodule"); 4320 return OutOfDate; 4321 } 4322 } 4323 break; 4324 } 4325 4326 case SUBMODULE_HEADER: { 4327 if (First) { 4328 Error("missing submodule metadata record at beginning of block"); 4329 return Failure; 4330 } 4331 4332 if (!CurrentModule) 4333 break; 4334 4335 // We lazily associate headers with their modules via the HeaderInfoTable. 4336 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 4337 // of complete filenames or remove it entirely. 4338 break; 4339 } 4340 4341 case SUBMODULE_EXCLUDED_HEADER: { 4342 if (First) { 4343 Error("missing submodule metadata record at beginning of block"); 4344 return Failure; 4345 } 4346 4347 if (!CurrentModule) 4348 break; 4349 4350 // We lazily associate headers with their modules via the HeaderInfoTable. 4351 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 4352 // of complete filenames or remove it entirely. 4353 break; 4354 } 4355 4356 case SUBMODULE_PRIVATE_HEADER: { 4357 if (First) { 4358 Error("missing submodule metadata record at beginning of block"); 4359 return Failure; 4360 } 4361 4362 if (!CurrentModule) 4363 break; 4364 4365 // We lazily associate headers with their modules via the HeaderInfoTable. 4366 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 4367 // of complete filenames or remove it entirely. 4368 break; 4369 } 4370 4371 case SUBMODULE_TOPHEADER: { 4372 if (First) { 4373 Error("missing submodule metadata record at beginning of block"); 4374 return Failure; 4375 } 4376 4377 if (!CurrentModule) 4378 break; 4379 4380 CurrentModule->addTopHeaderFilename(Blob); 4381 break; 4382 } 4383 4384 case SUBMODULE_UMBRELLA_DIR: { 4385 if (First) { 4386 Error("missing submodule metadata record at beginning of block"); 4387 return Failure; 4388 } 4389 4390 if (!CurrentModule) 4391 break; 4392 4393 if (const DirectoryEntry *Umbrella 4394 = PP.getFileManager().getDirectory(Blob)) { 4395 if (!CurrentModule->getUmbrellaDir()) 4396 ModMap.setUmbrellaDir(CurrentModule, Umbrella); 4397 else if (CurrentModule->getUmbrellaDir() != Umbrella) { 4398 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 4399 Error("mismatched umbrella directories in submodule"); 4400 return OutOfDate; 4401 } 4402 } 4403 break; 4404 } 4405 4406 case SUBMODULE_METADATA: { 4407 if (!First) { 4408 Error("submodule metadata record not at beginning of block"); 4409 return Failure; 4410 } 4411 First = false; 4412 4413 F.BaseSubmoduleID = getTotalNumSubmodules(); 4414 F.LocalNumSubmodules = Record[0]; 4415 unsigned LocalBaseSubmoduleID = Record[1]; 4416 if (F.LocalNumSubmodules > 0) { 4417 // Introduce the global -> local mapping for submodules within this 4418 // module. 4419 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 4420 4421 // Introduce the local -> global mapping for submodules within this 4422 // module. 4423 F.SubmoduleRemap.insertOrReplace( 4424 std::make_pair(LocalBaseSubmoduleID, 4425 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 4426 4427 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 4428 } 4429 break; 4430 } 4431 4432 case SUBMODULE_IMPORTS: { 4433 if (First) { 4434 Error("missing submodule metadata record at beginning of block"); 4435 return Failure; 4436 } 4437 4438 if (!CurrentModule) 4439 break; 4440 4441 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 4442 UnresolvedModuleRef Unresolved; 4443 Unresolved.File = &F; 4444 Unresolved.Mod = CurrentModule; 4445 Unresolved.ID = Record[Idx]; 4446 Unresolved.Kind = UnresolvedModuleRef::Import; 4447 Unresolved.IsWildcard = false; 4448 UnresolvedModuleRefs.push_back(Unresolved); 4449 } 4450 break; 4451 } 4452 4453 case SUBMODULE_EXPORTS: { 4454 if (First) { 4455 Error("missing submodule metadata record at beginning of block"); 4456 return Failure; 4457 } 4458 4459 if (!CurrentModule) 4460 break; 4461 4462 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 4463 UnresolvedModuleRef Unresolved; 4464 Unresolved.File = &F; 4465 Unresolved.Mod = CurrentModule; 4466 Unresolved.ID = Record[Idx]; 4467 Unresolved.Kind = UnresolvedModuleRef::Export; 4468 Unresolved.IsWildcard = Record[Idx + 1]; 4469 UnresolvedModuleRefs.push_back(Unresolved); 4470 } 4471 4472 // Once we've loaded the set of exports, there's no reason to keep 4473 // the parsed, unresolved exports around. 4474 CurrentModule->UnresolvedExports.clear(); 4475 break; 4476 } 4477 case SUBMODULE_REQUIRES: { 4478 if (First) { 4479 Error("missing submodule metadata record at beginning of block"); 4480 return Failure; 4481 } 4482 4483 if (!CurrentModule) 4484 break; 4485 4486 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(), 4487 Context.getTargetInfo()); 4488 break; 4489 } 4490 4491 case SUBMODULE_LINK_LIBRARY: 4492 if (First) { 4493 Error("missing submodule metadata record at beginning of block"); 4494 return Failure; 4495 } 4496 4497 if (!CurrentModule) 4498 break; 4499 4500 CurrentModule->LinkLibraries.push_back( 4501 Module::LinkLibrary(Blob, Record[0])); 4502 break; 4503 4504 case SUBMODULE_CONFIG_MACRO: 4505 if (First) { 4506 Error("missing submodule metadata record at beginning of block"); 4507 return Failure; 4508 } 4509 4510 if (!CurrentModule) 4511 break; 4512 4513 CurrentModule->ConfigMacros.push_back(Blob.str()); 4514 break; 4515 4516 case SUBMODULE_CONFLICT: { 4517 if (First) { 4518 Error("missing submodule metadata record at beginning of block"); 4519 return Failure; 4520 } 4521 4522 if (!CurrentModule) 4523 break; 4524 4525 UnresolvedModuleRef Unresolved; 4526 Unresolved.File = &F; 4527 Unresolved.Mod = CurrentModule; 4528 Unresolved.ID = Record[0]; 4529 Unresolved.Kind = UnresolvedModuleRef::Conflict; 4530 Unresolved.IsWildcard = false; 4531 Unresolved.String = Blob; 4532 UnresolvedModuleRefs.push_back(Unresolved); 4533 break; 4534 } 4535 } 4536 } 4537} 4538 4539/// \brief Parse the record that corresponds to a LangOptions data 4540/// structure. 4541/// 4542/// This routine parses the language options from the AST file and then gives 4543/// them to the AST listener if one is set. 4544/// 4545/// \returns true if the listener deems the file unacceptable, false otherwise. 4546bool ASTReader::ParseLanguageOptions(const RecordData &Record, 4547 bool Complain, 4548 ASTReaderListener &Listener) { 4549 LangOptions LangOpts; 4550 unsigned Idx = 0; 4551#define LANGOPT(Name, Bits, Default, Description) \ 4552 LangOpts.Name = Record[Idx++]; 4553#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 4554 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 4555#include "clang/Basic/LangOptions.def" 4556#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++]; 4557#include "clang/Basic/Sanitizers.def" 4558 4559 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 4560 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 4561 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 4562 4563 unsigned Length = Record[Idx++]; 4564 LangOpts.CurrentModule.assign(Record.begin() + Idx, 4565 Record.begin() + Idx + Length); 4566 4567 Idx += Length; 4568 4569 // Comment options. 4570 for (unsigned N = Record[Idx++]; N; --N) { 4571 LangOpts.CommentOpts.BlockCommandNames.push_back( 4572 ReadString(Record, Idx)); 4573 } 4574 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 4575 4576 return Listener.ReadLanguageOptions(LangOpts, Complain); 4577} 4578 4579bool ASTReader::ParseTargetOptions(const RecordData &Record, 4580 bool Complain, 4581 ASTReaderListener &Listener) { 4582 unsigned Idx = 0; 4583 TargetOptions TargetOpts; 4584 TargetOpts.Triple = ReadString(Record, Idx); 4585 TargetOpts.CPU = ReadString(Record, Idx); 4586 TargetOpts.ABI = ReadString(Record, Idx); 4587 for (unsigned N = Record[Idx++]; N; --N) { 4588 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 4589 } 4590 for (unsigned N = Record[Idx++]; N; --N) { 4591 TargetOpts.Features.push_back(ReadString(Record, Idx)); 4592 } 4593 4594 return Listener.ReadTargetOptions(TargetOpts, Complain); 4595} 4596 4597bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 4598 ASTReaderListener &Listener) { 4599 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 4600 unsigned Idx = 0; 4601#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 4602#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 4603 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 4604#include "clang/Basic/DiagnosticOptions.def" 4605 4606 for (unsigned N = Record[Idx++]; N; --N) { 4607 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 4608 } 4609 4610 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 4611} 4612 4613bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 4614 ASTReaderListener &Listener) { 4615 FileSystemOptions FSOpts; 4616 unsigned Idx = 0; 4617 FSOpts.WorkingDir = ReadString(Record, Idx); 4618 return Listener.ReadFileSystemOptions(FSOpts, Complain); 4619} 4620 4621bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 4622 bool Complain, 4623 ASTReaderListener &Listener) { 4624 HeaderSearchOptions HSOpts; 4625 unsigned Idx = 0; 4626 HSOpts.Sysroot = ReadString(Record, Idx); 4627 4628 // Include entries. 4629 for (unsigned N = Record[Idx++]; N; --N) { 4630 std::string Path = ReadString(Record, Idx); 4631 frontend::IncludeDirGroup Group 4632 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 4633 bool IsFramework = Record[Idx++]; 4634 bool IgnoreSysRoot = Record[Idx++]; 4635 HSOpts.UserEntries.push_back( 4636 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot)); 4637 } 4638 4639 // System header prefixes. 4640 for (unsigned N = Record[Idx++]; N; --N) { 4641 std::string Prefix = ReadString(Record, Idx); 4642 bool IsSystemHeader = Record[Idx++]; 4643 HSOpts.SystemHeaderPrefixes.push_back( 4644 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader)); 4645 } 4646 4647 HSOpts.ResourceDir = ReadString(Record, Idx); 4648 HSOpts.ModuleCachePath = ReadString(Record, Idx); 4649 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 4650 HSOpts.DisableModuleHash = Record[Idx++]; 4651 HSOpts.UseBuiltinIncludes = Record[Idx++]; 4652 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 4653 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 4654 HSOpts.UseLibcxx = Record[Idx++]; 4655 4656 return Listener.ReadHeaderSearchOptions(HSOpts, Complain); 4657} 4658 4659bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 4660 bool Complain, 4661 ASTReaderListener &Listener, 4662 std::string &SuggestedPredefines) { 4663 PreprocessorOptions PPOpts; 4664 unsigned Idx = 0; 4665 4666 // Macro definitions/undefs 4667 for (unsigned N = Record[Idx++]; N; --N) { 4668 std::string Macro = ReadString(Record, Idx); 4669 bool IsUndef = Record[Idx++]; 4670 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 4671 } 4672 4673 // Includes 4674 for (unsigned N = Record[Idx++]; N; --N) { 4675 PPOpts.Includes.push_back(ReadString(Record, Idx)); 4676 } 4677 4678 // Macro Includes 4679 for (unsigned N = Record[Idx++]; N; --N) { 4680 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 4681 } 4682 4683 PPOpts.UsePredefines = Record[Idx++]; 4684 PPOpts.DetailedRecord = Record[Idx++]; 4685 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 4686 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx); 4687 PPOpts.ObjCXXARCStandardLibrary = 4688 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 4689 SuggestedPredefines.clear(); 4690 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 4691 SuggestedPredefines); 4692} 4693 4694std::pair<ModuleFile *, unsigned> 4695ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 4696 GlobalPreprocessedEntityMapType::iterator 4697 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 4698 assert(I != GlobalPreprocessedEntityMap.end() && 4699 "Corrupted global preprocessed entity map"); 4700 ModuleFile *M = I->second; 4701 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 4702 return std::make_pair(M, LocalIndex); 4703} 4704 4705std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 4706ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 4707 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 4708 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 4709 Mod.NumPreprocessedEntities); 4710 4711 return std::make_pair(PreprocessingRecord::iterator(), 4712 PreprocessingRecord::iterator()); 4713} 4714 4715std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator> 4716ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 4717 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 4718 ModuleDeclIterator(this, &Mod, 4719 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 4720} 4721 4722PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 4723 PreprocessedEntityID PPID = Index+1; 4724 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4725 ModuleFile &M = *PPInfo.first; 4726 unsigned LocalIndex = PPInfo.second; 4727 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4728 4729 if (!PP.getPreprocessingRecord()) { 4730 Error("no preprocessing record"); 4731 return nullptr; 4732 } 4733 4734 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 4735 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 4736 4737 llvm::BitstreamEntry Entry = 4738 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 4739 if (Entry.Kind != llvm::BitstreamEntry::Record) 4740 return nullptr; 4741 4742 // Read the record. 4743 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin), 4744 ReadSourceLocation(M, PPOffs.End)); 4745 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 4746 StringRef Blob; 4747 RecordData Record; 4748 PreprocessorDetailRecordTypes RecType = 4749 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord( 4750 Entry.ID, Record, &Blob); 4751 switch (RecType) { 4752 case PPD_MACRO_EXPANSION: { 4753 bool isBuiltin = Record[0]; 4754 IdentifierInfo *Name = nullptr; 4755 MacroDefinition *Def = nullptr; 4756 if (isBuiltin) 4757 Name = getLocalIdentifier(M, Record[1]); 4758 else { 4759 PreprocessedEntityID 4760 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]); 4761 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)); 4762 } 4763 4764 MacroExpansion *ME; 4765 if (isBuiltin) 4766 ME = new (PPRec) MacroExpansion(Name, Range); 4767 else 4768 ME = new (PPRec) MacroExpansion(Def, Range); 4769 4770 return ME; 4771 } 4772 4773 case PPD_MACRO_DEFINITION: { 4774 // Decode the identifier info and then check again; if the macro is 4775 // still defined and associated with the identifier, 4776 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 4777 MacroDefinition *MD 4778 = new (PPRec) MacroDefinition(II, Range); 4779 4780 if (DeserializationListener) 4781 DeserializationListener->MacroDefinitionRead(PPID, MD); 4782 4783 return MD; 4784 } 4785 4786 case PPD_INCLUSION_DIRECTIVE: { 4787 const char *FullFileNameStart = Blob.data() + Record[0]; 4788 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 4789 const FileEntry *File = nullptr; 4790 if (!FullFileName.empty()) 4791 File = PP.getFileManager().getFile(FullFileName); 4792 4793 // FIXME: Stable encoding 4794 InclusionDirective::InclusionKind Kind 4795 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 4796 InclusionDirective *ID 4797 = new (PPRec) InclusionDirective(PPRec, Kind, 4798 StringRef(Blob.data(), Record[0]), 4799 Record[1], Record[3], 4800 File, 4801 Range); 4802 return ID; 4803 } 4804 } 4805 4806 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 4807} 4808 4809/// \brief \arg SLocMapI points at a chunk of a module that contains no 4810/// preprocessed entities or the entities it contains are not the ones we are 4811/// looking for. Find the next module that contains entities and return the ID 4812/// of the first entry. 4813PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 4814 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 4815 ++SLocMapI; 4816 for (GlobalSLocOffsetMapType::const_iterator 4817 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 4818 ModuleFile &M = *SLocMapI->second; 4819 if (M.NumPreprocessedEntities) 4820 return M.BasePreprocessedEntityID; 4821 } 4822 4823 return getTotalNumPreprocessedEntities(); 4824} 4825 4826namespace { 4827 4828template <unsigned PPEntityOffset::*PPLoc> 4829struct PPEntityComp { 4830 const ASTReader &Reader; 4831 ModuleFile &M; 4832 4833 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { } 4834 4835 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 4836 SourceLocation LHS = getLoc(L); 4837 SourceLocation RHS = getLoc(R); 4838 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4839 } 4840 4841 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 4842 SourceLocation LHS = getLoc(L); 4843 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4844 } 4845 4846 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 4847 SourceLocation RHS = getLoc(R); 4848 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4849 } 4850 4851 SourceLocation getLoc(const PPEntityOffset &PPE) const { 4852 return Reader.ReadSourceLocation(M, PPE.*PPLoc); 4853 } 4854}; 4855 4856} 4857 4858PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 4859 bool EndsAfter) const { 4860 if (SourceMgr.isLocalSourceLocation(Loc)) 4861 return getTotalNumPreprocessedEntities(); 4862 4863 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 4864 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 4865 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4866 "Corrupted global sloc offset map"); 4867 4868 if (SLocMapI->second->NumPreprocessedEntities == 0) 4869 return findNextPreprocessedEntity(SLocMapI); 4870 4871 ModuleFile &M = *SLocMapI->second; 4872 typedef const PPEntityOffset *pp_iterator; 4873 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4874 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4875 4876 size_t Count = M.NumPreprocessedEntities; 4877 size_t Half; 4878 pp_iterator First = pp_begin; 4879 pp_iterator PPI; 4880 4881 if (EndsAfter) { 4882 PPI = std::upper_bound(pp_begin, pp_end, Loc, 4883 PPEntityComp<&PPEntityOffset::Begin>(*this, M)); 4884 } else { 4885 // Do a binary search manually instead of using std::lower_bound because 4886 // The end locations of entities may be unordered (when a macro expansion 4887 // is inside another macro argument), but for this case it is not important 4888 // whether we get the first macro expansion or its containing macro. 4889 while (Count > 0) { 4890 Half = Count / 2; 4891 PPI = First; 4892 std::advance(PPI, Half); 4893 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End), 4894 Loc)) { 4895 First = PPI; 4896 ++First; 4897 Count = Count - Half - 1; 4898 } else 4899 Count = Half; 4900 } 4901 } 4902 4903 if (PPI == pp_end) 4904 return findNextPreprocessedEntity(SLocMapI); 4905 4906 return M.BasePreprocessedEntityID + (PPI - pp_begin); 4907} 4908 4909/// \brief Returns a pair of [Begin, End) indices of preallocated 4910/// preprocessed entities that \arg Range encompasses. 4911std::pair<unsigned, unsigned> 4912 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 4913 if (Range.isInvalid()) 4914 return std::make_pair(0,0); 4915 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 4916 4917 PreprocessedEntityID BeginID = 4918 findPreprocessedEntity(Range.getBegin(), false); 4919 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 4920 return std::make_pair(BeginID, EndID); 4921} 4922 4923/// \brief Optionally returns true or false if the preallocated preprocessed 4924/// entity with index \arg Index came from file \arg FID. 4925Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 4926 FileID FID) { 4927 if (FID.isInvalid()) 4928 return false; 4929 4930 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4931 ModuleFile &M = *PPInfo.first; 4932 unsigned LocalIndex = PPInfo.second; 4933 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4934 4935 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin); 4936 if (Loc.isInvalid()) 4937 return false; 4938 4939 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 4940 return true; 4941 else 4942 return false; 4943} 4944 4945namespace { 4946 /// \brief Visitor used to search for information about a header file. 4947 class HeaderFileInfoVisitor { 4948 const FileEntry *FE; 4949 4950 Optional<HeaderFileInfo> HFI; 4951 4952 public: 4953 explicit HeaderFileInfoVisitor(const FileEntry *FE) 4954 : FE(FE) { } 4955 4956 static bool visit(ModuleFile &M, void *UserData) { 4957 HeaderFileInfoVisitor *This 4958 = static_cast<HeaderFileInfoVisitor *>(UserData); 4959 4960 HeaderFileInfoLookupTable *Table 4961 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 4962 if (!Table) 4963 return false; 4964 4965 // Look in the on-disk hash table for an entry for this file name. 4966 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE); 4967 if (Pos == Table->end()) 4968 return false; 4969 4970 This->HFI = *Pos; 4971 return true; 4972 } 4973 4974 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 4975 }; 4976} 4977 4978HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 4979 HeaderFileInfoVisitor Visitor(FE); 4980 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); 4981 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 4982 return *HFI; 4983 4984 return HeaderFileInfo(); 4985} 4986 4987void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 4988 // FIXME: Make it work properly with modules. 4989 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates; 4990 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 4991 ModuleFile &F = *(*I); 4992 unsigned Idx = 0; 4993 DiagStates.clear(); 4994 assert(!Diag.DiagStates.empty()); 4995 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one. 4996 while (Idx < F.PragmaDiagMappings.size()) { 4997 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 4998 unsigned DiagStateID = F.PragmaDiagMappings[Idx++]; 4999 if (DiagStateID != 0) { 5000 Diag.DiagStatePoints.push_back( 5001 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1], 5002 FullSourceLoc(Loc, SourceMgr))); 5003 continue; 5004 } 5005 5006 assert(DiagStateID == 0); 5007 // A new DiagState was created here. 5008 Diag.DiagStates.push_back(*Diag.GetCurDiagState()); 5009 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back(); 5010 DiagStates.push_back(NewState); 5011 Diag.DiagStatePoints.push_back( 5012 DiagnosticsEngine::DiagStatePoint(NewState, 5013 FullSourceLoc(Loc, SourceMgr))); 5014 while (1) { 5015 assert(Idx < F.PragmaDiagMappings.size() && 5016 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 5017 if (Idx >= F.PragmaDiagMappings.size()) { 5018 break; // Something is messed up but at least avoid infinite loop in 5019 // release build. 5020 } 5021 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 5022 if (DiagID == (unsigned)-1) { 5023 break; // no more diag/map pairs for this location. 5024 } 5025 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++]; 5026 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc); 5027 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo); 5028 } 5029 } 5030 } 5031} 5032 5033/// \brief Get the correct cursor and offset for loading a type. 5034ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 5035 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 5036 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 5037 ModuleFile *M = I->second; 5038 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 5039} 5040 5041/// \brief Read and return the type with the given index.. 5042/// 5043/// The index is the type ID, shifted and minus the number of predefs. This 5044/// routine actually reads the record corresponding to the type at the given 5045/// location. It is a helper routine for GetType, which deals with reading type 5046/// IDs. 5047QualType ASTReader::readTypeRecord(unsigned Index) { 5048 RecordLocation Loc = TypeCursorForIndex(Index); 5049 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 5050 5051 // Keep track of where we are in the stream, then jump back there 5052 // after reading this type. 5053 SavedStreamPosition SavedPosition(DeclsCursor); 5054 5055 ReadingKindTracker ReadingKind(Read_Type, *this); 5056 5057 // Note that we are loading a type record. 5058 Deserializing AType(this); 5059 5060 unsigned Idx = 0; 5061 DeclsCursor.JumpToBit(Loc.Offset); 5062 RecordData Record; 5063 unsigned Code = DeclsCursor.ReadCode(); 5064 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) { 5065 case TYPE_EXT_QUAL: { 5066 if (Record.size() != 2) { 5067 Error("Incorrect encoding of extended qualifier type"); 5068 return QualType(); 5069 } 5070 QualType Base = readType(*Loc.F, Record, Idx); 5071 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 5072 return Context.getQualifiedType(Base, Quals); 5073 } 5074 5075 case TYPE_COMPLEX: { 5076 if (Record.size() != 1) { 5077 Error("Incorrect encoding of complex type"); 5078 return QualType(); 5079 } 5080 QualType ElemType = readType(*Loc.F, Record, Idx); 5081 return Context.getComplexType(ElemType); 5082 } 5083 5084 case TYPE_POINTER: { 5085 if (Record.size() != 1) { 5086 Error("Incorrect encoding of pointer type"); 5087 return QualType(); 5088 } 5089 QualType PointeeType = readType(*Loc.F, Record, Idx); 5090 return Context.getPointerType(PointeeType); 5091 } 5092 5093 case TYPE_DECAYED: { 5094 if (Record.size() != 1) { 5095 Error("Incorrect encoding of decayed type"); 5096 return QualType(); 5097 } 5098 QualType OriginalType = readType(*Loc.F, Record, Idx); 5099 QualType DT = Context.getAdjustedParameterType(OriginalType); 5100 if (!isa<DecayedType>(DT)) 5101 Error("Decayed type does not decay"); 5102 return DT; 5103 } 5104 5105 case TYPE_ADJUSTED: { 5106 if (Record.size() != 2) { 5107 Error("Incorrect encoding of adjusted type"); 5108 return QualType(); 5109 } 5110 QualType OriginalTy = readType(*Loc.F, Record, Idx); 5111 QualType AdjustedTy = readType(*Loc.F, Record, Idx); 5112 return Context.getAdjustedType(OriginalTy, AdjustedTy); 5113 } 5114 5115 case TYPE_BLOCK_POINTER: { 5116 if (Record.size() != 1) { 5117 Error("Incorrect encoding of block pointer type"); 5118 return QualType(); 5119 } 5120 QualType PointeeType = readType(*Loc.F, Record, Idx); 5121 return Context.getBlockPointerType(PointeeType); 5122 } 5123 5124 case TYPE_LVALUE_REFERENCE: { 5125 if (Record.size() != 2) { 5126 Error("Incorrect encoding of lvalue reference type"); 5127 return QualType(); 5128 } 5129 QualType PointeeType = readType(*Loc.F, Record, Idx); 5130 return Context.getLValueReferenceType(PointeeType, Record[1]); 5131 } 5132 5133 case TYPE_RVALUE_REFERENCE: { 5134 if (Record.size() != 1) { 5135 Error("Incorrect encoding of rvalue reference type"); 5136 return QualType(); 5137 } 5138 QualType PointeeType = readType(*Loc.F, Record, Idx); 5139 return Context.getRValueReferenceType(PointeeType); 5140 } 5141 5142 case TYPE_MEMBER_POINTER: { 5143 if (Record.size() != 2) { 5144 Error("Incorrect encoding of member pointer type"); 5145 return QualType(); 5146 } 5147 QualType PointeeType = readType(*Loc.F, Record, Idx); 5148 QualType ClassType = readType(*Loc.F, Record, Idx); 5149 if (PointeeType.isNull() || ClassType.isNull()) 5150 return QualType(); 5151 5152 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 5153 } 5154 5155 case TYPE_CONSTANT_ARRAY: { 5156 QualType ElementType = readType(*Loc.F, Record, Idx); 5157 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5158 unsigned IndexTypeQuals = Record[2]; 5159 unsigned Idx = 3; 5160 llvm::APInt Size = ReadAPInt(Record, Idx); 5161 return Context.getConstantArrayType(ElementType, Size, 5162 ASM, IndexTypeQuals); 5163 } 5164 5165 case TYPE_INCOMPLETE_ARRAY: { 5166 QualType ElementType = readType(*Loc.F, Record, Idx); 5167 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5168 unsigned IndexTypeQuals = Record[2]; 5169 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 5170 } 5171 5172 case TYPE_VARIABLE_ARRAY: { 5173 QualType ElementType = readType(*Loc.F, Record, Idx); 5174 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5175 unsigned IndexTypeQuals = Record[2]; 5176 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 5177 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 5178 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 5179 ASM, IndexTypeQuals, 5180 SourceRange(LBLoc, RBLoc)); 5181 } 5182 5183 case TYPE_VECTOR: { 5184 if (Record.size() != 3) { 5185 Error("incorrect encoding of vector type in AST file"); 5186 return QualType(); 5187 } 5188 5189 QualType ElementType = readType(*Loc.F, Record, Idx); 5190 unsigned NumElements = Record[1]; 5191 unsigned VecKind = Record[2]; 5192 return Context.getVectorType(ElementType, NumElements, 5193 (VectorType::VectorKind)VecKind); 5194 } 5195 5196 case TYPE_EXT_VECTOR: { 5197 if (Record.size() != 3) { 5198 Error("incorrect encoding of extended vector type in AST file"); 5199 return QualType(); 5200 } 5201 5202 QualType ElementType = readType(*Loc.F, Record, Idx); 5203 unsigned NumElements = Record[1]; 5204 return Context.getExtVectorType(ElementType, NumElements); 5205 } 5206 5207 case TYPE_FUNCTION_NO_PROTO: { 5208 if (Record.size() != 6) { 5209 Error("incorrect encoding of no-proto function type"); 5210 return QualType(); 5211 } 5212 QualType ResultType = readType(*Loc.F, Record, Idx); 5213 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 5214 (CallingConv)Record[4], Record[5]); 5215 return Context.getFunctionNoProtoType(ResultType, Info); 5216 } 5217 5218 case TYPE_FUNCTION_PROTO: { 5219 QualType ResultType = readType(*Loc.F, Record, Idx); 5220 5221 FunctionProtoType::ExtProtoInfo EPI; 5222 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 5223 /*hasregparm*/ Record[2], 5224 /*regparm*/ Record[3], 5225 static_cast<CallingConv>(Record[4]), 5226 /*produces*/ Record[5]); 5227 5228 unsigned Idx = 6; 5229 unsigned NumParams = Record[Idx++]; 5230 SmallVector<QualType, 16> ParamTypes; 5231 for (unsigned I = 0; I != NumParams; ++I) 5232 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 5233 5234 EPI.Variadic = Record[Idx++]; 5235 EPI.HasTrailingReturn = Record[Idx++]; 5236 EPI.TypeQuals = Record[Idx++]; 5237 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 5238 SmallVector<QualType, 8> ExceptionStorage; 5239 readExceptionSpec(*Loc.F, ExceptionStorage, EPI, Record, Idx); 5240 return Context.getFunctionType(ResultType, ParamTypes, EPI); 5241 } 5242 5243 case TYPE_UNRESOLVED_USING: { 5244 unsigned Idx = 0; 5245 return Context.getTypeDeclType( 5246 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 5247 } 5248 5249 case TYPE_TYPEDEF: { 5250 if (Record.size() != 2) { 5251 Error("incorrect encoding of typedef type"); 5252 return QualType(); 5253 } 5254 unsigned Idx = 0; 5255 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 5256 QualType Canonical = readType(*Loc.F, Record, Idx); 5257 if (!Canonical.isNull()) 5258 Canonical = Context.getCanonicalType(Canonical); 5259 return Context.getTypedefType(Decl, Canonical); 5260 } 5261 5262 case TYPE_TYPEOF_EXPR: 5263 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 5264 5265 case TYPE_TYPEOF: { 5266 if (Record.size() != 1) { 5267 Error("incorrect encoding of typeof(type) in AST file"); 5268 return QualType(); 5269 } 5270 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 5271 return Context.getTypeOfType(UnderlyingType); 5272 } 5273 5274 case TYPE_DECLTYPE: { 5275 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 5276 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 5277 } 5278 5279 case TYPE_UNARY_TRANSFORM: { 5280 QualType BaseType = readType(*Loc.F, Record, Idx); 5281 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 5282 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 5283 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 5284 } 5285 5286 case TYPE_AUTO: { 5287 QualType Deduced = readType(*Loc.F, Record, Idx); 5288 bool IsDecltypeAuto = Record[Idx++]; 5289 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false; 5290 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent); 5291 } 5292 5293 case TYPE_RECORD: { 5294 if (Record.size() != 2) { 5295 Error("incorrect encoding of record type"); 5296 return QualType(); 5297 } 5298 unsigned Idx = 0; 5299 bool IsDependent = Record[Idx++]; 5300 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 5301 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 5302 QualType T = Context.getRecordType(RD); 5303 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 5304 return T; 5305 } 5306 5307 case TYPE_ENUM: { 5308 if (Record.size() != 2) { 5309 Error("incorrect encoding of enum type"); 5310 return QualType(); 5311 } 5312 unsigned Idx = 0; 5313 bool IsDependent = Record[Idx++]; 5314 QualType T 5315 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 5316 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 5317 return T; 5318 } 5319 5320 case TYPE_ATTRIBUTED: { 5321 if (Record.size() != 3) { 5322 Error("incorrect encoding of attributed type"); 5323 return QualType(); 5324 } 5325 QualType modifiedType = readType(*Loc.F, Record, Idx); 5326 QualType equivalentType = readType(*Loc.F, Record, Idx); 5327 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 5328 return Context.getAttributedType(kind, modifiedType, equivalentType); 5329 } 5330 5331 case TYPE_PAREN: { 5332 if (Record.size() != 1) { 5333 Error("incorrect encoding of paren type"); 5334 return QualType(); 5335 } 5336 QualType InnerType = readType(*Loc.F, Record, Idx); 5337 return Context.getParenType(InnerType); 5338 } 5339 5340 case TYPE_PACK_EXPANSION: { 5341 if (Record.size() != 2) { 5342 Error("incorrect encoding of pack expansion type"); 5343 return QualType(); 5344 } 5345 QualType Pattern = readType(*Loc.F, Record, Idx); 5346 if (Pattern.isNull()) 5347 return QualType(); 5348 Optional<unsigned> NumExpansions; 5349 if (Record[1]) 5350 NumExpansions = Record[1] - 1; 5351 return Context.getPackExpansionType(Pattern, NumExpansions); 5352 } 5353 5354 case TYPE_ELABORATED: { 5355 unsigned Idx = 0; 5356 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 5357 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 5358 QualType NamedType = readType(*Loc.F, Record, Idx); 5359 return Context.getElaboratedType(Keyword, NNS, NamedType); 5360 } 5361 5362 case TYPE_OBJC_INTERFACE: { 5363 unsigned Idx = 0; 5364 ObjCInterfaceDecl *ItfD 5365 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 5366 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 5367 } 5368 5369 case TYPE_OBJC_OBJECT: { 5370 unsigned Idx = 0; 5371 QualType Base = readType(*Loc.F, Record, Idx); 5372 unsigned NumProtos = Record[Idx++]; 5373 SmallVector<ObjCProtocolDecl*, 4> Protos; 5374 for (unsigned I = 0; I != NumProtos; ++I) 5375 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 5376 return Context.getObjCObjectType(Base, Protos.data(), NumProtos); 5377 } 5378 5379 case TYPE_OBJC_OBJECT_POINTER: { 5380 unsigned Idx = 0; 5381 QualType Pointee = readType(*Loc.F, Record, Idx); 5382 return Context.getObjCObjectPointerType(Pointee); 5383 } 5384 5385 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 5386 unsigned Idx = 0; 5387 QualType Parm = readType(*Loc.F, Record, Idx); 5388 QualType Replacement = readType(*Loc.F, Record, Idx); 5389 return Context.getSubstTemplateTypeParmType( 5390 cast<TemplateTypeParmType>(Parm), 5391 Context.getCanonicalType(Replacement)); 5392 } 5393 5394 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 5395 unsigned Idx = 0; 5396 QualType Parm = readType(*Loc.F, Record, Idx); 5397 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 5398 return Context.getSubstTemplateTypeParmPackType( 5399 cast<TemplateTypeParmType>(Parm), 5400 ArgPack); 5401 } 5402 5403 case TYPE_INJECTED_CLASS_NAME: { 5404 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 5405 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 5406 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 5407 // for AST reading, too much interdependencies. 5408 const Type *T; 5409 if (const Type *Existing = D->getTypeForDecl()) 5410 T = Existing; 5411 else if (auto *Prev = D->getPreviousDecl()) 5412 T = Prev->getTypeForDecl(); 5413 else 5414 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST); 5415 return QualType(T, 0); 5416 } 5417 5418 case TYPE_TEMPLATE_TYPE_PARM: { 5419 unsigned Idx = 0; 5420 unsigned Depth = Record[Idx++]; 5421 unsigned Index = Record[Idx++]; 5422 bool Pack = Record[Idx++]; 5423 TemplateTypeParmDecl *D 5424 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 5425 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 5426 } 5427 5428 case TYPE_DEPENDENT_NAME: { 5429 unsigned Idx = 0; 5430 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 5431 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 5432 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 5433 QualType Canon = readType(*Loc.F, Record, Idx); 5434 if (!Canon.isNull()) 5435 Canon = Context.getCanonicalType(Canon); 5436 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 5437 } 5438 5439 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 5440 unsigned Idx = 0; 5441 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 5442 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 5443 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 5444 unsigned NumArgs = Record[Idx++]; 5445 SmallVector<TemplateArgument, 8> Args; 5446 Args.reserve(NumArgs); 5447 while (NumArgs--) 5448 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 5449 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 5450 Args.size(), Args.data()); 5451 } 5452 5453 case TYPE_DEPENDENT_SIZED_ARRAY: { 5454 unsigned Idx = 0; 5455 5456 // ArrayType 5457 QualType ElementType = readType(*Loc.F, Record, Idx); 5458 ArrayType::ArraySizeModifier ASM 5459 = (ArrayType::ArraySizeModifier)Record[Idx++]; 5460 unsigned IndexTypeQuals = Record[Idx++]; 5461 5462 // DependentSizedArrayType 5463 Expr *NumElts = ReadExpr(*Loc.F); 5464 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 5465 5466 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 5467 IndexTypeQuals, Brackets); 5468 } 5469 5470 case TYPE_TEMPLATE_SPECIALIZATION: { 5471 unsigned Idx = 0; 5472 bool IsDependent = Record[Idx++]; 5473 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 5474 SmallVector<TemplateArgument, 8> Args; 5475 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 5476 QualType Underlying = readType(*Loc.F, Record, Idx); 5477 QualType T; 5478 if (Underlying.isNull()) 5479 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(), 5480 Args.size()); 5481 else 5482 T = Context.getTemplateSpecializationType(Name, Args.data(), 5483 Args.size(), Underlying); 5484 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 5485 return T; 5486 } 5487 5488 case TYPE_ATOMIC: { 5489 if (Record.size() != 1) { 5490 Error("Incorrect encoding of atomic type"); 5491 return QualType(); 5492 } 5493 QualType ValueType = readType(*Loc.F, Record, Idx); 5494 return Context.getAtomicType(ValueType); 5495 } 5496 } 5497 llvm_unreachable("Invalid TypeCode!"); 5498} 5499 5500void ASTReader::readExceptionSpec(ModuleFile &ModuleFile, 5501 SmallVectorImpl<QualType> &Exceptions, 5502 FunctionProtoType::ExtProtoInfo &EPI, 5503 const RecordData &Record, unsigned &Idx) { 5504 ExceptionSpecificationType EST = 5505 static_cast<ExceptionSpecificationType>(Record[Idx++]); 5506 EPI.ExceptionSpecType = EST; 5507 if (EST == EST_Dynamic) { 5508 EPI.NumExceptions = Record[Idx++]; 5509 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 5510 Exceptions.push_back(readType(ModuleFile, Record, Idx)); 5511 EPI.Exceptions = Exceptions.data(); 5512 } else if (EST == EST_ComputedNoexcept) { 5513 EPI.NoexceptExpr = ReadExpr(ModuleFile); 5514 } else if (EST == EST_Uninstantiated) { 5515 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 5516 EPI.ExceptionSpecTemplate = 5517 ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 5518 } else if (EST == EST_Unevaluated) { 5519 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 5520 } 5521} 5522 5523class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 5524 ASTReader &Reader; 5525 ModuleFile &F; 5526 const ASTReader::RecordData &Record; 5527 unsigned &Idx; 5528 5529 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 5530 unsigned &I) { 5531 return Reader.ReadSourceLocation(F, R, I); 5532 } 5533 5534 template<typename T> 5535 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 5536 return Reader.ReadDeclAs<T>(F, Record, Idx); 5537 } 5538 5539public: 5540 TypeLocReader(ASTReader &Reader, ModuleFile &F, 5541 const ASTReader::RecordData &Record, unsigned &Idx) 5542 : Reader(Reader), F(F), Record(Record), Idx(Idx) 5543 { } 5544 5545 // We want compile-time assurance that we've enumerated all of 5546 // these, so unfortunately we have to declare them first, then 5547 // define them out-of-line. 5548#define ABSTRACT_TYPELOC(CLASS, PARENT) 5549#define TYPELOC(CLASS, PARENT) \ 5550 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 5551#include "clang/AST/TypeLocNodes.def" 5552 5553 void VisitFunctionTypeLoc(FunctionTypeLoc); 5554 void VisitArrayTypeLoc(ArrayTypeLoc); 5555}; 5556 5557void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 5558 // nothing to do 5559} 5560void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 5561 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 5562 if (TL.needsExtraLocalData()) { 5563 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 5564 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 5565 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 5566 TL.setModeAttr(Record[Idx++]); 5567 } 5568} 5569void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 5570 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5571} 5572void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 5573 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5574} 5575void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 5576 // nothing to do 5577} 5578void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 5579 // nothing to do 5580} 5581void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 5582 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 5583} 5584void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 5585 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 5586} 5587void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 5588 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 5589} 5590void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 5591 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5592 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5593} 5594void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 5595 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 5596 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 5597 if (Record[Idx++]) 5598 TL.setSizeExpr(Reader.ReadExpr(F)); 5599 else 5600 TL.setSizeExpr(nullptr); 5601} 5602void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 5603 VisitArrayTypeLoc(TL); 5604} 5605void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 5606 VisitArrayTypeLoc(TL); 5607} 5608void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 5609 VisitArrayTypeLoc(TL); 5610} 5611void TypeLocReader::VisitDependentSizedArrayTypeLoc( 5612 DependentSizedArrayTypeLoc TL) { 5613 VisitArrayTypeLoc(TL); 5614} 5615void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 5616 DependentSizedExtVectorTypeLoc TL) { 5617 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5618} 5619void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 5620 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5621} 5622void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 5623 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5624} 5625void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 5626 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 5627 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5628 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5629 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 5630 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 5631 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 5632 } 5633} 5634void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 5635 VisitFunctionTypeLoc(TL); 5636} 5637void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 5638 VisitFunctionTypeLoc(TL); 5639} 5640void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 5641 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5642} 5643void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 5644 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5645} 5646void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 5647 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 5648 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5649 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5650} 5651void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 5652 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 5653 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5654 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5655 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5656} 5657void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 5658 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5659} 5660void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 5661 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5662 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5663 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5664 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5665} 5666void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 5667 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5668} 5669void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 5670 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5671} 5672void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 5673 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5674} 5675void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 5676 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 5677 if (TL.hasAttrOperand()) { 5678 SourceRange range; 5679 range.setBegin(ReadSourceLocation(Record, Idx)); 5680 range.setEnd(ReadSourceLocation(Record, Idx)); 5681 TL.setAttrOperandParensRange(range); 5682 } 5683 if (TL.hasAttrExprOperand()) { 5684 if (Record[Idx++]) 5685 TL.setAttrExprOperand(Reader.ReadExpr(F)); 5686 else 5687 TL.setAttrExprOperand(nullptr); 5688 } else if (TL.hasAttrEnumOperand()) 5689 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 5690} 5691void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 5692 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5693} 5694void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 5695 SubstTemplateTypeParmTypeLoc TL) { 5696 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5697} 5698void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 5699 SubstTemplateTypeParmPackTypeLoc TL) { 5700 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5701} 5702void TypeLocReader::VisitTemplateSpecializationTypeLoc( 5703 TemplateSpecializationTypeLoc TL) { 5704 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5705 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5706 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5707 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5708 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 5709 TL.setArgLocInfo(i, 5710 Reader.GetTemplateArgumentLocInfo(F, 5711 TL.getTypePtr()->getArg(i).getKind(), 5712 Record, Idx)); 5713} 5714void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 5715 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5716 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5717} 5718void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 5719 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5720 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5721} 5722void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 5723 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5724} 5725void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 5726 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5727 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5728 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5729} 5730void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 5731 DependentTemplateSpecializationTypeLoc TL) { 5732 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5733 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5734 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5735 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5736 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5737 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5738 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 5739 TL.setArgLocInfo(I, 5740 Reader.GetTemplateArgumentLocInfo(F, 5741 TL.getTypePtr()->getArg(I).getKind(), 5742 Record, Idx)); 5743} 5744void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 5745 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 5746} 5747void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 5748 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5749} 5750void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 5751 TL.setHasBaseTypeAsWritten(Record[Idx++]); 5752 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5753 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5754 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 5755 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 5756} 5757void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 5758 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5759} 5760void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 5761 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5762 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5763 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5764} 5765 5766TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F, 5767 const RecordData &Record, 5768 unsigned &Idx) { 5769 QualType InfoTy = readType(F, Record, Idx); 5770 if (InfoTy.isNull()) 5771 return nullptr; 5772 5773 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 5774 TypeLocReader TLR(*this, F, Record, Idx); 5775 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 5776 TLR.Visit(TL); 5777 return TInfo; 5778} 5779 5780QualType ASTReader::GetType(TypeID ID) { 5781 unsigned FastQuals = ID & Qualifiers::FastMask; 5782 unsigned Index = ID >> Qualifiers::FastWidth; 5783 5784 if (Index < NUM_PREDEF_TYPE_IDS) { 5785 QualType T; 5786 switch ((PredefinedTypeIDs)Index) { 5787 case PREDEF_TYPE_NULL_ID: return QualType(); 5788 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; 5789 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; 5790 5791 case PREDEF_TYPE_CHAR_U_ID: 5792 case PREDEF_TYPE_CHAR_S_ID: 5793 // FIXME: Check that the signedness of CharTy is correct! 5794 T = Context.CharTy; 5795 break; 5796 5797 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; 5798 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; 5799 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; 5800 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; 5801 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; 5802 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break; 5803 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; 5804 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; 5805 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; 5806 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break; 5807 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; 5808 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; 5809 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break; 5810 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break; 5811 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; 5812 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; 5813 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; 5814 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; 5815 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break; 5816 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break; 5817 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; 5818 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break; 5819 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break; 5820 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break; 5821 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break; 5822 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break; 5823 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break; 5824 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break; 5825 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break; 5826 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break; 5827 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break; 5828 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break; 5829 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break; 5830 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break; 5831 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break; 5832 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break; 5833 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break; 5834 5835 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 5836 T = Context.getAutoRRefDeductType(); 5837 break; 5838 5839 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 5840 T = Context.ARCUnbridgedCastTy; 5841 break; 5842 5843 case PREDEF_TYPE_VA_LIST_TAG: 5844 T = Context.getVaListTagType(); 5845 break; 5846 5847 case PREDEF_TYPE_BUILTIN_FN: 5848 T = Context.BuiltinFnTy; 5849 break; 5850 } 5851 5852 assert(!T.isNull() && "Unknown predefined type"); 5853 return T.withFastQualifiers(FastQuals); 5854 } 5855 5856 Index -= NUM_PREDEF_TYPE_IDS; 5857 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 5858 if (TypesLoaded[Index].isNull()) { 5859 TypesLoaded[Index] = readTypeRecord(Index); 5860 if (TypesLoaded[Index].isNull()) 5861 return QualType(); 5862 5863 TypesLoaded[Index]->setFromAST(); 5864 if (DeserializationListener) 5865 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 5866 TypesLoaded[Index]); 5867 } 5868 5869 return TypesLoaded[Index].withFastQualifiers(FastQuals); 5870} 5871 5872QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 5873 return GetType(getGlobalTypeID(F, LocalID)); 5874} 5875 5876serialization::TypeID 5877ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 5878 unsigned FastQuals = LocalID & Qualifiers::FastMask; 5879 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 5880 5881 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 5882 return LocalID; 5883 5884 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5885 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 5886 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 5887 5888 unsigned GlobalIndex = LocalIndex + I->second; 5889 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 5890} 5891 5892TemplateArgumentLocInfo 5893ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 5894 TemplateArgument::ArgKind Kind, 5895 const RecordData &Record, 5896 unsigned &Index) { 5897 switch (Kind) { 5898 case TemplateArgument::Expression: 5899 return ReadExpr(F); 5900 case TemplateArgument::Type: 5901 return GetTypeSourceInfo(F, Record, Index); 5902 case TemplateArgument::Template: { 5903 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5904 Index); 5905 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5906 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5907 SourceLocation()); 5908 } 5909 case TemplateArgument::TemplateExpansion: { 5910 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5911 Index); 5912 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5913 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 5914 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5915 EllipsisLoc); 5916 } 5917 case TemplateArgument::Null: 5918 case TemplateArgument::Integral: 5919 case TemplateArgument::Declaration: 5920 case TemplateArgument::NullPtr: 5921 case TemplateArgument::Pack: 5922 // FIXME: Is this right? 5923 return TemplateArgumentLocInfo(); 5924 } 5925 llvm_unreachable("unexpected template argument loc"); 5926} 5927 5928TemplateArgumentLoc 5929ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 5930 const RecordData &Record, unsigned &Index) { 5931 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 5932 5933 if (Arg.getKind() == TemplateArgument::Expression) { 5934 if (Record[Index++]) // bool InfoHasSameExpr. 5935 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 5936 } 5937 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 5938 Record, Index)); 5939} 5940 5941const ASTTemplateArgumentListInfo* 5942ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F, 5943 const RecordData &Record, 5944 unsigned &Index) { 5945 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index); 5946 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index); 5947 unsigned NumArgsAsWritten = Record[Index++]; 5948 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 5949 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 5950 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index)); 5951 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 5952} 5953 5954Decl *ASTReader::GetExternalDecl(uint32_t ID) { 5955 return GetDecl(ID); 5956} 5957 5958void ASTReader::CompleteRedeclChain(const Decl *D) { 5959 if (NumCurrentElementsDeserializing) { 5960 // We arrange to not care about the complete redeclaration chain while we're 5961 // deserializing. Just remember that the AST has marked this one as complete 5962 // but that it's not actually complete yet, so we know we still need to 5963 // complete it later. 5964 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 5965 return; 5966 } 5967 5968 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 5969 5970 // Recursively ensure that the decl context itself is complete 5971 // (in particular, this matters if the decl context is a namespace). 5972 // 5973 // FIXME: This should be performed by lookup instead of here. 5974 cast<Decl>(DC)->getMostRecentDecl(); 5975 5976 // If this is a named declaration, complete it by looking it up 5977 // within its context. 5978 // 5979 // FIXME: We don't currently handle the cases where we can't do this; 5980 // merging a class definition that contains unnamed entities should merge 5981 // those entities. Likewise, merging a function definition should merge 5982 // all mergeable entities within it. 5983 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 5984 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 5985 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 5986 auto *II = Name.getAsIdentifierInfo(); 5987 if (isa<TranslationUnitDecl>(DC) && II) { 5988 // Outside of C++, we don't have a lookup table for the TU, so update 5989 // the identifier instead. In C++, either way should work fine. 5990 if (II->isOutOfDate()) 5991 updateOutOfDateIdentifier(*II); 5992 } else 5993 DC->lookup(Name); 5994 } 5995 } 5996} 5997 5998uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, 5999 const RecordData &Record, 6000 unsigned &Idx) { 6001 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) { 6002 Error("malformed AST file: missing C++ base specifier"); 6003 return 0; 6004 } 6005 6006 unsigned LocalID = Record[Idx++]; 6007 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]); 6008} 6009 6010CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 6011 RecordLocation Loc = getLocalBitOffset(Offset); 6012 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 6013 SavedStreamPosition SavedPosition(Cursor); 6014 Cursor.JumpToBit(Loc.Offset); 6015 ReadingKindTracker ReadingKind(Read_Decl, *this); 6016 RecordData Record; 6017 unsigned Code = Cursor.ReadCode(); 6018 unsigned RecCode = Cursor.readRecord(Code, Record); 6019 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 6020 Error("malformed AST file: missing C++ base specifiers"); 6021 return nullptr; 6022 } 6023 6024 unsigned Idx = 0; 6025 unsigned NumBases = Record[Idx++]; 6026 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 6027 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 6028 for (unsigned I = 0; I != NumBases; ++I) 6029 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 6030 return Bases; 6031} 6032 6033serialization::DeclID 6034ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 6035 if (LocalID < NUM_PREDEF_DECL_IDS) 6036 return LocalID; 6037 6038 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6039 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 6040 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 6041 6042 return LocalID + I->second; 6043} 6044 6045bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 6046 ModuleFile &M) const { 6047 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID); 6048 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 6049 return &M == I->second; 6050} 6051 6052ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 6053 if (!D->isFromASTFile()) 6054 return nullptr; 6055 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 6056 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 6057 return I->second; 6058} 6059 6060SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 6061 if (ID < NUM_PREDEF_DECL_IDS) 6062 return SourceLocation(); 6063 6064 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 6065 6066 if (Index > DeclsLoaded.size()) { 6067 Error("declaration ID out-of-range for AST file"); 6068 return SourceLocation(); 6069 } 6070 6071 if (Decl *D = DeclsLoaded[Index]) 6072 return D->getLocation(); 6073 6074 unsigned RawLocation = 0; 6075 RecordLocation Rec = DeclCursorForID(ID, RawLocation); 6076 return ReadSourceLocation(*Rec.F, RawLocation); 6077} 6078 6079Decl *ASTReader::GetExistingDecl(DeclID ID) { 6080 if (ID < NUM_PREDEF_DECL_IDS) { 6081 switch ((PredefinedDeclIDs)ID) { 6082 case PREDEF_DECL_NULL_ID: 6083 return nullptr; 6084 6085 case PREDEF_DECL_TRANSLATION_UNIT_ID: 6086 return Context.getTranslationUnitDecl(); 6087 6088 case PREDEF_DECL_OBJC_ID_ID: 6089 return Context.getObjCIdDecl(); 6090 6091 case PREDEF_DECL_OBJC_SEL_ID: 6092 return Context.getObjCSelDecl(); 6093 6094 case PREDEF_DECL_OBJC_CLASS_ID: 6095 return Context.getObjCClassDecl(); 6096 6097 case PREDEF_DECL_OBJC_PROTOCOL_ID: 6098 return Context.getObjCProtocolDecl(); 6099 6100 case PREDEF_DECL_INT_128_ID: 6101 return Context.getInt128Decl(); 6102 6103 case PREDEF_DECL_UNSIGNED_INT_128_ID: 6104 return Context.getUInt128Decl(); 6105 6106 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 6107 return Context.getObjCInstanceTypeDecl(); 6108 6109 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 6110 return Context.getBuiltinVaListDecl(); 6111 } 6112 } 6113 6114 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 6115 6116 if (Index >= DeclsLoaded.size()) { 6117 assert(0 && "declaration ID out-of-range for AST file"); 6118 Error("declaration ID out-of-range for AST file"); 6119 return nullptr; 6120 } 6121 6122 return DeclsLoaded[Index]; 6123} 6124 6125Decl *ASTReader::GetDecl(DeclID ID) { 6126 if (ID < NUM_PREDEF_DECL_IDS) 6127 return GetExistingDecl(ID); 6128 6129 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 6130 6131 if (Index >= DeclsLoaded.size()) { 6132 assert(0 && "declaration ID out-of-range for AST file"); 6133 Error("declaration ID out-of-range for AST file"); 6134 return nullptr; 6135 } 6136 6137 if (!DeclsLoaded[Index]) { 6138 ReadDeclRecord(ID); 6139 if (DeserializationListener) 6140 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 6141 } 6142 6143 return DeclsLoaded[Index]; 6144} 6145 6146DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 6147 DeclID GlobalID) { 6148 if (GlobalID < NUM_PREDEF_DECL_IDS) 6149 return GlobalID; 6150 6151 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 6152 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 6153 ModuleFile *Owner = I->second; 6154 6155 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 6156 = M.GlobalToLocalDeclIDs.find(Owner); 6157 if (Pos == M.GlobalToLocalDeclIDs.end()) 6158 return 0; 6159 6160 return GlobalID - Owner->BaseDeclID + Pos->second; 6161} 6162 6163serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 6164 const RecordData &Record, 6165 unsigned &Idx) { 6166 if (Idx >= Record.size()) { 6167 Error("Corrupted AST file"); 6168 return 0; 6169 } 6170 6171 return getGlobalDeclID(F, Record[Idx++]); 6172} 6173 6174/// \brief Resolve the offset of a statement into a statement. 6175/// 6176/// This operation will read a new statement from the external 6177/// source each time it is called, and is meant to be used via a 6178/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 6179Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 6180 // Switch case IDs are per Decl. 6181 ClearSwitchCaseIDs(); 6182 6183 // Offset here is a global offset across the entire chain. 6184 RecordLocation Loc = getLocalBitOffset(Offset); 6185 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 6186 return ReadStmtFromStream(*Loc.F); 6187} 6188 6189namespace { 6190 class FindExternalLexicalDeclsVisitor { 6191 ASTReader &Reader; 6192 const DeclContext *DC; 6193 bool (*isKindWeWant)(Decl::Kind); 6194 6195 SmallVectorImpl<Decl*> &Decls; 6196 bool PredefsVisited[NUM_PREDEF_DECL_IDS]; 6197 6198 public: 6199 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC, 6200 bool (*isKindWeWant)(Decl::Kind), 6201 SmallVectorImpl<Decl*> &Decls) 6202 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls) 6203 { 6204 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I) 6205 PredefsVisited[I] = false; 6206 } 6207 6208 static bool visit(ModuleFile &M, bool Preorder, void *UserData) { 6209 if (Preorder) 6210 return false; 6211 6212 FindExternalLexicalDeclsVisitor *This 6213 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData); 6214 6215 ModuleFile::DeclContextInfosMap::iterator Info 6216 = M.DeclContextInfos.find(This->DC); 6217 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls) 6218 return false; 6219 6220 // Load all of the declaration IDs 6221 for (const KindDeclIDPair *ID = Info->second.LexicalDecls, 6222 *IDE = ID + Info->second.NumLexicalDecls; 6223 ID != IDE; ++ID) { 6224 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first)) 6225 continue; 6226 6227 // Don't add predefined declarations to the lexical context more 6228 // than once. 6229 if (ID->second < NUM_PREDEF_DECL_IDS) { 6230 if (This->PredefsVisited[ID->second]) 6231 continue; 6232 6233 This->PredefsVisited[ID->second] = true; 6234 } 6235 6236 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) { 6237 if (!This->DC->isDeclInLexicalTraversal(D)) 6238 This->Decls.push_back(D); 6239 } 6240 } 6241 6242 return false; 6243 } 6244 }; 6245} 6246 6247ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 6248 bool (*isKindWeWant)(Decl::Kind), 6249 SmallVectorImpl<Decl*> &Decls) { 6250 // There might be lexical decls in multiple modules, for the TU at 6251 // least. Walk all of the modules in the order they were loaded. 6252 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls); 6253 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor); 6254 ++NumLexicalDeclContextsRead; 6255 return ELR_Success; 6256} 6257 6258namespace { 6259 6260class DeclIDComp { 6261 ASTReader &Reader; 6262 ModuleFile &Mod; 6263 6264public: 6265 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 6266 6267 bool operator()(LocalDeclID L, LocalDeclID R) const { 6268 SourceLocation LHS = getLocation(L); 6269 SourceLocation RHS = getLocation(R); 6270 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6271 } 6272 6273 bool operator()(SourceLocation LHS, LocalDeclID R) const { 6274 SourceLocation RHS = getLocation(R); 6275 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6276 } 6277 6278 bool operator()(LocalDeclID L, SourceLocation RHS) const { 6279 SourceLocation LHS = getLocation(L); 6280 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6281 } 6282 6283 SourceLocation getLocation(LocalDeclID ID) const { 6284 return Reader.getSourceManager().getFileLoc( 6285 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 6286 } 6287}; 6288 6289} 6290 6291void ASTReader::FindFileRegionDecls(FileID File, 6292 unsigned Offset, unsigned Length, 6293 SmallVectorImpl<Decl *> &Decls) { 6294 SourceManager &SM = getSourceManager(); 6295 6296 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 6297 if (I == FileDeclIDs.end()) 6298 return; 6299 6300 FileDeclsInfo &DInfo = I->second; 6301 if (DInfo.Decls.empty()) 6302 return; 6303 6304 SourceLocation 6305 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 6306 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 6307 6308 DeclIDComp DIDComp(*this, *DInfo.Mod); 6309 ArrayRef<serialization::LocalDeclID>::iterator 6310 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 6311 BeginLoc, DIDComp); 6312 if (BeginIt != DInfo.Decls.begin()) 6313 --BeginIt; 6314 6315 // If we are pointing at a top-level decl inside an objc container, we need 6316 // to backtrack until we find it otherwise we will fail to report that the 6317 // region overlaps with an objc container. 6318 while (BeginIt != DInfo.Decls.begin() && 6319 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 6320 ->isTopLevelDeclInObjCContainer()) 6321 --BeginIt; 6322 6323 ArrayRef<serialization::LocalDeclID>::iterator 6324 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 6325 EndLoc, DIDComp); 6326 if (EndIt != DInfo.Decls.end()) 6327 ++EndIt; 6328 6329 for (ArrayRef<serialization::LocalDeclID>::iterator 6330 DIt = BeginIt; DIt != EndIt; ++DIt) 6331 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 6332} 6333 6334namespace { 6335 /// \brief ModuleFile visitor used to perform name lookup into a 6336 /// declaration context. 6337 class DeclContextNameLookupVisitor { 6338 ASTReader &Reader; 6339 SmallVectorImpl<const DeclContext *> &Contexts; 6340 DeclarationName Name; 6341 SmallVectorImpl<NamedDecl *> &Decls; 6342 6343 public: 6344 DeclContextNameLookupVisitor(ASTReader &Reader, 6345 SmallVectorImpl<const DeclContext *> &Contexts, 6346 DeclarationName Name, 6347 SmallVectorImpl<NamedDecl *> &Decls) 6348 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { } 6349 6350 static bool visit(ModuleFile &M, void *UserData) { 6351 DeclContextNameLookupVisitor *This 6352 = static_cast<DeclContextNameLookupVisitor *>(UserData); 6353 6354 // Check whether we have any visible declaration information for 6355 // this context in this module. 6356 ModuleFile::DeclContextInfosMap::iterator Info; 6357 bool FoundInfo = false; 6358 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 6359 Info = M.DeclContextInfos.find(This->Contexts[I]); 6360 if (Info != M.DeclContextInfos.end() && 6361 Info->second.NameLookupTableData) { 6362 FoundInfo = true; 6363 break; 6364 } 6365 } 6366 6367 if (!FoundInfo) 6368 return false; 6369 6370 // Look for this name within this module. 6371 ASTDeclContextNameLookupTable *LookupTable = 6372 Info->second.NameLookupTableData; 6373 ASTDeclContextNameLookupTable::iterator Pos 6374 = LookupTable->find(This->Name); 6375 if (Pos == LookupTable->end()) 6376 return false; 6377 6378 bool FoundAnything = false; 6379 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 6380 for (; Data.first != Data.second; ++Data.first) { 6381 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first); 6382 if (!ND) 6383 continue; 6384 6385 if (ND->getDeclName() != This->Name) { 6386 // A name might be null because the decl's redeclarable part is 6387 // currently read before reading its name. The lookup is triggered by 6388 // building that decl (likely indirectly), and so it is later in the 6389 // sense of "already existing" and can be ignored here. 6390 continue; 6391 } 6392 6393 // Record this declaration. 6394 FoundAnything = true; 6395 This->Decls.push_back(ND); 6396 } 6397 6398 return FoundAnything; 6399 } 6400 }; 6401} 6402 6403/// \brief Retrieve the "definitive" module file for the definition of the 6404/// given declaration context, if there is one. 6405/// 6406/// The "definitive" module file is the only place where we need to look to 6407/// find information about the declarations within the given declaration 6408/// context. For example, C++ and Objective-C classes, C structs/unions, and 6409/// Objective-C protocols, categories, and extensions are all defined in a 6410/// single place in the source code, so they have definitive module files 6411/// associated with them. C++ namespaces, on the other hand, can have 6412/// definitions in multiple different module files. 6413/// 6414/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's 6415/// NDEBUG checking. 6416static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC, 6417 ASTReader &Reader) { 6418 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC)) 6419 return Reader.getOwningModuleFile(cast<Decl>(DefDC)); 6420 6421 return nullptr; 6422} 6423 6424bool 6425ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 6426 DeclarationName Name) { 6427 assert(DC->hasExternalVisibleStorage() && 6428 "DeclContext has no visible decls in storage"); 6429 if (!Name) 6430 return false; 6431 6432 SmallVector<NamedDecl *, 64> Decls; 6433 6434 // Compute the declaration contexts we need to look into. Multiple such 6435 // declaration contexts occur when two declaration contexts from disjoint 6436 // modules get merged, e.g., when two namespaces with the same name are 6437 // independently defined in separate modules. 6438 SmallVector<const DeclContext *, 2> Contexts; 6439 Contexts.push_back(DC); 6440 6441 if (DC->isNamespace()) { 6442 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 6443 if (Merged != MergedDecls.end()) { 6444 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 6445 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 6446 } 6447 } 6448 if (isa<CXXRecordDecl>(DC)) { 6449 auto Merged = MergedLookups.find(DC); 6450 if (Merged != MergedLookups.end()) 6451 Contexts.insert(Contexts.end(), Merged->second.begin(), 6452 Merged->second.end()); 6453 } 6454 6455 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls); 6456 6457 // If we can definitively determine which module file to look into, 6458 // only look there. Otherwise, look in all module files. 6459 ModuleFile *Definitive; 6460 if (Contexts.size() == 1 && 6461 (Definitive = getDefinitiveModuleFileFor(DC, *this))) { 6462 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor); 6463 } else { 6464 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor); 6465 } 6466 ++NumVisibleDeclContextsRead; 6467 SetExternalVisibleDeclsForName(DC, Name, Decls); 6468 return !Decls.empty(); 6469} 6470 6471namespace { 6472 /// \brief ModuleFile visitor used to retrieve all visible names in a 6473 /// declaration context. 6474 class DeclContextAllNamesVisitor { 6475 ASTReader &Reader; 6476 SmallVectorImpl<const DeclContext *> &Contexts; 6477 DeclsMap &Decls; 6478 bool VisitAll; 6479 6480 public: 6481 DeclContextAllNamesVisitor(ASTReader &Reader, 6482 SmallVectorImpl<const DeclContext *> &Contexts, 6483 DeclsMap &Decls, bool VisitAll) 6484 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { } 6485 6486 static bool visit(ModuleFile &M, void *UserData) { 6487 DeclContextAllNamesVisitor *This 6488 = static_cast<DeclContextAllNamesVisitor *>(UserData); 6489 6490 // Check whether we have any visible declaration information for 6491 // this context in this module. 6492 ModuleFile::DeclContextInfosMap::iterator Info; 6493 bool FoundInfo = false; 6494 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 6495 Info = M.DeclContextInfos.find(This->Contexts[I]); 6496 if (Info != M.DeclContextInfos.end() && 6497 Info->second.NameLookupTableData) { 6498 FoundInfo = true; 6499 break; 6500 } 6501 } 6502 6503 if (!FoundInfo) 6504 return false; 6505 6506 ASTDeclContextNameLookupTable *LookupTable = 6507 Info->second.NameLookupTableData; 6508 bool FoundAnything = false; 6509 for (ASTDeclContextNameLookupTable::data_iterator 6510 I = LookupTable->data_begin(), E = LookupTable->data_end(); 6511 I != E; 6512 ++I) { 6513 ASTDeclContextNameLookupTrait::data_type Data = *I; 6514 for (; Data.first != Data.second; ++Data.first) { 6515 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, 6516 *Data.first); 6517 if (!ND) 6518 continue; 6519 6520 // Record this declaration. 6521 FoundAnything = true; 6522 This->Decls[ND->getDeclName()].push_back(ND); 6523 } 6524 } 6525 6526 return FoundAnything && !This->VisitAll; 6527 } 6528 }; 6529} 6530 6531void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 6532 if (!DC->hasExternalVisibleStorage()) 6533 return; 6534 DeclsMap Decls; 6535 6536 // Compute the declaration contexts we need to look into. Multiple such 6537 // declaration contexts occur when two declaration contexts from disjoint 6538 // modules get merged, e.g., when two namespaces with the same name are 6539 // independently defined in separate modules. 6540 SmallVector<const DeclContext *, 2> Contexts; 6541 Contexts.push_back(DC); 6542 6543 if (DC->isNamespace()) { 6544 MergedDeclsMap::iterator Merged 6545 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 6546 if (Merged != MergedDecls.end()) { 6547 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 6548 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 6549 } 6550 } 6551 6552 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls, 6553 /*VisitAll=*/DC->isFileContext()); 6554 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor); 6555 ++NumVisibleDeclContextsRead; 6556 6557 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 6558 SetExternalVisibleDeclsForName(DC, I->first, I->second); 6559 } 6560 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 6561} 6562 6563/// \brief Under non-PCH compilation the consumer receives the objc methods 6564/// before receiving the implementation, and codegen depends on this. 6565/// We simulate this by deserializing and passing to consumer the methods of the 6566/// implementation before passing the deserialized implementation decl. 6567static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 6568 ASTConsumer *Consumer) { 6569 assert(ImplD && Consumer); 6570 6571 for (auto *I : ImplD->methods()) 6572 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 6573 6574 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 6575} 6576 6577void ASTReader::PassInterestingDeclsToConsumer() { 6578 assert(Consumer); 6579 6580 if (PassingDeclsToConsumer) 6581 return; 6582 6583 // Guard variable to avoid recursively redoing the process of passing 6584 // decls to consumer. 6585 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 6586 true); 6587 6588 while (!InterestingDecls.empty()) { 6589 Decl *D = InterestingDecls.front(); 6590 InterestingDecls.pop_front(); 6591 6592 PassInterestingDeclToConsumer(D); 6593 } 6594} 6595 6596void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 6597 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 6598 PassObjCImplDeclToConsumer(ImplD, Consumer); 6599 else 6600 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 6601} 6602 6603void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 6604 this->Consumer = Consumer; 6605 6606 if (!Consumer) 6607 return; 6608 6609 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) { 6610 // Force deserialization of this decl, which will cause it to be queued for 6611 // passing to the consumer. 6612 GetDecl(EagerlyDeserializedDecls[I]); 6613 } 6614 EagerlyDeserializedDecls.clear(); 6615 6616 PassInterestingDeclsToConsumer(); 6617} 6618 6619void ASTReader::PrintStats() { 6620 std::fprintf(stderr, "*** AST File Statistics:\n"); 6621 6622 unsigned NumTypesLoaded 6623 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 6624 QualType()); 6625 unsigned NumDeclsLoaded 6626 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 6627 (Decl *)nullptr); 6628 unsigned NumIdentifiersLoaded 6629 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 6630 IdentifiersLoaded.end(), 6631 (IdentifierInfo *)nullptr); 6632 unsigned NumMacrosLoaded 6633 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 6634 MacrosLoaded.end(), 6635 (MacroInfo *)nullptr); 6636 unsigned NumSelectorsLoaded 6637 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 6638 SelectorsLoaded.end(), 6639 Selector()); 6640 6641 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 6642 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 6643 NumSLocEntriesRead, TotalNumSLocEntries, 6644 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 6645 if (!TypesLoaded.empty()) 6646 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 6647 NumTypesLoaded, (unsigned)TypesLoaded.size(), 6648 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 6649 if (!DeclsLoaded.empty()) 6650 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 6651 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 6652 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 6653 if (!IdentifiersLoaded.empty()) 6654 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 6655 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 6656 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 6657 if (!MacrosLoaded.empty()) 6658 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 6659 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 6660 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 6661 if (!SelectorsLoaded.empty()) 6662 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 6663 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 6664 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 6665 if (TotalNumStatements) 6666 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 6667 NumStatementsRead, TotalNumStatements, 6668 ((float)NumStatementsRead/TotalNumStatements * 100)); 6669 if (TotalNumMacros) 6670 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 6671 NumMacrosRead, TotalNumMacros, 6672 ((float)NumMacrosRead/TotalNumMacros * 100)); 6673 if (TotalLexicalDeclContexts) 6674 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 6675 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 6676 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 6677 * 100)); 6678 if (TotalVisibleDeclContexts) 6679 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 6680 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 6681 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 6682 * 100)); 6683 if (TotalNumMethodPoolEntries) { 6684 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 6685 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 6686 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 6687 * 100)); 6688 } 6689 if (NumMethodPoolLookups) { 6690 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 6691 NumMethodPoolHits, NumMethodPoolLookups, 6692 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 6693 } 6694 if (NumMethodPoolTableLookups) { 6695 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 6696 NumMethodPoolTableHits, NumMethodPoolTableLookups, 6697 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 6698 * 100.0)); 6699 } 6700 6701 if (NumIdentifierLookupHits) { 6702 std::fprintf(stderr, 6703 " %u / %u identifier table lookups succeeded (%f%%)\n", 6704 NumIdentifierLookupHits, NumIdentifierLookups, 6705 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 6706 } 6707 6708 if (GlobalIndex) { 6709 std::fprintf(stderr, "\n"); 6710 GlobalIndex->printStats(); 6711 } 6712 6713 std::fprintf(stderr, "\n"); 6714 dump(); 6715 std::fprintf(stderr, "\n"); 6716} 6717 6718template<typename Key, typename ModuleFile, unsigned InitialCapacity> 6719static void 6720dumpModuleIDMap(StringRef Name, 6721 const ContinuousRangeMap<Key, ModuleFile *, 6722 InitialCapacity> &Map) { 6723 if (Map.begin() == Map.end()) 6724 return; 6725 6726 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType; 6727 llvm::errs() << Name << ":\n"; 6728 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 6729 I != IEnd; ++I) { 6730 llvm::errs() << " " << I->first << " -> " << I->second->FileName 6731 << "\n"; 6732 } 6733} 6734 6735void ASTReader::dump() { 6736 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 6737 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 6738 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 6739 dumpModuleIDMap("Global type map", GlobalTypeMap); 6740 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 6741 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 6742 dumpModuleIDMap("Global macro map", GlobalMacroMap); 6743 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 6744 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 6745 dumpModuleIDMap("Global preprocessed entity map", 6746 GlobalPreprocessedEntityMap); 6747 6748 llvm::errs() << "\n*** PCH/Modules Loaded:"; 6749 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 6750 MEnd = ModuleMgr.end(); 6751 M != MEnd; ++M) 6752 (*M)->dump(); 6753} 6754 6755/// Return the amount of memory used by memory buffers, breaking down 6756/// by heap-backed versus mmap'ed memory. 6757void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 6758 for (ModuleConstIterator I = ModuleMgr.begin(), 6759 E = ModuleMgr.end(); I != E; ++I) { 6760 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) { 6761 size_t bytes = buf->getBufferSize(); 6762 switch (buf->getBufferKind()) { 6763 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 6764 sizes.malloc_bytes += bytes; 6765 break; 6766 case llvm::MemoryBuffer::MemoryBuffer_MMap: 6767 sizes.mmap_bytes += bytes; 6768 break; 6769 } 6770 } 6771 } 6772} 6773 6774void ASTReader::InitializeSema(Sema &S) { 6775 SemaObj = &S; 6776 S.addExternalSource(this); 6777 6778 // Makes sure any declarations that were deserialized "too early" 6779 // still get added to the identifier's declaration chains. 6780 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 6781 pushExternalDeclIntoScope(PreloadedDecls[I], 6782 PreloadedDecls[I]->getDeclName()); 6783 } 6784 PreloadedDecls.clear(); 6785 6786 // FIXME: What happens if these are changed by a module import? 6787 if (!FPPragmaOptions.empty()) { 6788 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 6789 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 6790 } 6791 6792 // FIXME: What happens if these are changed by a module import? 6793 if (!OpenCLExtensions.empty()) { 6794 unsigned I = 0; 6795#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 6796#include "clang/Basic/OpenCLExtensions.def" 6797 6798 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 6799 } 6800 6801 UpdateSema(); 6802} 6803 6804void ASTReader::UpdateSema() { 6805 assert(SemaObj && "no Sema to update"); 6806 6807 // Load the offsets of the declarations that Sema references. 6808 // They will be lazily deserialized when needed. 6809 if (!SemaDeclRefs.empty()) { 6810 assert(SemaDeclRefs.size() % 2 == 0); 6811 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) { 6812 if (!SemaObj->StdNamespace) 6813 SemaObj->StdNamespace = SemaDeclRefs[I]; 6814 if (!SemaObj->StdBadAlloc) 6815 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 6816 } 6817 SemaDeclRefs.clear(); 6818 } 6819 6820 // Update the state of 'pragma clang optimize'. Use the same API as if we had 6821 // encountered the pragma in the source. 6822 if(OptimizeOffPragmaLocation.isValid()) 6823 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation); 6824} 6825 6826IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 6827 // Note that we are loading an identifier. 6828 Deserializing AnIdentifier(this); 6829 StringRef Name(NameStart, NameEnd - NameStart); 6830 6831 // If there is a global index, look there first to determine which modules 6832 // provably do not have any results for this identifier. 6833 GlobalModuleIndex::HitSet Hits; 6834 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 6835 if (!loadGlobalIndex()) { 6836 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 6837 HitsPtr = &Hits; 6838 } 6839 } 6840 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 6841 NumIdentifierLookups, 6842 NumIdentifierLookupHits); 6843 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); 6844 IdentifierInfo *II = Visitor.getIdentifierInfo(); 6845 markIdentifierUpToDate(II); 6846 return II; 6847} 6848 6849namespace clang { 6850 /// \brief An identifier-lookup iterator that enumerates all of the 6851 /// identifiers stored within a set of AST files. 6852 class ASTIdentifierIterator : public IdentifierIterator { 6853 /// \brief The AST reader whose identifiers are being enumerated. 6854 const ASTReader &Reader; 6855 6856 /// \brief The current index into the chain of AST files stored in 6857 /// the AST reader. 6858 unsigned Index; 6859 6860 /// \brief The current position within the identifier lookup table 6861 /// of the current AST file. 6862 ASTIdentifierLookupTable::key_iterator Current; 6863 6864 /// \brief The end position within the identifier lookup table of 6865 /// the current AST file. 6866 ASTIdentifierLookupTable::key_iterator End; 6867 6868 public: 6869 explicit ASTIdentifierIterator(const ASTReader &Reader); 6870 6871 StringRef Next() override; 6872 }; 6873} 6874 6875ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 6876 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) { 6877 ASTIdentifierLookupTable *IdTable 6878 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable; 6879 Current = IdTable->key_begin(); 6880 End = IdTable->key_end(); 6881} 6882 6883StringRef ASTIdentifierIterator::Next() { 6884 while (Current == End) { 6885 // If we have exhausted all of our AST files, we're done. 6886 if (Index == 0) 6887 return StringRef(); 6888 6889 --Index; 6890 ASTIdentifierLookupTable *IdTable 6891 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index]. 6892 IdentifierLookupTable; 6893 Current = IdTable->key_begin(); 6894 End = IdTable->key_end(); 6895 } 6896 6897 // We have any identifiers remaining in the current AST file; return 6898 // the next one. 6899 StringRef Result = *Current; 6900 ++Current; 6901 return Result; 6902} 6903 6904IdentifierIterator *ASTReader::getIdentifiers() { 6905 if (!loadGlobalIndex()) 6906 return GlobalIndex->createIdentifierIterator(); 6907 6908 return new ASTIdentifierIterator(*this); 6909} 6910 6911namespace clang { namespace serialization { 6912 class ReadMethodPoolVisitor { 6913 ASTReader &Reader; 6914 Selector Sel; 6915 unsigned PriorGeneration; 6916 unsigned InstanceBits; 6917 unsigned FactoryBits; 6918 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 6919 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 6920 6921 public: 6922 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 6923 unsigned PriorGeneration) 6924 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration), 6925 InstanceBits(0), FactoryBits(0) { } 6926 6927 static bool visit(ModuleFile &M, void *UserData) { 6928 ReadMethodPoolVisitor *This 6929 = static_cast<ReadMethodPoolVisitor *>(UserData); 6930 6931 if (!M.SelectorLookupTable) 6932 return false; 6933 6934 // If we've already searched this module file, skip it now. 6935 if (M.Generation <= This->PriorGeneration) 6936 return true; 6937 6938 ++This->Reader.NumMethodPoolTableLookups; 6939 ASTSelectorLookupTable *PoolTable 6940 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 6941 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel); 6942 if (Pos == PoolTable->end()) 6943 return false; 6944 6945 ++This->Reader.NumMethodPoolTableHits; 6946 ++This->Reader.NumSelectorsRead; 6947 // FIXME: Not quite happy with the statistics here. We probably should 6948 // disable this tracking when called via LoadSelector. 6949 // Also, should entries without methods count as misses? 6950 ++This->Reader.NumMethodPoolEntriesRead; 6951 ASTSelectorLookupTrait::data_type Data = *Pos; 6952 if (This->Reader.DeserializationListener) 6953 This->Reader.DeserializationListener->SelectorRead(Data.ID, 6954 This->Sel); 6955 6956 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 6957 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 6958 This->InstanceBits = Data.InstanceBits; 6959 This->FactoryBits = Data.FactoryBits; 6960 return true; 6961 } 6962 6963 /// \brief Retrieve the instance methods found by this visitor. 6964 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 6965 return InstanceMethods; 6966 } 6967 6968 /// \brief Retrieve the instance methods found by this visitor. 6969 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 6970 return FactoryMethods; 6971 } 6972 6973 unsigned getInstanceBits() const { return InstanceBits; } 6974 unsigned getFactoryBits() const { return FactoryBits; } 6975 }; 6976} } // end namespace clang::serialization 6977 6978/// \brief Add the given set of methods to the method list. 6979static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 6980 ObjCMethodList &List) { 6981 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 6982 S.addMethodToGlobalList(&List, Methods[I]); 6983 } 6984} 6985 6986void ASTReader::ReadMethodPool(Selector Sel) { 6987 // Get the selector generation and update it to the current generation. 6988 unsigned &Generation = SelectorGeneration[Sel]; 6989 unsigned PriorGeneration = Generation; 6990 Generation = getGeneration(); 6991 6992 // Search for methods defined with this selector. 6993 ++NumMethodPoolLookups; 6994 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 6995 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor); 6996 6997 if (Visitor.getInstanceMethods().empty() && 6998 Visitor.getFactoryMethods().empty()) 6999 return; 7000 7001 ++NumMethodPoolHits; 7002 7003 if (!getSema()) 7004 return; 7005 7006 Sema &S = *getSema(); 7007 Sema::GlobalMethodPool::iterator Pos 7008 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 7009 7010 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 7011 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 7012 Pos->second.first.setBits(Visitor.getInstanceBits()); 7013 Pos->second.second.setBits(Visitor.getFactoryBits()); 7014} 7015 7016void ASTReader::ReadKnownNamespaces( 7017 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 7018 Namespaces.clear(); 7019 7020 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 7021 if (NamespaceDecl *Namespace 7022 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 7023 Namespaces.push_back(Namespace); 7024 } 7025} 7026 7027void ASTReader::ReadUndefinedButUsed( 7028 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) { 7029 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 7030 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 7031 SourceLocation Loc = 7032 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 7033 Undefined.insert(std::make_pair(D, Loc)); 7034 } 7035} 7036 7037void ASTReader::ReadTentativeDefinitions( 7038 SmallVectorImpl<VarDecl *> &TentativeDefs) { 7039 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 7040 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 7041 if (Var) 7042 TentativeDefs.push_back(Var); 7043 } 7044 TentativeDefinitions.clear(); 7045} 7046 7047void ASTReader::ReadUnusedFileScopedDecls( 7048 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 7049 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 7050 DeclaratorDecl *D 7051 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 7052 if (D) 7053 Decls.push_back(D); 7054 } 7055 UnusedFileScopedDecls.clear(); 7056} 7057 7058void ASTReader::ReadDelegatingConstructors( 7059 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 7060 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 7061 CXXConstructorDecl *D 7062 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 7063 if (D) 7064 Decls.push_back(D); 7065 } 7066 DelegatingCtorDecls.clear(); 7067} 7068 7069void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 7070 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 7071 TypedefNameDecl *D 7072 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 7073 if (D) 7074 Decls.push_back(D); 7075 } 7076 ExtVectorDecls.clear(); 7077} 7078 7079void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) { 7080 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { 7081 CXXRecordDecl *D 7082 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I])); 7083 if (D) 7084 Decls.push_back(D); 7085 } 7086 DynamicClasses.clear(); 7087} 7088 7089void 7090ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) { 7091 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) { 7092 NamedDecl *D 7093 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I])); 7094 if (D) 7095 Decls.push_back(D); 7096 } 7097 LocallyScopedExternCDecls.clear(); 7098} 7099 7100void ASTReader::ReadReferencedSelectors( 7101 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) { 7102 if (ReferencedSelectorsData.empty()) 7103 return; 7104 7105 // If there are @selector references added them to its pool. This is for 7106 // implementation of -Wselector. 7107 unsigned int DataSize = ReferencedSelectorsData.size()-1; 7108 unsigned I = 0; 7109 while (I < DataSize) { 7110 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 7111 SourceLocation SelLoc 7112 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 7113 Sels.push_back(std::make_pair(Sel, SelLoc)); 7114 } 7115 ReferencedSelectorsData.clear(); 7116} 7117 7118void ASTReader::ReadWeakUndeclaredIdentifiers( 7119 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) { 7120 if (WeakUndeclaredIdentifiers.empty()) 7121 return; 7122 7123 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 7124 IdentifierInfo *WeakId 7125 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 7126 IdentifierInfo *AliasId 7127 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 7128 SourceLocation Loc 7129 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 7130 bool Used = WeakUndeclaredIdentifiers[I++]; 7131 WeakInfo WI(AliasId, Loc); 7132 WI.setUsed(Used); 7133 WeakIDs.push_back(std::make_pair(WeakId, WI)); 7134 } 7135 WeakUndeclaredIdentifiers.clear(); 7136} 7137 7138void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 7139 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 7140 ExternalVTableUse VT; 7141 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 7142 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 7143 VT.DefinitionRequired = VTableUses[Idx++]; 7144 VTables.push_back(VT); 7145 } 7146 7147 VTableUses.clear(); 7148} 7149 7150void ASTReader::ReadPendingInstantiations( 7151 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) { 7152 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 7153 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 7154 SourceLocation Loc 7155 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 7156 7157 Pending.push_back(std::make_pair(D, Loc)); 7158 } 7159 PendingInstantiations.clear(); 7160} 7161 7162void ASTReader::ReadLateParsedTemplates( 7163 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) { 7164 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 7165 /* In loop */) { 7166 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 7167 7168 LateParsedTemplate *LT = new LateParsedTemplate; 7169 LT->D = GetDecl(LateParsedTemplates[Idx++]); 7170 7171 ModuleFile *F = getOwningModuleFile(LT->D); 7172 assert(F && "No module"); 7173 7174 unsigned TokN = LateParsedTemplates[Idx++]; 7175 LT->Toks.reserve(TokN); 7176 for (unsigned T = 0; T < TokN; ++T) 7177 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 7178 7179 LPTMap[FD] = LT; 7180 } 7181 7182 LateParsedTemplates.clear(); 7183} 7184 7185void ASTReader::LoadSelector(Selector Sel) { 7186 // It would be complicated to avoid reading the methods anyway. So don't. 7187 ReadMethodPool(Sel); 7188} 7189 7190void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 7191 assert(ID && "Non-zero identifier ID required"); 7192 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 7193 IdentifiersLoaded[ID - 1] = II; 7194 if (DeserializationListener) 7195 DeserializationListener->IdentifierRead(ID, II); 7196} 7197 7198/// \brief Set the globally-visible declarations associated with the given 7199/// identifier. 7200/// 7201/// If the AST reader is currently in a state where the given declaration IDs 7202/// cannot safely be resolved, they are queued until it is safe to resolve 7203/// them. 7204/// 7205/// \param II an IdentifierInfo that refers to one or more globally-visible 7206/// declarations. 7207/// 7208/// \param DeclIDs the set of declaration IDs with the name @p II that are 7209/// visible at global scope. 7210/// 7211/// \param Decls if non-null, this vector will be populated with the set of 7212/// deserialized declarations. These declarations will not be pushed into 7213/// scope. 7214void 7215ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 7216 const SmallVectorImpl<uint32_t> &DeclIDs, 7217 SmallVectorImpl<Decl *> *Decls) { 7218 if (NumCurrentElementsDeserializing && !Decls) { 7219 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 7220 return; 7221 } 7222 7223 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 7224 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 7225 if (SemaObj) { 7226 // If we're simply supposed to record the declarations, do so now. 7227 if (Decls) { 7228 Decls->push_back(D); 7229 continue; 7230 } 7231 7232 // Introduce this declaration into the translation-unit scope 7233 // and add it to the declaration chain for this identifier, so 7234 // that (unqualified) name lookup will find it. 7235 pushExternalDeclIntoScope(D, II); 7236 } else { 7237 // Queue this declaration so that it will be added to the 7238 // translation unit scope and identifier's declaration chain 7239 // once a Sema object is known. 7240 PreloadedDecls.push_back(D); 7241 } 7242 } 7243} 7244 7245IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 7246 if (ID == 0) 7247 return nullptr; 7248 7249 if (IdentifiersLoaded.empty()) { 7250 Error("no identifier table in AST file"); 7251 return nullptr; 7252 } 7253 7254 ID -= 1; 7255 if (!IdentifiersLoaded[ID]) { 7256 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 7257 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 7258 ModuleFile *M = I->second; 7259 unsigned Index = ID - M->BaseIdentifierID; 7260 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 7261 7262 // All of the strings in the AST file are preceded by a 16-bit length. 7263 // Extract that 16-bit length to avoid having to execute strlen(). 7264 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 7265 // unsigned integers. This is important to avoid integer overflow when 7266 // we cast them to 'unsigned'. 7267 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 7268 unsigned StrLen = (((unsigned) StrLenPtr[0]) 7269 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 7270 IdentifiersLoaded[ID] 7271 = &PP.getIdentifierTable().get(StringRef(Str, StrLen)); 7272 if (DeserializationListener) 7273 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 7274 } 7275 7276 return IdentifiersLoaded[ID]; 7277} 7278 7279IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 7280 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 7281} 7282 7283IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 7284 if (LocalID < NUM_PREDEF_IDENT_IDS) 7285 return LocalID; 7286 7287 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7288 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 7289 assert(I != M.IdentifierRemap.end() 7290 && "Invalid index into identifier index remap"); 7291 7292 return LocalID + I->second; 7293} 7294 7295MacroInfo *ASTReader::getMacro(MacroID ID) { 7296 if (ID == 0) 7297 return nullptr; 7298 7299 if (MacrosLoaded.empty()) { 7300 Error("no macro table in AST file"); 7301 return nullptr; 7302 } 7303 7304 ID -= NUM_PREDEF_MACRO_IDS; 7305 if (!MacrosLoaded[ID]) { 7306 GlobalMacroMapType::iterator I 7307 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 7308 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 7309 ModuleFile *M = I->second; 7310 unsigned Index = ID - M->BaseMacroID; 7311 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]); 7312 7313 if (DeserializationListener) 7314 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 7315 MacrosLoaded[ID]); 7316 } 7317 7318 return MacrosLoaded[ID]; 7319} 7320 7321MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 7322 if (LocalID < NUM_PREDEF_MACRO_IDS) 7323 return LocalID; 7324 7325 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7326 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 7327 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 7328 7329 return LocalID + I->second; 7330} 7331 7332serialization::SubmoduleID 7333ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 7334 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 7335 return LocalID; 7336 7337 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7338 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 7339 assert(I != M.SubmoduleRemap.end() 7340 && "Invalid index into submodule index remap"); 7341 7342 return LocalID + I->second; 7343} 7344 7345Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 7346 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 7347 assert(GlobalID == 0 && "Unhandled global submodule ID"); 7348 return nullptr; 7349 } 7350 7351 if (GlobalID > SubmodulesLoaded.size()) { 7352 Error("submodule ID out of range in AST file"); 7353 return nullptr; 7354 } 7355 7356 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 7357} 7358 7359Module *ASTReader::getModule(unsigned ID) { 7360 return getSubmodule(ID); 7361} 7362 7363Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 7364 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 7365} 7366 7367Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 7368 if (ID == 0) 7369 return Selector(); 7370 7371 if (ID > SelectorsLoaded.size()) { 7372 Error("selector ID out of range in AST file"); 7373 return Selector(); 7374 } 7375 7376 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 7377 // Load this selector from the selector table. 7378 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 7379 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 7380 ModuleFile &M = *I->second; 7381 ASTSelectorLookupTrait Trait(*this, M); 7382 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 7383 SelectorsLoaded[ID - 1] = 7384 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 7385 if (DeserializationListener) 7386 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 7387 } 7388 7389 return SelectorsLoaded[ID - 1]; 7390} 7391 7392Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 7393 return DecodeSelector(ID); 7394} 7395 7396uint32_t ASTReader::GetNumExternalSelectors() { 7397 // ID 0 (the null selector) is considered an external selector. 7398 return getTotalNumSelectors() + 1; 7399} 7400 7401serialization::SelectorID 7402ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 7403 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 7404 return LocalID; 7405 7406 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7407 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 7408 assert(I != M.SelectorRemap.end() 7409 && "Invalid index into selector index remap"); 7410 7411 return LocalID + I->second; 7412} 7413 7414DeclarationName 7415ASTReader::ReadDeclarationName(ModuleFile &F, 7416 const RecordData &Record, unsigned &Idx) { 7417 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 7418 switch (Kind) { 7419 case DeclarationName::Identifier: 7420 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 7421 7422 case DeclarationName::ObjCZeroArgSelector: 7423 case DeclarationName::ObjCOneArgSelector: 7424 case DeclarationName::ObjCMultiArgSelector: 7425 return DeclarationName(ReadSelector(F, Record, Idx)); 7426 7427 case DeclarationName::CXXConstructorName: 7428 return Context.DeclarationNames.getCXXConstructorName( 7429 Context.getCanonicalType(readType(F, Record, Idx))); 7430 7431 case DeclarationName::CXXDestructorName: 7432 return Context.DeclarationNames.getCXXDestructorName( 7433 Context.getCanonicalType(readType(F, Record, Idx))); 7434 7435 case DeclarationName::CXXConversionFunctionName: 7436 return Context.DeclarationNames.getCXXConversionFunctionName( 7437 Context.getCanonicalType(readType(F, Record, Idx))); 7438 7439 case DeclarationName::CXXOperatorName: 7440 return Context.DeclarationNames.getCXXOperatorName( 7441 (OverloadedOperatorKind)Record[Idx++]); 7442 7443 case DeclarationName::CXXLiteralOperatorName: 7444 return Context.DeclarationNames.getCXXLiteralOperatorName( 7445 GetIdentifierInfo(F, Record, Idx)); 7446 7447 case DeclarationName::CXXUsingDirective: 7448 return DeclarationName::getUsingDirectiveName(); 7449 } 7450 7451 llvm_unreachable("Invalid NameKind!"); 7452} 7453 7454void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 7455 DeclarationNameLoc &DNLoc, 7456 DeclarationName Name, 7457 const RecordData &Record, unsigned &Idx) { 7458 switch (Name.getNameKind()) { 7459 case DeclarationName::CXXConstructorName: 7460 case DeclarationName::CXXDestructorName: 7461 case DeclarationName::CXXConversionFunctionName: 7462 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 7463 break; 7464 7465 case DeclarationName::CXXOperatorName: 7466 DNLoc.CXXOperatorName.BeginOpNameLoc 7467 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 7468 DNLoc.CXXOperatorName.EndOpNameLoc 7469 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 7470 break; 7471 7472 case DeclarationName::CXXLiteralOperatorName: 7473 DNLoc.CXXLiteralOperatorName.OpNameLoc 7474 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 7475 break; 7476 7477 case DeclarationName::Identifier: 7478 case DeclarationName::ObjCZeroArgSelector: 7479 case DeclarationName::ObjCOneArgSelector: 7480 case DeclarationName::ObjCMultiArgSelector: 7481 case DeclarationName::CXXUsingDirective: 7482 break; 7483 } 7484} 7485 7486void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 7487 DeclarationNameInfo &NameInfo, 7488 const RecordData &Record, unsigned &Idx) { 7489 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 7490 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 7491 DeclarationNameLoc DNLoc; 7492 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 7493 NameInfo.setInfo(DNLoc); 7494} 7495 7496void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 7497 const RecordData &Record, unsigned &Idx) { 7498 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 7499 unsigned NumTPLists = Record[Idx++]; 7500 Info.NumTemplParamLists = NumTPLists; 7501 if (NumTPLists) { 7502 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 7503 for (unsigned i=0; i != NumTPLists; ++i) 7504 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 7505 } 7506} 7507 7508TemplateName 7509ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 7510 unsigned &Idx) { 7511 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 7512 switch (Kind) { 7513 case TemplateName::Template: 7514 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 7515 7516 case TemplateName::OverloadedTemplate: { 7517 unsigned size = Record[Idx++]; 7518 UnresolvedSet<8> Decls; 7519 while (size--) 7520 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 7521 7522 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 7523 } 7524 7525 case TemplateName::QualifiedTemplate: { 7526 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 7527 bool hasTemplKeyword = Record[Idx++]; 7528 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 7529 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 7530 } 7531 7532 case TemplateName::DependentTemplate: { 7533 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 7534 if (Record[Idx++]) // isIdentifier 7535 return Context.getDependentTemplateName(NNS, 7536 GetIdentifierInfo(F, Record, 7537 Idx)); 7538 return Context.getDependentTemplateName(NNS, 7539 (OverloadedOperatorKind)Record[Idx++]); 7540 } 7541 7542 case TemplateName::SubstTemplateTemplateParm: { 7543 TemplateTemplateParmDecl *param 7544 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 7545 if (!param) return TemplateName(); 7546 TemplateName replacement = ReadTemplateName(F, Record, Idx); 7547 return Context.getSubstTemplateTemplateParm(param, replacement); 7548 } 7549 7550 case TemplateName::SubstTemplateTemplateParmPack: { 7551 TemplateTemplateParmDecl *Param 7552 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 7553 if (!Param) 7554 return TemplateName(); 7555 7556 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 7557 if (ArgPack.getKind() != TemplateArgument::Pack) 7558 return TemplateName(); 7559 7560 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 7561 } 7562 } 7563 7564 llvm_unreachable("Unhandled template name kind!"); 7565} 7566 7567TemplateArgument 7568ASTReader::ReadTemplateArgument(ModuleFile &F, 7569 const RecordData &Record, unsigned &Idx) { 7570 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 7571 switch (Kind) { 7572 case TemplateArgument::Null: 7573 return TemplateArgument(); 7574 case TemplateArgument::Type: 7575 return TemplateArgument(readType(F, Record, Idx)); 7576 case TemplateArgument::Declaration: { 7577 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 7578 bool ForReferenceParam = Record[Idx++]; 7579 return TemplateArgument(D, ForReferenceParam); 7580 } 7581 case TemplateArgument::NullPtr: 7582 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 7583 case TemplateArgument::Integral: { 7584 llvm::APSInt Value = ReadAPSInt(Record, Idx); 7585 QualType T = readType(F, Record, Idx); 7586 return TemplateArgument(Context, Value, T); 7587 } 7588 case TemplateArgument::Template: 7589 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 7590 case TemplateArgument::TemplateExpansion: { 7591 TemplateName Name = ReadTemplateName(F, Record, Idx); 7592 Optional<unsigned> NumTemplateExpansions; 7593 if (unsigned NumExpansions = Record[Idx++]) 7594 NumTemplateExpansions = NumExpansions - 1; 7595 return TemplateArgument(Name, NumTemplateExpansions); 7596 } 7597 case TemplateArgument::Expression: 7598 return TemplateArgument(ReadExpr(F)); 7599 case TemplateArgument::Pack: { 7600 unsigned NumArgs = Record[Idx++]; 7601 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 7602 for (unsigned I = 0; I != NumArgs; ++I) 7603 Args[I] = ReadTemplateArgument(F, Record, Idx); 7604 return TemplateArgument(Args, NumArgs); 7605 } 7606 } 7607 7608 llvm_unreachable("Unhandled template argument kind!"); 7609} 7610 7611TemplateParameterList * 7612ASTReader::ReadTemplateParameterList(ModuleFile &F, 7613 const RecordData &Record, unsigned &Idx) { 7614 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 7615 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 7616 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 7617 7618 unsigned NumParams = Record[Idx++]; 7619 SmallVector<NamedDecl *, 16> Params; 7620 Params.reserve(NumParams); 7621 while (NumParams--) 7622 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 7623 7624 TemplateParameterList* TemplateParams = 7625 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 7626 Params.data(), Params.size(), RAngleLoc); 7627 return TemplateParams; 7628} 7629 7630void 7631ASTReader:: 7632ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs, 7633 ModuleFile &F, const RecordData &Record, 7634 unsigned &Idx) { 7635 unsigned NumTemplateArgs = Record[Idx++]; 7636 TemplArgs.reserve(NumTemplateArgs); 7637 while (NumTemplateArgs--) 7638 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 7639} 7640 7641/// \brief Read a UnresolvedSet structure. 7642void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set, 7643 const RecordData &Record, unsigned &Idx) { 7644 unsigned NumDecls = Record[Idx++]; 7645 Set.reserve(Context, NumDecls); 7646 while (NumDecls--) { 7647 DeclID ID = ReadDeclID(F, Record, Idx); 7648 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 7649 Set.addLazyDecl(Context, ID, AS); 7650 } 7651} 7652 7653CXXBaseSpecifier 7654ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 7655 const RecordData &Record, unsigned &Idx) { 7656 bool isVirtual = static_cast<bool>(Record[Idx++]); 7657 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 7658 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 7659 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 7660 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 7661 SourceRange Range = ReadSourceRange(F, Record, Idx); 7662 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 7663 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 7664 EllipsisLoc); 7665 Result.setInheritConstructors(inheritConstructors); 7666 return Result; 7667} 7668 7669std::pair<CXXCtorInitializer **, unsigned> 7670ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 7671 unsigned &Idx) { 7672 CXXCtorInitializer **CtorInitializers = nullptr; 7673 unsigned NumInitializers = Record[Idx++]; 7674 if (NumInitializers) { 7675 CtorInitializers 7676 = new (Context) CXXCtorInitializer*[NumInitializers]; 7677 for (unsigned i=0; i != NumInitializers; ++i) { 7678 TypeSourceInfo *TInfo = nullptr; 7679 bool IsBaseVirtual = false; 7680 FieldDecl *Member = nullptr; 7681 IndirectFieldDecl *IndirectMember = nullptr; 7682 7683 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 7684 switch (Type) { 7685 case CTOR_INITIALIZER_BASE: 7686 TInfo = GetTypeSourceInfo(F, Record, Idx); 7687 IsBaseVirtual = Record[Idx++]; 7688 break; 7689 7690 case CTOR_INITIALIZER_DELEGATING: 7691 TInfo = GetTypeSourceInfo(F, Record, Idx); 7692 break; 7693 7694 case CTOR_INITIALIZER_MEMBER: 7695 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 7696 break; 7697 7698 case CTOR_INITIALIZER_INDIRECT_MEMBER: 7699 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 7700 break; 7701 } 7702 7703 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 7704 Expr *Init = ReadExpr(F); 7705 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 7706 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 7707 bool IsWritten = Record[Idx++]; 7708 unsigned SourceOrderOrNumArrayIndices; 7709 SmallVector<VarDecl *, 8> Indices; 7710 if (IsWritten) { 7711 SourceOrderOrNumArrayIndices = Record[Idx++]; 7712 } else { 7713 SourceOrderOrNumArrayIndices = Record[Idx++]; 7714 Indices.reserve(SourceOrderOrNumArrayIndices); 7715 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 7716 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 7717 } 7718 7719 CXXCtorInitializer *BOMInit; 7720 if (Type == CTOR_INITIALIZER_BASE) { 7721 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual, 7722 LParenLoc, Init, RParenLoc, 7723 MemberOrEllipsisLoc); 7724 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 7725 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc, 7726 Init, RParenLoc); 7727 } else if (IsWritten) { 7728 if (Member) 7729 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, 7730 LParenLoc, Init, RParenLoc); 7731 else 7732 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 7733 MemberOrEllipsisLoc, LParenLoc, 7734 Init, RParenLoc); 7735 } else { 7736 if (IndirectMember) { 7737 assert(Indices.empty() && "Indirect field improperly initialized"); 7738 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 7739 MemberOrEllipsisLoc, LParenLoc, 7740 Init, RParenLoc); 7741 } else { 7742 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc, 7743 LParenLoc, Init, RParenLoc, 7744 Indices.data(), Indices.size()); 7745 } 7746 } 7747 7748 if (IsWritten) 7749 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 7750 CtorInitializers[i] = BOMInit; 7751 } 7752 } 7753 7754 return std::make_pair(CtorInitializers, NumInitializers); 7755} 7756 7757NestedNameSpecifier * 7758ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 7759 const RecordData &Record, unsigned &Idx) { 7760 unsigned N = Record[Idx++]; 7761 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr; 7762 for (unsigned I = 0; I != N; ++I) { 7763 NestedNameSpecifier::SpecifierKind Kind 7764 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 7765 switch (Kind) { 7766 case NestedNameSpecifier::Identifier: { 7767 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 7768 NNS = NestedNameSpecifier::Create(Context, Prev, II); 7769 break; 7770 } 7771 7772 case NestedNameSpecifier::Namespace: { 7773 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 7774 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 7775 break; 7776 } 7777 7778 case NestedNameSpecifier::NamespaceAlias: { 7779 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 7780 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 7781 break; 7782 } 7783 7784 case NestedNameSpecifier::TypeSpec: 7785 case NestedNameSpecifier::TypeSpecWithTemplate: { 7786 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 7787 if (!T) 7788 return nullptr; 7789 7790 bool Template = Record[Idx++]; 7791 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 7792 break; 7793 } 7794 7795 case NestedNameSpecifier::Global: { 7796 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 7797 // No associated value, and there can't be a prefix. 7798 break; 7799 } 7800 } 7801 Prev = NNS; 7802 } 7803 return NNS; 7804} 7805 7806NestedNameSpecifierLoc 7807ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 7808 unsigned &Idx) { 7809 unsigned N = Record[Idx++]; 7810 NestedNameSpecifierLocBuilder Builder; 7811 for (unsigned I = 0; I != N; ++I) { 7812 NestedNameSpecifier::SpecifierKind Kind 7813 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 7814 switch (Kind) { 7815 case NestedNameSpecifier::Identifier: { 7816 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 7817 SourceRange Range = ReadSourceRange(F, Record, Idx); 7818 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 7819 break; 7820 } 7821 7822 case NestedNameSpecifier::Namespace: { 7823 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 7824 SourceRange Range = ReadSourceRange(F, Record, Idx); 7825 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 7826 break; 7827 } 7828 7829 case NestedNameSpecifier::NamespaceAlias: { 7830 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 7831 SourceRange Range = ReadSourceRange(F, Record, Idx); 7832 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 7833 break; 7834 } 7835 7836 case NestedNameSpecifier::TypeSpec: 7837 case NestedNameSpecifier::TypeSpecWithTemplate: { 7838 bool Template = Record[Idx++]; 7839 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 7840 if (!T) 7841 return NestedNameSpecifierLoc(); 7842 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 7843 7844 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 7845 Builder.Extend(Context, 7846 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 7847 T->getTypeLoc(), ColonColonLoc); 7848 break; 7849 } 7850 7851 case NestedNameSpecifier::Global: { 7852 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 7853 Builder.MakeGlobal(Context, ColonColonLoc); 7854 break; 7855 } 7856 } 7857 } 7858 7859 return Builder.getWithLocInContext(Context); 7860} 7861 7862SourceRange 7863ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 7864 unsigned &Idx) { 7865 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 7866 SourceLocation end = ReadSourceLocation(F, Record, Idx); 7867 return SourceRange(beg, end); 7868} 7869 7870/// \brief Read an integral value 7871llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 7872 unsigned BitWidth = Record[Idx++]; 7873 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 7874 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 7875 Idx += NumWords; 7876 return Result; 7877} 7878 7879/// \brief Read a signed integral value 7880llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 7881 bool isUnsigned = Record[Idx++]; 7882 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 7883} 7884 7885/// \brief Read a floating-point value 7886llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, 7887 const llvm::fltSemantics &Sem, 7888 unsigned &Idx) { 7889 return llvm::APFloat(Sem, ReadAPInt(Record, Idx)); 7890} 7891 7892// \brief Read a string 7893std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 7894 unsigned Len = Record[Idx++]; 7895 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 7896 Idx += Len; 7897 return Result; 7898} 7899 7900VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 7901 unsigned &Idx) { 7902 unsigned Major = Record[Idx++]; 7903 unsigned Minor = Record[Idx++]; 7904 unsigned Subminor = Record[Idx++]; 7905 if (Minor == 0) 7906 return VersionTuple(Major); 7907 if (Subminor == 0) 7908 return VersionTuple(Major, Minor - 1); 7909 return VersionTuple(Major, Minor - 1, Subminor - 1); 7910} 7911 7912CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 7913 const RecordData &Record, 7914 unsigned &Idx) { 7915 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 7916 return CXXTemporary::Create(Context, Decl); 7917} 7918 7919DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 7920 return Diag(CurrentImportLoc, DiagID); 7921} 7922 7923DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 7924 return Diags.Report(Loc, DiagID); 7925} 7926 7927/// \brief Retrieve the identifier table associated with the 7928/// preprocessor. 7929IdentifierTable &ASTReader::getIdentifierTable() { 7930 return PP.getIdentifierTable(); 7931} 7932 7933/// \brief Record that the given ID maps to the given switch-case 7934/// statement. 7935void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 7936 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 7937 "Already have a SwitchCase with this ID"); 7938 (*CurrSwitchCaseStmts)[ID] = SC; 7939} 7940 7941/// \brief Retrieve the switch-case statement with the given ID. 7942SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 7943 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 7944 return (*CurrSwitchCaseStmts)[ID]; 7945} 7946 7947void ASTReader::ClearSwitchCaseIDs() { 7948 CurrSwitchCaseStmts->clear(); 7949} 7950 7951void ASTReader::ReadComments() { 7952 std::vector<RawComment *> Comments; 7953 for (SmallVectorImpl<std::pair<BitstreamCursor, 7954 serialization::ModuleFile *> >::iterator 7955 I = CommentsCursors.begin(), 7956 E = CommentsCursors.end(); 7957 I != E; ++I) { 7958 Comments.clear(); 7959 BitstreamCursor &Cursor = I->first; 7960 serialization::ModuleFile &F = *I->second; 7961 SavedStreamPosition SavedPosition(Cursor); 7962 7963 RecordData Record; 7964 while (true) { 7965 llvm::BitstreamEntry Entry = 7966 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd); 7967 7968 switch (Entry.Kind) { 7969 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 7970 case llvm::BitstreamEntry::Error: 7971 Error("malformed block record in AST file"); 7972 return; 7973 case llvm::BitstreamEntry::EndBlock: 7974 goto NextCursor; 7975 case llvm::BitstreamEntry::Record: 7976 // The interesting case. 7977 break; 7978 } 7979 7980 // Read a record. 7981 Record.clear(); 7982 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 7983 case COMMENTS_RAW_COMMENT: { 7984 unsigned Idx = 0; 7985 SourceRange SR = ReadSourceRange(F, Record, Idx); 7986 RawComment::CommentKind Kind = 7987 (RawComment::CommentKind) Record[Idx++]; 7988 bool IsTrailingComment = Record[Idx++]; 7989 bool IsAlmostTrailingComment = Record[Idx++]; 7990 Comments.push_back(new (Context) RawComment( 7991 SR, Kind, IsTrailingComment, IsAlmostTrailingComment, 7992 Context.getLangOpts().CommentOpts.ParseAllComments)); 7993 break; 7994 } 7995 } 7996 } 7997 NextCursor: 7998 Context.Comments.addDeserializedComments(Comments); 7999 } 8000} 8001 8002std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 8003 // If we know the owning module, use it. 8004 if (Module *M = D->getOwningModule()) 8005 return M->getFullModuleName(); 8006 8007 // Otherwise, use the name of the top-level module the decl is within. 8008 if (ModuleFile *M = getOwningModuleFile(D)) 8009 return M->ModuleName; 8010 8011 // Not from a module. 8012 return ""; 8013} 8014 8015void ASTReader::finishPendingActions() { 8016 while (!PendingIdentifierInfos.empty() || 8017 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 8018 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 8019 !PendingUpdateRecords.empty() || !PendingOdrMergeChecks.empty()) { 8020 // If any identifiers with corresponding top-level declarations have 8021 // been loaded, load those declarations now. 8022 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > 8023 TopLevelDeclsMap; 8024 TopLevelDeclsMap TopLevelDecls; 8025 8026 while (!PendingIdentifierInfos.empty()) { 8027 IdentifierInfo *II = PendingIdentifierInfos.back().first; 8028 SmallVector<uint32_t, 4> DeclIDs = 8029 std::move(PendingIdentifierInfos.back().second); 8030 PendingIdentifierInfos.pop_back(); 8031 8032 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 8033 } 8034 8035 // For each decl chain that we wanted to complete while deserializing, mark 8036 // it as "still needs to be completed". 8037 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 8038 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 8039 } 8040 PendingIncompleteDeclChains.clear(); 8041 8042 // Load pending declaration chains. 8043 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) { 8044 loadPendingDeclChain(PendingDeclChains[I]); 8045 PendingDeclChainsKnown.erase(PendingDeclChains[I]); 8046 } 8047 PendingDeclChains.clear(); 8048 8049 // Make the most recent of the top-level declarations visible. 8050 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 8051 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 8052 IdentifierInfo *II = TLD->first; 8053 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 8054 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 8055 } 8056 } 8057 8058 // Load any pending macro definitions. 8059 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 8060 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 8061 SmallVector<PendingMacroInfo, 2> GlobalIDs; 8062 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 8063 // Initialize the macro history from chained-PCHs ahead of module imports. 8064 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 8065 ++IDIdx) { 8066 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 8067 if (Info.M->Kind != MK_Module) 8068 resolvePendingMacro(II, Info); 8069 } 8070 // Handle module imports. 8071 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 8072 ++IDIdx) { 8073 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 8074 if (Info.M->Kind == MK_Module) 8075 resolvePendingMacro(II, Info); 8076 } 8077 } 8078 PendingMacroIDs.clear(); 8079 8080 // Wire up the DeclContexts for Decls that we delayed setting until 8081 // recursive loading is completed. 8082 while (!PendingDeclContextInfos.empty()) { 8083 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 8084 PendingDeclContextInfos.pop_front(); 8085 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 8086 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 8087 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 8088 } 8089 8090 // Perform any pending declaration updates. 8091 while (!PendingUpdateRecords.empty()) { 8092 auto Update = PendingUpdateRecords.pop_back_val(); 8093 ReadingKindTracker ReadingKind(Read_Decl, *this); 8094 loadDeclUpdateRecords(Update.first, Update.second); 8095 } 8096 8097 // Trigger the import of the full definition of each class that had any 8098 // odr-merging problems, so we can produce better diagnostics for them. 8099 for (auto &Merge : PendingOdrMergeFailures) { 8100 Merge.first->buildLookup(); 8101 Merge.first->decls_begin(); 8102 Merge.first->bases_begin(); 8103 Merge.first->vbases_begin(); 8104 for (auto *RD : Merge.second) { 8105 RD->decls_begin(); 8106 RD->bases_begin(); 8107 RD->vbases_begin(); 8108 } 8109 } 8110 8111 // For each declaration from a merged context, check that the canonical 8112 // definition of that context also contains a declaration of the same 8113 // entity. 8114 while (!PendingOdrMergeChecks.empty()) { 8115 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 8116 8117 // FIXME: Skip over implicit declarations for now. This matters for things 8118 // like implicitly-declared special member functions. This isn't entirely 8119 // correct; we can end up with multiple unmerged declarations of the same 8120 // implicit entity. 8121 if (D->isImplicit()) 8122 continue; 8123 8124 DeclContext *CanonDef = D->getDeclContext(); 8125 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName()); 8126 8127 bool Found = false; 8128 const Decl *DCanon = D->getCanonicalDecl(); 8129 8130 llvm::SmallVector<const NamedDecl*, 4> Candidates; 8131 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); 8132 !Found && I != E; ++I) { 8133 for (auto RI : (*I)->redecls()) { 8134 if (RI->getLexicalDeclContext() == CanonDef) { 8135 // This declaration is present in the canonical definition. If it's 8136 // in the same redecl chain, it's the one we're looking for. 8137 if (RI->getCanonicalDecl() == DCanon) 8138 Found = true; 8139 else 8140 Candidates.push_back(cast<NamedDecl>(RI)); 8141 break; 8142 } 8143 } 8144 } 8145 8146 if (!Found) { 8147 D->setInvalidDecl(); 8148 8149 std::string CanonDefModule = 8150 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 8151 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 8152 << D << getOwningModuleNameForDiagnostic(D) 8153 << CanonDef << CanonDefModule.empty() << CanonDefModule; 8154 8155 if (Candidates.empty()) 8156 Diag(cast<Decl>(CanonDef)->getLocation(), 8157 diag::note_module_odr_violation_no_possible_decls) << D; 8158 else { 8159 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 8160 Diag(Candidates[I]->getLocation(), 8161 diag::note_module_odr_violation_possible_decl) 8162 << Candidates[I]; 8163 } 8164 8165 DiagnosedOdrMergeFailures.insert(CanonDef); 8166 } 8167 } 8168 } 8169 8170 // If we deserialized any C++ or Objective-C class definitions, any 8171 // Objective-C protocol definitions, or any redeclarable templates, make sure 8172 // that all redeclarations point to the definitions. Note that this can only 8173 // happen now, after the redeclaration chains have been fully wired. 8174 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(), 8175 DEnd = PendingDefinitions.end(); 8176 D != DEnd; ++D) { 8177 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) { 8178 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 8179 // Make sure that the TagType points at the definition. 8180 const_cast<TagType*>(TagT)->decl = TD; 8181 } 8182 8183 if (auto RD = dyn_cast<CXXRecordDecl>(*D)) { 8184 for (auto R : RD->redecls()) 8185 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 8186 } 8187 8188 continue; 8189 } 8190 8191 if (auto ID = dyn_cast<ObjCInterfaceDecl>(*D)) { 8192 // Make sure that the ObjCInterfaceType points at the definition. 8193 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 8194 ->Decl = ID; 8195 8196 for (auto R : ID->redecls()) 8197 R->Data = ID->Data; 8198 8199 continue; 8200 } 8201 8202 if (auto PD = dyn_cast<ObjCProtocolDecl>(*D)) { 8203 for (auto R : PD->redecls()) 8204 R->Data = PD->Data; 8205 8206 continue; 8207 } 8208 8209 auto RTD = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl(); 8210 for (auto R : RTD->redecls()) 8211 R->Common = RTD->Common; 8212 } 8213 PendingDefinitions.clear(); 8214 8215 // Load the bodies of any functions or methods we've encountered. We do 8216 // this now (delayed) so that we can be sure that the declaration chains 8217 // have been fully wired up. 8218 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 8219 PBEnd = PendingBodies.end(); 8220 PB != PBEnd; ++PB) { 8221 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 8222 // FIXME: Check for =delete/=default? 8223 // FIXME: Complain about ODR violations here? 8224 if (!getContext().getLangOpts().Modules || !FD->hasBody()) 8225 FD->setLazyBody(PB->second); 8226 continue; 8227 } 8228 8229 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 8230 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 8231 MD->setLazyBody(PB->second); 8232 } 8233 PendingBodies.clear(); 8234 8235 // Issue any pending ODR-failure diagnostics. 8236 for (auto &Merge : PendingOdrMergeFailures) { 8237 if (!DiagnosedOdrMergeFailures.insert(Merge.first)) 8238 continue; 8239 8240 bool Diagnosed = false; 8241 for (auto *RD : Merge.second) { 8242 // Multiple different declarations got merged together; tell the user 8243 // where they came from. 8244 if (Merge.first != RD) { 8245 // FIXME: Walk the definition, figure out what's different, 8246 // and diagnose that. 8247 if (!Diagnosed) { 8248 std::string Module = getOwningModuleNameForDiagnostic(Merge.first); 8249 Diag(Merge.first->getLocation(), 8250 diag::err_module_odr_violation_different_definitions) 8251 << Merge.first << Module.empty() << Module; 8252 Diagnosed = true; 8253 } 8254 8255 Diag(RD->getLocation(), 8256 diag::note_module_odr_violation_different_definitions) 8257 << getOwningModuleNameForDiagnostic(RD); 8258 } 8259 } 8260 8261 if (!Diagnosed) { 8262 // All definitions are updates to the same declaration. This happens if a 8263 // module instantiates the declaration of a class template specialization 8264 // and two or more other modules instantiate its definition. 8265 // 8266 // FIXME: Indicate which modules had instantiations of this definition. 8267 // FIXME: How can this even happen? 8268 Diag(Merge.first->getLocation(), 8269 diag::err_module_odr_violation_different_instantiations) 8270 << Merge.first; 8271 } 8272 } 8273 PendingOdrMergeFailures.clear(); 8274} 8275 8276void ASTReader::FinishedDeserializing() { 8277 assert(NumCurrentElementsDeserializing && 8278 "FinishedDeserializing not paired with StartedDeserializing"); 8279 if (NumCurrentElementsDeserializing == 1) { 8280 // We decrease NumCurrentElementsDeserializing only after pending actions 8281 // are finished, to avoid recursively re-calling finishPendingActions(). 8282 finishPendingActions(); 8283 } 8284 --NumCurrentElementsDeserializing; 8285 8286 if (NumCurrentElementsDeserializing == 0 && Consumer) { 8287 // We are not in recursive loading, so it's safe to pass the "interesting" 8288 // decls to the consumer. 8289 PassInterestingDeclsToConsumer(); 8290 } 8291} 8292 8293void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 8294 D = D->getMostRecentDecl(); 8295 8296 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 8297 SemaObj->TUScope->AddDecl(D); 8298 } else if (SemaObj->TUScope) { 8299 // Adding the decl to IdResolver may have failed because it was already in 8300 // (even though it was not added in scope). If it is already in, make sure 8301 // it gets in the scope as well. 8302 if (std::find(SemaObj->IdResolver.begin(Name), 8303 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 8304 SemaObj->TUScope->AddDecl(D); 8305 } 8306} 8307 8308ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot, 8309 bool DisableValidation, bool AllowASTWithCompilerErrors, 8310 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 8311 bool UseGlobalIndex) 8312 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr), 8313 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()), 8314 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()), 8315 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr), 8316 ModuleMgr(PP.getFileManager()), isysroot(isysroot), 8317 DisableValidation(DisableValidation), 8318 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 8319 AllowConfigurationMismatch(AllowConfigurationMismatch), 8320 ValidateSystemInputs(ValidateSystemInputs), 8321 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false), 8322 CurrSwitchCaseStmts(&SwitchCaseStmts), 8323 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0), 8324 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0), 8325 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0), 8326 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0), 8327 NumMethodPoolHits(0), NumMethodPoolTableLookups(0), 8328 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0), 8329 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 8330 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 8331 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 8332 PassingDeclsToConsumer(false), NumCXXBaseSpecifiersLoaded(0), 8333 ReadingKind(Read_None) { 8334 SourceMgr.setExternalSLocEntrySource(this); 8335} 8336 8337ASTReader::~ASTReader() { 8338 if (OwnsDeserializationListener) 8339 delete DeserializationListener; 8340 8341 for (DeclContextVisibleUpdatesPending::iterator 8342 I = PendingVisibleUpdates.begin(), 8343 E = PendingVisibleUpdates.end(); 8344 I != E; ++I) { 8345 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 8346 F = I->second.end(); 8347 J != F; ++J) 8348 delete J->first; 8349 } 8350} 8351