Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the PCHReader class, which reads a precompiled header. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/Frontend/PCHReader.h" |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/Type.h" |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 18 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 23 | #include "llvm/Bitcode/BitstreamReader.h" |
| 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
| 26 | #include <algorithm> |
| 27 | #include <cstdio> |
| 28 | |
| 29 | using namespace clang; |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Declaration deserialization |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | namespace { |
| 35 | class VISIBILITY_HIDDEN PCHDeclReader { |
| 36 | PCHReader &Reader; |
| 37 | const PCHReader::RecordData &Record; |
| 38 | unsigned &Idx; |
| 39 | |
| 40 | public: |
| 41 | PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record, |
| 42 | unsigned &Idx) |
| 43 | : Reader(Reader), Record(Record), Idx(Idx) { } |
| 44 | |
| 45 | void VisitDecl(Decl *D); |
| 46 | void VisitTranslationUnitDecl(TranslationUnitDecl *TU); |
| 47 | void VisitNamedDecl(NamedDecl *ND); |
| 48 | void VisitTypeDecl(TypeDecl *TD); |
| 49 | void VisitTypedefDecl(TypedefDecl *TD); |
| 50 | void VisitValueDecl(ValueDecl *VD); |
| 51 | void VisitVarDecl(VarDecl *VD); |
| 52 | |
| 53 | std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC); |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | void PCHDeclReader::VisitDecl(Decl *D) { |
| 58 | D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]))); |
| 59 | D->setLexicalDeclContext( |
| 60 | cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]))); |
| 61 | D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 62 | D->setInvalidDecl(Record[Idx++]); |
| 63 | // FIXME: hasAttrs |
| 64 | D->setImplicit(Record[Idx++]); |
| 65 | D->setAccess((AccessSpecifier)Record[Idx++]); |
| 66 | } |
| 67 | |
| 68 | void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { |
| 69 | VisitDecl(TU); |
| 70 | } |
| 71 | |
| 72 | void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) { |
| 73 | VisitDecl(ND); |
| 74 | ND->setDeclName(Reader.ReadDeclarationName(Record, Idx)); |
| 75 | } |
| 76 | |
| 77 | void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) { |
| 78 | VisitNamedDecl(TD); |
| 79 | // FIXME: circular dependencies here? |
| 80 | TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr()); |
| 81 | } |
| 82 | |
| 83 | void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) { |
| 84 | VisitTypeDecl(TD); |
| 85 | TD->setUnderlyingType(Reader.GetType(Record[Idx++])); |
| 86 | } |
| 87 | |
| 88 | void PCHDeclReader::VisitValueDecl(ValueDecl *VD) { |
| 89 | VisitNamedDecl(VD); |
| 90 | VD->setType(Reader.GetType(Record[Idx++])); |
| 91 | } |
| 92 | |
| 93 | void PCHDeclReader::VisitVarDecl(VarDecl *VD) { |
| 94 | VisitValueDecl(VD); |
| 95 | VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]); |
| 96 | VD->setThreadSpecified(Record[Idx++]); |
| 97 | VD->setCXXDirectInitializer(Record[Idx++]); |
| 98 | VD->setDeclaredInCondition(Record[Idx++]); |
| 99 | VD->setPreviousDeclaration( |
| 100 | cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 101 | VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 102 | } |
| 103 | |
| 104 | std::pair<uint64_t, uint64_t> |
| 105 | PCHDeclReader::VisitDeclContext(DeclContext *DC) { |
| 106 | uint64_t LexicalOffset = Record[Idx++]; |
| 107 | uint64_t VisibleOffset = 0; |
| 108 | if (DC->getPrimaryContext() == DC) |
| 109 | VisibleOffset = Record[Idx++]; |
| 110 | return std::make_pair(LexicalOffset, VisibleOffset); |
| 111 | } |
| 112 | |
| 113 | // FIXME: use the diagnostics machinery |
| 114 | static bool Error(const char *Str) { |
| 115 | std::fprintf(stderr, "%s\n", Str); |
| 116 | return true; |
| 117 | } |
| 118 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 119 | /// \brief Check the contents of the predefines buffer against the |
| 120 | /// contents of the predefines buffer used to build the PCH file. |
| 121 | /// |
| 122 | /// The contents of the two predefines buffers should be the same. If |
| 123 | /// not, then some command-line option changed the preprocessor state |
| 124 | /// and we must reject the PCH file. |
| 125 | /// |
| 126 | /// \param PCHPredef The start of the predefines buffer in the PCH |
| 127 | /// file. |
| 128 | /// |
| 129 | /// \param PCHPredefLen The length of the predefines buffer in the PCH |
| 130 | /// file. |
| 131 | /// |
| 132 | /// \param PCHBufferID The FileID for the PCH predefines buffer. |
| 133 | /// |
| 134 | /// \returns true if there was a mismatch (in which case the PCH file |
| 135 | /// should be ignored), or false otherwise. |
| 136 | bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef, |
| 137 | unsigned PCHPredefLen, |
| 138 | FileID PCHBufferID) { |
| 139 | const char *Predef = PP.getPredefines().c_str(); |
| 140 | unsigned PredefLen = PP.getPredefines().size(); |
| 141 | |
| 142 | // If the two predefines buffers compare equal, we're done!. |
| 143 | if (PredefLen == PCHPredefLen && |
| 144 | strncmp(Predef, PCHPredef, PCHPredefLen) == 0) |
| 145 | return false; |
| 146 | |
| 147 | // The predefines buffers are different. Produce a reasonable |
| 148 | // diagnostic showing where they are different. |
| 149 | |
| 150 | // The source locations (potentially in the two different predefines |
| 151 | // buffers) |
| 152 | SourceLocation Loc1, Loc2; |
| 153 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 154 | |
| 155 | // Create a source buffer for our predefines string, so |
| 156 | // that we can build a diagnostic that points into that |
| 157 | // source buffer. |
| 158 | FileID BufferID; |
| 159 | if (Predef && Predef[0]) { |
| 160 | llvm::MemoryBuffer *Buffer |
| 161 | = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen, |
| 162 | "<built-in>"); |
| 163 | BufferID = SourceMgr.createFileIDForMemBuffer(Buffer); |
| 164 | } |
| 165 | |
| 166 | unsigned MinLen = std::min(PredefLen, PCHPredefLen); |
| 167 | std::pair<const char *, const char *> Locations |
| 168 | = std::mismatch(Predef, Predef + MinLen, PCHPredef); |
| 169 | |
| 170 | if (Locations.first != Predef + MinLen) { |
| 171 | // We found the location in the two buffers where there is a |
| 172 | // difference. Form source locations to point there (in both |
| 173 | // buffers). |
| 174 | unsigned Offset = Locations.first - Predef; |
| 175 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 176 | .getFileLocWithOffset(Offset); |
| 177 | Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 178 | .getFileLocWithOffset(Offset); |
| 179 | } else if (PredefLen > PCHPredefLen) { |
| 180 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 181 | .getFileLocWithOffset(MinLen); |
| 182 | } else { |
| 183 | Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 184 | .getFileLocWithOffset(MinLen); |
| 185 | } |
| 186 | |
| 187 | Diag(Loc1, diag::warn_pch_preprocessor); |
| 188 | if (Loc2.isValid()) |
| 189 | Diag(Loc2, diag::note_predef_in_pch); |
| 190 | Diag(diag::note_ignoring_pch) << FileName; |
| 191 | return true; |
| 192 | } |
| 193 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 194 | /// \brief Read the source manager block |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 195 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 196 | using namespace SrcMgr; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 197 | if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
| 198 | Error("Malformed source manager block record"); |
| 199 | return Failure; |
| 200 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 201 | |
| 202 | SourceManager &SourceMgr = Context.getSourceManager(); |
| 203 | RecordData Record; |
| 204 | while (true) { |
| 205 | unsigned Code = Stream.ReadCode(); |
| 206 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 207 | if (Stream.ReadBlockEnd()) { |
| 208 | Error("Error at end of Source Manager block"); |
| 209 | return Failure; |
| 210 | } |
| 211 | |
| 212 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 216 | // No known subblocks, always skip them. |
| 217 | Stream.ReadSubBlockID(); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 218 | if (Stream.SkipBlock()) { |
| 219 | Error("Malformed block record"); |
| 220 | return Failure; |
| 221 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 222 | continue; |
| 223 | } |
| 224 | |
| 225 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 226 | Stream.ReadAbbrevRecord(); |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | // Read a record. |
| 231 | const char *BlobStart; |
| 232 | unsigned BlobLen; |
| 233 | Record.clear(); |
| 234 | switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 235 | default: // Default behavior: ignore. |
| 236 | break; |
| 237 | |
| 238 | case pch::SM_SLOC_FILE_ENTRY: { |
| 239 | // FIXME: We would really like to delay the creation of this |
| 240 | // FileEntry until it is actually required, e.g., when producing |
| 241 | // a diagnostic with a source location in this file. |
| 242 | const FileEntry *File |
| 243 | = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen); |
| 244 | // FIXME: Error recovery if file cannot be found. |
| 245 | SourceMgr.createFileID(File, |
| 246 | SourceLocation::getFromRawEncoding(Record[1]), |
| 247 | (CharacteristicKind)Record[2]); |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | case pch::SM_SLOC_BUFFER_ENTRY: { |
| 252 | const char *Name = BlobStart; |
| 253 | unsigned Code = Stream.ReadCode(); |
| 254 | Record.clear(); |
| 255 | unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen); |
| 256 | assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 257 | llvm::MemoryBuffer *Buffer |
| 258 | = llvm::MemoryBuffer::getMemBuffer(BlobStart, |
| 259 | BlobStart + BlobLen - 1, |
| 260 | Name); |
| 261 | FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer); |
| 262 | |
| 263 | if (strcmp(Name, "<built-in>") == 0 |
| 264 | && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID)) |
| 265 | return IgnorePCH; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | |
| 269 | case pch::SM_SLOC_INSTANTIATION_ENTRY: { |
| 270 | SourceLocation SpellingLoc |
| 271 | = SourceLocation::getFromRawEncoding(Record[1]); |
| 272 | SourceMgr.createInstantiationLoc( |
| 273 | SpellingLoc, |
| 274 | SourceLocation::getFromRawEncoding(Record[2]), |
| 275 | SourceLocation::getFromRawEncoding(Record[3]), |
| 276 | Lexer::MeasureTokenLength(SpellingLoc, |
| 277 | SourceMgr)); |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 285 | bool PCHReader::ReadPreprocessorBlock() { |
| 286 | if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) |
| 287 | return Error("Malformed preprocessor block record"); |
| 288 | |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 289 | RecordData Record; |
| 290 | llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; |
| 291 | MacroInfo *LastMacro = 0; |
| 292 | |
| 293 | while (true) { |
| 294 | unsigned Code = Stream.ReadCode(); |
| 295 | switch (Code) { |
| 296 | case llvm::bitc::END_BLOCK: |
| 297 | if (Stream.ReadBlockEnd()) |
| 298 | return Error("Error at end of preprocessor block"); |
| 299 | return false; |
| 300 | |
| 301 | case llvm::bitc::ENTER_SUBBLOCK: |
| 302 | // No known subblocks, always skip them. |
| 303 | Stream.ReadSubBlockID(); |
| 304 | if (Stream.SkipBlock()) |
| 305 | return Error("Malformed block record"); |
| 306 | continue; |
| 307 | |
| 308 | case llvm::bitc::DEFINE_ABBREV: |
| 309 | Stream.ReadAbbrevRecord(); |
| 310 | continue; |
| 311 | default: break; |
| 312 | } |
| 313 | |
| 314 | // Read a record. |
| 315 | Record.clear(); |
| 316 | pch::PreprocessorRecordTypes RecType = |
| 317 | (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); |
| 318 | switch (RecType) { |
| 319 | default: // Default behavior: ignore unknown records. |
| 320 | break; |
| 321 | |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 322 | case pch::PP_MACRO_OBJECT_LIKE: |
| 323 | case pch::PP_MACRO_FUNCTION_LIKE: { |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 324 | IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); |
| 325 | if (II == 0) |
| 326 | return Error("Macro must have a name"); |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 327 | SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]); |
| 328 | bool isUsed = Record[2]; |
| 329 | |
| 330 | MacroInfo *MI = PP.AllocateMacroInfo(Loc); |
| 331 | MI->setIsUsed(isUsed); |
| 332 | |
| 333 | if (RecType == pch::PP_MACRO_FUNCTION_LIKE) { |
| 334 | // Decode function-like macro info. |
| 335 | bool isC99VarArgs = Record[3]; |
| 336 | bool isGNUVarArgs = Record[4]; |
| 337 | MacroArgs.clear(); |
| 338 | unsigned NumArgs = Record[5]; |
| 339 | for (unsigned i = 0; i != NumArgs; ++i) |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 340 | MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 341 | |
| 342 | // Install function-like macro info. |
| 343 | MI->setIsFunctionLike(); |
| 344 | if (isC99VarArgs) MI->setIsC99Varargs(); |
| 345 | if (isGNUVarArgs) MI->setIsGNUVarargs(); |
| 346 | MI->setArgumentList(&MacroArgs[0], MacroArgs.size(), |
| 347 | PP.getPreprocessorAllocator()); |
| 348 | } |
| 349 | |
| 350 | // Finally, install the macro. |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 351 | PP.setMacroInfo(II, MI); |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 352 | |
| 353 | // Remember that we saw this macro last so that we add the tokens that |
| 354 | // form its body to it. |
| 355 | LastMacro = MI; |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | case pch::PP_TOKEN: { |
| 360 | // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just |
| 361 | // pretend we didn't see this. |
| 362 | if (LastMacro == 0) break; |
| 363 | |
| 364 | Token Tok; |
| 365 | Tok.startToken(); |
| 366 | Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0])); |
| 367 | Tok.setLength(Record[1]); |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 368 | if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) |
| 369 | Tok.setIdentifierInfo(II); |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 370 | Tok.setKind((tok::TokenKind)Record[3]); |
| 371 | Tok.setFlag((Token::TokenFlags)Record[4]); |
| 372 | LastMacro->AddTokenToBody(Tok); |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 379 | PCHReader::PCHReadResult PCHReader::ReadPCHBlock() { |
| 380 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
| 381 | Error("Malformed block record"); |
| 382 | return Failure; |
| 383 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 385 | uint64_t PreprocessorBlockBit = 0; |
| 386 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 387 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 388 | RecordData Record; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 389 | while (!Stream.AtEndOfStream()) { |
| 390 | unsigned Code = Stream.ReadCode(); |
| 391 | if (Code == llvm::bitc::END_BLOCK) { |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 392 | // If we saw the preprocessor block, read it now. |
| 393 | if (PreprocessorBlockBit) { |
| 394 | uint64_t SavedPos = Stream.GetCurrentBitNo(); |
| 395 | Stream.JumpToBit(PreprocessorBlockBit); |
| 396 | if (ReadPreprocessorBlock()) { |
| 397 | Error("Malformed preprocessor block"); |
| 398 | return Failure; |
| 399 | } |
| 400 | Stream.JumpToBit(SavedPos); |
| 401 | } |
| 402 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 403 | if (Stream.ReadBlockEnd()) { |
| 404 | Error("Error at end of module block"); |
| 405 | return Failure; |
| 406 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 408 | return Success; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 412 | switch (Stream.ReadSubBlockID()) { |
| 413 | case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded) |
| 414 | case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded) |
| 415 | default: // Skip unknown content. |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 416 | if (Stream.SkipBlock()) { |
| 417 | Error("Malformed block record"); |
| 418 | return Failure; |
| 419 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 420 | break; |
| 421 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 422 | case pch::PREPROCESSOR_BLOCK_ID: |
| 423 | // Skip the preprocessor block for now, but remember where it is. We |
| 424 | // want to read it in after the identifier table. |
| 425 | if (PreprocessorBlockBit) { |
| 426 | Error("Multiple preprocessor blocks found."); |
| 427 | return Failure; |
| 428 | } |
| 429 | PreprocessorBlockBit = Stream.GetCurrentBitNo(); |
| 430 | if (Stream.SkipBlock()) { |
| 431 | Error("Malformed block record"); |
| 432 | return Failure; |
| 433 | } |
| 434 | break; |
| 435 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 436 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 437 | switch (ReadSourceManagerBlock()) { |
| 438 | case Success: |
| 439 | break; |
| 440 | |
| 441 | case Failure: |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 442 | Error("Malformed source manager block"); |
| 443 | return Failure; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 444 | |
| 445 | case IgnorePCH: |
| 446 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 447 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 448 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 449 | } |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 450 | continue; |
| 451 | } |
| 452 | |
| 453 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 454 | Stream.ReadAbbrevRecord(); |
| 455 | continue; |
| 456 | } |
| 457 | |
| 458 | // Read and process a record. |
| 459 | Record.clear(); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 460 | const char *BlobStart = 0; |
| 461 | unsigned BlobLen = 0; |
| 462 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
| 463 | &BlobStart, &BlobLen)) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 464 | default: // Default behavior: ignore. |
| 465 | break; |
| 466 | |
| 467 | case pch::TYPE_OFFSET: |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 468 | if (!TypeOffsets.empty()) { |
| 469 | Error("Duplicate TYPE_OFFSET record in PCH file"); |
| 470 | return Failure; |
| 471 | } |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 472 | TypeOffsets.swap(Record); |
| 473 | TypeAlreadyLoaded.resize(TypeOffsets.size(), false); |
| 474 | break; |
| 475 | |
| 476 | case pch::DECL_OFFSET: |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 477 | if (!DeclOffsets.empty()) { |
| 478 | Error("Duplicate DECL_OFFSET record in PCH file"); |
| 479 | return Failure; |
| 480 | } |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 481 | DeclOffsets.swap(Record); |
| 482 | DeclAlreadyLoaded.resize(DeclOffsets.size(), false); |
| 483 | break; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 484 | |
| 485 | case pch::LANGUAGE_OPTIONS: |
| 486 | if (ParseLanguageOptions(Record)) |
| 487 | return IgnorePCH; |
| 488 | break; |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 490 | case pch::TARGET_TRIPLE: { |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 491 | std::string TargetTriple(BlobStart, BlobLen); |
| 492 | if (TargetTriple != Context.Target.getTargetTriple()) { |
| 493 | Diag(diag::warn_pch_target_triple) |
| 494 | << TargetTriple << Context.Target.getTargetTriple(); |
| 495 | Diag(diag::note_ignoring_pch) << FileName; |
| 496 | return IgnorePCH; |
| 497 | } |
| 498 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 499 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 500 | |
| 501 | case pch::IDENTIFIER_TABLE: |
| 502 | IdentifierTable = BlobStart; |
| 503 | break; |
| 504 | |
| 505 | case pch::IDENTIFIER_OFFSET: |
| 506 | if (!IdentifierData.empty()) { |
| 507 | Error("Duplicate IDENTIFIER_OFFSET record in PCH file"); |
| 508 | return Failure; |
| 509 | } |
| 510 | IdentifierData.swap(Record); |
| 511 | #ifndef NDEBUG |
| 512 | for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) { |
| 513 | if ((IdentifierData[I] & 0x01) == 0) { |
| 514 | Error("Malformed identifier table in the precompiled header"); |
| 515 | return Failure; |
| 516 | } |
| 517 | } |
| 518 | #endif |
| 519 | break; |
| 520 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 523 | Error("Premature end of bitstream"); |
| 524 | return Failure; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 527 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 528 | // Set the PCH file name. |
| 529 | this->FileName = FileName; |
| 530 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 531 | // Open the PCH file. |
| 532 | std::string ErrStr; |
| 533 | Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr)); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 534 | if (!Buffer) { |
| 535 | Error(ErrStr.c_str()); |
| 536 | return IgnorePCH; |
| 537 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 538 | |
| 539 | // Initialize the stream |
| 540 | Stream.init((const unsigned char *)Buffer->getBufferStart(), |
| 541 | (const unsigned char *)Buffer->getBufferEnd()); |
| 542 | |
| 543 | // Sniff for the signature. |
| 544 | if (Stream.Read(8) != 'C' || |
| 545 | Stream.Read(8) != 'P' || |
| 546 | Stream.Read(8) != 'C' || |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 547 | Stream.Read(8) != 'H') { |
| 548 | Error("Not a PCH file"); |
| 549 | return IgnorePCH; |
| 550 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 551 | |
| 552 | // We expect a number of well-defined blocks, though we don't necessarily |
| 553 | // need to understand them all. |
| 554 | while (!Stream.AtEndOfStream()) { |
| 555 | unsigned Code = Stream.ReadCode(); |
| 556 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 557 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
| 558 | Error("Invalid record at top-level"); |
| 559 | return Failure; |
| 560 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 561 | |
| 562 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 563 | |
| 564 | // We only know the PCH subblock ID. |
| 565 | switch (BlockID) { |
| 566 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 567 | if (Stream.ReadBlockInfoBlock()) { |
| 568 | Error("Malformed BlockInfoBlock"); |
| 569 | return Failure; |
| 570 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 571 | break; |
| 572 | case pch::PCH_BLOCK_ID: |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 573 | switch (ReadPCHBlock()) { |
| 574 | case Success: |
| 575 | break; |
| 576 | |
| 577 | case Failure: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 578 | return Failure; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 579 | |
| 580 | case IgnorePCH: |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 581 | // FIXME: We could consider reading through to the end of this |
| 582 | // PCH block, skipping subblocks, to see if there are other |
| 583 | // PCH blocks elsewhere. |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 584 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 585 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 586 | break; |
| 587 | default: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 588 | if (Stream.SkipBlock()) { |
| 589 | Error("Malformed block record"); |
| 590 | return Failure; |
| 591 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 592 | break; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // Load the translation unit declaration |
| 597 | ReadDeclRecord(DeclOffsets[0], 0); |
| 598 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 599 | return Success; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 602 | /// \brief Parse the record that corresponds to a LangOptions data |
| 603 | /// structure. |
| 604 | /// |
| 605 | /// This routine compares the language options used to generate the |
| 606 | /// PCH file against the language options set for the current |
| 607 | /// compilation. For each option, we classify differences between the |
| 608 | /// two compiler states as either "benign" or "important". Benign |
| 609 | /// differences don't matter, and we accept them without complaint |
| 610 | /// (and without modifying the language options). Differences between |
| 611 | /// the states for important options cause the PCH file to be |
| 612 | /// unusable, so we emit a warning and return true to indicate that |
| 613 | /// there was an error. |
| 614 | /// |
| 615 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 616 | bool PCHReader::ParseLanguageOptions( |
| 617 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
| 618 | const LangOptions &LangOpts = Context.getLangOptions(); |
| 619 | #define PARSE_LANGOPT_BENIGN(Option) ++Idx |
| 620 | #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ |
| 621 | if (Record[Idx] != LangOpts.Option) { \ |
| 622 | Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \ |
| 623 | Diag(diag::note_ignoring_pch) << FileName; \ |
| 624 | return true; \ |
| 625 | } \ |
| 626 | ++Idx |
| 627 | |
| 628 | unsigned Idx = 0; |
| 629 | PARSE_LANGOPT_BENIGN(Trigraphs); |
| 630 | PARSE_LANGOPT_BENIGN(BCPLComment); |
| 631 | PARSE_LANGOPT_BENIGN(DollarIdents); |
| 632 | PARSE_LANGOPT_BENIGN(AsmPreprocessor); |
| 633 | PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); |
| 634 | PARSE_LANGOPT_BENIGN(ImplicitInt); |
| 635 | PARSE_LANGOPT_BENIGN(Digraphs); |
| 636 | PARSE_LANGOPT_BENIGN(HexFloats); |
| 637 | PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); |
| 638 | PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); |
| 639 | PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); |
| 640 | PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); |
| 641 | PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions); |
| 642 | PARSE_LANGOPT_BENIGN(CXXOperatorName); |
| 643 | PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); |
| 644 | PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); |
| 645 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); |
| 646 | PARSE_LANGOPT_BENIGN(PascalStrings); |
| 647 | PARSE_LANGOPT_BENIGN(Boolean); |
| 648 | PARSE_LANGOPT_BENIGN(WritableStrings); |
| 649 | PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, |
| 650 | diag::warn_pch_lax_vector_conversions); |
| 651 | PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); |
| 652 | PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); |
| 653 | PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); |
| 654 | PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); |
| 655 | PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, |
| 656 | diag::warn_pch_thread_safe_statics); |
| 657 | PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); |
| 658 | PARSE_LANGOPT_BENIGN(EmitAllDecls); |
| 659 | PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); |
| 660 | PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking); |
| 661 | PARSE_LANGOPT_IMPORTANT(HeinousExtensions, |
| 662 | diag::warn_pch_heinous_extensions); |
| 663 | // FIXME: Most of the options below are benign if the macro wasn't |
| 664 | // used. Unfortunately, this means that a PCH compiled without |
| 665 | // optimization can't be used with optimization turned on, even |
| 666 | // though the only thing that changes is whether __OPTIMIZE__ was |
| 667 | // defined... but if __OPTIMIZE__ never showed up in the header, it |
| 668 | // doesn't matter. We could consider making this some special kind |
| 669 | // of check. |
| 670 | PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); |
| 671 | PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); |
| 672 | PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); |
| 673 | PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); |
| 674 | PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); |
| 675 | PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); |
| 676 | if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) { |
| 677 | Diag(diag::warn_pch_gc_mode) |
| 678 | << (unsigned)Record[Idx] << LangOpts.getGCMode(); |
| 679 | Diag(diag::note_ignoring_pch) << FileName; |
| 680 | return true; |
| 681 | } |
| 682 | ++Idx; |
| 683 | PARSE_LANGOPT_BENIGN(getVisibilityMode()); |
| 684 | PARSE_LANGOPT_BENIGN(InstantiationDepth); |
| 685 | #undef PARSE_LANGOPT_IRRELEVANT |
| 686 | #undef PARSE_LANGOPT_BENIGN |
| 687 | |
| 688 | return false; |
| 689 | } |
| 690 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 691 | /// \brief Read and return the type at the given offset. |
| 692 | /// |
| 693 | /// This routine actually reads the record corresponding to the type |
| 694 | /// at the given offset in the bitstream. It is a helper routine for |
| 695 | /// GetType, which deals with reading type IDs. |
| 696 | QualType PCHReader::ReadTypeRecord(uint64_t Offset) { |
| 697 | Stream.JumpToBit(Offset); |
| 698 | RecordData Record; |
| 699 | unsigned Code = Stream.ReadCode(); |
| 700 | switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) { |
| 701 | case pch::TYPE_FIXED_WIDTH_INT: { |
| 702 | assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type"); |
| 703 | return Context.getFixedWidthIntType(Record[0], Record[1]); |
| 704 | } |
| 705 | |
| 706 | case pch::TYPE_COMPLEX: { |
| 707 | assert(Record.size() == 1 && "Incorrect encoding of complex type"); |
| 708 | QualType ElemType = GetType(Record[0]); |
| 709 | return Context.getComplexType(ElemType); |
| 710 | } |
| 711 | |
| 712 | case pch::TYPE_POINTER: { |
| 713 | assert(Record.size() == 1 && "Incorrect encoding of pointer type"); |
| 714 | QualType PointeeType = GetType(Record[0]); |
| 715 | return Context.getPointerType(PointeeType); |
| 716 | } |
| 717 | |
| 718 | case pch::TYPE_BLOCK_POINTER: { |
| 719 | assert(Record.size() == 1 && "Incorrect encoding of block pointer type"); |
| 720 | QualType PointeeType = GetType(Record[0]); |
| 721 | return Context.getBlockPointerType(PointeeType); |
| 722 | } |
| 723 | |
| 724 | case pch::TYPE_LVALUE_REFERENCE: { |
| 725 | assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type"); |
| 726 | QualType PointeeType = GetType(Record[0]); |
| 727 | return Context.getLValueReferenceType(PointeeType); |
| 728 | } |
| 729 | |
| 730 | case pch::TYPE_RVALUE_REFERENCE: { |
| 731 | assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type"); |
| 732 | QualType PointeeType = GetType(Record[0]); |
| 733 | return Context.getRValueReferenceType(PointeeType); |
| 734 | } |
| 735 | |
| 736 | case pch::TYPE_MEMBER_POINTER: { |
| 737 | assert(Record.size() == 1 && "Incorrect encoding of member pointer type"); |
| 738 | QualType PointeeType = GetType(Record[0]); |
| 739 | QualType ClassType = GetType(Record[1]); |
| 740 | return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
| 741 | } |
| 742 | |
| 743 | // FIXME: Several other kinds of types to deserialize here! |
| 744 | default: |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 745 | assert(false && "Unable to deserialize this type"); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | |
| 749 | // Suppress a GCC warning |
| 750 | return QualType(); |
| 751 | } |
| 752 | |
| 753 | /// \brief Note that we have loaded the declaration with the given |
| 754 | /// Index. |
| 755 | /// |
| 756 | /// This routine notes that this declaration has already been loaded, |
| 757 | /// so that future GetDecl calls will return this declaration rather |
| 758 | /// than trying to load a new declaration. |
| 759 | inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) { |
| 760 | assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?"); |
| 761 | DeclAlreadyLoaded[Index] = true; |
| 762 | DeclOffsets[Index] = reinterpret_cast<uint64_t>(D); |
| 763 | } |
| 764 | |
| 765 | /// \brief Read the declaration at the given offset from the PCH file. |
| 766 | Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { |
| 767 | Decl *D = 0; |
| 768 | Stream.JumpToBit(Offset); |
| 769 | RecordData Record; |
| 770 | unsigned Code = Stream.ReadCode(); |
| 771 | unsigned Idx = 0; |
| 772 | PCHDeclReader Reader(*this, Record, Idx); |
| 773 | switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) { |
| 774 | case pch::DECL_TRANSLATION_UNIT: |
| 775 | assert(Index == 0 && "Translation unit must be at index 0"); |
| 776 | Reader.VisitTranslationUnitDecl(Context.getTranslationUnitDecl()); |
| 777 | D = Context.getTranslationUnitDecl(); |
| 778 | LoadedDecl(Index, D); |
| 779 | break; |
| 780 | |
| 781 | case pch::DECL_TYPEDEF: { |
| 782 | TypedefDecl *Typedef = TypedefDecl::Create(Context, 0, SourceLocation(), |
| 783 | 0, QualType()); |
| 784 | LoadedDecl(Index, Typedef); |
| 785 | Reader.VisitTypedefDecl(Typedef); |
| 786 | D = Typedef; |
| 787 | break; |
| 788 | } |
| 789 | |
| 790 | case pch::DECL_VAR: { |
| 791 | VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(), |
| 792 | VarDecl::None, SourceLocation()); |
| 793 | LoadedDecl(Index, Var); |
| 794 | Reader.VisitVarDecl(Var); |
| 795 | D = Var; |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | default: |
| 800 | assert(false && "Cannot de-serialize this kind of declaration"); |
| 801 | break; |
| 802 | } |
| 803 | |
| 804 | // If this declaration is also a declaration context, get the |
| 805 | // offsets for its tables of lexical and visible declarations. |
| 806 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) { |
| 807 | std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC); |
| 808 | if (Offsets.first || Offsets.second) { |
| 809 | DC->setHasExternalLexicalStorage(Offsets.first != 0); |
| 810 | DC->setHasExternalVisibleStorage(Offsets.second != 0); |
| 811 | DeclContextOffsets[DC] = Offsets; |
| 812 | } |
| 813 | } |
| 814 | assert(Idx == Record.size()); |
| 815 | |
| 816 | return D; |
| 817 | } |
| 818 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 819 | QualType PCHReader::GetType(pch::TypeID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 820 | unsigned Quals = ID & 0x07; |
| 821 | unsigned Index = ID >> 3; |
| 822 | |
| 823 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 824 | QualType T; |
| 825 | switch ((pch::PredefinedTypeIDs)Index) { |
| 826 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
| 827 | case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; |
| 828 | case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; |
| 829 | |
| 830 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 831 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 832 | // FIXME: Check that the signedness of CharTy is correct! |
| 833 | T = Context.CharTy; |
| 834 | break; |
| 835 | |
| 836 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; |
| 837 | case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; |
| 838 | case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; |
| 839 | case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; |
| 840 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; |
| 841 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; |
| 842 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; |
| 843 | case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; |
| 844 | case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break; |
| 845 | case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; |
| 846 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; |
| 847 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; |
| 848 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; |
| 849 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; |
| 850 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; |
| 851 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; |
| 852 | } |
| 853 | |
| 854 | assert(!T.isNull() && "Unknown predefined type"); |
| 855 | return T.getQualifiedType(Quals); |
| 856 | } |
| 857 | |
| 858 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
| 859 | if (!TypeAlreadyLoaded[Index]) { |
| 860 | // Load the type from the PCH file. |
| 861 | TypeOffsets[Index] = reinterpret_cast<uint64_t>( |
| 862 | ReadTypeRecord(TypeOffsets[Index]).getTypePtr()); |
| 863 | TypeAlreadyLoaded[Index] = true; |
| 864 | } |
| 865 | |
| 866 | return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals); |
| 867 | } |
| 868 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 869 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 870 | if (ID == 0) |
| 871 | return 0; |
| 872 | |
| 873 | unsigned Index = ID - 1; |
| 874 | if (DeclAlreadyLoaded[Index]) |
| 875 | return reinterpret_cast<Decl *>(DeclOffsets[Index]); |
| 876 | |
| 877 | // Load the declaration from the PCH file. |
| 878 | return ReadDeclRecord(DeclOffsets[Index], Index); |
| 879 | } |
| 880 | |
| 881 | bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 882 | llvm::SmallVectorImpl<pch::DeclID> &Decls) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 883 | assert(DC->hasExternalLexicalStorage() && |
| 884 | "DeclContext has no lexical decls in storage"); |
| 885 | uint64_t Offset = DeclContextOffsets[DC].first; |
| 886 | assert(Offset && "DeclContext has no lexical decls in storage"); |
| 887 | |
| 888 | // Load the record containing all of the declarations lexically in |
| 889 | // this context. |
| 890 | Stream.JumpToBit(Offset); |
| 891 | RecordData Record; |
| 892 | unsigned Code = Stream.ReadCode(); |
| 893 | unsigned RecCode = Stream.ReadRecord(Code, Record); |
| 894 | assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block"); |
| 895 | |
| 896 | // Load all of the declaration IDs |
| 897 | Decls.clear(); |
| 898 | Decls.insert(Decls.end(), Record.begin(), Record.end()); |
| 899 | return false; |
| 900 | } |
| 901 | |
| 902 | bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, |
| 903 | llvm::SmallVectorImpl<VisibleDeclaration> & Decls) { |
| 904 | assert(DC->hasExternalVisibleStorage() && |
| 905 | "DeclContext has no visible decls in storage"); |
| 906 | uint64_t Offset = DeclContextOffsets[DC].second; |
| 907 | assert(Offset && "DeclContext has no visible decls in storage"); |
| 908 | |
| 909 | // Load the record containing all of the declarations visible in |
| 910 | // this context. |
| 911 | Stream.JumpToBit(Offset); |
| 912 | RecordData Record; |
| 913 | unsigned Code = Stream.ReadCode(); |
| 914 | unsigned RecCode = Stream.ReadRecord(Code, Record); |
| 915 | assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block"); |
| 916 | if (Record.size() == 0) |
| 917 | return false; |
| 918 | |
| 919 | Decls.clear(); |
| 920 | |
| 921 | unsigned Idx = 0; |
| 922 | // llvm::SmallVector<uintptr_t, 16> DeclIDs; |
| 923 | while (Idx < Record.size()) { |
| 924 | Decls.push_back(VisibleDeclaration()); |
| 925 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 926 | |
| 927 | // FIXME: Don't actually read anything here! |
| 928 | unsigned Size = Record[Idx++]; |
| 929 | llvm::SmallVector<unsigned, 4> & LoadedDecls |
| 930 | = Decls.back().Declarations; |
| 931 | LoadedDecls.reserve(Size); |
| 932 | for (unsigned I = 0; I < Size; ++I) |
| 933 | LoadedDecls.push_back(Record[Idx++]); |
| 934 | } |
| 935 | |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | void PCHReader::PrintStats() { |
| 940 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 941 | |
| 942 | unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(), |
| 943 | TypeAlreadyLoaded.end(), |
| 944 | true); |
| 945 | unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(), |
| 946 | DeclAlreadyLoaded.end(), |
| 947 | true); |
| 948 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
| 949 | NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(), |
| 950 | ((float)NumTypesLoaded/(float)TypeAlreadyLoaded.size() * 100)); |
| 951 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
| 952 | NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(), |
| 953 | ((float)NumDeclsLoaded/(float)DeclAlreadyLoaded.size() * 100)); |
| 954 | std::fprintf(stderr, "\n"); |
| 955 | } |
| 956 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 957 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 958 | if (ID == 0) |
| 959 | return 0; |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 961 | if (!IdentifierTable || IdentifierData.empty()) { |
| 962 | Error("No identifier table in PCH file"); |
| 963 | return 0; |
| 964 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 966 | if (IdentifierData[ID - 1] & 0x01) { |
| 967 | uint64_t Offset = IdentifierData[ID - 1]; |
| 968 | IdentifierData[ID - 1] = reinterpret_cast<uint64_t>( |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 969 | &Context.Idents.get(IdentifierTable + Offset)); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 970 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 971 | |
| 972 | return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | DeclarationName |
| 976 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 977 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 978 | switch (Kind) { |
| 979 | case DeclarationName::Identifier: |
| 980 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 981 | |
| 982 | case DeclarationName::ObjCZeroArgSelector: |
| 983 | case DeclarationName::ObjCOneArgSelector: |
| 984 | case DeclarationName::ObjCMultiArgSelector: |
| 985 | assert(false && "Unable to de-serialize Objective-C selectors"); |
| 986 | break; |
| 987 | |
| 988 | case DeclarationName::CXXConstructorName: |
| 989 | return Context.DeclarationNames.getCXXConstructorName( |
| 990 | GetType(Record[Idx++])); |
| 991 | |
| 992 | case DeclarationName::CXXDestructorName: |
| 993 | return Context.DeclarationNames.getCXXDestructorName( |
| 994 | GetType(Record[Idx++])); |
| 995 | |
| 996 | case DeclarationName::CXXConversionFunctionName: |
| 997 | return Context.DeclarationNames.getCXXConversionFunctionName( |
| 998 | GetType(Record[Idx++])); |
| 999 | |
| 1000 | case DeclarationName::CXXOperatorName: |
| 1001 | return Context.DeclarationNames.getCXXOperatorName( |
| 1002 | (OverloadedOperatorKind)Record[Idx++]); |
| 1003 | |
| 1004 | case DeclarationName::CXXUsingDirective: |
| 1005 | return DeclarationName::getUsingDirectiveName(); |
| 1006 | } |
| 1007 | |
| 1008 | // Required to silence GCC warning |
| 1009 | return DeclarationName(); |
| 1010 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1011 | |
| 1012 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1013 | return Diag(SourceLocation(), DiagID); |
| 1014 | } |
| 1015 | |
| 1016 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
| 1017 | return PP.getDiagnostics().Report(FullSourceLoc(Loc, |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1018 | Context.getSourceManager()), |
| 1019 | DiagID); |
| 1020 | } |