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">; Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -9863,7 +9863,10 @@ def ext_mixed_decls_code : Extension< "ISO C90 forbids mixing declarations and code">, - InGroup>; + InGroup; +def warn_mixed_decls_code : Warning< + ext_mixed_decls_code.Text>, + InGroup, DefaultIgnore; def err_non_local_variable_decl_in_for : Error< "declaration of non-local variable in 'for' loop">; Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ clang/lib/Sema/SemaStmt.cpp @@ -410,9 +410,10 @@ ArrayRef 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(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/test/Sema/warn-mixed-decls.c =================================================================== --- /dev/null +++ clang/test/Sema/warn-mixed-decls.c @@ -0,0 +1,23 @@ +/* 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; /* expected-warning {{ISO C90 forbids mixing declarations and code}}*/ + return f; +}