Hi,

Attached is a patch that adds arithmetic with overflow builtins to Clang.  
Please review.  Thanks.

Arithmetic with overflow builtins are used to perform arithmetic operations 
with overflow detection.

Syntax:

bool __builtin_add_with_overflow(type *ptr, type a, type b);
bool __builtin_sub_with_overflow(type *ptr, type a, type b);
bool __builtin_mul_with_overflow(type *ptr, type a, type b);

Example of Use:

void *malloc_array(size_t n, size_t size) {
  size_t bytes;
  if (__builtin_mul_with_overflow(&bytes, n, size))
    return NULL;
  return malloc(bytes);
}

Description:

__builtin_op_with_overflow(ptr, a, b) stores the result of a op b in ptr, and 
returns true if an overflow occurred during the arithmetic operation. Note that 
type is inferred from *ptr. These builtins help developers write more efficient 
and correct code by avoiding ad hoc overflow checks.

Query for this feature with __has_builtin(__builtin_op_with_overflow).

- xi

Attachment: overflow.patch
Description: Binary data

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to