Skip to content

Suppress pedantic diagnostic for a file not ending in EOL #131794

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

Merged
merged 7 commits into from
Mar 19, 2025

Conversation

AaronBallman
Copy link
Collaborator

@AaronBallman AaronBallman commented Mar 18, 2025

WG14 added N3411 to the list of papers which apply to older versions of C in C2y, and WG21 adopted CWG787 as a Defect Report in C++11. So we no longer should be issuing a pedantic diagnostic about a file which does not end with a newline character.

We do, however, continue to support -Wnewline-eof as an opt-in diagnostic.

WG14 added N3411 to the list of papers which apply to older versions of
C, and WG21 adopted CWG787 as a Defect Report in C++11. So we no longer
should be issuing a pedantic diagnostic about a file which does not end
with a newline character.

We do, however, continue to support -Wnewline-eof as an opt-in
diagnostic.
@AaronBallman AaronBallman added c c++ clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer labels Mar 18, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang-tools-extra clangd clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clangd

Author: Aaron Ballman (AaronBallman)

Changes

WG14 added N3411 to the list of papers which apply to older versions of C in C2y, and WG21 adopted CWG787 as a Defect Report in C++11. So we no longer should be issuing a pedantic diagnostic about a file which does not end with a newline character.

We do, however, continue to support -Wnewline-eof as an opt-in diagnostic.


Full diff: https://github.com/llvm/llvm-project/pull/131794.diff

6 Files Affected:

  • (modified) clang-tools-extra/clangd/Preamble.cpp (-1)
  • (modified) clang/docs/ReleaseNotes.rst (+4-1)
  • (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (-2)
  • (modified) clang/lib/Lex/Lexer.cpp (+6-12)
  • (modified) clang/test/C/C2y/n3411.c (+2-1)
  • (modified) clang/test/Lexer/newline-eof.c (+10-5)
diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp
index b634981bb46bd..233d827e6ed7b 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -628,7 +628,6 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
     switch (Info.getID()) {
     case diag::warn_no_newline_eof:
     case diag::warn_cxx98_compat_no_newline_eof:
-    case diag::ext_no_newline_eof:
       // If the preamble doesn't span the whole file, drop the no newline at
       // eof warnings.
       return Bounds.Size != ContentsBuffer->getBufferSize()
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0adbc19f40096..71d7f9f85e739 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -128,7 +128,10 @@ C2y Feature Support
   also removes UB with code like ``(void)(void)1;``.
 - Implemented `WG14 N3411 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf>`_
   which allows a source file to not end with a newline character. This is still
-  reported as a conforming extension in earlier language modes.
+  reported as a conforming extension in earlier language modes. Note,
+  ``-pedantic`` will no longer diagnose this in either C or C++ modes as an
+  extension. This feature was adopted as applying to obsolete versions of C in
+  WG14 and as a defect report in WG21 (CWG787).
 - Implemented `WG14 N3353 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3353.htm>_`
   which adds the new ``0o`` and ``0O`` ocal literal prefixes and deprecates
   octal literals other than ``0`` which do not start with the new prefix. This
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index bdb7e9350b5f7..cf226856864c0 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -52,8 +52,6 @@ def ext_multi_line_line_comment : Extension<"multi-line // comment">,
 def ext_line_comment : Extension<
   "// comments are not allowed in this language">,
   InGroup<Comment>;
-def ext_no_newline_eof : Extension<"no newline at end of file">,
-  InGroup<NewlineEOF>;
 def warn_no_newline_eof : Warning<"no newline at end of file">,
   InGroup<NewlineEOF>, DefaultIgnore;
 
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 96d5d4f440768..fcc53f91b5728 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -3194,18 +3194,12 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
     SourceLocation EndLoc = getSourceLocation(BufferEnd);
     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))
-        DiagID = diag::warn_cxx98_compat_no_newline_eof;
-    } else {
-      // This is conforming in C2y, but is an extension in earlier language
-      // modes.
-      if (!LangOpts.C2y)
-        DiagID = diag::ext_no_newline_eof;
-    }
+    // 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 (LangOpts.CPlusPlus11 &&
+        !Diags.isIgnored(diag::warn_cxx98_compat_no_newline_eof, EndLoc))
+      DiagID = diag::warn_cxx98_compat_no_newline_eof;
 
     Diag(BufferEnd, DiagID) << FixItHint::CreateInsertion(EndLoc, "\n");
   }
diff --git a/clang/test/C/C2y/n3411.c b/clang/test/C/C2y/n3411.c
index 934a7c70fa67e..0fea769e30460 100644
--- a/clang/test/C/C2y/n3411.c
+++ b/clang/test/C/C2y/n3411.c
@@ -1,7 +1,8 @@
 // RUN: %clang_cc1 -verify=good -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
+// RUN: %clang_cc1 -verify=good -std=c23 -Wall -pedantic %s
 // RUN: %clang_cc1 -verify=good -std=c23 %s
+// RUN: %clang_cc1 -verify -Wnewline-eof -std=c23 %s
 
 /* WG14 N3411: Yes
  * Slay Some Earthly Demons XII
diff --git a/clang/test/Lexer/newline-eof.c b/clang/test/Lexer/newline-eof.c
index 9f5033384e16f..22245ac357efc 100644
--- a/clang/test/Lexer/newline-eof.c
+++ b/clang/test/Lexer/newline-eof.c
@@ -1,11 +1,16 @@
-// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
-// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
-// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++03 -pedantic -verify %s
-// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
+// Allowing a file to end without a newline was adopted as a Defect Report in
+// WG21 (CWG787) and in WG14 (added to the list of changes which apply to
+// earlier revisions of C in C2y). So it should not issue a pedantic diagnostic
+// in any language mode.
 
-// In C++11 mode, this is allowed, so don't warn in pedantic mode.
+// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
+// RUN: %clang_cc1 -fsyntax-only -pedantic -verify=good %s
+// RUN: %clang_cc1 -fsyntax-only -std=c89 -pedantic -Wno-comment -verify=good %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++03 -pedantic -verify=good %s
 // RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++11 -Wnewline-eof -verify %s
 // RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++11 -Werror -pedantic %s
+// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
+// good-no-diagnostics
 
 // Make sure the diagnostic shows up properly at the end of the last line.
 // CHECK: newline-eof.c:[[@LINE+3]]:67

@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

WG14 added N3411 to the list of papers which apply to older versions of C in C2y, and WG21 adopted CWG787 as a Defect Report in C++11. So we no longer should be issuing a pedantic diagnostic about a file which does not end with a newline character.

We do, however, continue to support -Wnewline-eof as an opt-in diagnostic.


Full diff: https://github.com/llvm/llvm-project/pull/131794.diff

6 Files Affected:

  • (modified) clang-tools-extra/clangd/Preamble.cpp (-1)
  • (modified) clang/docs/ReleaseNotes.rst (+4-1)
  • (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (-2)
  • (modified) clang/lib/Lex/Lexer.cpp (+6-12)
  • (modified) clang/test/C/C2y/n3411.c (+2-1)
  • (modified) clang/test/Lexer/newline-eof.c (+10-5)
diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp
index b634981bb46bd..233d827e6ed7b 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -628,7 +628,6 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
     switch (Info.getID()) {
     case diag::warn_no_newline_eof:
     case diag::warn_cxx98_compat_no_newline_eof:
-    case diag::ext_no_newline_eof:
       // If the preamble doesn't span the whole file, drop the no newline at
       // eof warnings.
       return Bounds.Size != ContentsBuffer->getBufferSize()
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0adbc19f40096..71d7f9f85e739 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -128,7 +128,10 @@ C2y Feature Support
   also removes UB with code like ``(void)(void)1;``.
 - Implemented `WG14 N3411 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf>`_
   which allows a source file to not end with a newline character. This is still
-  reported as a conforming extension in earlier language modes.
+  reported as a conforming extension in earlier language modes. Note,
+  ``-pedantic`` will no longer diagnose this in either C or C++ modes as an
+  extension. This feature was adopted as applying to obsolete versions of C in
+  WG14 and as a defect report in WG21 (CWG787).
 - Implemented `WG14 N3353 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3353.htm>_`
   which adds the new ``0o`` and ``0O`` ocal literal prefixes and deprecates
   octal literals other than ``0`` which do not start with the new prefix. This
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index bdb7e9350b5f7..cf226856864c0 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -52,8 +52,6 @@ def ext_multi_line_line_comment : Extension<"multi-line // comment">,
 def ext_line_comment : Extension<
   "// comments are not allowed in this language">,
   InGroup<Comment>;
-def ext_no_newline_eof : Extension<"no newline at end of file">,
-  InGroup<NewlineEOF>;
 def warn_no_newline_eof : Warning<"no newline at end of file">,
   InGroup<NewlineEOF>, DefaultIgnore;
 
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 96d5d4f440768..fcc53f91b5728 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -3194,18 +3194,12 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
     SourceLocation EndLoc = getSourceLocation(BufferEnd);
     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))
-        DiagID = diag::warn_cxx98_compat_no_newline_eof;
-    } else {
-      // This is conforming in C2y, but is an extension in earlier language
-      // modes.
-      if (!LangOpts.C2y)
-        DiagID = diag::ext_no_newline_eof;
-    }
+    // 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 (LangOpts.CPlusPlus11 &&
+        !Diags.isIgnored(diag::warn_cxx98_compat_no_newline_eof, EndLoc))
+      DiagID = diag::warn_cxx98_compat_no_newline_eof;
 
     Diag(BufferEnd, DiagID) << FixItHint::CreateInsertion(EndLoc, "\n");
   }
diff --git a/clang/test/C/C2y/n3411.c b/clang/test/C/C2y/n3411.c
index 934a7c70fa67e..0fea769e30460 100644
--- a/clang/test/C/C2y/n3411.c
+++ b/clang/test/C/C2y/n3411.c
@@ -1,7 +1,8 @@
 // RUN: %clang_cc1 -verify=good -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
+// RUN: %clang_cc1 -verify=good -std=c23 -Wall -pedantic %s
 // RUN: %clang_cc1 -verify=good -std=c23 %s
+// RUN: %clang_cc1 -verify -Wnewline-eof -std=c23 %s
 
 /* WG14 N3411: Yes
  * Slay Some Earthly Demons XII
diff --git a/clang/test/Lexer/newline-eof.c b/clang/test/Lexer/newline-eof.c
index 9f5033384e16f..22245ac357efc 100644
--- a/clang/test/Lexer/newline-eof.c
+++ b/clang/test/Lexer/newline-eof.c
@@ -1,11 +1,16 @@
-// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
-// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
-// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++03 -pedantic -verify %s
-// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
+// Allowing a file to end without a newline was adopted as a Defect Report in
+// WG21 (CWG787) and in WG14 (added to the list of changes which apply to
+// earlier revisions of C in C2y). So it should not issue a pedantic diagnostic
+// in any language mode.
 
-// In C++11 mode, this is allowed, so don't warn in pedantic mode.
+// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
+// RUN: %clang_cc1 -fsyntax-only -pedantic -verify=good %s
+// RUN: %clang_cc1 -fsyntax-only -std=c89 -pedantic -Wno-comment -verify=good %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++03 -pedantic -verify=good %s
 // RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++11 -Wnewline-eof -verify %s
 // RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++11 -Werror -pedantic %s
+// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
+// good-no-diagnostics
 
 // Make sure the diagnostic shows up properly at the end of the last line.
 // CHECK: newline-eof.c:[[@LINE+3]]:67

Note: I wasn't able to run the python script to regenerate the
cxx_dr_status.html page due to encoding errors, so that still needs to
be run.
@AaronBallman AaronBallman requested a review from Endilll as a code owner March 18, 2025 13:17
Review comments pointed out that this is no longer a necessary
diagnostic given that C++11 adopted the change as a defect report.
Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cxx_dr_status.html entry should be updated, too.
It will be trivial to do once #131812 is merged.

Copy link
Collaborator

@zygoloid zygoloid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

@Endilll
Copy link
Contributor

Endilll commented Mar 18, 2025

Both #131812 and #131816 have been merged, so you can update your branch and run the script now.

@AaronBallman
Copy link
Collaborator Author

Both #131812 and #131816 have been merged, so you can update your branch and run the script now.

Thank you! I re-ran the script and it seems to have an issue with line endings because the diff shows changes on every line in the file. I'm going to land these changes as-is, the script can be fixed up and list regenerated in a follow-up.

@AaronBallman AaronBallman merged commit 449cdfa into llvm:main Mar 19, 2025
12 checks passed
@AaronBallman AaronBallman deleted the aballman-newline-eol-pedantic branch March 19, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c c++ clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category clang-tools-extra clangd
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants