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