diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -58,6 +58,11 @@ release being diagnosed against). These new groups are automatically implied when passing ``-Wc++N-extensions``. Resolves PR33518. +- Support ``-Wdeclaration-after-statement`` with C99 and later standards, and + not just C89, matching GCC's behaviour. A notable usecase is supporting style + guides that forbid mixing declarations and code, but want to move to newer C + standards. + Non-comprehensive list of changes in this release ------------------------------------------------- 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 @@ -413,7 +413,10 @@ // 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) { + const unsigned MixedDeclsCodeID = getLangOpts().C99 + ? diag::warn_mixed_decls_code + : diag::ext_mixed_decls_code; + if (!getLangOpts().CPlusPlus && !Diags.isIgnored(MixedDeclsCodeID, L)) { // Note that __extension__ can be around a decl. unsigned i = 0; // Skip over all declarations. @@ -426,8 +429,7 @@ if (i != NumElts) { Decl *D = *cast(Elts[i])->decl_begin(); - Diag(D->getLocation(), !getLangOpts().C99 ? diag::ext_mixed_decls_code - : diag::warn_mixed_decls_code); + Diag(D->getLocation(), MixedDeclsCodeID); } } diff --git a/clang/test/Sema/warn-mixed-decls.c b/clang/test/Sema/warn-mixed-decls.c --- a/clang/test/Sema/warn-mixed-decls.c +++ b/clang/test/Sema/warn-mixed-decls.c @@ -1,13 +1,23 @@ /* RUN: %clang_cc1 -fsyntax-only -verify -std=c89 -pedantic %s */ +/* RUN: %clang_cc1 -fsyntax-only -verify -std=c89 -Wdeclaration-after-statement %s + */ /* RUN: %clang_cc1 -fsyntax-only -verify -std=c99 -Wdeclaration-after-statement %s */ +/* RUN: %clang_cc1 -fsyntax-only -verify -std=c11 -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 -std=c89 -Wall %s + */ +/* RUN: %clang_cc1 -fsyntax-only -verify=none -std=c99 -Wall -pedantic %s + */ +/* RUN: %clang_cc1 -fsyntax-only -verify=none -std=c11 -Wall -pedantic %s + */ /* RUN: %clang_cc1 -fsyntax-only -verify=none -x c++ %s */ /* RUN: %clang_cc1 -fsyntax-only -verify=none -x c++ -Wdeclaration-after-statement %s