[cfe-commits] r137714 - /cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp

Ted Kremenek kremenek at apple.com
Tue Aug 16 03:57:37 PDT 2011


Author: kremenek
Date: Tue Aug 16 05:57:37 2011
New Revision: 137714

URL: http://llvm.org/viewvc/llvm-project?rev=137714&view=rev
Log:
[analyzer] Enhance ConditionVisitor to handle arbitrary ValueDecls in binary expressions, and also handle inverting the order of comparison when the named decl appears on the RHS.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp?rev=137714&r1=137713&r2=137714&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp Tue Aug 16 05:57:37 2011
@@ -507,7 +507,7 @@
                                      const bool tookTrue,
                                      BugReporterContext &BRC);
   
-  void patternMatch(const Expr *Ex,
+  bool patternMatch(const Expr *Ex,
                     llvm::raw_ostream &Out,
                     BugReporterContext &BRC);
 };
@@ -615,15 +615,19 @@
   }
 }
 
-void ConditionVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
+bool ConditionVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
                                     BugReporterContext &BRC) {
   const Expr *OriginalExpr = Ex;
   Ex = Ex->IgnoreParenCasts();
 
   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
-    if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
-      Out << VD->getDeclName().getAsString();
-    return;
+    const bool quotes = isa<VarDecl>(DR->getDecl());
+    if (quotes)
+      Out << '\'';
+    Out << DR->getDecl()->getDeclName().getAsString();
+    if (quotes)
+      Out << '\'';
+    return quotes;
   }
   
   if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
@@ -631,19 +635,21 @@
     if (OriginalTy->isPointerType()) {
       if (IL->getValue() == 0) {
         Out << "null";
-        return;
+        return false;
       }
     }
     else if (OriginalTy->isObjCObjectPointerType()) {
       if (IL->getValue() == 0) {
         Out << "nil";
-        return;
+        return false;
       }
     }
     
     Out << IL->getValue();
-    return;
+    return false;
   }
+  
+  return false;
 }
 
 PathDiagnosticPiece *
@@ -652,23 +658,38 @@
                                 const bool tookTrue,
                                 BugReporterContext &BRC) {
   
+  bool shouldInvert = false;
+  
   llvm::SmallString<128> LhsString, RhsString;
   {
     llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);  
-    patternMatch(BExpr->getLHS(), OutLHS, BRC);
-    patternMatch(BExpr->getRHS(), OutRHS, BRC);
+    const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC);
+    const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC);
+    
+    shouldInvert = !isVarLHS && isVarRHS;    
   }
   
   if (LhsString.empty() || RhsString.empty())
     return 0;
 
+  // Should we invert the strings if the LHS is not a variable name?
+  
   llvm::SmallString<256> buf;
   llvm::raw_svector_ostream Out(buf);
-  Out << "Assuming " << LhsString << " is ";
+  Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
 
   // Do we need to invert the opcode?
   BinaryOperator::Opcode Op = BExpr->getOpcode();
-  
+    
+  if (shouldInvert)
+    switch (Op) {
+      default: break;
+      case BO_LT: Op = BO_GT; break;
+      case BO_GT: Op = BO_LT; break;
+      case BO_LE: Op = BO_GE; break;
+      case BO_GE: Op = BO_LE; break;
+    }
+
   if (!tookTrue)
     switch (Op) {
       case BO_EQ: Op = BO_NE; break;
@@ -693,7 +714,7 @@
       break;
   }
   
-  Out << RhsString;
+  Out << (shouldInvert ? LhsString : RhsString);
 
   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager());
   return new PathDiagnosticEventPiece(Loc, Out.str());





More information about the cfe-commits mailing list