Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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" |
Hartmut Kaiser | 3494725 | 2007-09-12 15:39:04 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | #include "llvm/System/Path.h" |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/Serialize.h" |
| 21 | #include "llvm/Bitcode/Deserialize.h" |
Ted Kremenek | 665dd4a | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Gabor Greif | 1501218 | 2007-07-12 16:00:00 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace SrcMgr; |
| 27 | using llvm::MemoryBuffer; |
| 28 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 29 | ContentCache::~ContentCache() { |
| 30 | delete Buffer; |
| 31 | delete [] SourceLineCache; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | // FIXME: REMOVE THESE |
| 35 | #include <unistd.h> |
| 36 | #include <sys/types.h> |
Anton Korobeynikov | bd0be39 | 2007-10-16 09:09:44 +0000 | [diff] [blame] | 37 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | #include <sys/uio.h> |
| 39 | #include <sys/fcntl.h> |
Chris Lattner | 6a4545e | 2007-09-03 18:24:56 +0000 | [diff] [blame] | 40 | #else |
| 41 | #include <io.h> |
| 42 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | #include <cerrno> |
| 44 | |
| 45 | static const MemoryBuffer *ReadFileFast(const FileEntry *FileEnt) { |
| 46 | #if 0 |
| 47 | // FIXME: Reintroduce this and zap this function once the common llvm stuff |
| 48 | // is fast for the small case. |
| 49 | return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()), |
| 50 | FileEnt->getSize()); |
| 51 | #endif |
| 52 | |
| 53 | // If the file is larger than some threshold, use 'read', otherwise use mmap. |
| 54 | if (FileEnt->getSize() >= 4096*4) |
| 55 | return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()), |
| 56 | 0, FileEnt->getSize()); |
| 57 | |
| 58 | MemoryBuffer *SB = MemoryBuffer::getNewUninitMemBuffer(FileEnt->getSize(), |
| 59 | FileEnt->getName()); |
| 60 | char *BufPtr = const_cast<char*>(SB->getBufferStart()); |
Chris Lattner | 6a4545e | 2007-09-03 18:24:56 +0000 | [diff] [blame] | 61 | |
Hartmut Kaiser | 3494725 | 2007-09-12 15:39:04 +0000 | [diff] [blame] | 62 | #if defined(LLVM_ON_WIN32) |
Chris Lattner | 6a4545e | 2007-09-03 18:24:56 +0000 | [diff] [blame] | 63 | int FD = ::open(FileEnt->getName(), O_RDONLY|O_BINARY); |
| 64 | #else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | int FD = ::open(FileEnt->getName(), O_RDONLY); |
Chris Lattner | 6a4545e | 2007-09-03 18:24:56 +0000 | [diff] [blame] | 66 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | if (FD == -1) { |
| 68 | delete SB; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | unsigned BytesLeft = FileEnt->getSize(); |
| 73 | while (BytesLeft) { |
| 74 | ssize_t NumRead = ::read(FD, BufPtr, BytesLeft); |
| 75 | if (NumRead != -1) { |
| 76 | BytesLeft -= NumRead; |
| 77 | BufPtr += NumRead; |
| 78 | } else if (errno == EINTR) { |
| 79 | // try again |
| 80 | } else { |
| 81 | // error reading. |
| 82 | close(FD); |
| 83 | delete SB; |
| 84 | return 0; |
| 85 | } |
| 86 | } |
| 87 | close(FD); |
| 88 | |
| 89 | return SB; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /// getFileInfo - Create or return a cached FileInfo for the specified file. |
| 94 | /// |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 95 | const ContentCache* SourceManager::getContentCache(const FileEntry *FileEnt) { |
| 96 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | assert(FileEnt && "Didn't specify a file entry to use?"); |
| 98 | // Do we already have information about this file? |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 99 | std::set<ContentCache>::iterator I = |
| 100 | FileInfos.lower_bound(ContentCache(FileEnt)); |
| 101 | |
| 102 | if (I != FileInfos.end() && I->Entry == FileEnt) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | return &*I; |
| 104 | |
| 105 | // Nope, get information. |
| 106 | const MemoryBuffer *File = ReadFileFast(FileEnt); |
| 107 | if (File == 0) |
| 108 | return 0; |
| 109 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 110 | ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 112 | Entry.Buffer = File; |
| 113 | Entry.SourceLineCache = 0; |
| 114 | Entry.NumLines = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | return &Entry; |
| 116 | } |
| 117 | |
| 118 | |
Ted Kremenek | d1c0eee | 2007-10-31 17:53:38 +0000 | [diff] [blame] | 119 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 120 | /// memory buffer. This does no caching. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 121 | const ContentCache* |
| 122 | SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 123 | // Add a new ContentCache to the MemBufferInfos list and return it. We |
| 124 | // must default construct the object first that the instance actually |
| 125 | // stored within MemBufferInfos actually owns the Buffer, and not any |
| 126 | // temporary we would use in the call to "push_back". |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 127 | MemBufferInfos.push_back(ContentCache()); |
| 128 | ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back()); |
| 129 | Entry.Buffer = Buffer; |
| 130 | return &Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 134 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 135 | /// include position. This works regardless of whether the ContentCache |
| 136 | /// corresponds to a file or some other input source. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 137 | unsigned SourceManager::createFileID(const ContentCache *File, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | SourceLocation IncludePos) { |
| 139 | // If FileEnt is really large (e.g. it's a large .i file), we may not be able |
| 140 | // to fit an arbitrary position in the file in the FilePos field. To handle |
| 141 | // this, we create one FileID for each chunk of the file that fits in a |
| 142 | // FilePos field. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 143 | unsigned FileSize = File->Buffer->getBufferSize(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 145 | FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) && |
| 147 | "Ran out of file ID's!"); |
| 148 | return FileIDs.size(); |
| 149 | } |
| 150 | |
| 151 | // Create one FileID for each chunk of the file. |
| 152 | unsigned Result = FileIDs.size()+1; |
| 153 | |
| 154 | unsigned ChunkNo = 0; |
| 155 | while (1) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 156 | FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | |
| 158 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break; |
| 159 | FileSize -= (1 << SourceLocation::FilePosBits); |
| 160 | } |
| 161 | |
| 162 | assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) && |
| 163 | "Ran out of file ID's!"); |
| 164 | return Result; |
| 165 | } |
| 166 | |
| 167 | /// getInstantiationLoc - Return a new SourceLocation that encodes the fact |
| 168 | /// that a token from physloc PhysLoc should actually be referenced from |
| 169 | /// InstantiationLoc. |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 170 | SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | SourceLocation InstantLoc) { |
Chris Lattner | abca2bb | 2007-07-15 06:35:27 +0000 | [diff] [blame] | 172 | // The specified source location may be a mapped location, due to a macro |
| 173 | // instantiation or #line directive. Strip off this information to find out |
| 174 | // where the characters are actually located. |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 175 | PhysLoc = getPhysicalLoc(PhysLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | |
| 177 | // Resolve InstantLoc down to a real logical location. |
| 178 | InstantLoc = getLogicalLoc(InstantLoc); |
| 179 | |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 180 | |
| 181 | // If the last macro id is close to the currently requested location, try to |
Chris Lattner | 991ae51 | 2007-08-02 03:55:37 +0000 | [diff] [blame] | 182 | // reuse it. This implements a small cache. |
| 183 | for (int i = MacroIDs.size()-1, e = MacroIDs.size()-6; i >= 0 && i != e; --i){ |
| 184 | MacroIDInfo &LastOne = MacroIDs[i]; |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 991ae51 | 2007-08-02 03:55:37 +0000 | [diff] [blame] | 186 | // The instanitation point and source physloc have to exactly match to reuse |
| 187 | // (for now). We could allow "nearby" instantiations in the future. |
Chris Lattner | 18807d2 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 188 | if (LastOne.getVirtualLoc() != InstantLoc || |
Chris Lattner | 991ae51 | 2007-08-02 03:55:37 +0000 | [diff] [blame] | 189 | LastOne.getPhysicalLoc().getFileID() != PhysLoc.getFileID()) |
| 190 | continue; |
| 191 | |
| 192 | // Check to see if the physloc of the token came from near enough to reuse. |
| 193 | int PhysDelta = PhysLoc.getRawFilePos() - |
| 194 | LastOne.getPhysicalLoc().getRawFilePos(); |
| 195 | if (SourceLocation::isValidMacroPhysOffs(PhysDelta)) |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 196 | return SourceLocation::getMacroLoc(i, PhysDelta, false, false); |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | 45011cf | 2007-07-20 18:26:45 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 200 | MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc)); |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 201 | return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 204 | /// getBufferData - Return a pointer to the start and end of the character |
| 205 | /// data for the specified FileID. |
| 206 | std::pair<const char*, const char*> |
| 207 | SourceManager::getBufferData(unsigned FileID) const { |
| 208 | const llvm::MemoryBuffer *Buf = getBuffer(FileID); |
| 209 | return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd()); |
| 210 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | /// getCharacterData - Return a pointer to the start of the specified location |
| 214 | /// in the appropriate MemoryBuffer. |
| 215 | const char *SourceManager::getCharacterData(SourceLocation SL) const { |
| 216 | // Note that this is a hot function in the getSpelling() path, which is |
| 217 | // heavily used by -E mode. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 218 | SL = getPhysicalLoc(SL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 220 | return getContentCache(SL.getFileID())->Buffer->getBufferStart() + |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 221 | getFullFilePos(SL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 225 | /// getColumnNumber - Return the column # for the specified file position. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | /// this is significantly cheaper to compute than the line number. This returns |
| 227 | /// zero if the column number isn't known. |
| 228 | unsigned SourceManager::getColumnNumber(SourceLocation Loc) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 229 | unsigned FileID = Loc.getFileID(); |
| 230 | if (FileID == 0) return 0; |
| 231 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 232 | unsigned FilePos = getFullFilePos(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | const MemoryBuffer *Buffer = getBuffer(FileID); |
| 234 | const char *Buf = Buffer->getBufferStart(); |
| 235 | |
| 236 | unsigned LineStart = FilePos; |
| 237 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 238 | --LineStart; |
| 239 | return FilePos-LineStart+1; |
| 240 | } |
| 241 | |
| 242 | /// getSourceName - This method returns the name of the file or buffer that |
| 243 | /// the SourceLocation specifies. This can be modified with #line directives, |
| 244 | /// etc. |
Chris Lattner | 8b6ca88 | 2007-08-30 05:59:30 +0000 | [diff] [blame] | 245 | const char *SourceManager::getSourceName(SourceLocation Loc) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 246 | unsigned FileID = Loc.getFileID(); |
| 247 | if (FileID == 0) return ""; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 248 | return getContentCache(FileID)->Buffer->getBufferIdentifier(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 251 | static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE; |
| 252 | static void ComputeLineNumbers(ContentCache* FI) { |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 253 | const MemoryBuffer *Buffer = FI->Buffer; |
| 254 | |
| 255 | // Find the file offsets of all of the *physical* source lines. This does |
| 256 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 257 | std::vector<unsigned> LineOffsets; |
| 258 | |
| 259 | // Line #1 starts at char 0. |
| 260 | LineOffsets.push_back(0); |
| 261 | |
| 262 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 263 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 264 | unsigned Offs = 0; |
| 265 | while (1) { |
| 266 | // Skip over the contents of the line. |
| 267 | // TODO: Vectorize this? This is very performance sensitive for programs |
| 268 | // with lots of diagnostics and in -E mode. |
| 269 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 270 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 271 | ++NextBuf; |
| 272 | Offs += NextBuf-Buf; |
| 273 | Buf = NextBuf; |
| 274 | |
| 275 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 276 | // If this is \n\r or \r\n, skip both characters. |
| 277 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 278 | ++Offs, ++Buf; |
| 279 | ++Offs, ++Buf; |
| 280 | LineOffsets.push_back(Offs); |
| 281 | } else { |
| 282 | // Otherwise, this is a null. If end of file, exit. |
| 283 | if (Buf == End) break; |
| 284 | // Otherwise, skip the null. |
| 285 | ++Offs, ++Buf; |
| 286 | } |
| 287 | } |
| 288 | LineOffsets.push_back(Offs); |
| 289 | |
| 290 | // Copy the offsets into the FileInfo structure. |
| 291 | FI->NumLines = LineOffsets.size(); |
| 292 | FI->SourceLineCache = new unsigned[LineOffsets.size()]; |
| 293 | std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); |
| 294 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | |
| 296 | /// getLineNumber - Given a SourceLocation, return the physical line number |
| 297 | /// for the position indicated. This requires building and caching a table of |
| 298 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 299 | /// about to emit a diagnostic. |
| 300 | unsigned SourceManager::getLineNumber(SourceLocation Loc) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | unsigned FileID = Loc.getFileID(); |
| 302 | if (FileID == 0) return 0; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 303 | |
| 304 | ContentCache* Content; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 305 | |
| 306 | if (LastLineNoFileIDQuery == FileID) |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 307 | Content = LastLineNoContentCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 308 | else |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 309 | Content = const_cast<ContentCache*>(getContentCache(FileID)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | |
| 311 | // If this is the first use of line information for this buffer, compute the |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 312 | /// SourceLineCache for it on demand. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 313 | if (Content->SourceLineCache == 0) |
| 314 | ComputeLineNumbers(Content); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | |
| 316 | // Okay, we know we have a line number table. Do a binary search to find the |
| 317 | // line number that this character position lands on. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 318 | unsigned *SourceLineCache = Content->SourceLineCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 319 | unsigned *SourceLineCacheStart = SourceLineCache; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 320 | unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 321 | |
| 322 | unsigned QueriedFilePos = getFullFilePos(Loc)+1; |
| 323 | |
| 324 | // If the previous query was to the same file, we know both the file pos from |
| 325 | // that query and the line number returned. This allows us to narrow the |
| 326 | // search space from the entire file to something near the match. |
| 327 | if (LastLineNoFileIDQuery == FileID) { |
| 328 | if (QueriedFilePos >= LastLineNoFilePos) { |
| 329 | SourceLineCache = SourceLineCache+LastLineNoResult-1; |
| 330 | |
| 331 | // The query is likely to be nearby the previous one. Here we check to |
| 332 | // see if it is within 5, 10 or 20 lines. It can be far away in cases |
| 333 | // where big comment blocks and vertical whitespace eat up lines but |
| 334 | // contribute no tokens. |
| 335 | if (SourceLineCache+5 < SourceLineCacheEnd) { |
| 336 | if (SourceLineCache[5] > QueriedFilePos) |
| 337 | SourceLineCacheEnd = SourceLineCache+5; |
| 338 | else if (SourceLineCache+10 < SourceLineCacheEnd) { |
| 339 | if (SourceLineCache[10] > QueriedFilePos) |
| 340 | SourceLineCacheEnd = SourceLineCache+10; |
| 341 | else if (SourceLineCache+20 < SourceLineCacheEnd) { |
| 342 | if (SourceLineCache[20] > QueriedFilePos) |
| 343 | SourceLineCacheEnd = SourceLineCache+20; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } else { |
| 348 | SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; |
| 349 | } |
| 350 | } |
| 351 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 352 | // If the spread is large, do a "radix" test as our initial guess, based on |
| 353 | // the assumption that lines average to approximately the same length. |
| 354 | // NOTE: This is currently disabled, as it does not appear to be profitable in |
| 355 | // initial measurements. |
| 356 | if (0 && SourceLineCacheEnd-SourceLineCache > 20) { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 357 | unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 358 | |
| 359 | // Take a stab at guessing where it is. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 360 | unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 361 | |
| 362 | // Check for -10 and +10 lines. |
| 363 | unsigned LowerBound = std::max(int(ApproxPos-10), 0); |
| 364 | unsigned UpperBound = std::min(ApproxPos+10, FileLen); |
| 365 | |
| 366 | // If the computed lower bound is less than the query location, move it in. |
| 367 | if (SourceLineCache < SourceLineCacheStart+LowerBound && |
| 368 | SourceLineCacheStart[LowerBound] < QueriedFilePos) |
| 369 | SourceLineCache = SourceLineCacheStart+LowerBound; |
| 370 | |
| 371 | // If the computed upper bound is greater than the query location, move it. |
| 372 | if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && |
| 373 | SourceLineCacheStart[UpperBound] >= QueriedFilePos) |
| 374 | SourceLineCacheEnd = SourceLineCacheStart+UpperBound; |
| 375 | } |
| 376 | |
| 377 | unsigned *Pos |
| 378 | = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 379 | unsigned LineNo = Pos-SourceLineCacheStart; |
| 380 | |
| 381 | LastLineNoFileIDQuery = FileID; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 382 | LastLineNoContentCache = Content; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 383 | LastLineNoFilePos = QueriedFilePos; |
| 384 | LastLineNoResult = LineNo; |
| 385 | return LineNo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | /// PrintStats - Print statistics to stderr. |
| 389 | /// |
| 390 | void SourceManager::PrintStats() const { |
Ted Kremenek | 665dd4a | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 391 | llvm::cerr << "\n*** Source Manager Stats:\n"; |
| 392 | llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 393 | << " mem buffers mapped, " << FileIDs.size() |
| 394 | << " file ID's allocated.\n"; |
| 395 | llvm::cerr << " " << FileIDs.size() << " normal buffer FileID's, " |
| 396 | << MacroIDs.size() << " macro expansion FileID's.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | unsigned NumLineNumsComputed = 0; |
| 399 | unsigned NumFileBytesMapped = 0; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 400 | for (std::set<ContentCache>::const_iterator I = |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 402 | NumLineNumsComputed += I->SourceLineCache != 0; |
| 403 | NumFileBytesMapped += I->Buffer->getBufferSize(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 404 | } |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 665dd4a | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 406 | llvm::cerr << NumFileBytesMapped << " bytes of files mapped, " |
| 407 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | } |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 409 | |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | // Serialization. |
| 412 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 413 | |
| 414 | void ContentCache::Emit(llvm::Serializer& S) const { |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 415 | S.FlushRecord(); |
| 416 | S.EmitPtr(this); |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 418 | if (Entry) S.EmitCStr(Buffer->getBufferIdentifier()); |
| 419 | else { |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 420 | const char* p = Buffer->getBufferStart(); |
| 421 | const char* e = Buffer->getBufferEnd(); |
| 422 | |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 423 | S.EmitInt(e-p); |
| 424 | |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 425 | for ( ; p != e; ++p) |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 426 | S.EmitInt(*p); |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 429 | S.FlushRecord(); |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 430 | } |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 431 | |
| 432 | void ContentCache::ReadToSourceManager(llvm::Deserializer& D, |
| 433 | SourceManager& SMgr, |
| 434 | FileManager* FMgr, |
| 435 | std::vector<char>& Buf) { |
| 436 | if (FMgr) { |
| 437 | llvm::SerializedPtrID PtrID = D.ReadPtrID(); |
| 438 | D.ReadCStr(Buf,false); |
| 439 | |
| 440 | // Create/fetch the FileEntry. |
| 441 | const char* start = &Buf[0]; |
| 442 | const FileEntry* E = FMgr->getFile(start,start+Buf.size()); |
| 443 | |
| 444 | assert (E && "Not yet supported: missing files."); |
| 445 | |
| 446 | // Get the ContextCache object and register it with the deserializer. |
| 447 | D.RegisterPtr(PtrID,SMgr.getContentCache(E)); |
| 448 | } |
| 449 | else { |
| 450 | // Register the ContextCache object with the deserializer. |
| 451 | SMgr.MemBufferInfos.push_back(ContentCache()); |
| 452 | ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back()); |
| 453 | D.RegisterPtr(&Entry); |
| 454 | |
| 455 | // Create the buffer. |
| 456 | unsigned Size = D.ReadInt(); |
| 457 | Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size); |
| 458 | |
| 459 | // Read the contents of the buffer. |
| 460 | char* p = const_cast<char*>(Entry.Buffer->getBufferStart()); |
| 461 | for (unsigned i = 0; i < Size ; ++i) |
| 462 | p[i] = D.ReadInt(); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void FileIDInfo::Emit(llvm::Serializer& S) const { |
| 467 | S.Emit(IncludeLoc); |
| 468 | S.EmitInt(ChunkNo); |
| 469 | S.EmitPtr(Content); |
| 470 | } |
| 471 | |
| 472 | FileIDInfo FileIDInfo::ReadVal(llvm::Deserializer& D) { |
| 473 | FileIDInfo I; |
| 474 | I.IncludeLoc = SourceLocation::ReadVal(D); |
| 475 | I.ChunkNo = D.ReadInt(); |
| 476 | D.ReadPtr(I.Content,false); |
| 477 | return I; |
| 478 | } |
| 479 | |
| 480 | void MacroIDInfo::Emit(llvm::Serializer& S) const { |
| 481 | S.Emit(VirtualLoc); |
| 482 | S.Emit(PhysicalLoc); |
| 483 | } |
| 484 | |
| 485 | MacroIDInfo MacroIDInfo::ReadVal(llvm::Deserializer& D) { |
| 486 | MacroIDInfo I; |
| 487 | I.VirtualLoc = SourceLocation::ReadVal(D); |
| 488 | I.PhysicalLoc = SourceLocation::ReadVal(D); |
| 489 | return I; |
| 490 | } |
| 491 | |
| 492 | void SourceManager::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 493 | S.EnterBlock(); |
| 494 | S.EmitPtr(this); |
| 495 | |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 496 | // Emit: FileInfos. Just emit the file name. |
| 497 | S.EnterBlock(); |
| 498 | |
| 499 | std::for_each(FileInfos.begin(),FileInfos.end(), |
| 500 | S.MakeEmitter<ContentCache>()); |
| 501 | |
| 502 | S.ExitBlock(); |
| 503 | |
| 504 | // Emit: MemBufferInfos |
| 505 | S.EnterBlock(); |
| 506 | |
| 507 | std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(), |
| 508 | S.MakeEmitter<ContentCache>()); |
| 509 | |
| 510 | S.ExitBlock(); |
| 511 | |
| 512 | // Emit: FileIDs |
| 513 | S.EmitInt(FileIDs.size()); |
| 514 | std::for_each(FileIDs.begin(), FileIDs.end(), S.MakeEmitter<FileIDInfo>()); |
| 515 | |
| 516 | // Emit: MacroIDs |
| 517 | S.EmitInt(MacroIDs.size()); |
| 518 | std::for_each(MacroIDs.begin(), MacroIDs.end(), S.MakeEmitter<MacroIDInfo>()); |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 519 | |
| 520 | S.ExitBlock(); |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 523 | SourceManager* |
| 524 | SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){ |
| 525 | SourceManager *M = new SourceManager(); |
| 526 | D.RegisterPtr(M); |
| 527 | |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 528 | std::vector<char> Buf; |
| 529 | |
| 530 | { // Read: FileInfos. |
| 531 | llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); |
| 532 | while (!D.FinishedBlock(BLoc)) |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 533 | ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf); |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | { // Read: MemBufferInfos. |
| 537 | llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); |
| 538 | while (!D.FinishedBlock(BLoc)) |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 539 | ContentCache::ReadToSourceManager(D,*M,NULL,Buf); |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // Read: FileIDs. |
| 543 | unsigned Size = D.ReadInt(); |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 544 | M->FileIDs.reserve(Size); |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 545 | for (; Size > 0 ; --Size) |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 546 | M->FileIDs.push_back(FileIDInfo::ReadVal(D)); |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 547 | |
| 548 | // Read: MacroIDs. |
| 549 | Size = D.ReadInt(); |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 550 | M->MacroIDs.reserve(Size); |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 551 | for (; Size > 0 ; --Size) |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 552 | M->MacroIDs.push_back(MacroIDInfo::ReadVal(D)); |
| 553 | |
| 554 | return M; |
Ted Kremenek | 1f2c7d1 | 2007-12-10 18:01:25 +0000 | [diff] [blame^] | 555 | } |