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 | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 30 | typedef uint32_t Offset; |
| 31 | |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 32 | typedef std::vector<std::pair<Offset, llvm::StringMapEntry<Offset>*> > |
Ted Kremenek | be29533 | 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 | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 57 | typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap; |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 58 | typedef llvm::StringMap<Offset, llvm::BumpPtrAllocator> CachedStrsTy; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | b978c66 | 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 | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 67 | CachedStrsTy CachedStrs; |
| 68 | |
| 69 | SpellMapTy* CurSpellMap; |
Ted Kremenek | 8f174e1 | 2008-12-23 02:52:12 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | b978c66 | 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 | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | b978c66 | 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 | |
| 105 | std::pair<Offset,Offset> EmitIdentifierTable(); |
| 106 | Offset EmitFileTable(); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 107 | PCHEntry LexTokens(Lexer& L); |
| 108 | void EmitCachedSpellings(); |
| 109 | |
Ted Kremenek | b978c66 | 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 | fc7e2ea | 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 | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 123 | IDMap::iterator I = IM.find(II); |
| 124 | |
| 125 | if (I == IM.end()) { |
Ted Kremenek | b978c66 | 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 | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 130 | return I->second; // We've already added 1. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Ted Kremenek | b978c66 | 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 | b978c66 | 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 | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 146 | default: |
| 147 | break; |
| 148 | case tok::string_literal: |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 149 | case tok::wide_string_literal: |
| 150 | case tok::angle_string_literal: |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 151 | case tok::numeric_constant: |
Ted Kremenek | be29533 | 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 | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 155 | const std::string& spelling = PP.getSpelling(T); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 156 | const char* s = spelling.c_str(); |
| 157 | |
| 158 | // Get the string entry. |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 159 | llvm::StringMapEntry<Offset> *E = |
| 160 | &CachedStrs.GetOrCreateValue(s, s+spelling.size()); |
Ted Kremenek | be29533 | 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 | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 165 | break; |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 166 | } |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 167 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 170 | namespace { |
| 171 | struct VISIBILITY_HIDDEN IDData { |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 172 | const IdentifierInfo* II; |
| 173 | uint32_t FileOffset; |
| 174 | const IdentifierTable::const_iterator::value_type* Str; |
| 175 | }; |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 176 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 178 | std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() { |
| 179 | |
| 180 | const IdentifierTable& T = PP.getIdentifierTable(); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 181 | |
| 182 | // Build an inverse map from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 183 | typedef std::vector<IDData> InverseIDMap; |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 184 | InverseIDMap IIDMap; |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 185 | IIDMap.resize(idcount); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 187 | // Generate mapping from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 188 | for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) { |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 189 | // Decrement by 1 because we are using a vector for the lookup and |
| 190 | // 0 is reserved for NULL. |
| 191 | assert(I->second > 0); |
| 192 | assert(I->second-1 < IIDMap.size()); |
| 193 | IIDMap[I->second-1].II = I->first; |
| 194 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 196 | // Get the string data associated with the IdentifierInfo. |
| 197 | for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) { |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 198 | IDMap::iterator IDI = IM.find(&(I->getValue())); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 199 | if (IDI == IM.end()) continue; |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 200 | IIDMap[IDI->second-1].Str = &(*I); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 203 | Offset DataOff = Out.tell(); |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 205 | for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) { |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 206 | // Record the location for this data. |
| 207 | I->FileOffset = Out.tell(); |
| 208 | // Write out the keyword. |
| 209 | unsigned len = I->Str->getKeyLength(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 210 | Emit32(len); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 211 | const char* buf = I->Str->getKeyData(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 212 | EmitBuf(buf, buf+len); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 213 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 215 | // Now emit the table mapping from persistent IDs to PTH file offsets. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 216 | Offset IDOff = Out.tell(); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 218 | // Emit the number of identifiers. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 219 | Emit32(idcount); |
Ted Kremenek | 6183e48 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 221 | for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 222 | Emit32(I->FileOffset); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 224 | return std::make_pair(DataOff, IDOff); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 227 | Offset PTHWriter::EmitFileTable() { |
| 228 | // Determine the offset where this table appears in the PTH file. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 229 | Offset off = (Offset) Out.tell(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 231 | // Output the size of the table. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 232 | Emit32(PM.size()); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 233 | |
| 234 | for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) { |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 235 | const FileEntry* FE = I->first; |
Ted Kremenek | 8dffd9b | 2008-12-04 22:36:44 +0000 | [diff] [blame] | 236 | const char* Name = FE->getName(); |
| 237 | unsigned size = strlen(Name); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 238 | Emit32(size); |
| 239 | EmitBuf(Name, Name+size); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 240 | Emit32(I->second.getTokenOffset()); |
| 241 | Emit32(I->second.getPPCondTableOffset()); |
| 242 | Emit32(I->second.getSpellingTableOffset()); |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 245 | return off; |
| 246 | } |
| 247 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 248 | PCHEntry PTHWriter::LexTokens(Lexer& L) { |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 250 | // Record the location within the token file. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 251 | Offset off = (Offset) Out.tell(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 252 | |
| 253 | // Keep track of matching '#if' ... '#endif'. |
| 254 | typedef std::vector<std::pair<Offset, unsigned> > PPCondTable; |
| 255 | PPCondTable PPCond; |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 256 | std::vector<unsigned> PPStartCond; |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 257 | bool ParsingPreprocessorDirective = false; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 258 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 259 | // Allocate a spelling map for this source file. |
| 260 | llvm::OwningPtr<SpellMapTy> Spellings(new SpellMapTy()); |
| 261 | CurSpellMap = Spellings.get(); |
| 262 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 263 | Token Tok; |
| 264 | |
| 265 | do { |
| 266 | L.LexFromRawLexer(Tok); |
| 267 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 268 | if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) && |
| 269 | ParsingPreprocessorDirective) { |
| 270 | // Insert an eom token into the token cache. It has the same |
| 271 | // position as the next token that is not on the same line as the |
| 272 | // preprocessor directive. Observe that we continue processing |
| 273 | // 'Tok' when we exit this branch. |
| 274 | Token Tmp = Tok; |
| 275 | Tmp.setKind(tok::eom); |
| 276 | Tmp.clearFlag(Token::StartOfLine); |
| 277 | Tmp.setIdentifierInfo(0); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 278 | EmitToken(Tmp); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 279 | ParsingPreprocessorDirective = false; |
| 280 | } |
| 281 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 282 | if (Tok.is(tok::identifier)) { |
| 283 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 284 | continue; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 285 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 286 | |
| 287 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 288 | // Special processing for #include. Store the '#' token and lex |
| 289 | // the next token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 290 | assert(!ParsingPreprocessorDirective); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 291 | Offset HashOff = (Offset) Out.tell(); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 292 | EmitToken(Tok); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 293 | |
| 294 | // Get the next token. |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 295 | L.LexFromRawLexer(Tok); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 296 | |
| 297 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 298 | |
| 299 | // Did we see 'include'/'import'/'include_next'? |
| 300 | if (!Tok.is(tok::identifier)) |
| 301 | continue; |
| 302 | |
| 303 | IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok); |
| 304 | Tok.setIdentifierInfo(II); |
| 305 | tok::PPKeywordKind K = II->getPPKeywordID(); |
| 306 | |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 307 | assert(K != tok::pp_not_keyword); |
| 308 | ParsingPreprocessorDirective = true; |
| 309 | |
| 310 | switch (K) { |
| 311 | default: |
| 312 | break; |
| 313 | case tok::pp_include: |
| 314 | case tok::pp_import: |
| 315 | case tok::pp_include_next: { |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 316 | // Save the 'include' token. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 317 | EmitToken(Tok); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 318 | // Lex the next token as an include string. |
| 319 | L.setParsingPreprocessorDirective(true); |
| 320 | L.LexIncludeFilename(Tok); |
| 321 | L.setParsingPreprocessorDirective(false); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 322 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 323 | if (Tok.is(tok::identifier)) |
| 324 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 325 | |
| 326 | break; |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 327 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 328 | case tok::pp_if: |
| 329 | case tok::pp_ifdef: |
| 330 | case tok::pp_ifndef: { |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 331 | // Ad an entry for '#if' and friends. We initially set the target index |
| 332 | // to 0. This will get backpatched when we hit #endif. |
| 333 | PPStartCond.push_back(PPCond.size()); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 334 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 335 | break; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 336 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 337 | case tok::pp_endif: { |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 338 | // 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] | 339 | // This will later be set to zero when emitting to the PTH file. We |
| 340 | // use 0 for uninitialized indices because that is easier to debug. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 341 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 342 | // Backpatch the opening '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 343 | assert(!PPStartCond.empty()); |
| 344 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 345 | assert(PPCond[PPStartCond.back()].second == 0); |
| 346 | PPCond[PPStartCond.back()].second = index; |
| 347 | PPStartCond.pop_back(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 348 | // Add the new entry to PPCond. |
| 349 | PPCond.push_back(std::make_pair(HashOff, index)); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 350 | break; |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 351 | } |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 352 | case tok::pp_elif: |
| 353 | case tok::pp_else: { |
| 354 | // Add an entry for #elif or #else. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 355 | // This serves as both a closing and opening of a conditional block. |
| 356 | // This means that its entry will get backpatched later. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 357 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 358 | // Backpatch the previous '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 359 | assert(!PPStartCond.empty()); |
| 360 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 361 | assert(PPCond[PPStartCond.back()].second == 0); |
| 362 | PPCond[PPStartCond.back()].second = index; |
| 363 | PPStartCond.pop_back(); |
| 364 | // Now add '#elif' as a new block opening. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 365 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
| 366 | PPStartCond.push_back(index); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 367 | break; |
| 368 | } |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 369 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 372 | while (EmitToken(Tok), Tok.isNot(tok::eof)); |
| 373 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 374 | assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals."); |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 375 | |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 376 | // Next write out PPCond. |
| 377 | Offset PPCondOff = (Offset) Out.tell(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 378 | |
| 379 | // 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] | 380 | Emit32(PPCond.size()); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 381 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 382 | for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) { |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 383 | Emit32(PPCond[i].first - off); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 384 | uint32_t x = PPCond[i].second; |
| 385 | assert(x != 0 && "PPCond entry not backpatched."); |
| 386 | // Emit zero for #endifs. This allows us to do checking when |
| 387 | // we read the PTH file back in. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 388 | Emit32(x == i ? 0 : x); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 391 | return PCHEntry(off, PPCondOff, Spellings.take()); |
| 392 | } |
| 393 | |
| 394 | void PTHWriter::EmitCachedSpellings() { |
| 395 | // Write each cached string to the PTH file and update the |
| 396 | // the string map entry to contain the relevant offset. |
| 397 | // |
| 398 | // FIXME: We can write the strings out in order of their frequency. This |
| 399 | // may result in better locality. |
| 400 | // |
| 401 | for (CachedStrsTy::iterator I = CachedStrs.begin(), E = CachedStrs.end(); |
| 402 | I!=E; ++I) { |
| 403 | |
| 404 | Offset off = Out.tell(); |
| 405 | |
| 406 | // Write out the length of the string before the string itself. |
| 407 | unsigned len = I->getKeyLength(); |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 408 | Emit16(len); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 409 | |
| 410 | // Write out the string data. |
| 411 | const char* data = I->getKeyData(); |
| 412 | EmitBuf(data, data+len); |
| 413 | |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 414 | // Write out a single blank character. |
| 415 | Emit8(' '); |
| 416 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 417 | // Now patch the offset of the string in the PTH file into the string map. |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 418 | I->setValue(off); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // Now emit the spelling tables. |
| 422 | for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) { |
| 423 | SpellMapTy& spellings = I->second.getSpellings(); |
| 424 | I->second.setSpellingTableOffset(Out.tell()); |
| 425 | |
| 426 | // Write out the number of spellings. |
| 427 | unsigned n = spellings.size(); |
| 428 | Emit32(n); |
| 429 | |
| 430 | for (unsigned i = 0; i < n; ++i) { |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 431 | // Write out the offset of the token within the source file. |
| 432 | Emit32(spellings[i].first); |
| 433 | |
| 434 | // Write out the offset of the spelling data within the PTH file. |
Ted Kremenek | e338ccd | 2009-01-09 00:37:37 +0000 | [diff] [blame] | 435 | Emit32(spellings[i].second->getValue()); |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // Delete the spelling map for this source file. |
| 439 | delete &spellings; |
| 440 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 441 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 443 | void PTHWriter::GeneratePTH() { |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 444 | // Iterate over all the files in SourceManager. Create a lexer |
| 445 | // for each file and cache the tokens. |
| 446 | SourceManager& SM = PP.getSourceManager(); |
| 447 | const LangOptions& LOpts = PP.getLangOptions(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 448 | |
| 449 | for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end(); |
| 450 | I!=E; ++I) { |
| 451 | |
| 452 | const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 453 | if (!C) continue; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 455 | const FileEntry* FE = C->Entry; // Does this entry correspond to a file? |
| 456 | if (!FE) continue; |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 457 | |
| 458 | // FIXME: Handle files with non-absolute paths. |
| 459 | llvm::sys::Path P(FE->getName()); |
| 460 | if (!P.isAbsolute()) |
| 461 | continue; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 462 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 463 | PCHMap::iterator PI = PM.find(FE); // Have we already processed this file? |
| 464 | if (PI != PM.end()) continue; |
| 465 | |
Chris Lattner | 1b23014 | 2009-01-06 04:47:20 +0000 | [diff] [blame] | 466 | const llvm::MemoryBuffer* B = C->getBuffer(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 467 | if (!B) continue; |
| 468 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 469 | Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts, |
| 470 | B->getBufferStart(), B->getBufferEnd(), B); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 471 | |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 472 | PM[FE] = LexTokens(L); |
Daniel Dunbar | 31309ab | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 473 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 474 | |
| 475 | // Write out the identifier table. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 476 | std::pair<Offset,Offset> IdTableOff = EmitIdentifierTable(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 477 | |
Ted Kremenek | be29533 | 2009-01-08 02:44:06 +0000 | [diff] [blame] | 478 | // Write out the cached strings table. |
| 479 | EmitCachedSpellings(); |
| 480 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 481 | // Write out the file table. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 482 | Offset FileTableOff = EmitFileTable(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 483 | |
| 484 | // Finally, write out the offset table at the end. |
Ted Kremenek | b978c66 | 2009-01-08 01:17:37 +0000 | [diff] [blame] | 485 | Emit32(IdTableOff.first); |
| 486 | Emit32(IdTableOff.second); |
| 487 | Emit32(FileTableOff); |
| 488 | } |
| 489 | |
| 490 | void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { |
| 491 | // Lex through the entire file. This will populate SourceManager with |
| 492 | // all of the header information. |
| 493 | Token Tok; |
| 494 | PP.EnterMainSourceFile(); |
| 495 | do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); |
| 496 | |
| 497 | // Open up the PTH file. |
| 498 | std::string ErrMsg; |
| 499 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
| 500 | |
| 501 | if (!ErrMsg.empty()) { |
| 502 | llvm::errs() << "PTH error: " << ErrMsg << "\n"; |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | // Create the PTHWriter and generate the PTH file. |
| 507 | PTHWriter PW(Out, PP); |
| 508 | PW.GeneratePTH(); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 509 | } |