r362025 - [analyzer] ConditionBRVisitor: Remove duplicated code

Csaba Dabis via cfe-commits cfe-commits at lists.llvm.org
Wed May 29 13:18:08 PDT 2019


Author: charusso
Date: Wed May 29 13:18:07 2019
New Revision: 362025

URL: http://llvm.org/viewvc/llvm-project?rev=362025&view=rev
Log:
[analyzer] ConditionBRVisitor: Remove duplicated code

Summary: -

Reviewers: NoQ, george.karpenkov

Reviewed By: NoQ

Subscribers: cfe-commits, xazax.hun, baloghadamsoftware, szepet, a.sidorin,
             mikhail.ramalho, Szelethus, donat.nagy, dkrupp

Tags: #clang

Differential Revision: https://reviews.llvm.org/D58199

Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h
    cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h?rev=362025&r1=362024&r2=362025&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h Wed May 29 13:18:07 2019
@@ -207,6 +207,18 @@ public:
                          BugReporterContext &BRC, BugReport &R,
                          const ExplodedNode *N, bool TookTrue);
 
+  /// Tries to print the value of the given expression.
+  ///
+  /// \param CondVarExpr The expression to print its value.
+  /// \param Out The stream to print.
+  /// \param N The node where we encountered the condition.
+  /// \param TookTrue Whether we took the \c true branch of 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, raw_ostream &Out,
+                  const ExplodedNode *N, bool TookTrue, bool IsAssuming);
+
   bool patternMatch(const Expr *Ex,
                     const Expr *ParentEx,
                     raw_ostream &Out,

Modified: cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp?rev=362025&r1=362024&r2=362025&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp Wed May 29 13:18:07 2019
@@ -2190,17 +2190,7 @@ std::shared_ptr<PathDiagnosticPiece> Con
   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, Out, N, TookTrue, /*IsAssuming=*/true))
     return nullptr;
 
   const LocationContext *LCtx = N->getLocationContext();
@@ -2232,22 +2222,7 @@ std::shared_ptr<PathDiagnosticPiece> Con
 
   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<const llvm::APSInt *> 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, Out, N, TookTrue, IsAssuming))
     return nullptr;
 
   const LocationContext *LCtx = N->getLocationContext();
@@ -2271,6 +2246,36 @@ std::shared_ptr<PathDiagnosticPiece> Con
   return std::move(event);
 }
 
+bool ConditionBRVisitor::printValue(const Expr *CondVarExpr, raw_ostream &Out,
+                                    const ExplodedNode *N, bool TookTrue,
+                                    bool IsAssuming) {
+  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<const llvm::APSInt *> 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::GenericTrueMessage =
     "Assuming the condition is true";
 const char *const ConditionBRVisitor::GenericFalseMessage =




More information about the cfe-commits mailing list