[clang] [clang][bytecode] Reject enumerators with BPFPreserveAccessIndexAttr (PR #243449)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 17 04:14:15 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
See https://github.com/llvm/llvm-project/commit/c080a0b9b673ecce94e06c19213487cb233d24e1
---
Full diff: https://github.com/llvm/llvm-project/pull/243449.diff
4 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+29)
- (modified) clang/lib/AST/ByteCode/Interp.h (+8)
- (modified) clang/lib/AST/ByteCode/Opcodes.td (+4)
- (modified) clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c (+1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 5044656e3afff..ad786154db8cb 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4725,6 +4725,35 @@ bool Compiler<Emitter>::VisitCXXRewrittenBinaryOperator(
template <class Emitter>
bool Compiler<Emitter>::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
+ // Diagnose a relocated BPF enumerator, i.e., of an enum with the
+ // preserve_access_index attribute, which is not a constant expression. Return
+ // true if diagnosed.
+ auto getAccessIndexConstantDecl = [&]() -> const EnumConstantDecl * {
+ const auto *DRE = dyn_cast<DeclRefExpr>(E->getSyntacticForm());
+ if (!DRE)
+ return nullptr;
+ const auto *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl());
+ if (!ECD)
+ return nullptr;
+ const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext());
+ if (ED && ED->hasAttr<BPFPreserveAccessIndexAttr>())
+ return ECD;
+ return nullptr;
+ };
+
+ if (const EnumConstantDecl *ECD = getAccessIndexConstantDecl()) {
+ const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
+ // An anonymous enum is only relocated through its typedef name.
+ QualType EnumTy;
+ if (ED->getDeclName())
+ EnumTy = Ctx.getASTContext().getCanonicalTagType(ED);
+ else if (const TypedefNameDecl *TD = ED->getTypedefNameForAnonDecl())
+ EnumTy = Ctx.getASTContext().getTypeDeclType(TD);
+ else
+ return false;
+
+ return this->emitInvalidAccessIndexAttr(ECD, EnumTy.getTypePtr(), E);
+ }
for (const Expr *SemE : E->semantics()) {
if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 80927ecc77c0a..372888e8098bf 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3794,6 +3794,14 @@ inline bool InvalidStore(InterpState &S, CodePtr OpPC, const Type *T) {
return false;
}
+inline bool InvalidAccessIndexAttr(InterpState &S, CodePtr OpPC,
+ const ValueDecl *ECD, const Type *EnumTy) {
+ const Expr *E = S.Current->getExpr(OpPC);
+ S.FFDiag(E, diag::note_constexpr_bpf_preserve_ai_enumerator)
+ << ECD << QualType(EnumTy, 0) << E->getSourceRange();
+ return false;
+}
+
inline bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC) {
if (S.inConstantContext()) {
const SourceRange &ArgRange = S.Current->getRange(OpPC);
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 23aa5abeb0a88..18c265a5dee5c 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -1078,3 +1078,7 @@ def PopMSVCCE : SuccessOpcode;
def GetStringPtr : SuccessOpcode {
let Args = [ArgExpr];
}
+
+def InvalidAccessIndexAttr : Opcode {
+ let Args = [ArgValueDecl, ArgTypePtr];
+}
diff --git a/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c b/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c
index 26cdc0fc93902..a474160b7ffc3 100644
--- a/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c
+++ b/clang/test/Sema/bpf-attr-preserve-access-index-enum-constant.c
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=err %s
+// RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=err %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=warn \
// RUN: -Wno-error=bpf-enum-not-relocated %s
// RUN: %clang_cc1 -triple bpf -debug-info-kind=limited -fsyntax-only -verify=none \
``````````
</details>
https://github.com/llvm/llvm-project/pull/243449
More information about the cfe-commits
mailing list