[clang] [Clang][NFC] Clarify some SourceManager related code (PR #153527)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 13 19:52:13 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Shafik Yaghmour (shafik)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/153527.diff
2 Files Affected:
- (modified) clang/lib/Basic/SourceManager.cpp (+2-2)
- (modified) clang/lib/Frontend/TextDiagnostic.cpp (+2)
``````````diff
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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/153527
More information about the cfe-commits
mailing list