-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parser: implement stdckdint.h #624
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure these are memoized properly? Getting a bit worried about something like ziglang/zig#7948 happening here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to know if that's happening - I had to up the eval branch quote for
checkVarArg
- is that why you ask?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No just because there are so many calls now. I checked with
@compileLog
and they are getting memoized properly.