On 2/16/21 1:52 PM, Jeff Law wrote:
On 2/11/21 10:18 PM, Jason Merrill via Gcc-patches wrote:
The patch for PR92372 made us start warning about a flatten attribute on an
alias. But in the case of C++ 'tor base/complete variants, the user didn't
create the alias, so we shouldn't warn.
I could also remove the attribute in maybe_clone_body, but here seems a bit
better.
Tested x86_64-pc-linux-gnu. OK for trunk?
gcc/ChangeLog:
PR c++/96078
* cgraph.c (cgraph_node::create_same_body_alias): Remove flatten
attribute from alias.
gcc/testsuite/ChangeLog:
PR c++/96078
* g++.dg/ext/attr-flatten1.C: New test.
But shouldn't we validate that we've got a C++ ctor/dtor rather than
blindly removing the attribute from all aliases? ISTM like the patch
as-is would always suppress warnings when we create a same-body alias
regardless of why we created a same-body alias.
Fair enough. How about this approach, instead? If the target also has
attribute flatten, we're getting the desired effect even though the
called symbol is an alias.
>From 740414f268382625525a892fd14357f694ca4391 Mon Sep 17 00:00:00 2001
From: Jason Merrill <ja...@redhat.com>
Date: Thu, 11 Feb 2021 22:01:19 -0500
Subject: [PATCH] cgraph: flatten and same_body aliases [PR96078]
To: gcc-patches@gcc.gnu.org
The patch for PR92372 made us start warning about a flatten attribute on an
alias. But in the case of C++ 'tor base/complete variants, the user didn't
create the alias. If the alias target also has the attribute, the alias
points to a flattened function, so we shouldn't warn.
gcc/ChangeLog:
PR c++/96078
* cgraphunit.c (process_function_and_variable_attributes): Don't
warn about flatten on an alias if the target also has it.
* cgraph.h (symtab_node::get_alias_target_tree): New.
gcc/testsuite/ChangeLog:
PR c++/96078
* g++.dg/ext/attr-flatten1.C: New test.
---
gcc/cgraph.h | 14 ++++++++++++++
gcc/cgraphunit.c | 7 +++++--
gcc/testsuite/g++.dg/ext/attr-flatten1.C | 9 +++++++++
3 files changed, 28 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/attr-flatten1.C
diff --git a/gcc/cgraph.h b/gcc/cgraph.h
index 47b5c02d770..4a1f89920f5 100644
--- a/gcc/cgraph.h
+++ b/gcc/cgraph.h
@@ -319,6 +319,9 @@ public:
/* Return node that alias is aliasing. */
inline symtab_node *get_alias_target (void);
+ /* Return DECL that alias is aliasing. */
+ inline tree get_alias_target_tree ();
+
/* Set section for symbol and its aliases. */
void set_section (const char *section);
@@ -2665,6 +2668,17 @@ symtab_node::get_alias_target (void)
return ref->referred;
}
+/* Return the DECL (or identifier) that alias is aliasing. Unlike the above,
+ this works whether or not the alias has been analyzed already. */
+
+inline tree
+symtab_node::get_alias_target_tree ()
+{
+ if (alias_target)
+ return alias_target;
+ return get_alias_target ()->decl;
+}
+
/* Return next reachable static symbol with initializer after the node. */
inline symtab_node *
diff --git a/gcc/cgraphunit.c b/gcc/cgraphunit.c
index b401f0817a3..1c74cee69ac 100644
--- a/gcc/cgraphunit.c
+++ b/gcc/cgraphunit.c
@@ -859,8 +859,11 @@ process_function_and_variable_attributes (cgraph_node *first,
if (node->alias
&& lookup_attribute ("flatten", DECL_ATTRIBUTES (decl)))
{
- warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
- "%<flatten%> attribute is ignored on aliases");
+ tree tdecl = node->get_alias_target_tree ();
+ if (!tdecl || !DECL_P (tdecl)
+ || !lookup_attribute ("flatten", DECL_ATTRIBUTES (tdecl)))
+ warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
+ "%<flatten%> attribute is ignored on aliases");
}
if (DECL_PRESERVE_P (decl))
node->mark_force_output ();
diff --git a/gcc/testsuite/g++.dg/ext/attr-flatten1.C b/gcc/testsuite/g++.dg/ext/attr-flatten1.C
new file mode 100644
index 00000000000..5bcbfb6f4aa
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/attr-flatten1.C
@@ -0,0 +1,9 @@
+// PR c++/96078
+// { dg-do compile { target c++11 } }
+
+struct A {
+ [[gnu::flatten]] A() {}
+ [[gnu::flatten]] ~A() {}
+};
+
+A a;
--
2.27.0