Refactor common functionality into SourceManager::getFileIDSize, no functionality change.

llvm-svn: 138127
This commit is contained in:
Argyrios Kyrtzidis
2011-08-19 22:34:01 +00:00
parent f86615ca5c
commit 34d729d46d
2 changed files with 24 additions and 26 deletions

View File

@@ -914,6 +914,25 @@ public:
return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
}
/// \brief The size of the SLocEnty that \arg FID represents.
unsigned getFileIDSize(FileID FID) const {
bool Invalid = false;
const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
if (Invalid)
return 0;
int ID = FID.ID;
unsigned NextOffset;
if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size()))
NextOffset = getNextLocalOffset();
else if (ID+1 == -1)
NextOffset = MaxLoadedOffset;
else
NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset();
return NextOffset - Entry.getOffset() - 1;
}
/// \brief Given a specific chunk of a FileID (FileID with offset+length),
/// returns true if \arg Loc is inside that chunk and sets relative offset
/// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
@@ -924,21 +943,13 @@ public:
if (Loc.isInvalid())
return false;
unsigned start = getSLocEntry(FID).getOffset() + offset;
unsigned FIDOffs = getSLocEntry(FID).getOffset();
unsigned start = FIDOffs + offset;
unsigned end = start + length;
#ifndef NDEBUG
// Make sure offset/length describe a chunk inside the given FileID.
unsigned NextOffset;
if (FID.ID == -2)
NextOffset = 1U << 31U;
else if (FID.ID+1 == (int)LocalSLocEntryTable.size())
NextOffset = getNextLocalOffset();
else
NextOffset = getSLocEntryByID(FID.ID+1).getOffset();
assert(start < NextOffset);
assert(end < NextOffset);
#endif
assert(start < FIDOffs + getFileIDSize(FID));
assert(end <= FIDOffs + getFileIDSize(FID));
if (Loc.getOffset() >= start && Loc.getOffset() < end) {
if (relativeOffset)

View File

@@ -1498,21 +1498,8 @@ SourceLocation SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) {
// that was lexed.
SourceLocation SpellLoc = Entry.getExpansion().getSpellingLoc();
unsigned NextOffset;
if (ID > 0) {
if (unsigned(ID+1) == local_sloc_entry_size())
NextOffset = getNextLocalOffset();
else
NextOffset = getLocalSLocEntry(ID+1).getOffset();
} else {
if (ID+1 == -1)
NextOffset = MaxLoadedOffset;
else
NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset();
}
unsigned EntrySize = NextOffset - Entry.getOffset() - 1;
unsigned BeginOffs = SpellLoc.getOffset();
unsigned EndOffs = BeginOffs + EntrySize;
unsigned EndOffs = BeginOffs + getFileIDSize(FileID::get(ID));
if (BeginOffs <= Loc.getOffset() && Loc.getOffset() < EndOffs) {
SourceLocation ExpandLoc = SourceLocation::getMacroLoc(Entry.getOffset());
// Replace current Loc with the expanded location and continue.