Author: Timm Bäder Date: 2024-02-25T17:26:36+01:00 New Revision: 411c5dde59fa4c427941143ca0ec8cd8fdaee407
URL: https://github.com/llvm/llvm-project/commit/411c5dde59fa4c427941143ca0ec8cd8fdaee407 DIFF: https://github.com/llvm/llvm-project/commit/411c5dde59fa4c427941143ca0ec8cd8fdaee407.diff LOG: [clang][Interp] Handle null function pointers We were instead asserting that they are non-null before. Added: Modified: clang/lib/AST/Interp/FunctionPointer.h clang/lib/AST/Interp/Interp.h clang/test/AST/Interp/functions.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/Interp/FunctionPointer.h b/clang/lib/AST/Interp/FunctionPointer.h index 4a3f993d4882e2..bb3da9b50aa552 100644 --- a/clang/lib/AST/Interp/FunctionPointer.h +++ b/clang/lib/AST/Interp/FunctionPointer.h @@ -26,6 +26,7 @@ class FunctionPointer final { FunctionPointer(const Function *Func) : Func(Func) { assert(Func); } const Function *getFunction() const { return Func; } + bool isZero() const { return !Func; } APValue toAPValue() const { if (!Func) diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h index 23a2756a18f690..d885d19ce7064f 100644 --- a/clang/lib/AST/Interp/Interp.h +++ b/clang/lib/AST/Interp/Interp.h @@ -2087,6 +2087,12 @@ inline bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize) { const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>(); const Function *F = FuncPtr.getFunction(); + if (!F) { + const Expr *E = S.Current->getExpr(OpPC); + S.FFDiag(E, diag::note_constexpr_null_callee) + << const_cast<Expr *>(E) << E->getSourceRange(); + return false; + } assert(F); assert(ArgSize >= F->getWrittenArgSize()); diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp index 51269741eb9018..9daf8722050f07 100644 --- a/clang/test/AST/Interp/functions.cpp +++ b/clang/test/AST/Interp/functions.cpp @@ -123,9 +123,12 @@ namespace FunctionPointers { } constexpr int applyBinOp(int a, int b, int (*op)(int, int)) { - return op(a, b); + return op(a, b); // both-note {{evaluates to a null function pointer}} } static_assert(applyBinOp(1, 2, add) == 3, ""); + static_assert(applyBinOp(1, 2, nullptr) == 3, ""); // both-error {{not an integral constant expression}} \ + // both-note {{in call to}} + constexpr int ignoreReturnValue() { int (*foo)(int, int) = add; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits