[clang] 1041423 - [clang][SourceManager] Reuse code when computing Column and Line numbers (#166593)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 5 11:29:54 PST 2025
Author: SKill
Date: 2025-11-05T14:29:50-05:00
New Revision: 1041423393ff64834df793a8bd982fa6c898d5d8
URL: https://github.com/llvm/llvm-project/commit/1041423393ff64834df793a8bd982fa6c898d5d8
DIFF: https://github.com/llvm/llvm-project/commit/1041423393ff64834df793a8bd982fa6c898d5d8.diff
LOG: [clang][SourceManager] Reuse code when computing Column and Line numbers (#166593)
Added:
Modified:
clang/include/clang/Basic/SourceManager.h
clang/lib/Basic/SourceManager.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 6d9d074d78026..bc9e97863556d 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -1409,10 +1409,15 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// before calling this method.
unsigned getColumnNumber(FileID FID, unsigned FilePos,
bool *Invalid = nullptr) const;
+ unsigned getColumnNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
unsigned getSpellingColumnNumber(SourceLocation Loc,
- bool *Invalid = nullptr) const;
+ bool *Invalid = nullptr) const {
+ return getColumnNumber(getSpellingLoc(Loc), Invalid);
+ }
unsigned getExpansionColumnNumber(SourceLocation Loc,
- bool *Invalid = nullptr) const;
+ bool *Invalid = nullptr) const {
+ return getColumnNumber(getExpansionLoc(Loc), Invalid);
+ }
unsigned getPresumedColumnNumber(SourceLocation Loc,
bool *Invalid = nullptr) const;
@@ -1423,8 +1428,15 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// MemoryBuffer, so this is not cheap: use only when about to emit a
/// diagnostic.
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const;
- unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
- unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
+ unsigned getLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
+ unsigned getSpellingLineNumber(SourceLocation Loc,
+ bool *Invalid = nullptr) const {
+ return getLineNumber(getSpellingLoc(Loc), Invalid);
+ }
+ unsigned getExpansionLineNumber(SourceLocation Loc,
+ bool *Invalid = nullptr) const {
+ return getLineNumber(getExpansionLoc(Loc), Invalid);
+ }
unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
/// Return the filename or buffer identifier of the buffer the
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 7dc81c50f87a2..b6cc6ec9365f5 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1159,17 +1159,11 @@ static bool isInvalid(LocType Loc, bool *Invalid) {
return MyInvalid;
}
-unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
- bool *Invalid) const {
- if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
- return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
-}
-
-unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
- bool *Invalid) const {
+unsigned SourceManager::getColumnNumber(SourceLocation Loc,
+ bool *Invalid) const {
+ assert(Loc.isFileID());
if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
+ FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
}
@@ -1367,18 +1361,13 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
return LineNo;
}
-unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
- bool *Invalid) const {
- if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
- return getLineNumber(LocInfo.first, LocInfo.second);
-}
-unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
- bool *Invalid) const {
+unsigned SourceManager::getLineNumber(SourceLocation Loc, bool *Invalid) const {
+ assert(Loc.isFileID());
if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
+ FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
return getLineNumber(LocInfo.first, LocInfo.second);
}
+
unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
bool *Invalid) const {
PresumedLoc PLoc = getPresumedLoc(Loc);
More information about the cfe-commits
mailing list