Skip to content

Commit d9a7510

Browse files
coslusim642
authored andcommitted
Fix C11 generic warnings
Partially cherry-picked from 898fdf1.
1 parent 203f283 commit d9a7510

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

src/check.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ let rec checkType (t: typ) (ctx: ctxType) =
278278
(fun (an, at, aa) ->
279279
checkType at CTFArg;
280280
checkAttributes aa) (argsToList targs)
281+
| TDefault -> ()
281282

282283
(* Check that a type is a promoted integral type *)
283284
and checkIntegralType (t: typ) =
@@ -618,7 +619,12 @@ and checkExp (isconst: bool) (e: exp) : typ =
618619
(* | TComp _ -> E.s (bug "Cast of a composite type") *)
619620
| TVoid _ -> E.s (bug "Cast of a void type")
620621
| _ -> tres
621-
end)
622+
end
623+
| Generic (exp, lst) ->
624+
let typ = checkExp false exp in
625+
List.iter (fun (t, e) -> checkType t CTExp; ignore (checkExp false e)) lst;
626+
typ
627+
)
622628
() (* The argument of withContext *)
623629

624630
and checkInit (i: init) : typ =

src/cil.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,14 @@ let rec typeOf (e: exp) : typ =
19601960
TArray (t,_, a) -> TPtr(t, a)
19611961
| _ -> E.s (E.bug "typeOf: StartOf on a non-array")
19621962
end
1963-
| Generic (e, lst) -> match lst with (t, e1) :: rest -> typeOf e1 | _ -> voidType (* TODO: implement properly *)
1963+
| Generic (exp, lst) ->
1964+
let typeOfExp = typeOf exp in
1965+
let rec findType lst =
1966+
match lst with
1967+
[] -> voidType
1968+
| (t, e) :: rest -> if t = typeOfExp then typeOf e else findType rest
1969+
in
1970+
findType lst
19641971

19651972
and typeOfInit (i: init) : typ =
19661973
match i with

src/ext/pta/ptranal.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ and analyze_expr (e : exp ) : A.tau =
257257
| SizeOfE _ -> A.bottom ()
258258
| Imag __ -> A.bottom ()
259259
| Real __ -> A.bottom ()
260+
| Generic (_, _) -> failwith "not implemented yet"
260261
in
261262
H.add expressions e result;
262263
result

src/frontc/cabs2cil.ml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ let docEnv () =
543543
H.iter (fun k d -> acc := (k, d) :: !acc) env;
544544
docList ~sep:line (fun (k, d) -> dprintf " %s -> %a" k doone d) () !acc
545545

546+
let rec stripParenLocal e = match e with
547+
| A.PAREN e2 -> stripParenLocal e2
548+
| _ -> e
546549

547550

548551
(* Add a new variable. Do alpha-conversion if necessary *)
@@ -666,6 +669,7 @@ let rec stripConstLocalType (t: typ) : typ =
666669
let a' = dc a in if a != a' then TVoid a' else t
667670
| TBuiltin_va_list a ->
668671
let a' = dc a in if a != a' then TBuiltin_va_list a' else t
672+
| TDefault -> t
669673

670674

671675
let constFoldTypeVisitor = object (self)
@@ -4847,24 +4851,29 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
48474851
| A.EXPR_PATTERN _ -> E.s (E.bug "EXPR_PATTERN in cabs2cil input")
48484852

48494853
| A.GENERIC (expr, lst) ->
4850-
let get_exp tupel = match tupel with _, e, _ -> e in
4851-
let exp = get_exp (doExp false expr (AExp None)) in
4852-
let rec make_cil_list cabs_l cil_l = match cabs_l with
4854+
let get_exp (_, e, _) = e in
4855+
let exp = get_exp (doExp false (stripParenLocal expr) (AExp None)) in
4856+
let cil_list =
4857+
let rec make_cil_list cabs_l cil_l = match cabs_l with
48534858
| [] -> cil_l
4854-
| (s, e) :: rest -> make_cil_list rest (((doOnlyType s JUSTBASE),get_exp (doExp false e (AExp None))) :: cil_l)
4859+
| (s, e) :: rest -> make_cil_list rest (((doOnlyType s JUSTBASE),get_exp (doExp false (stripParenLocal e) (AExp None))) :: cil_l)
4860+
in
4861+
make_cil_list lst []
48554862
in
4856-
let cil_list = make_cil_list lst [] in
48574863
let exp_typ = typeOf exp in
48584864
let default_typ = ref voidType in
48594865
let typ =
4860-
let rec get_typ l = match l with
4866+
let rec get_typ lst = match lst with
48614867
| (TDefault, e) :: rest -> default_typ := typeOf e; get_typ rest
48624868
| (t, e) :: rest -> if t = exp_typ then typeOf e else get_typ rest
4863-
| [] -> !default_typ
4869+
| [] ->
4870+
if !default_typ = voidType then
4871+
E.s(error "Controlling expression of generic selection is not compatible with any association")
4872+
else !default_typ
48644873
in
48654874
get_typ cil_list
48664875
in
4867-
finishExp empty (Generic(exp, (make_cil_list lst []))) typ
4876+
finishExp empty (Generic(exp, (cil_list))) typ
48684877

48694878
with e when continueOnError -> begin
48704879
(*ignore (E.log "error in doExp (%s)" (Printexc.to_string e));*)
@@ -6833,11 +6842,6 @@ and doStatement (s : A.statement) : chunk =
68336842
consLabel "booo_statement" empty (convLoc (C.get_statementloc s)) false
68346843
end
68356844

6836-
6837-
let rec stripParenLocal e = match e with
6838-
| A.PAREN e2 -> stripParenLocal e2
6839-
| _ -> e
6840-
68416845
class stripParenClass : V.cabsVisitor = object (self)
68426846
inherit V.nopCabsVisitor
68436847

src/frontc/cprint.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ and print_type_spec = function
196196
(print_enum_items enum_items)
197197
| TtypeofE e -> printl ["__typeof__";"("]; print_expression e; print ") "
198198
| TtypeofT (s,d) -> printl ["__typeof__";"("]; print_onlytype (s, d); print ") "
199+
| Tdefault -> print "default "
199200

200201

201202
(* print "struct foo", but with specified keyword and a list of
@@ -403,6 +404,7 @@ and get_operator exp =
403404
| TYPE_SIZEOF _ -> ("", 16)
404405
| EXPR_ALIGNOF exp -> ("", 16)
405406
| TYPE_ALIGNOF _ -> ("", 16)
407+
| GENERIC _ -> ("", 16)
406408
| IMAG exp -> ("", 16)
407409
| REAL exp -> ("", 16)
408410
| CLASSIFYTYPE exp -> ("", 16)
@@ -538,6 +540,20 @@ and print_expression_level (lvl: int) (exp : expression) =
538540
printl ["__alignof__";"("];
539541
print_onlytype (bt, dt);
540542
print ")"
543+
| GENERIC (exp, lst) ->
544+
let print_generic_list l =
545+
match l with
546+
[] -> ()
547+
| (s, e) :: tl ->
548+
print ", ";
549+
print_onlytype (s, JUSTBASE);
550+
print ": ";
551+
print_expression_level 0 e;
552+
in
553+
printl ["_Generic";"("];
554+
print_expression_level 0 exp;
555+
print_generic_list lst;
556+
print ")"
541557
| IMAG exp ->
542558
printl ["__imag__";"("];
543559
print_expression_level 0 exp;

0 commit comments

Comments
 (0)