From patchwork Fri Jan 29 17:01:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 60812 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp1237186lbb; Fri, 29 Jan 2016 09:02:40 -0800 (PST) X-Received: by 10.98.15.207 with SMTP id 76mr15000378pfp.60.1454086960490; Fri, 29 Jan 2016 09:02:40 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id w3si25245898pfi.110.2016.01.29.09.02.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 Jan 2016 09:02:40 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-420396-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; spf=pass (google.com: domain of gcc-patches-return-420396-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-420396-patch=linaro.org@gcc.gnu.org; dkim=pass header.i=@gcc.gnu.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:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=smmq43PZlzgLukXp+2 Dhp9+bD4duji+1JjrifQuNQXhOqJAwyCEz7VYWiSzHrEiDjkKDXs25Ocpx+dFvHj ENLrUuCMykUozrBztnJMAvnmluhLmQGgxmA8FcXMhDyH2lM6u/TUbI7VLJvfYgIg iIxaM756KaneQmFz4EJDO0Vfo= 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:date:message-id:subject :from:to:cc:content-type; s=default; bh=CeXML1dLwSOHjFEmSUC5m2eJ ZRQ=; b=QdMIUmxTeWB3ehJvDbUr8EgzEnSZVBlzvI7AUtevpTE8Tmqfh1POyypI PL5VcQfBItNu7sXdX2sPf7g2rdTN4R8Ld1ZmxXpyEt/DnvjDYUpb7jlW3b8TGPQN 4OXn5dJ6qHbwGAXQ/8Tivk6Y5k2C2/Cr4OlkhO+aldzl3BYvCCk= Received: (qmail 36025 invoked by alias); 29 Jan 2016 17:02:11 -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 35994 invoked by uid 89); 29 Jan 2016 17:02:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=UD:value, util, UD:dg.exp, dgexp X-HELO: mail-io0-f181.google.com Received: from mail-io0-f181.google.com (HELO mail-io0-f181.google.com) (209.85.223.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 29 Jan 2016 17:02:01 +0000 Received: by mail-io0-f181.google.com with SMTP id 9so16289767iom.1 for ; Fri, 29 Jan 2016 09:02:00 -0800 (PST) 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:date :message-id:subject:from:to:cc:content-type; bh=tBh10+k7inW2mTR798TVzMETu4t1yr93DT5O7iq5aUY=; b=CtruwJb94uqj3NDAktbPdMkKJV3JNZNBjgAgvMAg37ECHp7AjZ0Kf9MgVTylavCMIO PSFotBOa/m4jhrBh6Dgxx7YdPpefNvXqrgIeiQ9nq+KezGX/sk9bo3Fygz9fLqXumfGM 8cHjx62QOJqYhucauPY53uXEb1aYHoUSYhAZIg79JA0D3t948MiqBJZ+6l9heFAbdMZt e8oIqtsSYsyUWVx3IGYx4T8zwyU2IyhRl/R1FE4/yOHxdtzLDP2XF6Vp9jINgH7is+4T qD2M1QbCyeKbdFKRwT2GnTXk277jSwlpMjeaxEmThLROXSD04wJAX8Jl8K/HJLADcZwg BNcw== X-Gm-Message-State: AG10YOSiXnnQeAEIOc+R7jYe/2jHMS/q0xu+X1MWKmj5KBjcirW2D7Lb5mTz8UHEFtaSN5t+j/fUFTbh7HuQXnyt MIME-Version: 1.0 X-Received: by 10.107.18.101 with SMTP id a98mr10914916ioj.181.1454086918944; Fri, 29 Jan 2016 09:01:58 -0800 (PST) Received: by 10.36.199.66 with HTTP; Fri, 29 Jan 2016 09:01:58 -0800 (PST) In-Reply-To: <20160128233308.GI25193@redhat.com> References: <20160128233308.GI25193@redhat.com> Date: Fri, 29 Jan 2016 22:31:58 +0530 Message-ID: Subject: Re: [C++ patch] report better diagnostic for static following '[' in parameter declaration From: Prathamesh Kulkarni To: Marek Polacek Cc: gcc Patches X-IsSubscribed: yes On 29 January 2016 at 05:03, Marek Polacek wrote: > On Fri, Jan 29, 2016 at 04:46:56AM +0530, Prathamesh Kulkarni wrote: >> @@ -19016,10 +19017,22 @@ cp_parser_direct_declarator (cp_parser* parser, >> cp_lexer_consume_token (parser->lexer); >> /* Peek at the next token. */ >> token = cp_lexer_peek_token (parser->lexer); >> + >> + /* If static keyword immediately follows [, report error. */ >> + if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC) >> + && current_binding_level->kind == sk_function_parms) >> + { >> + error_at (token->location, >> + "static array size is a C99 feature," >> + "not permitted in C++"); >> + bounds = error_mark_node; >> + } >> + > > I think this isn't sufficient as-is; if we're changing the diagnostics here, > we should also handle e.g. void f(int a[const 10]); where clang++ says > g.C:1:13: error: qualifier in array size is a C99 feature, not permitted in C++ > > And also e.g. > void f(int a[const static 10]); > void f(int a[static const 10]); > and similar. Thanks for the review. AFAIK the type-qualifiers would be const, restrict, volatile and _Atomic (n1570 p 6.7.3) ? I added a check for those and for variable length array. I am having issues with writing the test-case, some cases pass with -std=c++11 but fail with -std=c++98. Could you please have a look ? Thanks, Prathamesh > > Marek diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 33f1df3..04137b3 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -982,6 +982,24 @@ cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer) } } +static bool +cp_lexer_next_token_is_c_type_qual (cp_lexer *lexer) +{ + if (cp_lexer_next_token_is_keyword (lexer, RID_CONST) + || cp_lexer_next_token_is_keyword (lexer, RID_VOLATILE)) + return true; + + cp_token *token = cp_lexer_peek_token (lexer); + if (token->type == CPP_NAME) + { + tree name = token->u.value; + const char *p = IDENTIFIER_POINTER (name); + return !strcmp (p, "restrict") || !strcmp (p, "_Atomic"); + } + + return false; +} + /* Returns TRUE iff the token T begins a decltype type. */ static bool @@ -18998,10 +19016,40 @@ cp_parser_direct_declarator (cp_parser* parser, cp_lexer_consume_token (parser->lexer); /* Peek at the next token. */ token = cp_lexer_peek_token (parser->lexer); + + /* If static or type-qualifier or * immediately follows [, + report error. */ + if (current_binding_level->kind == sk_function_parms) + { + if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC)) + { + error_at (token->location, + "static array size is a C99 feature, " + "not permitted in C++"); + bounds = error_mark_node; + } + else if (cp_lexer_next_token_is_c_type_qual (parser->lexer)) + { + error_at (token->location, + "qualifier in array size is a C99 feature, " + "not permitted in C++"); + bounds = error_mark_node; + } + + else if (token->type == CPP_MULT) + { + error_at (token->location, + "variable-length array size is a C99 feature, " + "not permitted in C++"); + bounds = error_mark_node; + } + } + /* If the next token is `]', then there is no constant-expression. */ - if (token->type != CPP_CLOSE_SQUARE) + if (token->type != CPP_CLOSE_SQUARE && bounds != error_mark_node) { + bool non_constant_p; bounds = cp_parser_constant_expression (parser, diff --git a/gcc/testsuite/g++.dg/parse/static-array-error.C b/gcc/testsuite/g++.dg/parse/static-array-error.C new file mode 100644 index 0000000..028320d --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/static-array-error.C @@ -0,0 +1,33 @@ +// { dg-do compile } + +void f1(int a[static 10]); /* { dg-error "static array size is a C99 feature" } */ +/* { dg-error "expected '\\]' before 'static'" "" { target *-*-* } 3 } */ +/* { dg-error "expected '\\)' before 'static'" "" { target *-*-* } 3 } */ +/* { dg-error "expected initializer before 'static'" "" { target *-*-* } 3 } */ + +void f2(int a[const 10]); /* { dg-error "qualifier in array size is a C99 feature" } */ +/* { dg-error "expected '\\]' before 'const'" "" { target *-*-* } 8 } */ +/* { dg-error "expected '\\)' before 'const'" "" { target *-*-* } 8 } */ +/* { dg-error "expected initializer before numeric constant" "" { target *-*-* } 8 } */ + +void f3(int a[restrict 10]); /* { dg-error "qualifier in array size is a C99 feature" } */ +/* { dg-error "expected '\\]' before 'restrict'" "" { target *-*-* } 13 } */ +/* { dg-error "expected '\\)' before 'restrict'" "" { target *-*-* } 13 } */ +/* { dg-error "expected initializer before 'restrict'" "" { target *-*-* } 13 } */ + +void f4(int a[volatile 10]); /* { dg-error "qualifier in array size is a C99 feature" } */ +/* { dg-error "expected '\\]' before 'volatile'" "" { target *-*-* } 18 } */ +/* { dg-error "expected '\\)' before 'volatile'" "" { target *-*-* } 18 } */ +/* { dg-error "expected initializer before numeric constant" "" { target *-*-* } 18 } */ + + +void f4(int a[_Atomic 10]); /* { dg-error "qualifier in array size is a C99 feature" } */ +/* { dg-error "expected '\\]' before '_Atomic'" "" { target *-*-* } 24 } */ +/* { dg-error "expected '\\)' before '_Atomic'" "" { target *-*-* } 24 } */ +/* { dg-error "expected initializer before '_Atomic'" "" { target *-*-* } 24 } */ + + +void f5(int a[*]); /* { dg-error "variable-length array size is a C99 feature" } */ +/* { dg-error "expected ']' before '*' token" "" { target *-*-* } 24 } */ +/* { dg-error "expected '\\)' before '*' token" "" { target *-*-* } 24 } */ +/* { dg-error "expected initializer before '*' token" "" { target *-*-* } 24 } */