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