Skip to content

Commit 2e73628

Browse files
authored
Merge pull request #204 from jprotopopov-ut/builtin-choose-expr-1
Try to evaluate `__builtin_choose_expr` arguments as constants if possible
2 parents 597cbec + a25e5db commit 2e73628

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/frontc/cabs2cil.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,7 +4459,7 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
44594459
the castTo do this work. This was necessary for
44604460
test/small1/union5, in which a transparent union is passed
44614461
as an argument *)
4462-
let (sa, a', att) = force_right_to_left_evaluation (doExp false a (AExp None)) in
4462+
let (sa, a', att) = force_right_to_left_evaluation (doExp asconst a (AExp None)) in
44634463
let (_, a'') = castTo ~kind:Implicit att at a' in (* C11 6.5.2.2.7 *)
44644464
(sa :: ss, a'' :: args')
44654465
| ([], args) -> (* No more types *)
@@ -4469,7 +4469,7 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
44694469
| [] -> ([], [])
44704470
| a :: args ->
44714471
let (ss, args') = loop args in
4472-
let (sa, a', at) = force_right_to_left_evaluation (doExp false a (AExp None)) in
4472+
let (sa, a', at) = force_right_to_left_evaluation (doExp asconst a (AExp None)) in
44734473
if isBuiltinChooseExprOrTgmath then
44744474
(* This built-in function is analogous to the `? :'
44754475
operator in C, except that the expression returned

test/small1/builtin_choose_expr2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Both bitfield widths shall be evaluated constantly
2+
struct S1 {
3+
int x : __builtin_choose_expr(sizeof(long) > sizeof(int) || sizeof(int) > sizeof(short), 1, 2),
4+
y : __builtin_choose_expr(sizeof(char) > sizeof(short) && sizeof(char) == sizeof(char), 3, 4);
5+
} s1;

test/testcil.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ sub addToGroup {
552552
addTest("test/builtin2 ");
553553
addTest("testrun/builtin3 ");
554554
addTest("testrun/builtin_choose_expr");
555+
addTest("test/builtin_choose_expr2");
555556
addTest("testrungcc/builtin_object_size _GNUCC=1 OPTIMIZE=1");
556557
addTest("testrun/builtin4 ");
557558
addTest("test/builtin5 ");

0 commit comments

Comments
 (0)