Skip to content
Snippets Groups Projects
Commit 0c717ebf authored by Patrick Baudin's avatar Patrick Baudin Committed by Andre Maroneze
Browse files

[lint] adds attribute 'indent-formatter=tool' where tool={ocp-indent,clang-format,default}

parent 2705371d
No related branches found
No related tags found
No related merge requests found
...@@ -49,12 +49,40 @@ let lines_from_in channel = ...@@ -49,12 +49,40 @@ let lines_from_in channel =
let acc = lines_from_buffer [] content 0 in let acc = lines_from_buffer [] content 0 in
List.rev acc List.rev acc
(**************************************************************************)
(* Supported indent formatter *)
type formatter_cmds =
{ mutable is_available : bool option ;
available_cmd : string ;
check_cmd: string ;
update_cmd: string
}
let c_indent_formatter =
{ is_available = None ;
available_cmd = "clang-format --version > /dev/null";
check_cmd = "clang-format --dry-run -Werror" ;
update_cmd = "clang-format -i"
}
type indent_formatter = Ocp_indent | Tool of formatter_cmds
let ml_indent_formatter = Ocp_indent
let parse_indent_formatter = function
| "unset" | "set" | "default" | "" -> None
| "ocp-indent" -> Some ml_indent_formatter
| "clang-format" -> Some (Tool c_indent_formatter)
| s -> Format.eprintf "Unsupported tool: %s@." s ; None
(**************************************************************************) (**************************************************************************)
(* Available Checks and corresponding attributes *) (* Available Checks and corresponding attributes *)
type checks = type checks =
{ eoleof : bool { eoleof : bool
; indent : bool ; indent : bool
; indent_formatter : indent_formatter option
; syntax : bool ; syntax : bool
; utf8 : bool ; utf8 : bool
} }
...@@ -62,22 +90,25 @@ type checks = ...@@ -62,22 +90,25 @@ type checks =
let no_checks = let no_checks =
{ eoleof = false { eoleof = false
; indent = false ; indent = false
; indent_formatter = None (* the default one *)
; syntax = false ; syntax = false
; utf8 = false ; utf8 = false
} }
let add_attr checks attr value = let add_attr checks attr value =
let value = value = "set" in let is_set v = v = "set" in
match attr with match attr with
| "check-eoleof" -> { checks with eoleof = value } | "check-eoleof" -> { checks with eoleof = is_set value }
| "check-indent" -> { checks with indent = value } | "check-indent" -> { checks with indent = is_set value }
| "check-syntax" -> { checks with syntax = value } | "check-syntax" -> { checks with syntax = is_set value }
| "check-utf8" -> { checks with utf8 = value } | "check-utf8" -> { checks with utf8 = is_set value }
| "indent-formatter" -> { checks with indent_formatter = parse_indent_formatter value }
| _ -> failwith (Format.sprintf "Unknown attr %s" attr) | _ -> failwith (Format.sprintf "Unknown attr %s" attr)
let handled_attr s = let handled_attr s =
s = "check-eoleof" || s = "check-indent" || s = "check-eoleof" || s = "check-indent" ||
s = "check-syntax" || s = "check-utf8" s = "check-syntax" || s = "check-utf8" ||
s = "indent-formatter"
let ignored_attr s = let ignored_attr s =
not (handled_attr s) not (handled_attr s)
...@@ -219,28 +250,31 @@ let check_ml_indent ~update file = ...@@ -219,28 +250,31 @@ let check_ml_indent ~update file =
(* C/H *) (* C/H *)
let clang_format_available = ref None let is_formatter_available indent_formatter =
let clang_format_available () = match indent_formatter.is_available with
match !clang_format_available with
| None -> | None ->
clang_format_available := let is_available = (0 = Sys.command indent_formatter.available_cmd) in
Some (0 = Sys.command "clang-format --version > /dev/null") ; indent_formatter.is_available <- Some is_available ;
Option.get !clang_format_available is_available
| Some available -> available | Some is_available -> is_available
let check_c_indent ~update file =
if not @@ clang_format_available () then true
else
let opt = if update then "-i" else "--dry-run -Werror" in
0 = Sys.command (Format.sprintf "clang-format %s \"%s\"" opt file)
exception Bad_ext exception Bad_ext
let check_indent ~update file = let check_indent ~tool ~update file =
match Filename.extension file with let tool = match tool with
| ".c" | ".h" -> check_c_indent ~update file | Some tool -> tool
| ".ml" | ".mli" -> check_ml_indent ~update file | None -> (* uses the default formatter *)
| _ -> raise Bad_ext match Filename.extension file with
| ".c" | ".h" -> Tool c_indent_formatter
| ".ml" | ".mli" -> ml_indent_formatter
| _ -> raise Bad_ext
in match tool with
| Ocp_indent -> check_ml_indent ~update file
| Tool indent_formatter ->
if not @@ is_formatter_available indent_formatter then true
else
let cmd = if update then indent_formatter.update_cmd else indent_formatter.check_cmd in
0 = Sys.command (Format.sprintf "%s \"%s\"" cmd file)
let res = ref true let res = ref true
...@@ -249,7 +283,7 @@ let res = ref true ...@@ -249,7 +283,7 @@ let res = ref true
let check ~verbose ~update file params = let check ~verbose ~update file params =
if verbose then if verbose then
Format.printf "Checking %s@." file ; Format.printf "Checking %s@." file ;
if Sys.is_directory file then () if Sys .is_directory file then ()
else begin else begin
let in_chan = open_in file in let in_chan = open_in file in
let content = read_buffered in_chan in let content = read_buffered in_chan in
...@@ -290,7 +324,7 @@ let check ~verbose ~update file params = ...@@ -290,7 +324,7 @@ let check ~verbose ~update file params =
(* Indentation *) (* Indentation *)
try try
if params.indent then if params.indent then
if not @@ check_indent ~update file then begin if not @@ check_indent ~tool:params.indent_formatter ~update file then begin
Format.eprintf "Bad indentation for %s@." file ; Format.eprintf "Bad indentation for %s@." file ;
res := false res := false
end ; end ;
...@@ -318,7 +352,7 @@ let sort argspec = ...@@ -318,7 +352,7 @@ let sort argspec =
(* Main *) (* Main *)
let () = let () =
if not @@ clang_format_available () then if not @@ is_formatter_available c_indent_formatter then
Format.eprintf "clang-format unavailable, I will not check C files@." ; Format.eprintf "clang-format unavailable, I will not check C files@." ;
Arg.parse Arg.parse
(Arg.align (sort argspec)) (Arg.align (sort argspec))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment