Skip to content

Commit 5bb0213

Browse files
committed
[clang][bytecode] Reject calls to pure virtual functions
1 parent 876174f commit 5bb0213

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,18 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
13771377
size_t ArgSize = Func->getArgSize() + VarArgSize;
13781378
size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
13791379
Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1380+
const FunctionDecl *Callee = Func->getDecl();
1381+
1382+
// C++2a [class.abstract]p6:
1383+
// the effect of making a virtual call to a pure virtual function [...] is
1384+
// undefined
1385+
if (Callee->isPureVirtual()) {
1386+
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_pure_virtual_call,
1387+
1)
1388+
<< Callee;
1389+
S.Note(Callee->getLocation(), diag::note_declared_at);
1390+
return false;
1391+
}
13801392

13811393
const CXXRecordDecl *DynamicDecl = nullptr;
13821394
{
@@ -1393,7 +1405,7 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
13931405
assert(DynamicDecl);
13941406

13951407
const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
1396-
const auto *InitialFunction = cast<CXXMethodDecl>(Func->getDecl());
1408+
const auto *InitialFunction = cast<CXXMethodDecl>(Callee);
13971409
const CXXMethodDecl *Overrider = S.getContext().getOverridingFunction(
13981410
DynamicDecl, StaticDecl, InitialFunction);
13991411

clang/test/AST/ByteCode/cxx2a.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,13 @@ consteval int f(int i) {
177177
return 2 * i;
178178
}
179179
static_assert(test(42));
180+
181+
namespace PureVirtual {
182+
struct Abstract {
183+
constexpr virtual void f() = 0; // both-note {{declared here}}
184+
constexpr Abstract() { do_it(); } // both-note {{in call to}}
185+
constexpr void do_it() { f(); } // both-note {{pure virtual function 'PureVirtual::Abstract::f' called}}
186+
};
187+
struct PureVirtualCall : Abstract { void f(); }; // both-note {{in call to 'Abstract}}
188+
constexpr PureVirtualCall pure_virtual_call; // both-error {{constant expression}} both-note {{in call to 'PureVirtualCall}}
189+
}

0 commit comments

Comments
 (0)