Skip to content

Commit 449cdfa

Browse files
authored
Suppress pedantic diagnostic for a file not ending in EOL (#131794)
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.
1 parent e0db416 commit 449cdfa

File tree

8 files changed

+36
-44
lines changed

8 files changed

+36
-44
lines changed

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,8 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
627627
return DiagnosticsEngine::Ignored;
628628
switch (Info.getID()) {
629629
case diag::warn_no_newline_eof:
630-
case diag::warn_cxx98_compat_no_newline_eof:
631-
case diag::ext_no_newline_eof:
632630
// If the preamble doesn't span the whole file, drop the no newline at
633-
// eof warnings.
631+
// eof warning.
634632
return Bounds.Size != ContentsBuffer->getBufferSize()
635633
? DiagnosticsEngine::Level::Ignored
636634
: DiagLevel;

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ C2y Feature Support
130130
that ``_Generic`` selection associations may now have ``void`` type, but it
131131
also removes UB with code like ``(void)(void)1;``.
132132
- Implemented `WG14 N3411 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf>`_
133-
which allows a source file to not end with a newline character. This is still
134-
reported as a conforming extension in earlier language modes.
133+
which allows a source file to not end with a newline character. Note,
134+
``-pedantic`` will no longer diagnose this in either C or C++ modes. This
135+
feature was adopted as applying to obsolete versions of C in WG14 and as a
136+
defect report in WG21 (CWG787).
135137
- Implemented `WG14 N3353 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3353.htm>_`
136138
which adds the new ``0o`` and ``0O`` ocal literal prefixes and deprecates
137139
octal literals other than ``0`` which do not start with the new prefix. This

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,9 @@ def ext_multi_line_line_comment : Extension<"multi-line // comment">,
5252
def ext_line_comment : Extension<
5353
"// comments are not allowed in this language">,
5454
InGroup<Comment>;
55-
def ext_no_newline_eof : Extension<"no newline at end of file">,
56-
InGroup<NewlineEOF>;
5755
def warn_no_newline_eof : Warning<"no newline at end of file">,
5856
InGroup<NewlineEOF>, DefaultIgnore;
5957

60-
def warn_cxx98_compat_no_newline_eof : Warning<
61-
"C++98 requires newline at end of file">,
62-
InGroup<CXX98CompatPedantic>, DefaultIgnore;
63-
6458
def ext_dollar_in_identifier : Extension<"'$' in identifier">,
6559
InGroup<DiagGroup<"dollar-in-identifier-extension">>;
6660
def ext_charize_microsoft : Extension<

clang/lib/Lex/Lexer.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,28 +3187,12 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
31873187
ConditionalStack.pop_back();
31883188
}
31893189

3190-
// C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
3191-
// a pedwarn.
3192-
if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) {
3193-
DiagnosticsEngine &Diags = PP->getDiagnostics();
3194-
SourceLocation EndLoc = getSourceLocation(BufferEnd);
3195-
unsigned DiagID = diag::warn_no_newline_eof;
3196-
3197-
if (LangOpts.CPlusPlus11) {
3198-
// C++11 [lex.phases] 2.2 p2
3199-
// Prefer the C++98 pedantic compatibility warning over the generic,
3200-
// non-extension, user-requested "missing newline at EOF" warning.
3201-
if (!Diags.isIgnored(diag::warn_cxx98_compat_no_newline_eof, EndLoc))
3202-
DiagID = diag::warn_cxx98_compat_no_newline_eof;
3203-
} else {
3204-
// This is conforming in C2y, but is an extension in earlier language
3205-
// modes.
3206-
if (!LangOpts.C2y)
3207-
DiagID = diag::ext_no_newline_eof;
3208-
}
3209-
3210-
Diag(BufferEnd, DiagID) << FixItHint::CreateInsertion(EndLoc, "\n");
3211-
}
3190+
// Before C++11 and C2y, a file not ending with a newline was UB. Both
3191+
// standards changed this behavior (as a DR or equivalent), but we still have
3192+
// an opt-in diagnostic to warn about it.
3193+
if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
3194+
Diag(BufferEnd, diag::warn_no_newline_eof)
3195+
<< FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
32123196

32133197
BufferPtr = CurPtr;
32143198

clang/test/C/C2y/n3411.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// RUN: %clang_cc1 -verify=good -std=c2y -Wall -pedantic %s
22
// RUN: %clang_cc1 -verify -Wnewline-eof -std=c2y -Wall -pedantic %s
3-
// RUN: %clang_cc1 -verify -std=c23 -Wall -pedantic %s
3+
// RUN: %clang_cc1 -verify=good -std=c23 -Wall -pedantic %s
44
// RUN: %clang_cc1 -verify=good -std=c23 %s
5+
// RUN: %clang_cc1 -verify -Wnewline-eof -std=c23 %s
56

67
/* WG14 N3411: Yes
78
* Slay Some Earthly Demons XII

clang/test/CXX/drs/cwg787.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
6+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++23 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
7+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++2c %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
8+
// expected-no-diagnostics
9+
10+
// This file intentionally does not end with a newline. CWG787 made this
11+
// well-defined behavior.
12+
13+
// cwg787: 21

clang/test/Lexer/newline-eof-c++98-compat.cpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

clang/test/Lexer/newline-eof.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
2-
// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
3-
// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++03 -pedantic -verify %s
4-
// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
1+
// Allowing a file to end without a newline was adopted as a Defect Report in
2+
// WG21 (CWG787) and in WG14 (added to the list of changes which apply to
3+
// earlier revisions of C in C2y). So it should not issue a pedantic diagnostic
4+
// in any language mode.
55

6-
// In C++11 mode, this is allowed, so don't warn in pedantic mode.
6+
// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
7+
// RUN: %clang_cc1 -fsyntax-only -pedantic -verify=good %s
8+
// RUN: %clang_cc1 -fsyntax-only -std=c89 -pedantic -Wno-comment -verify=good %s
9+
// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++03 -pedantic -verify=good %s
710
// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++11 -Wnewline-eof -verify %s
811
// RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++11 -Werror -pedantic %s
12+
// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
13+
// good-no-diagnostics
914

1015
// Make sure the diagnostic shows up properly at the end of the last line.
1116
// CHECK: newline-eof.c:[[@LINE+3]]:67

0 commit comments

Comments
 (0)