diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h +++ b/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); diff --git a/clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp b/clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp --- a/clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp +++ b/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()) { + 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(); if (!AttrType) return Nullability::Unspecified;