]> granicus.if.org Git - clang/commitdiff
[analyzer] Push correct version of 'Track indices of arrays'
authorKristof Umann <dkszelethus@gmail.com>
Sun, 16 Jun 2019 15:41:25 +0000 (15:41 +0000)
committerKristof Umann <dkszelethus@gmail.com>
Sun, 16 Jun 2019 15:41:25 +0000 (15:41 +0000)
Messed up the commit, oops.

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

lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
test/Analysis/diagnostics/track_subexpressions.cpp

index be6e233458665cc934c3f398bd0c14b1c188f3bc..c830bfa98023b693a1f0c94f39758a76fb525df0 100644 (file)
@@ -1740,9 +1740,10 @@ bool bugreporter::trackExpressionValue(const ExplodedNode *InputNode,
   if (const Expr *Receiver = NilReceiverBRVisitor::getNilReceiver(Inner, LVNode))
     trackExpressionValue(LVNode, Receiver, report, EnableNullFPSuppression);
 
+  // Track the index if this is an array subscript.
   if (const auto *Arr = dyn_cast<ArraySubscriptExpr>(Inner))
     trackExpressionValue(
-        LVNode, Arr->getIdx(), report, EnableNullFPSuppression);
+        LVNode, Arr->getIdx(), report, /*EnableNullFPSuppression*/ false);
 
   // See if the expression we're interested refers to a variable.
   // If so, we can track both its contents and constraints on its value.
index 9097a05a490627ca7a3bde7086b3e66d6d2d9555..99fc0a6e384a659220d36769a928430648b9ca7d 100644 (file)
@@ -23,11 +23,12 @@ void consume(int);
 
 int getIndex(int x) {
   int a;
-  if (x > 0)
-    a = 3;
+  if (x > 0) // expected-note {{Assuming 'x' is > 0}}
+             // expected-note@-1 {{Taking true branch}}
+    a = 3; // expected-note {{The value 3 is assigned to 'a'}}
   else
     a = 2;
-  return a;
+  return a; // expected-note {{Returning the value 3 (loaded from 'a')}}
 }
 
 int getInt();
@@ -36,9 +37,47 @@ void testArrayIndexTracking() {
   int arr[10];
 
   for (int i = 0; i < 3; ++i)
+    // expected-note@-1 3{{Loop condition is true.  Entering loop body}}
+    // expected-note@-2 {{Loop condition is false. Execution continues on line 43}}
     arr[i] = 0;
   int x = getInt();
-  int n = getIndex(x);
+  int n = getIndex(x); // expected-note {{Calling 'getIndex'}}
+                       // expected-note@-1 {{Returning from 'getIndex'}}
+                       // expected-note@-2 {{'n' initialized to 3}}
   consume(arr[n]);
+  // expected-note@-1 {{1st function call argument is an uninitialized value}}
+  // expected-warning@-2{{1st function call argument is an uninitialized value}}
 }
 } // end of namespace array_index_tracking
+
+namespace multi_array_index_tracking {
+void consume(int);
+
+int getIndex(int x) {
+  int a;
+  if (x > 0) // expected-note {{Assuming 'x' is > 0}}
+             // expected-note@-1 {{Taking true branch}}
+    a = 3; // expected-note {{The value 3 is assigned to 'a'}}
+  else
+    a = 2;
+  return a; // expected-note {{Returning the value 3 (loaded from 'a')}}
+}
+
+int getInt();
+
+void testArrayIndexTracking() {
+  int arr[2][10];
+
+  for (int i = 0; i < 3; ++i)
+    // expected-note@-1 3{{Loop condition is true.  Entering loop body}}
+    // expected-note@-2 {{Loop condition is false. Execution continues on line 75}}
+    arr[1][i] = 0;
+  int x = getInt();
+  int n = getIndex(x); // expected-note {{Calling 'getIndex'}}
+                       // expected-note@-1 {{Returning from 'getIndex'}}
+                       // expected-note@-2 {{'n' initialized to 3}}
+  consume(arr[1][n]);
+  // expected-note@-1 {{1st function call argument is an uninitialized value}}
+  // expected-warning@-2{{1st function call argument is an uninitialized value}}
+}
+} // end of namespace mulit_array_index_tracking