https://github.com/keepyixiao updated https://github.com/llvm/llvm-project/pull/239911
>From 1416f849b3c0f6cc5ce1832ae1046d3d8b1daa71 Mon Sep 17 00:00:00 2001 From: nudt_yixiao <[email protected]> Date: Sat, 7 Nov 2026 11:39:17 +0800 Subject: [PATCH 1/2] [Clang] Preserve original arguments in constexpr call diagnostics When constexpr evaluation fails, call stack diagnostics may show modified parameter values instead of the original arguments passed at the call site. Save the original argument values in CallStackFrame::OriginalArgs and use them when describing constexpr calls. Add regression tests for modified and multiple function arguments. --- clang/docs/ReleaseNotes.md | 3 + clang/lib/AST/ExprConstant.cpp | 25 ++++++- .../SemaCXX/constant-expression-cxx14.cpp | 71 +++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index e6e84323ca73f1..1cbbc5f20f0cec 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -797,6 +797,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and - Clang will now no longer assert or miscompile on a constructor that has more than 65535 base and member initializers. (#GH219366) +- Fixed an issue where diagnostics for failed constant expressions could report modified parameter + values instead of the original argument values at the constexpr function call site. (#GH107554) + ### Improvements to Clang's time-trace ### Improvements to Coverage Mapping diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 57d5bb475d0f9b..fc4b3f8f938fa3 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -512,6 +512,11 @@ namespace { /// Index - The call index of this call. unsigned Index; + /// The values of the parameters at the beginning of this call. + /// Used to describe the constexpr call in diagnostics, since the + /// parameter values may be modified during constant evaluation. + llvm::SmallVector<APValue, 4> OriginalArgs; + /// The stack of integers for tracking version numbers for temporaries. SmallVector<unsigned, 2> TempVersionStack = {1}; unsigned CurTempVersion = TempVersionStack.back(); @@ -1292,6 +1297,17 @@ CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange, Index(Info.NextCallIndex++) { Info.CurrentCall = this; ++Info.CallStackDepth; + // Save the original argument values before executing + // the callee. The parameter slots may be modified + // during constant evaluation. + if (Arguments) { + for (const ParmVarDecl *Param : Callee->parameters()) { + if (const APValue *V = Info.getParamSlot(Arguments, Param)) + OriginalArgs.push_back(*V); + else + OriginalArgs.emplace_back(); + } + } } CallStackFrame::~CallStackFrame() { @@ -1976,12 +1992,17 @@ void CallStackFrame::describe(raw_ostream &Out) const { } Out << '('; - + unsigned ArgIndex = ExplicitInstanceParam; llvm::ListSeparator Comma; for (const ParmVarDecl *Param : Callee->parameters().slice(ExplicitInstanceParam)) { Out << Comma; - const APValue *V = Info.getParamSlot(Arguments, Param); + const APValue *V = nullptr; + + if (ArgIndex < OriginalArgs.size() && !OriginalArgs[ArgIndex].isAbsent()) { + V = &OriginalArgs[ArgIndex]; + } + ArgIndex++; if (V) V->printPretty(Out, Info.Ctx, Param->getType()); else diff --git a/clang/test/SemaCXX/constant-expression-cxx14.cpp b/clang/test/SemaCXX/constant-expression-cxx14.cpp index c64466700808ad..ee2be4908fa3a8 100644 --- a/clang/test/SemaCXX/constant-expression-cxx14.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx14.cpp @@ -1466,3 +1466,74 @@ constexpr bool missingCase() { 1u: return false; // expected-error {{expected 'case' keyword before expression}} } } + +constexpr int f(int x) { + ++x; + return x / x; // expected-note {{division by zero}} +} + +constexpr int ii = -1; +static_assert(f(ii) == 1, ""); // expected-error {{static assertion expression is not an integral constant expression}} \ + expected-note {{in call to 'f(-1)'}} + +constexpr int multi_arg_1(int x, int y) { + ++x; + ++y; + return x / (y - y); // expected-note {{division by zero}} +} + +constexpr int a = -1; +constexpr int b = 1; + +static_assert(multi_arg_1(a, b) == 1, ""); // expected-error {{not an integral constant expression}} \ + expected-note {{in call to 'multi_arg_1(-1, 1)'}} + +constexpr int multi_arg_2(int x, int y) { + ++y; + return x / (y - y); // expected-note {{division by zero}} +} + +constexpr int a1 = -1; +constexpr int b1 = 1; + +static_assert(multi_arg_2(a1, b1) == 1, ""); // expected-error {{not an integral constant expression}} \ + expected-note {{in call to 'multi_arg_2(-1, 1)'}} + +constexpr int ref_arg_1(int &x) { + ++x; + return x / (x - x); // expected-note {{division by zero}} +} + +constexpr int test_ref_arg_1() { + int a = -1; + return ref_arg_1(a); // expected-note {{in call to 'ref_arg_1(a)'}} +} + +static_assert(test_ref_arg_1() == 1, ""); // expected-error {{not an integral constant expression}} \ + expected-note {{in call to 'test_ref_arg_1()'}} + +constexpr int ref_arg_2(int &x, int &y) { + ++x; + ++y; + return x / (y - y); // expected-note {{division by zero}} +} + +constexpr int test_ref_arg_2() { + int a = -1; + int b = 1; + return ref_arg_2(a, b); // expected-note {{in call to 'ref_arg_2(a, b)'}} +} + +static_assert(test_ref_arg_2() == 1, ""); // expected-error {{not an integral constant expression}} \ + expected-note {{in call to 'test_ref_arg_2()'}} + +char c[3] = "Hi"; + +constexpr char g1(int i) { i *= 2; return c[i]; } // expected-note {{cannot refer to element 10 of array of 3 elements in a constant expression}} +constexpr char f1(int i, int j) { + auto line = i * 4; + return g1(line + j); // expected-note {{in call to 'g1(5)'}} +} + +static_assert(f1(1, 1), ""); // expected-error {{not an integral constant expression}} \ + expected-note {{in call to 'f1(1, 1)'}} >From 9140ae0b1672ebe15aa1d5fa46d0593559634693 Mon Sep 17 00:00:00 2001 From: nudt_yixiao <[email protected]> Date: Thu, 12 Nov 2026 18:02:51 +0800 Subject: [PATCH 2/2] Apply batched suggestions from code review Co-authored-by: Timm Baeder <[email protected]> --- clang/lib/AST/ExprConstant.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index fc4b3f8f938fa3..ad7c266942fbda 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -1999,10 +1999,9 @@ void CallStackFrame::describe(raw_ostream &Out) const { Out << Comma; const APValue *V = nullptr; - if (ArgIndex < OriginalArgs.size() && !OriginalArgs[ArgIndex].isAbsent()) { + if (ArgIndex < OriginalArgs.size() && !OriginalArgs[ArgIndex].isAbsent()) V = &OriginalArgs[ArgIndex]; - } - ArgIndex++; + ++ArgIndex; if (V) V->printPretty(Out, Info.Ctx, Param->getType()); else _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
