[clang] Add SuppressKeywordAttrs printing policy (PR #175398)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 10 15:38:41 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Kim Gräsman (kimgr)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/175398.diff
3 Files Affected:
- (modified) clang/include/clang/AST/PrettyPrinter.h (+6-1)
- (modified) clang/lib/AST/DeclPrinter.cpp (+5-2)
- (modified) clang/unittests/AST/DeclPrinterTest.cpp (+16)
``````````diff
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; }));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/175398
More information about the cfe-commits
mailing list