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