[cfe-commits] r122802 - in /cfe/trunk: include/clang/Basic/ lib/Sema/ test/CXX/dcl.decl/dcl.meaning/dcl.array/ test/Sema/ test/SemaCXX/ test/SemaTemplate/

Chandler Carruth chandlerc at gmail.com
Mon Jan 3 20:44:35 PST 2011


Author: chandlerc
Date: Mon Jan  3 22:44:35 2011
New Revision: 122802

URL: http://llvm.org/viewvc/llvm-project?rev=122802&view=rev
Log:
Enhance the diagnostic for negative array sizes to include the
declaration name of the array when present. This ensures that
a poor-man's C++03 static_assert will include the user error message
often embedded in the name.

Update all the tests to reflect the new wording, and add a test for the
name behavior.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
    cfe/trunk/test/Sema/array-constraint.c
    cfe/trunk/test/SemaCXX/virtual-override.cpp
    cfe/trunk/test/SemaTemplate/attributes.cpp
    cfe/trunk/test/SemaTemplate/default-expr-arguments.cpp
    cfe/trunk/test/SemaTemplate/friend-template.cpp
    cfe/trunk/test/SemaTemplate/instantiate-default-assignment-operator.cpp
    cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp
    cfe/trunk/test/SemaTemplate/instantiate-member-expr.cpp
    cfe/trunk/test/SemaTemplate/instantiate-member-pointers.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jan  3 22:44:35 2011
@@ -2167,6 +2167,8 @@
   "objective-c message has incomplete result type %0">;
 def err_illegal_decl_array_of_references : Error<
   "'%0' declared as array of references of type %1">;
+def err_decl_negative_array_size : Error<
+  "'%0' declared as an array with a negative size">;
 def err_array_star_outside_prototype : Error<
   "star modifier used outside of function prototype">;
 def err_illegal_decl_pointer_to_reference : Error<

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Mon Jan  3 22:44:35 2011
@@ -711,9 +711,12 @@
     // C99 6.7.5.2p1: If the expression is a constant expression, it shall
     // have a value greater than zero.
     if (ConstVal.isSigned() && ConstVal.isNegative()) {
-      Diag(ArraySize->getLocStart(),
-           diag::err_typecheck_negative_array_size)
-        << ArraySize->getSourceRange();
+      if (Entity)
+        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
+          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
+      else
+        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
+          << ArraySize->getSourceRange();
       return QualType();
     }
     if (ConstVal == 0) {

Modified: cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp (original)
+++ cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1.cpp Mon Jan  3 22:44:35 2011
@@ -16,7 +16,7 @@
 Abstract ar5[10]; // expected-error {{abstract class}}
 
 // If we have a size, it must be greater than zero.
-int ar6[-1]; // expected-error {{array size is negative}}
+int ar6[-1]; // expected-error {{array with a negative size}}
 int ar7[0u]; // expected-warning {{zero size arrays are an extension}}
 
 // An array with unknown bound is incomplete.
@@ -42,3 +42,13 @@
   typename T::type x; // expected-error {{has no members}}
 };
 S<int> ar10[10]; // expected-note {{requested here}}
+
+// Ensure that negative array size errors include the name of the declared
+// array as this is often used to simulate static_assert with template
+// instantiations, placing the 'error message' in the declarator name.
+int
+user_error_message
+[-1]; // expected-error {{user_error_message}}
+typedef int
+another_user_error_message
+[-1]; // expected-error {{another_user_error_message}}

Modified: cfe/trunk/test/Sema/array-constraint.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-constraint.c?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/Sema/array-constraint.c (original)
+++ cfe/trunk/test/Sema/array-constraint.c Mon Jan  3 22:44:35 2011
@@ -36,7 +36,7 @@
 void check_size() {
   float f;
   int size_not_int[f]; // expected-error {{size of array has non-integer type 'float'}}
-  int negative_size[1-2]; // expected-error{{array size is negative}}
+  int negative_size[1-2]; // expected-error{{array with a negative size}}
   int zero_size[0]; // expected-warning{{zero size arrays are an extension}}
 }
 

Modified: cfe/trunk/test/SemaCXX/virtual-override.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/virtual-override.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/virtual-override.cpp (original)
+++ cfe/trunk/test/SemaCXX/virtual-override.cpp Mon Jan  3 22:44:35 2011
@@ -121,7 +121,7 @@
   struct a { };
   
   template<typename T> struct b : a {
-    int a[sizeof(T) ? -1 : -1]; // expected-error {{array size is negative}}
+    int a[sizeof(T) ? -1 : -1]; // expected-error {{array with a negative size}}
   };
   
   class A {

Modified: cfe/trunk/test/SemaTemplate/attributes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/attributes.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/attributes.cpp (original)
+++ cfe/trunk/test/SemaTemplate/attributes.cpp Mon Jan  3 22:44:35 2011
@@ -7,7 +7,7 @@
   };
 
   template <bool X> struct check {
-    int check_failed[X ? 1 : -1]; // expected-error {{array size is negative}}
+    int check_failed[X ? 1 : -1]; // expected-error {{array with a negative size}}
   };
 
   template <int N> struct check_alignment {

Modified: cfe/trunk/test/SemaTemplate/default-expr-arguments.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/default-expr-arguments.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/default-expr-arguments.cpp (original)
+++ cfe/trunk/test/SemaTemplate/default-expr-arguments.cpp Mon Jan  3 22:44:35 2011
@@ -151,7 +151,7 @@
 namespace PR5810 {
   template<typename T>
   struct allocator {
-    allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array size is negative}}
+    allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}}
   };
   
   template<typename T>

Modified: cfe/trunk/test/SemaTemplate/friend-template.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/friend-template.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/friend-template.cpp (original)
+++ cfe/trunk/test/SemaTemplate/friend-template.cpp Mon Jan  3 22:44:35 2011
@@ -93,7 +93,7 @@
   };
 
   template<typename T> void f(const A<T>&) {
-    int a[sizeof(T) ? -1 : -1]; // expected-error {{array size is negative}}
+    int a[sizeof(T) ? -1 : -1]; // expected-error {{array with a negative size}}
   }
 
   void f() {

Modified: cfe/trunk/test/SemaTemplate/instantiate-default-assignment-operator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-default-assignment-operator.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-default-assignment-operator.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-default-assignment-operator.cpp Mon Jan  3 22:44:35 2011
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 template<typename> struct PassRefPtr { };
 template<typename T> struct RefPtr {
-  RefPtr& operator=(const RefPtr&) { int a[sizeof(T) ? -1 : -1];} // expected-error 2 {{array size is negative}}
+  RefPtr& operator=(const RefPtr&) { int a[sizeof(T) ? -1 : -1];} // expected-error 2 {{array with a negative size}}
   RefPtr& operator=(const PassRefPtr<T>&);
 };
 

Modified: cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-expr-4.cpp Mon Jan  3 22:44:35 2011
@@ -46,8 +46,8 @@
 // Ensure that both the constructor and the destructor are instantiated by
 // checking for parse errors from each.
 template<int N> struct BadX {
-  BadX() { int a[-N]; } // expected-error {{array size is negative}}
-  ~BadX() { int a[-N]; } // expected-error {{array size is negative}}
+  BadX() { int a[-N]; } // expected-error {{array with a negative size}}
+  ~BadX() { int a[-N]; } // expected-error {{array with a negative size}}
 };
 
 template<int N>

Modified: cfe/trunk/test/SemaTemplate/instantiate-member-expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-member-expr.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-member-expr.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-member-expr.cpp Mon Jan  3 22:44:35 2011
@@ -6,7 +6,7 @@
 
 template<typename T>
 struct vector {
-  void push_back(const T&) { int a[sizeof(T) ? -1: -1]; } // expected-error {{array size is negative}}
+  void push_back(const T&) { int a[sizeof(T) ? -1: -1]; } // expected-error {{array with a negative size}}
 };
 
 class ExprEngine {

Modified: cfe/trunk/test/SemaTemplate/instantiate-member-pointers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-member-pointers.cpp?rev=122802&r1=122801&r2=122802&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-member-pointers.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-member-pointers.cpp Mon Jan  3 22:44:35 2011
@@ -61,7 +61,7 @@
     typedef instantiate_function<&S::instantiate> x; // expected-note{{instantiation}}
   };
   template <typename T> void S<T>::instantiate() {
-    int a[(int)sizeof(T)-42]; // expected-error{{array size is negative}}
+    int a[(int)sizeof(T)-42]; // expected-error{{array with a negative size}}
   }
   S<int> s; 
 }





More information about the cfe-commits mailing list