Skip to content

Commit bb893c3

Browse files
committed
Update SourceManager::getLineNumber to return the correct line # for macro
instantiations. llvm-svn: 38558
1 parent 30709b0 commit bb893c3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

clang/Basic/SourceManager.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,16 @@ unsigned SourceManager::getColumnNumber(SourceLocation IncludePos) const {
159159
/// line offsets for the SourceBuffer, so this is not cheap: use only when
160160
/// about to emit a diagnostic.
161161
unsigned SourceManager::getLineNumber(SourceLocation IncludePos) {
162-
FileInfo *FileInfo = getFileInfo(IncludePos.getFileID());
162+
unsigned FileID = IncludePos.getFileID();
163+
// If this is a macro, we need to get the instantiation location.
164+
const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
165+
if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
166+
IncludePos = FIDInfo->IncludeLoc;
167+
FileID = IncludePos.getFileID();
168+
FIDInfo = getFIDInfo(FileID);
169+
}
170+
171+
FileInfo *FileInfo = getFileInfo(FileID);
163172

164173
// If this is the first use of line information for this buffer, compute the
165174
/// SourceLineCache for it on demand.

clang/include/clang/Basic/SourceManager.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,22 @@ class SourceManager {
258258

259259
/// Return the InfoRec structure for the specified FileID. This is always the
260260
/// physical reference for the ID.
261-
const SrcMgr::InfoRec *getInfoRec(unsigned FileID) const {
262-
const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
263-
261+
const SrcMgr::InfoRec *getInfoRec(const SrcMgr::FileIDInfo *FIDInfo) const {
264262
// For Macros, the physical loc is specified by the MacroTokenFileID.
265263
if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
266264
FIDInfo = &FileIDs[FIDInfo->u.MacroTokenFileID-1];
267265

268266
return FIDInfo->getNormalBufferInfo();
269267
}
268+
const SrcMgr::InfoRec *getInfoRec(unsigned FileID) const {
269+
return getInfoRec(getFIDInfo(FileID));
270+
}
270271

272+
SrcMgr::FileInfo *getFileInfo(const SrcMgr::FileIDInfo *FIDInfo) const {
273+
if (const SrcMgr::InfoRec *IR = getInfoRec(FIDInfo))
274+
return const_cast<SrcMgr::FileInfo *>(&IR->second);
275+
return 0;
276+
}
271277
SrcMgr::FileInfo *getFileInfo(unsigned FileID) const {
272278
if (const SrcMgr::InfoRec *IR = getInfoRec(FileID))
273279
return const_cast<SrcMgr::FileInfo *>(&IR->second);

0 commit comments

Comments
 (0)