Skip to content

Commit

Permalink
[clang][Interp] Diagnose dummy pointers used in Inc/Dec ops
Browse files Browse the repository at this point in the history
For example for unknown parameter decls.
  • Loading branch information
tbaederr committed Mar 6, 2024
1 parent 8e4887f commit 12fdabc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 5 additions & 6 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,7 @@ enum class IncDecOp {

template <typename T, IncDecOp Op, PushVal DoPush>
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (Ptr.isDummy())
return false;
assert(!Ptr.isDummy());

if constexpr (std::is_same_v<T, Boolean>) {
if (!S.getLangOpts().CPlusPlus14)
Expand Down Expand Up @@ -585,7 +584,7 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool Inc(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();
if (Ptr.isDummy())
if (!CheckDummy(S, OpPC, Ptr))
return false;
if (!CheckInitialized(S, OpPC, Ptr, AK_Increment))
return false;
Expand All @@ -599,7 +598,7 @@ bool Inc(InterpState &S, CodePtr OpPC) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool IncPop(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();
if (Ptr.isDummy())
if (!CheckDummy(S, OpPC, Ptr))
return false;
if (!CheckInitialized(S, OpPC, Ptr, AK_Increment))
return false;
Expand All @@ -614,7 +613,7 @@ bool IncPop(InterpState &S, CodePtr OpPC) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool Dec(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();
if (Ptr.isDummy())
if (!CheckDummy(S, OpPC, Ptr))
return false;
if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement))
return false;
Expand All @@ -628,7 +627,7 @@ bool Dec(InterpState &S, CodePtr OpPC) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool DecPop(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();
if (Ptr.isDummy())
if (!CheckDummy(S, OpPC, Ptr))
return false;
if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement))
return false;
Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,18 @@ namespace IncDec {
return a[1];
}
static_assert(f() == 3, "");

int nonconst(int a) { // both-note 4{{declared here}}
static_assert(a++, ""); // both-error {{not an integral constant expression}} \
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
static_assert(a--, ""); // both-error {{not an integral constant expression}} \
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
static_assert(++a, ""); // both-error {{not an integral constant expression}} \
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
static_assert(--a, ""); // both-error {{not an integral constant expression}} \
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
}

};
#endif

Expand Down

0 comments on commit 12fdabc

Please sign in to comment.