]> granicus.if.org Git - clang/commitdiff
Uninitialized variables: two little changes:
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 17 Jul 2012 01:27:33 +0000 (01:27 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 17 Jul 2012 01:27:33 +0000 (01:27 +0000)
 * 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;'

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160334 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/UninitializedValues.cpp
lib/Sema/AnalysisBasedWarnings.cpp
test/Sema/uninit-variables.c

index 1b37c29990169aed67e3d1fa36add5df34a02faf..acf695a1e9c7bae2d6d25a84e236e69271f7cf11 100644 (file)
@@ -447,7 +447,9 @@ void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) {
   // 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);
 }
 
index d696283f7f510924dd6f6137111269fbf3238cdb..19a7d6f35c8f10728b058a4cdf37ca99d51f45d7 100644 (file)
@@ -1220,6 +1220,7 @@ AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
   else {
     AC.getCFGBuildOptions()
       .setAlwaysAdd(Stmt::BinaryOperatorClass)
+      .setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
       .setAlwaysAdd(Stmt::BlockExprClass)
       .setAlwaysAdd(Stmt::CStyleCastExprClass)
       .setAlwaysAdd(Stmt::DeclRefExprClass)
index 4e3d74b3eafe6a39068b3c56dc2f5551a7e9bb7c..9257751e4717769ed245b8bc2409a68d0fd4295e 100644 (file)
@@ -33,8 +33,8 @@ int test5() {
 
 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 returns_twice() {
 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;
 }