-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #578
- Loading branch information
Showing
6 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* <stdckdint.h> for the Aro C compiler */ | ||
|
||
#pragma once | ||
|
||
#define __STDC_VERSION_STDCKDINT_H__ 202311L | ||
|
||
#define ckd_add(result, a, b) __builtin_add_overflow(a, b, result) | ||
#define ckd_sub(result, a, b) __builtin_sub_overflow(a, b, result) | ||
#define ckd_mul(result, a, b) __builtin_mul_overflow(a, b, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
fn_def: 'fn () void' | ||
name: foo | ||
body: | ||
compound_stmt: 'void' | ||
var: 'char' | ||
name: x | ||
init: | ||
implicit_cast: (int_cast) 'char' | ||
int_literal: 'int' (value: 0) | ||
|
||
var: 'unsigned int' | ||
name: y | ||
init: | ||
implicit_cast: (int_cast) 'unsigned int' | ||
int_literal: 'int' (value: 2) | ||
|
||
var: '_Bool' | ||
name: overflowed | ||
|
||
var: 'long' | ||
name: res | ||
|
||
assign_expr: '_Bool' | ||
lhs: | ||
decl_ref_expr: '_Bool' lvalue | ||
name: overflowed | ||
rhs: | ||
builtin_call_expr: '_Bool' | ||
name: __builtin_add_overflow | ||
args: | ||
implicit_cast: (lval_to_rval) 'char' | ||
decl_ref_expr: 'char' lvalue | ||
name: x | ||
implicit_cast: (lval_to_rval) 'unsigned int' | ||
decl_ref_expr: 'unsigned int' lvalue | ||
name: y | ||
addr_of_expr: '*long' | ||
operand: | ||
decl_ref_expr: 'long' lvalue | ||
name: res | ||
|
||
assign_expr: '_Bool' | ||
lhs: | ||
decl_ref_expr: '_Bool' lvalue | ||
name: overflowed | ||
rhs: | ||
builtin_call_expr: '_Bool' | ||
name: __builtin_sub_overflow | ||
args: | ||
implicit_cast: (lval_to_rval) 'char' | ||
decl_ref_expr: 'char' lvalue | ||
name: x | ||
implicit_cast: (lval_to_rval) 'unsigned int' | ||
decl_ref_expr: 'unsigned int' lvalue | ||
name: y | ||
addr_of_expr: '*long' | ||
operand: | ||
decl_ref_expr: 'long' lvalue | ||
name: res | ||
|
||
assign_expr: '_Bool' | ||
lhs: | ||
decl_ref_expr: '_Bool' lvalue | ||
name: overflowed | ||
rhs: | ||
builtin_call_expr: '_Bool' | ||
name: __builtin_mul_overflow | ||
args: | ||
implicit_cast: (lval_to_rval) 'char' | ||
decl_ref_expr: 'char' lvalue | ||
name: x | ||
implicit_cast: (lval_to_rval) 'unsigned int' | ||
decl_ref_expr: 'unsigned int' lvalue | ||
name: y | ||
addr_of_expr: '*long' | ||
operand: | ||
decl_ref_expr: 'long' lvalue | ||
name: res | ||
|
||
implicit_return: 'void' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdckdint.h> | ||
|
||
enum E { | ||
A, | ||
}; | ||
|
||
void foo(void) { | ||
int x = 0; | ||
unsigned y = 2; | ||
_Bool b, overflowed; | ||
enum E e; | ||
unsigned res; | ||
const unsigned const_res; | ||
unsigned arr[2]; | ||
|
||
overflowed = ckd_add(&res, x, y); | ||
overflowed = ckd_sub(&res, x, y); | ||
overflowed = ckd_mul(&res, x, y); | ||
overflowed = ckd_add(arr, x, y); | ||
|
||
overflowed = ckd_add(res, x, y); // non-pointer result | ||
overflowed = ckd_sub(&res, 1.2, y); // non-int argument | ||
overflowed = ckd_mul(&b, x, y); // pointer to boolean result | ||
overflowed = ckd_add(&const_res, x, y); // pointer to const result | ||
overflowed = ckd_sub(&e, x, y); // pointer to enumerated result | ||
overflowed = ckd_mul((void *)&res, x, y); // pointer to non-int result | ||
|
||
} | ||
|
||
#define EXPECTED_ERRORS "stdckdint.c:21:18: error: result argument to overflow builtin must be a pointer to a non-const integer ('unsigned int' invalid)" \ | ||
"stdckdint.h:7:60: note: expanded from here" \ | ||
"stdckdint.c:21:26: note: expanded from here" \ | ||
"stdckdint.c:22:18: error: operand argument to overflow builtin must be an integer ('double' invalid)" \ | ||
"stdckdint.h:8:54: note: expanded from here" \ | ||
"stdckdint.c:22:32: note: expanded from here" \ | ||
"stdckdint.c:23:18: error: result argument to overflow builtin must be a pointer to a non-const integer ('_Bool *' invalid)" \ | ||
"stdckdint.h:9:60: note: expanded from here" \ | ||
"stdckdint.c:23:26: note: expanded from here" \ | ||
"stdckdint.c:24:18: error: result argument to overflow builtin must be a pointer to a non-const integer ('const unsigned int *' invalid)" \ | ||
"stdckdint.h:7:60: note: expanded from here" \ | ||
"stdckdint.c:24:26: note: expanded from here" \ | ||
"stdckdint.c:25:18: error: result argument to overflow builtin must be a pointer to a non-const integer ('enum E *' invalid)" \ | ||
"stdckdint.h:8:60: note: expanded from here" \ | ||
"stdckdint.c:25:26: note: expanded from here" \ | ||
"stdckdint.c:26:18: error: result argument to overflow builtin must be a pointer to a non-const integer ('void *' invalid)" \ | ||
"stdckdint.h:9:60: note: expanded from here" \ | ||
"stdckdint.c:26:26: note: expanded from here" \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdckdint.h> | ||
|
||
void foo(void) { | ||
char x = 0; | ||
unsigned y = 2; | ||
_Bool overflowed; | ||
long res; | ||
|
||
overflowed = ckd_add(&res, x, y); | ||
overflowed = ckd_sub(&res, x, y); | ||
overflowed = ckd_mul(&res, x, y); | ||
} |