r335927 - [Parse] Make -Wgcc-compat complain about for loop inits in C89

George Burgess IV via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 28 14:36:00 PDT 2018


Author: gbiv
Date: Thu Jun 28 14:36:00 2018
New Revision: 335927

URL: http://llvm.org/viewvc/llvm-project?rev=335927&view=rev
Log:
[Parse] Make -Wgcc-compat complain about for loop inits in C89

While clang allows declarations in for loop init statements in c89 and
gnu89, gcc does not. So, we should probably warn if users care about gcc
compatibility.

Differential Revision: https://reviews.llvm.org/D47840

Added:
    cfe/trunk/test/Parser/gcc-for-loop-init-compatibility.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
    cfe/trunk/lib/Parse/ParseStmt.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=335927&r1=335926&r2=335927&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Thu Jun 28 14:36:00 2018
@@ -173,6 +173,9 @@ def warn_attribute_on_function_definitio
 def warn_gcc_attribute_location : Warning<
   "GCC does not allow an attribute in this position on a function declaration">, 
   InGroup<GccCompat>;
+def warn_gcc_variable_decl_in_for_loop : Warning<
+  "GCC does not allow variable declarations in for loop initializers before "
+  "C99">, InGroup<GccCompat>;
 def warn_attribute_no_decl : Warning<
   "attribute %0 ignored, because it is not attached to a declaration">, 
   InGroup<IgnoredAttributes>;

Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=335927&r1=335926&r2=335927&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Thu Jun 28 14:36:00 2018
@@ -1624,8 +1624,10 @@ StmtResult Parser::ParseForStatement(Sou
     ParenBraceBracketBalancer BalancerRAIIObj(*this);
 
     // Parse declaration, which eats the ';'.
-    if (!C99orCXXorObjC)   // Use of C99-style for loops in C90 mode?
+    if (!C99orCXXorObjC) {   // Use of C99-style for loops in C90 mode?
       Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
+      Diag(Tok, diag::warn_gcc_variable_decl_in_for_loop);
+    }
 
     // In C++0x, "for (T NS:a" might not be a typo for ::
     bool MightBeForRangeStmt = getLangOpts().CPlusPlus;

Added: cfe/trunk/test/Parser/gcc-for-loop-init-compatibility.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/gcc-for-loop-init-compatibility.c?rev=335927&view=auto
==============================================================================
--- cfe/trunk/test/Parser/gcc-for-loop-init-compatibility.c (added)
+++ cfe/trunk/test/Parser/gcc-for-loop-init-compatibility.c Thu Jun 28 14:36:00 2018
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c89 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=gnu89 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -verify %s -DC99
+
+#ifdef C99
+// expected-no-diagnostics
+#endif
+
+void foo() {
+#ifndef C99
+  // expected-warning at +2{{GCC does not allow variable declarations in for loop initializers before C99}}
+#endif
+  for (int i = 0; i < 10; i++)
+    ;
+}




More information about the cfe-commits mailing list