blob: 339a1c466bc967613072c55e4d6e27af299dc98f [file] [log] [blame]
Ted Kremeneka4b44dd2009-02-13 19:13:46 +00001//===--- CacheTokens.cpp - Caching of lexer tokens for PTH support --------===//
Ted Kremenek85888962008-10-21 00:54:44 +00002//
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//
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000010// This provides a possible implementation of PTH support for Clang that is
Ted Kremenek85888962008-10-21 00:54:44 +000011// based on caching lexed tokens and identifiers.
12//
13//===----------------------------------------------------------------------===//
14
Eli Friedmanb09f6e12009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Ted Kremenek85888962008-10-21 00:54:44 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/Diagnostic.h"
Douglas Gregor9378ba42009-04-20 07:08:21 +000020#include "clang/Basic/OnDiskHashTable.h"
Ted Kremenek85888962008-10-21 00:54:44 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
Daniel Dunbardf5a2372009-10-17 20:43:58 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000025#include "llvm/Support/Compiler.h"
Ted Kremenek85888962008-10-21 00:54:44 +000026#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000028#include "llvm/System/Path.h"
Ted Kremenek85888962008-10-21 00:54:44 +000029
Cedric Venetea684e62009-02-14 16:15:20 +000030// FIXME: put this somewhere else?
31#ifndef S_ISDIR
32#define S_ISDIR(x) (((x)&_S_IFDIR)!=0)
33#endif
34
Ted Kremenek85888962008-10-21 00:54:44 +000035using namespace clang;
Douglas Gregor9378ba42009-04-20 07:08:21 +000036using namespace clang::io;
Ted Kremenekf0e1f792009-02-10 01:14:45 +000037
38//===----------------------------------------------------------------------===//
39// PTH-specific stuff.
40//===----------------------------------------------------------------------===//
41
Ted Kremenekbe295332009-01-08 02:44:06 +000042namespace {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000043class VISIBILITY_HIDDEN PTHEntry {
Mike Stump1eb44332009-09-09 15:08:12 +000044 Offset TokenData, PPCondData;
Ted Kremenekbe295332009-01-08 02:44:06 +000045
Mike Stump1eb44332009-09-09 15:08:12 +000046public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000047 PTHEntry() {}
Ted Kremenekbe295332009-01-08 02:44:06 +000048
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000049 PTHEntry(Offset td, Offset ppcd)
Ted Kremenek277faca2009-01-27 00:01:05 +000050 : TokenData(td), PPCondData(ppcd) {}
Mike Stump1eb44332009-09-09 15:08:12 +000051
52 Offset getTokenOffset() const { return TokenData; }
Ted Kremenekbe295332009-01-08 02:44:06 +000053 Offset getPPCondTableOffset() const { return PPCondData; }
Ted Kremenek277faca2009-01-27 00:01:05 +000054};
Mike Stump1eb44332009-09-09 15:08:12 +000055
56
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000057class VISIBILITY_HIDDEN PTHEntryKeyVariant {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000058 union { const FileEntry* FE; const char* Path; };
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000059 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000060 struct stat *StatBuf;
Ted Kremenekd8c02922009-02-10 22:16:22 +000061public:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000062 PTHEntryKeyVariant(const FileEntry *fe)
63 : FE(fe), Kind(IsFE), StatBuf(0) {}
64
65 PTHEntryKeyVariant(struct stat* statbuf, const char* path)
66 : Path(path), Kind(IsDE), StatBuf(new struct stat(*statbuf)) {}
67
68 PTHEntryKeyVariant(const char* path)
69 : Path(path), Kind(IsNoExist), StatBuf(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000070
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000071 bool isFile() const { return Kind == IsFE; }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Daniel Dunbardf5a2372009-10-17 20:43:58 +000073 llvm::StringRef getString() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000074 return Kind == IsFE ? FE->getName() : Path;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000075 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000077 unsigned getKind() const { return (unsigned) Kind; }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Ted Kremeneka4b44dd2009-02-13 19:13:46 +000079 void EmitData(llvm::raw_ostream& Out) {
80 switch (Kind) {
81 case IsFE:
82 // Emit stat information.
83 ::Emit32(Out, FE->getInode());
84 ::Emit32(Out, FE->getDevice());
85 ::Emit16(Out, FE->getFileMode());
86 ::Emit64(Out, FE->getModificationTime());
87 ::Emit64(Out, FE->getSize());
88 break;
89 case IsDE:
Ted Kremenekad6ce5c2009-02-13 22:07:44 +000090 // Emit stat information.
91 ::Emit32(Out, (uint32_t) StatBuf->st_ino);
92 ::Emit32(Out, (uint32_t) StatBuf->st_dev);
93 ::Emit16(Out, (uint16_t) StatBuf->st_mode);
94 ::Emit64(Out, (uint64_t) StatBuf->st_mtime);
95 ::Emit64(Out, (uint64_t) StatBuf->st_size);
96 delete StatBuf;
97 break;
98 default:
99 break;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000100 }
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000103 unsigned getRepresentationLength() const {
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000104 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000105 }
106};
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000108class VISIBILITY_HIDDEN FileEntryPTHEntryInfo {
109public:
110 typedef PTHEntryKeyVariant key_type;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000111 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000113 typedef PTHEntry data_type;
114 typedef const PTHEntry& data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000116 static unsigned ComputeHash(PTHEntryKeyVariant V) {
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000117 return llvm::HashString(V.getString());
Ted Kremenekd8c02922009-02-10 22:16:22 +0000118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
120 static std::pair<unsigned,unsigned>
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000121 EmitKeyDataLength(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
122 const PTHEntry& E) {
Ted Kremenekd8c02922009-02-10 22:16:22 +0000123
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000124 unsigned n = V.getString().size() + 1 + 1;
Ted Kremenekd8c02922009-02-10 22:16:22 +0000125 ::Emit16(Out, n);
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000127 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000128 ::Emit8(Out, m);
129
130 return std::make_pair(n, m);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000133 static void EmitKey(llvm::raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
134 // Emit the entry kind.
135 ::Emit8(Out, (unsigned) V.getKind());
136 // Emit the string.
Daniel Dunbardf5a2372009-10-17 20:43:58 +0000137 Out.write(V.getString().data(), n - 1);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
140 static void EmitData(llvm::raw_ostream& Out, PTHEntryKeyVariant V,
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000141 const PTHEntry& E, unsigned) {
142
143
144 // For file entries emit the offsets into the PTH file for token data
145 // and the preprocessor blocks table.
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000146 if (V.isFile()) {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000147 ::Emit32(Out, E.getTokenOffset());
148 ::Emit32(Out, E.getPPCondTableOffset());
149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000151 // Emit any other data associated with the key (i.e., stat information).
152 V.EmitData(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000153 }
Ted Kremenekd8c02922009-02-10 22:16:22 +0000154};
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek277faca2009-01-27 00:01:05 +0000156class OffsetOpt {
157 bool valid;
158 Offset off;
159public:
160 OffsetOpt() : valid(false) {}
161 bool hasOffset() const { return valid; }
162 Offset getOffset() const { assert(valid); return off; }
163 void setOffset(Offset o) { off = o; valid = true; }
Ted Kremenekbe295332009-01-08 02:44:06 +0000164};
165} // end anonymous namespace
166
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000167typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000168typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenek277faca2009-01-27 00:01:05 +0000169typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek85888962008-10-21 00:54:44 +0000170
Ted Kremenekb978c662009-01-08 01:17:37 +0000171namespace {
172class VISIBILITY_HIDDEN PTHWriter {
173 IDMap IM;
174 llvm::raw_fd_ostream& Out;
175 Preprocessor& PP;
176 uint32_t idcount;
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000177 PTHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +0000178 CachedStrsTy CachedStrs;
Ted Kremenek277faca2009-01-27 00:01:05 +0000179 Offset CurStrOffset;
180 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
Ted Kremenek8f174e12008-12-23 02:52:12 +0000181
Ted Kremenekb978c662009-01-08 01:17:37 +0000182 //// Get the persistent id for the given IdentifierInfo*.
183 uint32_t ResolveID(const IdentifierInfo* II);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenekb978c662009-01-08 01:17:37 +0000185 /// Emit a token to the PTH file.
186 void EmitToken(const Token& T);
187
188 void Emit8(uint32_t V) {
189 Out << (unsigned char)(V);
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000192 void Emit16(uint32_t V) { ::Emit16(Out, V); }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenekb978c662009-01-08 01:17:37 +0000194 void Emit24(uint32_t V) {
195 Out << (unsigned char)(V);
196 Out << (unsigned char)(V >> 8);
197 Out << (unsigned char)(V >> 16);
198 assert((V >> 24) == 0);
199 }
200
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000201 void Emit32(uint32_t V) { ::Emit32(Out, V); }
202
Chris Lattnerf2390362009-03-28 00:16:20 +0000203 void EmitBuf(const char *Ptr, unsigned NumBytes) {
204 Out.write(Ptr, NumBytes);
Ted Kremenekb978c662009-01-08 01:17:37 +0000205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000207 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
208 /// a hashtable mapping from identifier strings to persistent IDs.
209 /// The second is a straight table mapping from persistent IDs to string data
210 /// (the keys of the first table).
Ted Kremenekf1de4642009-02-11 16:06:55 +0000211 std::pair<Offset, Offset> EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenekf1de4642009-02-11 16:06:55 +0000213 /// EmitFileTable - Emit a table mapping from file name strings to PTH
214 /// token data.
215 Offset EmitFileTable() { return PM.Emit(Out); }
216
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000217 PTHEntry LexTokens(Lexer& L);
Ted Kremenek277faca2009-01-27 00:01:05 +0000218 Offset EmitCachedSpellings();
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000219
Ted Kremenekb978c662009-01-08 01:17:37 +0000220public:
Mike Stump1eb44332009-09-09 15:08:12 +0000221 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
Ted Kremenek277faca2009-01-27 00:01:05 +0000222 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattner52ba8702009-03-28 00:55:35 +0000224 PTHMap &getPM() { return PM; }
Ted Kremenekd5cded42009-03-19 22:10:38 +0000225 void GeneratePTH(const std::string *MainFile = 0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000226};
227} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000228
229uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000230 // Null IdentifierInfo's map to the persistent ID 0.
231 if (!II)
232 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek85888962008-10-21 00:54:44 +0000234 IDMap::iterator I = IM.find(II);
Chris Lattnerf2390362009-03-28 00:16:20 +0000235 if (I != IM.end())
236 return I->second; // We've already added 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattnerf2390362009-03-28 00:16:20 +0000238 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
239 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000240}
241
Ted Kremenekb978c662009-01-08 01:17:37 +0000242void PTHWriter::EmitToken(const Token& T) {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000243 // Emit the token kind, flags, and length.
244 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
245 (((uint32_t) T.getLength()) << 16));
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattnerf2390362009-03-28 00:16:20 +0000247 if (!T.isLiteral()) {
248 Emit32(ResolveID(T.getIdentifierInfo()));
249 } else {
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000250 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
251 // source code.
252 const char* s = T.getLiteralData();
253 unsigned len = T.getLength();
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000254
Chris Lattner47246be2009-01-26 19:29:26 +0000255 // Get the string entry.
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000256 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s, s+len);
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000258 // If this is a new string entry, bump the PTH offset.
Ted Kremenek277faca2009-01-27 00:01:05 +0000259 if (!E->getValue().hasOffset()) {
260 E->getValue().setOffset(CurStrOffset);
261 StrEntries.push_back(E);
Ted Kremenekf2a223f2009-02-24 01:26:56 +0000262 CurStrOffset += len + 1;
Ted Kremenek277faca2009-01-27 00:01:05 +0000263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000265 // Emit the relative offset into the PTH file for the spelling string.
Ted Kremenek277faca2009-01-27 00:01:05 +0000266 Emit32(E->getValue().getOffset());
Ted Kremenekb978c662009-01-08 01:17:37 +0000267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek25cbd9f2009-02-24 00:30:21 +0000269 // Emit the offset into the original source file of this token so that we
270 // can reconstruct its SourceLocation.
Chris Lattner52c29082009-01-27 06:27:13 +0000271 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +0000272}
273
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000274PTHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenek7b78b7c2009-01-19 23:13:15 +0000275 // Pad 0's so that we emit tokens to a 4-byte alignment.
276 // This speed up reading them back in.
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000277 Pad(Out, 4);
278 Offset off = (Offset) Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenekfb645b62008-12-11 23:36:38 +0000280 // Keep track of matching '#if' ... '#endif'.
281 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
282 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000283 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000284 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000285 Token Tok;
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000287 do {
288 L.LexFromRawLexer(Tok);
Ted Kremenek726080d2009-02-10 22:43:16 +0000289 NextToken:
290
Ted Kremeneke5680f32008-12-23 01:30:52 +0000291 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
292 ParsingPreprocessorDirective) {
293 // Insert an eom token into the token cache. It has the same
294 // position as the next token that is not on the same line as the
295 // preprocessor directive. Observe that we continue processing
296 // 'Tok' when we exit this branch.
297 Token Tmp = Tok;
298 Tmp.setKind(tok::eom);
299 Tmp.clearFlag(Token::StartOfLine);
300 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000301 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000302 ParsingPreprocessorDirective = false;
303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000305 if (Tok.is(tok::identifier)) {
306 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000307 EmitToken(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000308 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000309 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000310
311 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000312 // Special processing for #include. Store the '#' token and lex
313 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000314 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000315 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000316 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000317
318 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000319 L.LexFromRawLexer(Tok);
Chris Lattner21356192009-04-19 07:25:40 +0000320
321 // If we see the start of line, then we had a null directive "#".
322 if (Tok.isAtStartOfLine())
323 goto NextToken;
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000325 // Did we see 'include'/'import'/'include_next'?
Chris Lattnerd4b14462009-04-19 07:15:51 +0000326 if (Tok.isNot(tok::identifier)) {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000327 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000328 continue;
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000331 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
332 Tok.setIdentifierInfo(II);
333 tok::PPKeywordKind K = II->getPPKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremeneke5680f32008-12-23 01:30:52 +0000335 ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremeneke5680f32008-12-23 01:30:52 +0000337 switch (K) {
Chris Lattneraa269c22009-04-19 07:32:03 +0000338 case tok::pp_not_keyword:
339 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
340 // them through.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000341 default:
342 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremeneke5680f32008-12-23 01:30:52 +0000344 case tok::pp_include:
345 case tok::pp_import:
Mike Stump1eb44332009-09-09 15:08:12 +0000346 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000347 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000348 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000349 // Lex the next token as an include string.
350 L.setParsingPreprocessorDirective(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000351 L.LexIncludeFilename(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000352 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000353 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000354 if (Tok.is(tok::identifier))
355 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremeneke5680f32008-12-23 01:30:52 +0000357 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000358 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000359 case tok::pp_if:
360 case tok::pp_ifdef:
361 case tok::pp_ifndef: {
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000362 // Add an entry for '#if' and friends. We initially set the target
363 // index to 0. This will get backpatched when we hit #endif.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000364 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000365 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000366 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000367 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000368 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000369 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000370 // This will later be set to zero when emitting to the PTH file. We
371 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000372 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000373 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000374 assert(!PPStartCond.empty());
375 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000376 assert(PPCond[PPStartCond.back()].second == 0);
377 PPCond[PPStartCond.back()].second = index;
Mike Stump1eb44332009-09-09 15:08:12 +0000378 PPStartCond.pop_back();
379 // Add the new entry to PPCond.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000380 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenek726080d2009-02-10 22:43:16 +0000381 EmitToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek726080d2009-02-10 22:43:16 +0000383 // Some files have gibberish on the same line as '#endif'.
384 // Discard these tokens.
Chris Lattnerd4b14462009-04-19 07:15:51 +0000385 do
386 L.LexFromRawLexer(Tok);
387 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
Ted Kremenek726080d2009-02-10 22:43:16 +0000388 // We have the next token in hand.
389 // Don't immediately lex the next one.
Mike Stump1eb44332009-09-09 15:08:12 +0000390 goto NextToken;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000391 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000392 case tok::pp_elif:
393 case tok::pp_else: {
394 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000395 // This serves as both a closing and opening of a conditional block.
396 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000397 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000398 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000399 assert(!PPStartCond.empty());
400 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000401 assert(PPCond[PPStartCond.back()].second == 0);
402 PPCond[PPStartCond.back()].second = index;
403 PPStartCond.pop_back();
404 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000405 PPCond.push_back(std::make_pair(HashOff, 0U));
406 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000407 break;
408 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000409 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000412 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000413 }
Ted Kremeneke4f6b1e2009-02-10 22:27:09 +0000414 while (Tok.isNot(tok::eof));
Ted Kremenekb978c662009-01-08 01:17:37 +0000415
Ted Kremenekdad7b342008-12-12 18:31:09 +0000416 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000417
Ted Kremenekfb645b62008-12-11 23:36:38 +0000418 // Next write out PPCond.
419 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000420
421 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000422 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000423
Ted Kremenekdad7b342008-12-12 18:31:09 +0000424 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000425 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000426 uint32_t x = PPCond[i].second;
427 assert(x != 0 && "PPCond entry not backpatched.");
428 // Emit zero for #endifs. This allows us to do checking when
429 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000430 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000431 }
432
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000433 return PTHEntry(off, PPCondOff);
Ted Kremenekbe295332009-01-08 02:44:06 +0000434}
435
Ted Kremenek277faca2009-01-27 00:01:05 +0000436Offset PTHWriter::EmitCachedSpellings() {
437 // Write each cached strings to the PTH file.
438 Offset SpellingsOff = Out.tell();
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek277faca2009-01-27 00:01:05 +0000440 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
Chris Lattnerf2390362009-03-28 00:16:20 +0000441 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
442 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek277faca2009-01-27 00:01:05 +0000444 return SpellingsOff;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000445}
Ted Kremenek85888962008-10-21 00:54:44 +0000446
Ted Kremenekd5cded42009-03-19 22:10:38 +0000447void PTHWriter::GeneratePTH(const std::string *MainFile) {
Ted Kremeneke1b64982009-01-26 21:43:14 +0000448 // Generate the prologue.
449 Out << "cfe-pth";
Ted Kremenek67d15052009-01-26 21:50:21 +0000450 Emit32(PTHManager::Version);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000452 // Leave 4 words for the prologue.
453 Offset PrologueOffset = Out.tell();
Chris Lattnerf2390362009-03-28 00:16:20 +0000454 for (unsigned i = 0; i < 4; ++i)
455 Emit32(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenekd5cded42009-03-19 22:10:38 +0000457 // Write the name of the MainFile.
Chris Lattnerf2390362009-03-28 00:16:20 +0000458 if (MainFile && !MainFile->empty()) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000459 Emit16(MainFile->length());
Chris Lattnerf2390362009-03-28 00:16:20 +0000460 EmitBuf(MainFile->data(), MainFile->length());
461 } else {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000462 // String with 0 bytes.
463 Emit16(0);
464 }
465 Emit8(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek85888962008-10-21 00:54:44 +0000467 // Iterate over all the files in SourceManager. Create a lexer
468 // for each file and cache the tokens.
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000469 SourceManager &SM = PP.getSourceManager();
470 const LangOptions &LOpts = PP.getLangOptions();
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000472 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
473 E = SM.fileinfo_end(); I != E; ++I) {
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000474 const SrcMgr::ContentCache &C = *I->second;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000475 const FileEntry *FE = C.Entry;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000477 // FIXME: Handle files with non-absolute paths.
478 llvm::sys::Path P(FE->getName());
479 if (!P.isAbsolute())
480 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000481
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000482 const llvm::MemoryBuffer *B = C.getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000483 if (!B) continue;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000484
Chris Lattner2b2453a2009-01-17 06:22:33 +0000485 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
Chris Lattner025c3a62009-01-17 07:35:14 +0000486 Lexer L(FID, SM, LOpts);
Ted Kremenekd8c02922009-02-10 22:16:22 +0000487 PM.insert(FE, LexTokens(L));
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000488 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000489
490 // Write out the identifier table.
Chris Lattnerf2390362009-03-28 00:16:20 +0000491 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenekbe295332009-01-08 02:44:06 +0000493 // Write out the cached strings table.
Ted Kremenek277faca2009-01-27 00:01:05 +0000494 Offset SpellingOff = EmitCachedSpellings();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000496 // Write out the file table.
Mike Stump1eb44332009-09-09 15:08:12 +0000497 Offset FileTableOff = EmitFileTable();
498
Ted Kremeneka4bd8eb2009-02-11 23:34:32 +0000499 // Finally, write the prologue.
500 Out.seek(PrologueOffset);
Ted Kremenekb978c662009-01-08 01:17:37 +0000501 Emit32(IdTableOff.first);
Ted Kremenekf1de4642009-02-11 16:06:55 +0000502 Emit32(IdTableOff.second);
Ted Kremenekb978c662009-01-08 01:17:37 +0000503 Emit32(FileTableOff);
Ted Kremenek277faca2009-01-27 00:01:05 +0000504 Emit32(SpellingOff);
Ted Kremenekb978c662009-01-08 01:17:37 +0000505}
506
Chris Lattner52ba8702009-03-28 00:55:35 +0000507namespace {
508/// StatListener - A simple "interpose" object used to monitor stat calls
509/// invoked by FileManager while processing the original sources used
510/// as input to PTH generation. StatListener populates the PTHWriter's
511/// file map with stat information for directories as well as negative stats.
512/// Stat information for files are populated elsewhere.
513class StatListener : public StatSysCallCache {
514 PTHMap &PM;
515public:
516 StatListener(PTHMap &pm) : PM(pm) {}
517 ~StatListener() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattner52ba8702009-03-28 00:55:35 +0000519 int stat(const char *path, struct stat *buf) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000520 int result = StatSysCallCache::stat(path, buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattner52ba8702009-03-28 00:55:35 +0000522 if (result != 0) // Failed 'stat'.
523 PM.insert(path, PTHEntry());
524 else if (S_ISDIR(buf->st_mode)) {
525 // Only cache directories with absolute paths.
526 if (!llvm::sys::Path(path).isAbsolute())
527 return result;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner52ba8702009-03-28 00:55:35 +0000529 PM.insert(PTHEntryKeyVariant(buf, path), PTHEntry());
530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattner52ba8702009-03-28 00:55:35 +0000532 return result;
533 }
534};
535} // end anonymous namespace
536
537
Eli Friedmanf54fce82009-05-19 01:02:07 +0000538void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
Ted Kremenekd5cded42009-03-19 22:10:38 +0000539 // Get the name of the main file.
540 const SourceManager &SrcMgr = PP.getSourceManager();
541 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
542 llvm::sys::Path MainFilePath(MainFile->getName());
543 std::string MainFileName;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenekd5cded42009-03-19 22:10:38 +0000545 if (!MainFilePath.isAbsolute()) {
546 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000547 P.appendComponent(MainFilePath.str());
548 MainFileName = P.str();
Chris Lattnerf2390362009-03-28 00:16:20 +0000549 } else {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000550 MainFileName = MainFilePath.str();
Ted Kremenekd5cded42009-03-19 22:10:38 +0000551 }
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000552
553 // Create the PTHWriter.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000554 PTHWriter PW(*OS, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000556 // Install the 'stat' system call listener in the FileManager.
Douglas Gregor52e71082009-10-16 18:18:30 +0000557 StatListener *StatCache = new StatListener(PW.getPM());
558 PP.getFileManager().addStatCache(StatCache, /*AtBeginning=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000560 // Lex through the entire file. This will populate SourceManager with
561 // all of the header information.
562 Token Tok;
563 PP.EnterMainSourceFile();
564 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Ted Kremenekad6ce5c2009-02-13 22:07:44 +0000566 // Generate the PTH file.
Douglas Gregor52e71082009-10-16 18:18:30 +0000567 PP.getFileManager().removeStatCache(StatCache);
Ted Kremenekd5cded42009-03-19 22:10:38 +0000568 PW.GeneratePTH(&MainFileName);
Ted Kremenek85888962008-10-21 00:54:44 +0000569}
Ted Kremeneke0ea5dc2009-02-10 01:06:17 +0000570
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000571//===----------------------------------------------------------------------===//
572
Chris Lattnerf2390362009-03-28 00:16:20 +0000573class PTHIdKey {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000574public:
575 const IdentifierInfo* II;
576 uint32_t FileOffset;
577};
578
Chris Lattnerf2390362009-03-28 00:16:20 +0000579namespace {
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000580class VISIBILITY_HIDDEN PTHIdentifierTableTrait {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000581public:
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000582 typedef PTHIdKey* key_type;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000583 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000585 typedef uint32_t data_type;
586 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000588 static unsigned ComputeHash(PTHIdKey* key) {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000589 return llvm::HashString(key->II->getName());
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
592 static std::pair<unsigned,unsigned>
593 EmitKeyDataLength(llvm::raw_ostream& Out, const PTHIdKey* key, uint32_t) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000594 unsigned n = key->II->getLength() + 1;
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000595 ::Emit16(Out, n);
596 return std::make_pair(n, sizeof(uint32_t));
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregor3432fa52009-04-20 07:36:26 +0000599 static void EmitKey(llvm::raw_ostream& Out, PTHIdKey* key, unsigned n) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000600 // Record the location of the key data. This is used when generating
601 // the mapping from persistent IDs to strings.
602 key->FileOffset = Out.tell();
Daniel Dunbare013d682009-10-18 20:26:12 +0000603 Out.write(key->II->getNameStart(), n);
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000606 static void EmitData(llvm::raw_ostream& Out, PTHIdKey*, uint32_t pID,
Ted Kremenek337edcd2009-02-12 03:26:59 +0000607 unsigned) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000608 ::Emit32(Out, pID);
Mike Stump1eb44332009-09-09 15:08:12 +0000609 }
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000610};
611} // end anonymous namespace
612
613/// EmitIdentifierTable - Emits two tables to the PTH file. The first is
614/// a hashtable mapping from identifier strings to persistent IDs. The second
615/// is a straight table mapping from persistent IDs to string data (the
616/// keys of the first table).
617///
618std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
619 // Build two maps:
620 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
621 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
622
623 // Note that we use 'calloc', so all the bytes are 0.
Chris Lattnerf2390362009-03-28 00:16:20 +0000624 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000625
626 // Create the hashtable.
Ted Kremeneka4b44dd2009-02-13 19:13:46 +0000627 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000629 // Generate mapping from persistent IDs -> IdentifierInfo*.
Chris Lattnerf2390362009-03-28 00:16:20 +0000630 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000631 // Decrement by 1 because we are using a vector for the lookup and
632 // 0 is reserved for NULL.
633 assert(I->second > 0);
634 assert(I->second-1 < idcount);
635 unsigned idx = I->second-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000637 // Store the mapping from persistent ID to IdentifierInfo*
638 IIDMap[idx].II = I->first;
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000640 // Store the reverse mapping in a hashtable.
641 IIOffMap.insert(&IIDMap[idx], I->second);
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000644 // Write out the inverse map first. This causes the PCIDKey entries to
645 // record PTH file offsets for the string data. This is used to write
646 // the second table.
647 Offset StringTableOffset = IIOffMap.Emit(Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
649 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000650 Offset IDOff = Out.tell();
651 Emit32(idcount); // Emit the number of identifiers.
Chris Lattnerf2390362009-03-28 00:16:20 +0000652 for (unsigned i = 0 ; i < idcount; ++i)
653 Emit32(IIDMap[i].FileOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000655 // Finally, release the inverse map.
656 free(IIDMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek7e3a0042009-02-11 21:29:16 +0000658 return std::make_pair(IDOff, StringTableOffset);
659}