[cfe-commits] r160334 - in /cfe/trunk: lib/Analysis/UninitializedValues.cpp lib/Sema/AnalysisBasedWarnings.cpp test/Sema/uninit-variables.c

Richard Smith richard-llvm at metafoo.co.uk
Mon Jul 16 18:27:33 PDT 2012


Author: rsmith
Date: Mon Jul 16 20:27:33 2012
New Revision: 160334

URL: http://llvm.org/viewvc/llvm-project?rev=160334&view=rev
Log:
Uninitialized variables: two little changes:
 * Treat compound assignment as a use, at Jordy's request.
 * Always add compound assignments into the CFG, so we can correctly diagnose the use in 'return x += 1;'

Modified:
    cfe/trunk/lib/Analysis/UninitializedValues.cpp
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
    cfe/trunk/test/Sema/uninit-variables.c

Modified: cfe/trunk/lib/Analysis/UninitializedValues.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/UninitializedValues.cpp?rev=160334&r1=160333&r2=160334&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/UninitializedValues.cpp (original)
+++ cfe/trunk/lib/Analysis/UninitializedValues.cpp Mon Jul 16 20:27:33 2012
@@ -447,7 +447,9 @@
   // when TransferFunctions visits it. A compound-assignment does not affect
   // whether a variable is uninitialized, and there's no point counting it as a
   // use.
-  if (BO->isAssignmentOp())
+  if (BO->isCompoundAssignmentOp())
+    classify(BO->getLHS(), Use);
+  else if (BO->getOpcode() == BO_Assign)
     classify(BO->getLHS(), Ignore);
 }
 

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=160334&r1=160333&r2=160334&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Mon Jul 16 20:27:33 2012
@@ -1220,6 +1220,7 @@
   else {
     AC.getCFGBuildOptions()
       .setAlwaysAdd(Stmt::BinaryOperatorClass)
+      .setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
       .setAlwaysAdd(Stmt::BlockExprClass)
       .setAlwaysAdd(Stmt::CStyleCastExprClass)
       .setAlwaysAdd(Stmt::DeclRefExprClass)

Modified: cfe/trunk/test/Sema/uninit-variables.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/uninit-variables.c?rev=160334&r1=160333&r2=160334&view=diff
==============================================================================
--- cfe/trunk/test/Sema/uninit-variables.c (original)
+++ cfe/trunk/test/Sema/uninit-variables.c Mon Jul 16 20:27:33 2012
@@ -33,8 +33,8 @@
 
 int test6() {
   int x; // expected-note{{initialize the variable 'x' to silence this warning}}
-  x += 2;
-  return x; // expected-warning{{variable 'x' is uninitialized when used here}}
+  x += 2; // expected-warning{{variable 'x' is uninitialized when used here}}
+  return x;
 }
 
 int test7(int y) {
@@ -489,12 +489,17 @@
 int compound_assign(int *arr, int n) {
   int sum; // expected-note {{initialize}}
   for (int i = 0; i < n; ++i)
-    sum += arr[i];
-  return sum / n; // expected-warning {{variable 'sum' is uninitialized}}
+    sum += arr[i]; // expected-warning {{variable 'sum' is uninitialized}}
+  return sum / n;
 }
 
-void compound_assign_2(int n) {
-  volatile int ignore;
-  for (int j = 0; j < n; ++j)
-    ignore += test1(); // ok
+int compound_assign_2() {
+  int x; // expected-note {{initialize}}
+  return x += 1; // expected-warning {{variable 'x' is uninitialized}}
+}
+
+int compound_assign_3() {
+  int x; // expected-note {{initialize}}
+  x *= 0; // expected-warning {{variable 'x' is uninitialized}}
+  return x;
 }





More information about the cfe-commits mailing list