[clang] [BPF] Support preserve_access_index attribute for enum (PR #232498)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 09:18:01 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()) {
----------------
yonghong-song wrote:

Good point. Missed these a few cases. Will add.

https://github.com/llvm/llvm-project/pull/232498


More information about the cfe-commits mailing list