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 29f17164164e0..5e377a6c0c247 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 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 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\n" + "class alignas(32) [[deprecated]] A final {};", + classTemplateDecl(hasName("A")).bind("id"), + "template class A {}", + [](PrintingPolicy &Policy) { Policy.SuppressDeclAttributes = true; })); +}