Clang happily ignoring an attribute

Hello everyone,

currently I’m checking if some functions are inlined with clang or not. Because of that I have a lot of unused variables storing return values - but clang optimizes them away including the function. Even “volatile” didn’t stopped Clang from doing so. So I tried using the GCC attribute “used”. But clang just responses:

1>main.cpp(9,23): warning : ‘used’ attribute ignored [-Wignored-attributes]
1> void* attribute((used)) adr = _ReturnAddress();

Why is clang doing this? And how can I prevent him easily from optimizing my values without referencing them?

Kind greetings
Björn
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789
Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika

Hello everyone,

currently I'm checking if some functions are inlined with clang or not. Because of that I have a lot of unused variables storing return values - but clang optimizes them away including the function. Even "volatile" didn't stopped Clang from doing so. So I tried using the GCC attribute "used". But clang just responses:
1>main.cpp(9,23): warning : 'used' attribute ignored [-Wignored-attributes]
1> void* __attribute__((used)) adr = _ReturnAddress();

Why is clang doing this? And how can I prevent him easily from optimizing my values without referencing them?

It's hard to say for certain without a bit more information, but my
guess is that your variable does not have static storage duration. For
instance, the used attribute does not apply to local variables unless
they're also declared static.

HTH!

~Aaron

On Wed, Apr 18, 2018 at 2:00 AM, via cfe-dev <cfe-dev@lists.llvm.org> wrote:
>
> Hello everyone,
>
> currently I'm checking if some functions are inlined with clang or not. Because of that I have a lot of unused variables storing return values - but clang optimizes them away including the function. Even "volatile" didn't stopped Clang from doing so. So I tried using the GCC attribute "used". But clang just responses:
> 1>main.cpp(9,23): warning : 'used' attribute ignored [-Wignored-attributes]
> 1> void* __attribute__((used)) adr = _ReturnAddress();
>
>
> Why is clang doing this? And how can I prevent him easily from optimizing my values without referencing them?

It's hard to say for certain without a bit more information, but my
guess is that your variable does not have static storage duration. For
instance, the used attribute does not apply to local variables unless
they're also declared static.

HTH!

~Aaron

Hey Aaron,

what is HTH? :0

Ohhh! Thank you! This was the problem. I forgot to add "static". Sorry!

Kind greetings,
Björn

Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789
Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika

Hello everyone,

currently I'm checking if some functions are inlined with clang or not.
Because of that I have a lot of unused variables storing return values - but
clang optimizes them away including the function. Even "volatile" didn't
stopped Clang from doing so. So I tried using the GCC attribute "used". But
clang just responses:
1>main.cpp(9,23): warning : 'used' attribute ignored
[-Wignored-attributes]
1> void* __attribute__((used)) adr = _ReturnAddress();

Why is clang doing this? And how can I prevent him easily from optimizing
my values without referencing them?

It's hard to say for certain without a bit more information, but my
guess is that your variable does not have static storage duration. For
instance, the used attribute does not apply to local variables unless
they're also declared static.

HTH!

~Aaron

Hey Aaron,

what is HTH? :0

"Hope This Helps" :slight_smile:

Ohhh! Thank you! This was the problem. I forgot to add "static". Sorry!

Glad to help!

~Aaron