================
@@ -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)) {
+ Diag(Call->getBeginLoc(), diag::note_preserve_enum_value_not_const)
+ << Call->getSourceRange();
+ Diag(Call->getBeginLoc(), diag::note_preserve_enum_value_case_attr);
+ }
+ return ER;
+ }
+ if (Replaced.empty())
+ return ER;
+
+ // Evaluate E with the local enumerator values.
+ Expr::EvalResult Result;
+ if (ConstE->isGLValue() ||
+ !ConstE->EvaluateAsRValue(Result, getASTContext(),
+ /*InConstantContext=*/true) ||
+ !Result.Val.isInt()) {
+ // The value is not an integer rvalue, e.g., a compound literal folded as
+ // an extension. Keep the enumerators constants and warn.
+ for (const auto &R : Replaced)
+ warnEnumeratorNotRelocated(*this, cast<DeclRefExpr>(*R.first));
+ return ER;
+ }
+
+ // Restore the relocated enumerators. ActOnFinishSwitchStmt() looks through a
+ // ConstantExpr at the top of a case value to find its unpromoted type, so
put
+ // the ConstantExpr in a no-op cast to keep its value, e.g.,
+ // NoOp(ConstantExpr(A)) for "case A:".
+ for (auto [Slot, Ref] : Replaced)
+ *Slot = Ref;
+ ASTContext &Context = getASTContext();
+ Expr *CE = ConstantExpr::Create(Context, E, Result.Val);
+ return ImplicitCastExpr::Create(Context, E->getType(), CK_NoOp, CE,
+ /*BasePath=*/nullptr, VK_PRValue,
+ FPOptionsOverride());
----------------
4ast wrote:
Am I reading it right that a switch on the enum type warns for every label with
`-Wconversion`:
```
switch (e) enum E, promoted to unsigned int
case A: int, PseudoObjectExpr
```
`AnalyzeImplicitConversions()` looks through the no-op cast and the
`ConstantExpr`. `TryGetExprRange()` can't evaluate the `PseudoObjectExpr` and
returns the full range of `int`. The result is "implicit conversion changes
signedness: 'int' to 'unsigned int'".
https://github.com/llvm/llvm-project/pull/243338
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits