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