blob: d8c92497b6bd3525d7eed9b744b45d043f9942b9 [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
33static void Emit32(llvm::raw_ostream& Out, uint32_t V) {
34 Out << (unsigned char)(V);
35 Out << (unsigned char)(V >> 8);
36 Out << (unsigned char)(V >> 16);
37 Out << (unsigned char)(V >> 24);
38}
39
40static void Emit8(llvm::raw_ostream& Out, uint32_t V) {
41 Out << (unsigned char)(V);
42}
43
44static void EmitBuf(llvm::raw_ostream& Out, const char* I, const char* E) {
45 for ( ; I != E ; ++I) Out << *I;
46}
47
48static uint32_t ResolveID(IDMap& IM, uint32_t& idx, const IdentifierInfo* II) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000049
50 // Null IdentifierInfo's map to the persistent ID 0.
51 if (!II)
52 return 0;
53
Ted Kremenek85888962008-10-21 00:54:44 +000054 IDMap::iterator I = IM.find(II);
55
56 if (I == IM.end()) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000057 IM[II] = ++idx; // Pre-increment since '0' is reserved for NULL.
58 return idx;
Ted Kremenek85888962008-10-21 00:54:44 +000059 }
60
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000061 return I->second; // We've already added 1.
Ted Kremenek85888962008-10-21 00:54:44 +000062}
63
64static void EmitToken(llvm::raw_ostream& Out, const Token& T,
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000065 const SourceManager& SMgr,
Ted Kremenek85888962008-10-21 00:54:44 +000066 uint32_t& idcount, IDMap& IM) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000067
Ted Kremenek85888962008-10-21 00:54:44 +000068 Emit8(Out, T.getKind());
69 Emit8(Out, T.getFlags());
70 Emit32(Out, ResolveID(IM, idcount, T.getIdentifierInfo()));
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000071 Emit32(Out, SMgr.getFullFilePos(T.getLocation()));
Ted Kremenek85888962008-10-21 00:54:44 +000072 Emit32(Out, T.getLength());
73}
74
Ted Kremenekfa59aad2008-11-26 23:58:26 +000075struct IDData {
76 const IdentifierInfo* II;
77 uint32_t FileOffset;
78 const IdentifierTable::const_iterator::value_type* Str;
79};
Ted Kremeneka3d764c2008-11-26 03:36:26 +000080
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000081static std::pair<Offset,Offset>
Ted Kremenekfa59aad2008-11-26 23:58:26 +000082EmitIdentifierTable(llvm::raw_fd_ostream& Out, uint32_t max,
83 const IdentifierTable& T, const IDMap& IM) {
84
85 // Build an inverse map from persistent IDs -> IdentifierInfo*.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000086 typedef std::vector<IDData> InverseIDMap;
Ted Kremenekfa59aad2008-11-26 23:58:26 +000087 InverseIDMap IIDMap;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000088 IIDMap.resize(max);
Ted Kremenek85888962008-10-21 00:54:44 +000089
Ted Kremenekfa59aad2008-11-26 23:58:26 +000090 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +000091 for (IDMap::const_iterator I=IM.begin(), E=IM.end(); I!=E; ++I) {
92 // Decrement by 1 because we are using a vector for the lookup and
93 // 0 is reserved for NULL.
94 assert(I->second > 0);
95 assert(I->second-1 < IIDMap.size());
96 IIDMap[I->second-1].II = I->first;
97 }
Ted Kremenek85888962008-10-21 00:54:44 +000098
Ted Kremenekfa59aad2008-11-26 23:58:26 +000099 // Get the string data associated with the IdentifierInfo.
100 for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
101 IDMap::const_iterator IDI = IM.find(&(I->getValue()));
102 if (IDI == IM.end()) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000103 IIDMap[IDI->second-1].Str = &(*I);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000104 }
105
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000106 Offset DataOff = Out.tell();
Ted Kremenek6183e482008-12-03 01:16:39 +0000107
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000108 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000109 // Record the location for this data.
110 I->FileOffset = Out.tell();
111 // Write out the keyword.
112 unsigned len = I->Str->getKeyLength();
Ted Kremenek85888962008-10-21 00:54:44 +0000113 Emit32(Out, len);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000114 const char* buf = I->Str->getKeyData();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000115 EmitBuf(Out, buf, buf+len);
Ted Kremenek85888962008-10-21 00:54:44 +0000116 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000117
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000118 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000119 Offset IDOff = Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000120
Ted Kremenek6183e482008-12-03 01:16:39 +0000121 // Emit the number of identifiers.
122 Emit32(Out, max);
123
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000124 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000125 Emit32(Out, I->FileOffset);
126
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000127 return std::make_pair(DataOff, IDOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000128}
129
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000130Offset EmitFileTable(llvm::raw_fd_ostream& Out, SourceManager& SM, PCHMap& PM) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000131
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000132 Offset off = (Offset) Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000133
134 // Output the size of the table.
135 Emit32(Out, PM.size());
136
137 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000138 const FileEntry* FE = I->first;
Ted Kremenek8dffd9b2008-12-04 22:36:44 +0000139 const char* Name = FE->getName();
140 unsigned size = strlen(Name);
141 Emit32(Out, size);
142 EmitBuf(Out, Name, Name+size);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000143 Emit32(Out, I->second.first);
144 Emit32(Out, I->second.second);
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000145 }
146
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000147 return off;
148}
149
Ted Kremenekfb645b62008-12-11 23:36:38 +0000150static std::pair<Offset,Offset>
151LexTokens(llvm::raw_fd_ostream& Out, Lexer& L, Preprocessor& PP,
152 uint32_t& idcount, IDMap& IM) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000153
154 // Record the location within the token file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000155 Offset off = (Offset) Out.tell();
156 SourceManager& SMgr = PP.getSourceManager();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000157
158 // Keep track of matching '#if' ... '#endif'.
159 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
160 PPCondTable PPCond;
Ted Kremenekdad7b342008-12-12 18:31:09 +0000161 std::vector<unsigned> PPStartCond;
Ted Kremeneke5680f32008-12-23 01:30:52 +0000162 bool ParsingPreprocessorDirective = false;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000163
164 Token Tok;
165
166 do {
167 L.LexFromRawLexer(Tok);
168
Ted Kremeneke5680f32008-12-23 01:30:52 +0000169 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
170 ParsingPreprocessorDirective) {
171 // Insert an eom token into the token cache. It has the same
172 // position as the next token that is not on the same line as the
173 // preprocessor directive. Observe that we continue processing
174 // 'Tok' when we exit this branch.
175 Token Tmp = Tok;
176 Tmp.setKind(tok::eom);
177 Tmp.clearFlag(Token::StartOfLine);
178 Tmp.setIdentifierInfo(0);
179 EmitToken(Out, Tmp, SMgr, idcount, IM);
180 ParsingPreprocessorDirective = false;
181 }
182
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000183 if (Tok.is(tok::identifier)) {
184 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000185 continue;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000186 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000187
188 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000189 // Special processing for #include. Store the '#' token and lex
190 // the next token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000191 assert(!ParsingPreprocessorDirective);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000192 Offset HashOff = (Offset) Out.tell();
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000193 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000194
195 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000196 L.LexFromRawLexer(Tok);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000197
198 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000199
200 // Did we see 'include'/'import'/'include_next'?
201 if (!Tok.is(tok::identifier))
202 continue;
203
204 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
205 Tok.setIdentifierInfo(II);
206 tok::PPKeywordKind K = II->getPPKeywordID();
207
Ted Kremeneke5680f32008-12-23 01:30:52 +0000208 assert(K != tok::pp_not_keyword);
209 ParsingPreprocessorDirective = true;
210
211 switch (K) {
212 default:
213 break;
214 case tok::pp_include:
215 case tok::pp_import:
216 case tok::pp_include_next: {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000217 // Save the 'include' token.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000218 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000219 // Lex the next token as an include string.
220 L.setParsingPreprocessorDirective(true);
221 L.LexIncludeFilename(Tok);
222 L.setParsingPreprocessorDirective(false);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000223 assert(!Tok.isAtStartOfLine());
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000224 if (Tok.is(tok::identifier))
225 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000226
227 break;
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000228 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000229 case tok::pp_if:
230 case tok::pp_ifdef:
231 case tok::pp_ifndef: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000232 // Ad an entry for '#if' and friends. We initially set the target index
233 // to 0. This will get backpatched when we hit #endif.
234 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000235 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000236 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000237 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000238 case tok::pp_endif: {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000239 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000240 // This will later be set to zero when emitting to the PTH file. We
241 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000242 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000243 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000244 assert(!PPStartCond.empty());
245 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000246 assert(PPCond[PPStartCond.back()].second == 0);
247 PPCond[PPStartCond.back()].second = index;
248 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000249 // Add the new entry to PPCond.
250 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremeneke5680f32008-12-23 01:30:52 +0000251 break;
Ted Kremenekfb645b62008-12-11 23:36:38 +0000252 }
Ted Kremeneke5680f32008-12-23 01:30:52 +0000253 case tok::pp_elif:
254 case tok::pp_else: {
255 // Add an entry for #elif or #else.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000256 // This serves as both a closing and opening of a conditional block.
257 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000258 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000259 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000260 assert(!PPStartCond.empty());
261 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000262 assert(PPCond[PPStartCond.back()].second == 0);
263 PPCond[PPStartCond.back()].second = index;
264 PPStartCond.pop_back();
265 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000266 PPCond.push_back(std::make_pair(HashOff, 0U));
267 PPStartCond.push_back(index);
Ted Kremeneke5680f32008-12-23 01:30:52 +0000268 break;
269 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000270 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000271 }
272 }
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000273 while (EmitToken(Out, Tok, SMgr, idcount, IM), Tok.isNot(tok::eof));
Ted Kremenekfb645b62008-12-11 23:36:38 +0000274
Ted Kremenekdad7b342008-12-12 18:31:09 +0000275 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
276
Ted Kremenekfb645b62008-12-11 23:36:38 +0000277 // Next write out PPCond.
278 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000279
280 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000281 Emit32(Out, PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000282
Ted Kremenekdad7b342008-12-12 18:31:09 +0000283 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
284 Emit32(Out, PPCond[i].first - off);
285 uint32_t x = PPCond[i].second;
286 assert(x != 0 && "PPCond entry not backpatched.");
287 // Emit zero for #endifs. This allows us to do checking when
288 // we read the PTH file back in.
289 Emit32(Out, x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000290 }
291
292 return std::make_pair(off,PPCondOff);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000293}
Ted Kremenek85888962008-10-21 00:54:44 +0000294
295void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
296 // Lex through the entire file. This will populate SourceManager with
297 // all of the header information.
298 Token Tok;
299 PP.EnterMainSourceFile();
300 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
301
302 // Iterate over all the files in SourceManager. Create a lexer
303 // for each file and cache the tokens.
304 SourceManager& SM = PP.getSourceManager();
305 const LangOptions& LOpts = PP.getLangOptions();
306 llvm::raw_ostream& os = llvm::errs();
307
308 PCHMap PM;
309 IDMap IM;
Ted Kremenek85888962008-10-21 00:54:44 +0000310 uint32_t idcount = 0;
311
312 std::string ErrMsg;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000313 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
Ted Kremenek85888962008-10-21 00:54:44 +0000314
315 if (!ErrMsg.empty()) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000316 os << "PTH error: " << ErrMsg << "\n";
Ted Kremenek85888962008-10-21 00:54:44 +0000317 return;
318 }
319
320 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
321 I!=E; ++I) {
322
323 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000324 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000325
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000326 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
327 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000328
329 // FIXME: Handle files with non-absolute paths.
330 llvm::sys::Path P(FE->getName());
331 if (!P.isAbsolute())
332 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000333
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000334 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
335 if (PI != PM.end()) continue;
336
337 const llvm::MemoryBuffer* B = C->Buffer;
338 if (!B) continue;
339
Ted Kremenek85888962008-10-21 00:54:44 +0000340 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
341 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000342
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000343 PM[FE] = LexTokens(Out, L, PP, idcount, IM);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000344 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000345
346 // Write out the identifier table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000347 std::pair<Offset,Offset> IdTableOff =
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000348 EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM);
Ted Kremenek85888962008-10-21 00:54:44 +0000349
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000350 // Write out the file table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000351 Offset FileTableOff = EmitFileTable(Out, SM, PM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000352
353 // Finally, write out the offset table at the end.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000354 Emit32(Out, IdTableOff.first);
355 Emit32(Out, IdTableOff.second);
356 Emit32(Out, FileTableOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000357}