Skip to content

Commit 12fdabc

Browse files
committed
[clang][Interp] Diagnose dummy pointers used in Inc/Dec ops
For example for unknown parameter decls.
1 parent 8e4887f commit 12fdabc

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,7 @@ enum class IncDecOp {
525525

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

531530
if constexpr (std::is_same_v<T, Boolean>) {
532531
if (!S.getLangOpts().CPlusPlus14)
@@ -585,7 +584,7 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
585584
template <PrimType Name, class T = typename PrimConv<Name>::T>
586585
bool Inc(InterpState &S, CodePtr OpPC) {
587586
const Pointer &Ptr = S.Stk.pop<Pointer>();
588-
if (Ptr.isDummy())
587+
if (!CheckDummy(S, OpPC, Ptr))
589588
return false;
590589
if (!CheckInitialized(S, OpPC, Ptr, AK_Increment))
591590
return false;
@@ -599,7 +598,7 @@ bool Inc(InterpState &S, CodePtr OpPC) {
599598
template <PrimType Name, class T = typename PrimConv<Name>::T>
600599
bool IncPop(InterpState &S, CodePtr OpPC) {
601600
const Pointer &Ptr = S.Stk.pop<Pointer>();
602-
if (Ptr.isDummy())
601+
if (!CheckDummy(S, OpPC, Ptr))
603602
return false;
604603
if (!CheckInitialized(S, OpPC, Ptr, AK_Increment))
605604
return false;
@@ -614,7 +613,7 @@ bool IncPop(InterpState &S, CodePtr OpPC) {
614613
template <PrimType Name, class T = typename PrimConv<Name>::T>
615614
bool Dec(InterpState &S, CodePtr OpPC) {
616615
const Pointer &Ptr = S.Stk.pop<Pointer>();
617-
if (Ptr.isDummy())
616+
if (!CheckDummy(S, OpPC, Ptr))
618617
return false;
619618
if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement))
620619
return false;
@@ -628,7 +627,7 @@ bool Dec(InterpState &S, CodePtr OpPC) {
628627
template <PrimType Name, class T = typename PrimConv<Name>::T>
629628
bool DecPop(InterpState &S, CodePtr OpPC) {
630629
const Pointer &Ptr = S.Stk.pop<Pointer>();
631-
if (Ptr.isDummy())
630+
if (!CheckDummy(S, OpPC, Ptr))
632631
return false;
633632
if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement))
634633
return false;

clang/test/AST/Interp/literals.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,18 @@ namespace IncDec {
839839
return a[1];
840840
}
841841
static_assert(f() == 3, "");
842+
843+
int nonconst(int a) { // both-note 4{{declared here}}
844+
static_assert(a++, ""); // both-error {{not an integral constant expression}} \
845+
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
846+
static_assert(a--, ""); // both-error {{not an integral constant expression}} \
847+
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
848+
static_assert(++a, ""); // both-error {{not an integral constant expression}} \
849+
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
850+
static_assert(--a, ""); // both-error {{not an integral constant expression}} \
851+
// both-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
852+
}
853+
842854
};
843855
#endif
844856

0 commit comments

Comments
 (0)