Ted Kremenek | 71c6cc6 | 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 | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 25 | #include "llvm/System/Path.h" |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 31 | typedef uint32_t Offset; |
| 32 | |
Ted Kremenek | e98da52 | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 33 | typedef std::vector<std::pair<Offset, llvm::StringMapEntry<Offset>*> > |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 34 | SpellMapTy; |
| 35 | |
| 36 | namespace { |
| 37 | class VISIBILITY_HIDDEN PCHEntry { |
| 38 | Offset TokenData, PPCondData; |
| 39 | union { Offset SpellingOff; SpellMapTy* Spellings; }; |
| 40 | |
| 41 | public: |
| 42 | PCHEntry() {} |
| 43 | |
| 44 | PCHEntry(Offset td, Offset ppcd, SpellMapTy* sp) |
| 45 | : TokenData(td), PPCondData(ppcd), Spellings(sp) {} |
| 46 | |
| 47 | Offset getTokenOffset() const { return TokenData; } |
| 48 | Offset getPPCondTableOffset() const { return PPCondData; } |
| 49 | SpellMapTy& getSpellings() const { return *Spellings; } |
| 50 | |
| 51 | void setSpellingTableOffset(Offset off) { SpellingOff = off; } |
| 52 | Offset getSpellingTableOffset() const { return SpellingOff; } |
| 53 | |
| 54 | }; |
| 55 | } // end anonymous namespace |
| 56 | |
| 57 | typedef llvm::DenseMap<const FileEntry*, PCHEntry> PCHMap; |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 58 | typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap; |
Ted Kremenek | e98da52 | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 59 | typedef llvm::StringMap<Offset, llvm::BumpPtrAllocator> CachedStrsTy; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 61 | namespace { |
| 62 | class VISIBILITY_HIDDEN PTHWriter { |
| 63 | IDMap IM; |
| 64 | llvm::raw_fd_ostream& Out; |
| 65 | Preprocessor& PP; |
| 66 | uint32_t idcount; |
| 67 | PCHMap PM; |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 68 | CachedStrsTy CachedStrs; |
| 69 | |
| 70 | SpellMapTy* CurSpellMap; |
Ted Kremenek | d2c4abb | 2008-12-23 02:52:12 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 72 | //// Get the persistent id for the given IdentifierInfo*. |
| 73 | uint32_t ResolveID(const IdentifierInfo* II); |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 75 | /// Emit a token to the PTH file. |
| 76 | void EmitToken(const Token& T); |
| 77 | |
| 78 | void Emit8(uint32_t V) { |
| 79 | Out << (unsigned char)(V); |
| 80 | } |
| 81 | |
| 82 | void Emit16(uint32_t V) { |
| 83 | Out << (unsigned char)(V); |
| 84 | Out << (unsigned char)(V >> 8); |
| 85 | assert((V >> 16) == 0); |
| 86 | } |
| 87 | |
| 88 | void Emit24(uint32_t V) { |
| 89 | Out << (unsigned char)(V); |
| 90 | Out << (unsigned char)(V >> 8); |
| 91 | Out << (unsigned char)(V >> 16); |
| 92 | assert((V >> 24) == 0); |
| 93 | } |
| 94 | |
| 95 | void Emit32(uint32_t V) { |
| 96 | Out << (unsigned char)(V); |
| 97 | Out << (unsigned char)(V >> 8); |
| 98 | Out << (unsigned char)(V >> 16); |
| 99 | Out << (unsigned char)(V >> 24); |
| 100 | } |
| 101 | |
| 102 | void EmitBuf(const char* I, const char* E) { |
| 103 | for ( ; I != E ; ++I) Out << *I; |
| 104 | } |
| 105 | |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 106 | std::pair<Offset,std::pair<Offset, Offset> > EmitIdentifierTable(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 107 | Offset EmitFileTable(); |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 108 | PCHEntry LexTokens(Lexer& L); |
| 109 | void EmitCachedSpellings(); |
| 110 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 111 | public: |
| 112 | PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp) |
| 113 | : Out(out), PP(pp), idcount(0) {} |
| 114 | |
| 115 | void GeneratePTH(); |
| 116 | }; |
| 117 | } // end anonymous namespace |
| 118 | |
| 119 | uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) { |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 120 | // Null IdentifierInfo's map to the persistent ID 0. |
| 121 | if (!II) |
| 122 | return 0; |
| 123 | |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 124 | IDMap::iterator I = IM.find(II); |
| 125 | |
| 126 | if (I == IM.end()) { |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 127 | IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL. |
| 128 | return idcount; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 131 | return I->second; // We've already added 1. |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 134 | void PTHWriter::EmitToken(const Token& T) { |
| 135 | uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation()); |
Ted Kremenek | b8344ef | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 136 | |
| 137 | Emit32(((uint32_t) T.getKind()) | |
| 138 | (((uint32_t) T.getFlags()) << 8) | |
| 139 | (((uint32_t) T.getLength()) << 16)); |
| 140 | Emit32(ResolveID(T.getIdentifierInfo())); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 141 | Emit32(fpos); |
Ted Kremenek | b8344ef | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 6ad1f50 | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 143 | // Literals (strings, numbers, characters) get cached spellings. |
| 144 | if (T.isLiteral()) { |
| 145 | // FIXME: This uses the slow getSpelling(). Perhaps we do better |
| 146 | // in the future? This only slows down PTH generation. |
| 147 | const std::string &spelling = PP.getSpelling(T); |
| 148 | const char* s = spelling.c_str(); |
| 149 | |
| 150 | // Get the string entry. |
| 151 | llvm::StringMapEntry<Offset> *E = |
| 152 | &CachedStrs.GetOrCreateValue(s, s+spelling.size()); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 6ad1f50 | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 154 | // Store the address of the string entry in our spelling map. |
| 155 | CurSpellMap->push_back(std::make_pair(fpos, E)); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 156 | } |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 159 | namespace { |
| 160 | struct VISIBILITY_HIDDEN IDData { |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 161 | const IdentifierInfo* II; |
| 162 | uint32_t FileOffset; |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | class VISIBILITY_HIDDEN CompareIDDataIndex { |
| 166 | IDData* Table; |
| 167 | public: |
| 168 | CompareIDDataIndex(IDData* table) : Table(table) {} |
| 169 | |
| 170 | bool operator()(unsigned i, unsigned j) const { |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 171 | const IdentifierInfo* II_i = Table[i].II; |
| 172 | const IdentifierInfo* II_j = Table[j].II; |
| 173 | |
| 174 | unsigned i_len = II_i->getLength(); |
| 175 | unsigned j_len = II_j->getLength(); |
| 176 | |
| 177 | if (i_len > j_len) |
| 178 | return false; |
| 179 | |
| 180 | if (i_len < j_len) |
| 181 | return true; |
| 182 | |
| 183 | // Otherwise, compare the strings themselves! |
| 184 | return strncmp(II_i->getName(), II_j->getName(), i_len) < 0; |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 185 | } |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 186 | }; |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 187 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 189 | std::pair<Offset,std::pair<Offset,Offset> > |
| 190 | PTHWriter::EmitIdentifierTable() { |
| 191 | llvm::BumpPtrAllocator Alloc; |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 192 | |
| 193 | // Build an inverse map from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 194 | IDData* IIDMap = Alloc.Allocate<IDData>(idcount); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 196 | // Generate mapping from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 197 | for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) { |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 198 | // Decrement by 1 because we are using a vector for the lookup and |
| 199 | // 0 is reserved for NULL. |
| 200 | assert(I->second > 0); |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 201 | assert(I->second-1 < idcount); |
| 202 | unsigned idx = I->second-1; |
| 203 | IIDMap[idx].II = I->first; |
| 204 | } |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 206 | // We want to write out the strings in lexical order to support binary |
| 207 | // search of strings to identifiers. Create such a table. |
| 208 | unsigned *LexicalOrder = Alloc.Allocate<unsigned>(idcount); |
| 209 | for (unsigned i = 0; i < idcount ; ++i ) LexicalOrder[i] = i; |
| 210 | std::sort(LexicalOrder, LexicalOrder+idcount, CompareIDDataIndex(IIDMap)); |
| 211 | |
| 212 | // Write out the lexically-sorted table of persistent ids. |
| 213 | Offset LexicalOff = Out.tell(); |
| 214 | for (unsigned i = 0; i < idcount ; ++i) Emit32(LexicalOrder[i]); |
| 215 | |
| 216 | // Write out the string data itself. |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 217 | Offset DataOff = Out.tell(); |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 219 | for (unsigned i = 0; i < idcount; ++i) { |
| 220 | IDData& d = IIDMap[i]; |
| 221 | d.FileOffset = Out.tell(); // Record the location for this data. |
| 222 | unsigned len = d.II->getLength(); // Write out the string length. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 223 | Emit32(len); |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 224 | const char* buf = d.II->getName(); // Write out the string data. |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 225 | EmitBuf(buf, buf+len); |
| 226 | // Emit a null character for those clients expecting that IdentifierInfo |
| 227 | // strings are null terminated. |
| 228 | Emit8('\0'); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 229 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 231 | // Now emit the table mapping from persistent IDs to PTH file offsets. |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 232 | Offset IDOff = Out.tell(); |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 233 | Emit32(idcount); // Emit the number of identifiers. |
| 234 | for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset); |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 236 | return std::make_pair(DataOff, std::make_pair(IDOff, LexicalOff)); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 239 | Offset PTHWriter::EmitFileTable() { |
| 240 | // Determine the offset where this table appears in the PTH file. |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 241 | Offset off = (Offset) Out.tell(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 243 | // Output the size of the table. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 244 | Emit32(PM.size()); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 245 | |
| 246 | for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) { |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 247 | const FileEntry* FE = I->first; |
Ted Kremenek | 1109055 | 2008-12-04 22:36:44 +0000 | [diff] [blame] | 248 | const char* Name = FE->getName(); |
| 249 | unsigned size = strlen(Name); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 250 | Emit32(size); |
| 251 | EmitBuf(Name, Name+size); |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 252 | Emit32(I->second.getTokenOffset()); |
| 253 | Emit32(I->second.getPPCondTableOffset()); |
| 254 | Emit32(I->second.getSpellingTableOffset()); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 257 | return off; |
| 258 | } |
| 259 | |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 260 | PCHEntry PTHWriter::LexTokens(Lexer& L) { |
Ted Kremenek | b8344ef | 2009-01-19 23:13:15 +0000 | [diff] [blame] | 261 | // Pad 0's so that we emit tokens to a 4-byte alignment. |
| 262 | // This speed up reading them back in. |
| 263 | Offset off = (Offset) Out.tell(); |
| 264 | for (unsigned Pad = off % 4 ; Pad != 0 ; --Pad, ++off) Emit8(0); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 265 | |
| 266 | // Keep track of matching '#if' ... '#endif'. |
| 267 | typedef std::vector<std::pair<Offset, unsigned> > PPCondTable; |
| 268 | PPCondTable PPCond; |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 269 | std::vector<unsigned> PPStartCond; |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 270 | bool ParsingPreprocessorDirective = false; |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 272 | // Allocate a spelling map for this source file. |
| 273 | llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy()); |
| 274 | CurSpellMap = Spellings.get(); |
| 275 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 276 | Token Tok; |
| 277 | |
| 278 | do { |
| 279 | L.LexFromRawLexer(Tok); |
| 280 | |
Ted Kremenek | 9ab79bf | 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 | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 291 | EmitToken(Tmp); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 292 | ParsingPreprocessorDirective = false; |
| 293 | } |
| 294 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 295 | if (Tok.is(tok::identifier)) { |
| 296 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 297 | continue; |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 298 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 299 | |
| 300 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Ted Kremenek | d330ec1 | 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 | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 303 | assert(!ParsingPreprocessorDirective); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 304 | Offset HashOff = (Offset) Out.tell(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 305 | EmitToken(Tok); |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 306 | |
| 307 | // Get the next token. |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 308 | L.LexFromRawLexer(Tok); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 309 | |
| 310 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | d330ec1 | 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 | 9ab79bf | 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 | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 329 | // Save the 'include' token. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 330 | EmitToken(Tok); |
Ted Kremenek | d330ec1 | 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 | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 335 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 336 | if (Tok.is(tok::identifier)) |
| 337 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 338 | |
| 339 | break; |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 340 | } |
Ted Kremenek | 9ab79bf | 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 | 8309c92 | 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 | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 347 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 348 | break; |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 349 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 350 | case tok::pp_endif: { |
Ted Kremenek | 8309c92 | 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 | 0b5038d | 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 | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 354 | unsigned index = PPCond.size(); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 355 | // Backpatch the opening '#if' entry. |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 356 | assert(!PPStartCond.empty()); |
| 357 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | 8309c92 | 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 | 0b5038d | 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 | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 363 | break; |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | 9ab79bf | 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 | 0b5038d | 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 | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 370 | unsigned index = PPCond.size(); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 371 | // Backpatch the previous '#if' entry. |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 372 | assert(!PPStartCond.empty()); |
| 373 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | 8309c92 | 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 | 0b5038d | 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 | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 380 | break; |
| 381 | } |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 382 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 385 | while (EmitToken(Tok), Tok.isNot(tok::eof)); |
| 386 | |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 387 | assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals."); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 389 | // Next write out PPCond. |
| 390 | Offset PPCondOff = (Offset) Out.tell(); |
Ted Kremenek | 0b5038d | 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 | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 393 | Emit32(PPCond.size()); |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 395 | for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) { |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 396 | Emit32(PPCond[i].first - off); |
Ted Kremenek | 0b5038d | 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 | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 401 | Emit32(x == i ? 0 : x); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 404 | return PCHEntry(off, PPCondOff, Spellings.take()); |
| 405 | } |
| 406 | |
| 407 | void PTHWriter::EmitCachedSpellings() { |
| 408 | // Write each cached string to the PTH file and update the |
| 409 | // the string map entry to contain the relevant offset. |
| 410 | // |
| 411 | // FIXME: We can write the strings out in order of their frequency. This |
| 412 | // may result in better locality. |
| 413 | // |
| 414 | for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end(); |
| 415 | I!=E; ++I) { |
| 416 | |
| 417 | Offset off = Out.tell(); |
| 418 | |
| 419 | // Write out the length of the string before the string itself. |
| 420 | unsigned len = I->getKeyLength(); |
Ted Kremenek | e98da52 | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 421 | Emit16(len); |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 422 | |
| 423 | // Write out the string data. |
| 424 | const char* data = I->getKeyData(); |
| 425 | EmitBuf(data, data+len); |
| 426 | |
Ted Kremenek | e98da52 | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 427 | // Write out a single blank character. |
| 428 | Emit8(' '); |
| 429 | |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 430 | // Now patch the offset of the string in the PTH file into the string map. |
Ted Kremenek | e98da52 | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 431 | I->setValue(off); |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Now emit the spelling tables. |
| 435 | for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) { |
| 436 | SpellMapTy& spellings = I->second.getSpellings(); |
| 437 | I->second.setSpellingTableOffset(Out.tell()); |
| 438 | |
| 439 | // Write out the number of spellings. |
| 440 | unsigned n = spellings.size(); |
| 441 | Emit32(n); |
| 442 | |
| 443 | for (unsigned i = 0; i < n; ++i) { |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 444 | // Write out the offset of the token within the source file. |
| 445 | Emit32(spellings[i].first); |
| 446 | |
| 447 | // Write out the offset of the spelling data within the PTH file. |
Ted Kremenek | e98da52 | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 448 | Emit32(spellings[i].second->getValue()); |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Delete the spelling map for this source file. |
| 452 | delete &spellings; |
| 453 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 454 | } |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 455 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 456 | void PTHWriter::GeneratePTH() { |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 457 | // Generate the prologue. |
| 458 | Out << "cfe-pth"; |
Ted Kremenek | 169fc35 | 2009-01-26 21:50:21 +0000 | [diff] [blame^] | 459 | Emit32(PTHManager::Version); |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 460 | Offset JumpOffset = Out.tell(); |
| 461 | Emit32(0); |
| 462 | |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 463 | // Iterate over all the files in SourceManager. Create a lexer |
| 464 | // for each file and cache the tokens. |
Chris Lattner | ef63fd5 | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 465 | SourceManager &SM = PP.getSourceManager(); |
| 466 | const LangOptions &LOpts = PP.getLangOptions(); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 467 | |
Chris Lattner | ef63fd5 | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 468 | for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(), |
| 469 | E = SM.fileinfo_end(); I != E; ++I) { |
| 470 | const SrcMgr::ContentCache &C = *I; |
| 471 | const FileEntry *FE = C.Entry; |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 472 | |
| 473 | // FIXME: Handle files with non-absolute paths. |
| 474 | llvm::sys::Path P(FE->getName()); |
| 475 | if (!P.isAbsolute()) |
| 476 | continue; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 47d8268 | 2009-01-17 03:49:48 +0000 | [diff] [blame] | 478 | assert(!PM.count(FE) && "fileinfo's are not uniqued on FileEntry?"); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 479 | |
Chris Lattner | ef63fd5 | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 480 | const llvm::MemoryBuffer *B = C.getBuffer(); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 481 | if (!B) continue; |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 482 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 483 | FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User); |
Chris Lattner | c7b2359 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 484 | Lexer L(FID, SM, LOpts); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 485 | PM[FE] = LexTokens(L); |
Daniel Dunbar | eee6d10 | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 486 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 487 | |
| 488 | // Write out the identifier table. |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 489 | const std::pair<Offset, std::pair<Offset,Offset> >& IdTableOff |
| 490 | = EmitIdentifierTable(); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 2e39556 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 492 | // Write out the cached strings table. |
| 493 | EmitCachedSpellings(); |
| 494 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 495 | // Write out the file table. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 496 | Offset FileTableOff = EmitFileTable(); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 497 | |
| 498 | // Finally, write out the offset table at the end. |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 499 | Offset JumpTargetOffset = Out.tell(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 500 | Emit32(IdTableOff.first); |
Ted Kremenek | d4d6d22 | 2009-01-15 01:26:25 +0000 | [diff] [blame] | 501 | Emit32(IdTableOff.second.first); |
| 502 | Emit32(IdTableOff.second.second); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 503 | Emit32(FileTableOff); |
Ted Kremenek | 58b9f93 | 2009-01-26 21:43:14 +0000 | [diff] [blame] | 504 | |
| 505 | // Now write the offset in the prologue. |
| 506 | Out.seek(JumpOffset); |
| 507 | Emit32(JumpTargetOffset); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { |
| 511 | // Lex through the entire file. This will populate SourceManager with |
| 512 | // all of the header information. |
| 513 | Token Tok; |
| 514 | PP.EnterMainSourceFile(); |
| 515 | do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); |
| 516 | |
| 517 | // Open up the PTH file. |
| 518 | std::string ErrMsg; |
| 519 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
| 520 | |
| 521 | if (!ErrMsg.empty()) { |
| 522 | llvm::errs() << "PTH error: " << ErrMsg << "\n"; |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | // Create the PTHWriter and generate the PTH file. |
| 527 | PTHWriter PW(Out, PP); |
| 528 | PW.GeneratePTH(); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 529 | } |