Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- SourceManager.cpp - Track and cache source files -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SourceManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/SourceManager.h" |
| 15 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 17 | #include "llvm/System/Path.h" |
| 18 | #include <algorithm> |
| 19 | #include <iostream> |
Gabor Greif | ffc337b | 2007-07-12 16:00:00 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 21 | using namespace clang; |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame] | 22 | using namespace SrcMgr; |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 23 | using llvm::MemoryBuffer; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 24 | |
| 25 | SourceManager::~SourceManager() { |
| 26 | for (std::map<const FileEntry *, FileInfo>::iterator I = FileInfos.begin(), |
| 27 | E = FileInfos.end(); I != E; ++I) { |
| 28 | delete I->second.Buffer; |
| 29 | delete[] I->second.SourceLineCache; |
| 30 | } |
| 31 | |
| 32 | for (std::list<InfoRec>::iterator I = MemBufferInfos.begin(), |
| 33 | E = MemBufferInfos.end(); I != E; ++I) { |
| 34 | delete I->second.Buffer; |
| 35 | delete[] I->second.SourceLineCache; |
| 36 | } |
| 37 | } |
| 38 | |
Chris Lattner | e92976d | 2007-04-29 06:44:41 +0000 | [diff] [blame] | 39 | |
| 40 | // FIXME: REMOVE THESE |
| 41 | #include <unistd.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/uio.h> |
| 44 | #include <sys/fcntl.h> |
| 45 | #include <cerrno> |
| 46 | |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 47 | static const MemoryBuffer *ReadFileFast(const FileEntry *FileEnt) { |
Chris Lattner | e92976d | 2007-04-29 06:44:41 +0000 | [diff] [blame] | 48 | #if 0 |
| 49 | // FIXME: Reintroduce this and zap this function once the common llvm stuff |
| 50 | // is fast for the small case. |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 51 | return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()), |
Chris Lattner | e92976d | 2007-04-29 06:44:41 +0000 | [diff] [blame] | 52 | FileEnt->getSize()); |
| 53 | #endif |
| 54 | |
| 55 | // If the file is larger than some threshold, use 'read', otherwise use mmap. |
| 56 | if (FileEnt->getSize() >= 4096*4) |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 57 | return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()), |
Chris Lattner | 776050e | 2007-05-06 23:34:12 +0000 | [diff] [blame] | 58 | 0, FileEnt->getSize()); |
Chris Lattner | e92976d | 2007-04-29 06:44:41 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 60 | MemoryBuffer *SB = MemoryBuffer::getNewUninitMemBuffer(FileEnt->getSize(), |
Chris Lattner | e92976d | 2007-04-29 06:44:41 +0000 | [diff] [blame] | 61 | FileEnt->getName()); |
| 62 | char *BufPtr = const_cast<char*>(SB->getBufferStart()); |
| 63 | |
| 64 | int FD = ::open(FileEnt->getName(), O_RDONLY); |
| 65 | if (FD == -1) { |
| 66 | delete SB; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | unsigned BytesLeft = FileEnt->getSize(); |
| 71 | while (BytesLeft) { |
| 72 | ssize_t NumRead = ::read(FD, BufPtr, BytesLeft); |
| 73 | if (NumRead != -1) { |
| 74 | BytesLeft -= NumRead; |
| 75 | BufPtr += NumRead; |
| 76 | } else if (errno == EINTR) { |
| 77 | // try again |
| 78 | } else { |
| 79 | // error reading. |
| 80 | close(FD); |
| 81 | delete SB; |
| 82 | return 0; |
| 83 | } |
| 84 | } |
| 85 | close(FD); |
| 86 | |
| 87 | return SB; |
| 88 | } |
| 89 | |
| 90 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 91 | /// getFileInfo - Create or return a cached FileInfo for the specified file. |
| 92 | /// |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame] | 93 | const InfoRec * |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 94 | SourceManager::getInfoRec(const FileEntry *FileEnt) { |
| 95 | assert(FileEnt && "Didn't specify a file entry to use?"); |
| 96 | // Do we already have information about this file? |
| 97 | std::map<const FileEntry *, FileInfo>::iterator I = |
| 98 | FileInfos.lower_bound(FileEnt); |
| 99 | if (I != FileInfos.end() && I->first == FileEnt) |
| 100 | return &*I; |
| 101 | |
| 102 | // Nope, get information. |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 103 | const MemoryBuffer *File = ReadFileFast(FileEnt); |
Chris Lattner | 35f9985 | 2007-04-29 06:08:57 +0000 | [diff] [blame] | 104 | if (File == 0) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 105 | return 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 106 | |
| 107 | const InfoRec &Entry = |
| 108 | *FileInfos.insert(I, std::make_pair(FileEnt, FileInfo())); |
| 109 | FileInfo &Info = const_cast<FileInfo &>(Entry.second); |
| 110 | |
| 111 | Info.Buffer = File; |
| 112 | Info.SourceLineCache = 0; |
| 113 | Info.NumLines = 0; |
| 114 | return &Entry; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /// createMemBufferInfoRec - Create a new info record for the specified memory |
| 119 | /// buffer. This does no caching. |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame] | 120 | const InfoRec * |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 121 | SourceManager::createMemBufferInfoRec(const MemoryBuffer *Buffer) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 122 | // Add a new info record to the MemBufferInfos list and return it. |
| 123 | FileInfo FI; |
| 124 | FI.Buffer = Buffer; |
| 125 | FI.SourceLineCache = 0; |
| 126 | FI.NumLines = 0; |
| 127 | MemBufferInfos.push_back(InfoRec(0, FI)); |
| 128 | return &MemBufferInfos.back(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /// createFileID - Create a new fileID for the specified InfoRec and include |
| 133 | /// position. This works regardless of whether the InfoRec corresponds to a |
| 134 | /// file or some other input source. |
| 135 | unsigned SourceManager::createFileID(const InfoRec *File, |
| 136 | SourceLocation IncludePos) { |
| 137 | // If FileEnt is really large (e.g. it's a large .i file), we may not be able |
| 138 | // to fit an arbitrary position in the file in the FilePos field. To handle |
| 139 | // this, we create one FileID for each chunk of the file that fits in a |
| 140 | // FilePos field. |
| 141 | unsigned FileSize = File->second.Buffer->getBufferSize(); |
| 142 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) { |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 143 | FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File)); |
Chris Lattner | 2a904d0 | 2006-10-22 06:33:42 +0000 | [diff] [blame] | 144 | assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) && |
| 145 | "Ran out of file ID's!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 146 | return FileIDs.size(); |
| 147 | } |
| 148 | |
| 149 | // Create one FileID for each chunk of the file. |
| 150 | unsigned Result = FileIDs.size()+1; |
| 151 | |
| 152 | unsigned ChunkNo = 0; |
| 153 | while (1) { |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 154 | FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 155 | |
| 156 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break; |
| 157 | FileSize -= (1 << SourceLocation::FilePosBits); |
| 158 | } |
| 159 | |
Chris Lattner | 2a904d0 | 2006-10-22 06:33:42 +0000 | [diff] [blame] | 160 | assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) && |
| 161 | "Ran out of file ID's!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 162 | return Result; |
| 163 | } |
| 164 | |
Chris Lattner | 7d6a4f6 | 2006-06-30 06:10:08 +0000 | [diff] [blame] | 165 | /// getInstantiationLoc - Return a new SourceLocation that encodes the fact |
| 166 | /// that a token from physloc PhysLoc should actually be referenced from |
| 167 | /// InstantiationLoc. |
Chris Lattner | ca8ebc0 | 2007-07-20 18:00:12 +0000 | [diff] [blame^] | 168 | SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc, |
Chris Lattner | 7d6a4f6 | 2006-06-30 06:10:08 +0000 | [diff] [blame] | 169 | SourceLocation InstantLoc) { |
Chris Lattner | 3fc74e2 | 2007-07-15 06:35:27 +0000 | [diff] [blame] | 170 | // The specified source location may be a mapped location, due to a macro |
| 171 | // instantiation or #line directive. Strip off this information to find out |
| 172 | // where the characters are actually located. |
Chris Lattner | ca8ebc0 | 2007-07-20 18:00:12 +0000 | [diff] [blame^] | 173 | PhysLoc = getPhysicalLoc(PhysLoc); |
Chris Lattner | 351050b | 2006-07-16 18:05:08 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 4c37a8c | 2006-06-30 06:15:08 +0000 | [diff] [blame] | 175 | // Resolve InstantLoc down to a real logical location. |
| 176 | InstantLoc = getLogicalLoc(InstantLoc); |
Chris Lattner | 7fa8c88 | 2006-07-20 06:48:52 +0000 | [diff] [blame] | 177 | |
Chris Lattner | ca8ebc0 | 2007-07-20 18:00:12 +0000 | [diff] [blame^] | 178 | |
| 179 | // If the last macro id is close to the currently requested location, try to |
| 180 | // reuse it. This implements a single-entry cache. |
| 181 | if (!MacroIDs.empty()) { |
| 182 | MacroIDInfo &LastOne = MacroIDs.back(); |
| 183 | if (LastOne.getInstantiationLoc() == InstantLoc && |
| 184 | LastOne.getPhysicalLoc().getFileID() == PhysLoc.getFileID()) { |
| 185 | |
| 186 | int PhysDelta = PhysLoc.getRawFilePos() - |
| 187 | LastOne.getPhysicalLoc().getRawFilePos(); |
| 188 | if (unsigned(PhysDelta) < (1 << SourceLocation::MacroPhysOffsBits)) |
| 189 | return SourceLocation::getMacroLoc(MacroIDs.size()-1, |
| 190 | (unsigned)PhysDelta, 0); |
| 191 | |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 197 | // FIXME: intelligently cache macroid's. |
| 198 | MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc)); |
| 199 | |
| 200 | return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0, 0); |
| 201 | |
| 202 | #if 0 |
Chris Lattner | 7fa8c88 | 2006-07-20 06:48:52 +0000 | [diff] [blame] | 203 | unsigned InstantiationFileID; |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 7fa8c88 | 2006-07-20 06:48:52 +0000 | [diff] [blame] | 205 | // If this is the same instantiation as was requested last time, return this |
| 206 | // immediately. |
| 207 | if (PhysLoc.getFileID() == LastInstantiationLoc_MacroFID && |
| 208 | InstantLoc == LastInstantiationLoc_InstantLoc) { |
| 209 | InstantiationFileID = LastInstantiationLoc_Result; |
| 210 | } else { |
| 211 | // Add a FileID for this. FIXME: should cache these! |
| 212 | FileIDs.push_back(FileIDInfo::getMacroExpansion(InstantLoc, |
| 213 | PhysLoc.getFileID())); |
| 214 | InstantiationFileID = FileIDs.size(); |
| 215 | |
| 216 | // Remember this in the single-entry cache for next time. |
| 217 | LastInstantiationLoc_MacroFID = PhysLoc.getFileID(); |
| 218 | LastInstantiationLoc_InstantLoc = InstantLoc; |
| 219 | LastInstantiationLoc_Result = InstantiationFileID; |
| 220 | } |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 221 | return SourceLocation::getMacroLoc(InstantiationFileID, |
| 222 | PhysLoc.getRawFilePos()); |
| 223 | #endif |
Chris Lattner | 7d6a4f6 | 2006-06-30 06:10:08 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 227 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 228 | /// getCharacterData - Return a pointer to the start of the specified location |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 229 | /// in the appropriate MemoryBuffer. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 230 | const char *SourceManager::getCharacterData(SourceLocation SL) const { |
Chris Lattner | d3a15f7 | 2006-07-04 23:01:03 +0000 | [diff] [blame] | 231 | // Note that this is a hot function in the getSpelling() path, which is |
| 232 | // heavily used by -E mode. |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 233 | SL = getPhysicalLoc(SL); |
Chris Lattner | d3a15f7 | 2006-07-04 23:01:03 +0000 | [diff] [blame] | 234 | |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 235 | return getFileInfo(SL.getFileID())->Buffer->getBufferStart() + |
| 236 | getFullFilePos(SL); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | 685730f | 2006-06-26 01:36:22 +0000 | [diff] [blame] | 239 | |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 240 | /// getColumnNumber - Return the column # for the specified file position. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 241 | /// this is significantly cheaper to compute than the line number. This returns |
| 242 | /// zero if the column number isn't known. |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 243 | unsigned SourceManager::getColumnNumber(SourceLocation Loc) const { |
| 244 | unsigned FileID = Loc.getFileID(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 245 | if (FileID == 0) return 0; |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 246 | |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 247 | unsigned FilePos = getFullFilePos(Loc); |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 248 | const MemoryBuffer *Buffer = getBuffer(FileID); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 249 | const char *Buf = Buffer->getBufferStart(); |
| 250 | |
| 251 | unsigned LineStart = FilePos; |
| 252 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 253 | --LineStart; |
| 254 | return FilePos-LineStart+1; |
| 255 | } |
| 256 | |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 257 | /// getSourceName - This method returns the name of the file or buffer that |
| 258 | /// the SourceLocation specifies. This can be modified with #line directives, |
| 259 | /// etc. |
| 260 | std::string SourceManager::getSourceName(SourceLocation Loc) { |
| 261 | unsigned FileID = Loc.getFileID(); |
| 262 | if (FileID == 0) return ""; |
Chris Lattner | 2dffd2b | 2006-06-29 16:44:08 +0000 | [diff] [blame] | 263 | return getFileInfo(FileID)->Buffer->getBufferIdentifier(); |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 267 | /// getLineNumber - Given a SourceLocation, return the physical line number |
| 268 | /// for the position indicated. This requires building and caching a table of |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 269 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 270 | /// about to emit a diagnostic. |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 271 | unsigned SourceManager::getLineNumber(SourceLocation Loc) { |
Chris Lattner | a85a9d2 | 2006-07-02 20:07:52 +0000 | [diff] [blame] | 272 | unsigned FileID = Loc.getFileID(); |
| 273 | if (FileID == 0) return 0; |
| 274 | FileInfo *FileInfo = getFileInfo(FileID); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 275 | |
| 276 | // If this is the first use of line information for this buffer, compute the |
| 277 | /// SourceLineCache for it on demand. |
| 278 | if (FileInfo->SourceLineCache == 0) { |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 279 | const MemoryBuffer *Buffer = FileInfo->Buffer; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 280 | |
| 281 | // Find the file offsets of all of the *physical* source lines. This does |
| 282 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 283 | std::vector<unsigned> LineOffsets; |
| 284 | |
| 285 | // Line #1 starts at char 0. |
| 286 | LineOffsets.push_back(0); |
| 287 | |
| 288 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 289 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 290 | unsigned Offs = 0; |
| 291 | while (1) { |
| 292 | // Skip over the contents of the line. |
| 293 | // TODO: Vectorize this? This is very performance sensitive for programs |
Chris Lattner | d5da3ea | 2006-07-04 21:11:41 +0000 | [diff] [blame] | 294 | // with lots of diagnostics and in -E mode. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 295 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 296 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 297 | ++NextBuf; |
| 298 | Offs += NextBuf-Buf; |
| 299 | Buf = NextBuf; |
| 300 | |
| 301 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 302 | // If this is \n\r or \r\n, skip both characters. |
| 303 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 304 | ++Offs, ++Buf; |
| 305 | ++Offs, ++Buf; |
| 306 | LineOffsets.push_back(Offs); |
| 307 | } else { |
| 308 | // Otherwise, this is a null. If end of file, exit. |
| 309 | if (Buf == End) break; |
| 310 | // Otherwise, skip the null. |
| 311 | ++Offs, ++Buf; |
| 312 | } |
| 313 | } |
| 314 | LineOffsets.push_back(Offs); |
| 315 | |
| 316 | // Copy the offsets into the FileInfo structure. |
| 317 | FileInfo->NumLines = LineOffsets.size(); |
| 318 | FileInfo->SourceLineCache = new unsigned[LineOffsets.size()]; |
| 319 | std::copy(LineOffsets.begin(), LineOffsets.end(), |
| 320 | FileInfo->SourceLineCache); |
| 321 | } |
| 322 | |
| 323 | // Okay, we know we have a line number table. Do a binary search to find the |
| 324 | // line number that this character position lands on. |
| 325 | unsigned NumLines = FileInfo->NumLines; |
| 326 | unsigned *SourceLineCache = FileInfo->SourceLineCache; |
| 327 | |
| 328 | // TODO: If this is performance sensitive, we could try doing simple radix |
| 329 | // type approaches to make good (tight?) initial guesses based on the |
| 330 | // assumption that all lines are the same average size. |
| 331 | unsigned *Pos = std::lower_bound(SourceLineCache, SourceLineCache+NumLines, |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 332 | getFullFilePos(Loc)+1); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 333 | return Pos-SourceLineCache; |
| 334 | } |
| 335 | |
| 336 | /// PrintStats - Print statistics to stderr. |
| 337 | /// |
| 338 | void SourceManager::PrintStats() const { |
| 339 | std::cerr << "\n*** Source Manager Stats:\n"; |
| 340 | std::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 341 | << " mem buffers mapped, " << FileIDs.size() |
| 342 | << " file ID's allocated.\n"; |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 343 | std::cerr << " " << FileIDs.size() << " normal buffer FileID's, " |
| 344 | << MacroIDs.size() << " macro expansion FileID's.\n"; |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 345 | |
| 346 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 347 | |
| 348 | unsigned NumLineNumsComputed = 0; |
| 349 | unsigned NumFileBytesMapped = 0; |
| 350 | for (std::map<const FileEntry *, FileInfo>::const_iterator I = |
| 351 | FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
| 352 | NumLineNumsComputed += I->second.SourceLineCache != 0; |
| 353 | NumFileBytesMapped += I->second.Buffer->getBufferSize(); |
| 354 | } |
| 355 | std::cerr << NumFileBytesMapped << " bytes of files mapped, " |
| 356 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
| 357 | } |