[PATCH] D10892: warning when inner condition is identical

Daniel Marjamäki daniel.marjamaki at evidente.se
Tue Aug 4 01:11:15 PDT 2015


danielmarjamaki updated this revision to Diff 31302.
danielmarjamaki added a comment.

Minor cleanup


http://reviews.llvm.org/D10892

Files:
  lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
  test/Analysis/identical-expressions.cpp

Index: test/Analysis/identical-expressions.cpp
===================================================================
--- test/Analysis/identical-expressions.cpp
+++ test/Analysis/identical-expressions.cpp
@@ -1530,3 +1530,37 @@
     c = 0LL;
   }
 }
+
+
+///////////////////////////////////////////////////////////////////////////////
+// Identical inner conditions
+
+void test_warn_inner_if_1(int x) {
+  if (x == 1) {
+    if (x == 1) // expected-warning {{inner condition is identical to previous condition}}
+      ;
+  }
+
+  // FIXME: Should warn here. The warning is currently not emitted because there
+  // is code between the conditions.
+  if (x == 1) {
+    int y = x;
+    if (x == 1)
+      ;
+  }
+}
+
+void test_nowarn_inner_if_1(int x) {
+  // Don't warn when condition has side effects.
+  if (x++ == 1) {
+    if (x++ == 1)
+      ;
+  }
+
+  // Don't warn when x is changed before inner condition.
+  if (x < 10) {
+    x++;
+    if (x < 10)
+      ;
+  }
+}
Index: lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
===================================================================
--- lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
+++ lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
@@ -108,6 +108,24 @@
   const Stmt *Stmt1 = I->getThen();
   const Stmt *Stmt2 = I->getElse();
 
+  // Check for identical inner condition:
+  //
+  // if (x<10) {
+  //   if (x<10) {
+  //   ..
+  if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(Stmt1)) {
+    if (!CS->body_empty()) {
+      const IfStmt *InnerIf = dyn_cast<IfStmt>(*CS->body_begin());
+      if (InnerIf && isIdenticalStmt(AC->getASTContext(), I->getCond(), InnerIf->getCond(), /*ignoreSideEffects=*/ false)) {
+        PathDiagnosticLocation ELoc(InnerIf->getCond(), BR.getSourceManager(), AC);
+        BR.EmitBasicReport(AC->getDecl(), Checker, "Identical conditions",
+          categories::LogicError,
+          "inner condition is identical to previous condition",
+          ELoc);
+      }
+    }
+  }
+
   // Check for identical conditions:
   //
   // if (b) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10892.31302.patch
Type: text/x-patch
Size: 2065 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150804/6360a4d9/attachment.bin>


More information about the cfe-commits mailing list