xazax.hun updated this revision to Diff 34458. xazax.hun added a comment. - Updated to latest trunk - Reworded the diagnostic messages
http://reviews.llvm.org/D12619 Files: lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
Index: lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp =================================================================== --- lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp +++ 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<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 @@ 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<check::Bind, check::PreCall, check::PreStmt<ReturnStmt>, @@ -176,7 +174,6 @@ 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 @@ 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 @@ 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 @@ 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; } - return RetNullability; + 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 Nullability::Unspecified; } /// Calculate the nullability of the result of a message expr based on the
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits