From patchwork Tue May 2 17:11:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 98453 Delivered-To: patch@linaro.org Received: by 10.140.109.52 with SMTP id k49csp1975481qgf; Tue, 2 May 2017 10:12:21 -0700 (PDT) X-Received: by 10.84.238.206 with SMTP id l14mr23404715pln.189.1493745141774; Tue, 02 May 2017 10:12:21 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id e21si8167674pgn.43.2017.05.02.10.12.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 May 2017 10:12:21 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-452625-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-452625-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-452625-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=mZX56Sv/Do48jBHoo+PpRUggtoC9Tcrsta6oPENPB4v6JX LTD20mkl5pVjYzMTtkU0+GbeHGjgbI6Q9glYVdN4nf+m2XtC3jukJvYhK1vQQ0jU glbDUmwz+mM0EKE8bTk52l73PA3bgfrJkseVDipA+DaZSZu6OXnap5H632vKk= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=vbyhT+G3yjyBFTornOlHOcrMNmY=; b=OLgr4iWxpqSyKRZfKlp3 iDaTS/nXvs1LvOiydSQO0CNwJHy1p27ySTbHaAgSNy61VU6C3ffVFPrjBQzmVT+f c/ttptZW9fOq/61KG/MYPTmJva4dqIVYxUNiQmHoOiJ3/uNQUcKE0+1X72LmVaxv avfoMAKVEXDGXCRO/ms9Tto= Received: (qmail 2325 invoked by alias); 2 May 2017 17:12:02 -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 2286 invoked by uid 89); 2 May 2017 17:12:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-it0-f49.google.com Received: from mail-it0-f49.google.com (HELO mail-it0-f49.google.com) (209.85.214.49) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 02 May 2017 17:11:59 +0000 Received: by mail-it0-f49.google.com with SMTP id e65so20149385ita.1 for ; Tue, 02 May 2017 10:12:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=63OOQVQ0Ccb9Ujgltyp7SREEQyU/9eqU0Wk6X8kX+Z0=; b=c2Hsal2dK7do6VwiYDhzjxKD7CvJNFEU3QIl3vAZ+E4NbrSAiGQQKtkNlE3FUVECWG ccLi3R4rgIgugE5aVi/3+i7BAFQb6FsNK16J+Bh8iJK7aLLFlEX52BbyW9PCtzamjGL/ vRTySpj8RmcNM4p9417Yfytu4HkBZXE5nBKAB2Szr2OgEb2fVkkfuN5BeOrhz599TtmY G3afcErekn6/vRGzL9O6eEhyzQ9V6viV4gc1aWS93QTWEuOSN2Z/OibjS8jjN+dOwr9z 7SPjvjD3QskSgJ1CIh0xSQKjjgnwWsrJY8WOfL+8gGe9k9MTtawfmDhhjieTKuRBRJA3 nbwA== X-Gm-Message-State: AN3rC/72WSGPId4Y5mRBQVGZYpWbLkS02O0Mk2ms9euMLQ8bOJnLk8sd dJBiAwGsOi4x9B1J0xszFgWL9omfP3T4 X-Received: by 10.36.137.212 with SMTP id s203mr3682091itd.57.1493745120278; Tue, 02 May 2017 10:12:00 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.25.67 with HTTP; Tue, 2 May 2017 10:11:59 -0700 (PDT) From: Prathamesh Kulkarni Date: Tue, 2 May 2017 22:41:59 +0530 Message-ID: Subject: [1/2] PR 78736: New warning -Wenum-conversion To: gcc Patches , Marek Polacek , "Joseph S. Myers" X-IsSubscribed: yes Hi, The attached patch attempts to add option -Wenum-conversion for C and objective-C similar to clang, which warns when an enum value of a type is implicitly converted to enum value of another type and is enabled by Wall. Bootstrapped+tested on x86_64-unknown-linux-gnu. Is the patch OK for trunk ? Thanks, Prathamesh 2017-05-02 Prathamesh Kulkarni * doc/invoke.text: Document Wenum-conversion. * c-family/c.opt (Wenum-conversion): New option. * c/c-typeck.c (convert_for_assignment): Handle Wenum-conversion. testsuite/ * gcc.dg/Wenum-conversion.c: New test-case. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 9ad2f6e1fcc..e04312ec253 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -492,6 +492,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-conversion +C ObjC Var(warn_enum_conversion) Init(0) Warning LangEnabledBy(C Objc,Wall) +Warn about implicit conversion of enum types. + Werror C ObjC C++ ObjC++ ; Documented in common.opt diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 6f9909c6396..c9cde8d7fef 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -6309,6 +6309,20 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type, } } + if (warn_enum_conversion) + { + tree checktype = origtype != NULL_TREE ? origtype : rhstype; + if (checktype != error_mark_node + && TREE_CODE (checktype) == ENUMERAL_TYPE + && TREE_CODE (type) == ENUMERAL_TYPE + && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) + { + gcc_rich_location loc (location); + warning_at_rich_loc (&loc, 0, "implicit conversion from" + " enum type of %qT to %qT", checktype, type); + } + } + if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) return rhs; diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 0eeea7b3b87..79b1e175374 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -273,7 +273,7 @@ Objective-C and Objective-C++ Dialects}. -Wdisabled-optimization @gol -Wno-discarded-qualifiers -Wno-discarded-array-qualifiers @gol -Wno-div-by-zero -Wdouble-promotion -Wduplicated-cond @gol --Wempty-body -Wenum-compare -Wno-endif-labels -Wexpansion-to-defined @gol +-Wempty-body -Wenum-compare -Wenum-conversion -Wno-endif-labels -Wexpansion-to-defined @gol -Werror -Werror=* -Wextra-semi -Wfatal-errors @gol -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args @gol @@ -3754,6 +3754,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}. -Wcomment @gol -Wduplicate-decl-specifier @r{(C and Objective-C only)} @gol -Wenum-compare @r{(in C/ObjC; this is on by default in C++)} @gol +-Wenum-conversion @r{in C/ObjC;} @gol -Wformat @gol -Wint-in-bool-context @gol -Wimplicit @r{(C and Objective-C only)} @gol @@ -5961,6 +5962,12 @@ In C++ enumerated type mismatches in conditional expressions are also diagnosed and the warning is enabled by default. In C this warning is enabled by @option{-Wall}. +@item -Wenum-conversion @r{(C, Objective-C only)} +@opindex Wenum-conversion +@opindex Wno-enum-conversion +Warn when an enum value of a type is implicitly converted to an enum of +another type. This warning is enabled by @option{-Wall}. + @item -Wextra-semi @r{(C++, Objective-C++ only)} @opindex Wextra-semi @opindex Wno-extra-semi diff --git a/gcc/testsuite/gcc.dg/Wenum-conversion.c b/gcc/testsuite/gcc.dg/Wenum-conversion.c new file mode 100644 index 00000000000..4459109c7cb --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wenum-conversion.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-Wenum-conversion" } */ + +enum X { x1, x2 }; +enum Y { y1, y2 }; + +enum X obj = y1; /* { dg-warning "implicit conversion from enum type of .enum Y. to .enum X." } */ +enum Y obj2 = y1; + +enum X obj3; +void foo() +{ + obj3 = y2; /* { dg-warning "implicit conversion from enum type of .enum Y. to .enum X." } */ +} + +void bar(enum X); +void f(void) +{ + bar (y1); /* { dg-warning "implicit conversion from enum type of .enum Y. to .enum X." } */ +}