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 | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | class VISIBILITY_HIDDEN PCHEntry { |
| 35 | Offset TokenData, PPCondData; |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 36 | |
| 37 | public: |
| 38 | PCHEntry() {} |
| 39 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 40 | PCHEntry(Offset td, Offset ppcd) |
| 41 | : TokenData(td), PPCondData(ppcd) {} |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 43 | Offset getTokenOffset() const { return TokenData; } |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 44 | Offset getPPCondTableOffset() const { return PPCondData; } |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 45 | }; |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 47 | class OffsetOpt { |
| 48 | bool valid; |
| 49 | Offset off; |
| 50 | public: |
| 51 | OffsetOpt() : valid(false) {} |
| 52 | bool hasOffset() const { return valid; } |
| 53 | Offset getOffset() const { assert(valid); return off; } |
| 54 | void setOffset(Offset o) { off = o; valid = true; } |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 55 | }; |
| 56 | } // end anonymous namespace |
| 57 | |
| 58 | typedef llvm::DenseMap<const FileEntry*, PCHEntry> PCHMap; |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 59 | typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap; |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 60 | typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 62 | namespace { |
| 63 | class VISIBILITY_HIDDEN PTHWriter { |
| 64 | IDMap IM; |
| 65 | llvm::raw_fd_ostream& Out; |
| 66 | Preprocessor& PP; |
| 67 | uint32_t idcount; |
| 68 | PCHMap PM; |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 69 | CachedStrsTy CachedStrs; |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 70 | Offset CurStrOffset; |
| 71 | std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries; |
Ted Kremenek | 8f174e1 | 2008-12-23 02:52:12 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 73 | //// Get the persistent id for the given IdentifierInfo*. |
| 74 | uint32_t ResolveID(const IdentifierInfo* II); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 76 | /// Emit a token to the PTH file. |
| 77 | void EmitToken(const Token& T); |
| 78 | |
| 79 | void Emit8(uint32_t V) { |
| 80 | Out << (unsigned char)(V); |
| 81 | } |
| 82 | |
| 83 | void Emit16(uint32_t V) { |
| 84 | Out << (unsigned char)(V); |
| 85 | Out << (unsigned char)(V >> 8); |
| 86 | assert((V >> 16) == 0); |
| 87 | } |
| 88 | |
| 89 | void Emit24(uint32_t V) { |
| 90 | Out << (unsigned char)(V); |
| 91 | Out << (unsigned char)(V >> 8); |
| 92 | Out << (unsigned char)(V >> 16); |
| 93 | assert((V >> 24) == 0); |
| 94 | } |
| 95 | |
| 96 | void Emit32(uint32_t V) { |
| 97 | Out << (unsigned char)(V); |
| 98 | Out << (unsigned char)(V >> 8); |
| 99 | Out << (unsigned char)(V >> 16); |
| 100 | Out << (unsigned char)(V >> 24); |
| 101 | } |
| 102 | |
| 103 | void EmitBuf(const char* I, const char* E) { |
| 104 | for ( ; I != E ; ++I) Out << *I; |
| 105 | } |
| 106 | |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 107 | std::pair<Offset,std::pair<Offset, Offset> > EmitIdentifierTable(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 108 | Offset EmitFileTable(); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 109 | PCHEntry LexTokens(Lexer& L); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 110 | Offset EmitCachedSpellings(); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 112 | public: |
| 113 | PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp) |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 114 | : Out(out), PP(pp), idcount(0), CurStrOffset(0) {} |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 115 | |
| 116 | void GeneratePTH(); |
| 117 | }; |
| 118 | } // end anonymous namespace |
| 119 | |
| 120 | uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) { |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 121 | // Null IdentifierInfo's map to the persistent ID 0. |
| 122 | if (!II) |
| 123 | return 0; |
| 124 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 125 | IDMap::iterator I = IM.find(II); |
| 126 | |
| 127 | if (I == IM.end()) { |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 128 | IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL. |
| 129 | return idcount; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 132 | return I->second; // We've already added 1. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 135 | void PTHWriter::EmitToken(const Token& T) { |
Ted Kremenek | 7b78b7c | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 136 | Emit32(((uint32_t) T.getKind()) | |
| 137 | (((uint32_t) T.getFlags()) << 8) | |
| 138 | (((uint32_t) T.getLength()) << 16)); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 140 | // Literals (strings, numbers, characters) get cached spellings. |
| 141 | if (T.isLiteral()) { |
| 142 | // FIXME: This uses the slow getSpelling(). Perhaps we do better |
| 143 | // in the future? This only slows down PTH generation. |
| 144 | const std::string &spelling = PP.getSpelling(T); |
| 145 | const char* s = spelling.c_str(); |
| 146 | |
| 147 | // Get the string entry. |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 148 | llvm::StringMapEntry<OffsetOpt> *E = |
| 149 | &CachedStrs.GetOrCreateValue(s, s+spelling.size()); |
| 150 | |
| 151 | if (!E->getValue().hasOffset()) { |
| 152 | E->getValue().setOffset(CurStrOffset); |
| 153 | StrEntries.push_back(E); |
| 154 | CurStrOffset += spelling.size() + 1; |
| 155 | } |
| 156 | |
| 157 | Emit32(E->getValue().getOffset()); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 158 | } |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 159 | else |
| 160 | Emit32(ResolveID(T.getIdentifierInfo())); |
| 161 | |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 162 | Emit32(PP.getSourceManager().getFileOffset(T.getLocation())); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 165 | namespace { |
| 166 | struct VISIBILITY_HIDDEN IDData { |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 167 | const IdentifierInfo* II; |
| 168 | uint32_t FileOffset; |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | class VISIBILITY_HIDDEN CompareIDDataIndex { |
| 172 | IDData* Table; |
| 173 | public: |
| 174 | CompareIDDataIndex(IDData* table) : Table(table) {} |
| 175 | |
| 176 | bool operator()(unsigned i, unsigned j) const { |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 177 | const IdentifierInfo* II_i = Table[i].II; |
| 178 | const IdentifierInfo* II_j = Table[j].II; |
| 179 | |
| 180 | unsigned i_len = II_i->getLength(); |
| 181 | unsigned j_len = II_j->getLength(); |
| 182 | |
| 183 | if (i_len > j_len) |
| 184 | return false; |
| 185 | |
| 186 | if (i_len < j_len) |
| 187 | return true; |
| 188 | |
| 189 | // Otherwise, compare the strings themselves! |
| 190 | return strncmp(II_i->getName(), II_j->getName(), i_len) < 0; |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 191 | } |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 192 | }; |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 193 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 195 | std::pair<Offset,std::pair<Offset,Offset> > |
| 196 | PTHWriter::EmitIdentifierTable() { |
| 197 | llvm::BumpPtrAllocator Alloc; |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 198 | |
| 199 | // Build an inverse map from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 200 | IDData* IIDMap = Alloc.Allocate<IDData>(idcount); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 202 | // Generate mapping from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 203 | for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) { |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 204 | // Decrement by 1 because we are using a vector for the lookup and |
| 205 | // 0 is reserved for NULL. |
| 206 | assert(I->second > 0); |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 207 | assert(I->second-1 < idcount); |
| 208 | unsigned idx = I->second-1; |
| 209 | IIDMap[idx].II = I->first; |
| 210 | } |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 212 | // We want to write out the strings in lexical order to support binary |
| 213 | // search of strings to identifiers. Create such a table. |
| 214 | unsigned *LexicalOrder = Alloc.Allocate<unsigned>(idcount); |
| 215 | for (unsigned i = 0; i < idcount ; ++i ) LexicalOrder[i] = i; |
| 216 | std::sort(LexicalOrder, LexicalOrder+idcount, CompareIDDataIndex(IIDMap)); |
| 217 | |
| 218 | // Write out the lexically-sorted table of persistent ids. |
| 219 | Offset LexicalOff = Out.tell(); |
| 220 | for (unsigned i = 0; i < idcount ; ++i) Emit32(LexicalOrder[i]); |
| 221 | |
| 222 | // Write out the string data itself. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 223 | Offset DataOff = Out.tell(); |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 225 | for (unsigned i = 0; i < idcount; ++i) { |
| 226 | IDData& d = IIDMap[i]; |
| 227 | d.FileOffset = Out.tell(); // Record the location for this data. |
| 228 | unsigned len = d.II->getLength(); // Write out the string length. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 229 | Emit32(len); |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 230 | const char* buf = d.II->getName(); // Write out the string data. |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 231 | EmitBuf(buf, buf+len); |
| 232 | // Emit a null character for those clients expecting that IdentifierInfo |
| 233 | // strings are null terminated. |
| 234 | Emit8('\0'); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 235 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 237 | // Now emit the table mapping from persistent IDs to PTH file offsets. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 238 | Offset IDOff = Out.tell(); |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 239 | Emit32(idcount); // Emit the number of identifiers. |
| 240 | for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset); |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 241 | |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 242 | return std::make_pair(DataOff, std::make_pair(IDOff, LexicalOff)); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 245 | Offset PTHWriter::EmitFileTable() { |
| 246 | // Determine the offset where this table appears in the PTH file. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 247 | Offset off = (Offset) Out.tell(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 249 | // Output the size of the table. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 250 | Emit32(PM.size()); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 251 | |
| 252 | for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) { |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 253 | const FileEntry* FE = I->first; |
Ted Kremenek | 8dffd9b | 2008-12-04 22:36:44 +0000 | [diff] [blame] | 254 | const char* Name = FE->getName(); |
| 255 | unsigned size = strlen(Name); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 256 | Emit32(size); |
| 257 | EmitBuf(Name, Name+size); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 258 | Emit32(I->second.getTokenOffset()); |
| 259 | Emit32(I->second.getPPCondTableOffset()); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 262 | return off; |
| 263 | } |
| 264 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 265 | PCHEntry PTHWriter::LexTokens(Lexer& L) { |
Ted Kremenek | 7b78b7c | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 266 | // Pad 0's so that we emit tokens to a 4-byte alignment. |
| 267 | // This speed up reading them back in. |
| 268 | Offset off = (Offset) Out.tell(); |
| 269 | for (unsigned Pad = off % 4 ; Pad != 0 ; --Pad, ++off) Emit8(0); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 270 | |
| 271 | // Keep track of matching '#if' ... '#endif'. |
| 272 | typedef std::vector<std::pair<Offset, unsigned> > PPCondTable; |
| 273 | PPCondTable PPCond; |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 274 | std::vector<unsigned> PPStartCond; |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 275 | bool ParsingPreprocessorDirective = false; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 276 | Token Tok; |
| 277 | |
| 278 | do { |
| 279 | L.LexFromRawLexer(Tok); |
| 280 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 281 | if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) && |
| 282 | ParsingPreprocessorDirective) { |
| 283 | // Insert an eom token into the token cache. It has the same |
| 284 | // position as the next token that is not on the same line as the |
| 285 | // preprocessor directive. Observe that we continue processing |
| 286 | // 'Tok' when we exit this branch. |
| 287 | Token Tmp = Tok; |
| 288 | Tmp.setKind(tok::eom); |
| 289 | Tmp.clearFlag(Token::StartOfLine); |
| 290 | Tmp.setIdentifierInfo(0); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 291 | EmitToken(Tmp); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 292 | ParsingPreprocessorDirective = false; |
| 293 | } |
| 294 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 295 | if (Tok.is(tok::identifier)) { |
| 296 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 297 | continue; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 298 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 299 | |
| 300 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 301 | // Special processing for #include. Store the '#' token and lex |
| 302 | // the next token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 303 | assert(!ParsingPreprocessorDirective); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 304 | Offset HashOff = (Offset) Out.tell(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 305 | EmitToken(Tok); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 306 | |
| 307 | // Get the next token. |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 308 | L.LexFromRawLexer(Tok); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 309 | |
| 310 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 311 | |
| 312 | // Did we see 'include'/'import'/'include_next'? |
| 313 | if (!Tok.is(tok::identifier)) |
| 314 | continue; |
| 315 | |
| 316 | IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok); |
| 317 | Tok.setIdentifierInfo(II); |
| 318 | tok::PPKeywordKind K = II->getPPKeywordID(); |
| 319 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 320 | assert(K != tok::pp_not_keyword); |
| 321 | ParsingPreprocessorDirective = true; |
| 322 | |
| 323 | switch (K) { |
| 324 | default: |
| 325 | break; |
| 326 | case tok::pp_include: |
| 327 | case tok::pp_import: |
| 328 | case tok::pp_include_next: { |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 329 | // Save the 'include' token. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 330 | EmitToken(Tok); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 331 | // Lex the next token as an include string. |
| 332 | L.setParsingPreprocessorDirective(true); |
| 333 | L.LexIncludeFilename(Tok); |
| 334 | L.setParsingPreprocessorDirective(false); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 335 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 336 | if (Tok.is(tok::identifier)) |
| 337 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 338 | |
| 339 | break; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 340 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 341 | case tok::pp_if: |
| 342 | case tok::pp_ifdef: |
| 343 | case tok::pp_ifndef: { |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 344 | // Ad an entry for '#if' and friends. We initially set the target index |
| 345 | // to 0. This will get backpatched when we hit #endif. |
| 346 | PPStartCond.push_back(PPCond.size()); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 347 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 348 | break; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 349 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 350 | case tok::pp_endif: { |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 351 | // 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] | 352 | // This will later be set to zero when emitting to the PTH file. We |
| 353 | // use 0 for uninitialized indices because that is easier to debug. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 354 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 355 | // Backpatch the opening '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 356 | assert(!PPStartCond.empty()); |
| 357 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 358 | assert(PPCond[PPStartCond.back()].second == 0); |
| 359 | PPCond[PPStartCond.back()].second = index; |
| 360 | PPStartCond.pop_back(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 361 | // Add the new entry to PPCond. |
| 362 | PPCond.push_back(std::make_pair(HashOff, index)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 363 | break; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 365 | case tok::pp_elif: |
| 366 | case tok::pp_else: { |
| 367 | // Add an entry for #elif or #else. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 368 | // This serves as both a closing and opening of a conditional block. |
| 369 | // This means that its entry will get backpatched later. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 370 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 371 | // Backpatch the previous '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 372 | assert(!PPStartCond.empty()); |
| 373 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 374 | assert(PPCond[PPStartCond.back()].second == 0); |
| 375 | PPCond[PPStartCond.back()].second = index; |
| 376 | PPStartCond.pop_back(); |
| 377 | // Now add '#elif' as a new block opening. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 378 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
| 379 | PPStartCond.push_back(index); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 380 | break; |
| 381 | } |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 382 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 385 | while (EmitToken(Tok), Tok.isNot(tok::eof)); |
| 386 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 387 | assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals."); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 389 | // Next write out PPCond. |
| 390 | Offset PPCondOff = (Offset) Out.tell(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 391 | |
| 392 | // 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] | 393 | Emit32(PPCond.size()); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 395 | for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) { |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 396 | Emit32(PPCond[i].first - off); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 397 | uint32_t x = PPCond[i].second; |
| 398 | assert(x != 0 && "PPCond entry not backpatched."); |
| 399 | // Emit zero for #endifs. This allows us to do checking when |
| 400 | // we read the PTH file back in. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 401 | Emit32(x == i ? 0 : x); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 404 | return PCHEntry(off, PPCondOff); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 407 | Offset PTHWriter::EmitCachedSpellings() { |
| 408 | // Write each cached strings to the PTH file. |
| 409 | Offset SpellingsOff = Out.tell(); |
| 410 | |
| 411 | for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator |
| 412 | I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I) { |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 414 | const char* data = (*I)->getKeyData(); |
| 415 | EmitBuf(data, data + (*I)->getKeyLength()); |
| 416 | Emit8('\0'); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 419 | return SpellingsOff; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 420 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 422 | void PTHWriter::GeneratePTH() { |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 423 | // Generate the prologue. |
| 424 | Out << "cfe-pth"; |
Ted Kremenek | 67d1505 | 2009-01-26 21:50:21 +0000 | [diff] [blame] | 425 | Emit32(PTHManager::Version); |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 426 | Offset JumpOffset = Out.tell(); |
| 427 | Emit32(0); |
| 428 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 429 | // Iterate over all the files in SourceManager. Create a lexer |
| 430 | // for each file and cache the tokens. |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 431 | SourceManager &SM = PP.getSourceManager(); |
| 432 | const LangOptions &LOpts = PP.getLangOptions(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 433 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 434 | for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(), |
| 435 | E = SM.fileinfo_end(); I != E; ++I) { |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 436 | const SrcMgr::ContentCache &C = *I->second; |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 437 | const FileEntry *FE = C.Entry; |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 438 | |
| 439 | // FIXME: Handle files with non-absolute paths. |
| 440 | llvm::sys::Path P(FE->getName()); |
| 441 | if (!P.isAbsolute()) |
| 442 | continue; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 5c26385 | 2009-01-17 03:49:48 +0000 | [diff] [blame] | 444 | assert(!PM.count(FE) && "fileinfo's are not uniqued on FileEntry?"); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 445 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 446 | const llvm::MemoryBuffer *B = C.getBuffer(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 447 | if (!B) continue; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 448 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 449 | FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User); |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 450 | Lexer L(FID, SM, LOpts); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 451 | PM[FE] = LexTokens(L); |
Daniel Dunbar | 31309ab | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 452 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 453 | |
| 454 | // Write out the identifier table. |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 455 | const std::pair<Offset, std::pair<Offset,Offset> >& IdTableOff |
| 456 | = EmitIdentifierTable(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 457 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 458 | // Write out the cached strings table. |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 459 | Offset SpellingOff = EmitCachedSpellings(); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 460 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 461 | // Write out the file table. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 462 | Offset FileTableOff = EmitFileTable(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 463 | |
| 464 | // Finally, write out the offset table at the end. |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 465 | Offset JumpTargetOffset = Out.tell(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 466 | Emit32(IdTableOff.first); |
Ted Kremenek | 293b4af | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 467 | Emit32(IdTableOff.second.first); |
| 468 | Emit32(IdTableOff.second.second); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 469 | Emit32(FileTableOff); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 470 | Emit32(SpellingOff); |
Ted Kremenek | e1b6498 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 471 | |
| 472 | // Now write the offset in the prologue. |
| 473 | Out.seek(JumpOffset); |
| 474 | Emit32(JumpTargetOffset); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { |
| 478 | // Lex through the entire file. This will populate SourceManager with |
| 479 | // all of the header information. |
| 480 | Token Tok; |
| 481 | PP.EnterMainSourceFile(); |
| 482 | do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); |
| 483 | |
| 484 | // Open up the PTH file. |
| 485 | std::string ErrMsg; |
| 486 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
| 487 | |
| 488 | if (!ErrMsg.empty()) { |
| 489 | llvm::errs() << "PTH error: " << ErrMsg << "\n"; |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | // Create the PTHWriter and generate the PTH file. |
| 494 | PTHWriter PW(Out, PP); |
| 495 | PW.GeneratePTH(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 496 | } |