diff options
author | Nathan Sidwell <nathan@acm.org> | 2020-03-18 05:16:28 -0700 |
---|---|---|
committer | Nathan Sidwell <nathan@acm.org> | 2020-03-18 05:16:28 -0700 |
commit | 11cf25c40e3f586d19474108c78a2dfad7925902 (patch) | |
tree | 26063674dc76d9ded651520bc6f5b2b16139a3f8 /gcc/testsuite/g++.dg | |
parent | 1ba9acb11e3589b96ed945ed2a3af6acd6377018 (diff) | |
download | gcc-11cf25c40e3f586d19474108c78a2dfad7925902.zip gcc-11cf25c40e3f586d19474108c78a2dfad7925902.tar.gz gcc-11cf25c40e3f586d19474108c78a2dfad7925902.tar.bz2 |
PR c++/94147 - mangling of lambdas assigned to globals
This patch implements Jason's suggestion of pushing a lambda scope
when parsing a global variable initializer. That bit worked fine, but
happened to cause g++.dg/opt/dump1.C to not give any
used-but-not-defined warnings.
The reason was no_linkage_check, which considers any lambda that has
an extra-scope to have linkage. Which is technically correct. Except
that we think that all types that have linkage have external linkage.
Our representation of linkage and visibility is somewhat inaccurate,
particularly when it comes to types. We have TREE_PUBLIC,
DECL_EXTERNAL, DECL_VISIBILITY, DECL_COMDAT, DECL_NOT_REALLY_EXTERN.
It could really do with a through cleanup, but that won't be a simple
task.
The best I could come up with was seeing if the extra scope was a
VAR_DECL, and if that was TREE_PUBLIC and the var was inline (its
COMDATness is sadly not set at that point) or a template
instantiation, then the lambda had linkage. Otherwise it's as-if it
has no-linkage from the POV of compiler internals.
This is an ABI change (so we should document it), but it's changing
mangling from an unpredictable (in practice) counter, to something the
ABI defines. So I'm not concerned about mangling-changed warnings, or
preserving the broken mangling under some ABI selection flag. Code
that did this worked by accident within a single TU. It'll continue
to work by design there, and across TUs.
* parser.c (cp_parser_init_declarator): Namespace-scope variables
provide a lambda scope.
* tree.c (no_linkage_check): Lambdas with a variable for extra
scope have a linkage from the variable.
Diffstat (limited to 'gcc/testsuite/g++.dg')
-rw-r--r-- | gcc/testsuite/g++.dg/abi/lambda-vis.C | 23 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/abi/mangle74.C | 30 |
2 files changed, 53 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/abi/lambda-vis.C b/gcc/testsuite/g++.dg/abi/lambda-vis.C new file mode 100644 index 0000000..c3eb157 --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/lambda-vis.C @@ -0,0 +1,23 @@ +// { dg-do compile { target c++17 } } +// { dg-options "-fno-inline" } + +template<typename T> int sfoo (T); // { dg-warning "used but never defined" } +template<typename T> int gfoo (T); // { dg-warning "used but never defined" } +template<typename T> int ifoo (T); // OK +template<typename T> struct Wrapper {}; +template<typename T> Wrapper<T> capture (T &&) {return Wrapper<T> ();} + +static int svar = sfoo (capture ([]{})); + +int gvar = gfoo (capture ([]{})); + +inline int ivar = ifoo (capture ([]{})); + +// { dg-final { scan-assembler {_Z7captureINL4svarMUlvE_EE7WrapperIT_EOS2_:} } } +// { dg-final { scan-assembler {_Z7captureIN4gvarMUlvE_EE7WrapperIT_EOS2_:} } } +// { dg-final { scan-assembler {_Z7captureIN4ivarMUlvE_EE7WrapperIT_EOS2_:} } } + +// Calls to the foos are emitted. +// { dg-final { scan-assembler {call[ \t]*_Z4sfooI7WrapperINL4svarMUlvE_EEEiT_} { target { i?86-*-* x86_64-*-* } } } } +// { dg-final { scan-assembler {call[ \t]*_Z4gfooI7WrapperIN4gvarMUlvE_EEEiT_} { target { i?86-*-* x86_64-*-* } } } } +// { dg-final { scan-assembler {call[ \t]*_Z4ifooI7WrapperIN4ivarMUlvE_EEEiT_} { target { i?86-*-* x86_64-*-* } } } } diff --git a/gcc/testsuite/g++.dg/abi/mangle74.C b/gcc/testsuite/g++.dg/abi/mangle74.C new file mode 100644 index 0000000..4e1c632 --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/mangle74.C @@ -0,0 +1,30 @@ +// { dg-do compile { target c++17 } } +// { dg-options "-fno-inline -O0" } + +inline auto var = [] () {return 2;}; + +int bob () +{ +return var (); +} + +struct Foo +{ + static inline auto bar = [] () {return 4;}; +}; + +int bill () +{ + return Foo::bar (); +} + +// this one should have internal linkage (from svar) +static auto svar = [] () {return 8;}; +int thorn () +{ + return svar (); +} + +// { dg-final { scan-assembler "_ZNK3varMUlvE_clEv:" } } +// { dg-final { scan-assembler "_ZNK3Foo3barMUlvE_clEv:" { xfail *-*-* } } } +// { dg-final { scan-assembler-not "_ZNK3FooUlvE_clEv:" { xfail *-*-* } } } |