blob: 7a6542ee96c7ec3f309e4f8973ceb5e8e8e7ea36 [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.
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 Lattner5f4b1ff2006-06-20 05:02:40 +000072const InfoRec *
Chris Lattner22eb9722006-06-18 05:43:12 +000073SourceManager::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.
87unsigned 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 Lattner5f4b1ff2006-06-20 05:02:40 +000095 FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, 0, File));
Chris Lattner22eb9722006-06-18 05:43:12 +000096 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 Lattner5f4b1ff2006-06-20 05:02:40 +0000104 FileIDs.push_back(FileIDInfo::getNormalBuffer(IncludePos, ChunkNo++, File));
Chris Lattner22eb9722006-06-18 05:43:12 +0000105
106 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
107 FileSize -= (1 << SourceLocation::FilePosBits);
108 }
109
110 return Result;
111}
112
Chris Lattner30709b032006-06-21 03:01:55 +0000113/// createFileIDForMacroExp - Return a new FileID for a macro expansion at
114/// SourcePos, where the macro token character came from PhysicalFileID.
115///
116unsigned SourceManager::createFileIDForMacroExp(SourceLocation SourcePos,
117 unsigned PhysicalFileID) {
118 FileIDs.push_back(FileIDInfo::getMacroExpansion(SourcePos, PhysicalFileID));
119 return FileIDs.size();
120}
121
122
Chris Lattnerd01e2912006-06-18 16:22:51 +0000123/// getCharacterData - Return a pointer to the start of the specified location
124/// in the appropriate SourceBuffer. This returns null if it cannot be
125/// computed (e.g. invalid SourceLocation).
126const char *SourceManager::getCharacterData(SourceLocation SL) const {
127 if (unsigned FileID = SL.getFileID())
128 return getFileInfo(FileID)->Buffer->getBufferStart() + getFilePos(SL);
129 return 0;
130}
131
132
Chris Lattner22eb9722006-06-18 05:43:12 +0000133/// getColumnNumber - Return the column # for the specified include position.
134/// this is significantly cheaper to compute than the line number. This returns
135/// zero if the column number isn't known.
Chris Lattner9a13bde2006-06-21 04:57:09 +0000136unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
137 unsigned FileID = Loc.getFileID();
Chris Lattner22eb9722006-06-18 05:43:12 +0000138 if (FileID == 0) return 0;
Chris Lattner30709b032006-06-21 03:01:55 +0000139
140 // If this is a macro, we need to get the instantiation location.
141 const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
142 if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
Chris Lattner9a13bde2006-06-21 04:57:09 +0000143 Loc = FIDInfo->IncludeLoc;
144 FileID = Loc.getFileID();
Chris Lattner30709b032006-06-21 03:01:55 +0000145 }
146
Chris Lattner9a13bde2006-06-21 04:57:09 +0000147 unsigned FilePos = getFilePos(Loc);
Chris Lattner30709b032006-06-21 03:01:55 +0000148 const SourceBuffer *Buffer = getBuffer(FileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000149 const char *Buf = Buffer->getBufferStart();
150
151 unsigned LineStart = FilePos;
152 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
153 --LineStart;
154 return FilePos-LineStart+1;
155}
156
Chris Lattner9a13bde2006-06-21 04:57:09 +0000157/// getSourceName - This method returns the name of the file or buffer that
158/// the SourceLocation specifies. This can be modified with #line directives,
159/// etc.
160std::string SourceManager::getSourceName(SourceLocation Loc) {
161 unsigned FileID = Loc.getFileID();
162 if (FileID == 0) return "";
163
164 // If this is a macro, we need to get the instantiation location.
165 const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
166 if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
167 Loc = FIDInfo->IncludeLoc;
168 FIDInfo = getFIDInfo(Loc.getFileID());
169 }
170
171 return getFileInfo(FIDInfo)->Buffer->getBufferIdentifier();
172}
173
174
Chris Lattner22eb9722006-06-18 05:43:12 +0000175/// getLineNumber - Given a SourceLocation, return the physical line number
176/// for the position indicated. This requires building and caching a table of
177/// line offsets for the SourceBuffer, so this is not cheap: use only when
178/// about to emit a diagnostic.
Chris Lattner9a13bde2006-06-21 04:57:09 +0000179unsigned SourceManager::getLineNumber(SourceLocation Loc) {
180 unsigned FileID = Loc.getFileID();
Chris Lattnerbb893c32006-06-21 03:27:29 +0000181 // If this is a macro, we need to get the instantiation location.
182 const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
183 if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
Chris Lattner9a13bde2006-06-21 04:57:09 +0000184 Loc = FIDInfo->IncludeLoc;
185 FileID = Loc.getFileID();
Chris Lattnerbb893c32006-06-21 03:27:29 +0000186 FIDInfo = getFIDInfo(FileID);
187 }
188
189 FileInfo *FileInfo = getFileInfo(FileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000190
191 // If this is the first use of line information for this buffer, compute the
192 /// SourceLineCache for it on demand.
193 if (FileInfo->SourceLineCache == 0) {
194 const SourceBuffer *Buffer = FileInfo->Buffer;
195
196 // Find the file offsets of all of the *physical* source lines. This does
197 // not look at trigraphs, escaped newlines, or anything else tricky.
198 std::vector<unsigned> LineOffsets;
199
200 // Line #1 starts at char 0.
201 LineOffsets.push_back(0);
202
203 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
204 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
205 unsigned Offs = 0;
206 while (1) {
207 // Skip over the contents of the line.
208 // TODO: Vectorize this? This is very performance sensitive for programs
209 // with lots of diagnostics.
210 const unsigned char *NextBuf = (const unsigned char *)Buf;
211 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
212 ++NextBuf;
213 Offs += NextBuf-Buf;
214 Buf = NextBuf;
215
216 if (Buf[0] == '\n' || Buf[0] == '\r') {
217 // If this is \n\r or \r\n, skip both characters.
218 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
219 ++Offs, ++Buf;
220 ++Offs, ++Buf;
221 LineOffsets.push_back(Offs);
222 } else {
223 // Otherwise, this is a null. If end of file, exit.
224 if (Buf == End) break;
225 // Otherwise, skip the null.
226 ++Offs, ++Buf;
227 }
228 }
229 LineOffsets.push_back(Offs);
230
231 // Copy the offsets into the FileInfo structure.
232 FileInfo->NumLines = LineOffsets.size();
233 FileInfo->SourceLineCache = new unsigned[LineOffsets.size()];
234 std::copy(LineOffsets.begin(), LineOffsets.end(),
235 FileInfo->SourceLineCache);
236 }
237
238 // Okay, we know we have a line number table. Do a binary search to find the
239 // line number that this character position lands on.
240 unsigned NumLines = FileInfo->NumLines;
241 unsigned *SourceLineCache = FileInfo->SourceLineCache;
242
243 // TODO: If this is performance sensitive, we could try doing simple radix
244 // type approaches to make good (tight?) initial guesses based on the
245 // assumption that all lines are the same average size.
246 unsigned *Pos = std::lower_bound(SourceLineCache, SourceLineCache+NumLines,
Chris Lattner9a13bde2006-06-21 04:57:09 +0000247 getFilePos(Loc)+1);
Chris Lattner22eb9722006-06-18 05:43:12 +0000248 return Pos-SourceLineCache;
249}
250
251/// PrintStats - Print statistics to stderr.
252///
253void SourceManager::PrintStats() const {
254 std::cerr << "\n*** Source Manager Stats:\n";
255 std::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
256 << " mem buffers mapped, " << FileIDs.size()
257 << " file ID's allocated.\n";
Chris Lattner30709b032006-06-21 03:01:55 +0000258 unsigned NumBuffers = 0, NumMacros = 0;
259 for (unsigned i = 0, e = FileIDs.size(); i != e; ++i) {
260 if (FileIDs[i].IDType == FileIDInfo::NormalBuffer)
261 ++NumBuffers;
262 else if (FileIDs[i].IDType == FileIDInfo::MacroExpansion)
263 ++NumMacros;
264 else
265 assert(0 && "Unknown FileID!");
266 }
267 std::cerr << " " << NumBuffers << " normal buffer FileID's, "
268 << NumMacros << " macro expansion FileID's.\n";
269
270
Chris Lattner22eb9722006-06-18 05:43:12 +0000271
272 unsigned NumLineNumsComputed = 0;
273 unsigned NumFileBytesMapped = 0;
274 for (std::map<const FileEntry *, FileInfo>::const_iterator I =
275 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
276 NumLineNumsComputed += I->second.SourceLineCache != 0;
277 NumFileBytesMapped += I->second.Buffer->getBufferSize();
278 }
279 std::cerr << NumFileBytesMapped << " bytes of files mapped, "
280 << NumLineNumsComputed << " files with line #'s computed.\n";
281}