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,9 @@ const bool tookTrue, BugReporterContext &BRC, BugReport &R, const ExplodedNode *N); + StringRef getValueStr(const Expr *CondVarExpr, const bool tookTrue, + 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 @@ -2149,20 +2149,8 @@ // test/Analysis/inlining/path-notes.c) 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 - return nullptr; + Out << "Assuming " << LhsString << " is " + << getValueStr(CondVarExpr, tookTrue, N); const LocationContext *LCtx = N->getLocationContext(); PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx); @@ -2192,25 +2180,8 @@ SmallString<256> Buf; 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 - return nullptr; + Out << (IsAssuming ? "Assuming '" : "'") << VD->getDeclName() << "' is " + << getValueStr(DRE, tookTrue, N); const LocationContext *LCtx = N->getLocationContext(); PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); @@ -2229,6 +2200,28 @@ return std::move(event); } +StringRef ConditionBRVisitor::getValueStr(const Expr *CondVarExpr, + const bool tookTrue, + const ExplodedNode *N) { + QualType Ty = CondVarExpr->getType(); + + if (Ty->isPointerType()) + return tookTrue ? "non-null" : "null"; + + if (Ty->isObjCObjectPointerType()) + return tookTrue ? "non-nil" : "nil"; + + // At this point it is an int or enum. + Optional IntValue; + if (!IsAssuming) + IntValue = getConcreteIntegerValue(CondVarExpr, N); + + if (IsAssuming || !IntValue.hasValue()) + return tookTrue ? "not equal to 0" : "0"; + + return Twine(IntValue.getValue()->getZExtValue()).str(); +} + const char *const ConditionBRVisitor::TrueMessage = "The condition is true"; const char *const ConditionBRVisitor::FalseMessage = "The condition is false"; const char *const ConditionBRVisitor::AssumingTrueMessage =