The core.alpha.IdenticalExpr checker warns when some expressions are identical. 
For example when a if condition and else if condition are identical there is a 
warning.

This patch makes Clang warn also when an inner if condition is identical to the 
outer if condition.

http://reviews.llvm.org/D10892

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

Index: lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
===================================================================
--- lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
+++ lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
@@ -108,6 +108,25 @@
   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(), false)) {
+        SourceRange Sr = I->getCond()->getSourceRange();
+        PathDiagnosticLocation ELoc(InnerIf->getCond(), BR.getSourceManager(), 
AC);
+        BR.EmitBasicReport(AC->getDecl(), Checker, "Identical conditions",
+          categories::LogicError,
+          "inner condition is identical to previous condition",
+          ELoc, Sr);
+      }
+    }
+  }
+
   // Check for identical conditions:
   //
   // if (b) {
Index: test/Analysis/identical-expressions.cpp
===================================================================
--- test/Analysis/identical-expressions.cpp
+++ test/Analysis/identical-expressions.cpp
@@ -1530,3 +1530,15 @@
     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}}
+      ;
+  }
+}
+

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
Index: lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
===================================================================
--- lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
+++ lib/StaticAnalyzer/Checkers/IdenticalExprChecker.cpp
@@ -108,6 +108,25 @@
   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(), false)) {
+        SourceRange Sr = I->getCond()->getSourceRange();
+        PathDiagnosticLocation ELoc(InnerIf->getCond(), BR.getSourceManager(), AC);
+        BR.EmitBasicReport(AC->getDecl(), Checker, "Identical conditions",
+          categories::LogicError,
+          "inner condition is identical to previous condition",
+          ELoc, Sr);
+      }
+    }
+  }
+
   // Check for identical conditions:
   //
   // if (b) {
Index: test/Analysis/identical-expressions.cpp
===================================================================
--- test/Analysis/identical-expressions.cpp
+++ test/Analysis/identical-expressions.cpp
@@ -1530,3 +1530,15 @@
     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}}
+      ;
+  }
+}
+
_______________________________________________
cfe-commits mailing list
cfe-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to