From patchwork Wed Oct 30 07:24:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 2004028 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=SDCsk0FO; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XddvD0NSJz1xwF for ; Wed, 30 Oct 2024 18:25:28 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 090473858282 for ; Wed, 30 Oct 2024 07:25:25 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTP id 779A13858D28 for ; Wed, 30 Oct 2024 07:24:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 779A13858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 779A13858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730273104; cv=none; b=bZBVQuOYigB4c3ZEEK/uPNH7ewYQyHPLerQhXt7xw9jvecd55vi+bhqTbfVep7mNRSz5TNQGIRcuhJGZLgMJGaGVgfTrxSQ/2LN+AB4f+dB2BfEwa7U/jHHgEwG8ZmBvN0uFWsevW+30st22VrywZ/P2XxtiQ3DTogOiEPC32Xc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730273104; c=relaxed/simple; bh=gwA/PcvC5V8pSwNmUq66SWEHsylc51c9ALghnnkcfAQ=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=srmv+ovE4QP4W/uTzJXVeRTlV1mNcoyNO2LGNyqQ6AJUMk5U459deu1uXd/IWJSgScvEs8x55VwfleJZi1fQHetG2q3szSkc/OfgCghmITe8Xw5zvoqp9OKSvVzRGiV/H1H3jUEcufwJNhFH7ogl7X48yfvYu9uXVOBeGtL63Hk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730273099; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=DIDxpXgAGqPO4Nh33YCOe0QtRMmn4Q0LwlvIr+sk+rU=; b=SDCsk0FOH28OJASHPPkWpxX/cMMmFCGocRlPHAu+wVnQCTt1gfbMjYqGE7bPI/2VMMqazB ARMrxcX4zo9SbGHQBcIMYS5aaxkEYrJPr80TCwPRIYp1Jj0U/6oSnzuKeFCc4sF3t+h4iq jHg4tDBNSyx22iDZn6HT+nJpQMl0/mU= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-20-qa_GGOtAMJunhM4MAmRnoQ-1; Wed, 30 Oct 2024 03:24:57 -0400 X-MC-Unique: qa_GGOtAMJunhM4MAmRnoQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 093D91955F40 for ; Wed, 30 Oct 2024 07:24:57 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6E37C19560A7; Wed, 30 Oct 2024 07:24:56 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 49U7OrEh1784761 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 30 Oct 2024 08:24:53 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 49U7Orem1784760; Wed, 30 Oct 2024 08:24:53 +0100 Date: Wed, 30 Oct 2024 08:24:52 +0100 From: Jakub Jelinek To: "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c: Diagnose char argument to __builtin_stdc_* Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! When working on __builtin_stdc_rotate_*, I've noticed that while the second argument to those is explicitly allowed to have char type, the first argument to all the stdc_* type-generic functions is - standard unsigned integer type, excluding bool; - extended unsigned integer type; - or, bit-precise unsigned integer type whose width matches a standard or extended integer type, excluding bool. but the __builtin_stdc_* lowering code was diagnosing just !INTEGRAL_TYPE_P ENUMERAL_TYPE BOOLEAN_TYPE !TYPE_UNSIGNED Now, with -funsigned-char plain char type is TYPE_UNSIGNED, yet it isn't allowed because it isn't standard unsigned integer type, nor extended unsigned integer type, nor bit-precise unsigned integer type. The following patch diagnoses char arguments and adds testsuite coverage for that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Or should I make it a pedwarn instead? 2024-10-30 Jakub Jelinek gcc/c/ * c-parser.cc (c_parser_postfix_expression): Diagnose if first __builtin_stdc_* argument has char type even when -funsigned-char. gcc/testsuite/ * gcc.dg/builtin-stdc-bit-3.c: New test. * gcc.dg/builtin-stdc-rotate-3.c: New test. Jakub --- gcc/c/c-parser.cc.jj 2024-10-29 09:06:12.976008357 +0100 +++ gcc/c/c-parser.cc 2024-10-29 16:45:45.770813360 +0100 @@ -12382,6 +12382,14 @@ c_parser_postfix_expression (c_parser *p expr.set_error (); break; } + if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_p->value)) + == char_type_node) + { + error_at (loc, "argument 1 in call to function " + "%qs has % type", name); + expr.set_error (); + break; + } tree arg = arg_p->value; tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg)); /* Expand: --- gcc/testsuite/gcc.dg/builtin-stdc-bit-3.c.jj 2024-10-29 16:48:59.186127709 +0100 +++ gcc/testsuite/gcc.dg/builtin-stdc-bit-3.c 2024-10-29 16:49:56.188336214 +0100 @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-funsigned-char" } */ + +void +foo (void) +{ + __builtin_stdc_leading_zeros ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_leading_zeros' has 'char' type" } */ + __builtin_stdc_leading_ones ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_leading_ones' has 'char' type" } */ + __builtin_stdc_trailing_zeros ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_trailing_zeros' has 'char' type" } */ + __builtin_stdc_trailing_ones ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_trailing_ones' has 'char' type" } */ + __builtin_stdc_first_leading_zero ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_first_leading_zero' has 'char' type" } */ + __builtin_stdc_first_leading_one ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_first_leading_one' has 'char' type" } */ + __builtin_stdc_first_trailing_zero ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_first_trailing_zero' has 'char' type" } */ + __builtin_stdc_first_trailing_one ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_first_trailing_one' has 'char' type" } */ + __builtin_stdc_count_zeros ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_count_zeros' has 'char' type" } */ + __builtin_stdc_count_ones ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_count_ones' has 'char' type" } */ + __builtin_stdc_has_single_bit ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_has_single_bit' has 'char' type" } */ + __builtin_stdc_bit_width ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_bit_width' has 'char' type" } */ + __builtin_stdc_bit_floor ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_bit_floor' has 'char' type" } */ + __builtin_stdc_bit_ceil ((char) 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_bit_ceil' has 'char' type" } */ +} --- gcc/testsuite/gcc.dg/builtin-stdc-rotate-3.c.jj 2024-10-29 16:48:55.506178811 +0100 +++ gcc/testsuite/gcc.dg/builtin-stdc-rotate-3.c 2024-10-29 16:50:15.338070312 +0100 @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-funsigned-char" } */ + +void +foo (void) +{ + __builtin_stdc_rotate_left ((char) 0, 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_rotate_left' has 'char' type" } */ + __builtin_stdc_rotate_right ((char) 0, 0); /* { dg-error "argument 1 in call to function '__builtin_stdc_rotate_right' has 'char' type" } */ +}