blob: ceb16b0301a1fbe2c7370a7ba7975caa8643a973 [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"
Ted Kremenek62739d62008-12-02 19:44:08 +000024#include "llvm/System/Path.h"
Ted Kremenek71c6cc62008-10-21 00:54:44 +000025
26using namespace clang;
27
Ted Kremenek62739d62008-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 Kremenek71c6cc62008-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 Kremenek62739d62008-12-02 19:44:08 +000049
50 // Null IdentifierInfo's map to the persistent ID 0.
51 if (!II)
52 return 0;
53
Ted Kremenek71c6cc62008-10-21 00:54:44 +000054 IDMap::iterator I = IM.find(II);
55
56 if (I == IM.end()) {
Ted Kremenek62739d62008-12-02 19:44:08 +000057 IM[II] = ++idx; // Pre-increment since '0' is reserved for NULL.
58 return idx;
Ted Kremenek71c6cc62008-10-21 00:54:44 +000059 }
60
Ted Kremenek62739d62008-12-02 19:44:08 +000061 return I->second; // We've already added 1.
Ted Kremenek71c6cc62008-10-21 00:54:44 +000062}
63
64static void EmitToken(llvm::raw_ostream& Out, const Token& T,
Ted Kremenek62739d62008-12-02 19:44:08 +000065 const SourceManager& SMgr,
Ted Kremenek71c6cc62008-10-21 00:54:44 +000066 uint32_t& idcount, IDMap& IM) {
Ted Kremenek62739d62008-12-02 19:44:08 +000067
Ted Kremenek71c6cc62008-10-21 00:54:44 +000068 Emit8(Out, T.getKind());
69 Emit8(Out, T.getFlags());
70 Emit32(Out, ResolveID(IM, idcount, T.getIdentifierInfo()));
Ted Kremenek62739d62008-12-02 19:44:08 +000071 Emit32(Out, SMgr.getFullFilePos(T.getLocation()));
Ted Kremenek71c6cc62008-10-21 00:54:44 +000072 Emit32(Out, T.getLength());
73}
74
Ted Kremenek3e0bb5b2008-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 Kremenekd330ec12008-11-26 03:36:26 +000080
Ted Kremenek62739d62008-12-02 19:44:08 +000081static std::pair<Offset,Offset>
Ted Kremenek3e0bb5b2008-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 Kremenek62739d62008-12-02 19:44:08 +000086 typedef std::vector<IDData> InverseIDMap;
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +000087 InverseIDMap IIDMap;
Ted Kremenek62739d62008-12-02 19:44:08 +000088 IIDMap.resize(max);
Ted Kremenek71c6cc62008-10-21 00:54:44 +000089
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +000090 // Generate mapping from persistent IDs -> IdentifierInfo*.
Ted Kremenek62739d62008-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 Kremenek71c6cc62008-10-21 00:54:44 +000098
Ted Kremenek3e0bb5b2008-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 Kremenek62739d62008-12-02 19:44:08 +0000103 IIDMap[IDI->second-1].Str = &(*I);
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000104 }
105
Ted Kremenek62739d62008-12-02 19:44:08 +0000106 Offset DataOff = Out.tell();
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000107
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000108 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) {
Ted Kremenek62739d62008-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 Kremenek71c6cc62008-10-21 00:54:44 +0000113 Emit32(Out, len);
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000114 const char* buf = I->Str->getKeyData();
Ted Kremenekd330ec12008-11-26 03:36:26 +0000115 EmitBuf(Out, buf, buf+len);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000116 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000117
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000118 // Now emit the table mapping from persistent IDs to PTH file offsets.
Ted Kremenek62739d62008-12-02 19:44:08 +0000119 Offset IDOff = Out.tell();
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000120
Ted Kremenekdb4c8e82008-12-03 01:16:39 +0000121 // Emit the number of identifiers.
122 Emit32(Out, max);
123
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000124 for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I)
Ted Kremenek62739d62008-12-02 19:44:08 +0000125 Emit32(Out, I->FileOffset);
126
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000127 return std::make_pair(DataOff, IDOff);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000128}
129
Ted Kremenek62739d62008-12-02 19:44:08 +0000130Offset EmitFileTable(llvm::raw_fd_ostream& Out, SourceManager& SM, PCHMap& PM) {
Ted Kremenekd330ec12008-11-26 03:36:26 +0000131
Ted Kremenek62739d62008-12-02 19:44:08 +0000132 Offset off = (Offset) Out.tell();
Ted Kremenek3e0bb5b2008-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 Kremenek3e0bb5b2008-11-26 23:58:26 +0000138 const FileEntry* FE = I->first;
Ted Kremenek11090552008-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 Kremenek3e0bb5b2008-11-26 23:58:26 +0000143 Emit32(Out, I->second);
144 }
145
Ted Kremenekd330ec12008-11-26 03:36:26 +0000146 return off;
147}
148
Ted Kremenek62739d62008-12-02 19:44:08 +0000149static Offset LexTokens(llvm::raw_fd_ostream& Out, Lexer& L, Preprocessor& PP,
150 uint32_t& idcount, IDMap& IM) {
Ted Kremenekd330ec12008-11-26 03:36:26 +0000151
152 // Record the location within the token file.
Ted Kremenek62739d62008-12-02 19:44:08 +0000153 Offset off = (Offset) Out.tell();
154 SourceManager& SMgr = PP.getSourceManager();
Ted Kremenekd330ec12008-11-26 03:36:26 +0000155
156 Token Tok;
157
158 do {
159 L.LexFromRawLexer(Tok);
160
161 if (Tok.is(tok::identifier)) {
162 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
163 }
164 else if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
165 // Special processing for #include. Store the '#' token and lex
166 // the next token.
Ted Kremenek62739d62008-12-02 19:44:08 +0000167 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremenekd330ec12008-11-26 03:36:26 +0000168 L.LexFromRawLexer(Tok);
169
170 // Did we see 'include'/'import'/'include_next'?
171 if (!Tok.is(tok::identifier))
172 continue;
173
174 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
175 Tok.setIdentifierInfo(II);
176 tok::PPKeywordKind K = II->getPPKeywordID();
177
178 if (K == tok::pp_include || K == tok::pp_import ||
179 K == tok::pp_include_next) {
180
181 // Save the 'include' token.
Ted Kremenek62739d62008-12-02 19:44:08 +0000182 EmitToken(Out, Tok, SMgr, idcount, IM);
Ted Kremenekd330ec12008-11-26 03:36:26 +0000183
184 // Lex the next token as an include string.
185 L.setParsingPreprocessorDirective(true);
186 L.LexIncludeFilename(Tok);
187 L.setParsingPreprocessorDirective(false);
188
189 if (Tok.is(tok::identifier))
190 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
191 }
192 }
193 }
Ted Kremenek62739d62008-12-02 19:44:08 +0000194 while (EmitToken(Out, Tok, SMgr, idcount, IM), Tok.isNot(tok::eof));
195
Ted Kremenekd330ec12008-11-26 03:36:26 +0000196 return off;
197}
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000198
199void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
200 // Lex through the entire file. This will populate SourceManager with
201 // all of the header information.
202 Token Tok;
203 PP.EnterMainSourceFile();
204 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
205
206 // Iterate over all the files in SourceManager. Create a lexer
207 // for each file and cache the tokens.
208 SourceManager& SM = PP.getSourceManager();
209 const LangOptions& LOpts = PP.getLangOptions();
210 llvm::raw_ostream& os = llvm::errs();
211
212 PCHMap PM;
213 IDMap IM;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000214 uint32_t idcount = 0;
215
216 std::string ErrMsg;
Daniel Dunbar8fc9ba62008-11-13 05:09:21 +0000217 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000218
219 if (!ErrMsg.empty()) {
Ted Kremenek62739d62008-12-02 19:44:08 +0000220 os << "PTH error: " << ErrMsg << "\n";
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000221 return;
222 }
223
224 for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end();
225 I!=E; ++I) {
226
227 const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache();
Ted Kremenekd330ec12008-11-26 03:36:26 +0000228 if (!C) continue;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000229
Ted Kremenekd330ec12008-11-26 03:36:26 +0000230 const FileEntry* FE = C->Entry; // Does this entry correspond to a file?
231 if (!FE) continue;
Ted Kremenek62739d62008-12-02 19:44:08 +0000232
233 // FIXME: Handle files with non-absolute paths.
234 llvm::sys::Path P(FE->getName());
235 if (!P.isAbsolute())
236 continue;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000237
Ted Kremenekd330ec12008-11-26 03:36:26 +0000238 PCHMap::iterator PI = PM.find(FE); // Have we already processed this file?
239 if (PI != PM.end()) continue;
240
241 const llvm::MemoryBuffer* B = C->Buffer;
242 if (!B) continue;
243
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000244 Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts,
245 B->getBufferStart(), B->getBufferEnd(), B);
Daniel Dunbareee6d102008-11-26 02:18:33 +0000246
Ted Kremenekd330ec12008-11-26 03:36:26 +0000247 PM[FE] = LexTokens(Out, L, PP, idcount, IM);
Daniel Dunbareee6d102008-11-26 02:18:33 +0000248 }
Ted Kremenekd330ec12008-11-26 03:36:26 +0000249
250 // Write out the identifier table.
Ted Kremenek62739d62008-12-02 19:44:08 +0000251 std::pair<Offset,Offset> IdTableOff =
Ted Kremenek3e0bb5b2008-11-26 23:58:26 +0000252 EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000253
Ted Kremenekd330ec12008-11-26 03:36:26 +0000254 // Write out the file table.
Ted Kremenek62739d62008-12-02 19:44:08 +0000255 Offset FileTableOff = EmitFileTable(Out, SM, PM);
Ted Kremenekd330ec12008-11-26 03:36:26 +0000256
257 // Finally, write out the offset table at the end.
Ted Kremenek62739d62008-12-02 19:44:08 +0000258 Emit32(Out, IdTableOff.first);
259 Emit32(Out, IdTableOff.second);
260 Emit32(Out, FileTableOff);
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000261}