[clang] [clang][AST] Omit optional string args in attribute printers (PR #239378)

Max Winkler via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 06:03:33 PST 2026


https://github.com/max-winkler updated https://github.com/llvm/llvm-project/pull/239378

>From 5edb0492227106f30a9119af9bf569c2e0c0d0da Mon Sep 17 00:00:00 2001
From: Max Winkler <max.winkler at mathematik.tu-chemnitz.de>
Date: Thu, 5 Nov 2026 21:02:30 +0100
Subject: [PATCH 1/2] [clang][AST] Omit optional string args in attribute
 printers

The attribute pretty printers (Attr::printPretty and the per-attribute
printPretty methods) are generated by ClangAttrEmitter.cpp. Optional
arguments are only skipped if the corresponding Argument subclass
implements getIsOmitted(), which StringArgument did not. Optional string
arguments were therefore always printed, e.g. [[nodiscard("")]] or
__attribute__((unavailable(""))) for attributes written without a message.

Implement getIsOmitted() for StringArgument so that an empty optional
string is omitted, together with the surrounding parentheses.

deprecated and availability are printed by dedicated code in
ClangAttrEmitter.cpp and are not affected.

Updated two existing lit tests to match the new printer output. Add a
new lit test coverering optional string arguments (nodiscard,
unavailable and the unchanged deprecated).

Assisted-by: Claude
---
 clang/test/AST/attr-print-optional-arg.cpp | 31 ++++++++++++++++++++++
 clang/test/Sema/ast-print.c                |  2 +-
 clang/test/SemaCXX/cxx11-attr-print.cpp    |  8 +++---
 clang/utils/TableGen/ClangAttrEmitter.cpp  |  4 +++
 4 files changed, 39 insertions(+), 6 deletions(-)
 create mode 100644 clang/test/AST/attr-print-optional-arg.cpp

diff --git a/clang/test/AST/attr-print-optional-arg.cpp b/clang/test/AST/attr-print-optional-arg.cpp
new file mode 100644
index 00000000000000..1ae6696beef890
--- /dev/null
+++ b/clang/test/AST/attr-print-optional-arg.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -std=c++20 %s -ast-print | FileCheck %s
+// RUN: %clang_cc1 -std=c++20 -emit-pch -o %t.ast %s
+// RUN: %clang_cc1 -std=c++20 %t.ast -ast-print | FileCheck %s
+
+// Check that optional attribute arguments which were not written are omitted
+// when printing, instead of being printed with an empty/default value.
+ 
+// CHECK: {{\[\[}}nodiscard]] int a();
+[[nodiscard]] int a();
+
+// CHECK: {{\[\[}}nodiscard("no")]] int b();
+[[nodiscard("no")]] int b();
+
+// CHECK: {{\[\[}}nodiscard]] int c();
+[[nodiscard("")]] int c();
+
+// CHECK: __attribute__((unavailable)) int d();
+__attribute__((unavailable)) int d();
+
+// CHECK: __attribute__((unavailable("gone"))) int e();
+__attribute__((unavailable("gone"))) int e();
+
+// FIXME: [[deprecated]] should print without the empty message, but
+// deprecated is special-cased in ClangAttrEmitter.cpp
+// (writeDeprecatedAttrValue), which always prints the message.
+// CHECK: {{\[\[}}deprecated("")]] int f();
+[[deprecated]] int f();
+
+// CHECK: struct {{\[\[}}nodiscard]] S {
+// CHECK-NEXT: };
+struct [[nodiscard]] S {};
diff --git a/clang/test/Sema/ast-print.c b/clang/test/Sema/ast-print.c
index 2ba5ca34b51347..2db4c87292a633 100644
--- a/clang/test/Sema/ast-print.c
+++ b/clang/test/Sema/ast-print.c
@@ -83,7 +83,7 @@ void initializers(void) {
 enum EnumWithAttributes { // expected-warning {{'EnumWithAttributes' is deprecated}}
   // CHECK-NEXT: EnumWithAttributesFoo __attribute__((deprecated(""))),
   EnumWithAttributesFoo __attribute__((deprecated)),
-  // CHECK-NEXT: EnumWithAttributesBar __attribute__((unavailable(""))) = 50
+  // CHECK-NEXT: EnumWithAttributesBar __attribute__((unavailable)) = 50
   EnumWithAttributesBar __attribute__((unavailable)) = 50
   // CHECK-NEXT: } *EnumWithAttributesPtr;
 } __attribute__((deprecated)) *EnumWithAttributesPtr; // expected-note {{'EnumWithAttributes' has been explicitly marked deprecated here}}
diff --git a/clang/test/SemaCXX/cxx11-attr-print.cpp b/clang/test/SemaCXX/cxx11-attr-print.cpp
index 70274bd5aa72ab..eff8c7192ec7c5 100644
--- a/clang/test/SemaCXX/cxx11-attr-print.cpp
+++ b/clang/test/SemaCXX/cxx11-attr-print.cpp
@@ -36,15 +36,13 @@ int foo() __attribute__((const));
 // CHECK: int bar() __attribute__((__const));
 int bar() __attribute__((__const));
 
-// FIXME: It's unfortunate that the string literal prints with the below three
-// cases given that the string is only exposed via the [[nodiscard]] spelling.
-// CHECK: int f1() __attribute__((warn_unused_result("")));
+// CHECK: int f1() __attribute__((warn_unused_result));
 int f1() __attribute__((warn_unused_result));
 
-// CHECK: {{\[}}[clang::warn_unused_result("")]];
+// CHECK: {{\[}}[clang::warn_unused_result]];
 int f2 [[clang::warn_unused_result]] ();
 
-// CHECK: {{\[}}[gnu::warn_unused_result("")]];
+// CHECK: {{\[}}[gnu::warn_unused_result]];
 int f3 [[gnu::warn_unused_result]] ();
 
 // FIXME: ast-print need to print C++11
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 060c7a403f2b17..8d5fa7eec0e6dd 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -442,6 +442,10 @@ namespace {
       : Argument(Arg, Attr)
     {}
 
+    std::string getIsOmitted() const override {
+      return "get" + getUpperName().str() + "().empty()";
+    }
+
     void writeAccessors(raw_ostream &OS) const override {
       OS << "  llvm::StringRef get" << getUpperName() << "() const {\n";
       OS << "    return llvm::StringRef(" << getLowerName() << ", "

>From a2200d0fef8a75cdf03c165a7462b3f1f8e336ae Mon Sep 17 00:00:00 2001
From: Max Winkler <max.winkler at mathematik.tu-chemnitz.de>
Date: Sat, 7 Nov 2026 15:01:59 +0100
Subject: [PATCH 2/2] [clang][AST] Don't print empty arguments of the
 deprecated attribute

The pretty printer for `deprecated` is written by hand in
ClangAttrEmitter.cpp and always printed the message, so
`[[deprecated]]` and `__attribute__((deprecated))` were printed as
`[[deprecated("")]]` and `__attribute__((deprecated("")))`. Omit the
argument list if there is nothing to print.

The GNU spelling has an optional second argument, the replacement.
It is positional, so an empty message is still printed if there is
a replacement: `__attribute__((deprecated("", "repl")))`. The other
spellings only have the message.

Several tests spelled `deprecated("")` only to match the previous
output. They now spell `deprecated`.
---
 clang/test/AST/ast-print-enum-decl.c       | 14 ++---
 clang/test/AST/ast-print-record-decl.c     | 20 +++---
 clang/test/AST/attr-print-optional-arg.cpp | 73 ++++++++++++++++------
 clang/test/Sema/ast-print.c                |  8 +--
 clang/utils/TableGen/ClangAttrEmitter.cpp  | 21 ++++---
 5 files changed, 89 insertions(+), 47 deletions(-)

diff --git a/clang/test/AST/ast-print-enum-decl.c b/clang/test/AST/ast-print-enum-decl.c
index f88af682a00f58..4261647a5aacd0 100644
--- a/clang/test/AST/ast-print-enum-decl.c
+++ b/clang/test/AST/ast-print-enum-decl.c
@@ -17,14 +17,14 @@
 void defFirst(void) {
   // PRINT-NEXT: enum
   // PRINT-DAG:  __attribute__((aligned(16)))
-  // PRINT-DAG:  __attribute__((deprecated("")))
+  // PRINT-DAG:  __attribute__((deprecated))
   // PRINT-SAME: T {
   // PRINT-NEXT:   E0,
   // PRINT-NEXT:   E1
   // PRINT-NEXT: } *p0;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 2 {{'T' has been explicitly marked deprecated here}}
-  enum __attribute__((aligned(16))) __attribute__((deprecated(""))) T {
+  enum __attribute__((aligned(16))) __attribute__((deprecated)) T {
     E0, E1
   } *p0;
 
@@ -37,21 +37,21 @@ void defLast(void) {
   // PRINT-NEXT: enum __attribute__((aligned(16))) T *p0;
   enum __attribute__((aligned(16))) T *p0;
 
-  // PRINT-NEXT: enum __attribute__((deprecated(""))) T {
+  // PRINT-NEXT: enum __attribute__((deprecated)) T {
   // PRINT-NEXT:   E0,
   // PRINT-NEXT:   E1
   // PRINT-NEXT: } *p1;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 {{'T' has been explicitly marked deprecated here}}
-  enum __attribute__((deprecated(""))) T { E0, E1 } *p1;
+  enum __attribute__((deprecated)) T { E0, E1 } *p1;
 }
 
 // CHECK-LABEL: defMiddle
 void defMiddle(void) {
-  // PRINT-NEXT: enum __attribute__((deprecated(""))) T *p0;
+  // PRINT-NEXT: enum __attribute__((deprecated)) T *p0;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 3 {{'T' has been explicitly marked deprecated here}}
-  enum __attribute__((deprecated(""))) T *p0;
+  enum __attribute__((deprecated)) T *p0;
 
   // PRINT-NEXT: enum __attribute__((aligned(16))) T {
   // PRINT-NEXT:   E0
@@ -78,7 +78,7 @@ void declsOnly(void) {
   enum T *p2;
 
   // PRINT-NEXT: enum T *p3;
-  enum __attribute__((deprecated(""))) T *p3;
+  enum __attribute__((deprecated)) T *p3;
 
   // PRINT-NEXT: enum T *p4;
   enum T *p4;
diff --git a/clang/test/AST/ast-print-record-decl.c b/clang/test/AST/ast-print-record-decl.c
index 394f837c3645d5..e3596a714836ed 100644
--- a/clang/test/AST/ast-print-record-decl.c
+++ b/clang/test/AST/ast-print-record-decl.c
@@ -103,14 +103,14 @@ struct B {};
 void defFirst(void) {
   // PRINT-NEXT: [[KW]]
   // PRINT-DAG:  __attribute__((aligned(16)))
-  // PRINT-DAG:  __attribute__((deprecated("")))
+  // PRINT-DAG:  __attribute__((deprecated))
   // PRINT-NOT:  __attribute__
   // PRINT-SAME: T[[BASES]] {
   // PRINT-NEXT:   int i;
   // PRINT-NEXT: } *p0;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 2 {{'T' has been explicitly marked deprecated here}}
-  KW __attribute__((aligned(16))) __attribute__((deprecated(""))) T BASES {
+  KW __attribute__((aligned(16))) __attribute__((deprecated)) T BASES {
     int i;
   } *p0;
 
@@ -128,12 +128,12 @@ void defLast(void) {
   // PRINT-NEXT: [[KW]] __attribute__((aligned(16))) T *p0;
   KW __attribute__((aligned(16))) T *p0;
 
-  // PRINT-NEXT: [[KW]] __attribute__((deprecated(""))) T[[BASES]] {
+  // PRINT-NEXT: [[KW]] __attribute__((deprecated)) T[[BASES]] {
   // PRINT-NEXT:   int i;
   // PRINT-NEXT: } *p1;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 {{'T' has been explicitly marked deprecated here}}
-  KW __attribute__((deprecated(""))) T BASES { int i; } *p1;
+  KW __attribute__((deprecated)) T BASES { int i; } *p1;
 
   // LLVM: store i64 16
   long s0 = sizeof *p0;
@@ -143,10 +143,10 @@ void defLast(void) {
 
 // CHECK-LABEL: defMiddle
 void defMiddle(void) {
-  // PRINT-NEXT: [[KW]] __attribute__((deprecated(""))) T *p0;
+  // PRINT-NEXT: [[KW]] __attribute__((deprecated)) T *p0;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 3 {{'T' has been explicitly marked deprecated here}}
-  KW __attribute__((deprecated(""))) T *p0;
+  KW __attribute__((deprecated)) T *p0;
 
   // PRINT-NEXT: [[KW]] __attribute__((aligned(16))) T[[BASES]] {
   // PRINT-NEXT:   int i;
@@ -166,10 +166,10 @@ void defMiddle(void) {
 
 // CHECK-LABEL: defSelfRef
 void defSelfRef(void) {
-  // PRINT-NEXT: [[KW]] __attribute__((deprecated(""))) T *p0;
+  // PRINT-NEXT: [[KW]] __attribute__((deprecated)) T *p0;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 2 {{'T' has been explicitly marked deprecated here}}
-  KW __attribute__((deprecated(""))) T *p0;
+  KW __attribute__((deprecated)) T *p0;
 
   // PRINT-NEXT:  [[KW]] __attribute__((aligned(64))) T[[BASES]] {
   // PRINT-NEXT:    int i;
@@ -211,10 +211,10 @@ void declsOnly(void) {
   // PRINT-NEXT: [[KW]] T *p2;
   KW T *p2;
 
-  // PRINT-NEXT: [[KW]] __attribute__((deprecated(""))) T *p3;
+  // PRINT-NEXT: [[KW]] __attribute__((deprecated)) T *p3;
   // expected-warning at +2 {{'T' is deprecated}}
   // expected-note at +1 2 {{'T' has been explicitly marked deprecated here}}
-  KW __attribute__((deprecated(""))) T *p3;
+  KW __attribute__((deprecated)) T *p3;
 
   // PRINT-NEXT: [[KW]] T *p4;
   KW T *p4; // expected-warning {{'T' is deprecated}}
diff --git a/clang/test/AST/attr-print-optional-arg.cpp b/clang/test/AST/attr-print-optional-arg.cpp
index 1ae6696beef890..541cc4d8724748 100644
--- a/clang/test/AST/attr-print-optional-arg.cpp
+++ b/clang/test/AST/attr-print-optional-arg.cpp
@@ -1,30 +1,65 @@
-// RUN: %clang_cc1 -std=c++20 %s -ast-print | FileCheck %s
-// RUN: %clang_cc1 -std=c++20 -emit-pch -o %t.ast %s
-// RUN: %clang_cc1 -std=c++20 %t.ast -ast-print | FileCheck %s
+// RUN: %clang_cc1 -std=c++20 -fdeclspec %s -ast-print | FileCheck %s
+// RUN: %clang_cc1 -std=c++20 -fdeclspec -emit-pch -o %t.ast %s
+// RUN: %clang_cc1 -std=c++20 -fdeclspec %t.ast -ast-print | FileCheck %s
 
 // Check that optional attribute arguments which were not written are omitted
 // when printing, instead of being printed with an empty/default value.
- 
-// CHECK: {{\[\[}}nodiscard]] int a();
-[[nodiscard]] int a();
 
-// CHECK: {{\[\[}}nodiscard("no")]] int b();
-[[nodiscard("no")]] int b();
+// CHECK: {{\[\[}}nodiscard]] int f1();
+[[nodiscard]] int f1();
 
-// CHECK: {{\[\[}}nodiscard]] int c();
-[[nodiscard("")]] int c();
+// CHECK: {{\[\[}}nodiscard("no")]] int f2();
+[[nodiscard("no")]] int f2();
 
-// CHECK: __attribute__((unavailable)) int d();
-__attribute__((unavailable)) int d();
+// CHECK: {{\[\[}}nodiscard]] int f3();
+[[nodiscard("")]] int f3();
 
-// CHECK: __attribute__((unavailable("gone"))) int e();
-__attribute__((unavailable("gone"))) int e();
+// CHECK: __attribute__((unavailable)) int f4();
+__attribute__((unavailable)) int f4();
 
-// FIXME: [[deprecated]] should print without the empty message, but
-// deprecated is special-cased in ClangAttrEmitter.cpp
-// (writeDeprecatedAttrValue), which always prints the message.
-// CHECK: {{\[\[}}deprecated("")]] int f();
-[[deprecated]] int f();
+// CHECK: __attribute__((unavailable("gone"))) int f5();
+__attribute__((unavailable("gone"))) int f5();
+
+// CHECK: {{\[\[}}deprecated]] int f6();
+[[deprecated]] int f6();
+
+// CHECK: {{\[\[}}deprecated("msg")]] int f7();
+[[deprecated("msg")]] int f7();
+
+// An explicitly written empty message is not distinguishable from an omitted
+// one, so it is omitted as well.
+// CHECK: {{\[\[}}deprecated]] int f8();
+[[deprecated("")]] int f8();
+
+// CHECK: __attribute__((deprecated)) int f9();
+__attribute__((deprecated)) int f9();
+
+// CHECK: __attribute__((deprecated)) int f10();
+__attribute__((deprecated(""))) int f10();
+
+// CHECK: __attribute__((deprecated("msg"))) int f11();
+__attribute__((deprecated("msg"))) int f11();
+
+// The replacement is positional, so an empty message must be kept if there is
+// a replacement.
+// CHECK: __attribute__((deprecated("", "repl"))) int f12();
+__attribute__((deprecated("", "repl"))) int f12();
+
+// CHECK: {{\[\[}}gnu::deprecated]] int f13();
+[[gnu::deprecated]] int f13();
+
+// CHECK: {{\[\[}}gnu::deprecated("msg")]] int f14();
+[[gnu::deprecated("msg")]] int f14();
+
+// CHECK: __declspec(deprecated) int f15();
+__declspec(deprecated) int f15();
+
+// CHECK: __declspec(deprecated("msg")) int f16();
+__declspec(deprecated("msg")) int f16();
+
+// A non-optional string argument is always printed, even if it is empty.
+// CHECK: __attribute__((annotate(""))) int f17();
+__attribute__((annotate(""))) int f17();
 
 // CHECK: struct {{\[\[}}nodiscard]] S {
 // CHECK-NEXT: };
diff --git a/clang/test/Sema/ast-print.c b/clang/test/Sema/ast-print.c
index 2db4c87292a633..851133cf71e93d 100644
--- a/clang/test/Sema/ast-print.c
+++ b/clang/test/Sema/ast-print.c
@@ -79,23 +79,23 @@ void initializers(void) {
   } z = {(struct Z){}};
 }
 
-// CHECK-LABEL: enum __attribute__((deprecated(""))) EnumWithAttributes {
+// CHECK-LABEL: enum __attribute__((deprecated)) EnumWithAttributes {
 enum EnumWithAttributes { // expected-warning {{'EnumWithAttributes' is deprecated}}
-  // CHECK-NEXT: EnumWithAttributesFoo __attribute__((deprecated(""))),
+  // CHECK-NEXT: EnumWithAttributesFoo __attribute__((deprecated)),
   EnumWithAttributesFoo __attribute__((deprecated)),
   // CHECK-NEXT: EnumWithAttributesBar __attribute__((unavailable)) = 50
   EnumWithAttributesBar __attribute__((unavailable)) = 50
   // CHECK-NEXT: } *EnumWithAttributesPtr;
 } __attribute__((deprecated)) *EnumWithAttributesPtr; // expected-note {{'EnumWithAttributes' has been explicitly marked deprecated here}}
 
-// CHECK-LABEL: enum __attribute__((deprecated(""))) EnumWithAttributes2 *EnumWithAttributes2Ptr;
+// CHECK-LABEL: enum __attribute__((deprecated)) EnumWithAttributes2 *EnumWithAttributes2Ptr;
 // expected-warning at +2 {{'EnumWithAttributes2' is deprecated}}
 // expected-note at +1 {{'EnumWithAttributes2' has been explicitly marked deprecated here}}
 enum __attribute__((deprecated)) EnumWithAttributes2 *EnumWithAttributes2Ptr;
 
 // CHECK-LABEL: EnumWithAttributes3Fn
 void EnumWithAttributes3Fn(void) {
-  // CHECK-NEXT: enum __attribute__((deprecated(""))) EnumWithAttributes3 *EnumWithAttributes3Ptr;
+  // CHECK-NEXT: enum __attribute__((deprecated)) EnumWithAttributes3 *EnumWithAttributes3Ptr;
   // expected-warning at +2 {{'EnumWithAttributes3' is deprecated}}
   // expected-note at +1 {{'EnumWithAttributes3' has been explicitly marked deprecated here}}
   enum __attribute__((deprecated)) EnumWithAttributes3 *EnumWithAttributes3Ptr;
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 8d5fa7eec0e6dd..7377a220af5c1b 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -443,6 +443,8 @@ namespace {
     {}
 
     std::string getIsOmitted() const override {
+      // For optional arguments, an empty string is indistinguishable from an
+      // omitted one.
       return "get" + getUpperName().str() + "().empty()";
     }
 
@@ -1788,12 +1790,19 @@ static void writeAvailabilityValue(raw_ostream &OS) {
 }
 
 static void writeDeprecatedAttrValue(raw_ostream &OS, StringRef Variety) {
-  OS << "\\\"\" << getMessage() << \"\\\"\";\n";
   // Only GNU deprecated has an optional fixit argument at the second position.
-  if (Variety == "GNU")
-     OS << "    if (!getReplacement().empty()) OS << \", \\\"\""
-           " << getReplacement() << \"\\\"\";\n";
-  OS << "    OS << \"";
+  // The argument list is omitted if it would be empty.
+  if (Variety == "GNU") {
+    OS << "    if (!getMessage().empty() || !getReplacement().empty()) {\n";
+    OS << "      OS << \"(\\\"\" << getMessage() << \"\\\"\";\n";
+    OS << "      if (!getReplacement().empty())\n";
+    OS << "        OS << \", \\\"\" << getReplacement() << \"\\\"\";\n";
+    OS << "      OS << \")\";\n";
+    OS << "    }\n";
+  } else {
+    OS << "    if (!getMessage().empty())\n";
+    OS << "      OS << \"(\\\"\" << getMessage() << \"\\\")\";\n";
+  }
 }
 
 static void writeGetSpellingFunction(const Record &R, raw_ostream &OS) {
@@ -1905,9 +1914,7 @@ writePrettyPrintFunction(const Record &R,
       writeAvailabilityValue(OS);
       OS << ")\";\n";
     } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
-      OS << "    OS << \"(";
       writeDeprecatedAttrValue(OS, Variety);
-      OS << ")\";\n";
     } else {
       // To avoid printing parentheses around an empty argument list or
       // printing spurious commas at the end of an argument list, we need to



More information about the cfe-commits mailing list