[PATCH] D114787: [clang][PR51931] Enable `-Wdeclaration-after-statement` for all C versions

Markus Böck via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 12 09:21:59 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG118f966b46cf: [clang][#51931] Enable `-Wdeclaration-after-statement` for all C versions (authored by zero9178).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114787/new/

https://reviews.llvm.org/D114787

Files:
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaStmt.cpp
  clang/test/Sema/warn-mixed-decls.c


Index: clang/test/Sema/warn-mixed-decls.c
===================================================================
--- /dev/null
+++ clang/test/Sema/warn-mixed-decls.c
@@ -0,0 +1,28 @@
+/* RUN: %clang_cc1 -fsyntax-only -verify -std=c89 -pedantic %s
+ */
+/* RUN: %clang_cc1 -fsyntax-only -verify -std=c99 -Wdeclaration-after-statement %s
+ */
+
+/* Should not emit diagnostic when not pedantic, not enabled or in C++ Code*/
+/* RUN: %clang_cc1 -fsyntax-only -verify=none -std=c89 %s
+ */
+/* RUN: %clang_cc1 -fsyntax-only -verify=none -std=c99 %s
+ */
+/* RUN: %clang_cc1 -fsyntax-only -verify=none -x c++ %s
+ */
+/* RUN: %clang_cc1 -fsyntax-only -verify=none -x c++ -Wdeclaration-after-statement %s
+ */
+
+/* none-no-diagnostics */
+
+int foo(int i)
+{
+  i += 1;
+  int f = i;
+#if __STDC_VERSION__ < 199901L
+  /* expected-warning at -2 {{mixing declarations and code is a C99 extension}}*/
+#else
+  /* expected-warning at -4 {{mixing declarations and code is incompatible with standards before C99}}*/
+#endif
+  return f;
+}
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -410,9 +410,10 @@
                                    ArrayRef<Stmt *> Elts, bool isStmtExpr) {
   const unsigned NumElts = Elts.size();
 
-  // If we're in C89 mode, check that we don't have any decls after stmts.  If
-  // so, emit an extension diagnostic.
-  if (!getLangOpts().C99 && !getLangOpts().CPlusPlus) {
+  // If we're in C mode, check that we don't have any decls after stmts.  If
+  // so, emit an extension diagnostic in C89 and potentially a warning in later
+  // versions.
+  if (!getLangOpts().CPlusPlus) {
     // Note that __extension__ can be around a decl.
     unsigned i = 0;
     // Skip over all declarations.
@@ -425,7 +426,8 @@
 
     if (i != NumElts) {
       Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
-      Diag(D->getLocation(), diag::ext_mixed_decls_code);
+      Diag(D->getLocation(), !getLangOpts().C99 ? diag::ext_mixed_decls_code
+                                                : diag::warn_mixed_decls_code);
     }
   }
 
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9861,8 +9861,11 @@
   "argument to %0 must be a constant integer">;
 
 def ext_mixed_decls_code : Extension<
-  "ISO C90 forbids mixing declarations and code">,
-  InGroup<DiagGroup<"declaration-after-statement">>;
+  "mixing declarations and code is a C99 extension">,
+  InGroup<DeclarationAfterStatement>;
+def warn_mixed_decls_code : Warning<
+  "mixing declarations and code is incompatible with standards before C99">,
+  InGroup<DeclarationAfterStatement>, DefaultIgnore;
 
 def err_non_local_variable_decl_in_for : Error<
   "declaration of non-local variable in 'for' loop">;
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ clang/include/clang/Basic/DiagnosticGroups.td
@@ -241,6 +241,7 @@
 
 def EmptyBody : DiagGroup<"empty-body">;
 def Exceptions : DiagGroup<"exceptions">;
+def DeclarationAfterStatement : DiagGroup<"declaration-after-statement">;
 
 def GNUEmptyInitializer : DiagGroup<"gnu-empty-initializer">;
 def GNUEmptyStruct : DiagGroup<"gnu-empty-struct">;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114787.399354.patch
Type: text/x-patch
Size: 3505 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220112/dea1b3d5/attachment.bin>


More information about the cfe-commits mailing list