From patchwork Mon Oct 16 11:18:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 826237 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-464256-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="h0OQfySy"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yFwkH23vvz9sNc for ; Mon, 16 Oct 2017 22:16:21 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:reply-to:to:subject:message-id:mime-version:content-type; q=dns; s=default; b=PI9q3ZD4Zny8Bv/UzPRYE8IuslTNUQUxg8y+1VLtK3n O4RhIUJzNAqN4F+b9jtok9QfqA4GtSELx1TicAD9xyBw+6MKUgowiZPbJUa51ZNP HlNN7oXfFYX5ct+SGan2iDNO/ZfaqJj5uzzzz0PIbRAW7KzntXIZFuetUwV5RXjs = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:reply-to:to:subject:message-id:mime-version:content-type; s=default; bh=HqHtMr61Zg+qduTHi1XtqS1oL5o=; b=h0OQfySyqlUxpCU0D 96MAVFhbiq5HYz68ZguD3lYncIiVAdJN3X2CP85t1si8dMzsRE/l3c4wfW8CIZv3 ddfnwBroR6lyCtE/XIOB8FlYWIVHZWSJZrlHIgrRBDSCRDQiXXd7FZWwsOdYTJUJ 0herFAcBJvchEj5Mto04Wyygcg= Received: (qmail 13372 invoked by alias); 16 Oct 2017 11:16:12 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 12790 invoked by uid 89); 16 Oct 2017 11:16:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-16.1 required=5.0 tests=BAYES_00, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=50010, gol, Hx-languages-length:3176, sam X-HELO: mail.segfault.party Received: from segfault.party (HELO mail.segfault.party) (146.185.131.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 16 Oct 2017 11:16:08 +0000 Received: by mail.segfault.party (Postfix, from userid 1003) id 2B66B60544; Mon, 16 Oct 2017 11:18:42 +0000 (UTC) Date: Mon, 16 Oct 2017 11:18:42 +0000 X-Patchwork-Original-From: "Sam van Kampen via gcc-patches" From: "Li, Pan2 via Gcc-patches" Reply-To: Sam van Kampen To: gcc-patches@gcc.gnu.org Subject: [patch, c++] Add a warning flag for the enum bit-field declaration warning in bug #61414. Message-ID: <20171016111842.zmajbbopy2wkrzjz@segfault.party> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-IsSubscribed: yes This patch adds a warning flag for the warning described in bug report #61414. My proposed warning flag is -Wenum-bitfield-conversion, which corresponds with the warning flag that clang has for a similar warning. 2017-10-16 Sam van Kampen * c-family/c.opt: Add a warning flag for struct bit-fields being too small to hold enumerated types. * cp/class.c: Likewise. * doc/invoke.texi: Add documentation for said warning flag. Index: gcc/c-family/c.opt =================================================================== --- gcc/c-family/c.opt (revision 253769) +++ gcc/c-family/c.opt (working copy) @@ -500,6 +500,10 @@ Wenum-compare C ObjC C++ ObjC++ Var(warn_enum_compare) Init(-1) Warning LangEnabledBy(C ObjC,Wall || Wc++-compat) Warn about comparison of different enum types. +Wenum-bitfield-conversion +C++ Var(warn_enum_bitfield_conversion) Init(1) Warning +Warn about struct bit-fields being too small to hold enumerated types. + Werror C ObjC C++ ObjC++ ; Documented in common.opt Index: gcc/cp/class.c =================================================================== --- gcc/cp/class.c (revision 253769) +++ gcc/cp/class.c (working copy) @@ -3278,10 +3278,11 @@ check_bitfield_decl (tree field) && tree_int_cst_lt (TYPE_SIZE (type), w))) warning_at (DECL_SOURCE_LOCATION (field), 0, "width of %qD exceeds its type", field); - else if (TREE_CODE (type) == ENUMERAL_TYPE + else if (warn_enum_bitfield_conversion + && TREE_CODE (type) == ENUMERAL_TYPE && (0 > (compare_tree_int (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type)))))) - warning_at (DECL_SOURCE_LOCATION (field), 0, + warning_at (DECL_SOURCE_LOCATION (field), OPT_Wenum_bitfield_conversion, "%qD is too small to hold all values of %q#T", field, type); } Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 253769) +++ gcc/doc/invoke.texi (working copy) @@ -277,8 +277,8 @@ Objective-C and Objective-C++ Dialects}. -Wno-discarded-qualifiers -Wno-discarded-array-qualifiers @gol -Wno-div-by-zero -Wdouble-promotion @gol -Wduplicated-branches -Wduplicated-cond @gol --Wempty-body -Wenum-compare -Wno-endif-labels -Wexpansion-to-defined @gol --Werror -Werror=* -Wextra-semi -Wfatal-errors @gol +-Wempty-body -Wenum-bitfield-conversion -Wenum-compare -Wno-endif-labels @gol +-Wexpansion-to-defined -Werror -Werror=* -Wextra-semi -Wfatal-errors @gol -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args @gol -Wformat-nonliteral -Wformat-overflow=@var{n} @gol @@ -6126,6 +6126,12 @@ Warn when an expression is casted to its own type. Warn if an empty body occurs in an @code{if}, @code{else} or @code{do while} statement. This warning is also enabled by @option{-Wextra}. +@item -Wenum-bitfield-conversion +@opindex Wenum-bitfield-conversion +@opindex Wno-enum-bitfield-conversion +Warn about a bit-field potentially being too small to hold all values +of an enumerated type. This warning is enabled by default. + @item -Wenum-compare @opindex Wenum-compare @opindex Wno-enum-compare