blob: f12f3526d3fe70f571fc45279abf467c8bcbeadd [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 Kremeneka3d764c2008-11-26 03:36:26 +0000162
163 Token Tok;
164
165 do {
166 L.LexFromRawLexer(Tok);
167
168 if (Tok.is(tok::identifier)) {
169 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
170 }
171 else if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
172 // Special processing for #include. Store the '#' token and lex
173 // the next token.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000174 Offset HashOff = (Offset) Out.tell();
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000175 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremenekdad7b342008-12-12 18:31:09 +0000176
177 // Get the next token.
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000178 L.LexFromRawLexer(Tok);
179
180 // Did we see 'include'/'import'/'include_next'?
181 if (!Tok.is(tok::identifier))
182 continue;
183
184 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
185 Tok.setIdentifierInfo(II);
186 tok::PPKeywordKind K = II->getPPKeywordID();
187
188 if (K == tok::pp_include || K == tok::pp_import ||
189 K == tok::pp_include_next) {
190
191 // Save the 'include' token.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000192 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000193
194 // Lex the next token as an include string.
195 L.setParsingPreprocessorDirective(true);
196 L.LexIncludeFilename(Tok);
197 L.setParsingPreprocessorDirective(false);
198
199 if (Tok.is(tok::identifier))
200 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
201 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000202 else if (K == tok::pp_if || K == tok::pp_ifdef || K == tok::pp_ifndef) {
203 // Ad an entry for '#if' and friends. We initially set the target index
204 // to 0. This will get backpatched when we hit #endif.
205 PPStartCond.push_back(PPCond.size());
Ted Kremenekdad7b342008-12-12 18:31:09 +0000206 PPCond.push_back(std::make_pair(HashOff, 0U));
Ted Kremenekfb645b62008-12-11 23:36:38 +0000207 }
208 else if (K == tok::pp_endif) {
Ted Kremenekfb645b62008-12-11 23:36:38 +0000209 // Add an entry for '#endif'. We set the target table index to itself.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000210 // This will later be set to zero when emitting to the PTH file. We
211 // use 0 for uninitialized indices because that is easier to debug.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000212 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000213 // Backpatch the opening '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000214 assert(!PPStartCond.empty());
215 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000216 assert(PPCond[PPStartCond.back()].second == 0);
217 PPCond[PPStartCond.back()].second = index;
218 PPStartCond.pop_back();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000219 // Add the new entry to PPCond.
220 PPCond.push_back(std::make_pair(HashOff, index));
Ted Kremenekfb645b62008-12-11 23:36:38 +0000221 }
Ted Kremenekdad7b342008-12-12 18:31:09 +0000222 else if (K == tok::pp_elif || K == tok::pp_else) {
223 // Add an entry for '#elif' or '#else.
224 // This serves as both a closing and opening of a conditional block.
225 // This means that its entry will get backpatched later.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000226 unsigned index = PPCond.size();
Ted Kremenekfb645b62008-12-11 23:36:38 +0000227 // Backpatch the previous '#if' entry.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000228 assert(!PPStartCond.empty());
229 assert(PPCond.size() > PPStartCond.back());
Ted Kremenekfb645b62008-12-11 23:36:38 +0000230 assert(PPCond[PPStartCond.back()].second == 0);
231 PPCond[PPStartCond.back()].second = index;
232 PPStartCond.pop_back();
233 // Now add '#elif' as a new block opening.
Ted Kremenekdad7b342008-12-12 18:31:09 +0000234 PPCond.push_back(std::make_pair(HashOff, 0U));
235 PPStartCond.push_back(index);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000236 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000237 }
238 }
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000239 while (EmitToken(Out, Tok, SMgr, idcount, IM), Tok.isNot(tok::eof));
Ted Kremenekfb645b62008-12-11 23:36:38 +0000240
Ted Kremenekdad7b342008-12-12 18:31:09 +0000241 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
242
Ted Kremenekfb645b62008-12-11 23:36:38 +0000243 // Next write out PPCond.
244 Offset PPCondOff = (Offset) Out.tell();
Ted Kremenekdad7b342008-12-12 18:31:09 +0000245
246 // Write out the size of PPCond so that clients can identifer empty tables.
Ted Kremenekfb645b62008-12-11 23:36:38 +0000247 Emit32(Out, PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000248
Ted Kremenekdad7b342008-12-12 18:31:09 +0000249 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
250 Emit32(Out, PPCond[i].first - off);
251 uint32_t x = PPCond[i].second;
252 assert(x != 0 && "PPCond entry not backpatched.");
253 // Emit zero for #endifs. This allows us to do checking when
254 // we read the PTH file back in.
255 Emit32(Out, x == i ? 0 : x);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000256 }
257
258 return std::make_pair(off,PPCondOff);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000259}
Ted Kremenek85888962008-10-21 00:54:44 +0000260
261void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
262 // Lex through the entire file. This will populate SourceManager with
263 // all of the header information.
264 Token Tok;
265 PP.EnterMainSourceFile();
266 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
267
268 // Iterate over all the files in SourceManager. Create a lexer
269 // for each file and cache the tokens.
270 SourceManager& SM = PP.getSourceManager();
271 const LangOptions& LOpts = PP.getLangOptions();
272 llvm::raw_ostream& os = llvm::errs();
273
274 PCHMap PM;
275 IDMap IM;
Ted Kremenek85888962008-10-21 00:54:44 +0000276 uint32_t idcount = 0;
277
278 std::string ErrMsg;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000279 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
Ted Kremenek85888962008-10-21 00:54:44 +0000280
281 if (!ErrMsg.empty()) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000282 os << "PTH error: " << ErrMsg << "\n";
Ted Kremenek85888962008-10-21 00:54:44 +0000283 return;
284 }
285
286 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
287 I!=E; ++I) {
288
289 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000290 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000291
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000292 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
293 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000294
295 // FIXME: Handle files with non-absolute paths.
296 llvm::sys::Path P(FE->getName());
297 if (!P.isAbsolute())
298 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000299
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000300 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
301 if (PI != PM.end()) continue;
302
303 const llvm::MemoryBuffer* B = C->Buffer;
304 if (!B) continue;
305
Ted Kremenek85888962008-10-21 00:54:44 +0000306 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
307 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000308
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000309 PM[FE] = LexTokens(Out, L, PP, idcount, IM);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000310 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000311
312 // Write out the identifier table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000313 std::pair<Offset,Offset> IdTableOff =
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000314 EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM);
Ted Kremenek85888962008-10-21 00:54:44 +0000315
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000316 // Write out the file table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000317 Offset FileTableOff = EmitFileTable(Out, SM, PM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000318
319 // Finally, write out the offset table at the end.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000320 Emit32(Out, IdTableOff.first);
321 Emit32(Out, IdTableOff.second);
322 Emit32(Out, FileTableOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000323}