blob: 6c0c5da7f1f5685c0e6dcaa136ee031f77e74594 [file] [log] [blame]
Ted Kremenek85888962008-10-21 00:54:44 +00001//===--- CacheTokens.cpp - Caching of lexer tokens for PCH support --------===//
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 provides a possible implementation of PCH support for Clang that is
11// based on caching lexed tokens and identifiers.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Lex/Lexer.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenekbe295332009-01-08 02:44:06 +000022#include "llvm/ADT/StringMap.h"
Ted Kremenek85888962008-10-21 00:54:44 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000025#include "llvm/System/Path.h"
Ted Kremenekb978c662009-01-08 01:17:37 +000026#include "llvm/Support/Compiler.h"
Ted Kremenek85888962008-10-21 00:54:44 +000027
28using namespace clang;
29
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000030typedef uint32_t Offset;
31
Ted Kremenekf89f7f92009-01-08 23:40:50 +000032typedef std::pair<Offset,bool> SpellingTy;
33typedef std::vector<std::pair<Offset, llvm::StringMapEntry<SpellingTy>*> >
Ted Kremenekbe295332009-01-08 02:44:06 +000034 SpellMapTy;
35
36namespace {
37class VISIBILITY_HIDDEN PCHEntry {
38 Offset TokenData, PPCondData;
39 union { Offset SpellingOff; SpellMapTy* Spellings; };
40
41public:
42 PCHEntry() {}
43
44 PCHEntry(Offset td, Offset ppcd, SpellMapTy* sp)
45 : TokenData(td), PPCondData(ppcd), Spellings(sp) {}
46
47 Offset getTokenOffset() const { return TokenData; }
48 Offset getPPCondTableOffset() const { return PPCondData; }
49 SpellMapTy& getSpellings() const { return *Spellings; }
50
51 void setSpellingTableOffset(Offset off) { SpellingOff = off; }
52 Offset getSpellingTableOffset() const { return SpellingOff; }
53
54};
55} // end anonymous namespace
56
57typedef llvm::DenseMap<const FileEntry*, PCHEntry> PCHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000058typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenekf89f7f92009-01-08 23:40:50 +000059typedef llvm::StringMap< SpellingTy, llvm::BumpPtrAllocator> CachedStrsTy;
Ted Kremenek85888962008-10-21 00:54:44 +000060
Ted Kremenekb978c662009-01-08 01:17:37 +000061namespace {
62class VISIBILITY_HIDDEN PTHWriter {
63 IDMap IM;
64 llvm::raw_fd_ostream& Out;
65 Preprocessor& PP;
66 uint32_t idcount;
67 PCHMap PM;
Ted Kremenekbe295332009-01-08 02:44:06 +000068 CachedStrsTy CachedStrs;
69
70 SpellMapTy* CurSpellMap;
Ted Kremenek8f174e12008-12-23 02:52:12 +000071
Ted Kremenekb978c662009-01-08 01:17:37 +000072 //// Get the persistent id for the given IdentifierInfo*.
73 uint32_t ResolveID(const IdentifierInfo* II);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000074
Ted Kremenekb978c662009-01-08 01:17:37 +000075 /// Emit a token to the PTH file.
76 void EmitToken(const Token& T);
77
78 void Emit8(uint32_t V) {
79 Out << (unsigned char)(V);
80 }
81
82 void Emit16(uint32_t V) {
83 Out << (unsigned char)(V);
84 Out << (unsigned char)(V >> 8);
85 assert((V >> 16) == 0);
86 }
87
88 void Emit24(uint32_t V) {
89 Out << (unsigned char)(V);
90 Out << (unsigned char)(V >> 8);
91 Out << (unsigned char)(V >> 16);
92 assert((V >> 24) == 0);
93 }
94
95 void Emit32(uint32_t V) {
96 Out << (unsigned char)(V);
97 Out << (unsigned char)(V >> 8);
98 Out << (unsigned char)(V >> 16);
99 Out << (unsigned char)(V >> 24);
100 }
101
102 void EmitBuf(const char* I, const char* E) {
103 for ( ; I != E ; ++I) Out << *I;
104 }
105
106 std::pair<Offset,Offset> EmitIdentifierTable();
107 Offset EmitFileTable();
Ted Kremenekbe295332009-01-08 02:44:06 +0000108 PCHEntry LexTokens(Lexer& L);
109 void EmitCachedSpellings();
110
Ted Kremenekb978c662009-01-08 01:17:37 +0000111public:
112 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
113 : Out(out), PP(pp), idcount(0) {}
114
115 void GeneratePTH();
116};
117} // end anonymous namespace
118
119uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000120 // Null IdentifierInfo's map to the persistent ID 0.
121 if (!II)
122 return 0;
123
Ted Kremenek85888962008-10-21 00:54:44 +0000124 IDMap::iterator I = IM.find(II);
125
126 if (I == IM.end()) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000127 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
128 return idcount;
Ted Kremenek85888962008-10-21 00:54:44 +0000129 }
130
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000131 return I->second; // We've already added 1.
Ted Kremenek85888962008-10-21 00:54:44 +0000132}
133
Ted Kremenekb978c662009-01-08 01:17:37 +0000134void PTHWriter::EmitToken(const Token& T) {
135 uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation());
136 Emit8(T.getKind());
137 Emit8(T.getFlags());
138 Emit24(ResolveID(T.getIdentifierInfo()));
139 Emit32(fpos);
140 Emit16(T.getLength());
141
Ted Kremenekb978c662009-01-08 01:17:37 +0000142 // For specific tokens we cache their spelling.
143 if (T.getIdentifierInfo())
144 return;
145
146 switch (T.getKind()) {
Ted Kremenekf89f7f92009-01-08 23:40:50 +0000147 default: return;
148 case tok::numeric_constant:
149 case tok::string_literal:
Ted Kremenekb978c662009-01-08 01:17:37 +0000150 case tok::wide_string_literal:
151 case tok::angle_string_literal:
Ted Kremenekbe295332009-01-08 02:44:06 +0000152 case tok::char_constant: {
153 // FIXME: This uses the slow getSpelling(). Perhaps we do better
154 // in the future? This only slows down PTH generation.
Ted Kremenekf89f7f92009-01-08 23:40:50 +0000155 std::string spelling = PP.getSpelling(T);
156
157 // If the token is a numeric literal we add a space after the spelling.
158 // This is to handle a shortcoming in LiteralSupport.cpp where
159 // literals are assumed to have a "valid" character after them.
160 bool IsNumeric = T.getKind() == tok::numeric_constant;
161
162 if (IsNumeric)
163 spelling.push_back(' ');
164
Ted Kremenekbe295332009-01-08 02:44:06 +0000165 const char* s = spelling.c_str();
166
167 // Get the string entry.
Ted Kremenekf89f7f92009-01-08 23:40:50 +0000168 llvm::StringMapEntry<std::pair<Offset,bool> > *E =
169 &CachedStrs.GetOrCreateValue(s, s + spelling.size());
170
171 E->getValue().second = IsNumeric;
Ted Kremenekbe295332009-01-08 02:44:06 +0000172
173 // Store the address of the string entry in our spelling map.
174 (*CurSpellMap).push_back(std::make_pair(fpos, E));
175
Ted Kremenekb978c662009-01-08 01:17:37 +0000176 break;
Ted Kremenekbe295332009-01-08 02:44:06 +0000177 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000178 }
Ted Kremenek85888962008-10-21 00:54:44 +0000179}
180
Ted Kremenekb978c662009-01-08 01:17:37 +0000181namespace {
182struct VISIBILITY_HIDDEN IDData {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000183 const IdentifierInfo* II;
184 uint32_t FileOffset;
185 const IdentifierTable::const_iterator::value_type* Str;
186};
Ted Kremenekb978c662009-01-08 01:17:37 +0000187}
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000188
Ted Kremenekb978c662009-01-08 01:17:37 +0000189std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
190
191 const IdentifierTable& T = PP.getIdentifierTable();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000192
193 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000194 typedef std::vector<IDData> InverseIDMap;
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000195 InverseIDMap IIDMap;
Ted Kremenekb978c662009-01-08 01:17:37 +0000196 IIDMap.resize(idcount);
Ted Kremenek85888962008-10-21 00:54:44 +0000197
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000198 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenekb978c662009-01-08 01:17:37 +0000199 for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000200 // Decrement by 1 because we are using a vector for the lookup and
201 // 0 is reserved for NULL.
202 assert(I->second > 0);
203 assert(I->second-1 < IIDMap.size());
204 IIDMap[I->second-1].II = I->first;
205 }
Ted Kremenek85888962008-10-21 00:54:44 +0000206
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000207 // Get the string data associated with the IdentifierInfo.
208 for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000209 IDMap::iterator IDI = IM.find(&(I->getValue()));
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000210 if (IDI == IM.end()) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000211 IIDMap[IDI->second-1].Str = &(*I);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000212 }
213
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000214 Offset DataOff = Out.tell();
Ted Kremenek6183e482008-12-03 01:16:39 +0000215
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000216 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000217 // Record the location for this data.
218 I->FileOffset = Out.tell();
219 // Write out the keyword.
220 unsigned len = I->Str->getKeyLength();
Ted Kremenekb978c662009-01-08 01:17:37 +0000221 Emit32(len);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000222 const char* buf = I->Str->getKeyData();
Ted Kremenekb978c662009-01-08 01:17:37 +0000223 EmitBuf(buf, buf+len);
Ted Kremenek85888962008-10-21 00:54:44 +0000224 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000225
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000226 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000227 Offset IDOff = Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000228
Ted Kremenek6183e482008-12-03 01:16:39 +0000229 // Emit the number of identifiers.
Ted Kremenekb978c662009-01-08 01:17:37 +0000230 Emit32(idcount);
Ted Kremenek6183e482008-12-03 01:16:39 +0000231
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000232 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Ted Kremenekb978c662009-01-08 01:17:37 +0000233 Emit32(I->FileOffset);
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000234
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000235 return std::make_pair(DataOff, IDOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000236}
237
Ted Kremenekb978c662009-01-08 01:17:37 +0000238Offset PTHWriter::EmitFileTable() {
239 // Determine the offset where this table appears in the PTH file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000240 Offset off = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000241
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000242 // Output the size of the table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000243 Emit32(PM.size());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000244
245 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000246 const FileEntry* FE = I->first;
Ted Kremenek8dffd9b2008-12-04 22:36:44 +0000247 const char* Name = FE->getName();
248 unsigned size = strlen(Name);
Ted Kremenekb978c662009-01-08 01:17:37 +0000249 Emit32(size);
250 EmitBuf(Name, Name+size);
Ted Kremenekbe295332009-01-08 02:44:06 +0000251 Emit32(I->second.getTokenOffset());
252 Emit32(I->second.getPPCondTableOffset());
253 Emit32(I->second.getSpellingTableOffset());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000254 }
255
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000256 return off;
257}
258
Ted Kremenekbe295332009-01-08 02:44:06 +0000259PCHEntry PTHWriter::LexTokens(Lexer& L) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000260
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000261 // Record the location within the token file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000262 Offset off = (Offset) Out.tell();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000263
264 // Keep track of matching '#if' ... '#endif'.
265 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
266 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000267 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000268 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000269
Ted Kremenekbe295332009-01-08 02:44:06 +0000270 // Allocate a spelling map for this source file.
271 llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy());
272 CurSpellMap = Spellings.get();
273
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000274 Token Tok;
275
276 do {
277 L.LexFromRawLexer(Tok);
278
Ted Kremeneke5680f32008-12-23 01:30:52 +0000279 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
280 ParsingPreprocessorDirective) {
281 // Insert an eom token into the token cache. It has the same
282 // position as the next token that is not on the same line as the
283 // preprocessor directive. Observe that we continue processing
284 // 'Tok' when we exit this branch.
285 Token Tmp = Tok;
286 Tmp.setKind(tok::eom);
287 Tmp.clearFlag(Token::StartOfLine);
288 Tmp.setIdentifierInfo(0);
Ted Kremenekb978c662009-01-08 01:17:37 +0000289 EmitToken(Tmp);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000290 ParsingPreprocessorDirective = false;
291 }
292
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000293 if (Tok.is(tok::identifier)) {
294 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000295 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000296 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000297
298 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000299 // Special processing for #include. Store the '#' token and lex
300 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000301 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000302 Offset HashOff = (Offset) Out.tell();
Ted Kremenekb978c662009-01-08 01:17:37 +0000303 EmitToken(Tok);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000304
305 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000306 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000307
308 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000309
310 // Did we see 'include'/'import'/'include_next'?
311 if (!Tok.is(tok::identifier))
312 continue;
313
314 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
315 Tok.setIdentifierInfo(II);
316 tok::PPKeywordKind K = II->getPPKeywordID();
317
Ted Kremeneke5680f32008-12-23 01:30:52 +0000318 assert(K != tok::pp_not_keyword);
319 ParsingPreprocessorDirective = true;
320
321 switch (K) {
322 default:
323 break;
324 case tok::pp_include:
325 case tok::pp_import:
326 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000327 // Save the 'include' token.
Ted Kremenekb978c662009-01-08 01:17:37 +0000328 EmitToken(Tok);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000329 // Lex the next token as an include string.
330 L.setParsingPreprocessorDirective(true);
331 L.LexIncludeFilename(Tok);
332 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000333 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000334 if (Tok.is(tok::identifier))
335 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000336
337 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000338 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000339 case tok::pp_if:
340 case tok::pp_ifdef:
341 case tok::pp_ifndef: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000342 // Ad an entry for '#if' and friends. We initially set the target index
343 // to 0. This will get backpatched when we hit #endif.
344 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000345 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000346 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000347 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000348 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000349 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000350 // This will later be set to zero when emitting to the PTH file. We
351 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000352 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000353 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000354 assert(!PPStartCond.empty());
355 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000356 assert(PPCond[PPStartCond.back()].second == 0);
357 PPCond[PPStartCond.back()].second = index;
358 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000359 // Add the new entry to PPCond.
360 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000361 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000362 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000363 case tok::pp_elif:
364 case tok::pp_else: {
365 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000366 // This serves as both a closing and opening of a conditional block.
367 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000368 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000369 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000370 assert(!PPStartCond.empty());
371 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000372 assert(PPCond[PPStartCond.back()].second == 0);
373 PPCond[PPStartCond.back()].second = index;
374 PPStartCond.pop_back();
375 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000376 PPCond.push_back(std::make_pair(HashOff, 0U));
377 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000378 break;
379 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000380 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000381 }
382 }
Ted Kremenekb978c662009-01-08 01:17:37 +0000383 while (EmitToken(Tok), Tok.isNot(tok::eof));
384
Ted Kremenekdad7b342008-12-12 18:31:09 +0000385 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
Ted Kremenekb978c662009-01-08 01:17:37 +0000386
Ted Kremenekfb645b62008-12-11 23:36:38 +0000387 // Next write out PPCond.
388 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000389
390 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekb978c662009-01-08 01:17:37 +0000391 Emit32(PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000392
Ted Kremenekdad7b342008-12-12 18:31:09 +0000393 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
Ted Kremenekb978c662009-01-08 01:17:37 +0000394 Emit32(PPCond[i].first - off);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000395 uint32_t x = PPCond[i].second;
396 assert(x != 0 && "PPCond entry not backpatched.");
397 // Emit zero for #endifs. This allows us to do checking when
398 // we read the PTH file back in.
Ted Kremenekb978c662009-01-08 01:17:37 +0000399 Emit32(x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000400 }
401
Ted Kremenekbe295332009-01-08 02:44:06 +0000402 return PCHEntry(off, PPCondOff, Spellings.take());
403}
404
405void PTHWriter::EmitCachedSpellings() {
406 // Write each cached string to the PTH file and update the
407 // the string map entry to contain the relevant offset.
408 //
409 // FIXME: We can write the strings out in order of their frequency. This
410 // may result in better locality.
411 //
412 for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end();
413 I!=E; ++I) {
414
415 Offset off = Out.tell();
416
417 // Write out the length of the string before the string itself.
418 unsigned len = I->getKeyLength();
Ted Kremenekf89f7f92009-01-08 23:40:50 +0000419
420 // Adjust the length we write in the PTH file to accomodate for numeric
421 // literals. We implicitly have a space after them, but only want to later
422 // read the characters that are just part of the literal itself.
423 Emit16(I->getValue().second ? len - 1 : len);
Ted Kremenekbe295332009-01-08 02:44:06 +0000424
425 // Write out the string data.
426 const char* data = I->getKeyData();
427 EmitBuf(data, data+len);
428
429 // Now patch the offset of the string in the PTH file into the string map.
Ted Kremenekf89f7f92009-01-08 23:40:50 +0000430 I->getValue().first = off;
Ted Kremenekbe295332009-01-08 02:44:06 +0000431 }
432
433 // Now emit the spelling tables.
434 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
435 SpellMapTy& spellings = I->second.getSpellings();
436 I->second.setSpellingTableOffset(Out.tell());
437
438 // Write out the number of spellings.
439 unsigned n = spellings.size();
440 Emit32(n);
441
442 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekbe295332009-01-08 02:44:06 +0000443 // Write out the offset of the token within the source file.
444 Emit32(spellings[i].first);
445
446 // Write out the offset of the spelling data within the PTH file.
Ted Kremenekf89f7f92009-01-08 23:40:50 +0000447 Emit32(spellings[i].second->getValue().first);
Ted Kremenekbe295332009-01-08 02:44:06 +0000448 }
449
450 // Delete the spelling map for this source file.
451 delete &spellings;
452 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000453}
Ted Kremenek85888962008-10-21 00:54:44 +0000454
Ted Kremenekb978c662009-01-08 01:17:37 +0000455void PTHWriter::GeneratePTH() {
Ted Kremenek85888962008-10-21 00:54:44 +0000456 // Iterate over all the files in SourceManager. Create a lexer
457 // for each file and cache the tokens.
458 SourceManager& SM = PP.getSourceManager();
459 const LangOptions& LOpts = PP.getLangOptions();
Ted Kremenek85888962008-10-21 00:54:44 +0000460
461 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
462 I!=E; ++I) {
463
464 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000465 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000466
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000467 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
468 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000469
470 // FIXME: Handle files with non-absolute paths.
471 llvm::sys::Path P(FE->getName());
472 if (!P.isAbsolute())
473 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000474
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000475 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
476 if (PI != PM.end()) continue;
477
Chris Lattner1b230142009-01-06 04:47:20 +0000478 const llvm::MemoryBuffer* B = C->getBuffer();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000479 if (!B) continue;
480
Ted Kremenek85888962008-10-21 00:54:44 +0000481 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
482 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000483
Ted Kremenekb978c662009-01-08 01:17:37 +0000484 PM[FE] = LexTokens(L);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000485 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000486
487 // Write out the identifier table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000488 std::pair<Offset,Offset> IdTableOff = EmitIdentifierTable();
Ted Kremenek85888962008-10-21 00:54:44 +0000489
Ted Kremenekbe295332009-01-08 02:44:06 +0000490 // Write out the cached strings table.
491 EmitCachedSpellings();
492
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000493 // Write out the file table.
Ted Kremenekb978c662009-01-08 01:17:37 +0000494 Offset FileTableOff = EmitFileTable();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000495
496 // Finally, write out the offset table at the end.
Ted Kremenekb978c662009-01-08 01:17:37 +0000497 Emit32(IdTableOff.first);
498 Emit32(IdTableOff.second);
499 Emit32(FileTableOff);
500}
501
502void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
503 // Lex through the entire file. This will populate SourceManager with
504 // all of the header information.
505 Token Tok;
506 PP.EnterMainSourceFile();
507 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
508
509 // Open up the PTH file.
510 std::string ErrMsg;
511 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
512
513 if (!ErrMsg.empty()) {
514 llvm::errs() << "PTH error: " << ErrMsg << "\n";
515 return;
516 }
517
518 // Create the PTHWriter and generate the PTH file.
519 PTHWriter PW(Out, PP);
520 PW.GeneratePTH();
Ted Kremenek85888962008-10-21 00:54:44 +0000521}