[PATCH] Fix to PR5683 - issue diagnostic for pointer subtraction with type of size zero.
Serge Pavlov
sepavloff at gmail.com
Sun Apr 7 11:33:22 PDT 2013
sepavloff added you to the CC list for the revision "Fix to PR5683 - issue diagnostic for pointer subtraction with type of size zero.".
Hi rsmith,
http://llvm-reviews.chandlerc.com/D637
Files:
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/Sema/empty1.c
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -4243,6 +4243,9 @@
def err_typecheck_sub_ptr_compatible : Error<
"%diff{$ and $ are not pointers to compatible types|"
"pointers to incompatible types}0,1">;
+def err_sub_ptr_zero_size_types : Error<
+ "subtraction of pointers to types of zero length (%diff{$ and $)|}0,1 "
+ "is not allowed">;
def ext_typecheck_ordered_comparison_of_pointer_integer : ExtWarn<
"ordered comparison between pointer and integer (%0 and %1)">;
def ext_typecheck_ordered_comparison_of_pointer_and_zero : Extension<
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -6728,6 +6728,16 @@
diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get());
return QualType();
}
+
+ // If pointee type is a structure or union of zero size (GCC extension),
+ // the subtraction does not have a sense.
+ CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);
+ if (ElementSize.isZero() && rpointee.getTypePtr()->isStructureType()) {
+ Diag(Loc,diag::err_sub_ptr_zero_size_types)
+ << lpointee << rpointee
+ << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
+ return QualType();
+ }
}
if (!checkArithmeticBinOpPointerOperands(*this, Loc,
Index: test/Sema/empty1.c
===================================================================
--- /dev/null
+++ test/Sema/empty1.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+
+struct empty {};
+struct empty_2 {};
+
+struct empty* func_1p (struct empty* x) {
+ return x - 5;
+}
+
+int func_1 () {
+ struct empty v[1];
+ return v - v; // expected-error {{subtraction of pointers to types of zero length ('struct empty' and 'struct empty') is not allowed}}
+}
+
+int func_2 (struct empty* x) {
+ return 1 + x - x; // expected-error {{subtraction of pointers to types of zero length ('struct empty' and 'struct empty') is not allowed}}
+}
+
+int func_3 (struct empty* x, struct empty* y) {
+ return x - y; // expected-error {{subtraction of pointers to types of zero length ('struct empty' and 'struct empty') is not allowed}}
+}
+
+int func_4 (struct empty* x, const struct empty* y) {
+ return x - y; // expected-error {{subtraction of pointers to types of zero length ('struct empty' and 'const struct empty') is not allowed}}
+}
+
+int func_5 (volatile struct empty* x, const struct empty* y) {
+ return x - y; // expected-error {{subtraction of pointers to types of zero length ('volatile struct empty' and 'const struct empty') is not allowed}}
+}
+
+int func_6 (struct empty* x, struct empty_2* y) {
+ return x - y; // expected-error {{'struct empty *' and 'struct empty_2 *' are not pointers to compatible types}}
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D637.1.patch
Type: text/x-patch
Size: 3048 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130407/8c834d31/attachment.bin>
More information about the cfe-commits
mailing list