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" |
| 16 | #include "clang/Basic/SourceBuffer.h" |
| 17 | #include "llvm/System/Path.h" |
| 18 | #include <algorithm> |
| 19 | #include <iostream> |
| 20 | using namespace llvm; |
| 21 | using namespace clang; |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame^] | 22 | using namespace SrcMgr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 23 | |
| 24 | SourceManager::~SourceManager() { |
| 25 | for (std::map<const FileEntry *, FileInfo>::iterator I = FileInfos.begin(), |
| 26 | E = FileInfos.end(); I != E; ++I) { |
| 27 | delete I->second.Buffer; |
| 28 | delete[] I->second.SourceLineCache; |
| 29 | } |
| 30 | |
| 31 | for (std::list<InfoRec>::iterator I = MemBufferInfos.begin(), |
| 32 | E = MemBufferInfos.end(); I != E; ++I) { |
| 33 | delete I->second.Buffer; |
| 34 | delete[] I->second.SourceLineCache; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// getFileInfo - Create or return a cached FileInfo for the specified file. |
| 39 | /// |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame^] | 40 | const InfoRec * |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 41 | SourceManager::getInfoRec(const FileEntry *FileEnt) { |
| 42 | assert(FileEnt && "Didn't specify a file entry to use?"); |
| 43 | // Do we already have information about this file? |
| 44 | std::map<const FileEntry *, FileInfo>::iterator I = |
| 45 | FileInfos.lower_bound(FileEnt); |
| 46 | if (I != FileInfos.end() && I->first == FileEnt) |
| 47 | return &*I; |
| 48 | |
| 49 | // Nope, get information. |
| 50 | const SourceBuffer *File; |
| 51 | try { |
| 52 | File = clang::SourceBuffer::getFile(FileEnt); |
| 53 | if (File == 0) |
| 54 | return 0; |
| 55 | } catch (...) { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | const InfoRec &Entry = |
| 60 | *FileInfos.insert(I, std::make_pair(FileEnt, FileInfo())); |
| 61 | FileInfo &Info = const_cast<FileInfo &>(Entry.second); |
| 62 | |
| 63 | Info.Buffer = File; |
| 64 | Info.SourceLineCache = 0; |
| 65 | Info.NumLines = 0; |
| 66 | return &Entry; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /// createMemBufferInfoRec - Create a new info record for the specified memory |
| 71 | /// buffer. This does no caching. |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame^] | 72 | const InfoRec * |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 73 | SourceManager::createMemBufferInfoRec(const SourceBuffer *Buffer) { |
| 74 | // Add a new info record to the MemBufferInfos list and return it. |
| 75 | FileInfo FI; |
| 76 | FI.Buffer = Buffer; |
| 77 | FI.SourceLineCache = 0; |
| 78 | FI.NumLines = 0; |
| 79 | MemBufferInfos.push_back(InfoRec(0, FI)); |
| 80 | return &MemBufferInfos.back(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /// createFileID - Create a new fileID for the specified InfoRec and include |
| 85 | /// position. This works regardless of whether the InfoRec corresponds to a |
| 86 | /// file or some other input source. |
| 87 | unsigned SourceManager::createFileID(const InfoRec *File, |
| 88 | SourceLocation IncludePos) { |
| 89 | // If FileEnt is really large (e.g. it's a large .i file), we may not be able |
| 90 | // to fit an arbitrary position in the file in the FilePos field. To handle |
| 91 | // this, we create one FileID for each chunk of the file that fits in a |
| 92 | // FilePos field. |
| 93 | unsigned FileSize = File->second.Buffer->getBufferSize(); |
| 94 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) { |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame^] | 95 | FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, 0, File)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 96 | return FileIDs.size(); |
| 97 | } |
| 98 | |
| 99 | // Create one FileID for each chunk of the file. |
| 100 | unsigned Result = FileIDs.size()+1; |
| 101 | |
| 102 | unsigned ChunkNo = 0; |
| 103 | while (1) { |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame^] | 104 | FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, ChunkNo++, File)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 105 | |
| 106 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break; |
| 107 | FileSize -= (1 << SourceLocation::FilePosBits); |
| 108 | } |
| 109 | |
| 110 | return Result; |
| 111 | } |
| 112 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 113 | /// getCharacterData - Return a pointer to the start of the specified location |
| 114 | /// in the appropriate SourceBuffer. This returns null if it cannot be |
| 115 | /// computed (e.g. invalid SourceLocation). |
| 116 | const char *SourceManager::getCharacterData(SourceLocation SL) const { |
| 117 | if (unsigned FileID = SL.getFileID()) |
| 118 | return getFileInfo(FileID)->Buffer->getBufferStart() + getFilePos(SL); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 123 | /// getColumnNumber - Return the column # for the specified include position. |
| 124 | /// this is significantly cheaper to compute than the line number. This returns |
| 125 | /// zero if the column number isn't known. |
| 126 | unsigned SourceManager::getColumnNumber(SourceLocation IncludePos) const { |
| 127 | unsigned FileID = IncludePos.getFileID(); |
| 128 | if (FileID == 0) return 0; |
| 129 | FileInfo *FileInfo = getFileInfo(FileID); |
| 130 | unsigned FilePos = getFilePos(IncludePos); |
| 131 | const SourceBuffer *Buffer = FileInfo->Buffer; |
| 132 | const char *Buf = Buffer->getBufferStart(); |
| 133 | |
| 134 | unsigned LineStart = FilePos; |
| 135 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 136 | --LineStart; |
| 137 | return FilePos-LineStart+1; |
| 138 | } |
| 139 | |
| 140 | /// getLineNumber - Given a SourceLocation, return the physical line number |
| 141 | /// for the position indicated. This requires building and caching a table of |
| 142 | /// line offsets for the SourceBuffer, so this is not cheap: use only when |
| 143 | /// about to emit a diagnostic. |
| 144 | unsigned SourceManager::getLineNumber(SourceLocation IncludePos) { |
| 145 | FileInfo *FileInfo = getFileInfo(IncludePos.getFileID()); |
| 146 | |
| 147 | // If this is the first use of line information for this buffer, compute the |
| 148 | /// SourceLineCache for it on demand. |
| 149 | if (FileInfo->SourceLineCache == 0) { |
| 150 | const SourceBuffer *Buffer = FileInfo->Buffer; |
| 151 | |
| 152 | // Find the file offsets of all of the *physical* source lines. This does |
| 153 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 154 | std::vector<unsigned> LineOffsets; |
| 155 | |
| 156 | // Line #1 starts at char 0. |
| 157 | LineOffsets.push_back(0); |
| 158 | |
| 159 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 160 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 161 | unsigned Offs = 0; |
| 162 | while (1) { |
| 163 | // Skip over the contents of the line. |
| 164 | // TODO: Vectorize this? This is very performance sensitive for programs |
| 165 | // with lots of diagnostics. |
| 166 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 167 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 168 | ++NextBuf; |
| 169 | Offs += NextBuf-Buf; |
| 170 | Buf = NextBuf; |
| 171 | |
| 172 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 173 | // If this is \n\r or \r\n, skip both characters. |
| 174 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 175 | ++Offs, ++Buf; |
| 176 | ++Offs, ++Buf; |
| 177 | LineOffsets.push_back(Offs); |
| 178 | } else { |
| 179 | // Otherwise, this is a null. If end of file, exit. |
| 180 | if (Buf == End) break; |
| 181 | // Otherwise, skip the null. |
| 182 | ++Offs, ++Buf; |
| 183 | } |
| 184 | } |
| 185 | LineOffsets.push_back(Offs); |
| 186 | |
| 187 | // Copy the offsets into the FileInfo structure. |
| 188 | FileInfo->NumLines = LineOffsets.size(); |
| 189 | FileInfo->SourceLineCache = new unsigned[LineOffsets.size()]; |
| 190 | std::copy(LineOffsets.begin(), LineOffsets.end(), |
| 191 | FileInfo->SourceLineCache); |
| 192 | } |
| 193 | |
| 194 | // Okay, we know we have a line number table. Do a binary search to find the |
| 195 | // line number that this character position lands on. |
| 196 | unsigned NumLines = FileInfo->NumLines; |
| 197 | unsigned *SourceLineCache = FileInfo->SourceLineCache; |
| 198 | |
| 199 | // TODO: If this is performance sensitive, we could try doing simple radix |
| 200 | // type approaches to make good (tight?) initial guesses based on the |
| 201 | // assumption that all lines are the same average size. |
| 202 | unsigned *Pos = std::lower_bound(SourceLineCache, SourceLineCache+NumLines, |
| 203 | getFilePos(IncludePos)+1); |
| 204 | return Pos-SourceLineCache; |
| 205 | } |
| 206 | |
| 207 | /// PrintStats - Print statistics to stderr. |
| 208 | /// |
| 209 | void SourceManager::PrintStats() const { |
| 210 | std::cerr << "\n*** Source Manager Stats:\n"; |
| 211 | std::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 212 | << " mem buffers mapped, " << FileIDs.size() |
| 213 | << " file ID's allocated.\n"; |
| 214 | |
| 215 | unsigned NumLineNumsComputed = 0; |
| 216 | unsigned NumFileBytesMapped = 0; |
| 217 | for (std::map<const FileEntry *, FileInfo>::const_iterator I = |
| 218 | FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
| 219 | NumLineNumsComputed += I->second.SourceLineCache != 0; |
| 220 | NumFileBytesMapped += I->second.Buffer->getBufferSize(); |
| 221 | } |
| 222 | std::cerr << NumFileBytesMapped << " bytes of files mapped, " |
| 223 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
| 224 | } |