From b701d27c7d43f0709a93375390480607e5611f69 Mon Sep 17 00:00:00 2001 From: sneeker Date: Sun, 12 Jun 2022 22:24:06 +0000 Subject: [PATCH] feat(dep): source header edges from compiler depfiles Parses -MMD -MF output, unfolding line continuations, so the include set comes from the compiler rather than from hand-maintained declarations. --- lib/dep.ml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/dep.ml diff --git a/lib/dep.ml b/lib/dep.ml new file mode 100644 index 0000000..284f6cb --- /dev/null +++ b/lib/dep.ml @@ -0,0 +1,23 @@ +let of_file path = + match Fs.read path with + | exception Sys_error _ -> None + | raw -> + let b = Buffer.create (String.length raw) in + let n = String.length raw in + let i = ref 0 in + while !i < n do + if raw.[!i] = '\\' && !i + 1 < n && raw.[!i + 1] = '\n' then (Buffer.add_char b ' '; i := !i + 2) + else if raw.[!i] = '\n' then (Buffer.add_char b ' '; incr i) + else (Buffer.add_char b raw.[!i]; incr i) + done; + let flat = Buffer.contents b in + match String.index_opt flat ':' with + | None -> None + | Some c -> + let rhs = String.sub flat (c + 1) (String.length flat - c - 1) in + let parts = + String.split_on_char ' ' rhs + |> List.map String.trim + |> List.filter (fun s -> s <> "" && s <> "\\") + in + Some parts