Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/frontc/cabs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type typeSpecifier = (* Merge all specifiers into one type *)
| Tenum of string * enum_item list option * attribute list
| TtypeofE of expression (* GCC __typeof__ *)
| TtypeofT of specifier * decl_type (* GCC __typeof__ *)
| Tatomic of specifier * decl_type (* C11 _Atomic(type-name) *)
| Tdefault (** "default" in generic associations *)
| Tauto (** GCC __auto_type *)

Expand Down
14 changes: 14 additions & 0 deletions src/frontc/cabs2cil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2859,6 +2859,20 @@ let rec doSpecList (suggestedAnonName: string) (* This string will be part of
| [A.TtypeofT (specs, dt)] ->
let typ = doOnlyType specs dt in
typ
| [A.Tatomic (specs, dt)] ->
let typ = doOnlyType specs dt in
(* C11 6.7.2.4: the operand may not be an array, function,
atomic or qualified type. Only its outermost level matters. *)
(match unrollType typ with
| TArray _ | TFun _ ->
E.s (error "Array or function type in _Atomic type specifier")
| _ -> ());
let attrs = typeAttrs typ in
if List.exists (fun q -> hasAttribute q attrs)
["atomic"; "const"; "volatile"; "restrict"] then
E.s (error "Atomic or qualified type in _Atomic type specifier");
(* Qualify the completed type, including its abstract declarator. *)
typeAddAttributes [Attr ("atomic", [])] typ
| [A.Tauto] -> raise TautoEncountered

| _ ->
Expand Down
4 changes: 4 additions & 0 deletions src/frontc/cabsvisit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ and childrenTypeSpecifier vis ts =
let s' = visitCabsSpecifier vis s in
let dt' = visitCabsDeclType vis false dt in
if s != s' || dt != dt' then TtypeofT (s', dt') else ts
| Tatomic (s, dt) ->
let s' = visitCabsSpecifier vis s in
let dt' = visitCabsDeclType vis false dt in
if s != s' || dt != dt' then Tatomic (s', dt') else ts
| ts -> ts

and childrenSpecElem (vis: cabsVisitor) (se: spec_elem) : spec_elem =
Expand Down
8 changes: 7 additions & 1 deletion src/frontc/cparser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,6 @@ decl_spec_list_common: /* ISO 6.7 */
| REGISTER decl_spec_list_opt { SpecStorage REGISTER :: $2, $1}
/* ISO 6.7.2 */
| type_spec decl_spec_list_opt_no_named { SpecType (fst $1) :: $2, snd $1 }
| ATOMIC LPAREN decl_spec_list RPAREN decl_spec_list_opt_no_named { (fst $3) @ SpecCV(CV_ATOMIC) :: $5, $1 }
/* ISO 6.7.4 */
| INLINE decl_spec_list_opt { SpecInline :: $2, $1 }
| NORETURN decl_spec_list_opt { SpecNoreturn :: $2, $1 }
Expand Down Expand Up @@ -1149,6 +1148,12 @@ type_spec: /* ISO 6.7.2 */
| ENUM just_attributes LBRACE enum_list maybecomma RBRACE
{ Tenum ("", Some (List.rev $4), $2), $1 }
| NAMED_TYPE { Tnamed (fst $1), snd $1 }
/* C11 6.7.2.4: _Atomic accepts a full type-name, including an optional
abstract declarator. Restrictions on the operand type are checked
in Cabs2cil.doSpecList.
*/
| ATOMIC LPAREN type_name RPAREN { let s, d = $3 in
Tatomic (s, d), $1 }
| TYPEOF LPAREN comma_expression RPAREN { TtypeofE (smooth_expression (fst $3)), $1 }
| TYPEOF LPAREN type_name RPAREN { let s, d = $3 in
TtypeofT (s, d), $1 }
Expand Down Expand Up @@ -1465,6 +1470,7 @@ attribute:
| CONST { ("const", []), $1 }
| RESTRICT { ("restrict",[]), $1 }
| VOLATILE { ("volatile",[]), $1 }
| ATOMIC { ("atomic",[]), $1 }
| STATIC { ("static",[]), $1 }
;

Expand Down
1 change: 1 addition & 0 deletions src/frontc/cprint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ and print_type_spec = function
(print_enum_items enum_items)
| TtypeofE e -> printl ["__typeof__";"("]; print_expression e; print ") "
| TtypeofT (s,d) -> printl ["__typeof__";"("]; print_onlytype (s, d); print ") "
| Tatomic (s,d) -> printl ["_Atomic";"("]; print_onlytype (s, d); print ") "
| Tdefault -> print "default " (* TODO: is this right? *)
| Tauto -> print "__auto_type"

Expand Down
3 changes: 2 additions & 1 deletion src/frontc/patch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ begin
(unifyString name1 name2)
| TtypeofE(exp1), TtypeofE(exp2) ->
(unifyExpr exp1 exp2)
| TtypeofT(spec1, dtype1), TtypeofT(spec2, dtype2) ->
| TtypeofT(spec1, dtype1), TtypeofT(spec2, dtype2)
| Tatomic(spec1, dtype1), Tatomic(spec2, dtype2) ->
(unifySpecifiers spec1 spec2) @
(unifyDeclType dtype1 dtype2)
| _ -> (
Expand Down
8 changes: 2 additions & 6 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,15 @@ scott-nolink/%: $(TESTDIR)/small2/%.c $(TARGET_DEP)



OBJDIR_DUNE := $(CILHOME)/../install/default/lib/goblint-cil
OBJDIR_MAKE := $(CILHOME)/_build/src
ifdef NATIVECAML
CMXA := cmxa
CAMLC := ocamlopt
else
CMXA := cma
CAMLC := ocamlc
endif

# Use Dune's package environment rather than a hard-coded install path.
testrun/% : $(SMALL1)/%.ml
ocamlfind $(CAMLC) -I $(OBJDIR_DUNE) -I $(OBJDIR_MAKE) -package zarith unix.$(CMXA) str.$(CMXA) zarith.$(CMXA) \
goblintCil.$(CMXA) \
ocamlfind $(CAMLC) -package goblint-cil -linkpkg \
$(EXEOUT) $(basename $<).exe $<
$(basename $<).exe
echo SUCCESS
Expand Down
43 changes: 43 additions & 0 deletions test/atomic_qualifier.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
open GoblintCil

let source = {|
int *plain;
_Atomic int *pointee;
int * _Atomic pointer;
_Atomic int * _Atomic both;
int * _Atomic array[4];
void (* _Atomic callback)(void);
int * _Atomic *nested;
int * _Atomic volatile qualified;
|}

let () =
initCIL ();
let filename = Filename.temp_file "cil-atomic-qualifier" ".c" in
Fun.protect ~finally:(fun () -> Sys.remove filename) (fun () ->
let channel = open_out filename in
output_string channel source;
close_out channel;
let file = Frontc.parse filename () in
let variables = List.filter_map (function
| GVar (vi, _, _) | GVarDecl (vi, _) -> Some vi
| _ -> None
) file.globals in
let atomic = [Attr ("atomic", [])] in
let atomic_int = TInt (IInt, atomic) in
(* Check the qualifier's binding, not just whether parsing succeeds. *)
List.iter (fun (name, expected) ->
let vi = List.find (fun vi -> vi.vname = name) variables in
assert (vi.vattr = []);
if typeSig vi.vtype <> typeSig expected then
failwith (name ^ ": incorrect atomic qualifier placement")
) [
"plain", TPtr (intType, []);
"pointee", TPtr (atomic_int, []);
"pointer", TPtr (intType, atomic);
"both", TPtr (atomic_int, atomic);
"array", TArray (TPtr (intType, atomic), Some (integer 4), []);
"callback", TPtr (TFun (voidType, Some [], false, []), atomic);
"nested", TPtr (TPtr (intType, atomic), []);
"qualified", TPtr (intType, [Attr ("atomic", []); Attr ("volatile", [])]);
])
130 changes: 130 additions & 0 deletions test/atomic_specifier.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
open GoblintCil

let source = {|
_Atomic(int) scalar;
_Atomic(int *) pointer;
_Atomic(_Atomic(int) *) both;
_Atomic(int *) *nested;
_Atomic(int *) array[4];
_Atomic(void (*)(void)) callback;
_Atomic(int (*)[4]) array_pointer;
_Atomic(const int *) const_pointee;
const _Atomic(int *) const_pointer;
typedef int *ptr_t;
_Atomic(ptr_t) from_typedef;
typedef _Atomic(int *) atomic_ptr_t;
atomic_ptr_t via_typedef;
typedef struct Cell Cell;
struct Cell { int value; };
struct Stack { _Atomic(Cell *) top; };
void param(_Atomic(Cell *) *p);
_Atomic(Cell *) cast(void *p) { return (_Atomic(Cell *))p; }
|}

let variable file name =
List.find_map (function
| GVar (vi, _, _) | GVarDecl (vi, _) when vi.vname = name -> Some vi
| GFun (fd, _) when fd.svar.vname = name -> Some fd.svar
| _ -> None
) file.globals |> Option.get

let compound file name =
List.find_map (function
| GCompTag (ci, _) when ci.cname = name -> Some ci
| _ -> None
) file.globals |> Option.get

let check_type name expected actual =
if typeSig actual <> typeSig expected then
failwith (name ^ ": incorrect atomic type")

let atomic = [Attr ("atomic", [])]
let const = [Attr ("const", []); Attr ("pconst", [])]

let check_file file =
let pointer = TPtr (intType, atomic) in
let cell_pointer = TPtr (TComp (compound file "Cell", []), atomic) in
List.iter (fun (name, expected) ->
check_type name expected (variable file name).vtype
) [
"scalar", TInt (IInt, atomic);
"pointer", pointer;
"both", TPtr (TInt (IInt, atomic), atomic);
"nested", TPtr (pointer, []);
"array", TArray (pointer, Some (integer 4), []);
"callback", TPtr (TFun (voidType, Some [], false, []), atomic);
"array_pointer", TPtr (TArray (intType, Some (integer 4), []), atomic);
"const_pointee", TPtr (TInt (IInt, const), atomic);
"const_pointer", TPtr (intType, atomic @ const);
"from_typedef", pointer;
"via_typedef", pointer;
"param", TFun (voidType, Some ["p", TPtr (cell_pointer, []), []], false, []);
"cast", TFun (cell_pointer, Some ["p", voidPtrType, []], false, []);
];
let top = List.find (fun fi -> fi.fname = "top") (compound file "Stack").cfields in
check_type "top" cell_pointer top.ftype

let () =
initCIL ();
let filename = Filename.temp_file "cil-atomic-specifier" ".c" in
Fun.protect ~finally:(fun () -> Sys.remove filename) (fun () ->
let write source =
let channel = open_out filename in
output_string channel source;
close_out channel
in
write source;
Whitetrack.enabled := true;
let cabs, file = Frontc.parse_with_cabs filename () in
check_file file;

(* The CABS printer must retain the full type name inside _Atomic. *)
let channel = open_out filename in
Cprint.printFile channel cabs;
Whitetrack.setOutput stdout;
close_out channel;
Whitetrack.enabled := false;
check_file (Frontc.parse filename ());

(* Visitors must reach both the specifiers and the abstract declarator. *)
let visitor = object
inherit Cabsvisit.nopCabsVisitor
method! vtypespec = function
| Cabs.Tint -> Cabsvisit.ChangeTo Cabs.Tlong
| _ -> Cabsvisit.DoChildren
method! vexpr = function
| Cabs.CONSTANT (Cabs.CONST_INT "4") ->
Cabsvisit.ChangeTo (Cabs.CONSTANT (Cabs.CONST_INT "5"))
| _ -> Cabsvisit.DoChildren
end in
let visited = Cabs2cil.convFile (Cabsvisit.visitCabsFile visitor cabs) in
check_type "visited pointer" (TPtr (longType, atomic))
(variable visited "pointer").vtype;
check_type "visited array pointer"
(TPtr (TArray (longType, Some (integer 5), []), atomic))
(variable visited "array_pointer").vtype;

(* The type-name form may not contain an already qualified, atomic,
array or function type, including when hidden behind a typedef. *)
List.iter (fun source ->
write source;
Frontc.resetErrors ();
let rejected =
try
ignore (Frontc.parse filename ());
!Errormsg.hadErrors
with Errormsg.Error -> true
in
if not rejected then failwith ("accepted invalid atomic type: " ^ source)
) [
"_Atomic(int[4]) bad;";
"typedef int array_t[4]; _Atomic(array_t) bad;";
"typedef int func_t(void); _Atomic(func_t) bad;";
"_Atomic(const int) bad;";
"_Atomic(int * const) bad;";
"typedef volatile int qual_t; _Atomic(qual_t) bad;";
"_Atomic(_Atomic(int)) bad;";
"typedef _Atomic(int *) atomic_t; _Atomic(atomic_t) bad;";
"int _Atomic(int) bad;";
];
Frontc.resetErrors ())
10 changes: 10 additions & 0 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
(modules alpha_names)
(libraries goblint-cil))

(test
(name atomic_qualifier)
(modules atomic_qualifier)
(libraries goblint-cil))

(test
(name atomic_specifier)
(modules atomic_specifier)
(libraries goblint-cil))

(rule
(alias runtest)
(deps (alias_rec ../cilly) (package goblint-cil) (source_tree .))
Expand Down
2 changes: 1 addition & 1 deletion test/small1/c11-atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
_Atomic const int * p1; // p is a pointer to an atomic const int
const _Atomic(int) * p3; // same
_Atomic(int) const * p3;
// _Atomic(int*) const p4; // unsupported as of now
_Atomic(int*) const p4;

typedef _Atomic _Bool atomic_bool;

Expand Down
Loading