https://github.com/AaronBallman created https://github.com/llvm/llvm-project/pull/130180
This paper (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf) allows a source file to end without a newline. Clang has supported this as a conforming extension for a long time, so this suppresses the diagnotic in C2y mode but continues to diagnose as an extension in earlier language modes. It also continues to diagnose if the user passes -Wnewline-eof explicitly. >From 517b2b64e8dc67cf33b5e1aaef8d17c567b6b5d7 Mon Sep 17 00:00:00 2001 From: Aaron Ballman <aa...@aaronballman.com> Date: Thu, 6 Mar 2025 16:43:38 -0500 Subject: [PATCH] [C2y] Implement WG14 N3411 This paper allows a source file to end without a newline. Clang has supported this as a conforming extension for a long time, so this suppresses the diagnotic in C2y mode but continues to diagnose as an extension in earlier language modes. It also continues to diagnose if the user passes -Wnewline-eof explicitly. --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/Lex/Lexer.cpp | 15 +++++++-------- clang/test/C/C2y/n3411.c | 14 ++++++++++++++ clang/www/c_status.html | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 clang/test/C/C2y/n3411.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 38b5bdf1dd46b..36aeb6c736627 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -108,6 +108,9 @@ C Language Changes C2y Feature Support ^^^^^^^^^^^^^^^^^^^ +- Implemented N3411 which allows a source file to not end with a newline + character. This is still reported as a conforming extension in earlier + language modes. C23 Feature Support ^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 087c6f13aea66..c62a9f5041183 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -3192,23 +3192,22 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) { DiagnosticsEngine &Diags = PP->getDiagnostics(); SourceLocation EndLoc = getSourceLocation(BufferEnd); - unsigned DiagID; + unsigned DiagID = diag::warn_no_newline_eof; if (LangOpts.CPlusPlus11) { // C++11 [lex.phases] 2.2 p2 // Prefer the C++98 pedantic compatibility warning over the generic, // non-extension, user-requested "missing newline at EOF" warning. - if (!Diags.isIgnored(diag::warn_cxx98_compat_no_newline_eof, EndLoc)) { + if (!Diags.isIgnored(diag::warn_cxx98_compat_no_newline_eof, EndLoc)) DiagID = diag::warn_cxx98_compat_no_newline_eof; - } else { - DiagID = diag::warn_no_newline_eof; - } } else { - DiagID = diag::ext_no_newline_eof; + // This is conforming in C2y, but is an extension in earlier language + // modes. + if (!LangOpts.C2y) + DiagID = diag::ext_no_newline_eof; } - Diag(BufferEnd, DiagID) - << FixItHint::CreateInsertion(EndLoc, "\n"); + Diag(BufferEnd, DiagID) << FixItHint::CreateInsertion(EndLoc, "\n"); } BufferPtr = CurPtr; diff --git a/clang/test/C/C2y/n3411.c b/clang/test/C/C2y/n3411.c new file mode 100644 index 0000000000000..edfc1c16fe365 --- /dev/null +++ b/clang/test/C/C2y/n3411.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -verify=c2y -std=c2y -Wall -pedantic %s +// RUN: %clang_cc1 -verify -Wnewline-eof -std=c2y -Wall -pedantic %s +// RUN: %clang_cc1 -verify -std=c23 -Wall -pedantic %s + +/* WG14 N3411: Yes + * Slay Some Earthly Demons XII + * + * Allow a non-empty source file to end without a final newline character. Note + * that this file intentionally does not end with a trailing newline. + */ +// c2y-no-diagnostics + +int x; // Ensure the file contains at least one declaration. +// expected-warning {{no newline at end of file}} \ No newline at end of file diff --git a/clang/www/c_status.html b/clang/www/c_status.html index 4e912987b69c6..e8b50a37093f9 100644 --- a/clang/www/c_status.html +++ b/clang/www/c_status.html @@ -294,7 +294,7 @@ <h2 id="c2y">C2y implementation status</h2> <tr> <td>Slay Some Earthly Demons XII</td> <td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf">N3411</a></td> - <td class="unknown" align="center">Unknown</td> + <td class="unreleased" align="center">Clang 21</td> </tr> <tr> <td>Slay Some Earthly Demons XIII</td> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits