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