[PATCH] D142737: Updated getNullabilityAnnotation for checkers

Christopher Bazley via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 27 08:50:55 PST 2023


chrisbazley created this revision.
Herald added subscribers: steakhal, martong, JDevlieghere.
Herald added a reviewer: NoQ.
Herald added a project: All.
chrisbazley requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

NullabilityChecker and TrustNonnullChecker use
a pre-existing helper function,
getNullabilityAnnotation() to get a Nullability
value from a QualType.

That function is now updated to return
Nullability::Nullable if the given type is a
pointer to an _Optional-qualified type. The
purpose is to allow declarations such as

_Optional int *x;

to be treated (for static analysis purposes) as
equivalent to

int *_Nullable x;

thereby removing one barrier to adoption of the
_Optional type qualifier.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142737

Files:
  clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
  clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp


Index: clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
+++ clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
@@ -100,7 +100,21 @@
   return std::make_pair(VD, RHS);
 }
 
+bool pointeeIsOptional(QualType Type) {
+  if (const PointerType *PT = Type->getAs<PointerType>()) {
+    auto PointeeType = PT->getPointeeType();
+    if (PointeeType.isOptionalQualified()) {
+      return true;
+    }
+  }
+  return false;
+}
+
 Nullability getNullabilityAnnotation(QualType Type) {
+  if (pointeeIsOptional(Type)) {
+    return Nullability::Nullable;
+  }
+
   const auto *AttrType = Type->getAs<AttributedType>();
   if (!AttrType)
     return Nullability::Unspecified;
Index: clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
===================================================================
--- clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
+++ clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
@@ -62,6 +62,10 @@
   Nonnull
 };
 
+/// Find out whether the given type is a pointer to an optional value.
+/// If true then the pointer value should be treated as nullable.
+bool pointeeIsOptional(QualType Type);
+
 /// Get nullability annotation for a given type.
 Nullability getNullabilityAnnotation(QualType Type);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142737.492785.patch
Type: text/x-patch
Size: 1402 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230127/6a3171c0/attachment.bin>


More information about the cfe-commits mailing list