Skip to content

Commit

Permalink
Added a diagnostic for attributes dec after def
Browse files Browse the repository at this point in the history
The new warning is raise for function declaration after a function
definition that introduce new attributes, which are ignored.
Bug 23385
  • Loading branch information
bschommer authored May 7, 2018
1 parent 6657ff7 commit e2c2f22
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cparser/Diagnostics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type warning_type =
| Unused_parameter
| Wrong_ais_parameter
| Unused_ais_parameter
| Ignored_attributes

(* List of active warnings *)
let active_warnings: warning_type list ref = ref [
Expand All @@ -114,6 +115,7 @@ let active_warnings: warning_type list ref = ref [
Inline_asm_sdump;
Wrong_ais_parameter;
Unused_ais_parameter;
Ignored_attributes;
]

(* List of errors treated as warning *)
Expand Down Expand Up @@ -145,6 +147,7 @@ let string_of_warning = function
| Unused_parameter -> "unused-parameter"
| Wrong_ais_parameter -> "wrong-ais-parameter"
| Unused_ais_parameter -> "unused-ais-parameter"
| Ignored_attributes -> "ignored-attributes"

(* Activate the given warning *)
let activate_warning w () =
Expand Down Expand Up @@ -192,6 +195,7 @@ let wall () =
Unused_variable;
Unused_parameter;
Wrong_ais_parameter;
Ignored_attributes;
]

let wnothing () =
Expand Down Expand Up @@ -223,6 +227,7 @@ let werror () =
Unused_variable;
Wrong_ais_parameter;
Unused_ais_parameter;
Ignored_attributes;
]

(* Generate the warning key for the message *)
Expand Down Expand Up @@ -401,6 +406,7 @@ let warning_options =
error_option Unused_parameter @
error_option Wrong_ais_parameter @
error_option Unused_ais_parameter @
error_option Ignored_attributes @
[Exact ("-Wfatal-errors"), Set error_fatal;
Exact ("-fdiagnostics-color"), Ignore; (* Either output supports it or no color *)
Exact ("-fno-diagnostics-color"), Unset color_diagnostics;
Expand Down
1 change: 1 addition & 0 deletions cparser/Diagnostics.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type warning_type =
| Unused_parameter (** unused function parameter *)
| Wrong_ais_parameter (** wrong parameter type for ais replacement *)
| Unused_ais_parameter (** unused builtin ais parameter *)
| Ignored_attributes (** attributes declarations after definition *)

val warning : (string * int) -> warning_type -> ('a, Format.formatter, unit, unit, unit, unit) format6 -> 'a
(** [warning (f,c) w fmt arg1 ... argN] formats the arguments [arg1] to [argN] as warining according to
Expand Down
15 changes: 12 additions & 3 deletions cparser/Elab.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ let add_global_define loc name =
error loc "redefinition of '%s'" name;
global_defines := StringSet.add name !global_defines

let is_global_defined name =
StringSet.mem name !global_defines

let emit_elab ?(debuginfo = true) ?(linkage = false) env loc td =
let loc = elab_loc loc in
let dec ={ gdesc = td; gloc = loc } in
Expand Down Expand Up @@ -153,6 +156,12 @@ let combine_toplevel_definitions loc env s old_sto old_ty sto ty =
"redefinition of '%s' with a different type: %a vs %a"
s (print_typ env) old_ty (print_typ env) ty;
ty in
if is_global_defined s then begin
let old_attrs = attributes_of_type env old_ty
and new_attrs = attributes_of_type env ty in
if not (Cutil.incl_attributes new_attrs old_attrs) then
warning loc Ignored_attributes "attribute declaration must precede definition"
end;
let new_sto =
(* The only case not allowed is removing static *)
match old_sto,sto with
Expand Down Expand Up @@ -2238,10 +2247,10 @@ let enter_decdefs local loc env sto dl =
else sto in
(* enter ident in environment with declared type, because
initializer can refer to the ident *)
if init <> NO_INIT && not local then
add_global_define loc s;
let (id, sto', env1, ty, linkage) =
enter_or_refine_ident local loc env s sto1 ty in
if init <> NO_INIT && not local then
add_global_define loc s;
if not isfun && is_void_type env ty then
fatal_error loc "'%s' has incomplete type" s;
(* process the initializer *)
Expand Down Expand Up @@ -2415,9 +2424,9 @@ let elab_fundef env spec name defs body loc =
(ty_ret, params, vararg, attr)
| _ -> fatal_error loc "wrong type for function definition" in
(* Enter function in the environment, for recursive references *)
add_global_define loc s;
let (fun_id, sto1, env1, new_ty, _) =
enter_or_refine_ident false loc env1 s sto ty in
add_global_define loc s;
(* Take into account attributes from previous declarations of the function *)
let attr = attributes_of_type env1 new_ty in
let incomplete_param env ty =
Expand Down

0 comments on commit e2c2f22

Please sign in to comment.