From patchwork Sun Aug 28 13:02:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 74875 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1191898qga; Sun, 28 Aug 2016 06:03:26 -0700 (PDT) X-Received: by 10.66.72.40 with SMTP id a8mr23394570pav.15.1472389406071; Sun, 28 Aug 2016 06:03:26 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id h2si33244737pad.265.2016.08.28.06.03.25 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Aug 2016 06:03:26 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-434793-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-434793-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-434793-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=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:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; q=dns; s=default; b=NhD56DATZac5feu 2Jks4EA18EIEwxdvO3wVnHTEdXFKQmKof2PKXarOkNZv74nYiChmjig1ZCRn7LYG 2CIxC0YFs0v/Yj/QPqlvJk+afXdSHQrvw+T9UUWiGE7Vkh/GBYtXcsfELTap/qxQ koMgfFXkreSAxM6WersI7LvGFTxE= 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:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; s=default; bh=mCWSc3EFrAE4s/oy5BDMN ZoEkX8=; b=VmDObIbUW9Dm0YQfIE5Rrd+uxGhbhHQyiE9NF6RiZCWep+gW/PMoY NUHm3+0oUlg+T/v7c8iXp3oOx3Bdh6WCl/oATLb0UiKoDZ9Nu2RqwF1MY2lMe2tt 8cXI4nYClIGymUwkPHSi6s2o1Ohbb7wL5GBAdFfAV/qpJQwfQ4ksvk= Received: (qmail 36047 invoked by alias); 28 Aug 2016 13:03:13 -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 36029 invoked by uid 89); 28 Aug 2016 13:03:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.1 required=5.0 tests=AWL, BAYES_50, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=sk:null_po, specifier, component_ref, COMPONENT_REF X-HELO: mail-it0-f54.google.com Received: from mail-it0-f54.google.com (HELO mail-it0-f54.google.com) (209.85.214.54) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 28 Aug 2016 13:03:01 +0000 Received: by mail-it0-f54.google.com with SMTP id d65so61965882ith.0 for ; Sun, 28 Aug 2016 06:03:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=puMGERZj3u3uTBPz6Juq+FEKTLhDpsd4kxPUx5vU0eU=; b=FOaH52rnhh1jr7iFHqhmr6IS22jsjzDa2AoH6GjQXxzLIMTL/0/VykU5dcBQBlStGL +widOYnUnpDlKrSAIZRIz8DrLB+bgVfqLdBIH0eQGW4YmALDl8n5YGwVq0RQX7U+dFu7 Gp4NE72vHJQZOukgET1LwV+98zD1sf/jGaMYNj5MQXU7Hu/15Jf8Oramb3EgxuBurcvQ sipJwETbmvAKLvXaqp8Q6uV1x18W3kAv8SwyxqnOsVyNImWZ8jMIQJQJAcAB3KwVXsUp 3Pd4xogySCkS0ZmzVXD5OJrJgmoI8FdpLgtYo5wDoesloKggxm2G9AbmTwnHWL3dZu43 be+g== X-Gm-Message-State: AE9vXwP0mCca4/2aYjis6bObBNCoHgLWg8+H76nOBXTswPJbXykbKNN/nyzrEfh21joqrcW/Nc7czv6fRedUA5QV X-Received: by 10.107.130.81 with SMTP id e78mr15253781iod.137.1472389379533; Sun, 28 Aug 2016 06:02:59 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.65.232 with HTTP; Sun, 28 Aug 2016 06:02:59 -0700 (PDT) In-Reply-To: References: From: Prathamesh Kulkarni Date: Sun, 28 Aug 2016 18:32:59 +0530 Message-ID: Subject: Re: PR35503 - warn for restrict pointer To: Jason Merrill Cc: gcc Patches , Marek Polacek , "Joseph S. Myers" X-IsSubscribed: yes On 26 August 2016 at 21:25, Jason Merrill wrote: > On Fri, Aug 26, 2016 at 7:39 AM, Prathamesh Kulkarni > wrote: >> However with C++FE it appears TYPE_RESTRICT is not set for the >> parameters (buf and fmt) >> and hence the warning doesn't get emitted for C++. >> C FE sets TYPE_RESTRICT for them. I am not sure how to workaround this >> issue, and would be grateful for suggestions. > > In the C++ FE you can see TYPE_RESTRICT on the types of the > DECL_ARGUMENTS of the function. Thanks for the suggestions, I could indeed see TYPE_RESTRICT set on DECL_ARGUMENTS. The attached patch warns for both C and C++ with -Wrestrict, and I have put it under -Wall. I suppose we don't want to warn for null pointer args ? for eg: void f(int *restrict x, int *y); void foo(void) { f(NULL, NULL) ? } However I suppose warning for pointer constants other than zero is desirable ? I am not sure whether restrict is valid for obj-c/obj-c++, so I have just kept it to C and C++ in the patch. Should the warning also be included for obj-c and obj-c++ FE's ? Boostrapped and tested on x86_64-unknown-linux-gnu. OK to commit ? Thanks, Prathamesh > > Jason 2016-08-28 Prathamesh Kulkarni PR c/35503 * doc/invoke.texi: Document -Wrestrict. c-family/ * c-common.c (warn_for_restrict): New function. * c-common.h (warn_for_restrict): Declare it. * c.opt: New option -Wrestrit. c/ * c-parser.c (c_parser_postfix_expression_after_primary): Call warn_for_restrict. cp/ * parser.c (cp_parser_postfix_expression): Likewise. testsuite/ * c-c++-common/pr35503.c: New test-case. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 3feb910..ec8778f 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -13057,4 +13057,27 @@ diagnose_mismatched_attributes (tree olddecl, tree newdecl) return warned; } +/* Warn if an argument at position param_pos is passed to a + restrict-qualified param, and it aliases with another argument. */ + +void +warn_for_restrict (location_t loc, unsigned param_pos, vec *args) +{ + tree arg = (*args)[param_pos]; + if (operand_equal_p (arg, null_pointer_node, 0)) + return; + + for (unsigned i = 0; i < args->length (); ++i) + { + if (i == param_pos) + continue; + + tree current_arg = (*args)[i]; + if (operand_equal_p (arg, current_arg, 0)) + warning_at (loc, 0, + "passing argument %u to restrict qualified parameter aliases with " + "argument %u", param_pos + 1, i + 1); + } +} + #include "gt-c-family-c-common.h" diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index bc22baa..0526ad5 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -920,6 +920,7 @@ extern void c_parse_final_cleanups (void); extern void warn_for_omitted_condop (location_t, tree); extern void warn_for_memset (location_t, tree, tree, int); +extern void warn_for_restrict (location_t, unsigned, vec *); /* These macros provide convenient access to the various _STMT nodes. */ diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index a5358ed..a029a86 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1012,6 +1012,11 @@ Wduplicate-decl-specifier C ObjC Var(warn_duplicate_decl_specifier) Warning LangEnabledBy(C ObjC,Wall) Warn when a declaration has duplicate const, volatile, restrict or _Atomic specifier. +Wrestrict +C C++ Var(warn_restrict) Warning LangEnabledBy(C C++,Wall) +Warn when an argument passed to a restrict-qualified parameter aliases with +another argument. + ansi C ObjC C++ ObjC++ A synonym for -std=c89 (for C) or -std=c++98 (for C++). diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index fe0c95f..54e1e87 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -8369,6 +8369,17 @@ c_parser_postfix_expression_after_primary (c_parser *parser, warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask); } + if (TREE_CODE (expr.value) == FUNCTION_DECL && warn_restrict) + { + unsigned param_pos = 0; + for (tree t = TYPE_ARG_TYPES (TREE_TYPE (expr.value)); t; t = TREE_CHAIN (t), param_pos++) + { + tree type = TREE_VALUE (t); + if (POINTER_TYPE_P (type) && TYPE_RESTRICT (type)) + warn_for_restrict (expr_loc, param_pos, exprlist); + } + } + start = expr.get_start (); finish = parser->tokens_buf[0].get_finish (); expr.value diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 690e928..4710a08 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -6878,6 +6878,20 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p, warn_for_memset (input_location, arg0, arg2, literal_mask); } + if (TREE_CODE (postfix_expression) == FUNCTION_DECL + && warn_restrict) + { + unsigned param_pos = 0; + for (tree decl = DECL_ARGUMENTS (postfix_expression); + decl != NULL_TREE; + decl = DECL_CHAIN (decl), param_pos++) + { + tree type = TREE_TYPE (decl); + if (POINTER_TYPE_P (type) && TYPE_RESTRICT (type)) + warn_for_restrict (input_location, param_pos, args); + } + } + if (TREE_CODE (postfix_expression) == COMPONENT_REF) { tree instance = TREE_OPERAND (postfix_expression, 0); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 1f04501..ac8aab5 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -261,7 +261,7 @@ Objective-C and Objective-C++ Dialects}. -Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol -Wchar-subscripts -Wclobbered -Wcomment -Wconditionally-supported @gol -Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol --Wdelete-incomplete @gol +-Wdelete-incomplete -Wrestrict @gol -Wno-deprecated -Wno-deprecated-declarations -Wno-designated-init @gol -Wdisabled-optimization @gol -Wno-discarded-qualifiers -Wno-discarded-array-qualifiers @gol @@ -5274,6 +5274,12 @@ compilations. Warn when deleting a pointer to incomplete type, which may cause undefined behavior at runtime. This warning is enabled by default. +@item -Wrestrict @r{(C and C++ only)} +@opindex Wrestrict +@opindex Wno-restrict +Warn when an argument passed to a restrict-qualified parameter +aliases with another argument + @item -Wuseless-cast @r{(C++ and Objective-C++ only)} @opindex Wuseless-cast @opindex Wno-useless-cast diff --git a/gcc/testsuite/c-c++-common/pr35503.c b/gcc/testsuite/c-c++-common/pr35503.c new file mode 100644 index 0000000..fb12a71 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr35503.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-Wrestrict" } */ + +int foo (const char *__restrict buf, const char *__restrict fmt, ...); + +void f(void) +{ + char buf[100] = "hello"; + foo (buf, "%s-%s", buf, "world"); /* { dg-warning "passing argument 1 to restrict qualified parameter aliases with argument 3" } */ +}