5 # "script-and-regex.regex": "/^(?P<severity>.*?)\n(?P<message>.*?)\n(?P<line>\\d),(?P<char>\\d)(\n(?P<original>.*?)>>>>\n(?P<replacement>.*?)<<<<?)$/s",
7 # Arcanist linter that invokes clang-format via clang/tools/clang-format/clang-format-diff.py
8 # stdout from this script is parsed into a regex and used by Arcanist.
9 # https://secure.phabricator.com/book/phabricator/article/arcanist_lint_script_and_regex/
11 # To skip running all linters when creating/updating a diff, use `arc diff --nolint`.
13 # advice severity level is completely non-disruptive.
14 # switch to warning or error if you want to prompt the user.
15 if ! hash clang-format
>/dev
/null
; then
17 echo "clang-format not found in user’s local PATH; not linting file."
21 if ! git rev-parse
--git-dir >/dev
/null
; then
23 echo "not in git repostitory; not linting file."
29 original_file
="$(mktemp)"
30 formatted_file
="$(mktemp)"
32 readonly original_file
33 readonly formatted_file
34 cp -p "${src_file}" "${original_file}"
35 cp -p "${src_file}" "${formatted_file}"
39 rm -f "${formatted_file}" "${original_file}"
42 trap 'cleanup' INT HUP QUIT TERM EXIT
44 # Arcanist can filter out lint messages for unchanged lines, but for that, we
45 # need to generate line by line lint messages. Instead, we generate one lint
46 # message on line 1, char 1 with file content edited using clang-format-diff.py
48 # We do not use git-clang-format because it wants to modify the index,
49 # and arc is already holding the lock.
51 # We do not look for clang-format-diff or clang-format-diff.py in the PATH
52 # because whether/how these are installed differs between distributions,
53 # and we have an executable copy in the tree anyway.
54 arc_base_commit
=$
(arc
which --show-base)
55 git diff-index
-U0 "${arc_base_commit}" "${src_file}" \
56 | clang
/tools
/clang-format
/clang-format-diff.py
-style file -i -p1
58 cp -p "${src_file}" "${formatted_file}"
59 cp -p "${original_file}" "${src_file}"
60 if ! diff -q "${src_file}" "${formatted_file}" > /dev
/null
; then
62 echo "clang-format suggested style edits found:"
63 echo "1,1" # line,char of start of replacement.
66 cat "${formatted_file}"