From patchwork Fri Nov 15 01:34:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1195267 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-513527-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="sL/U6KNu"; 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 47Dgr02Sjrz9s7T for ; Fri, 15 Nov 2019 12:34:22 +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=HiwcCpweHowBBcTN5GrYxLocx2xxGyh6815L/MAGu6R51KfMhsaYg WKU2hxHOu7c+M4QPH1bl10tupZBT+yJ69FzwTrI1xQkkQV91x7HHEIHgssdnQjIf FxgICdppg1Qu5UlnUh/SGdyaBqIkFzqfHtCycUgSmSK1uMZX6epBYs= 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=g/mRLP3yiyfy15gnEnTTttkTRDE=; b=sL/U6KNupP2e6G+rdCbh NHUAOihgPLjSNT767yBKzyYw0Z0Hu/SYlpIgFXXHmnm8U8rGuaXtp4THedF3DDYT kMyq8rN9KgyzG5ELRRa5Y4XKExAckIJtt6Z2HdYxHxZWA2PaOtlpqmzmTY95rdk5 dLJLrOBTmqCv5urtoi/Pnko= Received: (qmail 29049 invoked by alias); 15 Nov 2019 01:34:14 -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 29020 invoked by uid 89); 15 Nov 2019 01:34:10 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: esa3.mentor.iphmx.com Received: from esa3.mentor.iphmx.com (HELO esa3.mentor.iphmx.com) (68.232.137.180) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 15 Nov 2019 01:34:09 +0000 IronPort-SDR: oKjetbv7t4eNHdrlooAskHe771wF7s53GdNIv8HSR4qXWGdzsemy5OIPLictnJIdvDX0rzsSpt Xd/dsxoTKGhnge2KFzIlA8UR/M1xFCHyeUspf4HPDDZrb//XTCFKwfJxWq6duzor1DFQiW+u6o rQnZH6sAHmcyW67yEsbdjKwqsp9kEI2NSY9JAf7BwI1JfF+c8g/OfanYwp/iRlRw9RjdcQJ9zX 3U3HDEVA6ZFLnxTJAz5rOI5sHppDCEUJrfcuxD0+lPobcAIn9l006T1aj9OLhoU6ZcLcq6hUtN GnE= Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa3.mentor.iphmx.com with ESMTP; 14 Nov 2019 17:34:06 -0800 IronPort-SDR: WxdP/8aZq0UluJAouIpLGzJ6S6azxkc9ES/lU4+EkZcXSBYNq7aMINy3N01dQzAGX5FHxq21uT Xk1tR64Ky6W68eEjXFojlA15dsKWY9US46fblVnOvfZj5yljUictZJyjJC1aD5SKGXcqM0C4IO RThlXhVdcJ+Ba0KWKolGbp9j/ANZrhzbyc9MlguZHyNL8JRbttvoZ5QkiRX9luYjPVGZdqtANV YoPaKIYy6CWXp+ozGWl45YwWb5KpTLV5aNgTyySSBKU43BCgdUL9gnM8z0cC6fOglsHI57Sl/6 CJE= Date: Fri, 15 Nov 2019 01:34:00 +0000 From: Joseph Myers To: Subject: Improve checks on C2x fallthrough attribute Message-ID: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 When adding C2x attribute support, some [[fallthrough]] support appeared as a side-effect because of code for that attribute going through separate paths from the normal attribute handling. However, going through those paths without the normal attribute handlers meant that certain checks, such as for the invalid usage [[fallthrough()]], did not operate. This patch improves checks by adding this attribute to the standard attribute table, so that the parser knows it expects no arguments, along with adding an explicit check for "[[fallthrough]];" attribute-declarations at top level. As with other attributes, there are still cases where warnings should be pedwarns because C2x constraints are violated, but this patch improves the attribute handling. Bootstrapped with no regressions on x86_64-pc-linux-gnu. Applied to mainline. gcc/c: 2019-11-15 Joseph Myers * c-decl.c (std_attribute_table): Add fallthrough. * c-parser.c (c_parser_declaration_or_fndef): Diagnose fallthrough attribute at top level. gcc/c-family: 2019-11-15 Joseph Myers * c-attribs.c (handle_fallthrough_attribute): Remove static. * c-common.h (handle_fallthrough_attribute): Declare. gcc/testsuite: 2019-11-15 Joseph Myers * gcc.dg/c2x-attr-fallthrough-2.c, gcc.dg/c2x-attr-fallthrough-3.c: New tests. Index: gcc/c/c-decl.c =================================================================== --- gcc/c/c-decl.c (revision 278268) +++ gcc/c/c-decl.c (working copy) @@ -4343,6 +4343,8 @@ const struct attribute_spec std_attribute_table[] affects_type_identity, handler, exclude } */ { "deprecated", 0, 1, false, false, false, false, handle_deprecated_attribute, NULL }, + { "fallthrough", 0, 0, false, false, false, false, + handle_fallthrough_attribute, NULL }, { NULL, 0, 0, false, false, false, false, NULL, NULL } }; Index: gcc/c/c-parser.c =================================================================== --- gcc/c/c-parser.c (revision 278268) +++ gcc/c/c-parser.c (working copy) @@ -1927,9 +1927,15 @@ c_parser_declaration_or_fndef (c_parser *parser, b { if (fallthru_attr_p != NULL) *fallthru_attr_p = true; - tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH, - void_type_node, 0); - add_stmt (fn); + if (nested) + { + tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH, + void_type_node, 0); + add_stmt (fn); + } + else + pedwarn (here, OPT_Wattributes, + "% attribute at top level"); } else if (empty_ok && !(have_attrs && specs->non_std_attrs_seen_p)) Index: gcc/c-family/c-attribs.c =================================================================== --- gcc/c-family/c-attribs.c (revision 278268) +++ gcc/c-family/c-attribs.c (working copy) @@ -144,7 +144,6 @@ static tree handle_simd_attribute (tree *, tree, t static tree handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *); static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *); -static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *); static tree handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *); static tree handle_copy_attribute (tree *, tree, tree, int, bool *); @@ -4114,7 +4113,7 @@ handle_designated_init_attribute (tree *node, tree /* Handle a "fallthrough" attribute; arguments as in struct attribute_spec.handler. */ -static tree +tree handle_fallthrough_attribute (tree *, tree name, tree, int, bool *no_add_attrs) { Index: gcc/c-family/c-common.h =================================================================== --- gcc/c-family/c-common.h (revision 278268) +++ gcc/c-family/c-common.h (working copy) @@ -1359,6 +1359,7 @@ extern void warn_for_multistatement_macros (locati extern bool attribute_takes_identifier_p (const_tree); extern tree handle_deprecated_attribute (tree *, tree, tree, int, bool *); extern tree handle_unused_attribute (tree *, tree, tree, int, bool *); +extern tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *); extern int parse_tm_stmt_attr (tree, int); extern int tm_attr_to_mask (tree); extern tree tm_mask_to_attr (int); Index: gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c =================================================================== --- gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c (nonexistent) +++ gcc/testsuite/gcc.dg/c2x-attr-fallthrough-2.c (working copy) @@ -0,0 +1,35 @@ +/* Test C2x attribute syntax. Invalid use of fallthrough attribute. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors -Wextra" } */ + +[[fallthrough]]; /* { dg-error "'fallthrough' attribute at top level" } */ + +int [[fallthrough]] x; /* { dg-warning "ignored" } */ +/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ + +int g () [[fallthrough]]; /* { dg-warning "ignored" } */ +/* { dg-message "that appertains to a type-specifier" "appertains" { target *-*-* } .-1 } */ + +int +f (int a) +{ + [[fallthrough]] int b = 2; /* { dg-warning "not followed by|ignored" } */ + switch (a) + { + case 1: + b = 1; /* { dg-warning "may fall through" } */ + case 2: + b = 2; /* { dg-warning "may fall through" } */ + [[fallthrough()]]; /* { dg-error "does not take any arguments" } */ + case 3: + b += 7; + break; + case 4: + b = 4; /* { dg-warning "may fall through" } */ + [[fallthrough(1)]]; /* { dg-error "does not take any arguments|expected" } */ + case 5: + b += 5; + break; + } + [[fallthrough]] return b; /* { dg-warning "ignored" } */ +} Index: gcc/testsuite/gcc.dg/c2x-attr-fallthrough-3.c =================================================================== --- gcc/testsuite/gcc.dg/c2x-attr-fallthrough-3.c (nonexistent) +++ gcc/testsuite/gcc.dg/c2x-attr-fallthrough-3.c (working copy) @@ -0,0 +1,18 @@ +/* Test C2x attribute syntax. Invalid use of fallthrough attribute + outside switch or in bad context inside switch. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors -Wextra" } */ + +int +f (int a) +{ + [[fallthrough]]; /* { dg-error "invalid use of attribute 'fallthrough'" } */ + switch (a) + { + case 1: + a++; + [[fallthrough]]; /* { dg-warning "attribute 'fallthrough' not preceding a case label or default label" } */ + a++; + } + return a; +}