Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | typedef llvm::DenseMap<const FileEntry*,uint64_t> PCHMap; |
| 28 | typedef llvm::DenseMap<const IdentifierInfo*,uint64_t> IDMap; |
| 29 | |
| 30 | static void Emit32(llvm::raw_ostream& Out, uint32_t V) { |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 31 | #if 0 |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 32 | Out << (unsigned char)(V); |
| 33 | Out << (unsigned char)(V >> 8); |
| 34 | Out << (unsigned char)(V >> 16); |
| 35 | Out << (unsigned char)(V >> 24); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 36 | #else |
| 37 | Out << V; |
| 38 | #endif |
| 39 | } |
| 40 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 41 | static void Emit64(llvm::raw_ostream& Out, uint64_t V) { |
| 42 | Out << V; |
| 43 | } |
| 44 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 45 | static 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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void Emit8(llvm::raw_ostream& Out, uint32_t V) { |
| 51 | Out << (unsigned char)(V); |
| 52 | } |
| 53 | |
| 54 | static void EmitBuf(llvm::raw_ostream& Out, const char* I, const char* E) { |
| 55 | for ( ; I != E ; ++I) Out << *I; |
| 56 | } |
| 57 | |
| 58 | static 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 | |
| 69 | static 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 | |
| 79 | static 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 Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 91 | struct IDData { |
| 92 | const IdentifierInfo* II; |
| 93 | uint32_t FileOffset; |
| 94 | const IdentifierTable::const_iterator::value_type* Str; |
| 95 | }; |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 97 | static std::pair<uint64_t,uint64_t> |
| 98 | EmitIdentifierTable(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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 106 | // 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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 110 | // 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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 123 | Emit32(Out, len); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 124 | const char* buf = I->Str->getKeyData(); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 125 | EmitBuf(Out, buf, buf+len); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 126 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 128 | // 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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 137 | static uint64_t EmitFileTable(llvm::raw_fd_ostream& Out, SourceManager& SM, |
| 138 | PCHMap& PM) { |
| 139 | |
| 140 | uint64_t off = Out.tell(); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 141 | |
| 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 Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 155 | return off; |
| 156 | } |
| 157 | |
| 158 | static 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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 206 | |
| 207 | void 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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 222 | uint32_t idcount = 0; |
| 223 | |
| 224 | std::string ErrMsg; |
Daniel Dunbar | 8fc9ba6 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 225 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 226 | |
| 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 Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 236 | if (!C) continue; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 238 | const FileEntry* FE = C->Entry; // Does this entry correspond to a file? |
| 239 | if (!FE) continue; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 241 | 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 Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 247 | Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts, |
| 248 | B->getBufferStart(), B->getBufferEnd(), B); |
Daniel Dunbar | eee6d10 | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 250 | PM[FE] = LexTokens(Out, L, PP, idcount, IM); |
Daniel Dunbar | eee6d10 | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 251 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 252 | |
| 253 | // Write out the identifier table. |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 254 | std::pair<uint64_t,uint64_t> IdTableOff = |
| 255 | EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 257 | // 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 Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 261 | EmitOffset(Out, IdTableOff.first); |
| 262 | EmitOffset(Out, IdTableOff.second); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 263 | EmitOffset(Out, FileTableOff); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 264 | } |