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