[clang] Add SuppressKeywordAttrs printing policy (PR #175398)
Kim Gräsman via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 12 10:51:02 PST 2026
https://github.com/kimgr updated https://github.com/llvm/llvm-project/pull/175398
>From 4d02426a8f6b3f9c7c521420f37885277f18aee6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kim=20Gr=C3=A4sman?= <kim.grasman at gmail.com>
Date: Fri, 2 Jan 2026 23:59:52 +0100
Subject: [PATCH] Add SuppressDeclAttributes printing policy
This policy causes DeclPrinter to skip attributes entirely when printing
attribute lists, for brevity.
Removing attributes from a printed Decl post-facto is very hard (tm),
especially in the presence of more complex declaration syntax, such as
template requires-expressions, alignas-expressions, default values, etc.
---
clang/include/clang/AST/PrettyPrinter.h | 7 ++++++-
clang/lib/AST/DeclPrinter.cpp | 2 +-
clang/unittests/AST/DeclPrinterTest.cpp | 16 ++++++++++++++++
3 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h
index 2ede8da209748..1564204833e64 100644
--- a/clang/include/clang/AST/PrettyPrinter.h
+++ b/clang/include/clang/AST/PrettyPrinter.h
@@ -82,7 +82,8 @@ struct PrintingPolicy {
PrintAsCanonical(false), PrintInjectedClassNameWithArguments(true),
UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
CleanUglifiedParameters(false), EntireContentsOfLargeArray(true),
- UseEnumerators(true), UseHLSLTypes(LO.HLSL) {}
+ UseEnumerators(true), UseHLSLTypes(LO.HLSL),
+ SuppressDeclAttributes(false) {}
/// Adjust this printing policy for cases where it's known that we're
/// printing C++ code (for instance, if AST dumping reaches a C++-only
@@ -358,6 +359,10 @@ struct PrintingPolicy {
LLVM_PREFERRED_TYPE(bool)
unsigned UseHLSLTypes : 1;
+ /// Whether to suppress attributes in decl printing.
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned SuppressDeclAttributes : 1;
+
/// Callbacks to use to allow the behavior of printing to be customized.
const PrintingCallbacks *Callbacks = nullptr;
};
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 205b6a53f80e5..722451522a265 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -257,7 +257,7 @@ static DeclPrinter::AttrPosAsWritten getPosAsWritten(const Attr *A,
std::optional<std::string>
DeclPrinter::prettyPrintAttributes(const Decl *D,
AttrPosAsWritten Pos /*=Default*/) {
- if (!D->hasAttrs())
+ if (Policy.SuppressDeclAttributes || !D->hasAttrs())
return std::nullopt;
std::string AttrStr;
diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp
index a412a9813b470..4368641850c20 100644
--- a/clang/unittests/AST/DeclPrinterTest.cpp
+++ b/clang/unittests/AST/DeclPrinterTest.cpp
@@ -1572,3 +1572,19 @@ TEST(DeclPrinter, TestTemplateFinalWithPolishForDecl) {
"template <typename T> class FinalTemplate final {}",
[](PrintingPolicy &Policy) { Policy.PolishForDeclaration = true; }));
}
+
+TEST(DeclPrinter, TestClassSuppressDeclAttributes) {
+ ASSERT_TRUE(PrintedDeclCXX11Matches(
+ "class alignas(32) [[deprecated]] A final {};",
+ cxxRecordDecl(hasName("A")).bind("id"), "class A {}",
+ [](PrintingPolicy &Policy) { Policy.SuppressDeclAttributes = true; }));
+}
+
+TEST(DeclPrinter, TestTemplateSuppressDeclAttributes) {
+ ASSERT_TRUE(PrintedDeclCXX11Matches(
+ "template<typename T>\n"
+ "class alignas(32) [[deprecated]] A final {};",
+ classTemplateDecl(hasName("A")).bind("id"),
+ "template <typename T> class A {}",
+ [](PrintingPolicy &Policy) { Policy.SuppressDeclAttributes = true; }));
+}
More information about the cfe-commits
mailing list