[clang] [BPF] Support preserve_access_index attribute for enum (PR #232498)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 7 20:05:32 PST 2026
================
@@ -179,10 +210,199 @@ void SemaBPF::handlePreserveAIRecord(RecordDecl *RD) {
}
void SemaBPF::handlePreserveAccessIndexAttr(Decl *D, const ParsedAttr &AL) {
- auto *Rec = cast<RecordDecl>(D);
- handlePreserveAIRecord(Rec);
- Rec->addAttr(::new (getASTContext())
- BPFPreserveAccessIndexAttr(getASTContext(), AL));
+ if (auto *Rec = dyn_cast<RecordDecl>(D))
+ handlePreserveAIRecord(Rec);
+ auto *A =
+ ::new (getASTContext()) BPFPreserveAccessIndexAttr(getASTContext(), AL);
+ if (auto *ED = dyn_cast<EnumDecl>(D);
+ ED && !ED->getDeclName() && !AL.isPragmaClangAttribute())
+ AnonPreserveAIEnums.insert({ED, A});
+ D->addAttr(A);
+}
+
+void SemaBPF::DiagnoseAnonymousPreserveAIEnums() {
+ for (auto [ED, A] : AnonPreserveAIEnums)
+ if (!ED->getTypedefNameForAnonDecl())
+ Diag(A->getLocation(), diag::warn_preserve_access_index_anon_enum) << A;
+ AnonPreserveAIEnums.clear();
+}
+
+// Return the enum type, or its typedef name, that the enumerator E is relocated
+// against, or a null type if E is not relocated.
+static QualType getPreserveAIEnumType(ASTContext &Context,
+ const DeclRefExpr *E) {
+ const auto *Enumerator = dyn_cast<EnumConstantDecl>(E->getDecl());
+ if (!Enumerator)
+ return QualType();
+
+ // In C, an enumerator usually has type int, not the enum type, so find the
+ // enum from its context. Within the enum's definition, e.g., A in B = A + 1,
+ // the enum is incomplete and A stays a constant.
+ const auto *ED = dyn_cast<EnumDecl>(Enumerator->getDeclContext());
+ if (!ED || !ED->isCompleteDefinition() ||
+ !ED->hasAttr<BPFPreserveAccessIndexAttr>())
+ return QualType();
+
+ // libbpf matches relocations by type name, so an anonymous enum needs a
+ // typedef name.
+ if (ED->getDeclName())
+ return Context.getCanonicalTagType(ED);
+ if (const TypedefNameDecl *TD = ED->getTypedefNameForAnonDecl())
+ return Context.getTypeDeclType(TD);
+ return QualType();
+}
+
+// Return the enumerator reference E, or, if it is relocated, a pseudo-object
+// whose semantic form is a __builtin_preserve_enum_value() call.
+Expr *SemaBPF::BuildPreserveAIEnumeratorRef(DeclRefExpr *E) {
+ if (SemaRef.isUnevaluatedContext())
+ return E;
+
+ ASTContext &Context = getASTContext();
+ QualType EnumTy = getPreserveAIEnumType(Context, E);
+ if (EnumTy.isNull())
+ return E;
+
+ // The value cannot be relocated where a constant is required: in a constant
+ // evaluated context, e.g., an array bound or a case label, or outside of a
+ // function, e.g., a global initializer. Warn, and use the enumerator's value.
+ if (SemaRef.isConstantEvaluatedContext() ||
+ SemaRef.CurContext->getRedeclContext()->isFileContext()) {
----------------
4ast wrote:
Three uses in a function need a constant too. This check doesn't see them:
```
use at file scope in a function
---------------------------- -------------- -----------------
static int s = A; new -W group hard error + note
int a[8] = {[A] = 1}; new -W group hard error + note
constexpr int c = A; (C23) new -W group hard error + note
```
The parser reads them in a `PotentiallyEvaluated` context, so `A` is wrapped. `-Wno-error=` and the pragma don't help there. The author of the prog can only type the number or move the variable to file scope.
AttrDocs and the commit log say that the value is used wherever a constant is required.
`keepPreserveAIEnumeratorsConstant()` is there for asm. Should `AddInitializerToDecl()` and `CheckArrayDesignatorExpr()` use it too? Then only builtin arguments need the note from ExprConstant.cpp.
https://github.com/llvm/llvm-project/pull/232498
More information about the cfe-commits
mailing list