[clang] Suppress printing lambda body for constexpr diagnostics (PR #185800)
Sherman Pay via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 11 22:01:05 PDT 2026
https://github.com/shermpay updated https://github.com/llvm/llvm-project/pull/185800
>From 274e898ca4abfdb8bef6df4bc093cb1617e91e3a Mon Sep 17 00:00:00 2001
From: Sherman Pay <shermanpay at google.com>
Date: Tue, 10 Mar 2026 20:54:59 -0700
Subject: [PATCH] Suppress printing lambda body for constexpr diagnostics
closes: 125914
Introduce `SupressLambdaBody` `PrintingPolicy` that is used only for
constexpr diagnostics. This ensures `--print-ast` still works the same.
Add two tests:
1. To ast-printer-lambda to ensure `--print-ast` works the same.
2. Ensure lambda body is not printed for constexpr diagnostics.
---
clang/include/clang/AST/PrettyPrinter.h | 4 ++++
clang/lib/AST/ExprConstant.cpp | 12 +++++++-----
clang/lib/AST/StmtPrinter.cpp | 2 +-
clang/test/AST/ast-printer-lambda.cpp | 10 +++++++++-
clang/test/AST/constexpr-lambda-diagnostic.cpp | 8 ++++++++
5 files changed, 29 insertions(+), 7 deletions(-)
create mode 100644 clang/test/AST/constexpr-lambda-diagnostic.cpp
diff --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h
index a937d020b7277..534424fac606f 100644
--- a/clang/include/clang/AST/PrettyPrinter.h
+++ b/clang/include/clang/AST/PrettyPrinter.h
@@ -371,6 +371,10 @@ struct PrintingPolicy {
LLVM_PREFERRED_TYPE(bool)
unsigned SuppressDeclAttributes : 1;
+ /// Whether to suppress printing the body of a lambda.
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned SuppressLambdaBody : 1;
+
/// Callbacks to use to allow the behavior of printing to be customized.
const PrintingCallbacks *Callbacks = nullptr;
};
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 429fef0a1afa8..76cd7dfbc4542 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -1862,19 +1862,22 @@ APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {
void CallStackFrame::describe(raw_ostream &Out) const {
bool IsMemberCall = false;
bool ExplicitInstanceParam = false;
+ auto PrintingPolicy = Info.Ctx.getPrintingPolicy();
+ PrintingPolicy.SuppressLambdaBody = true;
+
if (const auto *MD = dyn_cast<CXXMethodDecl>(Callee)) {
IsMemberCall = !isa<CXXConstructorDecl>(MD) && !MD->isStatic();
ExplicitInstanceParam = MD->isExplicitObjectMemberFunction();
}
if (!IsMemberCall)
- Callee->getNameForDiagnostic(Out, Info.Ctx.getPrintingPolicy(),
+ Callee->getNameForDiagnostic(Out, PrintingPolicy,
/*Qualified=*/false);
if (This && IsMemberCall) {
if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(CallExpr)) {
const Expr *Object = MCE->getImplicitObjectArgument();
- Object->printPretty(Out, /*Helper=*/nullptr, Info.Ctx.getPrintingPolicy(),
+ Object->printPretty(Out, /*Helper=*/nullptr, PrintingPolicy,
/*Indentation=*/0);
if (Object->getType()->isPointerType())
Out << "->";
@@ -1882,8 +1885,7 @@ void CallStackFrame::describe(raw_ostream &Out) const {
Out << ".";
} else if (const auto *OCE =
dyn_cast_if_present<CXXOperatorCallExpr>(CallExpr)) {
- OCE->getArg(0)->printPretty(Out, /*Helper=*/nullptr,
- Info.Ctx.getPrintingPolicy(),
+ OCE->getArg(0)->printPretty(Out, /*Helper=*/nullptr, PrintingPolicy,
/*Indentation=*/0);
Out << ".";
} else {
@@ -1894,7 +1896,7 @@ void CallStackFrame::describe(raw_ostream &Out) const {
Info.Ctx.getLValueReferenceType(This->Designator.MostDerivedType));
Out << ".";
}
- Callee->getNameForDiagnostic(Out, Info.Ctx.getPrintingPolicy(),
+ Callee->getNameForDiagnostic(Out, PrintingPolicy,
/*Qualified=*/false);
}
diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp
index 4d364fdcd5502..86adf6626ed15 100644
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -2463,7 +2463,7 @@ void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
// Print the body.
OS << ' ';
- if (Policy.TerseOutput)
+ if (Policy.TerseOutput || Policy.SuppressLambdaBody)
OS << "{}";
else
PrintRawCompoundStmt(Node->getCompoundStmtBody());
diff --git a/clang/test/AST/ast-printer-lambda.cpp b/clang/test/AST/ast-printer-lambda.cpp
index 08f1ff555b0b1..015aa7e791205 100644
--- a/clang/test/AST/ast-printer-lambda.cpp
+++ b/clang/test/AST/ast-printer-lambda.cpp
@@ -51,6 +51,14 @@ void test1(int i, T... t) {
auto lambda = [k{t...}] {};
//CHECK: [k{t...}] {
}
+{
+ auto lambda = [i]{
+ (void)i;
+ };
+ //CHECK: [i] {
+ //CHECK-NEXT: (void)i;
+ //CHECK-NEXT: };
+}
}
-};
\ No newline at end of file
+};
diff --git a/clang/test/AST/constexpr-lambda-diagnostic.cpp b/clang/test/AST/constexpr-lambda-diagnostic.cpp
new file mode 100644
index 0000000000000..1edc873718967
--- /dev/null
+++ b/clang/test/AST/constexpr-lambda-diagnostic.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -std=c++17 -verify=expected %s
+constexpr void undefined(); // expected-note {{declared here}}
+static constexpr int r = [] { // expected-error {{constexpr variable 'r' must be initialized by a constant expression}} \
+ expected-note {{in call to '[] {}.operator()()'}}}
+ undefined(); // expected-note {{undefined function 'undefined' cannot be used in a constant expression}}
+ return 0;
+ }();
+
More information about the cfe-commits
mailing list