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