[clang][SourceManager] Reuse code when computing Column and Line numbers (#166593)
GitOrigin-RevId: 1041423393ff64834df793a8bd982fa6c898d5d8
diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h
index 6d9d074..bc9e978 100644
--- a/include/clang/Basic/SourceManager.h
+++ b/include/clang/Basic/SourceManager.h
@@ -1409,10 +1409,15 @@
/// 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 @@
/// 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/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp
index 7dc81c5..b6cc6ec 100644
--- a/lib/Basic/SourceManager.cpp
+++ b/lib/Basic/SourceManager.cpp
@@ -1159,17 +1159,11 @@
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);
- return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
-}
-
-unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
- bool *Invalid) const {
- 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 @@
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::getExpansionLineNumber(SourceLocation Loc,
- bool *Invalid) const {
- if (isInvalid(Loc, Invalid)) return 0;
- FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
- return getLineNumber(LocInfo.first, LocInfo.second);
-}
+
unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
bool *Invalid) const {
PresumedLoc PLoc = getPresumedLoc(Loc);