[cfe-commits] r38594 - in /cfe/cfe/trunk: Basic/SourceManager.cpp include/clang/Basic/SourceManager.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:23:02 PDT 2007
Author: sabre
Date: Wed Jul 11 11:23:02 2007
New Revision: 38594
URL: http://llvm.org/viewvc/llvm-project?rev=38594&view=rev
Log:
Factor logical line lookup better.
Modified:
cfe/cfe/trunk/Basic/SourceManager.cpp
cfe/cfe/trunk/include/clang/Basic/SourceManager.h
Modified: cfe/cfe/trunk/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Basic/SourceManager.cpp?rev=38594&r1=38593&r2=38594&view=diff
==============================================================================
--- cfe/cfe/trunk/Basic/SourceManager.cpp (original)
+++ cfe/cfe/trunk/Basic/SourceManager.cpp Wed Jul 11 11:23:02 2007
@@ -115,6 +115,7 @@
///
unsigned SourceManager::createFileIDForMacroExp(SourceLocation SourcePos,
unsigned PhysicalFileID) {
+ SourcePos = getLogicalLoc(SourcePos);
FileIDs.push_back(FileIDInfo::getMacroExpansion(SourcePos, PhysicalFileID));
return FileIDs.size();
}
@@ -135,7 +136,7 @@
const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
// For Macros, the physical loc is specified by the MacroTokenFileID.
- while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
+ if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
FIDInfo = &FileIDs[FIDInfo->u.MacroTokenFileID-1];
return FIDInfo->IncludeLoc;
@@ -146,17 +147,10 @@
/// this is significantly cheaper to compute than the line number. This returns
/// zero if the column number isn't known.
unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
+ Loc = getLogicalLoc(Loc);
unsigned FileID = Loc.getFileID();
if (FileID == 0) return 0;
- // If this is a macro, we need to get the instantiation location.
- const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
- while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
- Loc = FIDInfo->IncludeLoc;
- FileID = Loc.getFileID();
- FIDInfo = getFIDInfo(FileID);
- }
-
unsigned FilePos = getFilePos(Loc);
const SourceBuffer *Buffer = getBuffer(FileID);
const char *Buf = Buffer->getBufferStart();
@@ -171,17 +165,10 @@
/// the SourceLocation specifies. This can be modified with #line directives,
/// etc.
std::string SourceManager::getSourceName(SourceLocation Loc) {
+ Loc = getLogicalLoc(Loc);
unsigned FileID = Loc.getFileID();
if (FileID == 0) return "";
-
- // If this is a macro, we need to get the instantiation location.
- const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
- while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
- Loc = FIDInfo->IncludeLoc;
- FIDInfo = getFIDInfo(Loc.getFileID());
- }
-
- return getFileInfo(FIDInfo)->Buffer->getBufferIdentifier();
+ return getFileInfo(FileID)->Buffer->getBufferIdentifier();
}
@@ -190,16 +177,8 @@
/// line offsets for the SourceBuffer, so this is not cheap: use only when
/// about to emit a diagnostic.
unsigned SourceManager::getLineNumber(SourceLocation Loc) {
- unsigned FileID = Loc.getFileID();
- // If this is a macro, we need to get the instantiation location.
- const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
- while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
- Loc = FIDInfo->IncludeLoc;
- FileID = Loc.getFileID();
- FIDInfo = getFIDInfo(FileID);
- }
-
- FileInfo *FileInfo = getFileInfo(FileID);
+ Loc = getLogicalLoc(Loc);
+ FileInfo *FileInfo = getFileInfo(Loc.getFileID());
// If this is the first use of line information for this buffer, compute the
/// SourceLineCache for it on demand.
Modified: cfe/cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/SourceManager.h?rev=38594&r1=38593&r2=38594&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/cfe/trunk/include/clang/Basic/SourceManager.h Wed Jul 11 11:23:02 2007
@@ -252,6 +252,17 @@
return FileIDs[FileID-1].getNormalBufferInfo()->first;
}
+ /// Given a SourceLocation object, return the logical location referenced by
+ /// the ID. This logical location is subject to #line directives, etc.
+ SourceLocation getLogicalLoc(SourceLocation Loc) const {
+ if (Loc.getFileID() == 0) return Loc;
+
+ const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(Loc.getFileID());
+ if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
+ return FIDInfo->IncludeLoc;
+ return Loc;
+ }
+
/// PrintStats - Print statistics to stderr.
///
void PrintStats() const;
More information about the cfe-commits
mailing list