Skip to content

Commit 9a13bde

Browse files
committed
Implement a new SourceManager::getSourceName method
llvm-svn: 38560
1 parent d12ad77 commit 9a13bde

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

clang/Basic/SourceManager.cpp

+28-10
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,18 @@ const char *SourceManager::getCharacterData(SourceLocation SL) const {
133133
/// getColumnNumber - Return the column # for the specified include position.
134134
/// this is significantly cheaper to compute than the line number. This returns
135135
/// zero if the column number isn't known.
136-
unsigned SourceManager::getColumnNumber(SourceLocation IncludePos) const {
137-
unsigned FileID = IncludePos.getFileID();
136+
unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
137+
unsigned FileID = Loc.getFileID();
138138
if (FileID == 0) return 0;
139139

140140
// If this is a macro, we need to get the instantiation location.
141141
const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
142142
if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
143-
IncludePos = FIDInfo->IncludeLoc;
144-
FileID = IncludePos.getFileID();
143+
Loc = FIDInfo->IncludeLoc;
144+
FileID = Loc.getFileID();
145145
}
146146

147-
unsigned FilePos = getFilePos(IncludePos);
147+
unsigned FilePos = getFilePos(Loc);
148148
const SourceBuffer *Buffer = getBuffer(FileID);
149149
const char *Buf = Buffer->getBufferStart();
150150

@@ -154,17 +154,35 @@ unsigned SourceManager::getColumnNumber(SourceLocation IncludePos) const {
154154
return FilePos-LineStart+1;
155155
}
156156

157+
/// 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.
160+
std::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+
157175
/// getLineNumber - Given a SourceLocation, return the physical line number
158176
/// for the position indicated. This requires building and caching a table of
159177
/// line offsets for the SourceBuffer, so this is not cheap: use only when
160178
/// about to emit a diagnostic.
161-
unsigned SourceManager::getLineNumber(SourceLocation IncludePos) {
162-
unsigned FileID = IncludePos.getFileID();
179+
unsigned SourceManager::getLineNumber(SourceLocation Loc) {
180+
unsigned FileID = Loc.getFileID();
163181
// If this is a macro, we need to get the instantiation location.
164182
const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
165183
if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
166-
IncludePos = FIDInfo->IncludeLoc;
167-
FileID = IncludePos.getFileID();
184+
Loc = FIDInfo->IncludeLoc;
185+
FileID = Loc.getFileID();
168186
FIDInfo = getFIDInfo(FileID);
169187
}
170188

@@ -226,7 +244,7 @@ unsigned SourceManager::getLineNumber(SourceLocation IncludePos) {
226244
// type approaches to make good (tight?) initial guesses based on the
227245
// assumption that all lines are the same average size.
228246
unsigned *Pos = std::lower_bound(SourceLineCache, SourceLineCache+NumLines,
229-
getFilePos(IncludePos)+1);
247+
getFilePos(Loc)+1);
230248
return Pos-SourceLineCache;
231249
}
232250

clang/include/clang/Basic/SourceManager.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ class SourceManager {
226226
/// line offsets for the SourceBuffer, so this is not cheap: use only when
227227
/// about to emit a diagnostic.
228228
unsigned getLineNumber(SourceLocation Loc);
229+
230+
/// getSourceName - This method returns the name of the file or buffer that
231+
/// the SourceLocation specifies. This can be modified with #line directives,
232+
/// etc.
233+
std::string getSourceName(SourceLocation Loc);
229234

230235
/// getFileEntryForFileID - Return the FileEntry record for the specified
231236
/// FileID if one exists.
@@ -262,7 +267,6 @@ class SourceManager {
262267
// For Macros, the physical loc is specified by the MacroTokenFileID.
263268
if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
264269
FIDInfo = &FileIDs[FIDInfo->u.MacroTokenFileID-1];
265-
266270
return FIDInfo->getNormalBufferInfo();
267271
}
268272
const SrcMgr::InfoRec *getInfoRec(unsigned FileID) const {

0 commit comments

Comments
 (0)