r189110 - Respect -Wnewline-eof even in C++11 mode.

Jordan Rose jordan_rose at apple.com
Fri Aug 23 08:42:01 PDT 2013


Author: jrose
Date: Fri Aug 23 10:42:01 2013
New Revision: 189110

URL: http://llvm.org/viewvc/llvm-project?rev=189110&view=rev
Log:
Respect -Wnewline-eof even in C++11 mode.

If the user has requested this warning, we should emit it, even if it's not
an extension in the current language mode. However, being an extension is
more important, so prefer the pedantic warning or the pedantic-compatibility
warning if those are enabled.

<rdar://problem/12922063>

Removed:
    cfe/trunk/test/Lexer/newline-eof-c++11.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
    cfe/trunk/lib/Lex/Lexer.cpp
    cfe/trunk/test/Lexer/newline-eof-c++98-compat.cpp
    cfe/trunk/test/Lexer/newline-eof.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=189110&r1=189109&r2=189110&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Fri Aug 23 10:42:01 2013
@@ -190,6 +190,7 @@ def MismatchedReturnTypes : DiagGroup<"m
 def MismatchedTags : DiagGroup<"mismatched-tags">;
 def MissingFieldInitializers : DiagGroup<"missing-field-initializers">;
 def ModuleConflict : DiagGroup<"module-conflict">;
+def NewlineEOF : DiagGroup<"newline-eof">;
 def NullArithmetic : DiagGroup<"null-arithmetic">;
 def NullCharacter : DiagGroup<"null-character">;
 def NullDereference : DiagGroup<"null-dereference">;

Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=189110&r1=189109&r2=189110&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Fri Aug 23 10:42:01 2013
@@ -47,7 +47,9 @@ 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<DiagGroup<"newline-eof">>;
+  InGroup<NewlineEOF>;
+def warn_no_newline_eof : Warning<"no newline at end of file">,
+  InGroup<NewlineEOF>, DefaultIgnore;
 
 def warn_cxx98_compat_no_newline_eof : Warning<
   "C++98 requires newline at end of file">,

Modified: cfe/trunk/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=189110&r1=189109&r2=189110&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Fri Aug 23 10:42:01 2013
@@ -2410,10 +2410,28 @@ bool Lexer::LexEndOfFile(Token &Result,
 
   // 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'))
-    Diag(BufferEnd, LangOpts.CPlusPlus11 ? // C++11 [lex.phases] 2.2 p2
-         diag::warn_cxx98_compat_no_newline_eof : diag::ext_no_newline_eof)
-    << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
+  if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) {
+    DiagnosticsEngine &Diags = PP->getDiagnostics();
+    SourceLocation EndLoc = getSourceLocation(BufferEnd);
+    unsigned DiagID;
+
+    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.getDiagnosticLevel(diag::warn_cxx98_compat_no_newline_eof,
+                                   EndLoc) != DiagnosticsEngine::Ignored) {
+        DiagID = diag::warn_cxx98_compat_no_newline_eof;
+      } else {
+        DiagID = diag::warn_no_newline_eof;
+      }
+    } else {
+      DiagID = diag::ext_no_newline_eof;
+    }
+
+    Diag(BufferEnd, DiagID)
+      << FixItHint::CreateInsertion(EndLoc, "\n");
+  }
 
   BufferPtr = CurPtr;
 

Removed: cfe/trunk/test/Lexer/newline-eof-c++11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/newline-eof-c%2B%2B11.cpp?rev=189109&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/newline-eof-c++11.cpp (original)
+++ cfe/trunk/test/Lexer/newline-eof-c++11.cpp (removed)
@@ -1,5 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wnewline-eof -verify %s
-// expected-no-diagnostics
-
-// The following line isn't terminated, don't fix it.
-void foo() {}
\ No newline at end of file

Modified: cfe/trunk/test/Lexer/newline-eof-c++98-compat.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/newline-eof-c%2B%2B98-compat.cpp?rev=189110&r1=189109&r2=189110&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/newline-eof-c++98-compat.cpp (original)
+++ cfe/trunk/test/Lexer/newline-eof-c++98-compat.cpp Fri Aug 23 10:42:01 2013
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -Wc++98-compat-pedantic -std=c++11 -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wc++98-compat-pedantic -Wnewline-eof -std=c++11 -verify %s
 
 // The following line isn't terminated, don't fix it.
 void foo() {} // expected-warning{{C++98 requires newline at end of file}}
\ No newline at end of file

Modified: cfe/trunk/test/Lexer/newline-eof.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/newline-eof.c?rev=189110&r1=189109&r2=189110&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/newline-eof.c (original)
+++ cfe/trunk/test/Lexer/newline-eof.c Fri Aug 23 10:42:01 2013
@@ -1,9 +1,15 @@
-// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
-// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
+// RUN: %clang -fsyntax-only -Wnewline-eof -verify %s
+// RUN: %clang -fsyntax-only -pedantic -verify %s
+// RUN: %clang -fsyntax-only -x c++ -std=c++03 -pedantic -verify %s
+// RUN: %clang -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
 // rdar://9133072
 
+// In C++11 mode, this is allowed, so don't warn in pedantic mode.
+// RUN: %clang -fsyntax-only -x c++ -std=c++11 -Wnewline-eof -verify %s
+// RUN: %clang -fsyntax-only -x c++ -std=c++11 -pedantic %s
+
 // Make sure the diagnostic shows up properly at the end of the last line.
-// CHECK: newline-eof.c:9:63
+// CHECK: newline-eof.c:[[@LINE+3]]:63
 
 // The following line isn't terminated, don't fix it.
 void foo() {} // expected-warning{{no newline at end of file}}
\ No newline at end of file





More information about the cfe-commits mailing list