From patchwork Tue Nov 23 22:06:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 48046 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 9932D3858031 for ; Tue, 23 Nov 2021 22:07:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9932D3858031 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637705235; bh=pYKgHeps/bs1Zcfx5HAQaKQp9cHIJ+eAgvftkbPBItI=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=RwgsRhj8ieMFi9FBxS6IQ6H96f7AN0vNpAX6L75rVyKZjLotwEQB0BPoSJqDs1euV k+th5PVltMsKP4O+cThIPpWvz7vbmgLtJzSJdr2nyR3SdIynrG3kanEg9dFtsmm+hg TPHyQYP9iT8BL4y9R41rBL9VwOOGntVwLIY+V0BI= 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 ESMTPS id C1A523858D28 for ; Tue, 23 Nov 2021 22:06:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C1A523858D28 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-548-nA5X2XqgPl-nWF7UN8006g-1; Tue, 23 Nov 2021 17:06:43 -0500 X-MC-Unique: nA5X2XqgPl-nWF7UN8006g-1 Received: by mail-qt1-f200.google.com with SMTP id h8-20020a05622a170800b002acc8656e05so606246qtk.7 for ; Tue, 23 Nov 2021 14:06:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=pYKgHeps/bs1Zcfx5HAQaKQp9cHIJ+eAgvftkbPBItI=; b=UF5wIAJDqFU+F794TG8kcybCpFtnrlTH78UvgQh2sZdyM9hBHzdzNfiXTSK30oy6xy hXYgwO/HHAffTpFEBdV7lDJBHPNx7gWEjpAHkedVNYwm6mYcFV2ies1R1M7bI2xhWRCh 0E/twT6l2Ochf7u7E1pBHp5vgaYljoi3ZcDz/R9hvP+2op7kYAm/+D0BKaGR+gZ1bO4l kPHKYpgxEPyy0jWsLk/gBnj7ubkG4YNt+2WnlD+hA68xJ4uAvRqoXAxEIcP9smQ79CgM zokQL/NURyG57mUT3pYIjU0qx7GztEHF09l/DqBlbbrIt4tuHikWSIU+zhvn+ZlBST0N YwcA== X-Gm-Message-State: AOAM533nXfI2hL1UIZZQu2HBLoSRU1PXrxNg/jMToT+ATPw0cPWdi+Cz pr5gPP4J5GLWguVRuZgHaRc2GyOWR2U3bAiChvMpXhM6GchPYvXPxAg7KzuNoISC5S/uHTdHa0N JcB0h/GfMtMKLMZOjiQ== X-Received: by 2002:ad4:4e14:: with SMTP id dl20mr758209qvb.32.1637705201816; Tue, 23 Nov 2021 14:06:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxO/7+HAkBSg4tnFMpnovy+VFWmezBcjTClVzgTeRuh5FrvAG9lyYiqvGMzTf4ETflvHi3zMQ== X-Received: by 2002:ad4:4e14:: with SMTP id dl20mr758144qvb.32.1637705201325; Tue, 23 Nov 2021 14:06:41 -0800 (PST) Received: from redhat.com ([2601:184:4780:4310::aac2]) by smtp.gmail.com with ESMTPSA id r16sm6658484qta.46.2021.11.23.14.06.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 14:06:40 -0800 (PST) Date: Tue, 23 Nov 2021 17:06:38 -0500 To: Jason Merrill Subject: [PATCH v2] c++: Fix missing NSDMI diagnostic in C++98 [PR103347] Message-ID: References: <20211122221734.364966-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.1.3 (2021-09-10) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-24.0 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" On Tue, Nov 23, 2021 at 02:42:12PM -0500, Jason Merrill wrote: > On 11/22/21 17:17, Marek Polacek wrote: > > Here the problem is that we aren't detecting a NSDMI in C++98: > > > > struct A { > > void *x = NULL; > > }; > > > > because maybe_warn_cpp0x uses input_location and that happens to point > > to NULL which comes from a system header. Jakub suggested changing the > > location to the '=', thereby avoiding the system header problem. To > > that end, I've added a new location_t member into cp_declarator. This > > member is used when this declarator is part of an init-declarator. The > > rest of the changes is obvious. I've also taken the liberty of adding > > loc_or_input_loc, since I want to avoid checking for UNKNOWN_LOCATION. > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > PR c++/103347 > > > > gcc/cp/ChangeLog: > > > > * cp-tree.h (struct cp_declarator): Add a location_t member. > > (maybe_warn_cpp0x): Add a location_t parameter with a default argument. > > (loc_or_input_loc): New. > > * decl.c (grokdeclarator): Use loc_or_input_loc. Pass init_loc down > > to maybe_warn_cpp0x. > > * error.c (maybe_warn_cpp0x): Add a location_t parameter. Use it. > > * parser.c (make_declarator): Initialize init_loc. > > (cp_parser_member_declaration): Set init_loc. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp0x/nsdmi-warn1.C: New test. > > * g++.dg/cpp0x/nsdmi-warn1.h: New file. > > --- > > gcc/cp/cp-tree.h | 16 +++++++++--- > > gcc/cp/decl.c | 22 +++++++++------- > > gcc/cp/error.c | 32 ++++++++++++------------ > > gcc/cp/parser.c | 2 ++ > > gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.C | 10 ++++++++ > > gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.h | 2 ++ > > 6 files changed, 55 insertions(+), 29 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.C > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.h > > > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > > index 3f56cb90d14..2037082b0c7 100644 > > --- a/gcc/cp/cp-tree.h > > +++ b/gcc/cp/cp-tree.h > > @@ -6231,9 +6231,11 @@ struct cp_declarator { > > /* If this declarator is parenthesized, this the open-paren. It is > > UNKNOWN_LOCATION when not parenthesized. */ > > location_t parenthesized; > > - > > - location_t id_loc; /* Currently only set for cdk_id, cdk_decomp and > > - cdk_function. */ > > + /* Currently only set for cdk_id, cdk_decomp and cdk_function. */ > > + location_t id_loc; > > + /* If this declarator is part of an init-declarator, the location of the > > + initializer. */ > > Currently this comment is inaccurate because we don't set it for all > init-declarators. That should be pretty trivial to do, even if we don't use > the location yet in other contexts. The following patch sets ->init_loc in a few more spots. I've looked at every cp_parser_declarator call and if it's followed by a =/{, I set ->init_loc. Pedantically, it's also an init-declarator if the declarator is followed by a requires-clause, but I've not looked for those cases. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Here the problem is that we aren't detecting a NSDMI in C++98: struct A { void *x = NULL; }; because maybe_warn_cpp0x uses input_location and that happens to point to NULL which comes from a system header. Jakub suggested changing the location to the '=', thereby avoiding the system header problem. To that end, I've added a new location_t member into cp_declarator. This member is used when this declarator is part of an init-declarator. The rest of the changes is obvious. I've also taken the liberty of adding loc_or_input_loc, since I want to avoid checking for UNKNOWN_LOCATION. PR c++/103347 gcc/cp/ChangeLog: * cp-tree.h (struct cp_declarator): Add a location_t member. (maybe_warn_cpp0x): Add a location_t parameter with a default argument. (loc_or_input_loc): New. * decl.c (grokdeclarator): Use loc_or_input_loc. Pass init_loc down to maybe_warn_cpp0x. * error.c (maybe_warn_cpp0x): Add a location_t parameter. Use it. * parser.c (make_declarator): Initialize init_loc. (cp_parser_member_declaration): Set init_loc. (cp_parser_condition): Likewise. (cp_parser_init_declarator): Likewise. (cp_parser_parameter_declaration): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/nsdmi-warn1.C: New test. * g++.dg/cpp0x/nsdmi-warn1.h: New file. --- gcc/cp/cp-tree.h | 16 +++++++++--- gcc/cp/decl.c | 22 +++++++++------- gcc/cp/error.c | 32 ++++++++++++------------ gcc/cp/parser.c | 8 ++++++ gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.C | 10 ++++++++ gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.h | 2 ++ 6 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.h base-commit: c59ec55c3459fba619e05ee7f59480b71e85ffd7 diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 3f56cb90d14..2037082b0c7 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6231,9 +6231,11 @@ struct cp_declarator { /* If this declarator is parenthesized, this the open-paren. It is UNKNOWN_LOCATION when not parenthesized. */ location_t parenthesized; - - location_t id_loc; /* Currently only set for cdk_id, cdk_decomp and - cdk_function. */ + /* Currently only set for cdk_id, cdk_decomp and cdk_function. */ + location_t id_loc; + /* If this declarator is part of an init-declarator, the location of the + initializer. */ + location_t init_loc; /* GNU Attributes that apply to this declarator. If the declarator is a pointer or a reference, these attribute apply to the type pointed to. */ @@ -6878,7 +6880,8 @@ extern const char *lang_decl_dwarf_name (tree, int, bool); extern const char *language_to_string (enum languages); extern const char *class_key_or_enum_as_string (tree); extern void maybe_warn_variadic_templates (void); -extern void maybe_warn_cpp0x (cpp0x_warn_str str); +extern void maybe_warn_cpp0x (cpp0x_warn_str str, + location_t = input_location); extern bool pedwarn_cxx98 (location_t, int, const char *, ...) ATTRIBUTE_GCC_DIAG(3,4); extern location_t location_of (tree); extern void qualified_name_lookup_error (tree, tree, tree, @@ -7996,6 +7999,11 @@ extern bool decl_in_std_namespace_p (tree); extern void require_complete_eh_spec_types (tree, tree); extern void cxx_incomplete_type_diagnostic (location_t, const_tree, const_tree, diagnostic_t); +inline location_t +loc_or_input_loc (location_t loc) +{ + return loc == UNKNOWN_LOCATION ? input_location : loc; +} inline location_t cp_expr_loc_or_loc (const_tree t, location_t or_loc) diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 9f68d1a5590..ae0e0bae9cc 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -11522,14 +11522,18 @@ grokdeclarator (const cp_declarator *declarator, if (initialized == SD_DEFAULTED || initialized == SD_DELETED) funcdef_flag = true; - location_t typespec_loc = smallest_type_location (type_quals, - declspecs->locations); - if (typespec_loc == UNKNOWN_LOCATION) - typespec_loc = input_location; - - location_t id_loc = declarator ? declarator->id_loc : input_location; - if (id_loc == UNKNOWN_LOCATION) - id_loc = input_location; + location_t typespec_loc = loc_or_input_loc (smallest_type_location + (type_quals, + declspecs->locations)); + location_t id_loc; + location_t init_loc; + if (declarator) + { + id_loc = loc_or_input_loc (declarator->id_loc); + init_loc = loc_or_input_loc (declarator->init_loc); + } + else + init_loc = id_loc = input_location; /* Look inside a declarator for the name being declared and get it as a string, for an error message. */ @@ -14042,7 +14046,7 @@ grokdeclarator (const cp_declarator *declarator, { /* An attempt is being made to initialize a non-static member. This is new in C++11. */ - maybe_warn_cpp0x (CPP0X_NSDMI); + maybe_warn_cpp0x (CPP0X_NSDMI, init_loc); /* If this has been parsed with static storage class, but errors forced staticp to be cleared, ensure NSDMI is diff --git a/gcc/cp/error.c b/gcc/cp/error.c index 872479369ab..98c1f0e4bdf 100644 --- a/gcc/cp/error.c +++ b/gcc/cp/error.c @@ -4428,84 +4428,84 @@ cp_printer (pretty_printer *pp, text_info *text, const char *spec, /* Warn about the use of C++0x features when appropriate. */ void -maybe_warn_cpp0x (cpp0x_warn_str str) +maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/) { if (cxx_dialect == cxx98) switch (str) { case CPP0X_INITIALIZER_LISTS: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "extended initializer lists " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_EXPLICIT_CONVERSION: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "explicit conversion operators " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_VARIADIC_TEMPLATES: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "variadic templates " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_LAMBDA_EXPR: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "lambda expressions " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_AUTO: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "C++11 auto only available with %<-std=c++11%> or " "%<-std=gnu++11%>"); break; case CPP0X_SCOPED_ENUMS: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "scoped enums only available with %<-std=c++11%> or " "%<-std=gnu++11%>"); break; case CPP0X_DEFAULTED_DELETED: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "defaulted and deleted functions " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_INLINE_NAMESPACES: if (pedantic) - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "inline namespaces " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_OVERRIDE_CONTROLS: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "override controls (override/final) " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_NSDMI: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "non-static data member initializers " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_USER_DEFINED_LITERALS: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "user-defined literals " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_DELEGATING_CTORS: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "delegating constructors " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_INHERITING_CTORS: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "inheriting constructors " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_ATTRIBUTES: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "C++11 attributes " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; case CPP0X_REF_QUALIFIER: - pedwarn (input_location, OPT_Wc__11_extensions, + pedwarn (loc, OPT_Wc__11_extensions, "ref-qualifiers " "only available with %<-std=c++11%> or %<-std=gnu++11%>"); break; diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index e2b5d6842fc..7a6a30208ef 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -1542,6 +1542,7 @@ make_declarator (cp_declarator_kind kind) declarator->declarator = NULL; declarator->parameter_pack_p = false; declarator->id_loc = UNKNOWN_LOCATION; + declarator->init_loc = UNKNOWN_LOCATION; return declarator; } @@ -13286,6 +13287,7 @@ cp_parser_condition (cp_parser* parser) attributes, prefix_attributes, &pushed_scope); + declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location; /* Parse the initializer. */ if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) { @@ -22492,6 +22494,7 @@ cp_parser_init_declarator (cp_parser* parser, { is_initialized = SD_INITIALIZED; initialization_kind = token->type; + declarator->init_loc = token->location; if (maybe_range_for_decl) *maybe_range_for_decl = error_mark_node; tmp_init_loc = token->location; @@ -24751,6 +24754,8 @@ cp_parser_parameter_declaration (cp_parser *parser, { tree type = decl_specifiers.type; token = cp_lexer_peek_token (parser->lexer); + if (declarator) + declarator->init_loc = token->location; /* If we are defining a class, then the tokens that make up the default argument must be saved and processed later. */ if (!template_parm_p && at_class_scope_p () @@ -27143,6 +27148,7 @@ cp_parser_member_declaration (cp_parser* parser) constant-initializer. When we call `grokfield', it will perform more stringent semantics checks. */ initializer_token_start = cp_lexer_peek_token (parser->lexer); + declarator->init_loc = initializer_token_start->location; if (function_declarator_p (declarator) || (decl_specifiers.type && TREE_CODE (decl_specifiers.type) == TYPE_DECL @@ -27171,6 +27177,8 @@ cp_parser_member_declaration (cp_parser* parser) && !function_declarator_p (declarator)) { bool x; + declarator->init_loc + = cp_lexer_peek_token (parser->lexer)->location; if (decl_specifiers.storage_class != sc_static) initializer = cp_parser_save_nsdmi (parser); else diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.C new file mode 100644 index 00000000000..aacc8b28255 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.C @@ -0,0 +1,10 @@ +// PR c++/103347 +// { dg-do compile { target c++11_down } } + +#include "nsdmi-warn1.h" + +struct A { + void *x = NULL; // { dg-error "11:only available" "" { target c++98_only } } + void *y{NULL}; // { dg-error "only available|extended initializer" "" { target c++98_only } } + int z = 1 + 2; // { dg-error "9:only available" "" { target c++98_only } } +}; diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.h b/gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.h new file mode 100644 index 00000000000..ee5be5a2478 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-warn1.h @@ -0,0 +1,2 @@ +#pragma GCC system_header +#define NULL (void *)0