Skip to content

[clang] implement printing of canonical expressions #135133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void StaticAccessedThroughInstanceCheck::check(
PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true;
PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope = true;

PrintingPolicyWithSuppressedTag.PrintCanonicalTypes =
PrintingPolicyWithSuppressedTag.PrintAsCanonical =
!BaseExpr->getType()->isTypedefNameType();

std::string BaseTypeName =
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/utils/Matchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool MatchesAnyListedTypeNameMatcher::matches(

PrintingPolicy PrintingPolicyWithSuppressedTag(
Finder->getASTContext().getLangOpts());
PrintingPolicyWithSuppressedTag.PrintCanonicalTypes = CanonicalTypes;
PrintingPolicyWithSuppressedTag.PrintAsCanonical = CanonicalTypes;
PrintingPolicyWithSuppressedTag.SuppressElaboration = true;
PrintingPolicyWithSuppressedTag.SuppressScope = false;
PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true;
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/AST/PrettyPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct PrintingPolicy {
MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
MSVCFormatting(false), ConstantsAsWritten(false),
SuppressImplicitBase(false), FullyQualifiedName(false),
PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true),
PrintAsCanonical(false), PrintInjectedClassNameWithArguments(true),
UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
CleanUglifiedParameters(false), EntireContentsOfLargeArray(true),
UseEnumerators(true), UseHLSLTypes(LO.HLSL) {}
Expand Down Expand Up @@ -310,9 +310,9 @@ struct PrintingPolicy {
LLVM_PREFERRED_TYPE(bool)
unsigned FullyQualifiedName : 1;

/// Whether to print types as written or canonically.
/// Whether to print entities as written or canonically.
LLVM_PREFERRED_TYPE(bool)
unsigned PrintCanonicalTypes : 1;
unsigned PrintAsCanonical : 1;

/// Whether to print an InjectedClassNameType with template arguments or as
/// written. When a template argument is unnamed, printing it results in
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/DeclPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
llvm::raw_string_ostream POut(Proto);
DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten();
if (TArgAsWritten && !Policy.PrintCanonicalTypes)
if (TArgAsWritten && !Policy.PrintAsCanonical)
TArgPrinter.printTemplateArguments(TArgAsWritten->arguments(), nullptr);
else if (const TemplateArgumentList *TArgs =
D->getTemplateSpecializationArgs())
Expand Down Expand Up @@ -1124,7 +1124,7 @@ void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
S->getSpecializedTemplate()->getTemplateParameters();
const ASTTemplateArgumentListInfo *TArgAsWritten =
S->getTemplateArgsAsWritten();
if (TArgAsWritten && !Policy.PrintCanonicalTypes)
if (TArgAsWritten && !Policy.PrintAsCanonical)
printTemplateArguments(TArgAsWritten->arguments(), TParams);
else
printTemplateArguments(S->getTemplateArgs().asArray(), TParams);
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/JSONNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,8 @@ void JSONNodeDumper::VisitTemplateExpansionTemplateArgument(
void JSONNodeDumper::VisitExpressionTemplateArgument(
const TemplateArgument &TA) {
JOS.attribute("isExpr", true);
if (TA.isCanonicalExpr())
JOS.attribute("isCanonical", true);
}
void JSONNodeDumper::VisitPackTemplateArgument(const TemplateArgument &TA) {
JOS.attribute("isPack", true);
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/AST/StmtPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,9 +1305,13 @@ void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Qualifier->print(OS, Policy);
if (Node->hasTemplateKeyword())
OS << "template ";

bool ForceAnonymous =
Policy.PrintAsCanonical && VD->getKind() == Decl::NonTypeTemplateParm;
DeclarationNameInfo NameInfo = Node->getNameInfo();
if (IdentifierInfo *ID = NameInfo.getName().getAsIdentifierInfo();
ID || NameInfo.getName().getNameKind() != DeclarationName::Identifier) {
!ForceAnonymous &&
(ID || NameInfo.getName().getNameKind() != DeclarationName::Identifier)) {
if (Policy.CleanUglifiedParameters &&
isa<ParmVarDecl, NonTypeTemplateParmDecl>(VD) && ID)
OS << ID->deuglifiedName();
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/AST/TemplateBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,12 @@ void TemplateArgument::print(const PrintingPolicy &Policy, raw_ostream &Out,
printIntegral(*this, Out, Policy, IncludeType);
break;

case Expression:
getAsExpr()->printPretty(Out, nullptr, Policy);
case Expression: {
PrintingPolicy ExprPolicy = Policy;
ExprPolicy.PrintAsCanonical = isCanonicalExpr();
getAsExpr()->printPretty(Out, nullptr, ExprPolicy);
break;
}

case Pack:
Out << "<";
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/AST/TemplateName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ bool TemplateName::containsUnexpandedParameterPack() const {

void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
Qualified Qual) const {
auto handleAnonymousTTP = [](TemplateDecl *TD, raw_ostream &OS) {
auto handleAnonymousTTP = [&](TemplateDecl *TD, raw_ostream &OS) {
if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(TD);
TTP && TTP->getIdentifier() == nullptr) {
TTP && (Policy.PrintAsCanonical || TTP->getIdentifier() == nullptr)) {
OS << "template-parameter-" << TTP->getDepth() << "-" << TTP->getIndex();
return true;
}
Expand All @@ -430,13 +430,19 @@ void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
// names more often than to export them, thus using the original name is
// most useful in this case.
TemplateDecl *Template = getAsTemplateDecl();
if (Policy.PrintAsCanonical)
Template = cast<TemplateDecl>(Template->getCanonicalDecl());
if (handleAnonymousTTP(Template, OS))
return;
if (Qual == Qualified::None)
OS << *Template;
else
Template->printQualifiedName(OS, Policy);
} else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
if (Policy.PrintAsCanonical) {
QTN->getUnderlyingTemplate().print(OS, Policy, Qual);
return;
}
if (NestedNameSpecifier *NNS = QTN->getQualifier();
Qual != Qualified::None && NNS)
NNS->print(OS, Policy);
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,8 @@ void TextNodeDumper::VisitTemplateExpansionTemplateArgument(
void TextNodeDumper::VisitExpressionTemplateArgument(
const TemplateArgument &TA) {
OS << " expr";
if (TA.isCanonicalExpr())
OS << " canonical";
dumpTemplateArgument(TA);
}

Expand Down
16 changes: 9 additions & 7 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) {

static SplitQualType splitAccordingToPolicy(QualType QT,
const PrintingPolicy &Policy) {
if (Policy.PrintCanonicalTypes)
if (Policy.PrintAsCanonical)
QT = QT.getCanonicalType();
return QT.split();
}
Expand Down Expand Up @@ -1273,8 +1273,11 @@ void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) {}

void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
OS << "decltype(";
if (T->getUnderlyingExpr())
T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
if (const Expr *E = T->getUnderlyingExpr()) {
PrintingPolicy ExprPolicy = Policy;
ExprPolicy.PrintAsCanonical = T->isCanonicalUnqualified();
E->printPretty(OS, nullptr, ExprPolicy);
}
OS << ')';
spaceBeforePlaceHolder(OS);
}
Expand Down Expand Up @@ -1548,7 +1551,7 @@ void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
const ASTTemplateArgumentListInfo *TArgAsWritten =
S->getTemplateArgsAsWritten();
IncludeStrongLifetimeRAII Strong(Policy);
if (TArgAsWritten && !Policy.PrintCanonicalTypes)
if (TArgAsWritten && !Policy.PrintAsCanonical)
printTemplateArgumentList(OS, TArgAsWritten->arguments(), Policy,
TParams);
else
Expand Down Expand Up @@ -2422,9 +2425,8 @@ static void
printTo(raw_ostream &OS, ArrayRef<TA> Args, const PrintingPolicy &Policy,
const TemplateParameterList *TPL, bool IsPack, unsigned ParmIndex) {
// Drop trailing template arguments that match default arguments.
if (TPL && Policy.SuppressDefaultTemplateArgs &&
!Policy.PrintCanonicalTypes && !Args.empty() && !IsPack &&
Args.size() <= TPL->size()) {
if (TPL && Policy.SuppressDefaultTemplateArgs && !Policy.PrintAsCanonical &&
!Args.empty() && !IsPack && Args.size() <= TPL->size()) {
llvm::SmallVector<TemplateArgument, 8> OrigArgs;
for (const TA &A : Args)
OrigArgs.push_back(getArgument(A));
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const {

PP.SuppressInlineNamespace =
PrintingPolicy::SuppressInlineNamespaceMode::None;
PP.PrintCanonicalTypes = true;
PP.PrintAsCanonical = true;
PP.UsePreferredNames = false;
PP.AlwaysIncludeTypeForTemplateArgument = true;
PP.UseEnumerators = false;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3463,7 +3463,7 @@ Sema::findFailedBooleanCondition(Expr *Cond) {
{
llvm::raw_string_ostream Out(Description);
PrintingPolicy Policy = getPrintingPolicy();
Policy.PrintCanonicalTypes = true;
Policy.PrintAsCanonical = true;
FailedBooleanConditionPrinterHelper Helper(Policy);
FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr);
}
Expand Down
Loading
Loading