blob: c97b2259d52d5387e58d1933cdf34dab52981b23 [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"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000024#include "llvm/System/Path.h"
Ted Kremenek85888962008-10-21 00:54:44 +000025
26using namespace clang;
27
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000028typedef uint32_t Offset;
29
Ted Kremenekfb645b62008-12-11 23:36:38 +000030typedef llvm::DenseMap<const FileEntry*,std::pair<Offset,Offset> > PCHMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000031typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
Ted Kremenek85888962008-10-21 00:54:44 +000032
Ted Kremenek8f174e12008-12-23 02:52:12 +000033static void Emit8(llvm::raw_ostream& Out, uint32_t V) {
34 Out << (unsigned char)(V);
35}
36
Ted Kremenek85888962008-10-21 00:54:44 +000037static void Emit32(llvm::raw_ostream& Out, uint32_t V) {
38 Out << (unsigned char)(V);
39 Out << (unsigned char)(V >> 8);
40 Out << (unsigned char)(V >> 16);
41 Out << (unsigned char)(V >> 24);
42}
43
Ted Kremenek8f174e12008-12-23 02:52:12 +000044static void Emit16(llvm::raw_ostream& Out, uint32_t V) {
Ted Kremenek85888962008-10-21 00:54:44 +000045 Out << (unsigned char)(V);
Ted Kremenek8f174e12008-12-23 02:52:12 +000046 Out << (unsigned char)(V >> 8);
47 assert((V >> 16) == 0);
Ted Kremenek85888962008-10-21 00:54:44 +000048}
49
50static void EmitBuf(llvm::raw_ostream& Out, const char* I, const char* E) {
51 for ( ; I != E ; ++I) Out << *I;
52}
53
54static uint32_t ResolveID(IDMap& IM, uint32_t& idx, const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000055
56 // Null IdentifierInfo's map to the persistent ID 0.
57 if (!II)
58 return 0;
59
Ted Kremenek85888962008-10-21 00:54:44 +000060 IDMap::iterator I = IM.find(II);
61
62 if (I == IM.end()) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000063 IM[II] = ++idx; // Pre-increment since '0' is reserved for NULL.
64 return idx;
Ted Kremenek85888962008-10-21 00:54:44 +000065 }
66
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000067 return I->second; // We've already added 1.
Ted Kremenek85888962008-10-21 00:54:44 +000068}
69
70static void EmitToken(llvm::raw_ostream& Out, const Token& T,
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000071 const SourceManager& SMgr,
Ted Kremenek85888962008-10-21 00:54:44 +000072 uint32_t& idcount, IDMap& IM) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000073
Ted Kremenek85888962008-10-21 00:54:44 +000074 Emit8(Out, T.getKind());
75 Emit8(Out, T.getFlags());
76 Emit32(Out, ResolveID(IM, idcount, T.getIdentifierInfo()));
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000077 Emit32(Out, SMgr.getFullFilePos(T.getLocation()));
Ted Kremenek8f174e12008-12-23 02:52:12 +000078 Emit16(Out, T.getLength());
Ted Kremenek85888962008-10-21 00:54:44 +000079}
80
Ted Kremenekfa59aad2008-11-26 23:58:26 +000081struct IDData {
82 const IdentifierInfo* II;
83 uint32_t FileOffset;
84 const IdentifierTable::const_iterator::value_type* Str;
85};
Ted Kremeneka3d764c2008-11-26 03:36:26 +000086
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000087static std::pair<Offset,Offset>
Ted Kremenekfa59aad2008-11-26 23:58:26 +000088EmitIdentifierTable(llvm::raw_fd_ostream& Out, uint32_t max,
89 const IdentifierTable& T, const IDMap& IM) {
90
91 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000092 typedef std::vector<IDData> InverseIDMap;
Ted Kremenekfa59aad2008-11-26 23:58:26 +000093 InverseIDMap IIDMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000094 IIDMap.resize(max);
Ted Kremenek85888962008-10-21 00:54:44 +000095
Ted Kremenekfa59aad2008-11-26 23:58:26 +000096 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000097 for (IDMap::const_iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
98 // Decrement by 1 because we are using a vector for the lookup and
99 // 0 is reserved for NULL.
100 assert(I->second > 0);
101 assert(I->second-1 < IIDMap.size());
102 IIDMap[I->second-1].II = I->first;
103 }
Ted Kremenek85888962008-10-21 00:54:44 +0000104
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000105 // Get the string data associated with the IdentifierInfo.
106 for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
107 IDMap::const_iterator IDI = IM.find(&(I->getValue()));
108 if (IDI == IM.end()) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000109 IIDMap[IDI->second-1].Str = &(*I);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000110 }
111
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000112 Offset DataOff = Out.tell();
Ted Kremenek6183e482008-12-03 01:16:39 +0000113
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000114 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000115 // Record the location for this data.
116 I->FileOffset = Out.tell();
117 // Write out the keyword.
118 unsigned len = I->Str->getKeyLength();
Ted Kremenek85888962008-10-21 00:54:44 +0000119 Emit32(Out, len);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000120 const char* buf = I->Str->getKeyData();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000121 EmitBuf(Out, buf, buf+len);
Ted Kremenek85888962008-10-21 00:54:44 +0000122 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000123
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000124 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000125 Offset IDOff = Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000126
Ted Kremenek6183e482008-12-03 01:16:39 +0000127 // Emit the number of identifiers.
128 Emit32(Out, max);
129
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000130 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000131 Emit32(Out, I->FileOffset);
132
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000133 return std::make_pair(DataOff, IDOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000134}
135
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000136Offset EmitFileTable(llvm::raw_fd_ostream& Out, SourceManager& SM, PCHMap& PM) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000137
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000138 Offset off = (Offset) Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000139
140 // Output the size of the table.
141 Emit32(Out, PM.size());
142
143 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000144 const FileEntry* FE = I->first;
Ted Kremenek8dffd9b2008-12-04 22:36:44 +0000145 const char* Name = FE->getName();
146 unsigned size = strlen(Name);
147 Emit32(Out, size);
148 EmitBuf(Out, Name, Name+size);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000149 Emit32(Out, I->second.first);
150 Emit32(Out, I->second.second);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000151 }
152
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000153 return off;
154}
155
Ted Kremenekfb645b62008-12-11 23:36:38 +0000156static std::pair<Offset,Offset>
157LexTokens(llvm::raw_fd_ostream& Out, Lexer& L, Preprocessor& PP,
158 uint32_t& idcount, IDMap& IM) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000159
160 // Record the location within the token file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000161 Offset off = (Offset) Out.tell();
162 SourceManager& SMgr = PP.getSourceManager();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000163
164 // Keep track of matching '#if' ... '#endif'.
165 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
166 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000167 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000168 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000169
170 Token Tok;
171
172 do {
173 L.LexFromRawLexer(Tok);
174
Ted Kremeneke5680f32008-12-23 01:30:52 +0000175 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
176 ParsingPreprocessorDirective) {
177 // Insert an eom token into the token cache. It has the same
178 // position as the next token that is not on the same line as the
179 // preprocessor directive. Observe that we continue processing
180 // 'Tok' when we exit this branch.
181 Token Tmp = Tok;
182 Tmp.setKind(tok::eom);
183 Tmp.clearFlag(Token::StartOfLine);
184 Tmp.setIdentifierInfo(0);
185 EmitToken(Out, Tmp, SMgr, idcount, IM);
186 ParsingPreprocessorDirective = false;
187 }
188
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000189 if (Tok.is(tok::identifier)) {
190 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000191 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000192 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000193
194 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000195 // Special processing for #include. Store the '#' token and lex
196 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000197 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000198 Offset HashOff = (Offset) Out.tell();
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000199 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000200
201 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000202 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000203
204 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000205
206 // Did we see 'include'/'import'/'include_next'?
207 if (!Tok.is(tok::identifier))
208 continue;
209
210 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
211 Tok.setIdentifierInfo(II);
212 tok::PPKeywordKind K = II->getPPKeywordID();
213
Ted Kremeneke5680f32008-12-23 01:30:52 +0000214 assert(K != tok::pp_not_keyword);
215 ParsingPreprocessorDirective = true;
216
217 switch (K) {
218 default:
219 break;
220 case tok::pp_include:
221 case tok::pp_import:
222 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000223 // Save the 'include' token.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000224 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000225 // Lex the next token as an include string.
226 L.setParsingPreprocessorDirective(true);
227 L.LexIncludeFilename(Tok);
228 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000229 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000230 if (Tok.is(tok::identifier))
231 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000232
233 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000234 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000235 case tok::pp_if:
236 case tok::pp_ifdef:
237 case tok::pp_ifndef: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000238 // Ad an entry for '#if' and friends. We initially set the target index
239 // to 0. This will get backpatched when we hit #endif.
240 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000241 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000242 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000243 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000244 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000245 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000246 // This will later be set to zero when emitting to the PTH file. We
247 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000248 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000249 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000250 assert(!PPStartCond.empty());
251 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000252 assert(PPCond[PPStartCond.back()].second == 0);
253 PPCond[PPStartCond.back()].second = index;
254 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000255 // Add the new entry to PPCond.
256 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000257 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000258 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000259 case tok::pp_elif:
260 case tok::pp_else: {
261 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000262 // This serves as both a closing and opening of a conditional block.
263 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000264 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000265 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000266 assert(!PPStartCond.empty());
267 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000268 assert(PPCond[PPStartCond.back()].second == 0);
269 PPCond[PPStartCond.back()].second = index;
270 PPStartCond.pop_back();
271 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000272 PPCond.push_back(std::make_pair(HashOff, 0U));
273 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000274 break;
275 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000276 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000277 }
278 }
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000279 while (EmitToken(Out, Tok, SMgr, idcount, IM), Tok.isNot(tok::eof));
Ted Kremenekfb645b62008-12-11 23:36:38 +0000280
Ted Kremenekdad7b342008-12-12 18:31:09 +0000281 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
282
Ted Kremenekfb645b62008-12-11 23:36:38 +0000283 // Next write out PPCond.
284 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000285
286 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000287 Emit32(Out, PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000288
Ted Kremenekdad7b342008-12-12 18:31:09 +0000289 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
290 Emit32(Out, PPCond[i].first - off);
291 uint32_t x = PPCond[i].second;
292 assert(x != 0 && "PPCond entry not backpatched.");
293 // Emit zero for #endifs. This allows us to do checking when
294 // we read the PTH file back in.
295 Emit32(Out, x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000296 }
297
298 return std::make_pair(off,PPCondOff);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000299}
Ted Kremenek85888962008-10-21 00:54:44 +0000300
301void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
302 // Lex through the entire file. This will populate SourceManager with
303 // all of the header information.
304 Token Tok;
305 PP.EnterMainSourceFile();
306 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
307
308 // Iterate over all the files in SourceManager. Create a lexer
309 // for each file and cache the tokens.
310 SourceManager& SM = PP.getSourceManager();
311 const LangOptions& LOpts = PP.getLangOptions();
312 llvm::raw_ostream& os = llvm::errs();
313
314 PCHMap PM;
315 IDMap IM;
Ted Kremenek85888962008-10-21 00:54:44 +0000316 uint32_t idcount = 0;
317
318 std::string ErrMsg;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000319 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
Ted Kremenek85888962008-10-21 00:54:44 +0000320
321 if (!ErrMsg.empty()) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000322 os << "PTH error: " << ErrMsg << "\n";
Ted Kremenek85888962008-10-21 00:54:44 +0000323 return;
324 }
325
326 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
327 I!=E; ++I) {
328
329 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000330 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000331
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000332 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
333 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000334
335 // FIXME: Handle files with non-absolute paths.
336 llvm::sys::Path P(FE->getName());
337 if (!P.isAbsolute())
338 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000339
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000340 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
341 if (PI != PM.end()) continue;
342
343 const llvm::MemoryBuffer* B = C->Buffer;
344 if (!B) continue;
345
Ted Kremenek85888962008-10-21 00:54:44 +0000346 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
347 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000348
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000349 PM[FE] = LexTokens(Out, L, PP, idcount, IM);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000350 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000351
352 // Write out the identifier table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000353 std::pair<Offset,Offset> IdTableOff =
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000354 EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM);
Ted Kremenek85888962008-10-21 00:54:44 +0000355
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000356 // Write out the file table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000357 Offset FileTableOff = EmitFileTable(Out, SM, PM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000358
359 // Finally, write out the offset table at the end.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000360 Emit32(Out, IdTableOff.first);
361 Emit32(Out, IdTableOff.second);
362 Emit32(Out, FileTableOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000363}