[clang] [clang][SourceManager] Reuse code when computing Column and Line numbers (PR #166593)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 5 09:07:43 PST 2025
https://github.com/SergejSalnikov updated https://github.com/llvm/llvm-project/pull/166593
>From 2b15525dc77371424b84e93a65738453699da46a Mon Sep 17 00:00:00 2001
From: SKill <skill at google.com>
Date: Wed, 5 Nov 2025 17:59:25 +0100
Subject: [PATCH 1/2] Reuse code when computing Column and Line numbers
---
clang/include/clang/Basic/SourceManager.h | 2 ++
clang/lib/Basic/SourceManager.cpp | 30 ++++++++++++++---------
2 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 6d9d074d78026..a6984f7b06cbc 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -1409,6 +1409,7 @@ 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;
unsigned getExpansionColumnNumber(SourceLocation Loc,
@@ -1423,6 +1424,7 @@ 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 getLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 7dc81c50f87a2..01faeb67597a2 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1159,18 +1159,22 @@ static bool isInvalid(LocType Loc, bool *Invalid) {
return MyInvalid;
}
-unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
- bool *Invalid) const {
+unsigned SourceManager::getColumnNumber(SourceLocation Loc,
+ bool *Invalid) const {
+ assert(Loc.isFileID());
if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
+ FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
}
+unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
+ bool *Invalid) const {
+ return getColumnNumber(getSpellingLoc(Loc), Invalid);
+}
+
unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
bool *Invalid) const {
- if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
- return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
+ return getColumnNumber(getExpansionLoc(Loc), Invalid);
}
unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
@@ -1367,17 +1371,19 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
return LineNo;
}
-unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
- bool *Invalid) const {
+unsigned SourceManager::getLineNumber(SourceLocation Loc, bool *Invalid) const {
+ assert(Loc.isFileID());
if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedSpellingLoc(Loc);
+ FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
return getLineNumber(LocInfo.first, LocInfo.second);
}
+unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
+ bool *Invalid) const {
+ return getLineNumber(getSpellingLoc(Loc), Invalid);
+}
unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
bool *Invalid) const {
- if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
- return getLineNumber(LocInfo.first, LocInfo.second);
+ return getLineNumber(getExpansionLoc(Loc), Invalid);
}
unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
bool *Invalid) const {
>From 05724b813b08fbbef031751a87d7a7ba23741e92 Mon Sep 17 00:00:00 2001
From: SKill <skill at google.com>
Date: Wed, 5 Nov 2025 18:07:34 +0100
Subject: [PATCH 2/2] Move short methods to .h file
---
clang/include/clang/Basic/SourceManager.h | 16 ++++++++++++----
clang/lib/Basic/SourceManager.cpp | 19 +------------------
2 files changed, 13 insertions(+), 22 deletions(-)
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index a6984f7b06cbc..6b7daebb32c1a 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -1411,9 +1411,13 @@ class SourceManager : public RefCountedBase<SourceManager> {
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;
@@ -1425,8 +1429,12 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// diagnostic.
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const;
unsigned getLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
- unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
- unsigned getExpansionLineNumber(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 01faeb67597a2..b6cc6ec9365f5 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1167,16 +1167,6 @@ unsigned SourceManager::getColumnNumber(SourceLocation Loc,
return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
}
-unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
- bool *Invalid) const {
- return getColumnNumber(getSpellingLoc(Loc), Invalid);
-}
-
-unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
- bool *Invalid) const {
- return getColumnNumber(getExpansionLoc(Loc), Invalid);
-}
-
unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
bool *Invalid) const {
PresumedLoc PLoc = getPresumedLoc(Loc);
@@ -1377,14 +1367,7 @@ unsigned SourceManager::getLineNumber(SourceLocation Loc, bool *Invalid) const {
FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
return getLineNumber(LocInfo.first, LocInfo.second);
}
-unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
- bool *Invalid) const {
- return getLineNumber(getSpellingLoc(Loc), Invalid);
-}
-unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
- bool *Invalid) const {
- return getLineNumber(getExpansionLoc(Loc), Invalid);
-}
+
unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
bool *Invalid) const {
PresumedLoc PLoc = getPresumedLoc(Loc);
More information about the cfe-commits
mailing list