Ted Kremenek | 8588896 | 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" |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 25 | #include "llvm/System/Path.h" |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 31 | typedef uint32_t Offset; |
| 32 | |
Ted Kremenek | e0ea5dc | 2009-02-10 01:06:17 +0000 | [diff] [blame] | 33 | static void Emit8(llvm::raw_ostream& Out, uint32_t V) { |
| 34 | Out << (unsigned char)(V); |
| 35 | } |
| 36 | |
| 37 | static void Emit16(llvm::raw_ostream& Out, uint32_t V) { |
| 38 | Out << (unsigned char)(V); |
| 39 | Out << (unsigned char)(V >> 8); |
| 40 | assert((V >> 16) == 0); |
| 41 | } |
| 42 | |
| 43 | static void Emit32(llvm::raw_ostream& Out, uint32_t V) { |
| 44 | Out << (unsigned char)(V); |
| 45 | Out << (unsigned char)(V >> 8); |
| 46 | Out << (unsigned char)(V >> 16); |
| 47 | Out << (unsigned char)(V >> 24); |
| 48 | } |
| 49 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 50 | static void Pad(llvm::raw_fd_ostream& Out, unsigned A) { |
| 51 | Offset off = (Offset) Out.tell(); |
| 52 | uint32_t n = ((uintptr_t)(off+A-1) & ~(uintptr_t)(A-1)) - off; |
| 53 | for ( ; n ; --n ) Emit8(Out, 0); |
Ted Kremenek | e0ea5dc | 2009-02-10 01:06:17 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 56 | // Bernstein hash function: |
| 57 | // This is basically copy-and-paste from StringMap. This likely won't |
| 58 | // stay here, which is why I didn't both to expose this function from |
| 59 | // String Map. |
| 60 | static unsigned BernsteinHash(const char* x) { |
| 61 | unsigned int R = 0; |
| 62 | for ( ; *x != '\0' ; ++x) R = R * 33 + *x; |
| 63 | return R + (R >> 5); |
| 64 | } |
| 65 | |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 66 | //===----------------------------------------------------------------------===// |
| 67 | // On Disk Hashtable Logic. This will eventually get refactored and put |
| 68 | // elsewhere. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | template<typename Info> |
| 72 | class OnDiskChainedHashTableGenerator { |
| 73 | unsigned NumBuckets; |
| 74 | unsigned NumEntries; |
| 75 | llvm::BumpPtrAllocator BA; |
| 76 | |
| 77 | class Item { |
| 78 | public: |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 79 | typename Info::key_type key; |
| 80 | typename Info::data_type data; |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 81 | Item *next; |
| 82 | const uint32_t hash; |
| 83 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 84 | Item(typename Info::key_type_ref k, typename Info::data_type_ref d) |
| 85 | : key(k), data(d), next(0), hash(Info::ComputeHash(k)) {} |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | class Bucket { |
| 89 | public: |
| 90 | Offset off; |
| 91 | Item* head; |
| 92 | unsigned length; |
| 93 | |
| 94 | Bucket() {} |
| 95 | }; |
| 96 | |
| 97 | Bucket* Buckets; |
| 98 | |
| 99 | private: |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 100 | void insert(Bucket* b, size_t size, Item* E) { |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 101 | unsigned idx = E->hash & (size - 1); |
| 102 | Bucket& B = b[idx]; |
| 103 | E->next = B.head; |
| 104 | ++B.length; |
| 105 | B.head = E; |
| 106 | } |
| 107 | |
| 108 | void resize(size_t newsize) { |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 109 | Bucket* newBuckets = (Bucket*) calloc(newsize, sizeof(Bucket)); |
| 110 | // Populate newBuckets with the old entries. |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 111 | for (unsigned i = 0; i < NumBuckets; ++i) |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 112 | for (Item* E = Buckets[i].head; E ; ) { |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 113 | Item* N = E->next; |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 114 | E->next = 0; |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 115 | insert(newBuckets, newsize, E); |
| 116 | E = N; |
| 117 | } |
| 118 | |
| 119 | free(Buckets); |
| 120 | NumBuckets = newsize; |
| 121 | Buckets = newBuckets; |
| 122 | } |
| 123 | |
| 124 | public: |
| 125 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 126 | void insert(typename Info::key_type_ref key, |
| 127 | typename Info::data_type_ref data) { |
| 128 | |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 129 | ++NumEntries; |
| 130 | if (4*NumEntries >= 3*NumBuckets) resize(NumBuckets*2); |
| 131 | insert(Buckets, NumBuckets, new (BA.Allocate<Item>()) Item(key, data)); |
| 132 | } |
| 133 | |
| 134 | Offset Emit(llvm::raw_fd_ostream& out) { |
| 135 | // Emit the payload of the table. |
| 136 | for (unsigned i = 0; i < NumBuckets; ++i) { |
| 137 | Bucket& B = Buckets[i]; |
| 138 | if (!B.head) continue; |
| 139 | |
| 140 | // Store the offset for the data of this bucket. |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 141 | B.off = out.tell(); |
| 142 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 143 | // Write out the number of items in the bucket. |
| 144 | Emit16(out, B.length); |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 145 | |
| 146 | // Write out the entries in the bucket. |
| 147 | for (Item *I = B.head; I ; I = I->next) { |
| 148 | Emit32(out, I->hash); |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 149 | const std::pair<unsigned, unsigned>& Len = |
| 150 | Info::EmitKeyDataLength(out, I->key, I->data); |
| 151 | Info::EmitKey(out, I->key, Len.first); |
| 152 | Info::EmitData(out, I->data, Len.second); |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | // Emit the hashtable itself. |
| 157 | Pad(out, 4); |
| 158 | Offset TableOff = out.tell(); |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 159 | Emit32(out, NumBuckets); |
| 160 | Emit32(out, NumEntries); |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 161 | for (unsigned i = 0; i < NumBuckets; ++i) Emit32(out, Buckets[i].off); |
| 162 | |
| 163 | return TableOff; |
| 164 | } |
| 165 | |
| 166 | OnDiskChainedHashTableGenerator() { |
| 167 | NumEntries = 0; |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 168 | NumBuckets = 64; |
| 169 | // Note that we do not need to run the constructors of the individual |
| 170 | // Bucket objects since 'calloc' returns bytes that are all 0. |
| 171 | Buckets = (Bucket*) calloc(NumBuckets, sizeof(Bucket)); |
Ted Kremenek | f0e1f79 | 2009-02-10 01:14:45 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | ~OnDiskChainedHashTableGenerator() { |
| 175 | free(Buckets); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | // PTH-specific stuff. |
| 181 | //===----------------------------------------------------------------------===// |
| 182 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 183 | namespace { |
| 184 | class VISIBILITY_HIDDEN PCHEntry { |
| 185 | Offset TokenData, PPCondData; |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 186 | |
| 187 | public: |
| 188 | PCHEntry() {} |
| 189 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 190 | PCHEntry(Offset td, Offset ppcd) |
| 191 | : TokenData(td), PPCondData(ppcd) {} |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 193 | Offset getTokenOffset() const { return TokenData; } |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 194 | Offset getPPCondTableOffset() const { return PPCondData; } |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 195 | }; |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 197 | class VISIBILITY_HIDDEN FileEntryPCHEntryInfo { |
| 198 | public: |
| 199 | typedef const FileEntry* key_type; |
| 200 | typedef key_type key_type_ref; |
| 201 | |
| 202 | typedef PCHEntry data_type; |
| 203 | typedef const PCHEntry& data_type_ref; |
| 204 | |
| 205 | static unsigned ComputeHash(const FileEntry* FE) { |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 206 | return BernsteinHash(FE->getName()); |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static std::pair<unsigned,unsigned> |
| 210 | EmitKeyDataLength(llvm::raw_ostream& Out, const FileEntry* FE, |
| 211 | const PCHEntry& E) { |
| 212 | |
| 213 | unsigned n = strlen(FE->getName()) + 1; |
| 214 | ::Emit16(Out, n); |
| 215 | return std::make_pair(n, 8); |
| 216 | } |
| 217 | |
| 218 | static void EmitKey(llvm::raw_ostream& Out, const FileEntry* FE, unsigned n) { |
| 219 | Out.write(FE->getName(), n); |
| 220 | } |
| 221 | |
| 222 | static void EmitData(llvm::raw_ostream& Out, const PCHEntry& E, unsigned) { |
| 223 | ::Emit32(Out, E.getTokenOffset()); |
| 224 | ::Emit32(Out, E.getPPCondTableOffset()); |
| 225 | } |
| 226 | }; |
| 227 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 228 | class OffsetOpt { |
| 229 | bool valid; |
| 230 | Offset off; |
| 231 | public: |
| 232 | OffsetOpt() : valid(false) {} |
| 233 | bool hasOffset() const { return valid; } |
| 234 | Offset getOffset() const { assert(valid); return off; } |
| 235 | void setOffset(Offset o) { off = o; valid = true; } |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 236 | }; |
| 237 | } // end anonymous namespace |
| 238 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 239 | typedef OnDiskChainedHashTableGenerator<FileEntryPCHEntryInfo> PCHMap; |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 240 | typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap; |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 241 | typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 243 | namespace { |
| 244 | class VISIBILITY_HIDDEN PTHWriter { |
| 245 | IDMap IM; |
| 246 | llvm::raw_fd_ostream& Out; |
| 247 | Preprocessor& PP; |
| 248 | uint32_t idcount; |
| 249 | PCHMap PM; |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 250 | CachedStrsTy CachedStrs; |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 251 | Offset CurStrOffset; |
| 252 | std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries; |
Ted Kremenek | 8f174e1 | 2008-12-23 02:52:12 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 254 | //// Get the persistent id for the given IdentifierInfo*. |
| 255 | uint32_t ResolveID(const IdentifierInfo* II); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 257 | /// Emit a token to the PTH file. |
| 258 | void EmitToken(const Token& T); |
| 259 | |
| 260 | void Emit8(uint32_t V) { |
| 261 | Out << (unsigned char)(V); |
| 262 | } |
| 263 | |
Ted Kremenek | e0ea5dc | 2009-02-10 01:06:17 +0000 | [diff] [blame] | 264 | void Emit16(uint32_t V) { ::Emit16(Out, V); } |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 265 | |
| 266 | void Emit24(uint32_t V) { |
| 267 | Out << (unsigned char)(V); |
| 268 | Out << (unsigned char)(V >> 8); |
| 269 | Out << (unsigned char)(V >> 16); |
| 270 | assert((V >> 24) == 0); |
| 271 | } |
| 272 | |
Ted Kremenek | e0ea5dc | 2009-02-10 01:06:17 +0000 | [diff] [blame] | 273 | void Emit32(uint32_t V) { ::Emit32(Out, V); } |
| 274 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 275 | void EmitBuf(const char* I, const char* E) { |
| 276 | for ( ; I != E ; ++I) Out << *I; |
| 277 | } |
| 278 | |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 279 | /// EmitIdentifierTable - Emits two tables to the PTH file. The first is |
| 280 | /// a hashtable mapping from identifier strings to persistent IDs. |
| 281 | /// The second is a straight table mapping from persistent IDs to string data |
| 282 | /// (the keys of the first table). |
Ted Kremenek | f1de464 | 2009-02-11 16:06:55 +0000 | [diff] [blame] | 283 | std::pair<Offset, Offset> EmitIdentifierTable(); |
| 284 | |
| 285 | /// EmitFileTable - Emit a table mapping from file name strings to PTH |
| 286 | /// token data. |
| 287 | Offset EmitFileTable() { return PM.Emit(Out); } |
| 288 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 289 | PCHEntry LexTokens(Lexer& L); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 290 | Offset EmitCachedSpellings(); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 292 | public: |
| 293 | PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp) |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 294 | : Out(out), PP(pp), idcount(0), CurStrOffset(0) {} |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 295 | |
| 296 | void GeneratePTH(); |
| 297 | }; |
| 298 | } // end anonymous namespace |
| 299 | |
| 300 | uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) { |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 301 | // Null IdentifierInfo's map to the persistent ID 0. |
| 302 | if (!II) |
| 303 | return 0; |
| 304 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 305 | IDMap::iterator I = IM.find(II); |
| 306 | |
| 307 | if (I == IM.end()) { |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 308 | IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL. |
| 309 | return idcount; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 312 | return I->second; // We've already added 1. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 315 | void PTHWriter::EmitToken(const Token& T) { |
Ted Kremenek | 7b78b7c | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 316 | Emit32(((uint32_t) T.getKind()) | |
| 317 | (((uint32_t) T.getFlags()) << 8) | |
| 318 | (((uint32_t) T.getLength()) << 16)); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 320 | // Literals (strings, numbers, characters) get cached spellings. |
| 321 | if (T.isLiteral()) { |
| 322 | // FIXME: This uses the slow getSpelling(). Perhaps we do better |
| 323 | // in the future? This only slows down PTH generation. |
| 324 | const std::string &spelling = PP.getSpelling(T); |
| 325 | const char* s = spelling.c_str(); |
| 326 | |
| 327 | // Get the string entry. |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 328 | llvm::StringMapEntry<OffsetOpt> *E = |
| 329 | &CachedStrs.GetOrCreateValue(s, s+spelling.size()); |
| 330 | |
| 331 | if (!E->getValue().hasOffset()) { |
| 332 | E->getValue().setOffset(CurStrOffset); |
| 333 | StrEntries.push_back(E); |
| 334 | CurStrOffset += spelling.size() + 1; |
| 335 | } |
| 336 | |
| 337 | Emit32(E->getValue().getOffset()); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 338 | } |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 339 | else |
| 340 | Emit32(ResolveID(T.getIdentifierInfo())); |
| 341 | |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 342 | Emit32(PP.getSourceManager().getFileOffset(T.getLocation())); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 345 | PCHEntry PTHWriter::LexTokens(Lexer& L) { |
Ted Kremenek | 7b78b7c | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 346 | // Pad 0's so that we emit tokens to a 4-byte alignment. |
| 347 | // This speed up reading them back in. |
Ted Kremenek | e0ea5dc | 2009-02-10 01:06:17 +0000 | [diff] [blame] | 348 | Pad(Out, 4); |
| 349 | Offset off = (Offset) Out.tell(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 350 | |
| 351 | // Keep track of matching '#if' ... '#endif'. |
| 352 | typedef std::vector<std::pair<Offset, unsigned> > PPCondTable; |
| 353 | PPCondTable PPCond; |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 354 | std::vector<unsigned> PPStartCond; |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 355 | bool ParsingPreprocessorDirective = false; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 356 | Token Tok; |
| 357 | |
| 358 | do { |
| 359 | L.LexFromRawLexer(Tok); |
Ted Kremenek | 726080d | 2009-02-10 22:43:16 +0000 | [diff] [blame] | 360 | NextToken: |
| 361 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 362 | if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) && |
| 363 | ParsingPreprocessorDirective) { |
| 364 | // Insert an eom token into the token cache. It has the same |
| 365 | // position as the next token that is not on the same line as the |
| 366 | // preprocessor directive. Observe that we continue processing |
| 367 | // 'Tok' when we exit this branch. |
| 368 | Token Tmp = Tok; |
| 369 | Tmp.setKind(tok::eom); |
| 370 | Tmp.clearFlag(Token::StartOfLine); |
| 371 | Tmp.setIdentifierInfo(0); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 372 | EmitToken(Tmp); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 373 | ParsingPreprocessorDirective = false; |
| 374 | } |
| 375 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 376 | if (Tok.is(tok::identifier)) { |
| 377 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | e4f6b1e | 2009-02-10 22:27:09 +0000 | [diff] [blame] | 378 | EmitToken(Tok); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 379 | continue; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 380 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 381 | |
| 382 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 383 | // Special processing for #include. Store the '#' token and lex |
| 384 | // the next token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 385 | assert(!ParsingPreprocessorDirective); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 386 | Offset HashOff = (Offset) Out.tell(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 387 | EmitToken(Tok); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 388 | |
| 389 | // Get the next token. |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 390 | L.LexFromRawLexer(Tok); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 391 | |
| 392 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 393 | |
| 394 | // Did we see 'include'/'import'/'include_next'? |
Ted Kremenek | e4f6b1e | 2009-02-10 22:27:09 +0000 | [diff] [blame] | 395 | if (!Tok.is(tok::identifier)) { |
| 396 | EmitToken(Tok); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 397 | continue; |
Ted Kremenek | e4f6b1e | 2009-02-10 22:27:09 +0000 | [diff] [blame] | 398 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 399 | |
| 400 | IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok); |
| 401 | Tok.setIdentifierInfo(II); |
| 402 | tok::PPKeywordKind K = II->getPPKeywordID(); |
| 403 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 404 | assert(K != tok::pp_not_keyword); |
| 405 | ParsingPreprocessorDirective = true; |
| 406 | |
| 407 | switch (K) { |
| 408 | default: |
| 409 | break; |
| 410 | case tok::pp_include: |
| 411 | case tok::pp_import: |
| 412 | case tok::pp_include_next: { |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 413 | // Save the 'include' token. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 414 | EmitToken(Tok); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 415 | // Lex the next token as an include string. |
| 416 | L.setParsingPreprocessorDirective(true); |
| 417 | L.LexIncludeFilename(Tok); |
| 418 | L.setParsingPreprocessorDirective(false); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 419 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 420 | if (Tok.is(tok::identifier)) |
| 421 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 422 | |
| 423 | break; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 424 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 425 | case tok::pp_if: |
| 426 | case tok::pp_ifdef: |
| 427 | case tok::pp_ifndef: { |
Ted Kremenek | e4f6b1e | 2009-02-10 22:27:09 +0000 | [diff] [blame] | 428 | // Add an entry for '#if' and friends. We initially set the target |
| 429 | // index to 0. This will get backpatched when we hit #endif. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 430 | PPStartCond.push_back(PPCond.size()); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 431 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 432 | break; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 433 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 434 | case tok::pp_endif: { |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 435 | // Add an entry for '#endif'. We set the target table index to itself. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 436 | // This will later be set to zero when emitting to the PTH file. We |
| 437 | // use 0 for uninitialized indices because that is easier to debug. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 438 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 439 | // Backpatch the opening '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 440 | assert(!PPStartCond.empty()); |
| 441 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 442 | assert(PPCond[PPStartCond.back()].second == 0); |
| 443 | PPCond[PPStartCond.back()].second = index; |
| 444 | PPStartCond.pop_back(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 445 | // Add the new entry to PPCond. |
| 446 | PPCond.push_back(std::make_pair(HashOff, index)); |
Ted Kremenek | 726080d | 2009-02-10 22:43:16 +0000 | [diff] [blame] | 447 | EmitToken(Tok); |
| 448 | |
| 449 | // Some files have gibberish on the same line as '#endif'. |
| 450 | // Discard these tokens. |
| 451 | do L.LexFromRawLexer(Tok); while (!Tok.is(tok::eof) && |
| 452 | !Tok.isAtStartOfLine()); |
| 453 | // We have the next token in hand. |
| 454 | // Don't immediately lex the next one. |
| 455 | goto NextToken; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 456 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 457 | case tok::pp_elif: |
| 458 | case tok::pp_else: { |
| 459 | // Add an entry for #elif or #else. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 460 | // This serves as both a closing and opening of a conditional block. |
| 461 | // This means that its entry will get backpatched later. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 462 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 463 | // Backpatch the previous '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 464 | assert(!PPStartCond.empty()); |
| 465 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 466 | assert(PPCond[PPStartCond.back()].second == 0); |
| 467 | PPCond[PPStartCond.back()].second = index; |
| 468 | PPStartCond.pop_back(); |
| 469 | // Now add '#elif' as a new block opening. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 470 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
| 471 | PPStartCond.push_back(index); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 472 | break; |
| 473 | } |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 474 | } |
Ted Kremenek | e4f6b1e | 2009-02-10 22:27:09 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | EmitToken(Tok); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 478 | } |
Ted Kremenek | e4f6b1e | 2009-02-10 22:27:09 +0000 | [diff] [blame] | 479 | while (Tok.isNot(tok::eof)); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 481 | assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals."); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 482 | |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 483 | // Next write out PPCond. |
| 484 | Offset PPCondOff = (Offset) Out.tell(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 485 | |
| 486 | // Write out the size of PPCond so that clients can identifer empty tables. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 487 | Emit32(PPCond.size()); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 488 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 489 | for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) { |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 490 | Emit32(PPCond[i].first - off); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 491 | uint32_t x = PPCond[i].second; |
| 492 | assert(x != 0 && "PPCond entry not backpatched."); |
| 493 | // Emit zero for #endifs. This allows us to do checking when |
| 494 | // we read the PTH file back in. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 495 | Emit32(x == i ? 0 : x); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 498 | return PCHEntry(off, PPCondOff); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 501 | Offset PTHWriter::EmitCachedSpellings() { |
| 502 | // Write each cached strings to the PTH file. |
| 503 | Offset SpellingsOff = Out.tell(); |
| 504 | |
| 505 | for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator |
| 506 | I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I) { |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 507 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 508 | const char* data = (*I)->getKeyData(); |
| 509 | EmitBuf(data, data + (*I)->getKeyLength()); |
| 510 | Emit8('\0'); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 513 | return SpellingsOff; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 514 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 516 | void PTHWriter::GeneratePTH() { |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 517 | // Generate the prologue. |
| 518 | Out << "cfe-pth"; |
Ted Kremenek | 67d1505 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 519 | Emit32(PTHManager::Version); |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame^] | 520 | |
| 521 | // Leave 4 words for the prologue. |
| 522 | Offset PrologueOffset = Out.tell(); |
| 523 | for (unsigned i = 0; i < 4 * sizeof(uint32_t); ++i) Emit8(0); |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 524 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 525 | // Iterate over all the files in SourceManager. Create a lexer |
| 526 | // for each file and cache the tokens. |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 527 | SourceManager &SM = PP.getSourceManager(); |
| 528 | const LangOptions &LOpts = PP.getLangOptions(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 529 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 530 | for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(), |
| 531 | E = SM.fileinfo_end(); I != E; ++I) { |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 532 | const SrcMgr::ContentCache &C = *I->second; |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 533 | const FileEntry *FE = C.Entry; |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 534 | |
| 535 | // FIXME: Handle files with non-absolute paths. |
| 536 | llvm::sys::Path P(FE->getName()); |
| 537 | if (!P.isAbsolute()) |
| 538 | continue; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 539 | |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 540 | // assert(!PM.count(FE) && "fileinfo's are not uniqued on FileEntry?"); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 541 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 542 | const llvm::MemoryBuffer *B = C.getBuffer(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 543 | if (!B) continue; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 545 | FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User); |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 546 | Lexer L(FID, SM, LOpts); |
Ted Kremenek | d8c0292 | 2009-02-10 22:16:22 +0000 | [diff] [blame] | 547 | PM.insert(FE, LexTokens(L)); |
Daniel Dunbar | 31309ab | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 548 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 549 | |
| 550 | // Write out the identifier table. |
Ted Kremenek | f1de464 | 2009-02-11 16:06:55 +0000 | [diff] [blame] | 551 | const std::pair<Offset,Offset>& IdTableOff = EmitIdentifierTable(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 552 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 553 | // Write out the cached strings table. |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 554 | Offset SpellingOff = EmitCachedSpellings(); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 555 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 556 | // Write out the file table. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 557 | Offset FileTableOff = EmitFileTable(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 558 | |
Ted Kremenek | a4bd8eb | 2009-02-11 23:34:32 +0000 | [diff] [blame^] | 559 | // Finally, write the prologue. |
| 560 | Out.seek(PrologueOffset); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 561 | Emit32(IdTableOff.first); |
Ted Kremenek | f1de464 | 2009-02-11 16:06:55 +0000 | [diff] [blame] | 562 | Emit32(IdTableOff.second); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 563 | Emit32(FileTableOff); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 564 | Emit32(SpellingOff); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { |
| 568 | // Lex through the entire file. This will populate SourceManager with |
| 569 | // all of the header information. |
| 570 | Token Tok; |
| 571 | PP.EnterMainSourceFile(); |
| 572 | do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); |
| 573 | |
| 574 | // Open up the PTH file. |
| 575 | std::string ErrMsg; |
| 576 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
| 577 | |
| 578 | if (!ErrMsg.empty()) { |
| 579 | llvm::errs() << "PTH error: " << ErrMsg << "\n"; |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | // Create the PTHWriter and generate the PTH file. |
| 584 | PTHWriter PW(Out, PP); |
| 585 | PW.GeneratePTH(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 586 | } |
Ted Kremenek | e0ea5dc | 2009-02-10 01:06:17 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | 7e3a004 | 2009-02-11 21:29:16 +0000 | [diff] [blame] | 588 | //===----------------------------------------------------------------------===// |
| 589 | |
| 590 | namespace { |
| 591 | class VISIBILITY_HIDDEN PCHIdKey { |
| 592 | public: |
| 593 | const IdentifierInfo* II; |
| 594 | uint32_t FileOffset; |
| 595 | }; |
| 596 | |
| 597 | class VISIBILITY_HIDDEN PCHIdentifierTableTrait { |
| 598 | public: |
| 599 | typedef PCHIdKey* key_type; |
| 600 | typedef key_type key_type_ref; |
| 601 | |
| 602 | typedef uint32_t data_type; |
| 603 | typedef data_type data_type_ref; |
| 604 | |
| 605 | static unsigned ComputeHash(PCHIdKey* key) { |
| 606 | return BernsteinHash(key->II->getName()); |
| 607 | } |
| 608 | |
| 609 | static std::pair<unsigned,unsigned> |
| 610 | EmitKeyDataLength(llvm::raw_ostream& Out, const PCHIdKey* key, uint32_t) { |
| 611 | unsigned n = strlen(key->II->getName()) + 1; |
| 612 | ::Emit16(Out, n); |
| 613 | return std::make_pair(n, sizeof(uint32_t)); |
| 614 | } |
| 615 | |
| 616 | static void EmitKey(llvm::raw_fd_ostream& Out, PCHIdKey* key, unsigned n) { |
| 617 | // Record the location of the key data. This is used when generating |
| 618 | // the mapping from persistent IDs to strings. |
| 619 | key->FileOffset = Out.tell(); |
| 620 | Out.write(key->II->getName(), n); |
| 621 | } |
| 622 | |
| 623 | static void EmitData(llvm::raw_ostream& Out, uint32_t pID, unsigned) { |
| 624 | ::Emit32(Out, pID); |
| 625 | } |
| 626 | }; |
| 627 | } // end anonymous namespace |
| 628 | |
| 629 | /// EmitIdentifierTable - Emits two tables to the PTH file. The first is |
| 630 | /// a hashtable mapping from identifier strings to persistent IDs. The second |
| 631 | /// is a straight table mapping from persistent IDs to string data (the |
| 632 | /// keys of the first table). |
| 633 | /// |
| 634 | std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() { |
| 635 | // Build two maps: |
| 636 | // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset) |
| 637 | // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs |
| 638 | |
| 639 | // Note that we use 'calloc', so all the bytes are 0. |
| 640 | PCHIdKey* IIDMap = (PCHIdKey*) calloc(idcount, sizeof(PCHIdKey)); |
| 641 | |
| 642 | // Create the hashtable. |
| 643 | OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> IIOffMap; |
| 644 | |
| 645 | // Generate mapping from persistent IDs -> IdentifierInfo*. |
| 646 | for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) { |
| 647 | // Decrement by 1 because we are using a vector for the lookup and |
| 648 | // 0 is reserved for NULL. |
| 649 | assert(I->second > 0); |
| 650 | assert(I->second-1 < idcount); |
| 651 | unsigned idx = I->second-1; |
| 652 | |
| 653 | // Store the mapping from persistent ID to IdentifierInfo* |
| 654 | IIDMap[idx].II = I->first; |
| 655 | |
| 656 | // Store the reverse mapping in a hashtable. |
| 657 | IIOffMap.insert(&IIDMap[idx], I->second); |
| 658 | } |
| 659 | |
| 660 | // Write out the inverse map first. This causes the PCIDKey entries to |
| 661 | // record PTH file offsets for the string data. This is used to write |
| 662 | // the second table. |
| 663 | Offset StringTableOffset = IIOffMap.Emit(Out); |
| 664 | |
| 665 | // Now emit the table mapping from persistent IDs to PTH file offsets. |
| 666 | Offset IDOff = Out.tell(); |
| 667 | Emit32(idcount); // Emit the number of identifiers. |
| 668 | for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset); |
| 669 | |
| 670 | // Finally, release the inverse map. |
| 671 | free(IIDMap); |
| 672 | |
| 673 | return std::make_pair(IDOff, StringTableOffset); |
| 674 | } |
| 675 | |
| 676 | |