[clang] [FlowSensitive][Nullability] 1/N add initial model for nullability analysis (PR #245392)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 03:54:14 PST 2026


================
@@ -0,0 +1,175 @@
+//===-- PointerNullability.cpp - Pointer nullability properties -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/FlowSensitive/Models/Nullability/PointerNullability.h"
+#include "clang/AST/Type.h"
+#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
+#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
+#include "clang/Analysis/FlowSensitive/Formula.h"
+#include "clang/Analysis/FlowSensitive/Value.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/Specifiers.h"
+#include "llvm/ADT/StringRef.h"
+#include <cassert>
+#include <optional>
+
+namespace clang::dataflow::nullability {
+
+/// The nullness information of a pointer is represented by two properties
+/// which indicate if its source was nullable, and if its value is null.
+constexpr llvm::StringLiteral kFromNullable = "from_nullable";
+constexpr llvm::StringLiteral kNull = "is_null";
+
+bool isSupportedPointerType(QualType T) { return isSupportedRawPointerType(T); }
+
+bool isSupportedRawPointerType(QualType T) { return T->isPointerType(); }
+
+PointerValue *getRawPointerValue(const Expr *PointerExpr,
+                                 const Environment &Env) {
+  return Env.get<PointerValue>(*PointerExpr);
+}
+
+PointerValue *getPointerValue(const Expr *PointerExpr, const Environment &Env) {
+  QualType Ty = PointerExpr->getType();
+  if (Ty->isNullPtrType() || isSupportedRawPointerType(Ty))
+    return getRawPointerValue(PointerExpr, Env);
+  return nullptr;
+}
+
+bool hasPointerNullState(const PointerValue &PointerVal) {
+  return PointerVal.getProperty(kFromNullable) != nullptr &&
+         PointerVal.getProperty(kNull) != nullptr;
+}
+
+PointerNullState getPointerNullState(const PointerValue &PointerVal) {
+  Value *FromNullableProp = PointerVal.getProperty(kFromNullable);
+  Value *NullProp = PointerVal.getProperty(kNull);
+
+  assert(FromNullableProp != nullptr && NullProp != nullptr &&
+         "PointerVal is missing null state!");
+
+  return {
+      isa<TopBoolValue>(FromNullableProp)
----------------
Xazax-hun wrote:

Could this be simplified if `TopBoolValue` also had a `formula` method that just returned null? 

https://github.com/llvm/llvm-project/pull/245392


More information about the cfe-commits mailing list