-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[Clang][NFC] Clarify some SourceManager related code #153527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][NFC] Clarify some SourceManager related code #153527
Conversation
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.
|
@llvm/pr-subscribers-clang Author: Shafik Yaghmour (shafik) ChangesStatic 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:
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);
|
AaronBallman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo Mariya's request for expanded assert explanations.
Fznamznon
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as someone dealing with c/c++ I don't really get why 0 column number is invalid, but that is already clearer, thanks
Lines and columns are one-based, but now I wonder if this change is correct because we do accept zero-based line numbers as a GNU extension. So it is possible to get a zero-based value, and we currently mess up with it: https://godbolt.org/z/8KMY3j7T9 Note how the second warning says @shafik does that code now cause an assertion? |
AaronBallman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requesting changes so we don't accidentally land; a new question came up.
It does not assert, it would have failed testing we have several lines in tests that test that diagnostic "directive with zero argument is a GNU extension" |
It looks like they are just using zero as invalid and then you need to subtract 1, we can see SourceManager also says they return zero for invalid:
I think in modern code we would use optional instead. |
AaronBallman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM again!
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.