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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PCHReader.h" |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 16 | #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 21 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 23 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 24 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 27 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 29 | #include "llvm/Bitcode/BitstreamReader.h" |
| 30 | #include "llvm/Support/Compiler.h" |
| 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | #include <algorithm> |
| 33 | #include <cstdio> |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 34 | #include <sys/stat.h> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 38 | // PCH reader implementation |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 41 | PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context) |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 42 | : SemaObj(0), PP(PP), Context(Context), Consumer(0), |
| 43 | IdentifierTableData(0), IdentifierLookupTable(0), |
| 44 | IdentifierOffsets(0), |
| 45 | MethodPoolLookupTable(0), MethodPoolLookupTableData(0), |
| 46 | TotalSelectorsInMethodPool(0), SelectorOffsets(0), |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 47 | TotalNumSelectors(0), NumStatHits(0), NumStatMisses(0), |
| 48 | NumSLocEntriesRead(0), NumStatementsRead(0), |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 49 | NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0), |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 50 | NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0) { } |
| 51 | |
| 52 | PCHReader::~PCHReader() {} |
| 53 | |
Chris Lattner | da93061 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 54 | Expr *PCHReader::ReadDeclExpr() { |
| 55 | return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor)); |
| 56 | } |
| 57 | |
| 58 | Expr *PCHReader::ReadTypeExpr() { |
Chris Lattner | 52e97d1 | 2009-04-27 05:41:06 +0000 | [diff] [blame] | 59 | return dyn_cast_or_null<Expr>(ReadStmt(Stream)); |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 63 | namespace { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 64 | class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait { |
| 65 | PCHReader &Reader; |
| 66 | |
| 67 | public: |
| 68 | typedef std::pair<ObjCMethodList, ObjCMethodList> data_type; |
| 69 | |
| 70 | typedef Selector external_key_type; |
| 71 | typedef external_key_type internal_key_type; |
| 72 | |
| 73 | explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { } |
| 74 | |
| 75 | static bool EqualKey(const internal_key_type& a, |
| 76 | const internal_key_type& b) { |
| 77 | return a == b; |
| 78 | } |
| 79 | |
| 80 | static unsigned ComputeHash(Selector Sel) { |
| 81 | unsigned N = Sel.getNumArgs(); |
| 82 | if (N == 0) |
| 83 | ++N; |
| 84 | unsigned R = 5381; |
| 85 | for (unsigned I = 0; I != N; ++I) |
| 86 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) |
| 87 | R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R); |
| 88 | return R; |
| 89 | } |
| 90 | |
| 91 | // This hopefully will just get inlined and removed by the optimizer. |
| 92 | static const internal_key_type& |
| 93 | GetInternalKey(const external_key_type& x) { return x; } |
| 94 | |
| 95 | static std::pair<unsigned, unsigned> |
| 96 | ReadKeyDataLength(const unsigned char*& d) { |
| 97 | using namespace clang::io; |
| 98 | unsigned KeyLen = ReadUnalignedLE16(d); |
| 99 | unsigned DataLen = ReadUnalignedLE16(d); |
| 100 | return std::make_pair(KeyLen, DataLen); |
| 101 | } |
| 102 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 103 | internal_key_type ReadKey(const unsigned char* d, unsigned) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 104 | using namespace clang::io; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 105 | SelectorTable &SelTable = Reader.getContext()->Selectors; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 106 | unsigned N = ReadUnalignedLE16(d); |
| 107 | IdentifierInfo *FirstII |
| 108 | = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); |
| 109 | if (N == 0) |
| 110 | return SelTable.getNullarySelector(FirstII); |
| 111 | else if (N == 1) |
| 112 | return SelTable.getUnarySelector(FirstII); |
| 113 | |
| 114 | llvm::SmallVector<IdentifierInfo *, 16> Args; |
| 115 | Args.push_back(FirstII); |
| 116 | for (unsigned I = 1; I != N; ++I) |
| 117 | Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d))); |
| 118 | |
| 119 | return SelTable.getSelector(N, &Args[0]); |
| 120 | } |
| 121 | |
| 122 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) { |
| 123 | using namespace clang::io; |
| 124 | unsigned NumInstanceMethods = ReadUnalignedLE16(d); |
| 125 | unsigned NumFactoryMethods = ReadUnalignedLE16(d); |
| 126 | |
| 127 | data_type Result; |
| 128 | |
| 129 | // Load instance methods |
| 130 | ObjCMethodList *Prev = 0; |
| 131 | for (unsigned I = 0; I != NumInstanceMethods; ++I) { |
| 132 | ObjCMethodDecl *Method |
| 133 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 134 | if (!Result.first.Method) { |
| 135 | // This is the first method, which is the easy case. |
| 136 | Result.first.Method = Method; |
| 137 | Prev = &Result.first; |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | Prev->Next = new ObjCMethodList(Method, 0); |
| 142 | Prev = Prev->Next; |
| 143 | } |
| 144 | |
| 145 | // Load factory methods |
| 146 | Prev = 0; |
| 147 | for (unsigned I = 0; I != NumFactoryMethods; ++I) { |
| 148 | ObjCMethodDecl *Method |
| 149 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 150 | if (!Result.second.Method) { |
| 151 | // This is the first method, which is the easy case. |
| 152 | Result.second.Method = Method; |
| 153 | Prev = &Result.second; |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | Prev->Next = new ObjCMethodList(Method, 0); |
| 158 | Prev = Prev->Next; |
| 159 | } |
| 160 | |
| 161 | return Result; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | } // end anonymous namespace |
| 166 | |
| 167 | /// \brief The on-disk hash table used for the global method pool. |
| 168 | typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait> |
| 169 | PCHMethodPoolLookupTable; |
| 170 | |
| 171 | namespace { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 172 | class VISIBILITY_HIDDEN PCHIdentifierLookupTrait { |
| 173 | PCHReader &Reader; |
| 174 | |
| 175 | // If we know the IdentifierInfo in advance, it is here and we will |
| 176 | // not build a new one. Used when deserializing information about an |
| 177 | // identifier that was constructed before the PCH file was read. |
| 178 | IdentifierInfo *KnownII; |
| 179 | |
| 180 | public: |
| 181 | typedef IdentifierInfo * data_type; |
| 182 | |
| 183 | typedef const std::pair<const char*, unsigned> external_key_type; |
| 184 | |
| 185 | typedef external_key_type internal_key_type; |
| 186 | |
| 187 | explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0) |
| 188 | : Reader(Reader), KnownII(II) { } |
| 189 | |
| 190 | static bool EqualKey(const internal_key_type& a, |
| 191 | const internal_key_type& b) { |
| 192 | return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 |
| 193 | : false; |
| 194 | } |
| 195 | |
| 196 | static unsigned ComputeHash(const internal_key_type& a) { |
| 197 | return BernsteinHash(a.first, a.second); |
| 198 | } |
| 199 | |
| 200 | // This hopefully will just get inlined and removed by the optimizer. |
| 201 | static const internal_key_type& |
| 202 | GetInternalKey(const external_key_type& x) { return x; } |
| 203 | |
| 204 | static std::pair<unsigned, unsigned> |
| 205 | ReadKeyDataLength(const unsigned char*& d) { |
| 206 | using namespace clang::io; |
Douglas Gregor | 5f8e330 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 207 | unsigned DataLen = ReadUnalignedLE16(d); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 208 | unsigned KeyLen = ReadUnalignedLE16(d); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 209 | return std::make_pair(KeyLen, DataLen); |
| 210 | } |
| 211 | |
| 212 | static std::pair<const char*, unsigned> |
| 213 | ReadKey(const unsigned char* d, unsigned n) { |
| 214 | assert(n >= 2 && d[n-1] == '\0'); |
| 215 | return std::make_pair((const char*) d, n-1); |
| 216 | } |
| 217 | |
| 218 | IdentifierInfo *ReadData(const internal_key_type& k, |
| 219 | const unsigned char* d, |
| 220 | unsigned DataLen) { |
| 221 | using namespace clang::io; |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 222 | uint32_t Bits = ReadUnalignedLE32(d); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 223 | bool CPlusPlusOperatorKeyword = Bits & 0x01; |
| 224 | Bits >>= 1; |
| 225 | bool Poisoned = Bits & 0x01; |
| 226 | Bits >>= 1; |
| 227 | bool ExtensionToken = Bits & 0x01; |
| 228 | Bits >>= 1; |
| 229 | bool hasMacroDefinition = Bits & 0x01; |
| 230 | Bits >>= 1; |
| 231 | unsigned ObjCOrBuiltinID = Bits & 0x3FF; |
| 232 | Bits >>= 10; |
| 233 | unsigned TokenID = Bits & 0xFF; |
| 234 | Bits >>= 8; |
| 235 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 236 | pch::IdentID ID = ReadUnalignedLE32(d); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 237 | assert(Bits == 0 && "Extra bits in the identifier?"); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 238 | DataLen -= 8; |
| 239 | |
| 240 | // Build the IdentifierInfo itself and link the identifier ID with |
| 241 | // the new IdentifierInfo. |
| 242 | IdentifierInfo *II = KnownII; |
| 243 | if (!II) |
Douglas Gregor | 5f8e330 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 244 | II = &Reader.getIdentifierTable().CreateIdentifierInfo( |
| 245 | k.first, k.first + k.second); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 246 | Reader.SetIdentifierInfo(ID, II); |
| 247 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 248 | // Set or check the various bits in the IdentifierInfo structure. |
| 249 | // FIXME: Load token IDs lazily, too? |
| 250 | assert((unsigned)II->getTokenID() == TokenID && |
| 251 | "Incorrect token ID loaded"); |
| 252 | (void)TokenID; |
| 253 | II->setObjCOrBuiltinID(ObjCOrBuiltinID); |
| 254 | assert(II->isExtensionToken() == ExtensionToken && |
| 255 | "Incorrect extension token flag"); |
| 256 | (void)ExtensionToken; |
| 257 | II->setIsPoisoned(Poisoned); |
| 258 | assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && |
| 259 | "Incorrect C++ operator keyword flag"); |
| 260 | (void)CPlusPlusOperatorKeyword; |
| 261 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 262 | // If this identifier is a macro, deserialize the macro |
| 263 | // definition. |
| 264 | if (hasMacroDefinition) { |
| 265 | uint32_t Offset = ReadUnalignedLE64(d); |
| 266 | Reader.ReadMacroRecord(Offset); |
| 267 | DataLen -= 8; |
| 268 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 269 | |
| 270 | // Read all of the declarations visible at global scope with this |
| 271 | // name. |
| 272 | Sema *SemaObj = Reader.getSema(); |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame^] | 273 | if (!SemaObj) return II; |
| 274 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 275 | while (DataLen > 0) { |
| 276 | NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 277 | if (SemaObj) { |
| 278 | // Introduce this declaration into the translation-unit scope |
| 279 | // and add it to the declaration chain for this identifier, so |
| 280 | // that (unqualified) name lookup will find it. |
| 281 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D)); |
| 282 | SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); |
| 283 | } else { |
| 284 | // Queue this declaration so that it will be added to the |
| 285 | // translation unit scope and identifier's declaration chain |
| 286 | // once a Sema object is known. |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 287 | Reader.PreloadedDecls.push_back(D); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | DataLen -= 4; |
| 291 | } |
| 292 | return II; |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | } // end anonymous namespace |
| 297 | |
| 298 | /// \brief The on-disk hash table used to contain information about |
| 299 | /// all of the identifiers in the program. |
| 300 | typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait> |
| 301 | PCHIdentifierLookupTable; |
| 302 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 303 | // FIXME: use the diagnostics machinery |
| 304 | static bool Error(const char *Str) { |
| 305 | std::fprintf(stderr, "%s\n", Str); |
| 306 | return true; |
| 307 | } |
| 308 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 309 | /// \brief Check the contents of the predefines buffer against the |
| 310 | /// contents of the predefines buffer used to build the PCH file. |
| 311 | /// |
| 312 | /// The contents of the two predefines buffers should be the same. If |
| 313 | /// not, then some command-line option changed the preprocessor state |
| 314 | /// and we must reject the PCH file. |
| 315 | /// |
| 316 | /// \param PCHPredef The start of the predefines buffer in the PCH |
| 317 | /// file. |
| 318 | /// |
| 319 | /// \param PCHPredefLen The length of the predefines buffer in the PCH |
| 320 | /// file. |
| 321 | /// |
| 322 | /// \param PCHBufferID The FileID for the PCH predefines buffer. |
| 323 | /// |
| 324 | /// \returns true if there was a mismatch (in which case the PCH file |
| 325 | /// should be ignored), or false otherwise. |
| 326 | bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef, |
| 327 | unsigned PCHPredefLen, |
| 328 | FileID PCHBufferID) { |
| 329 | const char *Predef = PP.getPredefines().c_str(); |
| 330 | unsigned PredefLen = PP.getPredefines().size(); |
| 331 | |
| 332 | // If the two predefines buffers compare equal, we're done!. |
| 333 | if (PredefLen == PCHPredefLen && |
| 334 | strncmp(Predef, PCHPredef, PCHPredefLen) == 0) |
| 335 | return false; |
| 336 | |
| 337 | // The predefines buffers are different. Produce a reasonable |
| 338 | // diagnostic showing where they are different. |
| 339 | |
| 340 | // The source locations (potentially in the two different predefines |
| 341 | // buffers) |
| 342 | SourceLocation Loc1, Loc2; |
| 343 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 344 | |
| 345 | // Create a source buffer for our predefines string, so |
| 346 | // that we can build a diagnostic that points into that |
| 347 | // source buffer. |
| 348 | FileID BufferID; |
| 349 | if (Predef && Predef[0]) { |
| 350 | llvm::MemoryBuffer *Buffer |
| 351 | = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen, |
| 352 | "<built-in>"); |
| 353 | BufferID = SourceMgr.createFileIDForMemBuffer(Buffer); |
| 354 | } |
| 355 | |
| 356 | unsigned MinLen = std::min(PredefLen, PCHPredefLen); |
| 357 | std::pair<const char *, const char *> Locations |
| 358 | = std::mismatch(Predef, Predef + MinLen, PCHPredef); |
| 359 | |
| 360 | if (Locations.first != Predef + MinLen) { |
| 361 | // We found the location in the two buffers where there is a |
| 362 | // difference. Form source locations to point there (in both |
| 363 | // buffers). |
| 364 | unsigned Offset = Locations.first - Predef; |
| 365 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 366 | .getFileLocWithOffset(Offset); |
| 367 | Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 368 | .getFileLocWithOffset(Offset); |
| 369 | } else if (PredefLen > PCHPredefLen) { |
| 370 | Loc1 = SourceMgr.getLocForStartOfFile(BufferID) |
| 371 | .getFileLocWithOffset(MinLen); |
| 372 | } else { |
| 373 | Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 374 | .getFileLocWithOffset(MinLen); |
| 375 | } |
| 376 | |
| 377 | Diag(Loc1, diag::warn_pch_preprocessor); |
| 378 | if (Loc2.isValid()) |
| 379 | Diag(Loc2, diag::note_predef_in_pch); |
| 380 | Diag(diag::note_ignoring_pch) << FileName; |
| 381 | return true; |
| 382 | } |
| 383 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 384 | //===----------------------------------------------------------------------===// |
| 385 | // Source Manager Deserialization |
| 386 | //===----------------------------------------------------------------------===// |
| 387 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 388 | /// \brief Read the line table in the source manager block. |
| 389 | /// \returns true if ther was an error. |
| 390 | static bool ParseLineTable(SourceManager &SourceMgr, |
| 391 | llvm::SmallVectorImpl<uint64_t> &Record) { |
| 392 | unsigned Idx = 0; |
| 393 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 394 | |
| 395 | // Parse the file names |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 396 | std::map<int, int> FileIDs; |
| 397 | for (int I = 0, N = Record[Idx++]; I != N; ++I) { |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 398 | // Extract the file name |
| 399 | unsigned FilenameLen = Record[Idx++]; |
| 400 | std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); |
| 401 | Idx += FilenameLen; |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 402 | FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), |
| 403 | Filename.size()); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // Parse the line entries |
| 407 | std::vector<LineEntry> Entries; |
| 408 | while (Idx < Record.size()) { |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 409 | int FID = FileIDs[Record[Idx++]]; |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 410 | |
| 411 | // Extract the line entries |
| 412 | unsigned NumEntries = Record[Idx++]; |
| 413 | Entries.clear(); |
| 414 | Entries.reserve(NumEntries); |
| 415 | for (unsigned I = 0; I != NumEntries; ++I) { |
| 416 | unsigned FileOffset = Record[Idx++]; |
| 417 | unsigned LineNo = Record[Idx++]; |
| 418 | int FilenameID = Record[Idx++]; |
| 419 | SrcMgr::CharacteristicKind FileKind |
| 420 | = (SrcMgr::CharacteristicKind)Record[Idx++]; |
| 421 | unsigned IncludeOffset = Record[Idx++]; |
| 422 | Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, |
| 423 | FileKind, IncludeOffset)); |
| 424 | } |
| 425 | LineTable.AddEntry(FID, Entries); |
| 426 | } |
| 427 | |
| 428 | return false; |
| 429 | } |
| 430 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 431 | namespace { |
| 432 | |
| 433 | class VISIBILITY_HIDDEN PCHStatData { |
| 434 | public: |
| 435 | const bool hasStat; |
| 436 | const ino_t ino; |
| 437 | const dev_t dev; |
| 438 | const mode_t mode; |
| 439 | const time_t mtime; |
| 440 | const off_t size; |
| 441 | |
| 442 | PCHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s) |
| 443 | : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {} |
| 444 | |
| 445 | PCHStatData() |
| 446 | : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {} |
| 447 | }; |
| 448 | |
| 449 | class VISIBILITY_HIDDEN PCHStatLookupTrait { |
| 450 | public: |
| 451 | typedef const char *external_key_type; |
| 452 | typedef const char *internal_key_type; |
| 453 | |
| 454 | typedef PCHStatData data_type; |
| 455 | |
| 456 | static unsigned ComputeHash(const char *path) { |
| 457 | return BernsteinHash(path); |
| 458 | } |
| 459 | |
| 460 | static internal_key_type GetInternalKey(const char *path) { return path; } |
| 461 | |
| 462 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 463 | return strcmp(a, b) == 0; |
| 464 | } |
| 465 | |
| 466 | static std::pair<unsigned, unsigned> |
| 467 | ReadKeyDataLength(const unsigned char*& d) { |
| 468 | unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); |
| 469 | unsigned DataLen = (unsigned) *d++; |
| 470 | return std::make_pair(KeyLen + 1, DataLen); |
| 471 | } |
| 472 | |
| 473 | static internal_key_type ReadKey(const unsigned char *d, unsigned) { |
| 474 | return (const char *)d; |
| 475 | } |
| 476 | |
| 477 | static data_type ReadData(const internal_key_type, const unsigned char *d, |
| 478 | unsigned /*DataLen*/) { |
| 479 | using namespace clang::io; |
| 480 | |
| 481 | if (*d++ == 1) |
| 482 | return data_type(); |
| 483 | |
| 484 | ino_t ino = (ino_t) ReadUnalignedLE32(d); |
| 485 | dev_t dev = (dev_t) ReadUnalignedLE32(d); |
| 486 | mode_t mode = (mode_t) ReadUnalignedLE16(d); |
| 487 | time_t mtime = (time_t) ReadUnalignedLE64(d); |
| 488 | off_t size = (off_t) ReadUnalignedLE64(d); |
| 489 | return data_type(ino, dev, mode, mtime, size); |
| 490 | } |
| 491 | }; |
| 492 | |
| 493 | /// \brief stat() cache for precompiled headers. |
| 494 | /// |
| 495 | /// This cache is very similar to the stat cache used by pretokenized |
| 496 | /// headers. |
| 497 | class VISIBILITY_HIDDEN PCHStatCache : public StatSysCallCache { |
| 498 | typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy; |
| 499 | CacheTy *Cache; |
| 500 | |
| 501 | unsigned &NumStatHits, &NumStatMisses; |
| 502 | public: |
| 503 | PCHStatCache(const unsigned char *Buckets, |
| 504 | const unsigned char *Base, |
| 505 | unsigned &NumStatHits, |
| 506 | unsigned &NumStatMisses) |
| 507 | : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) { |
| 508 | Cache = CacheTy::Create(Buckets, Base); |
| 509 | } |
| 510 | |
| 511 | ~PCHStatCache() { delete Cache; } |
| 512 | |
| 513 | int stat(const char *path, struct stat *buf) { |
| 514 | // Do the lookup for the file's data in the PCH file. |
| 515 | CacheTy::iterator I = Cache->find(path); |
| 516 | |
| 517 | // If we don't get a hit in the PCH file just forward to 'stat'. |
| 518 | if (I == Cache->end()) { |
| 519 | ++NumStatMisses; |
| 520 | return ::stat(path, buf); |
| 521 | } |
| 522 | |
| 523 | ++NumStatHits; |
| 524 | PCHStatData Data = *I; |
| 525 | |
| 526 | if (!Data.hasStat) |
| 527 | return 1; |
| 528 | |
| 529 | buf->st_ino = Data.ino; |
| 530 | buf->st_dev = Data.dev; |
| 531 | buf->st_mtime = Data.mtime; |
| 532 | buf->st_mode = Data.mode; |
| 533 | buf->st_size = Data.size; |
| 534 | return 0; |
| 535 | } |
| 536 | }; |
| 537 | } // end anonymous namespace |
| 538 | |
| 539 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 540 | /// \brief Read the source manager block |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 541 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 542 | using namespace SrcMgr; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 543 | |
| 544 | // Set the source-location entry cursor to the current position in |
| 545 | // the stream. This cursor will be used to read the contents of the |
| 546 | // source manager block initially, and then lazily read |
| 547 | // source-location entries as needed. |
| 548 | SLocEntryCursor = Stream; |
| 549 | |
| 550 | // The stream itself is going to skip over the source manager block. |
| 551 | if (Stream.SkipBlock()) { |
| 552 | Error("Malformed block record"); |
| 553 | return Failure; |
| 554 | } |
| 555 | |
| 556 | // Enter the source manager block. |
| 557 | if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 558 | Error("Malformed source manager block record"); |
| 559 | return Failure; |
| 560 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 561 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 562 | SourceManager &SourceMgr = PP.getSourceManager(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 563 | RecordData Record; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 564 | unsigned NumHeaderInfos = 0; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 565 | while (true) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 566 | unsigned Code = SLocEntryCursor.ReadCode(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 567 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 568 | if (SLocEntryCursor.ReadBlockEnd()) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 569 | Error("Error at end of Source Manager block"); |
| 570 | return Failure; |
| 571 | } |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 572 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 576 | // No known subblocks, always skip them. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 577 | SLocEntryCursor.ReadSubBlockID(); |
| 578 | if (SLocEntryCursor.SkipBlock()) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 579 | Error("Malformed block record"); |
| 580 | return Failure; |
| 581 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 582 | continue; |
| 583 | } |
| 584 | |
| 585 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 586 | SLocEntryCursor.ReadAbbrevRecord(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 587 | continue; |
| 588 | } |
| 589 | |
| 590 | // Read a record. |
| 591 | const char *BlobStart; |
| 592 | unsigned BlobLen; |
| 593 | Record.clear(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 594 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 595 | default: // Default behavior: ignore. |
| 596 | break; |
| 597 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 598 | case pch::SM_LINE_TABLE: |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 599 | if (ParseLineTable(SourceMgr, Record)) |
| 600 | return Failure; |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 601 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 602 | |
| 603 | case pch::SM_HEADER_FILE_INFO: { |
| 604 | HeaderFileInfo HFI; |
| 605 | HFI.isImport = Record[0]; |
| 606 | HFI.DirInfo = Record[1]; |
| 607 | HFI.NumIncludes = Record[2]; |
| 608 | HFI.ControllingMacroID = Record[3]; |
| 609 | PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 610 | break; |
| 611 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 612 | |
| 613 | case pch::SM_SLOC_FILE_ENTRY: |
| 614 | case pch::SM_SLOC_BUFFER_ENTRY: |
| 615 | case pch::SM_SLOC_INSTANTIATION_ENTRY: |
| 616 | // Once we hit one of the source location entries, we're done. |
| 617 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 622 | /// \brief Read in the source location entry with the given ID. |
| 623 | PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) { |
| 624 | if (ID == 0) |
| 625 | return Success; |
| 626 | |
| 627 | if (ID > TotalNumSLocEntries) { |
| 628 | Error("source location entry ID out-of-range for PCH file"); |
| 629 | return Failure; |
| 630 | } |
| 631 | |
| 632 | ++NumSLocEntriesRead; |
| 633 | SLocEntryCursor.JumpToBit(SLocOffsets[ID - 1]); |
| 634 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 635 | if (Code == llvm::bitc::END_BLOCK || |
| 636 | Code == llvm::bitc::ENTER_SUBBLOCK || |
| 637 | Code == llvm::bitc::DEFINE_ABBREV) { |
| 638 | Error("incorrectly-formatted source location entry in PCH file"); |
| 639 | return Failure; |
| 640 | } |
| 641 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 642 | SourceManager &SourceMgr = PP.getSourceManager(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 643 | RecordData Record; |
| 644 | const char *BlobStart; |
| 645 | unsigned BlobLen; |
| 646 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 647 | default: |
| 648 | Error("incorrectly-formatted source location entry in PCH file"); |
| 649 | return Failure; |
| 650 | |
| 651 | case pch::SM_SLOC_FILE_ENTRY: { |
| 652 | const FileEntry *File |
| 653 | = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen); |
| 654 | // FIXME: Error recovery if file cannot be found. |
| 655 | FileID FID = SourceMgr.createFileID(File, |
| 656 | SourceLocation::getFromRawEncoding(Record[1]), |
| 657 | (SrcMgr::CharacteristicKind)Record[2], |
| 658 | ID, Record[0]); |
| 659 | if (Record[3]) |
| 660 | const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()) |
| 661 | .setHasLineDirectives(); |
| 662 | |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | case pch::SM_SLOC_BUFFER_ENTRY: { |
| 667 | const char *Name = BlobStart; |
| 668 | unsigned Offset = Record[0]; |
| 669 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 670 | Record.clear(); |
| 671 | unsigned RecCode |
| 672 | = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); |
| 673 | assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file"); |
| 674 | (void)RecCode; |
| 675 | llvm::MemoryBuffer *Buffer |
| 676 | = llvm::MemoryBuffer::getMemBuffer(BlobStart, |
| 677 | BlobStart + BlobLen - 1, |
| 678 | Name); |
| 679 | FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset); |
| 680 | |
| 681 | if (strcmp(Name, "<built-in>") == 0 |
| 682 | && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID)) |
| 683 | return IgnorePCH; |
| 684 | |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | case pch::SM_SLOC_INSTANTIATION_ENTRY: { |
| 689 | SourceLocation SpellingLoc |
| 690 | = SourceLocation::getFromRawEncoding(Record[1]); |
| 691 | SourceMgr.createInstantiationLoc(SpellingLoc, |
| 692 | SourceLocation::getFromRawEncoding(Record[2]), |
| 693 | SourceLocation::getFromRawEncoding(Record[3]), |
| 694 | Record[4], |
| 695 | ID, |
| 696 | Record[0]); |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | return Success; |
| 702 | } |
| 703 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 704 | /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the |
| 705 | /// specified cursor. Read the abbreviations that are at the top of the block |
| 706 | /// and then leave the cursor pointing into the block. |
| 707 | bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, |
| 708 | unsigned BlockID) { |
| 709 | if (Cursor.EnterSubBlock(BlockID)) { |
| 710 | Error("Malformed block record"); |
| 711 | return Failure; |
| 712 | } |
| 713 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 714 | while (true) { |
| 715 | unsigned Code = Cursor.ReadCode(); |
| 716 | |
| 717 | // We expect all abbrevs to be at the start of the block. |
| 718 | if (Code != llvm::bitc::DEFINE_ABBREV) |
| 719 | return false; |
| 720 | Cursor.ReadAbbrevRecord(); |
| 721 | } |
| 722 | } |
| 723 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 724 | void PCHReader::ReadMacroRecord(uint64_t Offset) { |
| 725 | // Keep track of where we are in the stream, then jump back there |
| 726 | // after reading this macro. |
| 727 | SavedStreamPosition SavedPosition(Stream); |
| 728 | |
| 729 | Stream.JumpToBit(Offset); |
| 730 | RecordData Record; |
| 731 | llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; |
| 732 | MacroInfo *Macro = 0; |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 733 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 734 | while (true) { |
| 735 | unsigned Code = Stream.ReadCode(); |
| 736 | switch (Code) { |
| 737 | case llvm::bitc::END_BLOCK: |
| 738 | return; |
| 739 | |
| 740 | case llvm::bitc::ENTER_SUBBLOCK: |
| 741 | // No known subblocks, always skip them. |
| 742 | Stream.ReadSubBlockID(); |
| 743 | if (Stream.SkipBlock()) { |
| 744 | Error("Malformed block record"); |
| 745 | return; |
| 746 | } |
| 747 | continue; |
| 748 | |
| 749 | case llvm::bitc::DEFINE_ABBREV: |
| 750 | Stream.ReadAbbrevRecord(); |
| 751 | continue; |
| 752 | default: break; |
| 753 | } |
| 754 | |
| 755 | // Read a record. |
| 756 | Record.clear(); |
| 757 | pch::PreprocessorRecordTypes RecType = |
| 758 | (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); |
| 759 | switch (RecType) { |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 760 | case pch::PP_MACRO_OBJECT_LIKE: |
| 761 | case pch::PP_MACRO_FUNCTION_LIKE: { |
| 762 | // If we already have a macro, that means that we've hit the end |
| 763 | // of the definition of the macro we were looking for. We're |
| 764 | // done. |
| 765 | if (Macro) |
| 766 | return; |
| 767 | |
| 768 | IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); |
| 769 | if (II == 0) { |
| 770 | Error("Macro must have a name"); |
| 771 | return; |
| 772 | } |
| 773 | SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]); |
| 774 | bool isUsed = Record[2]; |
| 775 | |
| 776 | MacroInfo *MI = PP.AllocateMacroInfo(Loc); |
| 777 | MI->setIsUsed(isUsed); |
| 778 | |
| 779 | if (RecType == pch::PP_MACRO_FUNCTION_LIKE) { |
| 780 | // Decode function-like macro info. |
| 781 | bool isC99VarArgs = Record[3]; |
| 782 | bool isGNUVarArgs = Record[4]; |
| 783 | MacroArgs.clear(); |
| 784 | unsigned NumArgs = Record[5]; |
| 785 | for (unsigned i = 0; i != NumArgs; ++i) |
| 786 | MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); |
| 787 | |
| 788 | // Install function-like macro info. |
| 789 | MI->setIsFunctionLike(); |
| 790 | if (isC99VarArgs) MI->setIsC99Varargs(); |
| 791 | if (isGNUVarArgs) MI->setIsGNUVarargs(); |
| 792 | MI->setArgumentList(&MacroArgs[0], MacroArgs.size(), |
| 793 | PP.getPreprocessorAllocator()); |
| 794 | } |
| 795 | |
| 796 | // Finally, install the macro. |
| 797 | PP.setMacroInfo(II, MI); |
| 798 | |
| 799 | // Remember that we saw this macro last so that we add the tokens that |
| 800 | // form its body to it. |
| 801 | Macro = MI; |
| 802 | ++NumMacrosRead; |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | case pch::PP_TOKEN: { |
| 807 | // If we see a TOKEN before a PP_MACRO_*, then the file is |
| 808 | // erroneous, just pretend we didn't see this. |
| 809 | if (Macro == 0) break; |
| 810 | |
| 811 | Token Tok; |
| 812 | Tok.startToken(); |
| 813 | Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0])); |
| 814 | Tok.setLength(Record[1]); |
| 815 | if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) |
| 816 | Tok.setIdentifierInfo(II); |
| 817 | Tok.setKind((tok::TokenKind)Record[3]); |
| 818 | Tok.setFlag((Token::TokenFlags)Record[4]); |
| 819 | Macro->AddTokenToBody(Tok); |
| 820 | break; |
| 821 | } |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 822 | } |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 826 | PCHReader::PCHReadResult |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 827 | PCHReader::ReadPCHBlock() { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 828 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
| 829 | Error("Malformed block record"); |
| 830 | return Failure; |
| 831 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 832 | |
| 833 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 834 | RecordData Record; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 835 | while (!Stream.AtEndOfStream()) { |
| 836 | unsigned Code = Stream.ReadCode(); |
| 837 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 838 | if (Stream.ReadBlockEnd()) { |
| 839 | Error("Error at end of module block"); |
| 840 | return Failure; |
| 841 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 842 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 843 | return Success; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 847 | switch (Stream.ReadSubBlockID()) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 848 | case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded) |
| 849 | default: // Skip unknown content. |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 850 | if (Stream.SkipBlock()) { |
| 851 | Error("Malformed block record"); |
| 852 | return Failure; |
| 853 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 854 | break; |
| 855 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 856 | case pch::DECLS_BLOCK_ID: |
| 857 | // We lazily load the decls block, but we want to set up the |
| 858 | // DeclsCursor cursor to point into it. Clone our current bitcode |
| 859 | // cursor to it, enter the block and read the abbrevs in that block. |
| 860 | // With the main cursor, we just skip over it. |
| 861 | DeclsCursor = Stream; |
| 862 | if (Stream.SkipBlock() || // Skip with the main cursor. |
| 863 | // Read the abbrevs. |
| 864 | ReadBlockAbbrevs(DeclsCursor, pch::DECLS_BLOCK_ID)) { |
| 865 | Error("Malformed block record"); |
| 866 | return Failure; |
| 867 | } |
| 868 | break; |
| 869 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 870 | case pch::PREPROCESSOR_BLOCK_ID: |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 871 | if (Stream.SkipBlock()) { |
| 872 | Error("Malformed block record"); |
| 873 | return Failure; |
| 874 | } |
| 875 | break; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 877 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 878 | switch (ReadSourceManagerBlock()) { |
| 879 | case Success: |
| 880 | break; |
| 881 | |
| 882 | case Failure: |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 883 | Error("Malformed source manager block"); |
| 884 | return Failure; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 885 | |
| 886 | case IgnorePCH: |
| 887 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 888 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 889 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 890 | } |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 891 | continue; |
| 892 | } |
| 893 | |
| 894 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 895 | Stream.ReadAbbrevRecord(); |
| 896 | continue; |
| 897 | } |
| 898 | |
| 899 | // Read and process a record. |
| 900 | Record.clear(); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 901 | const char *BlobStart = 0; |
| 902 | unsigned BlobLen = 0; |
| 903 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
| 904 | &BlobStart, &BlobLen)) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 905 | default: // Default behavior: ignore. |
| 906 | break; |
| 907 | |
| 908 | case pch::TYPE_OFFSET: |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 909 | if (!TypesLoaded.empty()) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 910 | Error("Duplicate TYPE_OFFSET record in PCH file"); |
| 911 | return Failure; |
| 912 | } |
Chris Lattner | c732f5a | 2009-04-27 18:24:17 +0000 | [diff] [blame] | 913 | TypeOffsets = (const uint32_t *)BlobStart; |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 914 | TypesLoaded.resize(Record[0]); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 915 | break; |
| 916 | |
| 917 | case pch::DECL_OFFSET: |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 918 | if (!DeclsLoaded.empty()) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 919 | Error("Duplicate DECL_OFFSET record in PCH file"); |
| 920 | return Failure; |
| 921 | } |
Chris Lattner | c732f5a | 2009-04-27 18:24:17 +0000 | [diff] [blame] | 922 | DeclOffsets = (const uint32_t *)BlobStart; |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 923 | DeclsLoaded.resize(Record[0]); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 924 | break; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 925 | |
| 926 | case pch::LANGUAGE_OPTIONS: |
| 927 | if (ParseLanguageOptions(Record)) |
| 928 | return IgnorePCH; |
| 929 | break; |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 931 | case pch::TARGET_TRIPLE: { |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 932 | std::string TargetTriple(BlobStart, BlobLen); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 933 | if (TargetTriple != PP.getTargetInfo().getTargetTriple()) { |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 934 | Diag(diag::warn_pch_target_triple) |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 935 | << TargetTriple << PP.getTargetInfo().getTargetTriple(); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 936 | Diag(diag::note_ignoring_pch) << FileName; |
| 937 | return IgnorePCH; |
| 938 | } |
| 939 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 940 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 941 | |
| 942 | case pch::IDENTIFIER_TABLE: |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 943 | IdentifierTableData = BlobStart; |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 944 | if (Record[0]) { |
| 945 | IdentifierLookupTable |
| 946 | = PCHIdentifierLookupTable::Create( |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 947 | (const unsigned char *)IdentifierTableData + Record[0], |
| 948 | (const unsigned char *)IdentifierTableData, |
| 949 | PCHIdentifierLookupTrait(*this)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 950 | PP.getIdentifierTable().setExternalIdentifierLookup(this); |
| 951 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 952 | break; |
| 953 | |
| 954 | case pch::IDENTIFIER_OFFSET: |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 955 | if (!IdentifiersLoaded.empty()) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 956 | Error("Duplicate IDENTIFIER_OFFSET record in PCH file"); |
| 957 | return Failure; |
| 958 | } |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 959 | IdentifierOffsets = (const uint32_t *)BlobStart; |
| 960 | IdentifiersLoaded.resize(Record[0]); |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 961 | PP.getHeaderSearchInfo().SetExternalLookup(this); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 962 | break; |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 963 | |
| 964 | case pch::EXTERNAL_DEFINITIONS: |
| 965 | if (!ExternalDefinitions.empty()) { |
| 966 | Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file"); |
| 967 | return Failure; |
| 968 | } |
| 969 | ExternalDefinitions.swap(Record); |
| 970 | break; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 971 | |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 972 | case pch::SPECIAL_TYPES: |
| 973 | SpecialTypes.swap(Record); |
| 974 | break; |
| 975 | |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 976 | case pch::STATISTICS: |
| 977 | TotalNumStatements = Record[0]; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 978 | TotalNumMacros = Record[1]; |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 979 | TotalLexicalDeclContexts = Record[2]; |
| 980 | TotalVisibleDeclContexts = Record[3]; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 981 | break; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 982 | |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 983 | case pch::TENTATIVE_DEFINITIONS: |
| 984 | if (!TentativeDefinitions.empty()) { |
| 985 | Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file"); |
| 986 | return Failure; |
| 987 | } |
| 988 | TentativeDefinitions.swap(Record); |
| 989 | break; |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 990 | |
| 991 | case pch::LOCALLY_SCOPED_EXTERNAL_DECLS: |
| 992 | if (!LocallyScopedExternalDecls.empty()) { |
| 993 | Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file"); |
| 994 | return Failure; |
| 995 | } |
| 996 | LocallyScopedExternalDecls.swap(Record); |
| 997 | break; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 998 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 999 | case pch::SELECTOR_OFFSETS: |
| 1000 | SelectorOffsets = (const uint32_t *)BlobStart; |
| 1001 | TotalNumSelectors = Record[0]; |
| 1002 | SelectorsLoaded.resize(TotalNumSelectors); |
| 1003 | break; |
| 1004 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1005 | case pch::METHOD_POOL: |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1006 | MethodPoolLookupTableData = (const unsigned char *)BlobStart; |
| 1007 | if (Record[0]) |
| 1008 | MethodPoolLookupTable |
| 1009 | = PCHMethodPoolLookupTable::Create( |
| 1010 | MethodPoolLookupTableData + Record[0], |
| 1011 | MethodPoolLookupTableData, |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1012 | PCHMethodPoolLookupTrait(*this)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1013 | TotalSelectorsInMethodPool = Record[1]; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1014 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1015 | |
| 1016 | case pch::PP_COUNTER_VALUE: |
| 1017 | if (!Record.empty()) |
| 1018 | PP.setCounterValue(Record[0]); |
| 1019 | break; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1020 | |
| 1021 | case pch::SOURCE_LOCATION_OFFSETS: |
Chris Lattner | 090d9b5 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 1022 | SLocOffsets = (const uint32_t *)BlobStart; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1023 | TotalNumSLocEntries = Record[0]; |
| 1024 | PP.getSourceManager().PreallocateSLocEntries(this, |
| 1025 | TotalNumSLocEntries, |
| 1026 | Record[1]); |
| 1027 | break; |
| 1028 | |
| 1029 | case pch::SOURCE_LOCATION_PRELOADS: |
| 1030 | for (unsigned I = 0, N = Record.size(); I != N; ++I) { |
| 1031 | PCHReadResult Result = ReadSLocEntryRecord(Record[I]); |
| 1032 | if (Result != Success) |
| 1033 | return Result; |
| 1034 | } |
| 1035 | break; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1036 | |
| 1037 | case pch::STAT_CACHE: |
| 1038 | PP.getFileManager().setStatCache( |
| 1039 | new PCHStatCache((const unsigned char *)BlobStart + Record[0], |
| 1040 | (const unsigned char *)BlobStart, |
| 1041 | NumStatHits, NumStatMisses)); |
| 1042 | break; |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1043 | |
| 1044 | case pch::EXT_VECTOR_DECLS: |
| 1045 | if (!ExtVectorDecls.empty()) { |
| 1046 | Error("Duplicate EXT_VECTOR_DECLS record in PCH file"); |
| 1047 | return Failure; |
| 1048 | } |
| 1049 | ExtVectorDecls.swap(Record); |
| 1050 | break; |
| 1051 | |
| 1052 | case pch::OBJC_CATEGORY_IMPLEMENTATIONS: |
| 1053 | if (!ObjCCategoryImpls.empty()) { |
| 1054 | Error("Duplicate OBJC_CATEGORY_IMPLEMENTATIONS record in PCH file"); |
| 1055 | return Failure; |
| 1056 | } |
| 1057 | ObjCCategoryImpls.swap(Record); |
| 1058 | break; |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1059 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1060 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1061 | Error("Premature end of bitstream"); |
| 1062 | return Failure; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1065 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1066 | // Set the PCH file name. |
| 1067 | this->FileName = FileName; |
| 1068 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1069 | // Open the PCH file. |
| 1070 | std::string ErrStr; |
| 1071 | Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr)); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1072 | if (!Buffer) { |
| 1073 | Error(ErrStr.c_str()); |
| 1074 | return IgnorePCH; |
| 1075 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Initialize the stream |
Chris Lattner | b9fa917 | 2009-04-26 20:59:20 +0000 | [diff] [blame] | 1078 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
| 1079 | (const unsigned char *)Buffer->getBufferEnd()); |
| 1080 | Stream.init(StreamFile); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1081 | |
| 1082 | // Sniff for the signature. |
| 1083 | if (Stream.Read(8) != 'C' || |
| 1084 | Stream.Read(8) != 'P' || |
| 1085 | Stream.Read(8) != 'C' || |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1086 | Stream.Read(8) != 'H') { |
| 1087 | Error("Not a PCH file"); |
| 1088 | return IgnorePCH; |
| 1089 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1090 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1091 | while (!Stream.AtEndOfStream()) { |
| 1092 | unsigned Code = Stream.ReadCode(); |
| 1093 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1094 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
| 1095 | Error("Invalid record at top-level"); |
| 1096 | return Failure; |
| 1097 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1098 | |
| 1099 | unsigned BlockID = Stream.ReadSubBlockID(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1101 | // We only know the PCH subblock ID. |
| 1102 | switch (BlockID) { |
| 1103 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1104 | if (Stream.ReadBlockInfoBlock()) { |
| 1105 | Error("Malformed BlockInfoBlock"); |
| 1106 | return Failure; |
| 1107 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1108 | break; |
| 1109 | case pch::PCH_BLOCK_ID: |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1110 | switch (ReadPCHBlock()) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1111 | case Success: |
| 1112 | break; |
| 1113 | |
| 1114 | case Failure: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1115 | return Failure; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1116 | |
| 1117 | case IgnorePCH: |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1118 | // FIXME: We could consider reading through to the end of this |
| 1119 | // PCH block, skipping subblocks, to see if there are other |
| 1120 | // PCH blocks elsewhere. |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Clear out any preallocated source location entries, so that |
| 1123 | // the source manager does not try to resolve them later. |
| 1124 | PP.getSourceManager().ClearPreallocatedSLocEntries(); |
| 1125 | |
| 1126 | // Remove the stat cache. |
| 1127 | PP.getFileManager().setStatCache(0); |
| 1128 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1129 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1130 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1131 | break; |
| 1132 | default: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1133 | if (Stream.SkipBlock()) { |
| 1134 | Error("Malformed block record"); |
| 1135 | return Failure; |
| 1136 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | // Load the translation unit declaration |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1142 | if (Context) |
| 1143 | ReadDeclRecord(DeclOffsets[0], 0); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1145 | // Initialization of builtins and library builtins occurs before the |
| 1146 | // PCH file is read, so there may be some identifiers that were |
| 1147 | // loaded into the IdentifierTable before we intercepted the |
| 1148 | // creation of identifiers. Iterate through the list of known |
| 1149 | // identifiers and determine whether we have to establish |
| 1150 | // preprocessor definitions or top-level identifier declaration |
| 1151 | // chains for those identifiers. |
| 1152 | // |
| 1153 | // We copy the IdentifierInfo pointers to a small vector first, |
| 1154 | // since de-serializing declarations or macro definitions can add |
| 1155 | // new entries into the identifier table, invalidating the |
| 1156 | // iterators. |
| 1157 | llvm::SmallVector<IdentifierInfo *, 128> Identifiers; |
| 1158 | for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), |
| 1159 | IdEnd = PP.getIdentifierTable().end(); |
| 1160 | Id != IdEnd; ++Id) |
| 1161 | Identifiers.push_back(Id->second); |
| 1162 | PCHIdentifierLookupTable *IdTable |
| 1163 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
| 1164 | for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { |
| 1165 | IdentifierInfo *II = Identifiers[I]; |
| 1166 | // Look in the on-disk hash table for an entry for |
| 1167 | PCHIdentifierLookupTrait Info(*this, II); |
| 1168 | std::pair<const char*, unsigned> Key(II->getName(), II->getLength()); |
| 1169 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); |
| 1170 | if (Pos == IdTable->end()) |
| 1171 | continue; |
| 1172 | |
| 1173 | // Dereferencing the iterator has the effect of populating the |
| 1174 | // IdentifierInfo node with the various declarations it needs. |
| 1175 | (void)*Pos; |
| 1176 | } |
| 1177 | |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1178 | // Load the special types. |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1179 | if (Context) { |
| 1180 | Context->setBuiltinVaListType( |
| 1181 | GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST])); |
| 1182 | if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID]) |
| 1183 | Context->setObjCIdType(GetType(Id)); |
| 1184 | if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR]) |
| 1185 | Context->setObjCSelType(GetType(Sel)); |
| 1186 | if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL]) |
| 1187 | Context->setObjCProtoType(GetType(Proto)); |
| 1188 | if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS]) |
| 1189 | Context->setObjCClassType(GetType(Class)); |
| 1190 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING]) |
| 1191 | Context->setCFConstantStringType(GetType(String)); |
| 1192 | if (unsigned FastEnum |
| 1193 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) |
| 1194 | Context->setObjCFastEnumerationStateType(GetType(FastEnum)); |
| 1195 | } |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1196 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1197 | return Success; |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1200 | /// \brief Parse the record that corresponds to a LangOptions data |
| 1201 | /// structure. |
| 1202 | /// |
| 1203 | /// This routine compares the language options used to generate the |
| 1204 | /// PCH file against the language options set for the current |
| 1205 | /// compilation. For each option, we classify differences between the |
| 1206 | /// two compiler states as either "benign" or "important". Benign |
| 1207 | /// differences don't matter, and we accept them without complaint |
| 1208 | /// (and without modifying the language options). Differences between |
| 1209 | /// the states for important options cause the PCH file to be |
| 1210 | /// unusable, so we emit a warning and return true to indicate that |
| 1211 | /// there was an error. |
| 1212 | /// |
| 1213 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 1214 | bool PCHReader::ParseLanguageOptions( |
| 1215 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1216 | const LangOptions &LangOpts = PP.getLangOptions(); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1217 | #define PARSE_LANGOPT_BENIGN(Option) ++Idx |
| 1218 | #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ |
| 1219 | if (Record[Idx] != LangOpts.Option) { \ |
| 1220 | Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \ |
| 1221 | Diag(diag::note_ignoring_pch) << FileName; \ |
| 1222 | return true; \ |
| 1223 | } \ |
| 1224 | ++Idx |
| 1225 | |
| 1226 | unsigned Idx = 0; |
| 1227 | PARSE_LANGOPT_BENIGN(Trigraphs); |
| 1228 | PARSE_LANGOPT_BENIGN(BCPLComment); |
| 1229 | PARSE_LANGOPT_BENIGN(DollarIdents); |
| 1230 | PARSE_LANGOPT_BENIGN(AsmPreprocessor); |
| 1231 | PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); |
| 1232 | PARSE_LANGOPT_BENIGN(ImplicitInt); |
| 1233 | PARSE_LANGOPT_BENIGN(Digraphs); |
| 1234 | PARSE_LANGOPT_BENIGN(HexFloats); |
| 1235 | PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); |
| 1236 | PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); |
| 1237 | PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); |
| 1238 | PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); |
| 1239 | PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions); |
| 1240 | PARSE_LANGOPT_BENIGN(CXXOperatorName); |
| 1241 | PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); |
| 1242 | PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); |
| 1243 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); |
| 1244 | PARSE_LANGOPT_BENIGN(PascalStrings); |
| 1245 | PARSE_LANGOPT_BENIGN(Boolean); |
| 1246 | PARSE_LANGOPT_BENIGN(WritableStrings); |
| 1247 | PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, |
| 1248 | diag::warn_pch_lax_vector_conversions); |
| 1249 | PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); |
| 1250 | PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); |
| 1251 | PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); |
| 1252 | PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); |
| 1253 | PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, |
| 1254 | diag::warn_pch_thread_safe_statics); |
| 1255 | PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); |
| 1256 | PARSE_LANGOPT_BENIGN(EmitAllDecls); |
| 1257 | PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); |
| 1258 | PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking); |
| 1259 | PARSE_LANGOPT_IMPORTANT(HeinousExtensions, |
| 1260 | diag::warn_pch_heinous_extensions); |
| 1261 | // FIXME: Most of the options below are benign if the macro wasn't |
| 1262 | // used. Unfortunately, this means that a PCH compiled without |
| 1263 | // optimization can't be used with optimization turned on, even |
| 1264 | // though the only thing that changes is whether __OPTIMIZE__ was |
| 1265 | // defined... but if __OPTIMIZE__ never showed up in the header, it |
| 1266 | // doesn't matter. We could consider making this some special kind |
| 1267 | // of check. |
| 1268 | PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); |
| 1269 | PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); |
| 1270 | PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); |
| 1271 | PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); |
| 1272 | PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); |
| 1273 | PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); |
| 1274 | if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) { |
| 1275 | Diag(diag::warn_pch_gc_mode) |
| 1276 | << (unsigned)Record[Idx] << LangOpts.getGCMode(); |
| 1277 | Diag(diag::note_ignoring_pch) << FileName; |
| 1278 | return true; |
| 1279 | } |
| 1280 | ++Idx; |
| 1281 | PARSE_LANGOPT_BENIGN(getVisibilityMode()); |
| 1282 | PARSE_LANGOPT_BENIGN(InstantiationDepth); |
| 1283 | #undef PARSE_LANGOPT_IRRELEVANT |
| 1284 | #undef PARSE_LANGOPT_BENIGN |
| 1285 | |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1289 | /// \brief Read and return the type at the given offset. |
| 1290 | /// |
| 1291 | /// This routine actually reads the record corresponding to the type |
| 1292 | /// at the given offset in the bitstream. It is a helper routine for |
| 1293 | /// GetType, which deals with reading type IDs. |
| 1294 | QualType PCHReader::ReadTypeRecord(uint64_t Offset) { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1295 | // Keep track of where we are in the stream, then jump back there |
| 1296 | // after reading this type. |
| 1297 | SavedStreamPosition SavedPosition(Stream); |
| 1298 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1299 | Stream.JumpToBit(Offset); |
| 1300 | RecordData Record; |
| 1301 | unsigned Code = Stream.ReadCode(); |
| 1302 | switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) { |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1303 | case pch::TYPE_EXT_QUAL: { |
| 1304 | assert(Record.size() == 3 && |
| 1305 | "Incorrect encoding of extended qualifier type"); |
| 1306 | QualType Base = GetType(Record[0]); |
| 1307 | QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1]; |
| 1308 | unsigned AddressSpace = Record[2]; |
| 1309 | |
| 1310 | QualType T = Base; |
| 1311 | if (GCAttr != QualType::GCNone) |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1312 | T = Context->getObjCGCQualType(T, GCAttr); |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1313 | if (AddressSpace) |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1314 | T = Context->getAddrSpaceQualType(T, AddressSpace); |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1315 | return T; |
| 1316 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1318 | case pch::TYPE_FIXED_WIDTH_INT: { |
| 1319 | assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1320 | return Context->getFixedWidthIntType(Record[0], Record[1]); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | case pch::TYPE_COMPLEX: { |
| 1324 | assert(Record.size() == 1 && "Incorrect encoding of complex type"); |
| 1325 | QualType ElemType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1326 | return Context->getComplexType(ElemType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | case pch::TYPE_POINTER: { |
| 1330 | assert(Record.size() == 1 && "Incorrect encoding of pointer type"); |
| 1331 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1332 | return Context->getPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | case pch::TYPE_BLOCK_POINTER: { |
| 1336 | assert(Record.size() == 1 && "Incorrect encoding of block pointer type"); |
| 1337 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1338 | return Context->getBlockPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | case pch::TYPE_LVALUE_REFERENCE: { |
| 1342 | assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type"); |
| 1343 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1344 | return Context->getLValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | case pch::TYPE_RVALUE_REFERENCE: { |
| 1348 | assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type"); |
| 1349 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1350 | return Context->getRValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | case pch::TYPE_MEMBER_POINTER: { |
| 1354 | assert(Record.size() == 1 && "Incorrect encoding of member pointer type"); |
| 1355 | QualType PointeeType = GetType(Record[0]); |
| 1356 | QualType ClassType = GetType(Record[1]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1357 | return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1360 | case pch::TYPE_CONSTANT_ARRAY: { |
| 1361 | QualType ElementType = GetType(Record[0]); |
| 1362 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1363 | unsigned IndexTypeQuals = Record[2]; |
| 1364 | unsigned Idx = 3; |
| 1365 | llvm::APInt Size = ReadAPInt(Record, Idx); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1366 | return Context->getConstantArrayType(ElementType, Size, ASM,IndexTypeQuals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | case pch::TYPE_INCOMPLETE_ARRAY: { |
| 1370 | QualType ElementType = GetType(Record[0]); |
| 1371 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1372 | unsigned IndexTypeQuals = Record[2]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1373 | return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | case pch::TYPE_VARIABLE_ARRAY: { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1377 | QualType ElementType = GetType(Record[0]); |
| 1378 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1379 | unsigned IndexTypeQuals = Record[2]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1380 | return Context->getVariableArrayType(ElementType, ReadTypeExpr(), |
| 1381 | ASM, IndexTypeQuals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | case pch::TYPE_VECTOR: { |
| 1385 | if (Record.size() != 2) { |
| 1386 | Error("Incorrect encoding of vector type in PCH file"); |
| 1387 | return QualType(); |
| 1388 | } |
| 1389 | |
| 1390 | QualType ElementType = GetType(Record[0]); |
| 1391 | unsigned NumElements = Record[1]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1392 | return Context->getVectorType(ElementType, NumElements); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | case pch::TYPE_EXT_VECTOR: { |
| 1396 | if (Record.size() != 2) { |
| 1397 | Error("Incorrect encoding of extended vector type in PCH file"); |
| 1398 | return QualType(); |
| 1399 | } |
| 1400 | |
| 1401 | QualType ElementType = GetType(Record[0]); |
| 1402 | unsigned NumElements = Record[1]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1403 | return Context->getExtVectorType(ElementType, NumElements); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | case pch::TYPE_FUNCTION_NO_PROTO: { |
| 1407 | if (Record.size() != 1) { |
| 1408 | Error("Incorrect encoding of no-proto function type"); |
| 1409 | return QualType(); |
| 1410 | } |
| 1411 | QualType ResultType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1412 | return Context->getFunctionNoProtoType(ResultType); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | case pch::TYPE_FUNCTION_PROTO: { |
| 1416 | QualType ResultType = GetType(Record[0]); |
| 1417 | unsigned Idx = 1; |
| 1418 | unsigned NumParams = Record[Idx++]; |
| 1419 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 1420 | for (unsigned I = 0; I != NumParams; ++I) |
| 1421 | ParamTypes.push_back(GetType(Record[Idx++])); |
| 1422 | bool isVariadic = Record[Idx++]; |
| 1423 | unsigned Quals = Record[Idx++]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1424 | return Context->getFunctionType(ResultType, &ParamTypes[0], NumParams, |
| 1425 | isVariadic, Quals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | case pch::TYPE_TYPEDEF: |
| 1429 | assert(Record.size() == 1 && "Incorrect encoding of typedef type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1430 | return Context->getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1431 | |
| 1432 | case pch::TYPE_TYPEOF_EXPR: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1433 | return Context->getTypeOfExprType(ReadTypeExpr()); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1434 | |
| 1435 | case pch::TYPE_TYPEOF: { |
| 1436 | if (Record.size() != 1) { |
| 1437 | Error("Incorrect encoding of typeof(type) in PCH file"); |
| 1438 | return QualType(); |
| 1439 | } |
| 1440 | QualType UnderlyingType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1441 | return Context->getTypeOfType(UnderlyingType); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | case pch::TYPE_RECORD: |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1445 | assert(Record.size() == 1 && "Incorrect encoding of record type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1446 | return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1448 | case pch::TYPE_ENUM: |
| 1449 | assert(Record.size() == 1 && "Incorrect encoding of enum type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1450 | return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0]))); |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1451 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1452 | case pch::TYPE_OBJC_INTERFACE: |
Chris Lattner | 4dcf151a | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 1453 | assert(Record.size() == 1 && "Incorrect encoding of objc interface type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1454 | return Context->getObjCInterfaceType( |
Chris Lattner | 4dcf151a | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 1455 | cast<ObjCInterfaceDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 1457 | case pch::TYPE_OBJC_QUALIFIED_INTERFACE: { |
| 1458 | unsigned Idx = 0; |
| 1459 | ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); |
| 1460 | unsigned NumProtos = Record[Idx++]; |
| 1461 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1462 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1463 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1464 | return Context->getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos); |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 1465 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 1467 | case pch::TYPE_OBJC_QUALIFIED_ID: { |
| 1468 | unsigned Idx = 0; |
| 1469 | unsigned NumProtos = Record[Idx++]; |
| 1470 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1471 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1472 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1473 | return Context->getObjCQualifiedIdType(&Protos[0], NumProtos); |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 1474 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1475 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1476 | // Suppress a GCC warning |
| 1477 | return QualType(); |
| 1478 | } |
| 1479 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1481 | QualType PCHReader::GetType(pch::TypeID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1482 | unsigned Quals = ID & 0x07; |
| 1483 | unsigned Index = ID >> 3; |
| 1484 | |
| 1485 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 1486 | QualType T; |
| 1487 | switch ((pch::PredefinedTypeIDs)Index) { |
| 1488 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1489 | case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; |
| 1490 | case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1491 | |
| 1492 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 1493 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 1494 | // FIXME: Check that the signedness of CharTy is correct! |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1495 | T = Context->CharTy; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1496 | break; |
| 1497 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1498 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; |
| 1499 | case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; |
| 1500 | case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; |
| 1501 | case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; |
| 1502 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; |
| 1503 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; |
| 1504 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; |
| 1505 | case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; |
| 1506 | case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break; |
| 1507 | case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; |
| 1508 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; |
| 1509 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; |
| 1510 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; |
| 1511 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; |
| 1512 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; |
| 1513 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | assert(!T.isNull() && "Unknown predefined type"); |
| 1517 | return T.getQualifiedType(Quals); |
| 1518 | } |
| 1519 | |
| 1520 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 1521 | assert(Index < TypesLoaded.size() && "Type index out-of-range"); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1522 | if (!TypesLoaded[Index]) |
| 1523 | TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1525 | return QualType(TypesLoaded[Index], Quals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1528 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1529 | if (ID == 0) |
| 1530 | return 0; |
| 1531 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1532 | if (ID > DeclsLoaded.size()) { |
| 1533 | Error("Declaration ID out-of-range for PCH file"); |
| 1534 | return 0; |
| 1535 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1536 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1537 | unsigned Index = ID - 1; |
| 1538 | if (!DeclsLoaded[Index]) |
| 1539 | ReadDeclRecord(DeclOffsets[Index], Index); |
| 1540 | |
| 1541 | return DeclsLoaded[Index]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Chris Lattner | 887e2b3 | 2009-04-27 05:46:25 +0000 | [diff] [blame] | 1544 | /// \brief Resolve the offset of a statement into a statement. |
| 1545 | /// |
| 1546 | /// This operation will read a new statement from the external |
| 1547 | /// source each time it is called, and is meant to be used via a |
| 1548 | /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). |
| 1549 | Stmt *PCHReader::GetDeclStmt(uint64_t Offset) { |
Chris Lattner | da93061 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 1550 | // Since we know tha this statement is part of a decl, make sure to use the |
| 1551 | // decl cursor to read it. |
| 1552 | DeclsCursor.JumpToBit(Offset); |
| 1553 | return ReadStmt(DeclsCursor); |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1556 | bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1557 | llvm::SmallVectorImpl<pch::DeclID> &Decls) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1558 | assert(DC->hasExternalLexicalStorage() && |
| 1559 | "DeclContext has no lexical decls in storage"); |
| 1560 | uint64_t Offset = DeclContextOffsets[DC].first; |
| 1561 | assert(Offset && "DeclContext has no lexical decls in storage"); |
| 1562 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1563 | // Keep track of where we are in the stream, then jump back there |
| 1564 | // after reading this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1565 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1566 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1567 | // Load the record containing all of the declarations lexically in |
| 1568 | // this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1569 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1570 | RecordData Record; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1571 | unsigned Code = DeclsCursor.ReadCode(); |
| 1572 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 1573 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1574 | assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block"); |
| 1575 | |
| 1576 | // Load all of the declaration IDs |
| 1577 | Decls.clear(); |
| 1578 | Decls.insert(Decls.end(), Record.begin(), Record.end()); |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1579 | ++NumLexicalDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1580 | return false; |
| 1581 | } |
| 1582 | |
| 1583 | bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1584 | llvm::SmallVectorImpl<VisibleDeclaration> &Decls) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1585 | assert(DC->hasExternalVisibleStorage() && |
| 1586 | "DeclContext has no visible decls in storage"); |
| 1587 | uint64_t Offset = DeclContextOffsets[DC].second; |
| 1588 | assert(Offset && "DeclContext has no visible decls in storage"); |
| 1589 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1590 | // Keep track of where we are in the stream, then jump back there |
| 1591 | // after reading this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1592 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1593 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1594 | // Load the record containing all of the declarations visible in |
| 1595 | // this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1596 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1597 | RecordData Record; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1598 | unsigned Code = DeclsCursor.ReadCode(); |
| 1599 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 1600 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1601 | assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block"); |
| 1602 | if (Record.size() == 0) |
| 1603 | return false; |
| 1604 | |
| 1605 | Decls.clear(); |
| 1606 | |
| 1607 | unsigned Idx = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1608 | while (Idx < Record.size()) { |
| 1609 | Decls.push_back(VisibleDeclaration()); |
| 1610 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 1611 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1612 | unsigned Size = Record[Idx++]; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 1613 | llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1614 | LoadedDecls.reserve(Size); |
| 1615 | for (unsigned I = 0; I < Size; ++I) |
| 1616 | LoadedDecls.push_back(Record[Idx++]); |
| 1617 | } |
| 1618 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1619 | ++NumVisibleDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1620 | return false; |
| 1621 | } |
| 1622 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1623 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
Douglas Gregor | 0af2ca4 | 2009-04-22 19:09:20 +0000 | [diff] [blame] | 1624 | this->Consumer = Consumer; |
| 1625 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1626 | if (!Consumer) |
| 1627 | return; |
| 1628 | |
| 1629 | for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { |
| 1630 | Decl *D = GetDecl(ExternalDefinitions[I]); |
| 1631 | DeclGroupRef DG(D); |
| 1632 | Consumer->HandleTopLevelDecl(DG); |
| 1633 | } |
Douglas Gregor | c62a2fe | 2009-04-25 00:41:30 +0000 | [diff] [blame] | 1634 | |
| 1635 | for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) { |
| 1636 | DeclGroupRef DG(InterestingDecls[I]); |
| 1637 | Consumer->HandleTopLevelDecl(DG); |
| 1638 | } |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1641 | void PCHReader::PrintStats() { |
| 1642 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 1643 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1644 | unsigned NumTypesLoaded |
| 1645 | = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), |
| 1646 | (Type *)0); |
| 1647 | unsigned NumDeclsLoaded |
| 1648 | = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), |
| 1649 | (Decl *)0); |
| 1650 | unsigned NumIdentifiersLoaded |
| 1651 | = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), |
| 1652 | IdentifiersLoaded.end(), |
| 1653 | (IdentifierInfo *)0); |
| 1654 | unsigned NumSelectorsLoaded |
| 1655 | = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), |
| 1656 | SelectorsLoaded.end(), |
| 1657 | Selector()); |
Douglas Gregor | 2d41cc1 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1659 | std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); |
| 1660 | std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1661 | if (TotalNumSLocEntries) |
| 1662 | std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", |
| 1663 | NumSLocEntriesRead, TotalNumSLocEntries, |
| 1664 | ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1665 | if (!TypesLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1666 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1667 | NumTypesLoaded, (unsigned)TypesLoaded.size(), |
| 1668 | ((float)NumTypesLoaded/TypesLoaded.size() * 100)); |
| 1669 | if (!DeclsLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1670 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1671 | NumDeclsLoaded, (unsigned)DeclsLoaded.size(), |
| 1672 | ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1673 | if (!IdentifiersLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1674 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1675 | NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), |
| 1676 | ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1677 | if (TotalNumSelectors) |
| 1678 | std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", |
| 1679 | NumSelectorsLoaded, TotalNumSelectors, |
| 1680 | ((float)NumSelectorsLoaded/TotalNumSelectors * 100)); |
| 1681 | if (TotalNumStatements) |
| 1682 | std::fprintf(stderr, " %u/%u statements read (%f%%)\n", |
| 1683 | NumStatementsRead, TotalNumStatements, |
| 1684 | ((float)NumStatementsRead/TotalNumStatements * 100)); |
| 1685 | if (TotalNumMacros) |
| 1686 | std::fprintf(stderr, " %u/%u macros read (%f%%)\n", |
| 1687 | NumMacrosRead, TotalNumMacros, |
| 1688 | ((float)NumMacrosRead/TotalNumMacros * 100)); |
| 1689 | if (TotalLexicalDeclContexts) |
| 1690 | std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", |
| 1691 | NumLexicalDeclContextsRead, TotalLexicalDeclContexts, |
| 1692 | ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts |
| 1693 | * 100)); |
| 1694 | if (TotalVisibleDeclContexts) |
| 1695 | std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", |
| 1696 | NumVisibleDeclContextsRead, TotalVisibleDeclContexts, |
| 1697 | ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts |
| 1698 | * 100)); |
| 1699 | if (TotalSelectorsInMethodPool) { |
| 1700 | std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", |
| 1701 | NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool, |
| 1702 | ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool |
| 1703 | * 100)); |
| 1704 | std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); |
| 1705 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1706 | std::fprintf(stderr, "\n"); |
| 1707 | } |
| 1708 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1709 | void PCHReader::InitializeSema(Sema &S) { |
| 1710 | SemaObj = &S; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1711 | S.ExternalSource = this; |
| 1712 | |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 1713 | // Makes sure any declarations that were deserialized "too early" |
| 1714 | // still get added to the identifier's declaration chains. |
| 1715 | for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { |
| 1716 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I])); |
| 1717 | SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1718 | } |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 1719 | PreloadedDecls.clear(); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1720 | |
| 1721 | // If there were any tentative definitions, deserialize them and add |
| 1722 | // them to Sema's table of tentative definitions. |
| 1723 | for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { |
| 1724 | VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); |
| 1725 | SemaObj->TentativeDefinitions[Var->getDeclName()] = Var; |
| 1726 | } |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1727 | |
| 1728 | // If there were any locally-scoped external declarations, |
| 1729 | // deserialize them and add them to Sema's table of locally-scoped |
| 1730 | // external declarations. |
| 1731 | for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { |
| 1732 | NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); |
| 1733 | SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; |
| 1734 | } |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1735 | |
| 1736 | // If there were any ext_vector type declarations, deserialize them |
| 1737 | // and add them to Sema's vector of such declarations. |
| 1738 | for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) |
| 1739 | SemaObj->ExtVectorDecls.push_back( |
| 1740 | cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); |
| 1741 | |
| 1742 | // If there were any Objective-C category implementations, |
| 1743 | // deserialize them and add them to Sema's vector of such |
| 1744 | // definitions. |
| 1745 | for (unsigned I = 0, N = ObjCCategoryImpls.size(); I != N; ++I) |
| 1746 | SemaObj->ObjCCategoryImpls.push_back( |
| 1747 | cast<ObjCCategoryImplDecl>(GetDecl(ObjCCategoryImpls[I]))); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) { |
| 1751 | // Try to find this name within our on-disk hash table |
| 1752 | PCHIdentifierLookupTable *IdTable |
| 1753 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
| 1754 | std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); |
| 1755 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key); |
| 1756 | if (Pos == IdTable->end()) |
| 1757 | return 0; |
| 1758 | |
| 1759 | // Dereferencing the iterator has the effect of building the |
| 1760 | // IdentifierInfo node and populating it with the various |
| 1761 | // declarations it needs. |
| 1762 | return *Pos; |
| 1763 | } |
| 1764 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1765 | std::pair<ObjCMethodList, ObjCMethodList> |
| 1766 | PCHReader::ReadMethodPool(Selector Sel) { |
| 1767 | if (!MethodPoolLookupTable) |
| 1768 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
| 1769 | |
| 1770 | // Try to find this selector within our on-disk hash table. |
| 1771 | PCHMethodPoolLookupTable *PoolTable |
| 1772 | = (PCHMethodPoolLookupTable*)MethodPoolLookupTable; |
| 1773 | PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1774 | if (Pos == PoolTable->end()) { |
| 1775 | ++NumMethodPoolMisses; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1776 | return std::pair<ObjCMethodList, ObjCMethodList>();; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1777 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1779 | ++NumMethodPoolSelectorsRead; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1780 | return *Pos; |
| 1781 | } |
| 1782 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1783 | void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1784 | assert(ID && "Non-zero identifier ID required"); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1785 | assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range"); |
| 1786 | IdentifiersLoaded[ID - 1] = II; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1789 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1790 | if (ID == 0) |
| 1791 | return 0; |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1792 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1793 | if (!IdentifierTableData || IdentifiersLoaded.empty()) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1794 | Error("No identifier table in PCH file"); |
| 1795 | return 0; |
| 1796 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1797 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1798 | if (!IdentifiersLoaded[ID - 1]) { |
| 1799 | uint32_t Offset = IdentifierOffsets[ID - 1]; |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1800 | const char *Str = IdentifierTableData + Offset; |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1801 | |
| 1802 | // If there is an identifier lookup table, but the offset of this |
| 1803 | // string is after the identifier table itself, then we know that |
| 1804 | // this string is not in the on-disk hash table. Therefore, |
| 1805 | // disable lookup into the hash table when looking for this |
| 1806 | // identifier. |
| 1807 | PCHIdentifierLookupTable *IdTable |
| 1808 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1809 | if (!IdTable || |
| 1810 | Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) { |
| 1811 | // Turn off lookup into the on-disk hash table. We know that |
| 1812 | // this identifier is not there. |
| 1813 | if (IdTable) |
| 1814 | PP.getIdentifierTable().setExternalIdentifierLookup(0); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1815 | |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1816 | // All of the strings in the PCH file are preceded by a 16-bit |
| 1817 | // length. Extract that 16-bit length to avoid having to execute |
| 1818 | // strlen(). |
| 1819 | const char *StrLenPtr = Str - 2; |
| 1820 | unsigned StrLen = (((unsigned) StrLenPtr[0]) |
| 1821 | | (((unsigned) StrLenPtr[1]) << 8)) - 1; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1822 | IdentifiersLoaded[ID - 1]=&PP.getIdentifierTable().get(Str, Str + StrLen); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1823 | |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1824 | // Turn on lookup into the on-disk hash table, if we have an |
| 1825 | // on-disk hash table. |
| 1826 | if (IdTable) |
| 1827 | PP.getIdentifierTable().setExternalIdentifierLookup(this); |
| 1828 | } else { |
| 1829 | // The identifier is a key in our on-disk hash table. Since we |
| 1830 | // know where the hash table entry starts, just read in this |
| 1831 | // (key, value) pair. |
| 1832 | PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this)); |
| 1833 | const unsigned char *Pos = (const unsigned char *)Str - 4; |
| 1834 | std::pair<unsigned, unsigned> KeyDataLengths |
| 1835 | = Trait.ReadKeyDataLength(Pos); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 1837 | PCHIdentifierLookupTrait::internal_key_type InternalKey |
| 1838 | = Trait.ReadKey(Pos, KeyDataLengths.first); |
| 1839 | Pos = (const unsigned char *)Str + KeyDataLengths.first; |
| 1840 | IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos, |
| 1841 | KeyDataLengths.second); |
| 1842 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1843 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1844 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1845 | return IdentifiersLoaded[ID - 1]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1848 | void PCHReader::ReadSLocEntry(unsigned ID) { |
| 1849 | ReadSLocEntryRecord(ID); |
| 1850 | } |
| 1851 | |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1852 | Selector PCHReader::DecodeSelector(unsigned ID) { |
| 1853 | if (ID == 0) |
| 1854 | return Selector(); |
| 1855 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1856 | if (!MethodPoolLookupTableData) { |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1857 | Error("No selector table in PCH file"); |
| 1858 | return Selector(); |
| 1859 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1860 | |
| 1861 | if (ID > TotalNumSelectors) { |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1862 | Error("Selector ID out of range"); |
| 1863 | return Selector(); |
| 1864 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1865 | |
| 1866 | unsigned Index = ID - 1; |
| 1867 | if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) { |
| 1868 | // Load this selector from the selector table. |
| 1869 | // FIXME: endianness portability issues with SelectorOffsets table |
| 1870 | PCHMethodPoolLookupTrait Trait(*this); |
| 1871 | SelectorsLoaded[Index] |
| 1872 | = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0); |
| 1873 | } |
| 1874 | |
| 1875 | return SelectorsLoaded[Index]; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1878 | DeclarationName |
| 1879 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 1880 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 1881 | switch (Kind) { |
| 1882 | case DeclarationName::Identifier: |
| 1883 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 1884 | |
| 1885 | case DeclarationName::ObjCZeroArgSelector: |
| 1886 | case DeclarationName::ObjCOneArgSelector: |
| 1887 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | a7503a7 | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 1888 | return DeclarationName(GetSelector(Record, Idx)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1889 | |
| 1890 | case DeclarationName::CXXConstructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1891 | return Context->DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1892 | GetType(Record[Idx++])); |
| 1893 | |
| 1894 | case DeclarationName::CXXDestructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1895 | return Context->DeclarationNames.getCXXDestructorName( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1896 | GetType(Record[Idx++])); |
| 1897 | |
| 1898 | case DeclarationName::CXXConversionFunctionName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1899 | return Context->DeclarationNames.getCXXConversionFunctionName( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1900 | GetType(Record[Idx++])); |
| 1901 | |
| 1902 | case DeclarationName::CXXOperatorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1903 | return Context->DeclarationNames.getCXXOperatorName( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1904 | (OverloadedOperatorKind)Record[Idx++]); |
| 1905 | |
| 1906 | case DeclarationName::CXXUsingDirective: |
| 1907 | return DeclarationName::getUsingDirectiveName(); |
| 1908 | } |
| 1909 | |
| 1910 | // Required to silence GCC warning |
| 1911 | return DeclarationName(); |
| 1912 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1913 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1914 | /// \brief Read an integral value |
| 1915 | llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { |
| 1916 | unsigned BitWidth = Record[Idx++]; |
| 1917 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); |
| 1918 | llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); |
| 1919 | Idx += NumWords; |
| 1920 | return Result; |
| 1921 | } |
| 1922 | |
| 1923 | /// \brief Read a signed integral value |
| 1924 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 1925 | bool isUnsigned = Record[Idx++]; |
| 1926 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 1927 | } |
| 1928 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1929 | /// \brief Read a floating-point value |
| 1930 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1931 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 1932 | } |
| 1933 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1934 | // \brief Read a string |
| 1935 | std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) { |
| 1936 | unsigned Len = Record[Idx++]; |
| 1937 | std::string Result(&Record[Idx], &Record[Idx] + Len); |
| 1938 | Idx += Len; |
| 1939 | return Result; |
| 1940 | } |
| 1941 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1942 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1943 | return Diag(SourceLocation(), DiagID); |
| 1944 | } |
| 1945 | |
| 1946 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
| 1947 | return PP.getDiagnostics().Report(FullSourceLoc(Loc, |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1948 | PP.getSourceManager()), |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1949 | DiagID); |
| 1950 | } |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1952 | /// \brief Retrieve the identifier table associated with the |
| 1953 | /// preprocessor. |
| 1954 | IdentifierTable &PCHReader::getIdentifierTable() { |
| 1955 | return PP.getIdentifierTable(); |
| 1956 | } |
| 1957 | |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 1958 | /// \brief Record that the given ID maps to the given switch-case |
| 1959 | /// statement. |
| 1960 | void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { |
| 1961 | assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); |
| 1962 | SwitchCaseStmts[ID] = SC; |
| 1963 | } |
| 1964 | |
| 1965 | /// \brief Retrieve the switch-case statement with the given ID. |
| 1966 | SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) { |
| 1967 | assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); |
| 1968 | return SwitchCaseStmts[ID]; |
| 1969 | } |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 1970 | |
| 1971 | /// \brief Record that the given label statement has been |
| 1972 | /// deserialized and has the given ID. |
| 1973 | void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { |
| 1974 | assert(LabelStmts.find(ID) == LabelStmts.end() && |
| 1975 | "Deserialized label twice"); |
| 1976 | LabelStmts[ID] = S; |
| 1977 | |
| 1978 | // If we've already seen any goto statements that point to this |
| 1979 | // label, resolve them now. |
| 1980 | typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; |
| 1981 | std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); |
| 1982 | for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) |
| 1983 | Goto->second->setLabel(S); |
| 1984 | UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 1985 | |
| 1986 | // If we've already seen any address-label statements that point to |
| 1987 | // this label, resolve them now. |
| 1988 | typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; |
| 1989 | std::pair<AddrLabelIter, AddrLabelIter> AddrLabels |
| 1990 | = UnresolvedAddrLabelExprs.equal_range(ID); |
| 1991 | for (AddrLabelIter AddrLabel = AddrLabels.first; |
| 1992 | AddrLabel != AddrLabels.second; ++AddrLabel) |
| 1993 | AddrLabel->second->setLabel(S); |
| 1994 | UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | /// \brief Set the label of the given statement to the label |
| 1998 | /// identified by ID. |
| 1999 | /// |
| 2000 | /// Depending on the order in which the label and other statements |
| 2001 | /// referencing that label occur, this operation may complete |
| 2002 | /// immediately (updating the statement) or it may queue the |
| 2003 | /// statement to be back-patched later. |
| 2004 | void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) { |
| 2005 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 2006 | if (Label != LabelStmts.end()) { |
| 2007 | // We've already seen this label, so set the label of the goto and |
| 2008 | // we're done. |
| 2009 | S->setLabel(Label->second); |
| 2010 | } else { |
| 2011 | // We haven't seen this label yet, so add this goto to the set of |
| 2012 | // unresolved goto statements. |
| 2013 | UnresolvedGotoStmts.insert(std::make_pair(ID, S)); |
| 2014 | } |
| 2015 | } |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2016 | |
| 2017 | /// \brief Set the label of the given expression to the label |
| 2018 | /// identified by ID. |
| 2019 | /// |
| 2020 | /// Depending on the order in which the label and other statements |
| 2021 | /// referencing that label occur, this operation may complete |
| 2022 | /// immediately (updating the statement) or it may queue the |
| 2023 | /// statement to be back-patched later. |
| 2024 | void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { |
| 2025 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 2026 | if (Label != LabelStmts.end()) { |
| 2027 | // We've already seen this label, so set the label of the |
| 2028 | // label-address expression and we're done. |
| 2029 | S->setLabel(Label->second); |
| 2030 | } else { |
| 2031 | // We haven't seen this label yet, so add this label-address |
| 2032 | // expression to the set of unresolved label-address expressions. |
| 2033 | UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); |
| 2034 | } |
| 2035 | } |