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