public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/91304 - prefix attributes ignored in condition
@ 2019-08-21 16:55 Marek Polacek
  2019-08-23  2:19 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2019-08-21 16:55 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

Currently, we disregard prefix attributes in conditions, e.g.:

  if ([[maybe_unused]] int i = f()) { }

The problem here is that although we've parsed the attribute, it
was never passed down to start_decl, so the effects were lost.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-08-21  Marek Polacek  <polacek@redhat.com>

	PR c++/91304 - prefix attributes ignored in condition.
	* parser.c (cp_parser_condition): Handle prefix attributes.

	* g++.dg/cpp0x/gen-attrs-70.C: New test.

diff --git gcc/cp/parser.c gcc/cp/parser.c
index dbbfe1dbc2f..b410a6c030f 100644
--- gcc/cp/parser.c
+++ gcc/cp/parser.c
@@ -12066,6 +12066,10 @@ cp_parser_condition (cp_parser* parser)
   /* Restore the saved message.  */
   parser->type_definition_forbidden_message = saved_message;
 
+  /* Gather the attributes that were provided with the
+     decl-specifiers.  */
+  tree prefix_attributes = type_specifiers.attributes;
+
   cp_parser_maybe_commit_to_declaration (parser,
 					 type_specifiers.any_specifiers_p);
 
@@ -12116,7 +12120,7 @@ cp_parser_condition (cp_parser* parser)
 	  /* Create the declaration.  */
 	  decl = start_decl (declarator, &type_specifiers,
 			     /*initialized_p=*/true,
-			     attributes, /*prefix_attributes=*/NULL_TREE,
+			     attributes, prefix_attributes,
 			     &pushed_scope);
 
 	  /* Parse the initializer.  */
diff --git gcc/testsuite/g++.dg/cpp0x/gen-attrs-70.C gcc/testsuite/g++.dg/cpp0x/gen-attrs-70.C
new file mode 100644
index 00000000000..90a2e97a3f6
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-70.C
@@ -0,0 +1,13 @@
+// PR c++/91304 - prefix attributes ignored in condition.
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wall -Wextra" }
+
+int f();
+
+void g()
+{   
+  if ([[maybe_unused]] int i = f()) { }
+  if ([[deprecated]] int i = f()) { i = 10; } // { dg-warning ".i. is deprecated" }
+  if (int i [[maybe_unused]] = f()) { }
+  if (int i [[deprecated]] = f()) { i = 10; } // { dg-warning ".i. is deprecated" }
+}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: C++ PATCH for c++/91304 - prefix attributes ignored in condition
  2019-08-21 16:55 C++ PATCH for c++/91304 - prefix attributes ignored in condition Marek Polacek
@ 2019-08-23  2:19 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2019-08-23  2:19 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

On Wed, Aug 21, 2019 at 7:24 AM Marek Polacek <polacek@redhat.com> wrote:
>
> Currently, we disregard prefix attributes in conditions, e.g.:
>
>   if ([[maybe_unused]] int i = f()) { }
>
> The problem here is that although we've parsed the attribute, it
> was never passed down to start_decl, so the effects were lost.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2019-08-21  Marek Polacek  <polacek@redhat.com>
>
>         PR c++/91304 - prefix attributes ignored in condition.
>         * parser.c (cp_parser_condition): Handle prefix attributes.
>
>         * g++.dg/cpp0x/gen-attrs-70.C: New test.
>
> diff --git gcc/cp/parser.c gcc/cp/parser.c
> index dbbfe1dbc2f..b410a6c030f 100644
> --- gcc/cp/parser.c
> +++ gcc/cp/parser.c
> @@ -12066,6 +12066,10 @@ cp_parser_condition (cp_parser* parser)
>    /* Restore the saved message.  */
>    parser->type_definition_forbidden_message = saved_message;
>
> +  /* Gather the attributes that were provided with the
> +     decl-specifiers.  */
> +  tree prefix_attributes = type_specifiers.attributes;

The patch is OK, since it follows the existing pattern, but it's weird
that we depend on various places in the parser to extract the
attributes from the specifiers rather than deal with that in
grokdeclarator.

> +
>    cp_parser_maybe_commit_to_declaration (parser,
>                                          type_specifiers.any_specifiers_p);
>
> @@ -12116,7 +12120,7 @@ cp_parser_condition (cp_parser* parser)
>           /* Create the declaration.  */
>           decl = start_decl (declarator, &type_specifiers,
>                              /*initialized_p=*/true,
> -                            attributes, /*prefix_attributes=*/NULL_TREE,
> +                            attributes, prefix_attributes,
>                              &pushed_scope);
>
>           /* Parse the initializer.  */
> diff --git gcc/testsuite/g++.dg/cpp0x/gen-attrs-70.C gcc/testsuite/g++.dg/cpp0x/gen-attrs-70.C
> new file mode 100644
> index 00000000000..90a2e97a3f6
> --- /dev/null
> +++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-70.C
> @@ -0,0 +1,13 @@
> +// PR c++/91304 - prefix attributes ignored in condition.
> +// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-Wall -Wextra" }
> +
> +int f();
> +
> +void g()
> +{
> +  if ([[maybe_unused]] int i = f()) { }
> +  if ([[deprecated]] int i = f()) { i = 10; } // { dg-warning ".i. is deprecated" }
> +  if (int i [[maybe_unused]] = f()) { }
> +  if (int i [[deprecated]] = f()) { i = 10; } // { dg-warning ".i. is deprecated" }
> +}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-08-23  0:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-21 16:55 C++ PATCH for c++/91304 - prefix attributes ignored in condition Marek Polacek
2019-08-23  2:19 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).