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