This is an archive of the discontinued LLVM Phabricator instance.

Improve dumps of attributes
Needs ReviewPublic

Authored by giulianobelinassi on Aug 5 2023, 5:35 AM.

Details

Summary

Clang has several cases where attribute dumpings gets into
undesired places, or even is completely incorrect.

Therefore, this commit improves many cases, as listed below:

1- Variable with attributes have its attribute dumped before its value.

input:

int var __attribute__((unused)) = 0;

output before this commit:

int var = 0 __attribute__((unused)); // Compilation error.

after this patch:

int var __attribute__((unused)) = 0;

2- __declspec attributes are dumped on the left side of the declaration,

as recommended by MSVC docs:

input:

__declspec(thread) int var = 0;

output before this commit:

int var __declspec(thread) = 0;

output after this commit:

__declspec(thread) int var = 0;

3- Functions with body has its attributes dumped on the right side of

the declaration instead of left side when possible.  The point of
this is to (1) avoid attribute placement confusion in K&R C
functions and (2) keep compatibility with GCC.

input

int f(void) __attribute__((unused)) {}

output before this commit:

int f(void) __attribute__((unused)) {}

output after this commit:

__attribute__((unused)) int f(void) {}

The interesting case is with input:

int f(i) int i __attribute__((unused)); {}

output before this commit (incorrect):

int f(i) __attribute__((unused)) int i; {}

output after this commit (correct)

int f(i) int i __attribute__((unused));

And in cases where the attribute is not accepted on the left side of
the declaration is output on the right side of the declaration:

intput:

int f(int i) __attribute__((diagnose_if(i < 0, "oh no", "warning"))) {}

output before and after this commit:

int f(int i) __attribute__((diagnose_if(i < 0, "oh no", "warning"))) {}

The reason behind thius is because GCC does not accept diagnose_if and
i must be declared in order to be referenced in this attribute.

Fixes: https://github.com/llvm/llvm-project/issues/59973

Signed-off-by: Giuliano Belinassi <gbelinassi@suse.de>

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald Transcript
giulianobelinassi requested review of this revision.Aug 5 2023, 5:35 AM
Herald added a project: Restricted Project. · View Herald Transcript
giulianobelinassi abandoned this revision.Aug 5 2023, 5:37 AM

This was open by mistake.

Fix warning in MSVC

Summary:
Currently there is no PrintOnLeft attribute set, which results in an
empty switch-case. When compiling this, MSVC issues a warning saying
that the switch-case is empty. Fix this by using a macro and checking
if this macro is defined or not.

Links to D141714

Reviewers: aaron.ballman, erichkeane

Differential Revision: https://reviews.llvm.org/D157191