From patchwork Fri Jan 21 07:16:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 50298 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 24C433857C7D for ; Fri, 21 Jan 2022 07:17:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 24C433857C7D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642749423; bh=13RlUKb6PGgfNC3j5em/BIyUG0MnXRpNBYHfvF0olis=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=OONGLuFvf8TXXSeicRMtx0Lx7YvgRnjrqM45aztAtJ4ZOLNUnTNLifzE7sflIcHDe ZLeEfIRDeqYGw4fMZ5syBwQPQpIimLEkUCbTJcntNXW4UYGrIyrgSRtGA/Vs2OfEaP wcT8yKwIZrGP+6DFtxvOwksrH6j40DaxdOtsu7xA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 33D7F3858403 for ; Fri, 21 Jan 2022 07:16:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 33D7F3858403 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-653-0aOOo28YN6q6oK0jY-vFYg-1; Fri, 21 Jan 2022 02:16:30 -0500 X-MC-Unique: 0aOOo28YN6q6oK0jY-vFYg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 73A4580D690; Fri, 21 Jan 2022 07:16:29 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.125]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BCCD96E213; Fri, 21 Jan 2022 07:16:24 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 20L7GLAs2364316 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 21 Jan 2022 08:16:22 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 20L7GKI82364315; Fri, 21 Jan 2022 08:16:20 +0100 Date: Fri, 21 Jan 2022 08:16:20 +0100 To: "Joseph S. Myers" , Marek Polacek , Jason Merrill Subject: [PATCH] c-family: Fix up a -Wformat regression [PR104148] Message-ID: <20220121071620.GC2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! As can be seen on the testcase, GCC 11 no longer warns if the format string is wrapped inside of ()s. This regressed with r11-2457-gdf5cf47a978, which added if (TREE_NO_WARNING (param)) return; to check_function_arguments_recurse. That function is used with a callback for two cases, for -Wformat and for -Wnonnull. For the latter it is desirable to not warn in parameters or their subexpressions where that warning is suppressed, but for -Wformat the function is used solely to discover the string literals if any so that the c-format.cc code can diagnose them. I believe no warning suppression should stand in the way of that, -Wformat* warnings should be decided from warning suppression on the CALL_EXPR only. In the PR Martin argued that now that we have specialized warning_suppressed_p we should use it, so instead of adding a bool arg to check_function_arguments_recurse I've added opt_code to the function, but will defer the warning_suppressed_p change to him. For OPT_Wformat_ we don't want to call it anyway at all (as I said, I think there should be no suppression for it during the string discovery, there isn't just one -Wformat= option, there are many and warning_suppression_p even with no_warnings actually tests the TREE_NO_WARNING bit). Initially, I thought I'd restrict also call to fn with format_arg attribute handling in check_function_arguments_recurse to OPT_Wformat_ only, but after looking around, it perhaps is intentional that way, most functions with format_arg attribute don't have nonnull attribute for that arg too, various gettext implementations handle NULL argument by passing it through, but when result of gettext (NULL) etc. is passed to non-NULL argument, it makes sense to warn. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-01-21 Jakub Jelinek PR c++/104148 * c-common.h (check_function_arguments_recurse): Add for_format arg. * c-common.cc (check_function_nonnull): Pass false to check_function_arguments_recurse's last argument. (check_function_arguments_recurse): Add for_format argument, if true, don't stop on warning_suppressed_p. * c-format.cc (check_format_info): Pass true to check_function_arguments_recurse's last argument. * c-c++-common/Wformat-pr104148.c: New test. Jakub --- gcc/c-family/c-common.h.jj 2022-01-20 11:30:45.329581659 +0100 +++ gcc/c-family/c-common.h 2022-01-20 19:36:35.195601009 +0100 @@ -853,7 +853,8 @@ extern void check_function_arguments_rec (void *, tree, unsigned HOST_WIDE_INT), void *, tree, - unsigned HOST_WIDE_INT); + unsigned HOST_WIDE_INT, + opt_code); extern bool check_builtin_function_arguments (location_t, vec, tree, tree, int, tree *); extern void check_function_format (const_tree, tree, int, tree *, --- gcc/c-family/c-common.cc.jj 2022-01-20 11:30:45.329581659 +0100 +++ gcc/c-family/c-common.cc 2022-01-20 19:41:02.787901300 +0100 @@ -5592,7 +5592,7 @@ check_function_nonnull (nonnull_arg_ctx firstarg = 1; if (!closure) check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[0], - firstarg); + firstarg, OPT_Wnonnull); } tree attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (ctx.fntype)); @@ -5611,7 +5611,7 @@ check_function_nonnull (nonnull_arg_ctx if (a != NULL_TREE) for (int i = firstarg; i < nargs; i++) check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i], - i + 1); + i + 1, OPT_Wnonnull); else { /* Walk the argument list. If we encounter an argument number we @@ -5627,7 +5627,8 @@ check_function_nonnull (nonnull_arg_ctx if (a != NULL_TREE) check_function_arguments_recurse (check_nonnull_arg, &ctx, - argarray[i], i + 1); + argarray[i], i + 1, + OPT_Wnonnull); } } return ctx.warned_p; @@ -6095,14 +6096,16 @@ check_function_arguments (location_t loc /* Generic argument checking recursion routine. PARAM is the argument to be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked - once the argument is resolved. CTX is context for the callback. */ + once the argument is resolved. CTX is context for the callback. + OPT is the warning for which this is done. */ void check_function_arguments_recurse (void (*callback) (void *, tree, unsigned HOST_WIDE_INT), void *ctx, tree param, - unsigned HOST_WIDE_INT param_num) + unsigned HOST_WIDE_INT param_num, + opt_code opt) { - if (warning_suppressed_p (param)) + if (opt != OPT_Wformat_ && warning_suppressed_p (param)) return; if (CONVERT_EXPR_P (param) @@ -6111,7 +6114,8 @@ check_function_arguments_recurse (void ( { /* Strip coercion. */ check_function_arguments_recurse (callback, ctx, - TREE_OPERAND (param, 0), param_num); + TREE_OPERAND (param, 0), param_num, + opt); return; } @@ -6148,7 +6152,8 @@ check_function_arguments_recurse (void ( if (i == format_num) { check_function_arguments_recurse (callback, ctx, - inner_arg, param_num); + inner_arg, param_num, + opt); found_format_arg = true; break; } @@ -6170,10 +6175,10 @@ check_function_arguments_recurse (void ( /* Check both halves of the conditional expression. */ check_function_arguments_recurse (callback, ctx, TREE_OPERAND (param, 1), - param_num); + param_num, opt); check_function_arguments_recurse (callback, ctx, TREE_OPERAND (param, 2), - param_num); + param_num, opt); return; } } --- gcc/c-family/c-format.cc.jj 2022-01-20 11:30:45.346581418 +0100 +++ gcc/c-family/c-format.cc 2022-01-20 19:46:21.021500071 +0100 @@ -1531,7 +1531,7 @@ check_format_info (function_format_info format_ctx.arglocs = arglocs; check_function_arguments_recurse (check_format_arg, &format_ctx, - format_tree, arg_num); + format_tree, arg_num, OPT_Wformat_); location_t loc = format_ctx.res->format_string_loc; --- gcc/testsuite/c-c++-common/Wformat-pr104148.c.jj 2022-01-20 19:53:24.852638396 +0100 +++ gcc/testsuite/c-c++-common/Wformat-pr104148.c 2022-01-20 19:52:57.579015595 +0100 @@ -0,0 +1,33 @@ +/* PR c++/104148 */ +/* { dg-do compile } */ +/* { dg-options "-Wformat" } */ + +char *foo (const char *) __attribute__((format_arg(1))); +void bar (const char *, ...) __attribute__((format(printf, 1, 2))); + +void +baz (int x) +{ + bar ("%ld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar (x ? "%ld" : "%ld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar (x ? "%ld" : "%lld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */ + bar (foo ("%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar (x ? foo ("%ld") : "%ld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar (x ? foo ("%ld") : "%lld", x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */ + bar (foo (x ? "%ld" : "%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar (foo (x ? "%ld" : "%lld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */ + bar (("%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar ((x ? "%ld" : "%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar ((x ? "%ld" : "%lld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */ + bar ((foo ("%ld")), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar ((x ? foo ("%ld") : "%ld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar ((x ? foo ("%ld") : "%lld"), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */ + bar ((foo (x ? "%ld" : "%ld")), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + bar ((foo (x ? "%ld" : "%lld")), x); /* { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" } */ + /* { dg-warning "format '%lld' expects argument of type 'long long int', but argument 2 has type 'int'" "" { target *-*-* } .-1 } */ +}