Skip to content

Commit 1041423

Browse files
[clang][SourceManager] Reuse code when computing Column and Line numbers (#166593)
1 parent e2d2aff commit 1041423

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,10 +1409,15 @@ 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,
1413-
bool *Invalid = nullptr) const;
1414+
bool *Invalid = nullptr) const {
1415+
return getColumnNumber(getSpellingLoc(Loc), Invalid);
1416+
}
14141417
unsigned getExpansionColumnNumber(SourceLocation Loc,
1415-
bool *Invalid = nullptr) const;
1418+
bool *Invalid = nullptr) const {
1419+
return getColumnNumber(getExpansionLoc(Loc), Invalid);
1420+
}
14161421
unsigned getPresumedColumnNumber(SourceLocation Loc,
14171422
bool *Invalid = nullptr) const;
14181423

@@ -1423,8 +1428,15 @@ class SourceManager : public RefCountedBase<SourceManager> {
14231428
/// MemoryBuffer, so this is not cheap: use only when about to emit a
14241429
/// diagnostic.
14251430
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const;
1426-
unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1427-
unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1431+
unsigned getLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1432+
unsigned getSpellingLineNumber(SourceLocation Loc,
1433+
bool *Invalid = nullptr) const {
1434+
return getLineNumber(getSpellingLoc(Loc), Invalid);
1435+
}
1436+
unsigned getExpansionLineNumber(SourceLocation Loc,
1437+
bool *Invalid = nullptr) const {
1438+
return getLineNumber(getExpansionLoc(Loc), Invalid);
1439+
}
14281440
unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
14291441

14301442
/// Return the filename or buffer identifier of the buffer the

clang/lib/Basic/SourceManager.cpp

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

1162-
unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
1163-
bool *Invalid) const {
1164-
if (isInvalid(Loc, Invalid)) return 0;
1165-
FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
1166-
return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1167-
}
1168-
1169-
unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
1170-
bool *Invalid) const {
1162+
unsigned SourceManager::getColumnNumber(SourceLocation Loc,
1163+
bool *Invalid) const {
1164+
assert(Loc.isFileID());
11711165
if (isInvalid(Loc, Invalid)) return 0;
1172-
FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
1166+
FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
11731167
return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
11741168
}
11751169

@@ -1367,18 +1361,13 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
13671361
return LineNo;
13681362
}
13691363

1370-
unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1371-
bool *Invalid) const {
1372-
if (isInvalid(Loc, Invalid)) return 0;
1373-
FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
1374-
return getLineNumber(LocInfo.first, LocInfo.second);
1375-
}
1376-
unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
1377-
bool *Invalid) const {
1364+
unsigned SourceManager::getLineNumber(SourceLocation Loc, bool *Invalid) const {
1365+
assert(Loc.isFileID());
13781366
if (isInvalid(Loc, Invalid)) return 0;
1379-
FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
1367+
FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
13801368
return getLineNumber(LocInfo.first, LocInfo.second);
13811369
}
1370+
13821371
unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
13831372
bool *Invalid) const {
13841373
PresumedLoc PLoc = getPresumedLoc(Loc);

0 commit comments

Comments
 (0)