Skip to content

Commit

Permalink
[clang][Interp] Fix converting function pointers to bool
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D156786
  • Loading branch information
tbaederr committed Aug 1, 2023
1 parent 6ba4b21 commit 015ffba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
}

case CK_PointerToBoolean: {
PrimType PtrT = classifyPrim(SubExpr->getType());

// Just emit p != nullptr for this.
if (!this->visit(SubExpr))
return false;

if (!this->emitNullPtr(CE))
if (!this->emitNull(PtrT, CE))
return false;

return this->emitNEPtr(CE);
return this->emitNE(PtrT, CE);
}

case CK_ToVoid:
Expand Down
7 changes: 7 additions & 0 deletions clang/test/AST/Interp/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ namespace FunctionReturnType {

constexpr S s{ 12 };
static_assert(s.fp == nullptr, ""); // zero-initialized function pointer.

constexpr int (*op)(int, int) = add;
constexpr bool b = op;
static_assert(op, "");
static_assert(!!op, "");
constexpr int (*op2)(int, int) = nullptr;
static_assert(!op2, "");
}

namespace Comparison {
Expand Down

0 comments on commit 015ffba

Please sign in to comment.