From patchwork Mon Aug 5 17:51:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1142402 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-506241-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="tqAHQXYo"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lB1vH8Q2"; 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 462QLs6Ry2z9s3Z for ; Tue, 6 Aug 2019 03:51:49 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=Pj1DzzbQAcEdK8mrCwnGvuMdyu8V61AV1CI+C4naKsHIDO+UvP uNrWvuMOxhSoT3hQ77D/m5Ag8lM3U6TSB02v13SLp3D132t//+DFXRECruIYPEf9 VvZo86B4KDB4DhPLtyjAd25KOSYZmVchmt80aro9MXL8x66Mj5XYzogf0= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=nx8LEhh21rcOwM1lNbUP/ffr8AQ=; b=tqAHQXYoenWU+Wpc1ZEy tSeoJdxFYtthY6LadzRLUEizIx5VQSypEu8VSkpassd4AeV0Ko9zxh+yHMZ+CBHN 8+iCi6D4hG+IGqVE8/ve+vppSmQfoqAjEz5pva6F1ed/Clk/w6384p7/QUnsaTnW Orh4H+FGWj3ZMfUSXOvo5ig= Received: (qmail 106165 invoked by alias); 5 Aug 2019 17:51:43 -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 106115 invoked by uid 89); 5 Aug 2019 17:51:42 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-20.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=lu X-HELO: mail-qt1-f169.google.com Received: from mail-qt1-f169.google.com (HELO mail-qt1-f169.google.com) (209.85.160.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 05 Aug 2019 17:51:41 +0000 Received: by mail-qt1-f169.google.com with SMTP id d23so81839585qto.2 for ; Mon, 05 Aug 2019 10:51:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=Naz2ztKvcb3LSLC6i3o3Klbw26m5zRL9HORVNYTnWas=; b=lB1vH8Q24G+kDJiK/+xUf71Rf+XEM4giI3Bg5sVU9/3B61Xt1AoyRsFZoqCnGDiJx8 UGaK5R8cu86Mdtrtt8VCn3tV6hydYRFWIZ6EeoKaPPW0Jte7ZGVFuD8zUtVQfpNxSZeT fm43FP3AXZw7lR7BRXM8ApxrUBoGgJJp6EhEzhPZd3bGFIvL2G6mksCDdn03pTvUT6xU wHPEvsdWEWDJQZ1BS4CE/Qqls2rJ+AXWnDkD2Zsh868/C21zvbKc04VN5MTyyabPwX4D 634A6T8gz1I9EDUPGvmjfa8cZcMfSHoTiLtiqZqnn3W9tzeCms/IUjifdQwL/gjQSYml y2pg== Received: from [192.168.0.41] (97-118-116-95.hlrn.qwest.net. [97.118.116.95]) by smtp.gmail.com with ESMTPSA id m12sm33566287qkk.123.2019.08.05.10.51.38 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Mon, 05 Aug 2019 10:51:38 -0700 (PDT) To: gcc-patches From: Martin Sebor Subject: [PATCH] issue a correct fix-it hint for bad argument in GCC diagnostics (PR 80619) Message-ID: <89c63048-ce6a-ad21-b671-5a79f380a26b@gmail.com> Date: Mon, 5 Aug 2019 11:51:36 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 X-IsSubscribed: yes When the argument to a directive with a length modifier like %lu in a call to a GCC diagnostic function such as warning()) is not of the right integer type, GCC suggests to replace the length modifier in the directive with 'w'. For instance: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘int’ [-Wformat=] f ("%lu", 0); ~~^ %wu To issue the right hint (i.e., %u in the message above) the attached patch introduces a new format_lengths enumerator to represent the 'w' modifier. Tested on x86_64-linux. Martin PR c/80619 - bad fix-it hint for GCC %lu directive with int argument: %wu gcc/c-family/ChangeLog: PR c/80619 * c-format.c (printf_length_specs): Set FMT_LEN_w for "w". (asm_fprintf_length_spec): Same. * c-format.h (format_lengths): Add FMT_LEN_w. gcc/testsuite/ChangeLog: PR c/80619 * gcc.dg/format/pr80619.c: New test. diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index 6363fa4f686..1fa551957d9 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -497,7 +497,7 @@ static const format_length_info printf_length_specs[] = static const format_length_info asm_fprintf_length_specs[] = { { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 }, - { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 }, + { "w", FMT_LEN_w, STD_C89, NO_FMT, 0 }, { NO_FMT, NO_FMT, 0 } }; @@ -505,7 +505,7 @@ static const format_length_info asm_fprintf_length_specs[] = static const format_length_info gcc_diag_length_specs[] = { { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 }, - { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 }, + { "w", FMT_LEN_w, STD_C89, NO_FMT, 0 }, { NO_FMT, NO_FMT, 0 } }; diff --git a/gcc/c-family/c-format.h b/gcc/c-family/c-format.h index 972ba46f109..6aa68dfe883 100644 --- a/gcc/c-family/c-format.h +++ b/gcc/c-family/c-format.h @@ -36,6 +36,7 @@ enum format_lengths FMT_LEN_H, FMT_LEN_D, FMT_LEN_DD, + FMT_LEN_w, /* GCC's HOST_WIDE_INT. */ FMT_LEN_MAX }; diff --git a/gcc/testsuite/gcc.dg/format/pr80619.c b/gcc/testsuite/gcc.dg/format/pr80619.c new file mode 100644 index 00000000000..c9f0496a1c2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/pr80619.c @@ -0,0 +1,89 @@ +/* PR c/80619 - bad fix-it hint for GCC %lu directive with int argument: %wu + { dg-do compile } + { dg-options "-Wall -fdiagnostics-show-caret" } */ + +void T (const char*, ...) __attribute__ ((format (__gcc_diag__, 1, 2))); + +void test_suggested_modifier (void) +{ + T ("%ld", 0); // { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%ld", 0); + ~~^ ~ + | | + | int + long int + %d + { dg-end-multiline-output "" } */ + + T ("%li", 0); // { dg-warning "format '%li' expects argument of type 'long int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%li", 0); + ~~^ ~ + | | + | int + long int + %i + { dg-end-multiline-output "" } */ + + T ("%lu", 0); // { dg-warning "format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%lu", 0); + ~~^ ~ + | | + | int + long unsigned int + %u + { dg-end-multiline-output "" } */ + + T ("%lx", 0); // { dg-warning "format '%lx' expects argument of type 'long unsigned int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%lx", 0); + ~~^ ~ + | | + | int + long unsigned int + %x + { dg-end-multiline-output "" } */ + + T ("%lli", 0); // { dg-warning "format '%lli' expects argument of type 'long long int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%lli", 0); + ~~~^ ~ + | | + | int + long long int + %i + { dg-end-multiline-output "" } */ + + T ("%llo", 0); // { dg-warning "format '%llo' expects argument of type 'long long unsigned int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%llo", 0); + ~~~^ ~ + | | + | int + long long unsigned int + %o + { dg-end-multiline-output "" } */ + + T ("%llu", 0); // { dg-warning "format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%llu", 0); + ~~~^ ~ + | | + | int + long long unsigned int + %u + { dg-end-multiline-output "" } */ + + T ("%llx", 0); // { dg-warning "format '%llx' expects argument of type 'long long unsigned int', but argument 2 has type 'int'" } + /* { dg-begin-multiline-output "" } + T ("%llx", 0); + ~~~^ ~ + | | + | int + long long unsigned int + %x + { dg-end-multiline-output "" } */ +} +