Skip to content

Commit 3f31d32

Browse files
committed
[clang][dataflow] Model pointer value for builtin functions.
This fixes a false positive in the Crubit nullability verification. Reviewed By: gribozavr2 Differential Revision: https://reviews.llvm.org/D152683
1 parent d09fa8f commit 3f31d32

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

clang/lib/Analysis/FlowSensitive/Transfer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
400400
Env.setValue(Loc, NullPointerVal);
401401
break;
402402
}
403-
case CK_FunctionToPointerDecay: {
403+
case CK_FunctionToPointerDecay:
404+
case CK_BuiltinFnToFnPtr: {
404405
StorageLocation *PointeeLoc =
405406
Env.getStorageLocation(*SubExpr, SkipPast::Reference);
406407
if (PointeeLoc == nullptr)

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5327,4 +5327,37 @@ TEST(TransferTest, FunctionToPointerDecayHasValue) {
53275327
});
53285328
}
53295329

5330+
// Check that the pointer that a builtin function decays to is associated with
5331+
// a value.
5332+
TEST(TransferTest, BuiltinFunctionModeled) {
5333+
std::string Code = R"(
5334+
void target() {
5335+
__builtin_expect(0, 0);
5336+
// [[p]]
5337+
}
5338+
)";
5339+
runDataflow(
5340+
Code,
5341+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
5342+
ASTContext &ASTCtx) {
5343+
using ast_matchers::selectFirst;
5344+
using ast_matchers::match;
5345+
using ast_matchers::traverse;
5346+
using ast_matchers::implicitCastExpr;
5347+
using ast_matchers::hasCastKind;
5348+
5349+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
5350+
5351+
auto *ImplicitCast = selectFirst<ImplicitCastExpr>(
5352+
"implicit_cast",
5353+
match(traverse(TK_AsIs,
5354+
implicitCastExpr(hasCastKind(CK_BuiltinFnToFnPtr))
5355+
.bind("implicit_cast")),
5356+
ASTCtx));
5357+
5358+
ASSERT_THAT(ImplicitCast, NotNull());
5359+
EXPECT_THAT(Env.getValueStrict(*ImplicitCast), NotNull());
5360+
});
5361+
}
5362+
53305363
} // namespace

0 commit comments

Comments
 (0)