jmolloy created this revision. jmolloy added reviewers: davide, aaron.ballman. jmolloy added a subscriber: cfe-commits.
This means we shouldn't emit ubsan detection code or warn. Fixes PR25552. https://reviews.llvm.org/D23498 Files: lib/CodeGen/CGExprScalar.cpp lib/Sema/SemaExpr.cpp test/CodeGen/wrapv-lshr-sanitize.c test/Sema/negative-shift-wrapv.c Index: test/Sema/negative-shift-wrapv.c =================================================================== --- /dev/null +++ test/Sema/negative-shift-wrapv.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -Wall -ffreestanding -fsyntax-only -fwrapv -verify %s + +#include <limits.h> + +#define WORD_BIT (sizeof(int) * CHAR_BIT) + +void test() { + int i; + i = -1 << (WORD_BIT - 1); // no-warning +} + +// expected-no-diagnostics Index: test/CodeGen/wrapv-lshr-sanitize.c =================================================================== --- /dev/null +++ test/CodeGen/wrapv-lshr-sanitize.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -fsanitize=shift-base -emit-llvm %s -o - -triple x86_64-linux-gnu -fwrapv | opt -instnamer -S | FileCheck %s + +// CHECK-LABEL: @lsh_overflow +int lsh_overflow(int a, int b) { + // CHECK-NOT: br + // CHECK-NOT: call void @__ubsan_ + // CHECK-NOT: call void @llvm.trap + + // CHECK: %[[RET:.*]] = shl i32 + // CHECK-NEXT: ret i32 %[[RET]] + return a << b; +} Index: lib/Sema/SemaExpr.cpp =================================================================== --- lib/Sema/SemaExpr.cpp +++ lib/Sema/SemaExpr.cpp @@ -8567,7 +8567,7 @@ // If LHS does not have a signed type and non-negative value // then, the behavior is undefined. Warn about it. - if (Left.isNegative()) { + if (Left.isNegative() && !S.getLangOpts().isSignedOverflowDefined()) { S.DiagRuntimeBehavior(Loc, LHS.get(), S.PDiag(diag::warn_shift_lhs_negative) << LHS.get()->getSourceRange()); Index: lib/CodeGen/CGExprScalar.cpp =================================================================== --- lib/CodeGen/CGExprScalar.cpp +++ lib/CodeGen/CGExprScalar.cpp @@ -2706,7 +2706,8 @@ RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); bool SanitizeBase = CGF.SanOpts.has(SanitizerKind::ShiftBase) && - Ops.Ty->hasSignedIntegerRepresentation(); + Ops.Ty->hasSignedIntegerRepresentation() && + !CGF.getLangOpts().isSignedOverflowDefined(); bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent); // OpenCL 6.3j: shift values are effectively % word size of LHS. if (CGF.getLangOpts().OpenCL)
Index: test/Sema/negative-shift-wrapv.c =================================================================== --- /dev/null +++ test/Sema/negative-shift-wrapv.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -Wall -ffreestanding -fsyntax-only -fwrapv -verify %s + +#include <limits.h> + +#define WORD_BIT (sizeof(int) * CHAR_BIT) + +void test() { + int i; + i = -1 << (WORD_BIT - 1); // no-warning +} + +// expected-no-diagnostics Index: test/CodeGen/wrapv-lshr-sanitize.c =================================================================== --- /dev/null +++ test/CodeGen/wrapv-lshr-sanitize.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -fsanitize=shift-base -emit-llvm %s -o - -triple x86_64-linux-gnu -fwrapv | opt -instnamer -S | FileCheck %s + +// CHECK-LABEL: @lsh_overflow +int lsh_overflow(int a, int b) { + // CHECK-NOT: br + // CHECK-NOT: call void @__ubsan_ + // CHECK-NOT: call void @llvm.trap + + // CHECK: %[[RET:.*]] = shl i32 + // CHECK-NEXT: ret i32 %[[RET]] + return a << b; +} Index: lib/Sema/SemaExpr.cpp =================================================================== --- lib/Sema/SemaExpr.cpp +++ lib/Sema/SemaExpr.cpp @@ -8567,7 +8567,7 @@ // If LHS does not have a signed type and non-negative value // then, the behavior is undefined. Warn about it. - if (Left.isNegative()) { + if (Left.isNegative() && !S.getLangOpts().isSignedOverflowDefined()) { S.DiagRuntimeBehavior(Loc, LHS.get(), S.PDiag(diag::warn_shift_lhs_negative) << LHS.get()->getSourceRange()); Index: lib/CodeGen/CGExprScalar.cpp =================================================================== --- lib/CodeGen/CGExprScalar.cpp +++ lib/CodeGen/CGExprScalar.cpp @@ -2706,7 +2706,8 @@ RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); bool SanitizeBase = CGF.SanOpts.has(SanitizerKind::ShiftBase) && - Ops.Ty->hasSignedIntegerRepresentation(); + Ops.Ty->hasSignedIntegerRepresentation() && + !CGF.getLangOpts().isSignedOverflowDefined(); bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent); // OpenCL 6.3j: shift values are effectively % word size of LHS. if (CGF.getLangOpts().OpenCL)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits