[clang] [Clang] Warn on backslash-newline-EOF (PR #97585)
Mital Ashok via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 11 08:56:35 PDT 2024
================
@@ -3165,7 +3165,17 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
// C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
// a pedwarn.
- if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) {
+ if (CurPtr != BufferStart) {
+ StringRef LastNewline;
+ if (CurPtr[-1] == '\r' || CurPtr[-1] == '\n') {
+ LastNewline = StringRef(CurPtr - 1, 1);
+ if (CurPtr - 1 != BufferStart && CurPtr[-2] != CurPtr[-1] &&
+ (CurPtr[-2] == '\r' || CurPtr[-2] == '\n')) {
+ // \r\n or \n\r is one newline
----------------
MitalAshok wrote:
https://github.com/llvm/llvm-project/blob/e46468407a7bb7f8b2fe13675a5a1c32b85f8cad/clang/lib/Lex/Lexer.cpp#L1288
https://github.com/llvm/llvm-project/blob/e46468407a7bb7f8b2fe13675a5a1c32b85f8cad/clang/lib/Lex/Lexer.cpp#L2775
And with the current clang:
```c++
int main() { // this line ends with \n\r\
return 1;
}
```
(That is, the file is generated with `python3 -c 'open("test.cpp", "wb").write(b"int main() { // this line ends with \\n\\r\\\n\r return 1;\n}\n")'`)
Exits 0. It seems like Clang has always done this, gcc exits with code 1 here.
The reason given is efficiency:
https://github.com/llvm/llvm-project/blob/e46468407a7bb7f8b2fe13675a5a1c32b85f8cad/clang/lib/Lex/Lexer.cpp#L2711-L2715
Maybe those other places are a mistake since it does affect "correctness", and it should treat \n\r as a Unix style newline followed by an old Mac style newline, but it is currently treated as a single newline so thats what this patch treats it as.
https://github.com/llvm/llvm-project/pull/97585
More information about the cfe-commits
mailing list