blob: 1f8fc70d1694482db48dd7044cd949793c2ade55 [file] [log] [blame]
Wei Yi Tee8b58e192022-08-02 10:15:40 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef CRUBIT_NULLABILITY_VERIFICATION_POINTER_NULLABILITY_H_
6#define CRUBIT_NULLABILITY_VERIFICATION_POINTER_NULLABILITY_H_
7
8#include <utility>
9
Wei Yi Tee9c161612022-08-19 14:20:02 -070010#include "clang/AST/ASTContext.h"
Wei Yi Tee8b58e192022-08-02 10:15:40 -070011#include "clang/AST/Expr.h"
12#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
13#include "clang/Analysis/FlowSensitive/Value.h"
Wei Yi Tee9c161612022-08-19 14:20:02 -070014#include "clang/Basic/Specifiers.h"
Wei Yi Tee8b58e192022-08-02 10:15:40 -070015
16namespace clang {
17namespace tidy {
18namespace nullability {
19
Wei Yi Tee9c161612022-08-19 14:20:02 -070020/// Returns the `NullabilityKind` corresponding to the nullability annotation on
21/// `Type` if present. Otherwise, returns `NullabilityKind::Unspecified`.
22NullabilityKind getNullabilityKind(QualType Type, ASTContext& Ctx);
23
24/// Returns the `PointerValue` allocated to `PointerExpr` if available.
25/// Otherwise, returns nullptr.
Wei Yi Tee8374c8f2022-08-11 01:18:41 -070026dataflow::PointerValue* getPointerValueFromExpr(
27 const Expr* PointerExpr, const dataflow::Environment& Env);
Wei Yi Tee8b58e192022-08-02 10:15:40 -070028
Wei Yi Tee721ee972022-08-11 01:14:54 -070029/// Returns the properties representing the nullness information of a pointer.
30///
31/// The first boolean indicates if the pointer's nullability is known.
32/// The second boolean indicates if the pointer's value is not null.
33std::pair<dataflow::AtomicBoolValue&, dataflow::AtomicBoolValue&>
34getPointerNullState(const dataflow::PointerValue& PointerVal,
35 const dataflow::Environment& Env);
36
Wei Yi Tee721ee972022-08-11 01:14:54 -070037/// Sets the nullness properties on `PointerVal` if not already initialised.
38///
39/// The boolean properties may be constrained by specifying `KnownConstraint`
40/// and `NotNullConstraint`. Otherwise, the properties are set to freshly
41/// created atomic booleans.
42void initPointerNullState(dataflow::PointerValue& PointerVal,
43 dataflow::Environment& Env,
44 dataflow::BoolValue* KnownConstraint = nullptr,
Wei Yi Tee8b58e192022-08-02 10:15:40 -070045 dataflow::BoolValue* NotNullConstraint = nullptr);
46
Wei Yi Tee8374c8f2022-08-11 01:18:41 -070047/// Sets the nullness properties on `PointerVal` representing a nullptr if not
48/// already initialised.
49///
50/// `Known` is constrained to true, `NotNull` is constrained to false.
51inline void initNullPointer(dataflow::PointerValue& PointerVal,
52 dataflow::Environment& Env) {
53 initPointerNullState(PointerVal, Env,
54 /*KnownConstraint=*/&Env.getBoolLiteralValue(true),
55 /*NotNullConstraint=*/&Env.getBoolLiteralValue(false));
56}
57
58/// Sets the nullness properties on `PointerVal` representing a pointer that is
59/// not null if not already initialised.
60///
61/// `Known` is constrained to true, `NotNull` is constrained to true.
62inline void initNotNullPointer(dataflow::PointerValue& PointerVal,
63 dataflow::Environment& Env) {
64 initPointerNullState(PointerVal, Env,
65 /*KnownConstraint=*/&Env.getBoolLiteralValue(true),
66 /*NotNullConstraint=*/&Env.getBoolLiteralValue(true));
67}
68
69/// Sets the nullness properties on `PointerVal` representing a pointer that is
70/// nullable if not already initialised.
71///
72/// `Known` is constrained to true, `NotNull` is unconstrained.
73inline void initNullablePointer(dataflow::PointerValue& PointerVal,
74 dataflow::Environment& Env) {
75 initPointerNullState(PointerVal, Env,
76 /*KnownConstraint=*/&Env.getBoolLiteralValue(true));
77}
78
79/// Sets the nullness properties on `PointerVal` representing a pointer with
80/// unknown nullability if not already initialised.
81///
82/// `Known` is constrained to false, `NotNull` is unconstrained.
83inline void initUnknownPointer(dataflow::PointerValue& PointerVal,
84 dataflow::Environment& Env) {
85 initPointerNullState(PointerVal, Env,
86 /*KnownConstraint=*/&Env.getBoolLiteralValue(false));
87}
88
Wei Yi Tee036efdf2022-08-19 14:16:26 -070089/// Returns true if there is evidence that `PointerVal` may hold a nullptr.
90bool isNullable(const dataflow::PointerValue& PointerVal,
91 const dataflow::Environment& Env);
92
Wei Yi Tee8b58e192022-08-02 10:15:40 -070093} // namespace nullability
94} // namespace tidy
95} // namespace clang
96
97#endif // CRUBIT_NULLABILITY_VERIFICATION_POINTER_NULLABILITY_H_