[PATCH] D115094: Fix -Wdeclaration-after-statement doesn't work when used with -std=c99

ram via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 4 01:41:01 PST 2021


ram7bhupal created this revision.
ram7bhupal added reviewers: dblaikie, aaron.ballman.
ram7bhupal requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

-Wdeclaration-after-statement doesn't do anything if combined with -std=c99 or newer.

Take a look at the following program:

// prog.c
#include <stdio.h>

int main(void)
{

  printf("hello world\n");
  int i = 0;
  
  return 0;

}

If I compile it with clang with the following command:

$ clang -std=c99 -Wdeclaration-after-statement prog.c

it produces no warnings.

If I compile the same code with gcc with the following command:

$ gcc -std=c99 -Wdeclaration-after-statement prog.c

it produces the following warning:

prog.c: In function ‘main’:
prog.c:6:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

  6 |         int i = 0;
    |         ^~~

This is the behavior I would like to have with clang, but it only produces this warning if I use it with -std=c90 or -std=c89 or -ansi, like this:

$ clang -std=c90 -Wdeclaration-after-statement prog.c
prog.c:6:6: warning: ISO C90 forbids mixing declarations and code [-Wdeclaration-after-statement]

  int i = 0;
      ^

1 warning generated.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115094

Files:
  clang/lib/Sema/SemaStmt.cpp


Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -410,9 +410,9 @@
                                    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
+  // Check that we don't have any decls after stmts.  If
   // so, emit an extension diagnostic.
-  if (!getLangOpts().C99 && !getLangOpts().CPlusPlus) {
+  if (!getLangOpts().CPlusPlus) {
     // Note that __extension__ can be around a decl.
     unsigned i = 0;
     // Skip over all declarations.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115094.391818.patch
Type: text/x-patch
Size: 689 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211204/e332738d/attachment.bin>


More information about the cfe-commits mailing list