blob: 21e5ee09aed4bb9ef9270daaa91d7d29c22aa67c [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;
161 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 Kremeneka3d764c2008-11-26 03:36:26 +0000176 L.LexFromRawLexer(Tok);
177
178 // Did we see 'include'/'import'/'include_next'?
179 if (!Tok.is(tok::identifier))
180 continue;
181
182 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
183 Tok.setIdentifierInfo(II);
184 tok::PPKeywordKind K = II->getPPKeywordID();
185
186 if (K == tok::pp_include || K == tok::pp_import ||
187 K == tok::pp_include_next) {
188
189 // Save the 'include' token.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000190 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000191
192 // Lex the next token as an include string.
193 L.setParsingPreprocessorDirective(true);
194 L.LexIncludeFilename(Tok);
195 L.setParsingPreprocessorDirective(false);
196
197 if (Tok.is(tok::identifier))
198 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
199 }
Ted Kremenekfb645b62008-12-11 23:36:38 +0000200 else if (K == tok::pp_if || K == tok::pp_ifdef || K == tok::pp_ifndef) {
201 // Ad an entry for '#if' and friends. We initially set the target index
202 // to 0. This will get backpatched when we hit #endif.
203 PPStartCond.push_back(PPCond.size());
204 PPCond.push_back(std::make_pair((Offset) HashOff, 0U));
205 }
206 else if (K == tok::pp_endif) {
207 assert(!PPStartCond.empty());
208 // Add an entry for '#endif'. We set the target table index to itself.
209 unsigned index = PPCond.size();
210 PPCond.push_back(std::make_pair((Offset) HashOff, index));
211 // Backpatch the opening '#if' entry.
212 assert(PPCond[PPStartCond.back()].second == 0);
213 PPCond[PPStartCond.back()].second = index;
214 PPStartCond.pop_back();
215 }
216 else if (K == tok::pp_elif) {
217 assert(!PPStartCond.empty());
218 // Add an entry for '#elif'. This serves as both a closing and
219 // opening of a conditional block. This means that its entry
220 // will get backpatched later.
221 unsigned index = PPCond.size();
222 PPCond.push_back(std::make_pair((Offset) HashOff, 0U));
223 // Backpatch the previous '#if' entry.
224 assert(PPCond[PPStartCond.back()].second == 0);
225 PPCond[PPStartCond.back()].second = index;
226 PPStartCond.pop_back();
227 // Now add '#elif' as a new block opening.
228 PPStartCond.push_back(index);
229 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000230 }
231 }
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000232 while (EmitToken(Out, Tok, SMgr, idcount, IM), Tok.isNot(tok::eof));
Ted Kremenekfb645b62008-12-11 23:36:38 +0000233
234 // Next write out PPCond.
235 Offset PPCondOff = (Offset) Out.tell();
236 // Write out the size of PPCond so that clients can tell if the table is
237 // empty.
238 Emit32(Out, PPCond.size());
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000239
Ted Kremenekfb645b62008-12-11 23:36:38 +0000240 for (PPCondTable::iterator I=PPCond.begin(), E=PPCond.end(); I!=E; ++I) {
241 Emit32(Out, I->first - off);
242 Emit32(Out, I->second);
243 }
244
245 return std::make_pair(off,PPCondOff);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000246}
Ted Kremenek85888962008-10-21 00:54:44 +0000247
248void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
249 // Lex through the entire file. This will populate SourceManager with
250 // all of the header information.
251 Token Tok;
252 PP.EnterMainSourceFile();
253 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
254
255 // Iterate over all the files in SourceManager. Create a lexer
256 // for each file and cache the tokens.
257 SourceManager& SM = PP.getSourceManager();
258 const LangOptions& LOpts = PP.getLangOptions();
259 llvm::raw_ostream& os = llvm::errs();
260
261 PCHMap PM;
262 IDMap IM;
Ted Kremenek85888962008-10-21 00:54:44 +0000263 uint32_t idcount = 0;
264
265 std::string ErrMsg;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000266 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
Ted Kremenek85888962008-10-21 00:54:44 +0000267
268 if (!ErrMsg.empty()) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000269 os << "PTH error: " << ErrMsg << "\n";
Ted Kremenek85888962008-10-21 00:54:44 +0000270 return;
271 }
272
273 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
274 I!=E; ++I) {
275
276 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000277 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000278
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000279 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
280 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000281
282 // FIXME: Handle files with non-absolute paths.
283 llvm::sys::Path P(FE->getName());
284 if (!P.isAbsolute())
285 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000286
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000287 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
288 if (PI != PM.end()) continue;
289
290 const llvm::MemoryBuffer* B = C->Buffer;
291 if (!B) continue;
292
Ted Kremenek85888962008-10-21 00:54:44 +0000293 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
294 B->getBufferStart(), B->getBufferEnd(), B);
Ted Kremenekfb645b62008-12-11 23:36:38 +0000295
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000296 PM[FE] = LexTokens(Out, L, PP, idcount, IM);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000297 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000298
299 // Write out the identifier table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000300 std::pair<Offset,Offset> IdTableOff =
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000301 EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM);
Ted Kremenek85888962008-10-21 00:54:44 +0000302
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000303 // Write out the file table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000304 Offset FileTableOff = EmitFileTable(Out, SM, PM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000305
306 // Finally, write out the offset table at the end.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000307 Emit32(Out, IdTableOff.first);
308 Emit32(Out, IdTableOff.second);
309 Emit32(Out, FileTableOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000310}