blob: 9a0061ea0eed264a8aa42bb4a170e8c8b7ed0793 [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000018#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000021#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000022#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000023#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-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 Gregore1d918e2009-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 Gregorbd945002009-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
Douglas Gregorff0a9872009-04-13 17:12:42 +0000203 std::map<int, int> FileIDs;
204 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000205 // Extract the file name
206 unsigned FilenameLen = Record[Idx++];
207 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
208 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000209 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
210 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000211 }
212
213 // Parse the line entries
214 std::vector<LineEntry> Entries;
215 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000216 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000217
218 // Extract the line entries
219 unsigned NumEntries = Record[Idx++];
220 Entries.clear();
221 Entries.reserve(NumEntries);
222 for (unsigned I = 0; I != NumEntries; ++I) {
223 unsigned FileOffset = Record[Idx++];
224 unsigned LineNo = Record[Idx++];
225 int FilenameID = Record[Idx++];
226 SrcMgr::CharacteristicKind FileKind
227 = (SrcMgr::CharacteristicKind)Record[Idx++];
228 unsigned IncludeOffset = Record[Idx++];
229 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
230 FileKind, IncludeOffset));
231 }
232 LineTable.AddEntry(FID, Entries);
233 }
234
235 return false;
236}
237
Douglas Gregor14f79002009-04-10 03:52:48 +0000238/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000239PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000240 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000241 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
242 Error("Malformed source manager block record");
243 return Failure;
244 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000245
246 SourceManager &SourceMgr = Context.getSourceManager();
247 RecordData Record;
248 while (true) {
249 unsigned Code = Stream.ReadCode();
250 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000251 if (Stream.ReadBlockEnd()) {
252 Error("Error at end of Source Manager block");
253 return Failure;
254 }
255
256 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000257 }
258
259 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
260 // No known subblocks, always skip them.
261 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000262 if (Stream.SkipBlock()) {
263 Error("Malformed block record");
264 return Failure;
265 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000266 continue;
267 }
268
269 if (Code == llvm::bitc::DEFINE_ABBREV) {
270 Stream.ReadAbbrevRecord();
271 continue;
272 }
273
274 // Read a record.
275 const char *BlobStart;
276 unsigned BlobLen;
277 Record.clear();
278 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
279 default: // Default behavior: ignore.
280 break;
281
282 case pch::SM_SLOC_FILE_ENTRY: {
283 // FIXME: We would really like to delay the creation of this
284 // FileEntry until it is actually required, e.g., when producing
285 // a diagnostic with a source location in this file.
286 const FileEntry *File
287 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
288 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +0000289 FileID ID = SourceMgr.createFileID(File,
290 SourceLocation::getFromRawEncoding(Record[1]),
291 (CharacteristicKind)Record[2]);
292 if (Record[3])
293 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
294 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +0000295 break;
296 }
297
298 case pch::SM_SLOC_BUFFER_ENTRY: {
299 const char *Name = BlobStart;
300 unsigned Code = Stream.ReadCode();
301 Record.clear();
302 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
303 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000304 llvm::MemoryBuffer *Buffer
305 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
306 BlobStart + BlobLen - 1,
307 Name);
308 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
309
310 if (strcmp(Name, "<built-in>") == 0
311 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
312 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +0000313 break;
314 }
315
316 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
317 SourceLocation SpellingLoc
318 = SourceLocation::getFromRawEncoding(Record[1]);
319 SourceMgr.createInstantiationLoc(
320 SpellingLoc,
321 SourceLocation::getFromRawEncoding(Record[2]),
322 SourceLocation::getFromRawEncoding(Record[3]),
323 Lexer::MeasureTokenLength(SpellingLoc,
324 SourceMgr));
325 break;
326 }
327
Douglas Gregorbd945002009-04-13 16:31:14 +0000328 case pch::SM_LINE_TABLE: {
329 if (ParseLineTable(SourceMgr, Record))
330 return Failure;
331 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000332 }
333 }
334}
335
Chris Lattner42d42b52009-04-10 21:41:48 +0000336bool PCHReader::ReadPreprocessorBlock() {
337 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
338 return Error("Malformed preprocessor block record");
339
Chris Lattner42d42b52009-04-10 21:41:48 +0000340 RecordData Record;
341 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
342 MacroInfo *LastMacro = 0;
343
344 while (true) {
345 unsigned Code = Stream.ReadCode();
346 switch (Code) {
347 case llvm::bitc::END_BLOCK:
348 if (Stream.ReadBlockEnd())
349 return Error("Error at end of preprocessor block");
350 return false;
351
352 case llvm::bitc::ENTER_SUBBLOCK:
353 // No known subblocks, always skip them.
354 Stream.ReadSubBlockID();
355 if (Stream.SkipBlock())
356 return Error("Malformed block record");
357 continue;
358
359 case llvm::bitc::DEFINE_ABBREV:
360 Stream.ReadAbbrevRecord();
361 continue;
362 default: break;
363 }
364
365 // Read a record.
366 Record.clear();
367 pch::PreprocessorRecordTypes RecType =
368 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
369 switch (RecType) {
370 default: // Default behavior: ignore unknown records.
371 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000372 case pch::PP_COUNTER_VALUE:
373 if (!Record.empty())
374 PP.setCounterValue(Record[0]);
375 break;
376
Chris Lattner42d42b52009-04-10 21:41:48 +0000377 case pch::PP_MACRO_OBJECT_LIKE:
378 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +0000379 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
380 if (II == 0)
381 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +0000382 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
383 bool isUsed = Record[2];
384
385 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
386 MI->setIsUsed(isUsed);
387
388 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
389 // Decode function-like macro info.
390 bool isC99VarArgs = Record[3];
391 bool isGNUVarArgs = Record[4];
392 MacroArgs.clear();
393 unsigned NumArgs = Record[5];
394 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +0000395 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +0000396
397 // Install function-like macro info.
398 MI->setIsFunctionLike();
399 if (isC99VarArgs) MI->setIsC99Varargs();
400 if (isGNUVarArgs) MI->setIsGNUVarargs();
401 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
402 PP.getPreprocessorAllocator());
403 }
404
405 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +0000406 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +0000407
408 // Remember that we saw this macro last so that we add the tokens that
409 // form its body to it.
410 LastMacro = MI;
411 break;
412 }
413
414 case pch::PP_TOKEN: {
415 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
416 // pretend we didn't see this.
417 if (LastMacro == 0) break;
418
419 Token Tok;
420 Tok.startToken();
421 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
422 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +0000423 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
424 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +0000425 Tok.setKind((tok::TokenKind)Record[3]);
426 Tok.setFlag((Token::TokenFlags)Record[4]);
427 LastMacro->AddTokenToBody(Tok);
428 break;
429 }
430 }
431 }
432}
433
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000434PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
435 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
436 Error("Malformed block record");
437 return Failure;
438 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000439
Chris Lattner7356a312009-04-11 21:15:38 +0000440 uint64_t PreprocessorBlockBit = 0;
441
Douglas Gregor2cf26342009-04-09 22:27:44 +0000442 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +0000443 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000444 while (!Stream.AtEndOfStream()) {
445 unsigned Code = Stream.ReadCode();
446 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +0000447 // If we saw the preprocessor block, read it now.
448 if (PreprocessorBlockBit) {
449 uint64_t SavedPos = Stream.GetCurrentBitNo();
450 Stream.JumpToBit(PreprocessorBlockBit);
451 if (ReadPreprocessorBlock()) {
452 Error("Malformed preprocessor block");
453 return Failure;
454 }
455 Stream.JumpToBit(SavedPos);
456 }
457
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000458 if (Stream.ReadBlockEnd()) {
459 Error("Error at end of module block");
460 return Failure;
461 }
Chris Lattner7356a312009-04-11 21:15:38 +0000462
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000463 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000464 }
465
466 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
467 switch (Stream.ReadSubBlockID()) {
468 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
469 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
470 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000471 if (Stream.SkipBlock()) {
472 Error("Malformed block record");
473 return Failure;
474 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000475 break;
476
Chris Lattner7356a312009-04-11 21:15:38 +0000477 case pch::PREPROCESSOR_BLOCK_ID:
478 // Skip the preprocessor block for now, but remember where it is. We
479 // want to read it in after the identifier table.
480 if (PreprocessorBlockBit) {
481 Error("Multiple preprocessor blocks found.");
482 return Failure;
483 }
484 PreprocessorBlockBit = Stream.GetCurrentBitNo();
485 if (Stream.SkipBlock()) {
486 Error("Malformed block record");
487 return Failure;
488 }
489 break;
490
Douglas Gregor14f79002009-04-10 03:52:48 +0000491 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000492 switch (ReadSourceManagerBlock()) {
493 case Success:
494 break;
495
496 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000497 Error("Malformed source manager block");
498 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000499
500 case IgnorePCH:
501 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000502 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000503 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000504 }
Douglas Gregor8038d512009-04-10 17:25:41 +0000505 continue;
506 }
507
508 if (Code == llvm::bitc::DEFINE_ABBREV) {
509 Stream.ReadAbbrevRecord();
510 continue;
511 }
512
513 // Read and process a record.
514 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +0000515 const char *BlobStart = 0;
516 unsigned BlobLen = 0;
517 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
518 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +0000519 default: // Default behavior: ignore.
520 break;
521
522 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000523 if (!TypeOffsets.empty()) {
524 Error("Duplicate TYPE_OFFSET record in PCH file");
525 return Failure;
526 }
Douglas Gregor8038d512009-04-10 17:25:41 +0000527 TypeOffsets.swap(Record);
528 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
529 break;
530
531 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000532 if (!DeclOffsets.empty()) {
533 Error("Duplicate DECL_OFFSET record in PCH file");
534 return Failure;
535 }
Douglas Gregor8038d512009-04-10 17:25:41 +0000536 DeclOffsets.swap(Record);
537 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
538 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000539
540 case pch::LANGUAGE_OPTIONS:
541 if (ParseLanguageOptions(Record))
542 return IgnorePCH;
543 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +0000544
Douglas Gregorafaf3082009-04-11 00:14:32 +0000545 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000546 std::string TargetTriple(BlobStart, BlobLen);
547 if (TargetTriple != Context.Target.getTargetTriple()) {
548 Diag(diag::warn_pch_target_triple)
549 << TargetTriple << Context.Target.getTargetTriple();
550 Diag(diag::note_ignoring_pch) << FileName;
551 return IgnorePCH;
552 }
553 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000554 }
Douglas Gregorafaf3082009-04-11 00:14:32 +0000555
556 case pch::IDENTIFIER_TABLE:
557 IdentifierTable = BlobStart;
558 break;
559
560 case pch::IDENTIFIER_OFFSET:
561 if (!IdentifierData.empty()) {
562 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
563 return Failure;
564 }
565 IdentifierData.swap(Record);
566#ifndef NDEBUG
567 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
568 if ((IdentifierData[I] & 0x01) == 0) {
569 Error("Malformed identifier table in the precompiled header");
570 return Failure;
571 }
572 }
573#endif
574 break;
575 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000576 }
577
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000578 Error("Premature end of bitstream");
579 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000580}
581
Douglas Gregore1d918e2009-04-10 23:10:45 +0000582PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000583 // Set the PCH file name.
584 this->FileName = FileName;
585
Douglas Gregor2cf26342009-04-09 22:27:44 +0000586 // Open the PCH file.
587 std::string ErrStr;
588 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +0000589 if (!Buffer) {
590 Error(ErrStr.c_str());
591 return IgnorePCH;
592 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000593
594 // Initialize the stream
595 Stream.init((const unsigned char *)Buffer->getBufferStart(),
596 (const unsigned char *)Buffer->getBufferEnd());
597
598 // Sniff for the signature.
599 if (Stream.Read(8) != 'C' ||
600 Stream.Read(8) != 'P' ||
601 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +0000602 Stream.Read(8) != 'H') {
603 Error("Not a PCH file");
604 return IgnorePCH;
605 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000606
607 // We expect a number of well-defined blocks, though we don't necessarily
608 // need to understand them all.
609 while (!Stream.AtEndOfStream()) {
610 unsigned Code = Stream.ReadCode();
611
Douglas Gregore1d918e2009-04-10 23:10:45 +0000612 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
613 Error("Invalid record at top-level");
614 return Failure;
615 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000616
617 unsigned BlockID = Stream.ReadSubBlockID();
618
619 // We only know the PCH subblock ID.
620 switch (BlockID) {
621 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000622 if (Stream.ReadBlockInfoBlock()) {
623 Error("Malformed BlockInfoBlock");
624 return Failure;
625 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000626 break;
627 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000628 switch (ReadPCHBlock()) {
629 case Success:
630 break;
631
632 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000633 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000634
635 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +0000636 // FIXME: We could consider reading through to the end of this
637 // PCH block, skipping subblocks, to see if there are other
638 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +0000639 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000640 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000641 break;
642 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000643 if (Stream.SkipBlock()) {
644 Error("Malformed block record");
645 return Failure;
646 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000647 break;
648 }
649 }
650
651 // Load the translation unit declaration
652 ReadDeclRecord(DeclOffsets[0], 0);
653
Douglas Gregore1d918e2009-04-10 23:10:45 +0000654 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000655}
656
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000657/// \brief Parse the record that corresponds to a LangOptions data
658/// structure.
659///
660/// This routine compares the language options used to generate the
661/// PCH file against the language options set for the current
662/// compilation. For each option, we classify differences between the
663/// two compiler states as either "benign" or "important". Benign
664/// differences don't matter, and we accept them without complaint
665/// (and without modifying the language options). Differences between
666/// the states for important options cause the PCH file to be
667/// unusable, so we emit a warning and return true to indicate that
668/// there was an error.
669///
670/// \returns true if the PCH file is unacceptable, false otherwise.
671bool PCHReader::ParseLanguageOptions(
672 const llvm::SmallVectorImpl<uint64_t> &Record) {
673 const LangOptions &LangOpts = Context.getLangOptions();
674#define PARSE_LANGOPT_BENIGN(Option) ++Idx
675#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
676 if (Record[Idx] != LangOpts.Option) { \
677 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
678 Diag(diag::note_ignoring_pch) << FileName; \
679 return true; \
680 } \
681 ++Idx
682
683 unsigned Idx = 0;
684 PARSE_LANGOPT_BENIGN(Trigraphs);
685 PARSE_LANGOPT_BENIGN(BCPLComment);
686 PARSE_LANGOPT_BENIGN(DollarIdents);
687 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
688 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
689 PARSE_LANGOPT_BENIGN(ImplicitInt);
690 PARSE_LANGOPT_BENIGN(Digraphs);
691 PARSE_LANGOPT_BENIGN(HexFloats);
692 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
693 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
694 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
695 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
696 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
697 PARSE_LANGOPT_BENIGN(CXXOperatorName);
698 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
699 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
700 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
701 PARSE_LANGOPT_BENIGN(PascalStrings);
702 PARSE_LANGOPT_BENIGN(Boolean);
703 PARSE_LANGOPT_BENIGN(WritableStrings);
704 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
705 diag::warn_pch_lax_vector_conversions);
706 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
707 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
708 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
709 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
710 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
711 diag::warn_pch_thread_safe_statics);
712 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
713 PARSE_LANGOPT_BENIGN(EmitAllDecls);
714 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
715 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
716 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
717 diag::warn_pch_heinous_extensions);
718 // FIXME: Most of the options below are benign if the macro wasn't
719 // used. Unfortunately, this means that a PCH compiled without
720 // optimization can't be used with optimization turned on, even
721 // though the only thing that changes is whether __OPTIMIZE__ was
722 // defined... but if __OPTIMIZE__ never showed up in the header, it
723 // doesn't matter. We could consider making this some special kind
724 // of check.
725 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
726 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
727 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
728 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
729 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
730 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
731 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
732 Diag(diag::warn_pch_gc_mode)
733 << (unsigned)Record[Idx] << LangOpts.getGCMode();
734 Diag(diag::note_ignoring_pch) << FileName;
735 return true;
736 }
737 ++Idx;
738 PARSE_LANGOPT_BENIGN(getVisibilityMode());
739 PARSE_LANGOPT_BENIGN(InstantiationDepth);
740#undef PARSE_LANGOPT_IRRELEVANT
741#undef PARSE_LANGOPT_BENIGN
742
743 return false;
744}
745
Douglas Gregor2cf26342009-04-09 22:27:44 +0000746/// \brief Read and return the type at the given offset.
747///
748/// This routine actually reads the record corresponding to the type
749/// at the given offset in the bitstream. It is a helper routine for
750/// GetType, which deals with reading type IDs.
751QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
752 Stream.JumpToBit(Offset);
753 RecordData Record;
754 unsigned Code = Stream.ReadCode();
755 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
756 case pch::TYPE_FIXED_WIDTH_INT: {
757 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
758 return Context.getFixedWidthIntType(Record[0], Record[1]);
759 }
760
761 case pch::TYPE_COMPLEX: {
762 assert(Record.size() == 1 && "Incorrect encoding of complex type");
763 QualType ElemType = GetType(Record[0]);
764 return Context.getComplexType(ElemType);
765 }
766
767 case pch::TYPE_POINTER: {
768 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
769 QualType PointeeType = GetType(Record[0]);
770 return Context.getPointerType(PointeeType);
771 }
772
773 case pch::TYPE_BLOCK_POINTER: {
774 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
775 QualType PointeeType = GetType(Record[0]);
776 return Context.getBlockPointerType(PointeeType);
777 }
778
779 case pch::TYPE_LVALUE_REFERENCE: {
780 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
781 QualType PointeeType = GetType(Record[0]);
782 return Context.getLValueReferenceType(PointeeType);
783 }
784
785 case pch::TYPE_RVALUE_REFERENCE: {
786 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
787 QualType PointeeType = GetType(Record[0]);
788 return Context.getRValueReferenceType(PointeeType);
789 }
790
791 case pch::TYPE_MEMBER_POINTER: {
792 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
793 QualType PointeeType = GetType(Record[0]);
794 QualType ClassType = GetType(Record[1]);
795 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
796 }
797
798 // FIXME: Several other kinds of types to deserialize here!
799 default:
Douglas Gregor8038d512009-04-10 17:25:41 +0000800 assert(false && "Unable to deserialize this type");
Douglas Gregor2cf26342009-04-09 22:27:44 +0000801 break;
802 }
803
804 // Suppress a GCC warning
805 return QualType();
806}
807
808/// \brief Note that we have loaded the declaration with the given
809/// Index.
810///
811/// This routine notes that this declaration has already been loaded,
812/// so that future GetDecl calls will return this declaration rather
813/// than trying to load a new declaration.
814inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
815 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
816 DeclAlreadyLoaded[Index] = true;
817 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
818}
819
820/// \brief Read the declaration at the given offset from the PCH file.
821Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
822 Decl *D = 0;
823 Stream.JumpToBit(Offset);
824 RecordData Record;
825 unsigned Code = Stream.ReadCode();
826 unsigned Idx = 0;
827 PCHDeclReader Reader(*this, Record, Idx);
828 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
829 case pch::DECL_TRANSLATION_UNIT:
830 assert(Index == 0 && "Translation unit must be at index 0");
831 Reader.VisitTranslationUnitDecl(Context.getTranslationUnitDecl());
832 D = Context.getTranslationUnitDecl();
833 LoadedDecl(Index, D);
834 break;
835
836 case pch::DECL_TYPEDEF: {
837 TypedefDecl *Typedef = TypedefDecl::Create(Context, 0, SourceLocation(),
838 0, QualType());
839 LoadedDecl(Index, Typedef);
840 Reader.VisitTypedefDecl(Typedef);
841 D = Typedef;
842 break;
843 }
844
845 case pch::DECL_VAR: {
846 VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
847 VarDecl::None, SourceLocation());
848 LoadedDecl(Index, Var);
849 Reader.VisitVarDecl(Var);
850 D = Var;
851 break;
852 }
853
854 default:
855 assert(false && "Cannot de-serialize this kind of declaration");
856 break;
857 }
858
859 // If this declaration is also a declaration context, get the
860 // offsets for its tables of lexical and visible declarations.
861 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
862 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
863 if (Offsets.first || Offsets.second) {
864 DC->setHasExternalLexicalStorage(Offsets.first != 0);
865 DC->setHasExternalVisibleStorage(Offsets.second != 0);
866 DeclContextOffsets[DC] = Offsets;
867 }
868 }
869 assert(Idx == Record.size());
870
871 return D;
872}
873
Douglas Gregor8038d512009-04-10 17:25:41 +0000874QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000875 unsigned Quals = ID & 0x07;
876 unsigned Index = ID >> 3;
877
878 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
879 QualType T;
880 switch ((pch::PredefinedTypeIDs)Index) {
881 case pch::PREDEF_TYPE_NULL_ID: return QualType();
882 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
883 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
884
885 case pch::PREDEF_TYPE_CHAR_U_ID:
886 case pch::PREDEF_TYPE_CHAR_S_ID:
887 // FIXME: Check that the signedness of CharTy is correct!
888 T = Context.CharTy;
889 break;
890
891 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
892 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
893 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
894 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
895 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
896 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
897 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
898 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
899 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
900 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
901 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
902 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
903 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
904 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
905 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
906 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
907 }
908
909 assert(!T.isNull() && "Unknown predefined type");
910 return T.getQualifiedType(Quals);
911 }
912
913 Index -= pch::NUM_PREDEF_TYPE_IDS;
914 if (!TypeAlreadyLoaded[Index]) {
915 // Load the type from the PCH file.
916 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
917 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
918 TypeAlreadyLoaded[Index] = true;
919 }
920
921 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
922}
923
Douglas Gregor8038d512009-04-10 17:25:41 +0000924Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000925 if (ID == 0)
926 return 0;
927
928 unsigned Index = ID - 1;
929 if (DeclAlreadyLoaded[Index])
930 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
931
932 // Load the declaration from the PCH file.
933 return ReadDeclRecord(DeclOffsets[Index], Index);
934}
935
936bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +0000937 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000938 assert(DC->hasExternalLexicalStorage() &&
939 "DeclContext has no lexical decls in storage");
940 uint64_t Offset = DeclContextOffsets[DC].first;
941 assert(Offset && "DeclContext has no lexical decls in storage");
942
943 // Load the record containing all of the declarations lexically in
944 // this context.
945 Stream.JumpToBit(Offset);
946 RecordData Record;
947 unsigned Code = Stream.ReadCode();
948 unsigned RecCode = Stream.ReadRecord(Code, Record);
949 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
950
951 // Load all of the declaration IDs
952 Decls.clear();
953 Decls.insert(Decls.end(), Record.begin(), Record.end());
954 return false;
955}
956
957bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
958 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
959 assert(DC->hasExternalVisibleStorage() &&
960 "DeclContext has no visible decls in storage");
961 uint64_t Offset = DeclContextOffsets[DC].second;
962 assert(Offset && "DeclContext has no visible decls in storage");
963
964 // Load the record containing all of the declarations visible in
965 // this context.
966 Stream.JumpToBit(Offset);
967 RecordData Record;
968 unsigned Code = Stream.ReadCode();
969 unsigned RecCode = Stream.ReadRecord(Code, Record);
970 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
971 if (Record.size() == 0)
972 return false;
973
974 Decls.clear();
975
976 unsigned Idx = 0;
977 // llvm::SmallVector<uintptr_t, 16> DeclIDs;
978 while (Idx < Record.size()) {
979 Decls.push_back(VisibleDeclaration());
980 Decls.back().Name = ReadDeclarationName(Record, Idx);
981
982 // FIXME: Don't actually read anything here!
983 unsigned Size = Record[Idx++];
984 llvm::SmallVector<unsigned, 4> & LoadedDecls
985 = Decls.back().Declarations;
986 LoadedDecls.reserve(Size);
987 for (unsigned I = 0; I < Size; ++I)
988 LoadedDecls.push_back(Record[Idx++]);
989 }
990
991 return false;
992}
993
994void PCHReader::PrintStats() {
995 std::fprintf(stderr, "*** PCH Statistics:\n");
996
997 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
998 TypeAlreadyLoaded.end(),
999 true);
1000 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1001 DeclAlreadyLoaded.end(),
1002 true);
1003 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1004 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
1005 ((float)NumTypesLoaded/(float)TypeAlreadyLoaded.size() * 100));
1006 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1007 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
1008 ((float)NumDeclsLoaded/(float)DeclAlreadyLoaded.size() * 100));
1009 std::fprintf(stderr, "\n");
1010}
1011
Chris Lattner7356a312009-04-11 21:15:38 +00001012IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001013 if (ID == 0)
1014 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00001015
Douglas Gregorafaf3082009-04-11 00:14:32 +00001016 if (!IdentifierTable || IdentifierData.empty()) {
1017 Error("No identifier table in PCH file");
1018 return 0;
1019 }
Chris Lattner7356a312009-04-11 21:15:38 +00001020
Douglas Gregorafaf3082009-04-11 00:14:32 +00001021 if (IdentifierData[ID - 1] & 0x01) {
1022 uint64_t Offset = IdentifierData[ID - 1];
1023 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00001024 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001025 }
Chris Lattner7356a312009-04-11 21:15:38 +00001026
1027 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001028}
1029
1030DeclarationName
1031PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1032 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1033 switch (Kind) {
1034 case DeclarationName::Identifier:
1035 return DeclarationName(GetIdentifierInfo(Record, Idx));
1036
1037 case DeclarationName::ObjCZeroArgSelector:
1038 case DeclarationName::ObjCOneArgSelector:
1039 case DeclarationName::ObjCMultiArgSelector:
1040 assert(false && "Unable to de-serialize Objective-C selectors");
1041 break;
1042
1043 case DeclarationName::CXXConstructorName:
1044 return Context.DeclarationNames.getCXXConstructorName(
1045 GetType(Record[Idx++]));
1046
1047 case DeclarationName::CXXDestructorName:
1048 return Context.DeclarationNames.getCXXDestructorName(
1049 GetType(Record[Idx++]));
1050
1051 case DeclarationName::CXXConversionFunctionName:
1052 return Context.DeclarationNames.getCXXConversionFunctionName(
1053 GetType(Record[Idx++]));
1054
1055 case DeclarationName::CXXOperatorName:
1056 return Context.DeclarationNames.getCXXOperatorName(
1057 (OverloadedOperatorKind)Record[Idx++]);
1058
1059 case DeclarationName::CXXUsingDirective:
1060 return DeclarationName::getUsingDirectiveName();
1061 }
1062
1063 // Required to silence GCC warning
1064 return DeclarationName();
1065}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001066
1067DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001068 return Diag(SourceLocation(), DiagID);
1069}
1070
1071DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
1072 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001073 Context.getSourceManager()),
1074 DiagID);
1075}