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);
 


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);
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D142737: Update... Christopher Bazley via Phabricator via cfe-commits

Reply via email to