blob: 43021dcb5730f54dcf5626457355cc5dd829df04 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- 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>
20using namespace llvm;
21using namespace clang;
Chris Lattner5f4b1ff2006-06-20 05:02:40 +000022using namespace SrcMgr;
Chris Lattner22eb9722006-06-18 05:43:12 +000023
24SourceManager::~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 Lattner5f4b1ff2006-06-20 05:02:40 +000040const InfoRec *
Chris Lattner22eb9722006-06-18 05:43:12 +000041SourceManager::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 Lattner35f99852007-04-29 06:08:57 +000050 const SourceBuffer *File = clang::SourceBuffer::getFile(FileEnt);
51 if (File == 0)
Chris Lattner22eb9722006-06-18 05:43:12 +000052 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000053
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 Lattner5f4b1ff2006-06-20 05:02:40 +000067const InfoRec *
Chris Lattner22eb9722006-06-18 05:43:12 +000068SourceManager::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.
82unsigned 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 Lattner5f4b1ff2006-06-20 05:02:40 +000090 FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, 0, File));
Chris Lattner2a904d02006-10-22 06:33:42 +000091 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
92 "Ran out of file ID's!");
Chris Lattner22eb9722006-06-18 05:43:12 +000093 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 Lattner5f4b1ff2006-06-20 05:02:40 +0000101 FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, ChunkNo++, File));
Chris Lattner22eb9722006-06-18 05:43:12 +0000102
103 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
104 FileSize -= (1 << SourceLocation::FilePosBits);
105 }
106
Chris Lattner2a904d02006-10-22 06:33:42 +0000107 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
108 "Ran out of file ID's!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000109 return Result;
110}
111
Chris Lattner7d6a4f62006-06-30 06:10:08 +0000112/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
113/// that a token from physloc PhysLoc should actually be referenced from
114/// InstantiationLoc.
115SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
116 SourceLocation InstantLoc) {
Chris Lattner351050b2006-07-16 18:05:08 +0000117 assert(getFIDInfo(PhysLoc.getFileID())->IDType !=
118 SrcMgr::FileIDInfo::MacroExpansion &&
119 "Location instantiated in a macro?");
120
Chris Lattner4c37a8c2006-06-30 06:15:08 +0000121 // Resolve InstantLoc down to a real logical location.
122 InstantLoc = getLogicalLoc(InstantLoc);
Chris Lattner7fa8c882006-07-20 06:48:52 +0000123
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 Lattner4c37a8c2006-06-30 06:15:08 +0000141 return SourceLocation(InstantiationFileID, PhysLoc.getRawFilePos());
Chris Lattner7d6a4f62006-06-30 06:10:08 +0000142}
143
144
Chris Lattner30709b032006-06-21 03:01:55 +0000145
Chris Lattnerd01e2912006-06-18 16:22:51 +0000146/// getCharacterData - Return a pointer to the start of the specified location
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000147/// in the appropriate SourceBuffer.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000148const char *SourceManager::getCharacterData(SourceLocation SL) const {
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000149 // 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 Lattnerd01e2912006-06-18 16:22:51 +0000155}
156
Chris Lattner685730f2006-06-26 01:36:22 +0000157/// getIncludeLoc - Return the location of the #include for the specified
158/// FileID.
159SourceLocation 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 Lattner2dffd2b2006-06-29 16:44:08 +0000163 if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
Chris Lattner685730f2006-06-26 01:36:22 +0000164 FIDInfo = &FileIDs[FIDInfo->u.MacroTokenFileID-1];
165
166 return FIDInfo->IncludeLoc;
167}
168
Chris Lattnerd01e2912006-06-18 16:22:51 +0000169
Chris Lattner22eb9722006-06-18 05:43:12 +0000170/// 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 Lattner9a13bde2006-06-21 04:57:09 +0000173unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Chris Lattner2dffd2b2006-06-29 16:44:08 +0000174 Loc = getLogicalLoc(Loc);
Chris Lattner9a13bde2006-06-21 04:57:09 +0000175 unsigned FileID = Loc.getFileID();
Chris Lattner22eb9722006-06-18 05:43:12 +0000176 if (FileID == 0) return 0;
Chris Lattner30709b032006-06-21 03:01:55 +0000177
Chris Lattner9a13bde2006-06-21 04:57:09 +0000178 unsigned FilePos = getFilePos(Loc);
Chris Lattner30709b032006-06-21 03:01:55 +0000179 const SourceBuffer *Buffer = getBuffer(FileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000180 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 Lattner9a13bde2006-06-21 04:57:09 +0000188/// 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.
191std::string SourceManager::getSourceName(SourceLocation Loc) {
Chris Lattner2dffd2b2006-06-29 16:44:08 +0000192 Loc = getLogicalLoc(Loc);
Chris Lattner9a13bde2006-06-21 04:57:09 +0000193 unsigned FileID = Loc.getFileID();
194 if (FileID == 0) return "";
Chris Lattner2dffd2b2006-06-29 16:44:08 +0000195 return getFileInfo(FileID)->Buffer->getBufferIdentifier();
Chris Lattner9a13bde2006-06-21 04:57:09 +0000196}
197
198
Chris Lattner22eb9722006-06-18 05:43:12 +0000199/// 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 Lattner9a13bde2006-06-21 04:57:09 +0000203unsigned SourceManager::getLineNumber(SourceLocation Loc) {
Chris Lattner2dffd2b2006-06-29 16:44:08 +0000204 Loc = getLogicalLoc(Loc);
Chris Lattnera85a9d22006-07-02 20:07:52 +0000205 unsigned FileID = Loc.getFileID();
206 if (FileID == 0) return 0;
207 FileInfo *FileInfo = getFileInfo(FileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000208
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 Lattnerd5da3ea2006-07-04 21:11:41 +0000227 // with lots of diagnostics and in -E mode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000228 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 Lattner9a13bde2006-06-21 04:57:09 +0000265 getFilePos(Loc)+1);
Chris Lattner22eb9722006-06-18 05:43:12 +0000266 return Pos-SourceLineCache;
267}
268
Chris Lattnerf6fd68a2006-06-26 01:48:23 +0000269/// 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.
275unsigned 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 Lattnerdc9f9bf2006-06-29 06:33:42 +0000279 while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
280 Loc = FIDInfo->IncludeLoc;
281 FIDInfo = getFIDInfo(Loc.getFileID());
282 }
Chris Lattnerf6fd68a2006-06-26 01:48:23 +0000283
284 return getFilePos(Loc);
285}
286
287
Chris Lattner22eb9722006-06-18 05:43:12 +0000288/// PrintStats - Print statistics to stderr.
289///
290void 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 Lattner30709b032006-06-21 03:01:55 +0000295 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 Lattner22eb9722006-06-18 05:43:12 +0000308
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}