[PATCH] D141714: Fix ast print of variables with attributes

Giuliano Belinassi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 13 11:24:27 PST 2023


giulianobelinassi created this revision.
Herald added a project: All.
giulianobelinassi requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Previously clang AST prints the following declaration:

int fun_var_unused() {

  int x __attribute__((unused)) = 0;
  return x;

}

as:

int fun_var_unused() {

  int x = 0 __attribute__((unused));
  return x;

}

which is rejected by C/C++ parser. This patch modifies the logic to
print as the first example.

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/D141714

Files:
  clang/lib/AST/DeclPrinter.cpp
  clang/test/AST/ast-print-attr.c
  clang/test/AST/ast-print-pragmas.cpp


Index: clang/test/AST/ast-print-pragmas.cpp
===================================================================
--- clang/test/AST/ast-print-pragmas.cpp
+++ clang/test/AST/ast-print-pragmas.cpp
@@ -92,8 +92,9 @@
 
 #ifdef MS_EXT
 #pragma init_seg(compiler)
+/* FIXME: Trying to compile the output source results in compilation error.  */
 // MS-EXT: #pragma init_seg (.CRT$XCC){{$}}
-// MS-EXT-NEXT: int x = 3 __declspec(thread);
+// MS-EXT-NEXT: int x __declspec(thread) = 3;
 int __declspec(thread) x = 3;
 #endif //MS_EXT
 
Index: clang/test/AST/ast-print-attr.c
===================================================================
--- clang/test/AST/ast-print-attr.c
+++ clang/test/AST/ast-print-attr.c
@@ -32,3 +32,9 @@
 
 // CHECK: void fun_holds(int *a) __attribute__((ownership_holds(fun_holds, 1)));
 void fun_holds(int *a) __attribute__((ownership_holds(fun_holds, 1)));
+
+// CHECK: int fun_var_unused() {
+// CHECK-NEXT: int x __attribute__((unused)) = 0;
+// CHECK-NEXT: return x;
+// CHECK-NEXT: }
+int fun_var_unused() { int x __attribute__((unused)) = 0; return x; }
Index: clang/lib/AST/DeclPrinter.cpp
===================================================================
--- clang/lib/AST/DeclPrinter.cpp
+++ clang/lib/AST/DeclPrinter.cpp
@@ -891,6 +891,7 @@
                     D->getIdentifier())
                        ? D->getIdentifier()->deuglifiedName()
                        : D->getName());
+  prettyPrintAttributes(D);
   Expr *Init = D->getInit();
   if (!Policy.SuppressInitializers && Init) {
     bool ImplicitInit = false;
@@ -919,7 +920,6 @@
         Out << ")";
     }
   }
-  prettyPrintAttributes(D);
 }
 
 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141714.489077.patch
Type: text/x-patch
Size: 1699 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230113/a7315843/attachment.bin>


More information about the cfe-commits mailing list