https://github.com/kimgr created https://github.com/llvm/llvm-project/pull/175398
This policy causes DeclPrinter to skip keyword attributes when printing attribute lists, for brevity. Removing these attributes from a printed Decl post-facto is very hard (tm), especially in the presence of more complex syntax, such as template requires-expressions, alignas-expressions, default values, etc. From ce10170469bbe2dba7efa20bdd0876ff9f6ebb79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Gr=C3=A4sman?= <[email protected]> Date: Fri, 2 Jan 2026 23:59:52 +0100 Subject: [PATCH] Add SuppressKeywordAttrs printing policy This policy causes DeclPrinter to skip keyword attributes when printing attribute lists, for brevity. Removing these attributes from a printed Decl post-facto is very hard (tm), especially in the presence of more complex syntax, such as template requires-expressions, alignas-expressions, default values, etc. --- clang/include/clang/AST/PrettyPrinter.h | 7 ++++++- clang/lib/AST/DeclPrinter.cpp | 7 +++++-- clang/unittests/AST/DeclPrinterTest.cpp | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h index 2ede8da209748..a9125136bc4c3 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), + SuppressKeywordAttrs(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 keyword attributes in decl printing. + LLVM_PREFERRED_TYPE(bool) + unsigned SuppressKeywordAttrs : 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..b51d61a16f149 100644 --- a/clang/lib/AST/DeclPrinter.cpp +++ b/clang/lib/AST/DeclPrinter.cpp @@ -266,8 +266,11 @@ DeclPrinter::prettyPrintAttributes(const Decl *D, for (auto *A : D->getAttrs()) { if (A->isInherited() || A->isImplicit()) continue; - // Print out the keyword attributes, they aren't regular attributes. - if (Policy.PolishForDeclaration && !A->isKeywordAttribute()) + // Skip keyword attributes if explicitly suppressed. + if (A->isKeywordAttribute() && Policy.SuppressKeywordAttrs) + continue; + // PolishForDeclaration only wants keyword attributes; skip anything else. + if (!A->isKeywordAttribute() && Policy.PolishForDeclaration) continue; switch (A->getKind()) { #define ATTR(X) diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp index a412a9813b470..82d1b89ad51e9 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, TestClassSuppressKeywordAttrs) { + ASSERT_TRUE(PrintedDeclCXX11Matches( + "class alignas(32) A final {};", + cxxRecordDecl(hasName("A")).bind("id"), "class A {}", + [](PrintingPolicy &Policy) { Policy.SuppressKeywordAttrs = true; })); +} + +TEST(DeclPrinter, TestTemplateSuppressKeywordAttrs) { + ASSERT_TRUE(PrintedDeclCXX11Matches( + "template<typename T>\n" + "class alignas(32) A final {};", + classTemplateDecl(hasName("A")).bind("id"), + "template <typename T> class A {}", + [](PrintingPolicy &Policy) { Policy.SuppressKeywordAttrs = true; })); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
