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