diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/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">; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/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>; + "mixing declarations and code is a C99 extension">, + InGroup; +def warn_mixed_decls_code : Warning< + "mixing declarations and code is incompatible with standards before C99">, + InGroup, DefaultIgnore; def err_non_local_variable_decl_in_for : Error< "declaration of non-local variable in 'for' loop">; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp --- a/clang/lib/Sema/SemaStmt.cpp +++ b/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); } } diff --git a/clang/test/Sema/warn-mixed-decls.c b/clang/test/Sema/warn-mixed-decls.c new file mode 100644 --- /dev/null +++ b/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@-2 {{mixing declarations and code is a C99 extension}}*/ +#else + /* expected-warning@-4 {{mixing declarations and code is incompatible with standards before C99}}*/ +#endif + return f; +}