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 | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 162 | |
| 163 | Token Tok; |
| 164 | |
| 165 | do { |
| 166 | L.LexFromRawLexer(Tok); |
| 167 | |
| 168 | if (Tok.is(tok::identifier)) { |
| 169 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
| 170 | } |
| 171 | else if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 172 | // Special processing for #include. Store the '#' token and lex |
| 173 | // the next token. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 174 | Offset HashOff = (Offset) Out.tell(); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 175 | EmitToken(Out, Tok, SMgr, idcount, IM); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 176 | |
| 177 | // Get the next token. |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 178 | L.LexFromRawLexer(Tok); |
| 179 | |
| 180 | // Did we see 'include'/'import'/'include_next'? |
| 181 | if (!Tok.is(tok::identifier)) |
| 182 | continue; |
| 183 | |
| 184 | IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok); |
| 185 | Tok.setIdentifierInfo(II); |
| 186 | tok::PPKeywordKind K = II->getPPKeywordID(); |
| 187 | |
| 188 | if (K == tok::pp_include || K == tok::pp_import || |
| 189 | K == tok::pp_include_next) { |
| 190 | |
| 191 | // Save the 'include' token. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 192 | EmitToken(Out, Tok, SMgr, idcount, IM); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 193 | |
| 194 | // Lex the next token as an include string. |
| 195 | L.setParsingPreprocessorDirective(true); |
| 196 | L.LexIncludeFilename(Tok); |
| 197 | L.setParsingPreprocessorDirective(false); |
| 198 | |
| 199 | if (Tok.is(tok::identifier)) |
| 200 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
| 201 | } |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 202 | else if (K == tok::pp_if || K == tok::pp_ifdef || K == tok::pp_ifndef) { |
| 203 | // Ad an entry for '#if' and friends. We initially set the target index |
| 204 | // to 0. This will get backpatched when we hit #endif. |
| 205 | PPStartCond.push_back(PPCond.size()); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 206 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 207 | } |
| 208 | else if (K == tok::pp_endif) { |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 209 | // 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^] | 210 | // This will later be set to zero when emitting to the PTH file. We |
| 211 | // use 0 for uninitialized indices because that is easier to debug. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 212 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 213 | // Backpatch the opening '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 214 | assert(!PPStartCond.empty()); |
| 215 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 216 | assert(PPCond[PPStartCond.back()].second == 0); |
| 217 | PPCond[PPStartCond.back()].second = index; |
| 218 | PPStartCond.pop_back(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 219 | // Add the new entry to PPCond. |
| 220 | PPCond.push_back(std::make_pair(HashOff, index)); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 221 | } |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 222 | else if (K == tok::pp_elif || K == tok::pp_else) { |
| 223 | // Add an entry for '#elif' or '#else. |
| 224 | // This serves as both a closing and opening of a conditional block. |
| 225 | // This means that its entry will get backpatched later. |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 226 | unsigned index = PPCond.size(); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 227 | // Backpatch the previous '#if' entry. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 228 | assert(!PPStartCond.empty()); |
| 229 | assert(PPCond.size() > PPStartCond.back()); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 230 | assert(PPCond[PPStartCond.back()].second == 0); |
| 231 | PPCond[PPStartCond.back()].second = index; |
| 232 | PPStartCond.pop_back(); |
| 233 | // Now add '#elif' as a new block opening. |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 234 | PPCond.push_back(std::make_pair(HashOff, 0U)); |
| 235 | PPStartCond.push_back(index); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 236 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 239 | while (EmitToken(Out, Tok, SMgr, idcount, IM), Tok.isNot(tok::eof)); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 241 | assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals."); |
| 242 | |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 243 | // Next write out PPCond. |
| 244 | Offset PPCondOff = (Offset) Out.tell(); |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 245 | |
| 246 | // 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] | 247 | Emit32(Out, PPCond.size()); |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | dad7b34 | 2008-12-12 18:31:09 +0000 | [diff] [blame^] | 249 | for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) { |
| 250 | Emit32(Out, PPCond[i].first - off); |
| 251 | uint32_t x = PPCond[i].second; |
| 252 | assert(x != 0 && "PPCond entry not backpatched."); |
| 253 | // Emit zero for #endifs. This allows us to do checking when |
| 254 | // we read the PTH file back in. |
| 255 | Emit32(Out, x == i ? 0 : x); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return std::make_pair(off,PPCondOff); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 259 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 260 | |
| 261 | void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { |
| 262 | // Lex through the entire file. This will populate SourceManager with |
| 263 | // all of the header information. |
| 264 | Token Tok; |
| 265 | PP.EnterMainSourceFile(); |
| 266 | do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); |
| 267 | |
| 268 | // Iterate over all the files in SourceManager. Create a lexer |
| 269 | // for each file and cache the tokens. |
| 270 | SourceManager& SM = PP.getSourceManager(); |
| 271 | const LangOptions& LOpts = PP.getLangOptions(); |
| 272 | llvm::raw_ostream& os = llvm::errs(); |
| 273 | |
| 274 | PCHMap PM; |
| 275 | IDMap IM; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 276 | uint32_t idcount = 0; |
| 277 | |
| 278 | std::string ErrMsg; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 279 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 280 | |
| 281 | if (!ErrMsg.empty()) { |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 282 | os << "PTH error: " << ErrMsg << "\n"; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
| 286 | for (SourceManager::fileid_iterator I=SM.fileid_begin(), E=SM.fileid_end(); |
| 287 | I!=E; ++I) { |
| 288 | |
| 289 | const SrcMgr::ContentCache* C = I.getFileIDInfo().getContentCache(); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 290 | if (!C) continue; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 292 | const FileEntry* FE = C->Entry; // Does this entry correspond to a file? |
| 293 | if (!FE) continue; |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 294 | |
| 295 | // FIXME: Handle files with non-absolute paths. |
| 296 | llvm::sys::Path P(FE->getName()); |
| 297 | if (!P.isAbsolute()) |
| 298 | continue; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 299 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 300 | PCHMap::iterator PI = PM.find(FE); // Have we already processed this file? |
| 301 | if (PI != PM.end()) continue; |
| 302 | |
| 303 | const llvm::MemoryBuffer* B = C->Buffer; |
| 304 | if (!B) continue; |
| 305 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 306 | Lexer L(SourceLocation::getFileLoc(I.getFileID(), 0), LOpts, |
| 307 | B->getBufferStart(), B->getBufferEnd(), B); |
Ted Kremenek | fb645b6 | 2008-12-11 23:36:38 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 309 | PM[FE] = LexTokens(Out, L, PP, idcount, IM); |
Daniel Dunbar | 31309ab | 2008-11-26 02:18:33 +0000 | [diff] [blame] | 310 | } |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 311 | |
| 312 | // Write out the identifier table. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 313 | std::pair<Offset,Offset> IdTableOff = |
Ted Kremenek | fa59aad | 2008-11-26 23:58:26 +0000 | [diff] [blame] | 314 | EmitIdentifierTable(Out, idcount, PP.getIdentifierTable(), IM); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 316 | // Write out the file table. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 317 | Offset FileTableOff = EmitFileTable(Out, SM, PM); |
Ted Kremenek | a3d764c | 2008-11-26 03:36:26 +0000 | [diff] [blame] | 318 | |
| 319 | // Finally, write out the offset table at the end. |
Ted Kremenek | fc7e2ea | 2008-12-02 19:44:08 +0000 | [diff] [blame] | 320 | Emit32(Out, IdTableOff.first); |
| 321 | Emit32(Out, IdTableOff.second); |
| 322 | Emit32(Out, FileTableOff); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 323 | } |