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. |
Chris Lattner | 35f9985 | 2007-04-29 06:08:57 +0000 | [diff] [blame^] | 50 | const SourceBuffer *File = clang::SourceBuffer::getFile(FileEnt); |
| 51 | if (File == 0) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 52 | return 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 53 | |
| 54 | const InfoRec &Entry = |
| 55 | *FileInfos.insert(I, std::make_pair(FileEnt, FileInfo())); |
| 56 | FileInfo &Info = const_cast<FileInfo &>(Entry.second); |
| 57 | |
| 58 | Info.Buffer = File; |
| 59 | Info.SourceLineCache = 0; |
| 60 | Info.NumLines = 0; |
| 61 | return &Entry; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /// createMemBufferInfoRec - Create a new info record for the specified memory |
| 66 | /// buffer. This does no caching. |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame] | 67 | const InfoRec * |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 68 | SourceManager::createMemBufferInfoRec(const SourceBuffer *Buffer) { |
| 69 | // Add a new info record to the MemBufferInfos list and return it. |
| 70 | FileInfo FI; |
| 71 | FI.Buffer = Buffer; |
| 72 | FI.SourceLineCache = 0; |
| 73 | FI.NumLines = 0; |
| 74 | MemBufferInfos.push_back(InfoRec(0, FI)); |
| 75 | return &MemBufferInfos.back(); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /// createFileID - Create a new fileID for the specified InfoRec and include |
| 80 | /// position. This works regardless of whether the InfoRec corresponds to a |
| 81 | /// file or some other input source. |
| 82 | unsigned SourceManager::createFileID(const InfoRec *File, |
| 83 | SourceLocation IncludePos) { |
| 84 | // If FileEnt is really large (e.g. it's a large .i file), we may not be able |
| 85 | // to fit an arbitrary position in the file in the FilePos field. To handle |
| 86 | // this, we create one FileID for each chunk of the file that fits in a |
| 87 | // FilePos field. |
| 88 | unsigned FileSize = File->second.Buffer->getBufferSize(); |
| 89 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) { |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame] | 90 | FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, 0, File)); |
Chris Lattner | 2a904d0 | 2006-10-22 06:33:42 +0000 | [diff] [blame] | 91 | assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) && |
| 92 | "Ran out of file ID's!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 93 | return FileIDs.size(); |
| 94 | } |
| 95 | |
| 96 | // Create one FileID for each chunk of the file. |
| 97 | unsigned Result = FileIDs.size()+1; |
| 98 | |
| 99 | unsigned ChunkNo = 0; |
| 100 | while (1) { |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame] | 101 | FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, ChunkNo++, File)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 102 | |
| 103 | if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break; |
| 104 | FileSize -= (1 << SourceLocation::FilePosBits); |
| 105 | } |
| 106 | |
Chris Lattner | 2a904d0 | 2006-10-22 06:33:42 +0000 | [diff] [blame] | 107 | assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) && |
| 108 | "Ran out of file ID's!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 109 | return Result; |
| 110 | } |
| 111 | |
Chris Lattner | 7d6a4f6 | 2006-06-30 06:10:08 +0000 | [diff] [blame] | 112 | /// getInstantiationLoc - Return a new SourceLocation that encodes the fact |
| 113 | /// that a token from physloc PhysLoc should actually be referenced from |
| 114 | /// InstantiationLoc. |
| 115 | SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc, |
| 116 | SourceLocation InstantLoc) { |
Chris Lattner | 351050b | 2006-07-16 18:05:08 +0000 | [diff] [blame] | 117 | assert(getFIDInfo(PhysLoc.getFileID())->IDType != |
| 118 | SrcMgr::FileIDInfo::MacroExpansion && |
| 119 | "Location instantiated in a macro?"); |
| 120 | |
Chris Lattner | 4c37a8c | 2006-06-30 06:15:08 +0000 | [diff] [blame] | 121 | // Resolve InstantLoc down to a real logical location. |
| 122 | InstantLoc = getLogicalLoc(InstantLoc); |
Chris Lattner | 7fa8c88 | 2006-07-20 06:48:52 +0000 | [diff] [blame] | 123 | |
| 124 | unsigned InstantiationFileID; |
| 125 | // If this is the same instantiation as was requested last time, return this |
| 126 | // immediately. |
| 127 | if (PhysLoc.getFileID() == LastInstantiationLoc_MacroFID && |
| 128 | InstantLoc == LastInstantiationLoc_InstantLoc) { |
| 129 | InstantiationFileID = LastInstantiationLoc_Result; |
| 130 | } else { |
| 131 | // Add a FileID for this. FIXME: should cache these! |
| 132 | FileIDs.push_back(FileIDInfo::getMacroExpansion(InstantLoc, |
| 133 | PhysLoc.getFileID())); |
| 134 | InstantiationFileID = FileIDs.size(); |
| 135 | |
| 136 | // Remember this in the single-entry cache for next time. |
| 137 | LastInstantiationLoc_MacroFID = PhysLoc.getFileID(); |
| 138 | LastInstantiationLoc_InstantLoc = InstantLoc; |
| 139 | LastInstantiationLoc_Result = InstantiationFileID; |
| 140 | } |
Chris Lattner | 4c37a8c | 2006-06-30 06:15:08 +0000 | [diff] [blame] | 141 | return SourceLocation(InstantiationFileID, PhysLoc.getRawFilePos()); |
Chris Lattner | 7d6a4f6 | 2006-06-30 06:10:08 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 145 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 146 | /// getCharacterData - Return a pointer to the start of the specified location |
Chris Lattner | d3a15f7 | 2006-07-04 23:01:03 +0000 | [diff] [blame] | 147 | /// in the appropriate SourceBuffer. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 148 | const char *SourceManager::getCharacterData(SourceLocation SL) const { |
Chris Lattner | d3a15f7 | 2006-07-04 23:01:03 +0000 | [diff] [blame] | 149 | // Note that this is a hot function in the getSpelling() path, which is |
| 150 | // heavily used by -E mode. |
| 151 | unsigned FileID = SL.getFileID(); |
| 152 | assert(FileID && "Invalid source location!"); |
| 153 | |
| 154 | return getFileInfo(FileID)->Buffer->getBufferStart() + getFilePos(SL); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 685730f | 2006-06-26 01:36:22 +0000 | [diff] [blame] | 157 | /// getIncludeLoc - Return the location of the #include for the specified |
| 158 | /// FileID. |
| 159 | SourceLocation SourceManager::getIncludeLoc(unsigned FileID) const { |
| 160 | const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID); |
| 161 | |
| 162 | // For Macros, the physical loc is specified by the MacroTokenFileID. |
Chris Lattner | 2dffd2b | 2006-06-29 16:44:08 +0000 | [diff] [blame] | 163 | if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) |
Chris Lattner | 685730f | 2006-06-26 01:36:22 +0000 | [diff] [blame] | 164 | FIDInfo = &FileIDs[FIDInfo->u.MacroTokenFileID-1]; |
| 165 | |
| 166 | return FIDInfo->IncludeLoc; |
| 167 | } |
| 168 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 170 | /// getColumnNumber - Return the column # for the specified include position. |
| 171 | /// this is significantly cheaper to compute than the line number. This returns |
| 172 | /// zero if the column number isn't known. |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 173 | unsigned SourceManager::getColumnNumber(SourceLocation Loc) const { |
Chris Lattner | 2dffd2b | 2006-06-29 16:44:08 +0000 | [diff] [blame] | 174 | Loc = getLogicalLoc(Loc); |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 175 | unsigned FileID = Loc.getFileID(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 176 | if (FileID == 0) return 0; |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 178 | unsigned FilePos = getFilePos(Loc); |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 179 | const SourceBuffer *Buffer = getBuffer(FileID); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 180 | const char *Buf = Buffer->getBufferStart(); |
| 181 | |
| 182 | unsigned LineStart = FilePos; |
| 183 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 184 | --LineStart; |
| 185 | return FilePos-LineStart+1; |
| 186 | } |
| 187 | |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 188 | /// getSourceName - This method returns the name of the file or buffer that |
| 189 | /// the SourceLocation specifies. This can be modified with #line directives, |
| 190 | /// etc. |
| 191 | std::string SourceManager::getSourceName(SourceLocation Loc) { |
Chris Lattner | 2dffd2b | 2006-06-29 16:44:08 +0000 | [diff] [blame] | 192 | Loc = getLogicalLoc(Loc); |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 193 | unsigned FileID = Loc.getFileID(); |
| 194 | if (FileID == 0) return ""; |
Chris Lattner | 2dffd2b | 2006-06-29 16:44:08 +0000 | [diff] [blame] | 195 | return getFileInfo(FileID)->Buffer->getBufferIdentifier(); |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 199 | /// getLineNumber - Given a SourceLocation, return the physical line number |
| 200 | /// for the position indicated. This requires building and caching a table of |
| 201 | /// line offsets for the SourceBuffer, so this is not cheap: use only when |
| 202 | /// about to emit a diagnostic. |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 203 | unsigned SourceManager::getLineNumber(SourceLocation Loc) { |
Chris Lattner | 2dffd2b | 2006-06-29 16:44:08 +0000 | [diff] [blame] | 204 | Loc = getLogicalLoc(Loc); |
Chris Lattner | a85a9d2 | 2006-07-02 20:07:52 +0000 | [diff] [blame] | 205 | unsigned FileID = Loc.getFileID(); |
| 206 | if (FileID == 0) return 0; |
| 207 | FileInfo *FileInfo = getFileInfo(FileID); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 208 | |
| 209 | // If this is the first use of line information for this buffer, compute the |
| 210 | /// SourceLineCache for it on demand. |
| 211 | if (FileInfo->SourceLineCache == 0) { |
| 212 | const SourceBuffer *Buffer = FileInfo->Buffer; |
| 213 | |
| 214 | // Find the file offsets of all of the *physical* source lines. This does |
| 215 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 216 | std::vector<unsigned> LineOffsets; |
| 217 | |
| 218 | // Line #1 starts at char 0. |
| 219 | LineOffsets.push_back(0); |
| 220 | |
| 221 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 222 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 223 | unsigned Offs = 0; |
| 224 | while (1) { |
| 225 | // Skip over the contents of the line. |
| 226 | // TODO: Vectorize this? This is very performance sensitive for programs |
Chris Lattner | d5da3ea | 2006-07-04 21:11:41 +0000 | [diff] [blame] | 227 | // with lots of diagnostics and in -E mode. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 228 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 229 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 230 | ++NextBuf; |
| 231 | Offs += NextBuf-Buf; |
| 232 | Buf = NextBuf; |
| 233 | |
| 234 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 235 | // If this is \n\r or \r\n, skip both characters. |
| 236 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 237 | ++Offs, ++Buf; |
| 238 | ++Offs, ++Buf; |
| 239 | LineOffsets.push_back(Offs); |
| 240 | } else { |
| 241 | // Otherwise, this is a null. If end of file, exit. |
| 242 | if (Buf == End) break; |
| 243 | // Otherwise, skip the null. |
| 244 | ++Offs, ++Buf; |
| 245 | } |
| 246 | } |
| 247 | LineOffsets.push_back(Offs); |
| 248 | |
| 249 | // Copy the offsets into the FileInfo structure. |
| 250 | FileInfo->NumLines = LineOffsets.size(); |
| 251 | FileInfo->SourceLineCache = new unsigned[LineOffsets.size()]; |
| 252 | std::copy(LineOffsets.begin(), LineOffsets.end(), |
| 253 | FileInfo->SourceLineCache); |
| 254 | } |
| 255 | |
| 256 | // Okay, we know we have a line number table. Do a binary search to find the |
| 257 | // line number that this character position lands on. |
| 258 | unsigned NumLines = FileInfo->NumLines; |
| 259 | unsigned *SourceLineCache = FileInfo->SourceLineCache; |
| 260 | |
| 261 | // TODO: If this is performance sensitive, we could try doing simple radix |
| 262 | // type approaches to make good (tight?) initial guesses based on the |
| 263 | // assumption that all lines are the same average size. |
| 264 | unsigned *Pos = std::lower_bound(SourceLineCache, SourceLineCache+NumLines, |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 265 | getFilePos(Loc)+1); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 266 | return Pos-SourceLineCache; |
| 267 | } |
| 268 | |
Chris Lattner | f6fd68a | 2006-06-26 01:48:23 +0000 | [diff] [blame] | 269 | /// getSourceFilePos - This method returns the *logical* offset from the start |
| 270 | /// of the file that the specified SourceLocation represents. This returns |
| 271 | /// the location of the *logical* character data, not the physical file |
| 272 | /// position. In the case of macros, for example, this returns where the |
| 273 | /// macro was instantiated, not where the characters for the macro can be |
| 274 | /// found. |
| 275 | unsigned SourceManager::getSourceFilePos(SourceLocation Loc) const { |
| 276 | |
| 277 | // If this is a macro, we need to get the instantiation location. |
| 278 | const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(Loc.getFileID()); |
Chris Lattner | dc9f9bf | 2006-06-29 06:33:42 +0000 | [diff] [blame] | 279 | while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) { |
| 280 | Loc = FIDInfo->IncludeLoc; |
| 281 | FIDInfo = getFIDInfo(Loc.getFileID()); |
| 282 | } |
Chris Lattner | f6fd68a | 2006-06-26 01:48:23 +0000 | [diff] [blame] | 283 | |
| 284 | return getFilePos(Loc); |
| 285 | } |
| 286 | |
| 287 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 288 | /// PrintStats - Print statistics to stderr. |
| 289 | /// |
| 290 | void SourceManager::PrintStats() const { |
| 291 | std::cerr << "\n*** Source Manager Stats:\n"; |
| 292 | std::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 293 | << " mem buffers mapped, " << FileIDs.size() |
| 294 | << " file ID's allocated.\n"; |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 295 | unsigned NumBuffers = 0, NumMacros = 0; |
| 296 | for (unsigned i = 0, e = FileIDs.size(); i != e; ++i) { |
| 297 | if (FileIDs[i].IDType == FileIDInfo::NormalBuffer) |
| 298 | ++NumBuffers; |
| 299 | else if (FileIDs[i].IDType == FileIDInfo::MacroExpansion) |
| 300 | ++NumMacros; |
| 301 | else |
| 302 | assert(0 && "Unknown FileID!"); |
| 303 | } |
| 304 | std::cerr << " " << NumBuffers << " normal buffer FileID's, " |
| 305 | << NumMacros << " macro expansion FileID's.\n"; |
| 306 | |
| 307 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 308 | |
| 309 | unsigned NumLineNumsComputed = 0; |
| 310 | unsigned NumFileBytesMapped = 0; |
| 311 | for (std::map<const FileEntry *, FileInfo>::const_iterator I = |
| 312 | FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
| 313 | NumLineNumsComputed += I->second.SourceLineCache != 0; |
| 314 | NumFileBytesMapped += I->second.Buffer->getBufferSize(); |
| 315 | } |
| 316 | std::cerr << NumFileBytesMapped << " bytes of files mapped, " |
| 317 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
| 318 | } |