From patchwork Thu Aug 29 02:55:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 172515 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp1603291ily; Wed, 28 Aug 2019 19:56:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqwWnl/ul+tFW49vc8idsZPu8Q9fBbFSq/kwYEYrcYAgDHcvcCtPFtJKVCK1oqjs5qVqNqcZ X-Received: by 2002:a17:90a:d58d:: with SMTP id v13mr7421329pju.7.1567047384450; Wed, 28 Aug 2019 19:56:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567047384; cv=none; d=google.com; s=arc-20160816; b=zrSWRA/UH4viyVNzfhn+qPKKDdbdQ8xaqcUw+XkWBk/xtG1qquD9QDhGOGeTWwld+d bDGXh11AaNNlxfrpwwtoOrAoFc4padxP9uWilHLgPdrxaRL3/NUDTT4NsjgESn9uvuY0 ULNJBOURDJPWykqQ5pF6uZUlDuSu1KzCmsgBjfIiopiK/g6VxAClbcfhZLH1ddpU1IIZ 3s8dkyaQtwYlsi9/MQkXmnLkD3s7HsiDkAWpvnQyA8zHnQny/KAu3jXquA3615JCZMzW 6fW3wFC5BMovhlhx1nVNtZo84vEXq5P9JtOLeXUkTnPeclhbxI/4Gx2QVo8PKqLQEDLD aBmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:mime-version:dkim-signature :delivered-to:sender:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature; bh=fGMVSBVqxL8/rMpS6ujpZPaF5MqXRJrdQ0EUc+Elw08=; b=TFWTUyZeMWtz14rzZbPAsuhsTXl0JXgPfzu5eDiDV+hH/gl8Rm/vf1hCxGL2/oykXF /ll8af95P1kscWt90grPvRfz3idba5YvHjmPJVXn+7EWdWfV0PY93uruR9AdGuy7N7h7 1VYTVD131GPO1bzJKirEzs+wlxQG9HoRzp97auCnCgDj6fAT1r0Qaw1F506ZL46NesN6 wkWeD1R9asrP3rAeBR4OcZY4C5OmxMrwZUfDo6f1fn67oFIH6bXKjUf2NEgvAlj0z4iI QTr/cMytTqUJNFot9DWiI7vK4bIGGGjwY2KXUGoPUNk4ZR11G01UNw9y2lRngCCXObC9 fBBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=awvt45rw; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=yc15YbNF; spf=pass (google.com: domain of gcc-patches-return-507897-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gcc-patches-return-507897-patch=linaro.org@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id go1si678970plb.63.2019.08.28.19.56.24 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Aug 2019 19:56:24 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-507897-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 header.s=default header.b=awvt45rw; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=yc15YbNF; spf=pass (google.com: domain of gcc-patches-return-507897-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gcc-patches-return-507897-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=xxf/PZg7UPK+n33eyr/vVtj6ByW6zLJ78kSKimGW4anhVt FZjqZBQKmLBkK2hLkOH0EJxh/Mx2Uha5j8yQ/Li12VQNGfef4s6lCxJNbeuahHd7 TO59y1HMH+8ybnsJe9rMhMzq1xAbspc6tXkqutGcEJOBb/XPGMWfWVmE4Qte8= 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=LLVl9j5q4Wq6FrjKziSSpahGOqE=; b=awvt45rwy1d8kh5Kua2/ Upz40XLF7SVlm5gt5YbybVKjbe40p5mASNFj6/tSA6VIo0f4Wfnfl6IJkaEI5TVi AfoXUhj51JJBgqt98k8zMi8vwO4Vxml+UbV50En2v4UMCU9SwmNZNOy+P+uyV9VS xdT0Qnt0KV7KbC4uQVCdNVI= Received: (qmail 76167 invoked by alias); 29 Aug 2019 02:56: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 76159 invoked by uid 89); 29 Aug 2019 02:56:12 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=commonopt, UD:common.opt, diagnosed, common.opt X-HELO: mail-lj1-f171.google.com Received: from mail-lj1-f171.google.com (HELO mail-lj1-f171.google.com) (209.85.208.171) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 29 Aug 2019 02:56:10 +0000 Received: by mail-lj1-f171.google.com with SMTP id e24so1472676ljg.11 for ; Wed, 28 Aug 2019 19:56:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:from:date:message-id:subject:to; bh=JQX1FNpDXdk7wX0VWdap+zCXuDu/dNDHAAp54CXewoI=; b=yc15YbNFf7hPyd5xvHK9tYUrJe8ZBhgfThK5L7C1LspgBxXPTWW06IQtTALRQO16hE +MpRJ6N1GbxJOQafJ5q0eItjGl/my4HAGig8jD2Qq7tKOukqFQvjIt3/25+1BaCwJqVo iCbvB83zyguDggok01KAKCXzVQAfm6/Ark7cgKoqXwx5CSPVhWrXQXGROPwy43jbMAZ3 mvXPE7cvCn3WZOgiOaZKA9HjKrPySl8iHJ63qdiE2cW0xfA7TnHT6kmRYEXbTcHDtTgB GhnVeC4iJDpenGVLKIsvctI0+ol8qU3GIL4zERQD2Vn1appQK5qLZL8qfVa6marWmOaf LscQ== MIME-Version: 1.0 From: Prathamesh Kulkarni Date: Thu, 29 Aug 2019 08:25:30 +0530 Message-ID: Subject: PR78736 To: gcc Patches , "Joseph S. Myers" X-IsSubscribed: yes Hi, This is a rebased patch on trunk for PR78736. The last time, it got stuck, because of warning issues with libgfortran, for which I filed PR91593. The patch relegates the warning to Wextra instead, which only triggers (non-fatal) warnings in libgfortran/io/transfer.c, and survives bootstrap+test on x86_64-unknown-linux-gnu. (Also IIRC, Wall triggered several instances of the warning with allmodconfig kernel build last time). Is the patch OK to commit ? Thanks, Prathamesh 2019-08-29 Prathamesh Kulkarni * doc/invoke.texi: 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 257cadfa5f1..601457b3762 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -534,6 +534,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,Wextra) +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 2bbf0e21fb9..d4e12eb93d1 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -6726,6 +6726,21 @@ 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 (&loc, OPT_Wenum_conversion, + "implicit conversion from %qT to %qT", + checktype, type); + } + } + if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) { warn_for_address_or_pointer_of_packed_member (type, orig_rhs); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 549e043c67c..d497eb1f098 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -306,7 +306,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 +-Wempty-body -Wenum-compare -Wenum-conversion @gol +-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 @@ -4430,6 +4431,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 @@ -7002,6 +7004,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 a value of enumerated type is implicitly converted to a +different enumerated type. This warning is enabled by @option{-Wextra}. + @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..86033399b7d --- /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 Y. to .enum X." } */ +enum Y obj2 = y1; + +enum X obj3; +void foo() +{ + obj3 = y2; /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +} + +void bar(enum X); +void f(void) +{ + bar (y1); /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +}