Author: vsapsai Date: Tue Mar 27 14:29:05 2018 New Revision: 328671 URL: http://llvm.org/viewvc/llvm-project?rev=328671&view=rev Log: [Sema] Emit -Winteger-overflow for arguments in function calls, ObjC messages.
rdar://problem/35539384 Reviewers: ahatanak, nicholas, rsmith, jkorous-apple Reviewed By: jkorous-apple Subscribers: cfe-commits, jkorous-apple Differential Revision: https://reviews.llvm.org/D42938 Added: cfe/trunk/test/SemaObjC/integer-overflow.m Modified: cfe/trunk/lib/Sema/SemaChecking.cpp cfe/trunk/test/Sema/integer-overflow.c cfe/trunk/test/SemaCXX/integer-overflow.cpp Modified: cfe/trunk/lib/Sema/SemaChecking.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=328671&r1=328670&r2=328671&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaChecking.cpp (original) +++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Mar 27 14:29:05 2018 @@ -10225,18 +10225,22 @@ void Sema::CheckForIntOverflow (Expr *E) SmallVector<Expr *, 2> Exprs(1, E); do { - Expr *E = Exprs.pop_back_val(); + Expr *OriginalE = Exprs.pop_back_val(); + Expr *E = OriginalE->IgnoreParenCasts(); - if (isa<BinaryOperator>(E->IgnoreParenCasts())) { - E->IgnoreParenCasts()->EvaluateForOverflow(Context); + if (isa<BinaryOperator>(E)) { + E->EvaluateForOverflow(Context); continue; } - if (auto InitList = dyn_cast<InitListExpr>(E)) + if (auto InitList = dyn_cast<InitListExpr>(OriginalE)) Exprs.append(InitList->inits().begin(), InitList->inits().end()); - - if (isa<ObjCBoxedExpr>(E)) - E->IgnoreParenCasts()->EvaluateForOverflow(Context); + else if (isa<ObjCBoxedExpr>(OriginalE)) + E->EvaluateForOverflow(Context); + else if (auto Call = dyn_cast<CallExpr>(E)) + Exprs.append(Call->arg_begin(), Call->arg_end()); + else if (auto Message = dyn_cast<ObjCMessageExpr>(E)) + Exprs.append(Message->arg_begin(), Message->arg_end()); } while (!Exprs.empty()); } Modified: cfe/trunk/test/Sema/integer-overflow.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/integer-overflow.c?rev=328671&r1=328670&r2=328671&view=diff ============================================================================== --- cfe/trunk/test/Sema/integer-overflow.c (original) +++ cfe/trunk/test/Sema/integer-overflow.c Tue Mar 27 14:29:05 2018 @@ -158,6 +158,21 @@ uint64_t check_integer_overflows(int i) return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))); } +void check_integer_overflows_in_function_calls() { +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + (void)f0(4608 * 1024 * 1024); + +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + uint64_t x = f0(4608 * 1024 * 1024); + +// expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}} + uint64_t (*f0_ptr)(uint64_t) = &f0; + (void)(*f0_ptr)(4608 * 1024 * 1024); + +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + (void)f2(0, f0(4608 * 1024 * 1024)); +} + struct s { unsigned x; unsigned y; Modified: cfe/trunk/test/SemaCXX/integer-overflow.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/integer-overflow.cpp?rev=328671&r1=328670&r2=328671&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/integer-overflow.cpp (original) +++ cfe/trunk/test/SemaCXX/integer-overflow.cpp Tue Mar 27 14:29:05 2018 @@ -169,3 +169,18 @@ uint64_t check_integer_overflows(int i) // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))); } + +void check_integer_overflows_in_function_calls() { +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + (void)f0(4608 * 1024 * 1024); + +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + uint64_t x = f0(4608 * 1024 * 1024); + +// expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}} + uint64_t (*f0_ptr)(uint64_t) = &f0; + (void)(*f0_ptr)(4608 * 1024 * 1024); + +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + (void)f2(0, f0(4608 * 1024 * 1024)); +} Added: cfe/trunk/test/SemaObjC/integer-overflow.m URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/integer-overflow.m?rev=328671&view=auto ============================================================================== --- cfe/trunk/test/SemaObjC/integer-overflow.m (added) +++ cfe/trunk/test/SemaObjC/integer-overflow.m Tue Mar 27 14:29:05 2018 @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -Wno-objc-root-class -fsyntax-only -verify %s + +@interface Foo +@end + +@implementation Foo +- (int)add:(int)a with:(int)b { + return a + b; +} + +- (void)testIntegerOverflows { +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + (void)[self add:0 with:4608 * 1024 * 1024]; + +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + (void)[self add:0 with:[self add:4608 * 1024 * 1024 with:0]]; +} +@end _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits