================
@@ -402,4 +430,87 @@ Expr *SemaBPF::checkLocalConstantInitializer(const VarDecl
*VD, Expr *Init) {
return keepPreserveAIEnumeratorsConstant(Init);
}
+// Replace each relocated enumerator in Slot with its DeclRefExpr, and record
it
+// in Replaced to be restored.
+static void
+replaceEnumeratorRefs(Stmt *&Slot,
+ SmallVectorImpl<std::pair<Stmt **, Stmt *>> &Replaced) {
+ if (!Slot)
+ return;
+ if (auto *E = dyn_cast<Expr>(Slot);
+ E && SemaBPF::isPreserveAIEnumeratorRef(E)) {
+ Replaced.push_back({&Slot, Slot});
+ Slot = cast<PseudoObjectExpr>(E)->getSyntacticForm();
+ return;
+ }
+ for (Stmt *&Child : Slot->children())
+ replaceEnumeratorRefs(Child, Replaced);
+}
+
+// Return an explicit __builtin_preserve_enum_value() call in S, not in an
+// operand of sizeof or alignof.
+static const CallExpr *findPreserveEnumValueCall(const Stmt *S) {
+ if (!S || isa<UnaryExprOrTypeTraitExpr>(S))
+ return nullptr;
+ if (const auto *Call = dyn_cast<CallExpr>(S);
+ Call && Call->getBuiltinCallee() == BPF::BI__builtin_preserve_enum_value)
+ return Call;
+ for (const Stmt *Child : S->children())
+ if (const CallExpr *Call = findPreserveEnumValueCall(Child))
+ return Call;
+ return nullptr;
+}
+
+// Verify that the case label value E is an integer constant expression with
the
+// local enumerator values. A relocated enumerator in E is kept, so that
+// "case A:" behaves like "if (cond == A)". Return E in a ConstantExpr with the
+// local value, which is used to check the case values, e.g., for -Wswitch.
+ExprResult SemaBPF::verifyCaseExpr(Expr *E) {
+ SmallVector<std::pair<Stmt **, Stmt *>, 4> Replaced;
+ Stmt *Root = E;
+ replaceEnumeratorRefs(Root, Replaced);
+
+ // Check E with the local enumerator values.
+ auto *ConstE = cast<Expr>(Root);
+ ExprResult ER =
+ SemaRef.VerifyIntegerConstantExpression(ConstE, AllowFoldKind::Allow);
+ if (ER.isInvalid()) {
+ // An explicit __builtin_preserve_enum_value() call is not a constant, but
+ // an enumerator of an enum with the attribute would be relocated here.
+ if (const CallExpr *Call = findPreserveEnumValueCall(ConstE)) {
----------------
yonghong-song wrote:
Yes, will do a follow up.
https://github.com/llvm/llvm-project/pull/243338
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits