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" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 24 | #include "llvm/System/Path.h" |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 29 | typedef uint32_t Offset; |
| 30 | |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 31 | typedef llvm::DenseMap<const FileEntry*,std::pair<Offset,Offset> > PCHMap; |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 32 | typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 34 | namespace { |
| 35 | class VISIBILITY_HIDDEN PTHWriter { |
| 36 | IDMap IM; |
| 37 | llvm::raw_fd_ostream& Out; |
| 38 | Preprocessor& PP; |
| 39 | uint32_t idcount; |
| 40 | PCHMap PM; |
Ted Kremenek | d2c4abb | 2008-12-23 02:52:12 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 42 | //// Get the persistent id for the given IdentifierInfo*. |
| 43 | uint32_t ResolveID(const IdentifierInfo* II); |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 45 | /// Emit a token to the PTH file. |
| 46 | void EmitToken(const Token& T); |
| 47 | |
| 48 | void Emit8(uint32_t V) { |
| 49 | Out << (unsigned char)(V); |
| 50 | } |
| 51 | |
| 52 | void Emit16(uint32_t V) { |
| 53 | Out << (unsigned char)(V); |
| 54 | Out << (unsigned char)(V >> 8); |
| 55 | assert((V >> 16) == 0); |
| 56 | } |
| 57 | |
| 58 | void Emit24(uint32_t V) { |
| 59 | Out << (unsigned char)(V); |
| 60 | Out << (unsigned char)(V >> 8); |
| 61 | Out << (unsigned char)(V >> 16); |
| 62 | assert((V >> 24) == 0); |
| 63 | } |
| 64 | |
| 65 | void Emit32(uint32_t V) { |
| 66 | Out << (unsigned char)(V); |
| 67 | Out << (unsigned char)(V >> 8); |
| 68 | Out << (unsigned char)(V >> 16); |
| 69 | Out << (unsigned char)(V >> 24); |
| 70 | } |
| 71 | |
| 72 | void EmitBuf(const char* I, const char* E) { |
| 73 | for ( ; I != E ; ++I) Out << *I; |
| 74 | } |
| 75 | |
| 76 | std::pair<Offset,Offset> EmitIdentifierTable(); |
| 77 | Offset EmitFileTable(); |
| 78 | std::pair<Offset,Offset> LexTokens(Lexer& L); |
| 79 | |
| 80 | public: |
| 81 | PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp) |
| 82 | : Out(out), PP(pp), idcount(0) {} |
| 83 | |
| 84 | void GeneratePTH(); |
| 85 | }; |
| 86 | } // end anonymous namespace |
| 87 | |
| 88 | uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) { |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 89 | // Null IdentifierInfo's map to the persistent ID 0. |
| 90 | if (!II) |
| 91 | return 0; |
| 92 | |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 93 | IDMap::iterator I = IM.find(II); |
| 94 | |
| 95 | if (I == IM.end()) { |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 96 | IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL. |
| 97 | return idcount; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 100 | return I->second; // We've already added 1. |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 103 | void PTHWriter::EmitToken(const Token& T) { |
| 104 | uint32_t fpos = PP.getSourceManager().getFullFilePos(T.getLocation()); |
| 105 | Emit8(T.getKind()); |
| 106 | Emit8(T.getFlags()); |
| 107 | Emit24(ResolveID(T.getIdentifierInfo())); |
| 108 | Emit32(fpos); |
| 109 | Emit16(T.getLength()); |
| 110 | |
| 111 | #if 0 |
| 112 | // For specific tokens we cache their spelling. |
| 113 | if (T.getIdentifierInfo()) |
| 114 | return; |
| 115 | |
| 116 | switch (T.getKind()) { |
| 117 | default: |
| 118 | break; |
| 119 | case tok::string_literal: |
| 120 | case tok::wide_string_literal: |
| 121 | case tok::angle_string_literal: |
| 122 | case tok::numeric_constant: |
| 123 | case tok::char_constant: |
| 124 | CacheSpelling(T, fpos); |
| 125 | break; |
| 126 | } |
| 127 | #endif |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 130 | namespace { |
| 131 | struct VISIBILITY_HIDDEN IDData { |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 132 | const IdentifierInfo* II; |
| 133 | uint32_t FileOffset; |
| 134 | const IdentifierTable::const_iterator::value_type* Str; |
| 135 | }; |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 136 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 138 | std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() { |
| 139 | |
| 140 | const IdentifierTable& T = PP.getIdentifierTable(); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 141 | |
| 142 | // Build an inverse map from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 143 | typedef std::vector<IDData> InverseIDMap; |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 144 | InverseIDMap IIDMap; |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 145 | IIDMap.resize(idcount); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 147 | // Generate mapping from persistent IDs -> IdentifierInfo*. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 148 | for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) { |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 149 | // Decrement by 1 because we are using a vector for the lookup and |
| 150 | // 0 is reserved for NULL. |
| 151 | assert(I->second > 0); |
| 152 | assert(I->second-1 < IIDMap.size()); |
| 153 | IIDMap[I->second-1].II = I->first; |
| 154 | } |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 155 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 156 | // Get the string data associated with the IdentifierInfo. |
| 157 | for (IdentifierTable::const_iterator I=T.begin(), E=T.end(); I!=E; ++I) { |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 158 | IDMap::iterator IDI = IM.find(&(I->getValue())); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 159 | if (IDI == IM.end()) continue; |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 160 | IIDMap[IDI->second-1].Str = &(*I); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 163 | Offset DataOff = Out.tell(); |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 165 | for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) { |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 166 | // Record the location for this data. |
| 167 | I->FileOffset = Out.tell(); |
| 168 | // Write out the keyword. |
| 169 | unsigned len = I->Str->getKeyLength(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 170 | Emit32(len); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 171 | const char* buf = I->Str->getKeyData(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 172 | EmitBuf(buf, buf+len); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 173 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 175 | // Now emit the table mapping from persistent IDs to PTH file offsets. |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 176 | Offset IDOff = Out.tell(); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 178 | // Emit the number of identifiers. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 179 | Emit32(idcount); |
Ted Kremenek | db4c8e8 | 2008-12-03 01:16:39 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 181 | for (InverseIDMap::iterator I=IIDMap.begin(), E=IIDMap.end(); I!=E; ++I) |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 182 | Emit32(I->FileOffset); |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 184 | return std::make_pair(DataOff, IDOff); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 187 | Offset PTHWriter::EmitFileTable() { |
| 188 | // Determine the offset where this table appears in the PTH file. |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 189 | Offset off = (Offset) Out.tell(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 190 | |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 191 | // Output the size of the table. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 192 | Emit32(PM.size()); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 193 | |
| 194 | for (PCHMap::iterator I=PM.begin(), E=PM.end(); I!=E; ++I) { |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 195 | const FileEntry* FE = I->first; |
Ted Kremenek | 1109055 | 2008-12-04 22:36:44 +0000 | [diff] [blame] | 196 | const char* Name = FE->getName(); |
| 197 | unsigned size = strlen(Name); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 198 | Emit32(size); |
| 199 | EmitBuf(Name, Name+size); |
| 200 | Emit32(I->second.first); |
| 201 | Emit32(I->second.second); |
Ted Kremenek | 3e0bb5b | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 204 | return off; |
| 205 | } |
| 206 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 207 | std::pair<Offset,Offset> PTHWriter::LexTokens(Lexer& L) { |
| 208 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 209 | // Record the location within the token file. |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 210 | Offset off = (Offset) Out.tell(); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 211 | |
| 212 | // Keep track of matching '#if' ... '#endif'. |
| 213 | typedef std::vector<std::pair<Offset, unsigned> > PPCondTable; |
| 214 | PPCondTable PPCond; |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 215 | std::vector<unsigned> PPStartCond; |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 216 | bool ParsingPreprocessorDirective = false; |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 217 | |
| 218 | Token Tok; |
| 219 | |
| 220 | do { |
| 221 | L.LexFromRawLexer(Tok); |
| 222 | |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 223 | if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) && |
| 224 | ParsingPreprocessorDirective) { |
| 225 | // Insert an eom token into the token cache. It has the same |
| 226 | // position as the next token that is not on the same line as the |
| 227 | // preprocessor directive. Observe that we continue processing |
| 228 | // 'Tok' when we exit this branch. |
| 229 | Token Tmp = Tok; |
| 230 | Tmp.setKind(tok::eom); |
| 231 | Tmp.clearFlag(Token::StartOfLine); |
| 232 | Tmp.setIdentifierInfo(0); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 233 | EmitToken(Tmp); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 234 | ParsingPreprocessorDirective = false; |
| 235 | } |
| 236 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 237 | if (Tok.is(tok::identifier)) { |
| 238 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 239 | continue; |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 240 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 241 | |
| 242 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 243 | // Special processing for #include. Store the '#' token and lex |
| 244 | // the next token. |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 245 | assert(!ParsingPreprocessorDirective); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 246 | Offset HashOff = (Offset) Out.tell(); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 247 | EmitToken(Tok); |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 248 | |
| 249 | // Get the next token. |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 250 | L.LexFromRawLexer(Tok); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 251 | |
| 252 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 253 | |
| 254 | // Did we see 'include'/'import'/'include_next'? |
| 255 | if (!Tok.is(tok::identifier)) |
| 256 | continue; |
| 257 | |
| 258 | IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok); |
| 259 | Tok.setIdentifierInfo(II); |
| 260 | tok::PPKeywordKind K = II->getPPKeywordID(); |
| 261 | |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 262 | assert(K != tok::pp_not_keyword); |
| 263 | ParsingPreprocessorDirective = true; |
| 264 | |
| 265 | switch (K) { |
| 266 | default: |
| 267 | break; |
| 268 | case tok::pp_include: |
| 269 | case tok::pp_import: |
| 270 | case tok::pp_include_next: { |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 271 | // Save the 'include' token. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 272 | EmitToken(Tok); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 273 | // Lex the next token as an include string. |
| 274 | L.setParsingPreprocessorDirective(true); |
| 275 | L.LexIncludeFilename(Tok); |
| 276 | L.setParsingPreprocessorDirective(false); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 277 | assert(!Tok.isAtStartOfLine()); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 278 | if (Tok.is(tok::identifier)) |
| 279 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 280 | |
| 281 | break; |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 282 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 283 | case tok::pp_if: |
| 284 | case tok::pp_ifdef: |
| 285 | case tok::pp_ifndef: { |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 286 | // Ad an entry for '#if' and friends. We initially set the target index |
| 287 | // to 0. This will get backpatched when we hit #endif. |
| 288 | PPStartCond.push_back(PPCond.size()); |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 289 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 290 | break; |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 291 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 292 | case tok::pp_endif: { |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 293 | // 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] | 294 | // This will later be set to zero when emitting to the PTH file. We |
| 295 | // use 0 for uninitialized indices because that is easier to debug. |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 296 | unsigned index = PPCond.size(); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 297 | // Backpatch the opening '#if' entry. |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 298 | assert(!PPStartCond.empty()); |
| 299 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 300 | assert(PPCond[PPStartCond.back()].second == 0); |
| 301 | PPCond[PPStartCond.back()].second = index; |
| 302 | PPStartCond.pop_back(); |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 303 | // Add the new entry to PPCond. |
| 304 | PPCond.push_back(std::make_pair(HashOff, index)); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 305 | break; |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 306 | } |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 307 | case tok::pp_elif: |
| 308 | case tok::pp_else: { |
| 309 | // Add an entry for #elif or #else. |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 310 | // This serves as both a closing and opening of a conditional block. |
| 311 | // This means that its entry will get backpatched later. |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 312 | unsigned index = PPCond.size(); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 313 | // Backpatch the previous '#if' entry. |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 314 | assert(!PPStartCond.empty()); |
| 315 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 316 | assert(PPCond[PPStartCond.back()].second == 0); |
| 317 | PPCond[PPStartCond.back()].second = index; |
| 318 | PPStartCond.pop_back(); |
| 319 | // Now add '#elif' as a new block opening. |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 320 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
| 321 | PPStartCond.push_back(index); |
Ted Kremenek | 9ab79bf | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 324 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 327 | while (EmitToken(Tok), Tok.isNot(tok::eof)); |
| 328 | |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 329 | assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals."); |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 330 | |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 331 | // Next write out PPCond. |
| 332 | Offset PPCondOff = (Offset) Out.tell(); |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 333 | |
| 334 | // 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^] | 335 | Emit32(PPCond.size()); |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 337 | for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) { |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 338 | Emit32(PPCond[i].first - off); |
Ted Kremenek | 0b5038d | 2008-12-12 18:31:09 +0000 | [diff] [blame] | 339 | uint32_t x = PPCond[i].second; |
| 340 | assert(x != 0 && "PPCond entry not backpatched."); |
| 341 | // Emit zero for #endifs. This allows us to do checking when |
| 342 | // we read the PTH file back in. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 343 | Emit32(x == i ? 0 : x); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return std::make_pair(off,PPCondOff); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 347 | } |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 349 | void PTHWriter::GeneratePTH() { |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 350 | // Iterate over all the files in SourceManager. Create a lexer |
| 351 | // for each file and cache the tokens. |
| 352 | SourceManager& SM = PP.getSourceManager(); |
| 353 | const LangOptions& LOpts = PP.getLangOptions(); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 354 | |
| 355 | for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end(); |
| 356 | I!=E; ++I) { |
| 357 | |
| 358 | const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache(); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 359 | if (!C) continue; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 361 | const FileEntry* FE = C->Entry; // Does this entry correspond to a file? |
| 362 | if (!FE) continue; |
Ted Kremenek | 62739d6 | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 363 | |
| 364 | // FIXME: Handle files with non-absolute paths. |
| 365 | llvm::sys::Path P(FE->getName()); |
| 366 | if (!P.isAbsolute()) |
| 367 | continue; |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 369 | PCHMap::iterator PI = PM.find(FE); // Have we already processed this file? |
| 370 | if (PI != PM.end()) continue; |
| 371 | |
Chris Lattner | 5331d91 | 2009-01-06 04:47:20 +0000 | [diff] [blame] | 372 | const llvm::MemoryBuffer* B = C->getBuffer(); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 373 | if (!B) continue; |
| 374 | |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 375 | Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts, |
| 376 | B->getBufferStart(), B->getBufferEnd(), B); |
Ted Kremenek | 8309c92 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 377 | |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 378 | PM[FE] = LexTokens(L); |
Daniel Dunbar | eee6d10 | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 379 | } |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 380 | |
| 381 | // Write out the identifier table. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 382 | std::pair<Offset,Offset> IdTableOff = EmitIdentifierTable(); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 383 | |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 384 | // Write out the file table. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 385 | Offset FileTableOff = EmitFileTable(); |
Ted Kremenek | d330ec1 | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 386 | |
| 387 | // Finally, write out the offset table at the end. |
Ted Kremenek | 4029188 | 2009-01-08 01:17:37 +0000 | [diff] [blame^] | 388 | Emit32(IdTableOff.first); |
| 389 | Emit32(IdTableOff.second); |
| 390 | Emit32(FileTableOff); |
| 391 | } |
| 392 | |
| 393 | void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { |
| 394 | // Lex through the entire file. This will populate SourceManager with |
| 395 | // all of the header information. |
| 396 | Token Tok; |
| 397 | PP.EnterMainSourceFile(); |
| 398 | do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); |
| 399 | |
| 400 | // Open up the PTH file. |
| 401 | std::string ErrMsg; |
| 402 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
| 403 | |
| 404 | if (!ErrMsg.empty()) { |
| 405 | llvm::errs() << "PTH error: " << ErrMsg << "\n"; |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | // Create the PTHWriter and generate the PTH file. |
| 410 | PTHWriter PW(Out, PP); |
| 411 | PW.GeneratePTH(); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 412 | } |