summaryrefslogtreecommitdiffstats
path: root/clang/lib/AST
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2013-09-10 21:34:14 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2013-09-10 21:34:14 +0000
commit84c6b3d293d91e9d8403108d5069f7b6e6a57afa (patch)
treed9aef84d54689cd71d22630bb269ad86066e0dd2 /clang/lib/AST
parent754a3ead62b5a5bf685d23ab914148c55307106c (diff)
downloadbcm5719-llvm-84c6b3d293d91e9d8403108d5069f7b6e6a57afa.tar.gz
bcm5719-llvm-84c6b3d293d91e9d8403108d5069f7b6e6a57afa.zip
PR5683: Issue a warning when subtracting pointers to types of zero size, and
treat such subtractions as being non-constant. Patch by Serge Pavlov! With a few tweaks by me. llvm-svn: 190439
Diffstat (limited to 'clang/lib/AST')
-rw-r--r--clang/lib/AST/ExprConstant.cpp9
1 files changed, 9 insertions, 0 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index b6602145e28..218ce8101b5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -6570,6 +6570,15 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))
return false;
+ // As an extension, a type may have zero size (empty struct or union in
+ // C, array of zero length). Pointer subtraction in such cases has
+ // undefined behavior, so is not constant.
+ if (ElementSize.isZero()) {
+ Info.Diag(E, diag::note_constexpr_pointer_subtraction_zero_size)
+ << ElementType;
+ return false;
+ }
+
// FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
// and produce incorrect results when it overflows. Such behavior
// appears to be non-conforming, but is common, so perhaps we should
OpenPOWER on IntegriCloud