Index: cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp =================================================================== --- cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp +++ cfe/trunk/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp @@ -58,12 +58,12 @@ /// 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( std::min(static_cast(Lhs), static_cast(Rhs))); } -static const char *getNullabilityString(Nullability Nullab) { +const char *getNullabilityString(Nullability Nullab) { switch (Nullab) { case Nullability::Contradicted: return "contradicted"; @@ -74,7 +74,7 @@ case Nullability::Nonnull: return "nonnull"; } - assert(false); + llvm_unreachable("Unexpected enumeration."); return ""; } @@ -89,19 +89,17 @@ 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, @@ -176,7 +174,6 @@ if (!BT) BT.reset(new BugType(this, "Nullability", "Memory error")); const char *Msg = ErrorMessages[static_cast(Error)]; - assert(Msg); std::unique_ptr R(new BugReport(*BT, Msg, N)); if (Region) { R->markInteresting(Region); @@ -262,7 +259,7 @@ if (CheckSuperRegion) { if (auto FieldReg = Region->getAs()) return dyn_cast(FieldReg->getSuperRegion()); - else if (auto ElementReg = Region->getAs()) + if (auto ElementReg = Region->getAs()) return dyn_cast(ElementReg->getSuperRegion()); } @@ -272,12 +269,12 @@ 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(Region); + const NullabilityState *TrackedNullab = State->get(Region); const NullabilityState *TrackedNullabPrev = - statePrev->get(Region); + StatePrev->get(Region); if (!TrackedNullab) return nullptr; @@ -645,34 +642,31 @@ 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(); - if (ValueRegionSVal) { - const MemRegion *SelfRegion = ValueRegionSVal->getRegion(); - assert(SelfRegion); - - const NullabilityState *TrackedSelfNullability = - State->get(SelfRegion); - if (TrackedSelfNullability) { - RetNullability = TrackedSelfNullability->getValue(); - } - } - if (auto DefOrUnknown = Receiver.getAs()) { - // 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()) { + // 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(); + if (ValueRegionSVal) { + const MemRegion *SelfRegion = ValueRegionSVal->getRegion(); + assert(SelfRegion); + + const NullabilityState *TrackedSelfNullability = + State->get(SelfRegion); + if (TrackedSelfNullability) + return TrackedSelfNullability->getValue(); } - return RetNullability; + return Nullability::Unspecified; } /// Calculate the nullability of the result of a message expr based on the