Skip to content

Commit 7797529

Browse files
authored
Merge pull request #207 from jprotopopov-ut/auto-type-nonpure-1
Permit impure expressions in `__auto_type`
2 parents 2779db8 + 3a0567d commit 7797529

4 files changed

Lines changed: 44 additions & 4 deletions

File tree

src/frontc/cabs2cil.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5892,12 +5892,10 @@ and createAutoLocal ((((n, ndt, a, cloc) : A.name), (inite: A.init_expression))
58925892
else
58935893
match inite with
58945894
| SINGLE_INIT exp ->
5895-
(match doPureExp exp with
5896-
| Some exp ->
5897-
let t = Cil.typeOf exp in
5895+
(match doExp false exp (AExp None) with (* doExp with AExp handles array and function types (AType would not!) *)
5896+
| (_, _, t) ->
58985897
let specs = t,NoStorage,false,[] in
58995898
createLocal specs name
5900-
| None -> E.s (error "__auto_type but init not pure")
59015899
)
59025900
| _ -> E.s (error "__auto_type but not SINGLE_INIT")
59035901
(* Must catch the Static local variables. Make them global *)

test/small1/auto_type1.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct dummmy {
2+
char arr[0x0bad];
3+
};
4+
5+
extern volatile struct dummmy side_effect1;
6+
struct dummmy side_effect2(struct dummmy);
7+
8+
void *fn() {
9+
__auto_type x = side_effect2(side_effect1);
10+
static struct S1 {
11+
int a : __builtin_choose_expr(sizeof(x) == 0x0bad, 1, -1);
12+
} s1 = {0};
13+
return &s1;
14+
}

test/small1/auto_type2.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#define ASSERT_TYPE3(_expr, _type, _c) \
2+
struct assertType##_c { \
3+
int x: _Generic(_expr, _type : 1, default : -1); \
4+
} assertValue##_c = {0}
5+
#define ASSERT_TYPE2(_expr, _type, _c) ASSERT_TYPE3(_expr, _type, _c)
6+
#define ASSERT_TYPE(_expr, _type) ASSERT_TYPE2(_expr, _type, __COUNTER__)
7+
8+
void fn1(const int arg[128]) {
9+
const int local[128] = {0};
10+
11+
typeof(arg) ptr4;
12+
typeof(local) ptr5 = {0};
13+
__auto_type ptr6 = arg;
14+
__auto_type ptr7 = local;
15+
16+
ASSERT_TYPE(&ptr4, const int **);
17+
ASSERT_TYPE(&ptr5, const int (*)[128]);
18+
ASSERT_TYPE(&ptr6, const int **);
19+
ASSERT_TYPE(&ptr7, const int **);
20+
}
21+
22+
int main() {
23+
int arg[128] = {0};
24+
fn1(arg);
25+
return 0;
26+
}

test/testcil.pl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ sub addToGroup {
537537

538538

539539
addTest("testrun/typeof1 ");
540+
addTest("test/auto_type1");
541+
addTest("testrun/auto_type2");
540542
addTest("testrun/semicolon _GNUCC=1");
541543

542544
addTest("merge-ar ");

0 commit comments

Comments
 (0)