[clang][dataflow] Model pointer value for builtin functions.
authorMartin Braenne <mboehme@google.com>
Mon, 12 Jun 2023 08:11:03 +0000 (08:11 +0000)
committerMartin Braenne <mboehme@google.com>
Mon, 12 Jun 2023 12:23:18 +0000 (12:23 +0000)
This fixes a false positive in the Crubit nullability verification.

Reviewed By: gribozavr2

Differential Revision: https://reviews.llvm.org/D152683

clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

index 8547e50..e0cb872 100644 (file)
@@ -400,7 +400,8 @@ public:
       Env.setValue(Loc, NullPointerVal);
       break;
     }
-    case CK_FunctionToPointerDecay: {
+    case CK_FunctionToPointerDecay:
+    case CK_BuiltinFnToFnPtr: {
       StorageLocation *PointeeLoc =
           Env.getStorageLocation(*SubExpr, SkipPast::Reference);
       if (PointeeLoc == nullptr)
index 1a2442f..7077f73 100644 (file)
@@ -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