https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/179895
>From a2b54ca68f774ddc14b3310cea6e315e1b2a1ec4 Mon Sep 17 00:00:00 2001 From: Michael Buch <[email protected]> Date: Thu, 5 Feb 2026 09:23:13 +0000 Subject: [PATCH 1/2] [clang][Sema] Split a err_typecheck_assign_const diagnostic into a separate tablegen entry As of recently, in LLDB, when trying to mutate an object in a const method, we emit a hint about why we failed to run the expression (with an associated hint on how to fix it). This relies on the diagnostic ID that Clang told us about. However, we only want to emit this message when we assign to a member in a const method. But not all the other situations that `err_typecheck_assign` gets used in. We currently work around this by grepping the error message, but it would be nice if we could just rely on the diagnostic ID. This patch splits out the relevant diagnostic. This isn't urgent and we can live with the "grep the error message" approach. But if the Clang maintainers don't feel strongly about keeping the tablegen as-is, it'd be nice to clean up from LLDB's perspective. --- .../clang/Basic/DiagnosticSemaKinds.td | 43 +++++++++++-------- clang/lib/Sema/SemaExpr.cpp | 11 +++-- .../Clang/ClangUserExpression.cpp | 7 +-- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 71c478f4ca873..5ba359e41941e 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7961,24 +7961,31 @@ def warn_arith_conv_mixed_unicode_types "different Unicode character types %1 and %2">, InGroup<CharacterConversion>; -def err_typecheck_assign_const : Error< - "%select{" - "cannot assign to return value because function %1 returns a const value|" - "cannot assign to variable %1 with const-qualified type %2|" - "cannot assign to %select{non-|}1static data member %2 " - "with const-qualified type %3|" - "cannot assign to non-static data member within const member function %1|" - "cannot assign to %select{variable %2|non-static data member %2|lvalue}1 " - "with %select{|nested }3const-qualified data member %4|" - "read-only variable is not assignable}0">; - -def note_typecheck_assign_const : Note< - "%select{" - "function %1 which returns const-qualified type %2 declared here|" - "variable %1 declared const here|" - "%select{non-|}1static data member %2 declared const here|" - "member function %q1 is declared const here|" - "%select{|nested }1data member %2 declared const here}0">; +def err_typecheck_assign_const + : Error<"%select{" + "cannot assign to return value because function %1 returns a const " + "value|" + "cannot assign to variable %1 with const-qualified type %2|" + "cannot assign to %select{non-|}1static data member %2 " + "with const-qualified type %3|" + "cannot assign to %select{variable %2|non-static data member " + "%2|lvalue}1 " + "with %select{|nested }3const-qualified data member %4|" + "read-only variable is not assignable}0">; + +def note_typecheck_assign_const + : Note<"%select{" + "function %1 which returns const-qualified type %2 declared here|" + "variable %1 declared const here|" + "%select{non-|}1static data member %2 declared const here|" + "%select{|nested }1data member %2 declared const here}0">; + +def err_typecheck_assign_const_method + : Error<"cannot assign to non-static data member within const member " + "function %0">; + +def note_typecheck_assign_const_method + : Note<"member function %q0 is declared const here">; def warn_unsigned_always_true_comparison : Warning< "result of comparison of %select{%3|unsigned expression}0 %2 " diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 5795a71b5cae8..5c00f3d8d85c4 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -13829,9 +13829,8 @@ enum { ConstFunction, ConstVariable, ConstMember, - ConstMethod, NestedConstMember, - ConstUnknown, // Keep as last element + ConstUnknown, // Keep as last element }; /// Emit the "read-only variable not assignable" error and print notes to give @@ -13939,12 +13938,12 @@ static void DiagnoseConstAssignment(Sema &S, const Expr *E, if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { if (MD->isConst()) { if (!DiagnosticEmitted) { - S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange - << ConstMethod << MD; + S.Diag(Loc, diag::err_typecheck_assign_const_method) + << ExprRange << MD; DiagnosticEmitted = true; } - S.Diag(MD->getLocation(), diag::note_typecheck_assign_const) - << ConstMethod << MD << MD->getSourceRange(); + S.Diag(MD->getLocation(), diag::note_typecheck_assign_const_method) + << MD << MD->getSourceRange(); } } } diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp index fb16a6bd0ad1d..ebbaee1631a78 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp @@ -956,11 +956,8 @@ void ClangUserExpression::FixupParseErrorDiagnostics( switch (diag->GetCompilerID()) { case clang::diag::err_member_function_call_bad_cvr: return true; - case clang::diag::err_typecheck_assign_const: - // FIXME: can we split this particular error into a separate - // diagnostic ID so we don't need to scan the error message? - return diag->GetDetail().message.find( - "within const member function") != std::string::npos; + case clang::diag::err_typecheck_assign_const_method: + return true; default: return false; } >From 6827e5d3b43e2eb4d497d762d84e0c98c43521ec Mon Sep 17 00:00:00 2001 From: Michael Buch <[email protected]> Date: Thu, 5 Feb 2026 10:16:08 +0000 Subject: [PATCH 2/2] fixup! merge switch cases --- .../Plugins/ExpressionParser/Clang/ClangUserExpression.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp index ebbaee1631a78..ba17ce8a10a8f 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp @@ -955,7 +955,6 @@ void ClangUserExpression::FixupParseErrorDiagnostics( [](std::unique_ptr<Diagnostic> const &diag) { switch (diag->GetCompilerID()) { case clang::diag::err_member_function_call_bad_cvr: - return true; case clang::diag::err_typecheck_assign_const_method: return true; default: _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
