r247423 - [Static Analyzer] Minor cleanups for the nullability checker.

Gabor Horvath via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 11 09:29:05 PDT 2015


Author: xazax
Date: Fri Sep 11 11:29:05 2015
New Revision: 247423

URL: http://llvm.org/viewvc/llvm-project?rev=247423&view=rev
Log:
[Static Analyzer] Minor cleanups for the nullability checker.

Differential Revision: http://reviews.llvm.org/D12619


Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp?rev=247423&r1=247422&r2=247423&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp Fri Sep 11 11:29:05 2015
@@ -58,12 +58,12 @@ enum class Nullability : char {
 /// the nullability of the receiver or the nullability of the return type of the
 /// method, depending on which is more nullable. Contradicted is considered to
 /// be the most nullable, to avoid false positive results.
-static Nullability getMostNullable(Nullability Lhs, Nullability Rhs) {
+Nullability getMostNullable(Nullability Lhs, Nullability Rhs) {
   return static_cast<Nullability>(
       std::min(static_cast<char>(Lhs), static_cast<char>(Rhs)));
 }
 
-static const char *getNullabilityString(Nullability Nullab) {
+const char *getNullabilityString(Nullability Nullab) {
   switch (Nullab) {
   case Nullability::Contradicted:
     return "contradicted";
@@ -74,7 +74,7 @@ static const char *getNullabilityString(
   case Nullability::Nonnull:
     return "nonnull";
   }
-  assert(false);
+  llvm_unreachable("Unexpected enumeration.");
   return "";
 }
 
@@ -89,19 +89,17 @@ enum class ErrorKind : int {
   NullablePassedToNonnull
 };
 
-const char *ErrorMessages[] = {"Null pointer is assigned to a pointer which "
-                               "has _Nonnull type",
-                               "Null pointer is passed to a parameter which is "
-                               "marked as _Nonnull",
-                               "Null pointer is returned from a function that "
-                               "has _Nonnull return type",
-                               "Nullable pointer is assigned to a pointer "
-                               "which has _Nonnull type",
-                               "Nullable pointer is returned from a function "
-                               "that has _Nonnull return type",
-                               "Nullable pointer is dereferenced",
-                               "Nullable pointer is passed to a parameter "
-                               "which is marked as _Nonnull"};
+const char *const ErrorMessages[] = {
+    "Null is assigned to a pointer which is expected to have non-null value",
+    "Null passed to a callee that requires a non-null argument",
+    "Null is returned from a function that is expected to return a non-null "
+    "value",
+    "Nullable pointer is assigned to a pointer which is expected to have "
+    "non-null value",
+    "Nullable pointer is returned from a function that is expected to return a "
+    "non-null value",
+    "Nullable pointer is dereferenced",
+    "Nullable pointer is passed to a calle that requires a non-null argument"};
 
 class NullabilityChecker
     : public Checker<check::Bind, check::PreCall, check::PreStmt<ReturnStmt>,
@@ -176,7 +174,6 @@ private:
     if (!BT)
       BT.reset(new BugType(this, "Nullability", "Memory error"));
     const char *Msg = ErrorMessages[static_cast<int>(Error)];
-    assert(Msg);
     std::unique_ptr<BugReport> R(new BugReport(*BT, Msg, N));
     if (Region) {
       R->markInteresting(Region);
@@ -262,7 +259,7 @@ static const SymbolicRegion *getTrackReg
   if (CheckSuperRegion) {
     if (auto FieldReg = Region->getAs<FieldRegion>())
       return dyn_cast<SymbolicRegion>(FieldReg->getSuperRegion());
-    else if (auto ElementReg = Region->getAs<ElementRegion>())
+    if (auto ElementReg = Region->getAs<ElementRegion>())
       return dyn_cast<SymbolicRegion>(ElementReg->getSuperRegion());
   }
 
@@ -272,12 +269,12 @@ static const SymbolicRegion *getTrackReg
 PathDiagnosticPiece *NullabilityChecker::NullabilityBugVisitor::VisitNode(
     const ExplodedNode *N, const ExplodedNode *PrevN, BugReporterContext &BRC,
     BugReport &BR) {
-  ProgramStateRef state = N->getState();
-  ProgramStateRef statePrev = PrevN->getState();
+  ProgramStateRef State = N->getState();
+  ProgramStateRef StatePrev = PrevN->getState();
 
-  const NullabilityState *TrackedNullab = state->get<NullabilityMap>(Region);
+  const NullabilityState *TrackedNullab = State->get<NullabilityMap>(Region);
   const NullabilityState *TrackedNullabPrev =
-      statePrev->get<NullabilityMap>(Region);
+      StatePrev->get<NullabilityMap>(Region);
   if (!TrackedNullab)
     return nullptr;
 
@@ -645,34 +642,31 @@ void NullabilityChecker::checkPostCall(c
 
 static Nullability getReceiverNullability(const ObjCMethodCall &M,
                                           ProgramStateRef State) {
-  Nullability RetNullability = Nullability::Unspecified;
   if (M.isReceiverSelfOrSuper()) {
     // For super and super class receivers we assume that the receiver is
     // nonnull.
-    RetNullability = Nullability::Nonnull;
-  } else {
-    // Otherwise look up nullability in the state.
-    SVal Receiver = M.getReceiverSVal();
-    auto ValueRegionSVal = Receiver.getAs<loc::MemRegionVal>();
-    if (ValueRegionSVal) {
-      const MemRegion *SelfRegion = ValueRegionSVal->getRegion();
-      assert(SelfRegion);
-
-      const NullabilityState *TrackedSelfNullability =
-          State->get<NullabilityMap>(SelfRegion);
-      if (TrackedSelfNullability) {
-        RetNullability = TrackedSelfNullability->getValue();
-      }
-    }
-    if (auto DefOrUnknown = Receiver.getAs<DefinedOrUnknownSVal>()) {
-      // If the receiver is constrained to be nonnull, assume that it is nonnull
-      // regardless of its type.
-      NullConstraint Nullness = getNullConstraint(*DefOrUnknown, State);
-      if (Nullness == NullConstraint::IsNotNull)
-        RetNullability = Nullability::Nonnull;
-    }
+    return Nullability::Nonnull;
+  }
+  // Otherwise look up nullability in the state.
+  SVal Receiver = M.getReceiverSVal();
+  if (auto DefOrUnknown = Receiver.getAs<DefinedOrUnknownSVal>()) {
+    // If the receiver is constrained to be nonnull, assume that it is nonnull
+    // regardless of its type.
+    NullConstraint Nullness = getNullConstraint(*DefOrUnknown, State);
+    if (Nullness == NullConstraint::IsNotNull)
+      return Nullability::Nonnull;
+  }
+  auto ValueRegionSVal = Receiver.getAs<loc::MemRegionVal>();
+  if (ValueRegionSVal) {
+    const MemRegion *SelfRegion = ValueRegionSVal->getRegion();
+    assert(SelfRegion);
+
+    const NullabilityState *TrackedSelfNullability =
+        State->get<NullabilityMap>(SelfRegion);
+    if (TrackedSelfNullability)
+      return TrackedSelfNullability->getValue();
   }
-  return RetNullability;
+  return Nullability::Unspecified;
 }
 
 /// Calculate the nullability of the result of a message expr based on the




More information about the cfe-commits mailing list