Skip to content

Commit

Permalink
[Clang] Correctly handle allocation in template arguments
Browse files Browse the repository at this point in the history
Fixes #62462

Reviewed By: #clang-language-wg, erichkeane

Differential Revision: https://reviews.llvm.org/D150036
  • Loading branch information
cor3ntin committed May 8, 2023
1 parent 1d21d2e commit 9857caf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 3 additions & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ Bug Fixes in This Version
(`#62192 <https://github.com/llvm/llvm-project/issues/62192>`_)
- Fix crash when attempting to pass a non-pointer type as first argument of
``__builtin_assume_aligned``.
(`#62305 <https://github.com/llvm/llvm-project/issues/62305>`_)
(`#62305 <https://github.com/llvm/llvm-project/issues/62305>`_)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -416,6 +416,8 @@ Bug Fixes to C++ Support
initialization.
(`#61567 <https://github.com/llvm/llvm-project/issues/61567>`_)
- Fix a crash when expanding a pack as the index of a subscript expression.
- Fix handling of constexpr dynamic memory allocations in template
arguments. (`#62462 <https://github.com/llvm/llvm-project/issues/62462>`_)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
12 changes: 10 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15354,8 +15354,16 @@ bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
LValue LVal;
LVal.set(Base);

if (!::EvaluateInPlace(Result.Val, Info, LVal, this) || Result.HasSideEffects)
return false;
{
// C++23 [intro.execution]/p5
// A full-expression is [...] a constant-expression
// So we need to make sure temporary objects are destroyed after having
// evaluating the expression (per C++23 [class.temporary]/p4).
FullExpressionRAII Scope(Info);
if (!::EvaluateInPlace(Result.Val, Info, LVal, this) ||
Result.HasSideEffects || !Scope.destroy())
return false;
}

if (!Info.discardCleanups())
llvm_unreachable("Unhandled cleanup; missing full expression marker?");
Expand Down
24 changes: 24 additions & 0 deletions clang/test/SemaCXX/cxx2a-constexpr-dynalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,27 @@ namespace PR48606 {
}
static_assert(h());
}

namespace GH62462 {

class string {
public:
char *mem;
constexpr string() {
this->mem = new char(1);
}
constexpr ~string() {
delete this->mem;
}
constexpr unsigned size() const { return 4; }
};


template <unsigned N>
void test() {};

void f() {
test<string().size()>();
}

}

0 comments on commit 9857caf

Please sign in to comment.