[cfe-commits] r98795 - in /cfe/trunk: lib/Checker/CheckDeadStores.cpp test/Analysis/dead-stores.c

Ted Kremenek kremenek at apple.com
Wed Mar 17 18:22:39 PDT 2010


Author: kremenek
Date: Wed Mar 17 20:22:39 2010
New Revision: 98795

URL: http://llvm.org/viewvc/llvm-project?rev=98795&view=rev
Log:
Tweak dead stores checker to not emit a warning when initialization
a scalar variable with a scalar parameter.  This is a
form of defensive programming.  If the variable is unused,
it will be caused by -Wunused-variable.

Modified:
    cfe/trunk/lib/Checker/CheckDeadStores.cpp
    cfe/trunk/test/Analysis/dead-stores.c

Modified: cfe/trunk/lib/Checker/CheckDeadStores.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/CheckDeadStores.cpp?rev=98795&r1=98794&r2=98795&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/CheckDeadStores.cpp (original)
+++ cfe/trunk/lib/Checker/CheckDeadStores.cpp Wed Mar 17 20:22:39 2010
@@ -220,16 +220,25 @@
               if (E->isConstantInitializer(Ctx))
                 return;
 
-              // Special case: check for initializations from constant
-              //  variables.
-              //
-              //  e.g. extern const int MyConstant;
-              //       int x = MyConstant;
-              //
               if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
-                if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
+                if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
+                  // Special case: check for initialization from constant
+                  //  variables.
+                  //
+                  //  e.g. extern const int MyConstant;
+                  //       int x = MyConstant;
+                  //
                   if (VD->hasGlobalStorage() &&
-                      VD->getType().isConstQualified()) return;
+                      VD->getType().isConstQualified())
+                    return;
+                  // Special case: check for initialization from scalar
+                  //  parameters.  This is often a form of defensive
+                  //  programming.  Non-scalars are still an error since
+                  //  because it more likely represents an actual algorithmic
+                  //  bug.
+                  if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
+                    return;
+                }
 
               Report(V, DeadInit, V->getLocation(), E->getSourceRange());
             }

Modified: cfe/trunk/test/Analysis/dead-stores.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dead-stores.c?rev=98795&r1=98794&r2=98795&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Wed Mar 17 20:22:39 2010
@@ -1,18 +1,18 @@
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
 
 void f1() {
-  int k, y;
+  int k, y; // expected-warning{{unused variable 'k'}} expected-warning{{unused variable 'y'}}
   int abc=1;
-  long idx=abc+3*5; // expected-warning {{never read}}
+  long idx=abc+3*5; // expected-warning {{never read}} expected-warning{{unused variable 'idx'}}
 }
 
 void f2(void *b) {
  char *c = (char*)b; // no-warning
- char *d = b+1; // expected-warning {{never read}}
+ char *d = b+1; // expected-warning {{never read}} expected-warning{{unused variable 'd'}}
  printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \
  // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
 }
@@ -40,7 +40,7 @@
 void f5() {
 
   int x = 4; // no-warning
-  int *p = &x; // expected-warning{{never read}}
+  int *p = &x; // expected-warning{{never read}} expected-warning{{unused variable 'p'}}
 
 }
 
@@ -105,13 +105,20 @@
 }
 
 int f12a(int y) {
-  int x = y;  // expected-warning{{never read}}
+  int x = y;  // expected-warning{{unused variable 'x'}}
   return 1;
 }
 int f12b(int y) {
   int x __attribute__((unused)) = y;  // no-warning
   return 1;
 }
+int f12c(int y) {
+  // Allow initialiation of scalar variables by parameters as a form of
+  // defensive programming.
+  int x = y;  // no-warning
+  x = 1;
+  return x;
+}
 
 // Filed with PR 2630.  This code should produce no warnings.
 int f13(void)
@@ -138,7 +145,7 @@
 // Test case for <rdar://problem/6248086>
 void f15(unsigned x, unsigned y) {
   int count = x * y;   // no-warning
-  int z[count];
+  int z[count]; // expected-warning{{unused variable 'z'}}
 }
 
 int f16(int x) {
@@ -367,7 +374,7 @@
 }
 
 void f23_pos(int argc, char **argv) {
-  int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}}
+  int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}} expected-warning{{unused variable 'shouldLog'}}
   ^{ 
      f23_aux("I did too use it!\n");
   }();  
@@ -377,7 +384,7 @@
   // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
   int x = (y > 2); // no-warning
   ^ {
-      int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}}
+      int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}} expected-warning{{unused variable 'z'}}
   }();  
 }
 





More information about the cfe-commits mailing list