[clang] [Clang][NFC] Clarify some SourceManager related code (PR #153527)
Shafik Yaghmour via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 13 19:51:45 PDT 2025
https://github.com/shafik created https://github.com/llvm/llvm-project/pull/153527
Static analysis flagged the columns - 1 code, it was correct but the assumption was not obvious. I document the assumption w/ assertions.
While digging through related code I found getColumnNumber that looks wrong at first inspection and adding parentheses makes it clearer.
>From 6cd84865703310651f910f5f467204a354f49715 Mon Sep 17 00:00:00 2001
From: Shafik Yaghmour <shafik.yaghmour at intel.com>
Date: Wed, 13 Aug 2025 19:46:09 -0700
Subject: [PATCH] [Clang][NFC] Clarify some SourceManager related code
Static analysis flagged the columns - 1 code, it was correct but the assumption
was not obvious. I document the assumption w/ assertions.
While digging through related code I found getColumnNumber that looks wrong
at first inspection and adding parentheses makes it clearer.
---
clang/lib/Basic/SourceManager.cpp | 4 ++--
clang/lib/Frontend/TextDiagnostic.cpp | 2 ++
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 343c26e17236d..d8ec837f0f7b9 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1171,14 +1171,14 @@ unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
if (Buf[FilePos - 1] == '\r' || Buf[FilePos - 1] == '\n')
--FilePos;
}
- return FilePos - LineStart + 1;
+ return (FilePos - LineStart) + 1;
}
}
unsigned LineStart = FilePos;
while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
--LineStart;
- return FilePos-LineStart+1;
+ return (FilePos - LineStart) + 1;
}
// isInvalid - Return the result of calling loc.isInvalid(), and
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index ccdd59da89bd1..cb35257003441 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1095,6 +1095,8 @@ prepareAndFilterRanges(const SmallVectorImpl<CharSourceRange> &Ranges,
unsigned StartColumn = SM.getExpansionColumnNumber(Begin);
unsigned EndColumn = SM.getExpansionColumnNumber(End);
+ assert(StartColumn && "StartColumn has a value of 0");
+ assert(EndColumn && "EndColumn has a value of 0");
if (R.isTokenRange())
EndColumn += Lexer::MeasureTokenLength(End, SM, LangOpts);
More information about the cfe-commits
mailing list