melver updated this revision to Diff 401653. melver marked 4 inline comments as done. melver retitled this revision from "[clang] Respect -Wdeclaration-after-statement with C99 or later" to "[clang] Improve -Wdeclaration-after-statement". melver edited the summary of this revision. melver added a comment.
Rebase and rework to include improvements over already submitted code. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D117232/new/ https://reviews.llvm.org/D117232 Files: clang/docs/ReleaseNotes.rst clang/lib/Sema/SemaStmt.cpp clang/test/Sema/warn-mixed-decls.c Index: clang/test/Sema/warn-mixed-decls.c =================================================================== --- clang/test/Sema/warn-mixed-decls.c +++ 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 Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ 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<DeclStmt>(Elts[i])->decl_begin(); - Diag(D->getLocation(), !getLangOpts().C99 ? diag::ext_mixed_decls_code - : diag::warn_mixed_decls_code); + Diag(D->getLocation(), MixedDeclsCodeID); } } Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ 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 -------------------------------------------------
Index: clang/test/Sema/warn-mixed-decls.c =================================================================== --- clang/test/Sema/warn-mixed-decls.c +++ 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 Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ 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<DeclStmt>(Elts[i])->decl_begin(); - Diag(D->getLocation(), !getLangOpts().C99 ? diag::ext_mixed_decls_code - : diag::warn_mixed_decls_code); + Diag(D->getLocation(), MixedDeclsCodeID); } } Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ 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 -------------------------------------------------
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits