Skip to content

Commit

Permalink
[clang][dataflow] Model pointer value for builtin functions.
Browse files Browse the repository at this point in the history
This fixes a false positive in the Crubit nullability verification.

Reviewed By: gribozavr2

Differential Revision: https://reviews.llvm.org/D152683
  • Loading branch information
martinboehme committed Jun 12, 2023
1 parent d09fa8f commit 3f31d32
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
Env.setValue(Loc, NullPointerVal);
break;
}
case CK_FunctionToPointerDecay: {
case CK_FunctionToPointerDecay:
case CK_BuiltinFnToFnPtr: {
StorageLocation *PointeeLoc =
Env.getStorageLocation(*SubExpr, SkipPast::Reference);
if (PointeeLoc == nullptr)
Expand Down
33 changes: 33 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5327,4 +5327,37 @@ TEST(TransferTest, FunctionToPointerDecayHasValue) {
});
}

// Check that the pointer that a builtin function decays to is associated with
// a value.
TEST(TransferTest, BuiltinFunctionModeled) {
std::string Code = R"(
void target() {
__builtin_expect(0, 0);
// [[p]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
using ast_matchers::selectFirst;
using ast_matchers::match;
using ast_matchers::traverse;
using ast_matchers::implicitCastExpr;
using ast_matchers::hasCastKind;

const Environment &Env = getEnvironmentAtAnnotation(Results, "p");

auto *ImplicitCast = selectFirst<ImplicitCastExpr>(
"implicit_cast",
match(traverse(TK_AsIs,
implicitCastExpr(hasCastKind(CK_BuiltinFnToFnPtr))
.bind("implicit_cast")),
ASTCtx));

ASSERT_THAT(ImplicitCast, NotNull());
EXPECT_THAT(Env.getValueStrict(*ImplicitCast), NotNull());
});
}

} // namespace

0 comments on commit 3f31d32

Please sign in to comment.