Skip to content

Commit 69f20df

Browse files
Merge branch 'extended_float_types' into develop
2 parents 1a826a6 + e5c4a9c commit 69f20df

File tree

11 files changed

+88
-19
lines changed

11 files changed

+88
-19
lines changed

src/cil.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4278,7 +4278,8 @@ class defaultCilPrinterClass : cilPrinter = object (self)
42784278
method pAttr (Attr(an, args): attribute) : doc * bool =
42794279
(* Recognize and take care of some known cases *)
42804280
match an, args with
4281-
"const", [] -> nil, false (* don't print const directly, because of split local declarations *)
4281+
"atomic", [] -> text "_Atomic", false
4282+
| "const", [] -> nil, false (* don't print const directly, because of split local declarations *)
42824283
| "pconst", [] -> text "const", false (* pconst means print const *)
42834284
(* Put the aconst inside the attribute list *)
42844285
| "complex", [] when !c99Mode -> text "_Complex", false

src/frontc/cabs.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ and funspec =
9696
INLINE | VIRTUAL | EXPLICIT
9797

9898
and cvspec =
99-
CV_CONST | CV_VOLATILE | CV_RESTRICT | CV_COMPLEX
99+
CV_CONST | CV_VOLATILE | CV_RESTRICT | CV_COMPLEX | CV_ATOMIC
100100

101101
(* Type specifier elements. These appear at the start of a declaration *)
102102
(* Everywhere they appear in this file, they appear as a 'spec_elem list', *)
@@ -185,6 +185,7 @@ and definition =
185185
| TRANSFORMER of definition * definition list * cabsloc
186186
(* expression transformer: source and destination *)
187187
| EXPRTRANSFORMER of expression * expression * cabsloc
188+
| STATIC_ASSERT of expression * string * cabsloc
188189

189190

190191
(* the string is a file name, and then the list of toplevel forms *)

src/frontc/cabs2cil.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2704,6 +2704,7 @@ and convertCVtoAttr (src: A.cvspec list) : A.attribute list =
27042704
| CV_VOLATILE :: tl -> ("volatile",[]) :: (convertCVtoAttr tl)
27052705
| CV_RESTRICT :: tl -> ("restrict",[]) :: (convertCVtoAttr tl)
27062706
| CV_COMPLEX :: tl -> ("complex",[]) :: (convertCVtoAttr tl)
2707+
| CV_ATOMIC :: tl -> ("atomic",[]) :: (convertCVtoAttr tl)
27072708

27082709

27092710
and makeVarInfoCabs
@@ -6393,7 +6394,7 @@ and doDecl (isglobal: bool) : A.definition -> chunk = function
63936394
E.s (bug "doDecl returns non-empty statement for global"))
63946395
dl;
63956396
empty
6396-
6397+
| STATIC_ASSERT _ -> empty
63976398
| _ -> E.s (error "unexpected form of declaration")
63986399

63996400
and doTypedef ((specs, nl): A.name_group) =

src/frontc/cabshelper.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let getident () =
66
nextident := !nextident + 1;
77
!nextident
88

9-
let currentLoc () =
9+
let currentLoc () =
1010
let l, f, c, lc = Errormsg.getPosition () in
1111
{ lineno = l;
1212
filename = f;
@@ -17,8 +17,8 @@ let currentLoc () =
1717
endByteno = -1;
1818
endColumnno = -1;}
1919

20-
let cabslu = {lineno = -10;
21-
filename = "cabs loc unknown";
20+
let cabslu = {lineno = -10;
21+
filename = "cabs loc unknown";
2222
byteno = -10; columnno = -10;
2323
ident = 0;
2424
endLineno = -10; endByteno = -10; endColumnno = -10;}
@@ -27,9 +27,9 @@ let string_of_loc l =
2727
Printf.sprintf "%s:%d:%d-%d:%d" l.filename l.lineno l.columnno l.endLineno l.endColumnno
2828

2929
let joinLoc l1 l2 = match l1, l2 with
30-
| l1, l2 when l1.filename = l2.filename && l1.endByteno < 0 && l2.endByteno < 0 && l1.byteno <= l2.byteno ->
30+
| l1, l2 when l1.filename = l2.filename && l1.endByteno < 0 && l2.endByteno < 0 && l1.byteno <= l2.byteno ->
3131
{l1 with endLineno = l2.lineno; endByteno = l2.byteno; endColumnno = l2.columnno}
32-
| l1, l2 when l1.filename = l2.filename && l1.endByteno = l2.endByteno && l1.byteno = l2.byteno ->
32+
| l1, l2 when l1.filename = l2.filename && l1.endByteno = l2.endByteno && l1.byteno = l2.byteno ->
3333
l1 (* alias fundefs *)
3434
| _, _ ->
3535
(* some code generators leave start and end into different files: https://github.com/goblint/cil/issues/54 *)
@@ -76,6 +76,7 @@ let get_definitionloc (d : definition) : cabsloc =
7676
| TRANSFORMER(_, _, l) -> l
7777
| EXPRTRANSFORMER(_, _, l) -> l
7878
| LINKAGE (_, l, _) -> l
79+
| STATIC_ASSERT (_,_,l) -> l
7980

8081
let get_statementloc (s : statement) : cabsloc =
8182
begin
@@ -103,23 +104,23 @@ begin
103104
end
104105

105106

106-
let explodeStringToInts (s: string) : int64 list =
107-
let rec allChars i acc =
107+
let explodeStringToInts (s: string) : int64 list =
108+
let rec allChars i acc =
108109
if i < 0 then acc
109110
else allChars (i - 1) (Int64.of_int (Char.code (String.get s i)) :: acc)
110111
in
111112
allChars (-1 + String.length s) []
112113

113114
let valueOfDigit chr =
114-
let int_value =
115+
let int_value =
115116
match chr with
116117
'0'..'9' -> (Char.code chr) - (Char.code '0')
117118
| 'a'..'z' -> (Char.code chr) - (Char.code 'a') + 10
118119
| 'A'..'Z' -> (Char.code chr) - (Char.code 'A') + 10
119120
| _ -> Errormsg.s (Errormsg.bug "not a digit") in
120121
Int64.of_int int_value
121-
122-
122+
123+
123124
open Pretty
124-
let d_cabsloc () cl =
125+
let d_cabsloc () cl =
125126
text cl.filename ++ text ":" ++ num cl.lineno

src/frontc/cabsvisit.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ and childrenDefinition vis d =
325325
| LINKAGE (n, l, dl) ->
326326
let dl' = mapNoCopyList (visitCabsDefinition vis) dl in
327327
if dl' != dl then LINKAGE (n, l, dl') else d
328-
328+
329+
| STATIC_ASSERT _ -> d
329330
| TRANSFORMER _ -> d
330331
| EXPRTRANSFORMER _ -> d
331332

src/frontc/clexer.mll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ let init_lexicon _ =
119119
("const", fun loc -> CONST loc);
120120
("__const", fun loc -> CONST loc);
121121
("__const__", fun loc -> CONST loc);
122+
("_Atomic", fun loc -> ATOMIC loc);
122123
("_Complex", fun loc -> COMPLEX loc);
123124
("__complex__", fun loc -> COMPLEX loc);
124125
("static", fun loc -> STATIC loc);
@@ -168,6 +169,7 @@ let init_lexicon _ =
168169
("__inline", fun loc -> INLINE loc);
169170
("_inline", fun loc -> IDENT ("_inline", loc));
170171
("_Noreturn", fun loc -> NORETURN loc);
172+
("_Static_assert", fun loc -> STATIC_ASSERT loc);
171173
("__attribute__", fun loc -> ATTRIBUTE loc);
172174
("__attribute", fun loc -> ATTRIBUTE loc);
173175
(*

src/frontc/cparser.mly

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ let transformOffsetOf (speclist, dtype) member =
257257
%token<Cabs.cabsloc> GENERIC NORETURN /* C11 */
258258
%token<Cabs.cabsloc> ENUM STRUCT TYPEDEF UNION
259259
%token<Cabs.cabsloc> SIGNED UNSIGNED LONG SHORT
260-
%token<Cabs.cabsloc> VOLATILE EXTERN STATIC CONST RESTRICT AUTO REGISTER
260+
%token<Cabs.cabsloc> VOLATILE EXTERN STATIC CONST ATOMIC RESTRICT AUTO REGISTER
261261
%token<Cabs.cabsloc> THREAD
262262

263263
%token<Cabs.cabsloc> SIZEOF ALIGNOF
@@ -290,7 +290,7 @@ let transformOffsetOf (speclist, dtype) member =
290290
%token<Cabs.cabsloc> IF
291291
%token ELSE
292292

293-
%token<Cabs.cabsloc> ATTRIBUTE INLINE ASM TYPEOF REAL IMAG FUNCTION__ PRETTY_FUNCTION__ CLASSIFYTYPE
293+
%token<Cabs.cabsloc> ATTRIBUTE INLINE STATIC_ASSERT ASM TYPEOF REAL IMAG FUNCTION__ PRETTY_FUNCTION__ CLASSIFYTYPE
294294
%token LABEL__
295295
%token<Cabs.cabsloc> BUILTIN_VA_ARG ATTRIBUTE_USED
296296
%token BUILTIN_VA_LIST
@@ -323,7 +323,7 @@ let transformOffsetOf (speclist, dtype) member =
323323
%left INF SUP INF_EQ SUP_EQ
324324
%left INF_INF SUP_SUP
325325
%left PLUS MINUS
326-
%left STAR SLASH PERCENT CONST RESTRICT VOLATILE COMPLEX
326+
%left STAR SLASH PERCENT CONST RESTRICT ATOMIC VOLATILE COMPLEX
327327
%right EXCLAM TILDE PLUS_PLUS MINUS_MINUS CAST RPAREN ADDROF SIZEOF ALIGNOF IMAG REAL CLASSIFYTYPE
328328
%left LBRACKET
329329
%left DOT ARROW LPAREN LBRACE
@@ -930,6 +930,21 @@ declaration: /* ISO 6.7.*/
930930
{ doDeclaration (joinLoc (snd $1) $3) (fst $1) $2 }
931931
| decl_spec_list SEMICOLON
932932
{ doDeclaration (joinLoc (snd $1) $2) (fst $1) [] }
933+
| static_assert_declaration { let (e, m, loc) = $1 in STATIC_ASSERT (e, m, loc) }
934+
;
935+
936+
static_assert_declaration:
937+
938+
| STATIC_ASSERT LPAREN expression RPAREN /* C23 */
939+
{
940+
(fst $3, "", $1)
941+
}
942+
| STATIC_ASSERT LPAREN expression COMMA string_constant RPAREN
943+
{
944+
(fst $3, fst $5, $1)
945+
}
946+
;
947+
933948
;
934949
init_declarator_list: /* ISO 6.7 */
935950
init_declarator { [$1] }
@@ -951,9 +966,11 @@ decl_spec_list: /* ISO 6.7 */
951966
| REGISTER decl_spec_list_opt { SpecStorage REGISTER :: $2, $1}
952967
/* ISO 6.7.2 */
953968
| type_spec decl_spec_list_opt_no_named { SpecType (fst $1) :: $2, snd $1 }
969+
| ATOMIC LPAREN decl_spec_list RPAREN decl_spec_list_opt_no_named { (fst $3) @ SpecCV(CV_ATOMIC) :: $5, $1 }
954970
/* ISO 6.7.4 */
955971
| INLINE decl_spec_list_opt { SpecInline :: $2, $1 }
956972
| NORETURN decl_spec_list_opt { SpecNoreturn :: $2, $1 }
973+
957974
| cvspec decl_spec_list_opt { (fst $1) :: $2, snd $1 }
958975
| attribute_nocv decl_spec_list_opt { SpecAttr (fst $1) :: $2, snd $1 }
959976
/* specifier pattern variable (must be last in spec list) */
@@ -1049,6 +1066,15 @@ struct_decl_list: /* (* ISO 6.7.2. Except that we allow empty structs. We
10491066

10501067
| error SEMICOLON struct_decl_list
10511068
{ $3 }
1069+
/*(* C11 allows static_assert-declaration *)*/
1070+
| static_assert_declaration {
1071+
[]
1072+
}
1073+
1074+
| static_assert_declaration SEMICOLON struct_decl_list {
1075+
$3
1076+
}
1077+
10521078
;
10531079
field_decl_list: /* (* ISO 6.7.2 *) */
10541080
field_decl { [$1] }
@@ -1294,6 +1320,7 @@ cvspec:
12941320
| VOLATILE { SpecCV(CV_VOLATILE), $1 }
12951321
| RESTRICT { SpecCV(CV_RESTRICT), $1 }
12961322
| COMPLEX { SpecCV(CV_COMPLEX), $1 }
1323+
| ATOMIC { SpecCV(CV_ATOMIC), $1 }
12971324
;
12981325

12991326
/*** GCC attributes ***/

src/frontc/cprint.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ let rec print_specifiers (specs: spec_elem list) =
148148
| CV_CONST -> "const"
149149
| CV_VOLATILE -> "volatile"
150150
| CV_RESTRICT -> "restrict"
151-
| CV_COMPLEX -> "complex")
151+
| CV_COMPLEX -> "complex"
152+
| CV_ATOMIC -> "_Atomic"
153+
)
152154
| SpecAttr al -> print_attribute al; space ()
153155
| SpecType bt -> print_type_spec bt
154156
| SpecPattern name -> printl ["@specifier";"(";name;")"]
@@ -897,6 +899,13 @@ and print_def def =
897899
print " }";
898900
force_new_line()
899901

902+
| STATIC_ASSERT(e,str,loc) ->
903+
setLoc(loc);
904+
print "_Static_assert(";
905+
print_expression e;
906+
print ",";
907+
print_string str;
908+
print ");";
900909

901910
(* sm: print a comment if the printComments flag is set *)
902911
and comprint (str : string) : unit =

test/small1/c11-atomic.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "testharness.h"
2+
#include <stdnoreturn.h>
3+
4+
_Atomic const int * p1; // p is a pointer to an atomic const int
5+
const _Atomic(int) * p3; // same
6+
_Atomic(int) const * p3;
7+
// _Atomic(int*) const p4; // unsupported as of now
8+
9+
typedef _Atomic _Bool atomic_bool;
10+
11+
12+
int main() {
13+
SUCCESS;
14+
}

test/small1/c11-static-assert.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "testharness.h"
2+
#include <stdnoreturn.h>
3+
4+
_Static_assert (2 <= 18, "blubb");
5+
6+
7+
int main() {
8+
_Static_assert (2 <= 18, "blubb");
9+
SUCCESS;
10+
}

0 commit comments

Comments
 (0)