Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,43 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
ExplodedNodeSet dstPreStmt;
getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);

if (CastE->getCastKind() == CK_LValueToRValue ||
CastE->getCastKind() == CK_LValueToRValueBitCast) {
if (CastE->getCastKind() == CK_LValueToRValue) {
for (ExplodedNode *subExprNode : dstPreStmt) {
ProgramStateRef state = subExprNode->getState();
const LocationContext *LCtx = subExprNode->getLocationContext();
evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx));
}
return;
}
if (CastE->getCastKind() == CK_LValueToRValueBitCast) {
// Handle `__builtin_bit_cast`:
ExplodedNodeSet dstEvalLoad;

// Simulate the lvalue-to-rvalue conversion on `Ex`:
for (ExplodedNode *subExprNode : dstPreStmt) {
ProgramStateRef state = subExprNode->getState();
const LocationContext *LCtx = subExprNode->getLocationContext();
evalLocation(dstEvalLoad, CastE, Ex, subExprNode, 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(dstEvalLoad, Dst, *currBldrCtx);

for (ExplodedNode *Node : dstEvalLoad) {
ProgramStateRef state = Node->getState();
const LocationContext *LCtx = Node->getLocationContext();
// getAsRegion should always be successful since Ex is an lvalue:
SVal OrigV = state->getSVal(state->getSVal(Ex, LCtx).getAsRegion());
SVal CastedV =
svalBuilder.evalCast(svalBuilder.simplifySVal(state, OrigV),
CastE->getType(), Ex->getType());

state = state->BindExpr(CastE, LCtx, CastedV);
Bldr.generateNode(CastE, Node, state);
}
return;
}

// All other casts.
QualType T = CastE->getType();
Expand Down
14 changes: 12 additions & 2 deletions clang/test/Analysis/builtin_bitcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct A {
}
};
void gh_69922(size_t p) {
// expected-warning-re@+1 {{(reg_${{[0-9]+}}<size_t p>) & 1U}}
// expected-warning@+1 {{Unknown}}
clang_analyzer_dump(__builtin_bit_cast(A*, p & 1));

__builtin_bit_cast(A*, p & 1)->set(2); // no-crash
Expand All @@ -49,5 +49,15 @@ void gh_69922(size_t p) {
// store to the member variable `n`.

clang_analyzer_dump(__builtin_bit_cast(A*, p & 1)->n); // Ideally, this should print "2".
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<size_t p>) & 1U}}
// expected-warning@-1 {{Unknown}}
}

namespace {
typedef unsigned long uintptr_t;

bool previously_crash(const void *& ptr) {
clang_analyzer_dump(__builtin_bit_cast(void*, static_cast<uintptr_t>(-1)));
// expected-warning-re@-1 {{{{[0-9]+}} (Loc)}}
return ptr == __builtin_bit_cast(void*, static_cast<uintptr_t>(-1));
}
}
2 changes: 1 addition & 1 deletion clang/test/Analysis/exercise-ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void f4(char *array) {

_Static_assert(sizeof(int) == 4, "Wrong triple for the test");

clang_analyzer_dump_int(__builtin_bit_cast(int, b)); // expected-warning {{lazyCompoundVal}}
clang_analyzer_dump_int(__builtin_bit_cast(int, b)); // expected-warning {{Unknown}}
clang_analyzer_dump_int(array[__builtin_bit_cast(int, b)]); // expected-warning {{Unknown}}

array[__builtin_bit_cast(int, b)] = 0x10; // no crash
Expand Down