[cfe-commits] r125649 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaChecking.cpp test/Sema/array-bounds.c

Ted Kremenek kremenek at apple.com
Tue Feb 15 20:01:44 PST 2011


Author: kremenek
Date: Tue Feb 15 22:01:44 2011
New Revision: 125649

URL: http://llvm.org/viewvc/llvm-project?rev=125649&view=rev
Log:
Tweak -Warray-bounds diagnostics based on feedback from Chandler.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/array-bounds.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=125649&r1=125648&r2=125649&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Feb 15 22:01:44 2011
@@ -3379,9 +3379,14 @@
 def warn_explicit_conversion_functions : Warning<
   "explicit conversion functions are a C++0x extension">, InGroup<CXX0x>;
 
-def warn_array_index_out_of_bounds : Warning<
-  "array index %select{precedes first|excedes last}0 array element">,
+def warn_array_index_precedes_bounds : Warning<
+  "array index of '%0' indexes before the beginning of the array">,
   InGroup<DiagGroup<"array-bounds">>;
+def warn_array_index_exceeds_bounds : Warning<
+  "array index of '%0' indexes past the end of an array (that contains %1 elements)">,
+  InGroup<DiagGroup<"array-bounds">>;
+def note_array_index_out_of_bounds : Note<
+  "Array %0 declared here">;
 
 def warn_printf_write_back : Warning<
   "use of '%%n' in format string discouraged (potentially insecure)">,

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=125649&r1=125648&r2=125649&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Feb 15 22:01:44 2011
@@ -3095,18 +3095,24 @@
   llvm::APSInt result;
   if (!idx->isIntegerConstantExpr(result, Context))
     return;
-  unsigned kind = 2;
-  if (result.slt(0))
-    kind = /* precedes */ 0;
+
+  if (result.slt(0)) {
+    Diag(ae->getBase()->getLocStart(), diag::warn_array_index_precedes_bounds)
+      << result.toString(10, true) << idx->getSourceRange();
+  }
   else {
     const llvm::APInt &size = cat->getSize();
     if (size.getBitWidth() > result.getBitWidth())
       result = result.sext(size.getBitWidth());
-    if (result.sge(size))
-      kind = /* excedes */ 1;
+    if (result.sge(size)) {
+      Diag(ae->getBase()->getLocStart(), diag::warn_array_index_exceeds_bounds)
+        << result.toString(10, true) << size.toString(10, true)
+        << idx->getSourceRange();
+    }
+    else
+      return;
   }
-  if (kind < 2)
-    Diag(ae->getBase()->getLocEnd(), diag::warn_array_index_out_of_bounds)
-      << kind << idx->getSourceRange();
+  Diag(vd->getLocStart(), diag::note_array_index_out_of_bounds)
+    << vd->getDeclName();
 }
 

Modified: cfe/trunk/test/Sema/array-bounds.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-bounds.c?rev=125649&r1=125648&r2=125649&view=diff
==============================================================================
--- cfe/trunk/test/Sema/array-bounds.c (original)
+++ cfe/trunk/test/Sema/array-bounds.c Tue Feb 15 22:01:44 2011
@@ -1,16 +1,16 @@
 // RUN: %clang_cc1 -verify %s
 
 int foo() {
-  int x[2];
-  int y[2];
+  int x[2]; // expected-note 4 {{Array 'x' declared here}}
+  int y[2]; // expected-note 2 {{Array 'y' declared here}}
   int *p = &y[2]; // no-warning
   (void) sizeof(x[2]); // no-warning
-  y[2] = 2; // expected-warning{{array index excedes last array element}}
-  return x[2] +  // expected-warning{{array index excedes last array element}}
-         y[-1] + // expected-warning{{array index precedes first array element}}
-         x[sizeof(x)] +  // expected-warning{{array index excedes last array element}}
-         x[sizeof(x) / sizeof(x[0])] +  // expected-warning{{array index excedes last array element}}
+  y[2] = 2; // expected-warning{{array index of '2' indexes past the end of an array (that contains 2 elements)}}
+  return x[2] +  // expected-warning{{array index of '2' indexes past the end of an array (that contains 2 elements)}}
+         y[-1] + // expected-warning{{array index of '-1' indexes before the beginning of the array}}
+         x[sizeof(x)] +  // expected-warning{{array index of '8' indexes past the end of an array (that contains 2 elements)}}
+         x[sizeof(x) / sizeof(x[0])] +  // expected-warning{{array index of '2' indexes past the end of an array (that contains 2 elements)}}
          x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning
-         x[sizeof(x[2])]; // expected-warning{{array index excedes last array element}}
+         x[sizeof(x[2])]; // expected-warning{{array index of '4' indexes past the end of an array (that contains 2 elements)}}
 }
 





More information about the cfe-commits mailing list