blob: 536fefca4dbae88098a32e9239a61256c307293b [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
30typedef llvm::DenseMap<const FileEntry*,Offset> PCHMap;
31typedef 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 Kremenekfa59aad2008-11-26 23:58:26 +0000107
108 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
121 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000122 Emit32(Out, I->FileOffset);
123
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000124 return std::make_pair(DataOff, IDOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000125}
126
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000127Offset EmitFileTable(llvm::raw_fd_ostream& Out, SourceManager& SM, PCHMap& PM) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000128
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000129 Offset off = (Offset) Out.tell();
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000130
131 // Output the size of the table.
132 Emit32(Out, PM.size());
133
134 for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) {
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000135 const FileEntry* FE = I->first;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000136 llvm::sys::Path P(FE->getName());
137 assert(P.isAbsolute());
138 Emit32(Out, P.size());
139 const char* buf = P.c_str();
140 EmitBuf(Out, buf, buf+P.size());
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000141 Emit32(Out, I->second);
142 }
143
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000144 return off;
145}
146
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000147static Offset LexTokens(llvm::raw_fd_ostream& Out, Lexer& L, Preprocessor& PP,
148 uint32_t& idcount, IDMap& IM) {
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000149
150 // Record the location within the token file.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000151 Offset off = (Offset) Out.tell();
152 SourceManager& SMgr = PP.getSourceManager();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000153
154 Token Tok;
155
156 do {
157 L.LexFromRawLexer(Tok);
158
159 if (Tok.is(tok::identifier)) {
160 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
161 }
162 else if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
163 // Special processing for #include. Store the '#' token and lex
164 // the next token.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000165 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000166 L.LexFromRawLexer(Tok);
167
168 // Did we see 'include'/'import'/'include_next'?
169 if (!Tok.is(tok::identifier))
170 continue;
171
172 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
173 Tok.setIdentifierInfo(II);
174 tok::PPKeywordKind K = II->getPPKeywordID();
175
176 if (K == tok::pp_include || K == tok::pp_import ||
177 K == tok::pp_include_next) {
178
179 // Save the 'include' token.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000180 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000181
182 // Lex the next token as an include string.
183 L.setParsingPreprocessorDirective(true);
184 L.LexIncludeFilename(Tok);
185 L.setParsingPreprocessorDirective(false);
186
187 if (Tok.is(tok::identifier))
188 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
189 }
190 }
191 }
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000192 while (EmitToken(Out, Tok, SMgr, idcount, IM), Tok.isNot(tok::eof));
193
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000194 return off;
195}
Ted Kremenek85888962008-10-21 00:54:44 +0000196
197void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
198 // Lex through the entire file. This will populate SourceManager with
199 // all of the header information.
200 Token Tok;
201 PP.EnterMainSourceFile();
202 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
203
204 // Iterate over all the files in SourceManager. Create a lexer
205 // for each file and cache the tokens.
206 SourceManager& SM = PP.getSourceManager();
207 const LangOptions& LOpts = PP.getLangOptions();
208 llvm::raw_ostream& os = llvm::errs();
209
210 PCHMap PM;
211 IDMap IM;
Ted Kremenek85888962008-10-21 00:54:44 +0000212 uint32_t idcount = 0;
213
214 std::string ErrMsg;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000215 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
Ted Kremenek85888962008-10-21 00:54:44 +0000216
217 if (!ErrMsg.empty()) {
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000218 os << "PTH error: " << ErrMsg << "\n";
Ted Kremenek85888962008-10-21 00:54:44 +0000219 return;
220 }
221
222 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
223 I!=E; ++I) {
224
225 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000226 if (!C) continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000227
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000228 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
229 if (!FE) continue;
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000230
231 // FIXME: Handle files with non-absolute paths.
232 llvm::sys::Path P(FE->getName());
233 if (!P.isAbsolute())
234 continue;
Ted Kremenek85888962008-10-21 00:54:44 +0000235
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000236 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
237 if (PI != PM.end()) continue;
238
239 const llvm::MemoryBuffer* B = C->Buffer;
240 if (!B) continue;
241
Ted Kremenek85888962008-10-21 00:54:44 +0000242 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
243 B->getBufferStart(), B->getBufferEnd(), B);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000244
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000245 PM[FE] = LexTokens(Out, L, PP, idcount, IM);
Daniel Dunbar31309ab2008-11-26 02:18:33 +0000246 }
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000247
248 // Write out the identifier table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000249 std::pair<Offset,Offset> IdTableOff =
Ted Kremenekfa59aad2008-11-26 23:58:26 +0000250 EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM);
Ted Kremenek85888962008-10-21 00:54:44 +0000251
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000252 // Write out the file table.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000253 Offset FileTableOff = EmitFileTable(Out, SM, PM);
Ted Kremeneka3d764c2008-11-26 03:36:26 +0000254
255 // Finally, write out the offset table at the end.
Ted Kremenekfc7e2ea2008-12-02 19:44:08 +0000256 Emit32(Out, IdTableOff.first);
257 Emit32(Out, IdTableOff.second);
258 Emit32(Out, FileTableOff);
Ted Kremenek85888962008-10-21 00:54:44 +0000259}