[PATCH] D157191: Improve dumps of attributes

Giuliano Belinassi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 5 05:35:52 PDT 2023


giulianobelinassi created this revision.
Herald added subscribers: s.egerton, simoncook, asb.
Herald added a reviewer: aaron.ballman.
Herald added a project: All.
giulianobelinassi requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, wangpc, jplehr, sstefan1.
Herald added a project: clang.

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 at suse.de>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157191

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/CMakeLists.txt
  clang/lib/AST/DeclPrinter.cpp
  clang/test/AST/ast-print-attr-knr.c
  clang/test/AST/ast-print-attr.c
  clang/test/AST/ast-print-pragmas.cpp
  clang/test/Analysis/blocks.mm
  clang/test/OpenMP/assumes_codegen.cpp
  clang/test/OpenMP/assumes_print.cpp
  clang/test/OpenMP/assumes_template_print.cpp
  clang/test/OpenMP/declare_simd_ast_print.cpp
  clang/test/Sema/attr-print.c
  clang/test/SemaCXX/attr-print.cpp
  clang/test/SemaCXX/cxx11-attr-print.cpp
  clang/utils/TableGen/ClangAttrEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157191.547476.patch
Type: text/x-patch
Size: 30847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230805/579bb612/attachment-0001.bin>


More information about the cfe-commits mailing list