[clang] [StaticAnalyzer] Handle `__builtin_bit_cast` (PR #139188)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Sat May 10 06:16:15 PDT 2025
================
@@ -282,15 +282,48 @@ ProgramStateRef ExprEngine::handleLValueBitCast(
void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
ExplodedNode *Pred, ExplodedNodeSet &Dst) {
- ExplodedNodeSet dstPreStmt;
- getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);
-
- if (CastE->getCastKind() == CK_LValueToRValue ||
- CastE->getCastKind() == CK_LValueToRValueBitCast) {
- for (ExplodedNode *subExprNode : dstPreStmt) {
- ProgramStateRef state = subExprNode->getState();
- const LocationContext *LCtx = subExprNode->getLocationContext();
- evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx));
+ ExplodedNodeSet DstPreStmt;
+ getCheckerManager().runCheckersForPreStmt(DstPreStmt, Pred, CastE, *this);
+
+ if (CastE->getCastKind() == CK_LValueToRValue) {
+ for (ExplodedNode *Node : DstPreStmt) {
+ ProgramStateRef State = Node->getState();
+ const LocationContext *LCtx = Node->getLocationContext();
+ evalLoad(Dst, CastE, CastE, Node, State, State->getSVal(Ex, LCtx));
+ }
+ return;
+ }
+ if (CastE->getCastKind() == CK_LValueToRValueBitCast) {
+ // Handle `__builtin_bit_cast`:
+ ExplodedNodeSet DstEvalLoc;
+
+ // Simulate the lvalue-to-rvalue conversion on `Ex`:
+ for (ExplodedNode *Node : DstPreStmt) {
+ ProgramStateRef State = Node->getState();
+ const LocationContext *LCtx = Node->getLocationContext();
+ evalLocation(DstEvalLoc, CastE, Ex, Node, State, State->getSVal(Ex, LCtx),
+ true);
+ }
+ // Simulate the operation that actually casts the original value to a new
+ // value of the destination type :
+ StmtNodeBuilder Bldr(DstEvalLoc, Dst, *currBldrCtx);
+
+ for (ExplodedNode *Node : DstEvalLoc) {
+ ProgramStateRef State = Node->getState();
+ const LocationContext *LCtx = Node->getLocationContext();
+ // Although `Ex` is an lvalue, it could have `Loc::ConcreteInt` kind
+ // (e.g., `(int *)123456`). In such cases, there is no MemRegion
+ // available and we can't get the value to be casted.
+ const MemRegion *MR = State->getSVal(Ex, LCtx).getAsRegion();
+ SVal CastedV = UnknownVal();
+
+ if (MR) {
----------------
steakhal wrote:
You could use init ifs here.
https://github.com/llvm/llvm-project/pull/139188
More information about the cfe-commits
mailing list