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

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 20:00:38 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Jan Voung (jvoung)

<details>
<summary>Changes</summary>

For https://github.com/llvm/llvm-project/issues/245374

- Adds initial modeling of pointer nullability state for raw pointers: (a) Is the pointer from a _Nullable annotated value, and (2) is the pointer null? 
- Adds transfer function for assignments, addrOf, and nullptr.

Tests currently only inspect the dataflow state. Next patch will introduce a diagnoser, so that we can add tests checking emitted diagnostics. Then will handle comparisons, etc.

Mostly from https://github.com/google/crubit/tree/main/nullability but
- simplifying (deferring the hooks for now that we use for inference -- e.g., the [AdditionalConstraints](https://github.com/google/crubit/blob/7ef99d77672f716792140a5d5f6548cf7d638200/nullability/pointer_nullability.h#L151))
- top level pointers for now, not nested -- deferring the [type nullability](https://github.com/google/crubit/blob/7ef99d77672f716792140a5d5f6548cf7d638200/nullability/type_nullability.h#L88) [pass](https://github.com/google/crubit/blob/7ef99d77672f716792140a5d5f6548cf7d638200/nullability/type_transferer.h#L13)

Assisted-by: AGY for splitting the code

---

Patch is 59.69 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/245392.diff


18 Files Affected:

- (added) clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullability.h (+145) 
- (added) clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityAnalysis.h (+60) 
- (added) clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityLattice.h (+24) 
- (added) clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityMatchers.h (+34) 
- (added) clang/include/clang/Analysis/FlowSensitive/Models/Nullability/ValueTransferer.h (+46) 
- (modified) clang/lib/Analysis/FlowSensitive/Models/CMakeLists.txt (+2) 
- (added) clang/lib/Analysis/FlowSensitive/Models/Nullability/CMakeLists.txt (+17) 
- (added) clang/lib/Analysis/FlowSensitive/Models/Nullability/PointerNullability.cpp (+175) 
- (added) clang/lib/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityAnalysis.cpp (+136) 
- (added) clang/lib/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityMatchers.cpp (+34) 
- (added) clang/lib/Analysis/FlowSensitive/Models/Nullability/ValueTransferer.cpp (+176) 
- (modified) clang/unittests/Analysis/FlowSensitive/CMakeLists.txt (+2) 
- (added) clang/unittests/Analysis/FlowSensitive/Models/Nullability/AssignmentTest.cpp (+184) 
- (added) clang/unittests/Analysis/FlowSensitive/Models/Nullability/CMakeLists.txt (+24) 
- (added) clang/unittests/Analysis/FlowSensitive/Models/Nullability/CheckNullabilityState.cpp (+51) 
- (added) clang/unittests/Analysis/FlowSensitive/Models/Nullability/CheckNullabilityState.h (+27) 
- (added) clang/unittests/Analysis/FlowSensitive/Models/Nullability/JoinTest.cpp (+155) 
- (added) clang/unittests/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityTest.cpp (+130) 


``````````diff
diff --git a/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullability.h b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullability.h
new file mode 100644
index 00000000000000..ec40a729302a76
--- /dev/null
+++ b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullability.h
@@ -0,0 +1,145 @@
+//===-- PointerNullability.h - 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file extends the dataflow framework's Value model to track nullability.
+// We attach two boolean properties to each modeled pointer value:
+//  - is_null: whether the pointer may actually be null
+//    If this is false, dereferencing is safe.
+//  - from_nullable: whether the originating expression was considered nullable
+//    (e.g. a nullptr literal, or a reference to a Nullable-annotated variable)
+//    If this is false, dereferencing may be safe: we don't know the contract.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITY_H
+#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITY_H
+
+#include "clang/AST/Expr.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/Specifiers.h"
+#include <optional>
+
+namespace clang::dataflow::nullability {
+
+/// Is this exactly a pointer type that we track outer nullability for?
+/// This unwraps sugar, i.e. it looks at the canonical type.
+///
+/// (For now, only regular `PointerType`s. Support for smart pointers will be
+/// added later; code that will need to handle them already uses this function
+/// rather than `isSupportedRawPointerType()`, so it won't need to change. In
+/// future we should consider supporting pointer-to-member, ObjC pointers, etc).
+bool isSupportedPointerType(QualType T);
+
+/// Is this exactly a raw (non-smart) pointer type that we track outer
+/// nullability for?
+/// This unwraps sugar, i.e. it looks at the canonical type.
+bool isSupportedRawPointerType(QualType T);
+
+/// Returns the `PointerValue` allocated to `PointerExpr` if available.
+/// Otherwise, returns nullptr.
+PointerValue *getRawPointerValue(const Expr *PointerExpr,
+                                 const Environment &Env);
+
+/// Returns the `PointerValue` for a raw pointer expression, if available.
+/// Otherwise, returns nullptr.
+///
+/// This is intended to support both raw and smart pointer expressions.
+/// Smart pointers are not supported yet, so for now this is the same as
+/// `getRawPointerValue()`.
+///
+/// Use this function only if the expression can actually be either a raw or
+/// smart pointer; otherwise, use `getRawPointerValue()`.
+PointerValue *getPointerValue(const Expr *PointerExpr, const Environment &Env);
+
+// Returns true if the pointer has all properties necessary for representing
+// complete nullness information.
+// Otherwise, returns false.
+//
+// Pointers that are the value of some expression always have null state once
+// that expression has been analyzed. Other pointers, like the values of unused
+// parameters, may lack this state. This state is only set by
+// PointerNullabilityAnalysis, not by the dataflow framework.
+bool hasPointerNullState(const PointerValue &PointerVal);
+
+/// The properties representing nullness information for a pointer.
+///
+/// We attach these properties to every PointerValue taken by an expression.
+///
+/// A null pointer for `FromNullable` or `IsNull` represents "top", i.e. we have
+/// no information on this property.
+struct PointerNullState {
+  /// Did the pointer come from a known-nullable source?
+  const Formula *FromNullable;
+  /// Is the pointer's value null?
+  const Formula *IsNull;
+  // These are independent: sources with unknown nullability can yield nullptr!
+};
+
+/// Returns the properties representing the nullness information of a pointer.
+PointerNullState getPointerNullState(const PointerValue &PointerVal);
+
+/// Creates the nullness properties on `PointerVal` if not already initialised.
+///
+/// We call this when the framework produces a PointerValue for an expression.
+/// This ensures that the variable has usable "from nullable" and "is null"
+/// boolean variables, and that they are constrained based on the *original*
+/// source of the PointerValue.
+///
+/// For example:
+///    int *_Null_unspecified x = getNullable();
+///                               ~~~~~~~~~~~ <-- initPointerNullState(Nullable)
+///    *x;
+///     ~ <-- initPointerNullState(Unspecified) - no effect, already initialized
+///
+/// (We assume that the framework will not provide the same pointer from
+/// different initial sources, so the `Source` nullability is the same
+/// regardless of block evaluation order).
+void initPointerNullState(PointerValue &PointerVal,
+                          DataflowAnalysisContext &Ctx,
+                          std::optional<NullabilityKind> Source = std::nullopt);
+
+/// Initializes the nullness properties on `PointerVal` from `State`.
+///
+/// This overload may only be called on a freshly created `PointerValue` that
+/// does not yet have nullability properties.
+void initPointerNullState(PointerValue &PointerVal,
+                          DataflowAnalysisContext &Ctx, PointerNullState State);
+
+/// Variant of initPointerNullState, where the pointer is guaranteed null.
+void initNullPointer(PointerValue &PointerVal, DataflowAnalysisContext &Ctx);
+
+/// Returns true if `NullState` is known to be null or is from a nullable source
+/// and may be null.
+bool isNullable(PointerNullState NullState, const Environment &Env);
+
+/// Returns true if `PointerVal` is known to be null or is from a nullable
+/// source and may be null. Convenience overload for `isNullable` above.
+bool isNullable(const PointerValue &PointerVal, const Environment &Env);
+
+/// Returns the strongest provable assertion we can make for the given null
+/// state. If a pointer with that state may not be null, returns Nonnull. If it
+/// is known to be null or is from a nullable source and may be null, returns
+/// Nullable. Otherwise, returns Unspecified.
+NullabilityKind getNullability(PointerNullState PointerNullState,
+                               const Environment &Env);
+
+/// Returns the strongest provable assertion we can make about `PointerVal`.
+NullabilityKind getNullability(const PointerValue &PointerVal,
+                               const Environment &Env);
+
+/// Returns the strongest provable assertion we can make about the value of
+/// `E` in `Env`.
+NullabilityKind getNullability(const Expr *E, const Environment &Env);
+
+} // namespace clang::dataflow::nullability
+
+#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITY_H
diff --git a/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityAnalysis.h b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityAnalysis.h
new file mode 100644
index 00000000000000..0fba4f572a990e
--- /dev/null
+++ b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityAnalysis.h
@@ -0,0 +1,60 @@
+//===-- PointerNullabilityAnalysis.h - Nullability analysis -----*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYANALYSIS_H
+#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYANALYSIS_H
+
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Type.h"
+#include "clang/Analysis/CFG.h"
+#include "clang/Analysis/FlowSensitive/CFGMatchSwitch.h"
+#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
+#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
+#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
+#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
+#include "clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityLattice.h"
+#include "clang/Analysis/FlowSensitive/Value.h"
+
+namespace clang::dataflow::nullability {
+
+/// Analyzes constructs in the source code to collect nullability information
+/// about pointers at each program point. This analysis and the corresponding
+/// lattice were based on the gradual analysis in 'Estep, Sam, Jenna Wise,
+/// Jonathan Aldrich, Éric Tanter, Johannes Bader, and Joshua Sunshine. "Gradual
+/// Program Analysis for Null Pointers." In 35th European Conference on
+/// Object-Oriented Programming (ECOOP). 2021.'
+class PointerNullabilityAnalysis
+    : public DataflowAnalysis<PointerNullabilityAnalysis,
+                              PointerNullabilityLattice> {
+public:
+  explicit PointerNullabilityAnalysis(ASTContext &Context, Environment &Env);
+
+  PointerNullabilityLattice initialElement() { return {}; }
+
+  void transfer(const CFGElement &Elt, PointerNullabilityLattice &Lattice,
+                Environment &Env);
+
+  void join(QualType Type, const Value &Val1, const Environment &Env1,
+            const Value &Val2, const Environment &Env2, Value &MergedVal,
+            Environment &MergedEnv) override;
+
+  // TODO: Add `compare` and `widen` overrides, as in
+  // https://github.com/google/crubit/blob/5f9ae7da1aa70e5a1c0e536baf32e5ec56a7c003/nullability/pointer_nullability_analysis.cc#L239
+  // Until then, this only handles loop-free code precisely: the framework's
+  // default comparison treats any two pointer values at a loop head as
+  // equivalent, so the loop body is not re-analyzed when a pointer's
+  // nullability changes along the back edge.
+
+private:
+  // Transfers (flow-sensitive) value properties through statements.
+  CFGMatchSwitch<TransferState<PointerNullabilityLattice>> ValueTransferer;
+};
+
+} // namespace clang::dataflow::nullability
+
+#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYANALYSIS_H
diff --git a/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityLattice.h b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityLattice.h
new file mode 100644
index 00000000000000..8556facb04a8a5
--- /dev/null
+++ b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityLattice.h
@@ -0,0 +1,24 @@
+//===-- PointerNullabilityLattice.h - Nullability lattice -------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYLATTICE_H
+#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYLATTICE_H
+
+#include "clang/Analysis/FlowSensitive/NoopLattice.h"
+
+namespace clang::dataflow::nullability {
+
+// The analysis does not need any lattice state of its own yet. Pointer
+// nullability is tracked as properties on `PointerValue`s in the `Environment`.
+// We'll use this lattice later to store non-flow-sensitive state and to
+// cache const member call results.
+using PointerNullabilityLattice = NoopLattice;
+
+} // namespace clang::dataflow::nullability
+
+#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYLATTICE_H
diff --git a/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityMatchers.h b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityMatchers.h
new file mode 100644
index 00000000000000..1b6f4612256323
--- /dev/null
+++ b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityMatchers.h
@@ -0,0 +1,34 @@
+//===-- PointerNullabilityMatchers.h - AST matchers -------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYMATCHERS_H
+#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYMATCHERS_H
+
+#include "clang/AST/Stmt.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchersInternal.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+#include "clang/Analysis/FlowSensitive/Models/Nullability/PointerNullability.h"
+
+namespace clang::dataflow::nullability {
+
+AST_MATCHER(QualType, isSupportedPointer) {
+  return isSupportedPointerType(Node);
+}
+
+AST_MATCHER(QualType, isSupportedRawPointer) {
+  return isSupportedRawPointerType(Node);
+}
+
+ast_matchers::internal::Matcher<Stmt> isPointerExpr();
+ast_matchers::internal::Matcher<Stmt> isNullPointerLiteral();
+ast_matchers::internal::Matcher<Stmt> isAddrOf();
+
+} // namespace clang::dataflow::nullability
+
+#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_POINTERNULLABILITYMATCHERS_H
diff --git a/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/ValueTransferer.h b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/ValueTransferer.h
new file mode 100644
index 00000000000000..56fccf74b3dfaa
--- /dev/null
+++ b/clang/include/clang/Analysis/FlowSensitive/Models/Nullability/ValueTransferer.h
@@ -0,0 +1,46 @@
+//===-- ValueTransferer.h - Transfer functions for nullability --*- 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
+//
+// This file contains transfer functions for flow-sensitive pointer nullability
+// analysis. We call this "value transferer" to distinguish from "type
+// transferer" (see
+// https://github.com/google/crubit/blob/7ef99d77672f716792140a5d5f6548cf7d638200/nullability/type_transferer.h#L13)
+// In contrast, the type transferer only handles flow-insensitive properties
+// derived from the types.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_VALUETRANSFERER_H
+#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_VALUETRANSFERER_H
+
+#include "clang/AST/Expr.h"
+#include "clang/Analysis/FlowSensitive/CFGMatchSwitch.h"
+#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
+#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
+#include "clang/Analysis/FlowSensitive/Models/Nullability/PointerNullabilityLattice.h"
+#include "clang/Analysis/FlowSensitive/Value.h"
+
+namespace clang::dataflow::nullability {
+
+// Returns a switch over CFG elements that can be used to transfer
+// (flow-sensitive) value properties, i.e. to update the analysis state to
+// account for the impact of the CFG element.
+CFGMatchSwitch<TransferState<PointerNullabilityLattice>> buildValueTransferer();
+
+// If `E` is already associated with a `PointerValue`, returns it.
+// Otherwise, associates a newly created `PointerValue` with `E` and returns it.
+// Returns null iff `E` is not a raw pointer expression.
+PointerValue *ensureRawPointerHasValue(const Expr *E, Environment &Env);
+
+// Initialize the null state of `PointerVal` based on the nullability of the
+// type of `E`.
+void initPointerFromTypeNullability(
+    PointerValue &PointerVal, const Expr *E,
+    TransferState<PointerNullabilityLattice> &State);
+
+} // namespace clang::dataflow::nullability
+
+#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_NULLABILITY_VALUETRANSFERER_H
diff --git a/clang/lib/Analysis/FlowSensitive/Models/CMakeLists.txt b/clang/lib/Analysis/FlowSensitive/Models/CMakeLists.txt
index 41ab2c6fa786c5..773a3d0da6f0bf 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/CMakeLists.txt
+++ b/clang/lib/Analysis/FlowSensitive/Models/CMakeLists.txt
@@ -12,3 +12,5 @@ add_clang_library(clangAnalysisFlowSensitiveModels
   clangASTMatchers
   clangBasic
   )
+
+add_subdirectory(Nullability)
diff --git a/clang/lib/Analysis/FlowSensitive/Models/Nullability/CMakeLists.txt b/clang/lib/Analysis/FlowSensitive/Models/Nullability/CMakeLists.txt
new file mode 100644
index 00000000000000..88b157f5efb241
--- /dev/null
+++ b/clang/lib/Analysis/FlowSensitive/Models/Nullability/CMakeLists.txt
@@ -0,0 +1,17 @@
+set(LLVM_LINK_COMPONENTS
+  Support
+  )
+
+add_clang_library(clangAnalysisFlowSensitiveNullability
+  PointerNullability.cpp
+  PointerNullabilityAnalysis.cpp
+  PointerNullabilityMatchers.cpp
+  ValueTransferer.cpp
+
+  LINK_LIBS
+  clangAnalysis
+  clangAnalysisFlowSensitive
+  clangAST
+  clangASTMatchers
+  clangBasic
+  )
diff --git a/clang/lib/Analysis/FlowSensitive/Models/Nullability/PointerNullability.cpp b/clang/lib/Analysis/FlowSensitive/Models/Nullability/PointerNullability.cpp
new file mode 100644
index 00000000000000..ae8a5221ca23b4
--- /dev/null
+++ b/clang/lib/Analysis/FlowSensitive/Models/Nullability/PointerNullability.cpp
@@ -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)
+          ? nullptr
+          : &cast<BoolValue>(FromNullableProp)->formula(),
+      isa<TopBoolValue>(NullProp) ? nullptr
+                                  : &cast<BoolValue>(NullProp)->formula(),
+  };
+}
+
+static bool tryCreatePointerNullState(PointerValue &PointerVal, Arena &A,
+                                      const Formula ...
[truncated]

``````````

</details>


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


More information about the cfe-commits mailing list