Skip to content

Commit 68c9ddb

Browse files
authored
[clang][bytecode] Typecheck called function pointers more thorougly (#159757)
Fix two older FIXME items from the `functions.cpp` test.
1 parent b7e4edc commit 68c9ddb

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,6 @@ Context::getOverridingFunction(const CXXRecordDecl *DynamicDecl,
446446

447447
const Function *Context::getOrCreateFunction(const FunctionDecl *FuncDecl) {
448448
assert(FuncDecl);
449-
FuncDecl = FuncDecl->getMostRecentDecl();
450-
451449
if (const Function *Func = P->getFunction(FuncDecl))
452450
return Func;
453451

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,9 +1750,8 @@ bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
17501750
const Pointer &Ptr = S.Stk.pop<Pointer>();
17511751

17521752
if (Ptr.isZero()) {
1753-
const auto *E = cast<CallExpr>(S.Current->getExpr(OpPC));
1754-
S.FFDiag(E, diag::note_constexpr_null_callee)
1755-
<< const_cast<Expr *>(E->getCallee()) << E->getSourceRange();
1753+
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_null_callee)
1754+
<< const_cast<Expr *>(CE->getCallee()) << CE->getSourceRange();
17561755
return false;
17571756
}
17581757

@@ -1778,6 +1777,14 @@ bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
17781777
return false;
17791778
}
17801779

1780+
// Can happen when casting function pointers around.
1781+
QualType CalleeType = CE->getCallee()->getType();
1782+
if (CalleeType->isPointerType() &&
1783+
!S.getASTContext().hasSameFunctionTypeIgnoringExceptionSpec(
1784+
F->getDecl()->getType(), CalleeType->getPointeeType())) {
1785+
return false;
1786+
}
1787+
17811788
assert(ArgSize >= F->getWrittenArgSize());
17821789
uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
17831790

clang/test/AST/ByteCode/functions.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -pedantic -verify=expected,both %s
2-
// RUN: %clang_cc1 -std=c++14 -fexperimental-new-constant-interpreter -pedantic -verify=expected,both %s
3-
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -pedantic -verify=expected,both %s
4-
// RUN: %clang_cc1 -pedantic -verify=ref,both %s
5-
// RUN: %clang_cc1 -pedantic -std=c++14 -verify=ref,both %s
6-
// RUN: %clang_cc1 -pedantic -std=c++20 -verify=ref,both %s
1+
// RUN: %clang_cc1 -pedantic -verify=expected,both %s -fexperimental-new-constant-interpreter
2+
// RUN: %clang_cc1 -std=c++14 -pedantic -verify=expected,both %s -fexperimental-new-constant-interpreter
3+
// RUN: %clang_cc1 -std=c++20 -pedantic -verify=expected,both %s -fexperimental-new-constant-interpreter
4+
// RUN: %clang_cc1 -pedantic -verify=ref,both %s
5+
// RUN: %clang_cc1 -std=c++14 -pedantic -verify=ref,both %s
6+
// RUN: %clang_cc1 -std=c++20 -pedantic -verify=ref,both %s
77

88
#define fold(x) (__builtin_constant_p(0) ? (x) : (x))
99

@@ -672,10 +672,8 @@ namespace FunctionCast {
672672
constexpr int test4 = fold(IntFn(DoubleFn(f)))();
673673
constexpr int test5 = IntFn(fold(DoubleFn(f)))(); // both-error {{constant expression}} \
674674
// both-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
675-
// FIXME: Interpreter is less strict here.
676-
constexpr int test6 = fold(IntPtrFn(f2))() == nullptr; // ref-error {{constant expression}}
677-
// FIXME: The following crashes interpreter
678-
// constexpr int test6 = fold(IntFn(f3)());
675+
constexpr int test6 = fold(IntPtrFn(f2))() == nullptr; // both-error {{constant expression}}
676+
constexpr int test7 = fold(IntFn(f3)()); // both-error {{must be initialized by a constant expression}}
679677
}
680678

681679
#if __cplusplus >= 202002L

0 commit comments

Comments
 (0)