Skip to content

Commit 2b15525

Browse files
Reuse code when computing Column and Line numbers
1 parent 95c8750 commit 2b15525

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
14091409
/// before calling this method.
14101410
unsigned getColumnNumber(FileID FID, unsigned FilePos,
14111411
bool *Invalid = nullptr) const;
1412+
unsigned getColumnNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
14121413
unsigned getSpellingColumnNumber(SourceLocation Loc,
14131414
bool *Invalid = nullptr) const;
14141415
unsigned getExpansionColumnNumber(SourceLocation Loc,
@@ -1423,6 +1424,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
14231424
/// MemoryBuffer, so this is not cheap: use only when about to emit a
14241425
/// diagnostic.
14251426
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const;
1427+
unsigned getLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
14261428
unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
14271429
unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
14281430
unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;

clang/lib/Basic/SourceManager.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,18 +1159,22 @@ static bool isInvalid(LocType Loc, bool *Invalid) {
11591159
return MyInvalid;
11601160
}
11611161

1162-
unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
1163-
bool *Invalid) const {
1162+
unsigned SourceManager::getColumnNumber(SourceLocation Loc,
1163+
bool *Invalid) const {
1164+
assert(Loc.isFileID());
11641165
if (isInvalid(Loc, Invalid)) return 0;
1165-
FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
1166+
FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
11661167
return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
11671168
}
11681169

1170+
unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
1171+
bool *Invalid) const {
1172+
return getColumnNumber(getSpellingLoc(Loc), Invalid);
1173+
}
1174+
11691175
unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
11701176
bool *Invalid) const {
1171-
if (isInvalid(Loc, Invalid)) return 0;
1172-
FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
1173-
return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1177+
return getColumnNumber(getExpansionLoc(Loc), Invalid);
11741178
}
11751179

11761180
unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
@@ -1367,17 +1371,19 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
13671371
return LineNo;
13681372
}
13691373

1370-
unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1371-
bool *Invalid) const {
1374+
unsigned SourceManager::getLineNumber(SourceLocation Loc, bool *Invalid) const {
1375+
assert(Loc.isFileID());
13721376
if (isInvalid(Loc, Invalid)) return 0;
1373-
FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
1377+
FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
13741378
return getLineNumber(LocInfo.first, LocInfo.second);
13751379
}
1380+
unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1381+
bool *Invalid) const {
1382+
return getLineNumber(getSpellingLoc(Loc), Invalid);
1383+
}
13761384
unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
13771385
bool *Invalid) const {
1378-
if (isInvalid(Loc, Invalid)) return 0;
1379-
FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
1380-
return getLineNumber(LocInfo.first, LocInfo.second);
1386+
return getLineNumber(getExpansionLoc(Loc), Invalid);
13811387
}
13821388
unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
13831389
bool *Invalid) const {

0 commit comments

Comments
 (0)