Index: include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h =================================================================== --- include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h +++ include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h @@ -213,6 +213,18 @@ const bool tookTrue, BugReporterContext &BRC, BugReport &R, const ExplodedNode *N); + /// Tries to print the value of the given expression. + /// + /// \param CondVarExpr The expression to print its value. + /// \param TookTrue Whether we took the \c true branch of the condition. + /// \param Out The stream to print. + /// \param N The node where we encountered the condition. + /// + /// \return Whether the print was successful. (The printing is successful if + // we model the value and we could obtain it.) + bool printValue(const Expr *CondVarExpr, const bool TookTrue, + raw_ostream &Out, const ExplodedNode *N); + bool patternMatch(const Expr *Ex, const Expr *ParentEx, raw_ostream &Out, Index: lib/StaticAnalyzer/Core/BugReporterVisitors.cpp =================================================================== --- lib/StaticAnalyzer/Core/BugReporterVisitors.cpp +++ lib/StaticAnalyzer/Core/BugReporterVisitors.cpp @@ -2150,18 +2150,7 @@ SmallString<256> buf; llvm::raw_svector_ostream Out(buf); Out << "Assuming " << LhsString << " is "; - - QualType Ty = CondVarExpr->getType(); - - if (Ty->isPointerType()) - Out << (tookTrue ? "not null" : "null"); - else if (Ty->isObjCObjectPointerType()) - Out << (tookTrue ? "not nil" : "nil"); - else if (Ty->isBooleanType()) - Out << (tookTrue ? "true" : "false"); - else if (Ty->isIntegralOrEnumerationType()) - Out << (tookTrue ? "non-zero" : "zero"); - else + if (!printValue(CondVarExpr, tookTrue, Out, N)) return nullptr; const LocationContext *LCtx = N->getLocationContext(); @@ -2193,23 +2182,7 @@ llvm::raw_svector_ostream Out(Buf); Out << (IsAssuming ? "Assuming '" : "'") << VD->getDeclName() << "' is "; - - QualType Ty = VD->getType(); - - if (Ty->isPointerType()) - Out << (tookTrue ? "non-null" : "null"); - else if (Ty->isObjCObjectPointerType()) - Out << (tookTrue ? "non-nil" : "nil"); - else if (Ty->isScalarType()) { - Optional IntValue; - if (!IsAssuming) - IntValue = getConcreteIntegerValue(DRE, N); - - if (IsAssuming || !IntValue.hasValue()) - Out << (tookTrue ? "not equal to 0" : "0"); - else - Out << *IntValue.getValue(); - } else + if (!printValue(DRE, tookTrue, Out, N)) return nullptr; const LocationContext *LCtx = N->getLocationContext(); @@ -2229,6 +2202,36 @@ return std::move(event); } +bool ConditionBRVisitor::printValue(const Expr *CondVarExpr, + const bool TookTrue, raw_ostream &Out, + const ExplodedNode *N) { + QualType Ty = CondVarExpr->getType(); + + if (Ty->isPointerType()) { + Out << (TookTrue ? "non-null" : "null"); + return true; + } + + if (Ty->isObjCObjectPointerType()) { + Out << (TookTrue ? "non-nil" : "nil"); + return true; + } + + if (!Ty->isIntegralOrEnumerationType()) + return false; + + Optional IntValue; + if (!IsAssuming) + IntValue = getConcreteIntegerValue(CondVarExpr, N); + + if (IsAssuming || !IntValue.hasValue()) + Out << (TookTrue ? "not equal to 0" : "0"); + else + Out << *IntValue.getValue(); + + return true; +} + const char *const ConditionBRVisitor::TrueMessage = "The condition is true"; const char *const ConditionBRVisitor::FalseMessage = "The condition is false"; const char *const ConditionBRVisitor::AssumingTrueMessage =