Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 1 | // 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 Tee | 9c16161 | 2022-08-19 14:20:02 -0700 | [diff] [blame] | 10 | #include "clang/AST/ASTContext.h" |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 11 | #include "clang/AST/Expr.h" |
| 12 | #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" |
| 13 | #include "clang/Analysis/FlowSensitive/Value.h" |
Wei Yi Tee | 9c16161 | 2022-08-19 14:20:02 -0700 | [diff] [blame] | 14 | #include "clang/Basic/Specifiers.h" |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace nullability { |
| 19 | |
Wei Yi Tee | 9c16161 | 2022-08-19 14:20:02 -0700 | [diff] [blame] | 20 | /// Returns the `NullabilityKind` corresponding to the nullability annotation on |
| 21 | /// `Type` if present. Otherwise, returns `NullabilityKind::Unspecified`. |
| 22 | NullabilityKind getNullabilityKind(QualType Type, ASTContext& Ctx); |
| 23 | |
| 24 | /// Returns the `PointerValue` allocated to `PointerExpr` if available. |
| 25 | /// Otherwise, returns nullptr. |
Wei Yi Tee | 8374c8f | 2022-08-11 01:18:41 -0700 | [diff] [blame] | 26 | dataflow::PointerValue* getPointerValueFromExpr( |
| 27 | const Expr* PointerExpr, const dataflow::Environment& Env); |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 28 | |
Wei Yi Tee | 721ee97 | 2022-08-11 01:14:54 -0700 | [diff] [blame] | 29 | /// 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. |
| 33 | std::pair<dataflow::AtomicBoolValue&, dataflow::AtomicBoolValue&> |
| 34 | getPointerNullState(const dataflow::PointerValue& PointerVal, |
| 35 | const dataflow::Environment& Env); |
| 36 | |
Wei Yi Tee | 721ee97 | 2022-08-11 01:14:54 -0700 | [diff] [blame] | 37 | /// 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. |
| 42 | void initPointerNullState(dataflow::PointerValue& PointerVal, |
| 43 | dataflow::Environment& Env, |
| 44 | dataflow::BoolValue* KnownConstraint = nullptr, |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 45 | dataflow::BoolValue* NotNullConstraint = nullptr); |
| 46 | |
Wei Yi Tee | 8374c8f | 2022-08-11 01:18:41 -0700 | [diff] [blame] | 47 | /// 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. |
| 51 | inline 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. |
| 62 | inline 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. |
| 73 | inline 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. |
| 83 | inline void initUnknownPointer(dataflow::PointerValue& PointerVal, |
| 84 | dataflow::Environment& Env) { |
| 85 | initPointerNullState(PointerVal, Env, |
| 86 | /*KnownConstraint=*/&Env.getBoolLiteralValue(false)); |
| 87 | } |
| 88 | |
Wei Yi Tee | 036efdf | 2022-08-19 14:16:26 -0700 | [diff] [blame] | 89 | /// Returns true if there is evidence that `PointerVal` may hold a nullptr. |
| 90 | bool isNullable(const dataflow::PointerValue& PointerVal, |
| 91 | const dataflow::Environment& Env); |
| 92 | |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 93 | } // namespace nullability |
| 94 | } // namespace tidy |
| 95 | } // namespace clang |
| 96 | |
| 97 | #endif // CRUBIT_NULLABILITY_VERIFICATION_POINTER_NULLABILITY_H_ |