[clang] Improve -Wdeclaration-after-statement

With 118f966b46, Clang matches GCC's behaviour and allows enabling
-Wdeclaration-after-statement with C99 and later.

However, the check for mixing declarations and code is not a constant time
algorithm, and therefore should be guarded with Diags.isIgnored().

Furthermore, improve test coverage with: non-pedantic C89 with the
warning; C11 with the warning; and when using -Wall.

Finally, mention the changed behaviour in ReleaseNotes.rst.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D117232
This commit is contained in:
Marco Elver 2022-01-20 19:36:16 +01:00
parent 2d031ec5e5
commit c65186c89f
3 changed files with 20 additions and 3 deletions

View File

@ -58,6 +58,11 @@ Improvements to Clang's diagnostics
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
-------------------------------------------------

View File

@ -413,7 +413,10 @@ StmtResult Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
// 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 @@ StmtResult Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
if (i != NumElts) {
Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Diag(D->getLocation(), !getLangOpts().C99 ? diag::ext_mixed_decls_code
: diag::warn_mixed_decls_code);
Diag(D->getLocation(), MixedDeclsCodeID);
}
}

View File

@ -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