From patchwork Thu Feb 12 20:43:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 439327 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 85710140188 for ; Fri, 13 Feb 2015 08:00:24 +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:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=yuUDnJgdLmLZdpMrhYVrEDW9E/BRBJS0ZBUlZCYjDpBEuSZjll2ZT keiOfN6/BQs/AoPQNDwpXkyHhssQwEn67LWbtF/cJSuXH+LKEba1dFDuOCQPde0g tX1x0OfYD1Qujp095fw2sjo9/OokJh9Yst+WliQ3gXfp+1RGRFvk/k= 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:to:subject:message-id:mime-version:content-type; s= default; bh=jGS4+UUytNcCRG9sp6ZpNWaymAk=; b=Q+r3GftD+0LzBxu5QJ+k 6V4GyNsJwyiW7DdSz4/Q0L5tUuXIq6SVHab99oG8tdnyLt3YzRyzfCYKc5gaxGoc j1vZ+2UhYf5zxC39/UhSh6wmHKJzQ3cMeYiiW0903nlEC6wSlGVNU+mfPo72v5ip 49SIrmQrcbJ/+FqEMuaU7+k= Received: (qmail 11082 invoked by alias); 12 Feb 2015 20:43:25 -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 11039 invoked by uid 89); 12 Feb 2015 20:43:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 12 Feb 2015 20:43:22 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t1CKhJWv004486 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 12 Feb 2015 15:43:19 -0500 Received: from redhat.com ([10.40.204.30]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t1CKhFLU015827 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO); Thu, 12 Feb 2015 15:43:17 -0500 Date: Thu, 12 Feb 2015 21:43:14 +0100 From: Marek Polacek To: GCC Patches , Joseph Myers Subject: [C PATCH] Abate -Wformat-signedness warnings (PR c/65040) Message-ID: <20150212204314.GE17070@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) This patch tries to avoid emitting -Wformat-signedness warnings in cases where integer promotions are performed on unsigned values such as: unsigned short arg = N; printf ("%u\n", arg); here we can know that the promoted value of arg is in range <0, USHRT_MAX>, or printf ("%" PRIx8, (uint8_t) val); where the warning is just irritating. Fixed by bailing out when we see a value of unsigned type that has been promoted and the conversion specifier is of unsigned type. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2015-02-12 Marek Polacek PR c/65040 * c-format.c (check_format_types): Don't warn about different signedness if the original value is in the range of WANTED_TYPE. * c-c++-common/pr65040.c: New test. Marek diff --git gcc/c-family/c-format.c gcc/c-family/c-format.c index faaca09..2f49b2d 100644 --- gcc/c-family/c-format.c +++ gcc/c-family/c-format.c @@ -2456,8 +2456,7 @@ check_format_types (location_t loc, format_wanted_type *types) cur_type = TYPE_MAIN_VARIANT (cur_type); /* Check whether the argument type is a character type. This leniency - only applies to certain formats, flagged with 'c'. - */ + only applies to certain formats, flagged with 'c'. */ if (types->char_lenient_flag) char_type_flag = (cur_type == char_type_node || cur_type == signed_char_type_node @@ -2486,6 +2485,20 @@ check_format_types (location_t loc, format_wanted_type *types) ? wanted_type == c_common_unsigned_type (cur_type) : wanted_type == c_common_signed_type (cur_type))) continue; + /* Don't warn about differences merely in signedness if we know + that the current type is integer-promoted and its original type + was unsigned such as that it is in the range of WANTED_TYPE. */ + if (TREE_CODE (wanted_type) == INTEGER_TYPE + && TREE_CODE (cur_type) == INTEGER_TYPE + && warn_format_signedness + && TYPE_UNSIGNED (wanted_type) + && TREE_CODE (cur_param) == NOP_EXPR) + { + tree t = TREE_TYPE (TREE_OPERAND (cur_param, 0)); + if (TYPE_UNSIGNED (t) + && cur_type == lang_hooks.types.type_promotes_to (t)) + continue; + } /* Likewise, "signed char", "unsigned char" and "char" are equivalent but the above test won't consider them equivalent. */ if (wanted_type == char_type_node diff --git gcc/testsuite/c-c++-common/pr65040.c gcc/testsuite/c-c++-common/pr65040.c index e69de29..80da8f9 100644 --- gcc/testsuite/c-c++-common/pr65040.c +++ gcc/testsuite/c-c++-common/pr65040.c @@ -0,0 +1,21 @@ +/* PR c/65040 */ +/* { dg-do compile } */ +/* { dg-options "-Wformat -Wformat-signedness" } */ + +unsigned char uc; +signed char sc; +unsigned short us; +signed short ss; +unsigned int u; +int i; + +void +foo (void) +{ + __builtin_printf ("%u\n", uc); /* { dg-bogus "expects argument of type" } */ + __builtin_printf ("%u\n", sc); /* { dg-warning "expects argument of type" } */ + __builtin_printf ("%u\n", us); /* { dg-bogus "expects argument of type" } */ + __builtin_printf ("%u\n", ss); /* { dg-warning "expects argument of type" } */ + __builtin_printf ("%u\n", u); /* { dg-bogus "expects argument of type" } */ + __builtin_printf ("%u\n", i); /* { dg-warning "expects argument of type" } */ +}