[clang] 12fdabc - [clang][Interp] Diagnose dummy pointers used in Inc/Dec ops
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 6 07:57:19 PST 2024
Author: Timm Bäder
Date: 2024-03-06T16:57:02+01:00
New Revision: 12fdabc7908d3acbec42ce6172a225db85cb4f23
URL: https://github.com/llvm/llvm-project/commit/12fdabc7908d3acbec42ce6172a225db85cb4f23
DIFF: https://github.com/llvm/llvm-project/commit/12fdabc7908d3acbec42ce6172a225db85cb4f23.diff
LOG: [clang][Interp] Diagnose dummy pointers used in Inc/Dec ops
For example for unknown parameter decls.
Added:
Modified:
clang/lib/AST/Interp/Interp.h
clang/test/AST/Interp/literals.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 9c3d58d5619c46..43cbc2ff292c09 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -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)
@@ -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;
@@ -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;
@@ -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;
@@ -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;
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index d86609108ca446..7ae8499b1156dd 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -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
More information about the cfe-commits
mailing list