blob: add20f33b408af443e0b2e850c644861677930f7 [file] [log] [blame]
Ted Kremenek71c6cc62008-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"
24
25using namespace clang;
26
27typedef llvm::DenseMap<const FileEntry*,uint64_t> PCHMap;
28typedef llvm::DenseMap<const IdentifierInfo*,uint64_t> IDMap;
29
30static void Emit32(llvm::raw_ostream& Out, uint32_t V) {
Ted Kremenekd330ec12008-11-26 03:36:26 +000031#if 0
Ted Kremenek71c6cc62008-10-21 00:54:44 +000032 Out << (unsigned char)(V);
33 Out << (unsigned char)(V >> 8);
34 Out << (unsigned char)(V >> 16);
35 Out << (unsigned char)(V >> 24);
Ted Kremenekd330ec12008-11-26 03:36:26 +000036#else
37 Out << V;
38#endif
39}
40
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +000041static void Emit64(llvm::raw_ostream& Out, uint64_t V) {
42 Out << V;
43}
44
Ted Kremenekd330ec12008-11-26 03:36:26 +000045static void EmitOffset(llvm::raw_ostream& Out, uint64_t V) {
46 assert(((uint32_t) V) == V && "Offset exceeds 32 bits.");
47 Emit32(Out, (uint32_t) V);
Ted Kremenek71c6cc62008-10-21 00:54:44 +000048}
49
50static void Emit8(llvm::raw_ostream& Out, uint32_t V) {
51 Out << (unsigned char)(V);
52}
53
54static void EmitBuf(llvm::raw_ostream& Out, const char* I, const char* E) {
55 for ( ; I != E ; ++I) Out << *I;
56}
57
58static uint32_t ResolveID(IDMap& IM, uint32_t& idx, const IdentifierInfo* II) {
59 IDMap::iterator I = IM.find(II);
60
61 if (I == IM.end()) {
62 IM[II] = idx;
63 return idx++;
64 }
65
66 return I->second;
67}
68
69static void EmitToken(llvm::raw_ostream& Out, const Token& T,
70 uint32_t& idcount, IDMap& IM) {
71 Emit8(Out, T.getKind());
72 Emit8(Out, T.getFlags());
73 Emit32(Out, ResolveID(IM, idcount, T.getIdentifierInfo()));
74 Emit32(Out, T.getLocation().getRawEncoding());
75 Emit32(Out, T.getLength());
76}
77
78
79static void EmitIdentifier(llvm::raw_ostream& Out, const IdentifierInfo& II) {
80 uint32_t X = (uint32_t) II.getTokenID() << 19;
81 X |= (uint32_t) II.getBuiltinID() << 9;
82 X |= (uint32_t) II.getObjCKeywordID() << 4;
83 if (II.hasMacroDefinition()) X |= 0x8;
84 if (II.isExtensionToken()) X |= 0x4;
85 if (II.isPoisoned()) X |= 0x2;
86 if (II.isCPlusPlusOperatorKeyword()) X |= 0x1;
87
88 Emit32(Out, X);
89}
90
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +000091struct IDData {
92 const IdentifierInfo* II;
93 uint32_t FileOffset;
94 const IdentifierTable::const_iterator::value_type* Str;
95};
Ted Kremenekd330ec12008-11-26 03:36:26 +000096
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +000097static std::pair<uint64_t,uint64_t>
98EmitIdentifierTable(llvm::raw_fd_ostream& Out, uint32_t max,
99 const IdentifierTable& T, const IDMap& IM) {
100
101 // Build an inverse map from persistent IDs -> IdentifierInfo*.
102 typedef std::vector< IDData > InverseIDMap;
103 InverseIDMap IIDMap;
104 IIDMap.reserve(max);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000105
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000106 // Generate mapping from persistent IDs -> IdentifierInfo*.
107 for (IDMap::const_iterator I=IM.begin(), E=IM.end(); I!=E; ++I)
108 IIDMap[I->second].II = I->first;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000109
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000110 // Get the string data associated with the IdentifierInfo.
111 for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) {
112 IDMap::const_iterator IDI = IM.find(&(I->getValue()));
113 if (IDI == IM.end()) continue;
114 IIDMap[IDI->second].Str = &(*I);
115 }
116
117 uint64_t DataOff = Out.tell();
118
119 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
120 I->FileOffset = Out.tell(); // Record the location for this data.
121 EmitIdentifier(Out, *(I->II)); // Write out the identifier data.
122 unsigned len = I->Str->getKeyLength(); // Write out the keyword.
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000123 Emit32(Out, len);
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000124 const char* buf = I->Str->getKeyData();
Ted Kremenekd330ec12008-11-26 03:36:26 +0000125 EmitBuf(Out, buf, buf+len);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000126 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000127
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000128 // Now emit the table mapping from persistent IDs to PTH file offsets.
129 uint64_t IDOff = Out.tell();
130
131 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
132 EmitOffset(Out, I->FileOffset);
133
134 return std::make_pair(DataOff, IDOff);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000135}
136
Ted Kremenekd330ec12008-11-26 03:36:26 +0000137static uint64_t EmitFileTable(llvm::raw_fd_ostream& Out, SourceManager& SM,
138 PCHMap& PM) {
139
140 uint64_t off = Out.tell();
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000141
142 // Output the size of the table.
143 Emit32(Out, PM.size());
144
145 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
146 // For now emit inode information. In the future we should utilize
147 // the FileManager's internal mechanism of uniquing files, which differs
148 // for Windows and Unix-like systems.
149 const FileEntry* FE = I->first;
150 Emit64(Out, FE->getDevice());
151 Emit64(Out, FE->getInode());
152 Emit32(Out, I->second);
153 }
154
Ted Kremenekd330ec12008-11-26 03:36:26 +0000155 return off;
156}
157
158static uint64_t LexTokens(llvm::raw_fd_ostream& Out, Lexer& L, Preprocessor& PP,
159 uint32_t& idcount, IDMap& IM) {
160
161 // Record the location within the token file.
162 uint64_t off = Out.tell();
163
164 Token Tok;
165
166 do {
167 L.LexFromRawLexer(Tok);
168
169 if (Tok.is(tok::identifier)) {
170 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
171 }
172 else if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
173 // Special processing for #include. Store the '#' token and lex
174 // the next token.
175 EmitToken(Out, Tok, idcount, IM);
176 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.
190 EmitToken(Out, Tok, idcount, IM);
191
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 }
200 }
201 }
202 while (EmitToken(Out, Tok, idcount, IM), Tok.isNot(tok::eof));
203
204 return off;
205}
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000206
207void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
208 // Lex through the entire file. This will populate SourceManager with
209 // all of the header information.
210 Token Tok;
211 PP.EnterMainSourceFile();
212 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
213
214 // Iterate over all the files in SourceManager. Create a lexer
215 // for each file and cache the tokens.
216 SourceManager& SM = PP.getSourceManager();
217 const LangOptions& LOpts = PP.getLangOptions();
218 llvm::raw_ostream& os = llvm::errs();
219
220 PCHMap PM;
221 IDMap IM;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000222 uint32_t idcount = 0;
223
224 std::string ErrMsg;
Daniel Dunbar8fc9ba62008-11-13 05:09:21 +0000225 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000226
227 if (!ErrMsg.empty()) {
228 os << "PCH error: " << ErrMsg << "\n";
229 return;
230 }
231
232 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
233 I!=E; ++I) {
234
235 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremenekd330ec12008-11-26 03:36:26 +0000236 if (!C) continue;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000237
Ted Kremenekd330ec12008-11-26 03:36:26 +0000238 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
239 if (!FE) continue;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000240
Ted Kremenekd330ec12008-11-26 03:36:26 +0000241 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
242 if (PI != PM.end()) continue;
243
244 const llvm::MemoryBuffer* B = C->Buffer;
245 if (!B) continue;
246
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000247 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
248 B->getBufferStart(), B->getBufferEnd(), B);
Daniel Dunbareee6d102008-11-26 02:18:33 +0000249
Ted Kremenekd330ec12008-11-26 03:36:26 +0000250 PM[FE] = LexTokens(Out, L, PP, idcount, IM);
Daniel Dunbareee6d102008-11-26 02:18:33 +0000251 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000252
253 // Write out the identifier table.
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000254 std::pair<uint64_t,uint64_t> IdTableOff =
255 EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000256
Ted Kremenekd330ec12008-11-26 03:36:26 +0000257 // Write out the file table.
258 uint64_t FileTableOff = EmitFileTable(Out, SM, PM);
259
260 // Finally, write out the offset table at the end.
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000261 EmitOffset(Out, IdTableOff.first);
262 EmitOffset(Out, IdTableOff.second);
Ted Kremenekd330ec12008-11-26 03:36:26 +0000263 EmitOffset(Out, FileTableOff);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000264}