Setup clang-format as an Arcanist linter
Summary:
This uses clang-format-diff as a linter for Arcanist.
`arc lint` flow, also run as part of `arc diff` unless skipped with
`--nolint`, will now run the linter shell script on the changed files,
and prompt the user to accept the suggested changes.
Message when clang-format-diff is not installed:
{F6654094}
Example of the noise during code review when clang-format-diff is not installed:
https://reviews.llvm.org/differential/changeset/?ref=1115809
Prompt when clang-format-diff is installed and suggests edits:
{F6650223}
Reviewers: probinson, scott.linder
Reviewed By: scott.linder
Subscribers: scott.linder, MyDeveloperDay, JonasToth, danilaml, JDevlieghere, dberris, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D49116
2020-03-31 02:20:58 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
# "script-and-regex.regex": "/^(?P<severity>.*?)\n(?P<message>.*?)\n(?P<line>\\d),(?P<char>\\d)(\n(?P<original>.*?)>>>>\n(?P<replacement>.*?)<<<<?)$/s",
|
|
|
|
|
2020-04-04 03:48:28 +08:00
|
|
|
# Arcanist linter that invokes clang-format via clang/tools/clang-format/clang-format-diff.py
|
Setup clang-format as an Arcanist linter
Summary:
This uses clang-format-diff as a linter for Arcanist.
`arc lint` flow, also run as part of `arc diff` unless skipped with
`--nolint`, will now run the linter shell script on the changed files,
and prompt the user to accept the suggested changes.
Message when clang-format-diff is not installed:
{F6654094}
Example of the noise during code review when clang-format-diff is not installed:
https://reviews.llvm.org/differential/changeset/?ref=1115809
Prompt when clang-format-diff is installed and suggests edits:
{F6650223}
Reviewers: probinson, scott.linder
Reviewed By: scott.linder
Subscribers: scott.linder, MyDeveloperDay, JonasToth, danilaml, JDevlieghere, dberris, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D49116
2020-03-31 02:20:58 +08:00
|
|
|
# stdout from this script is parsed into a regex and used by Arcanist.
|
|
|
|
# https://secure.phabricator.com/book/phabricator/article/arcanist_lint_script_and_regex/
|
|
|
|
|
|
|
|
# To skip running all linters when creating/updating a diff, use `arc diff --nolint`.
|
|
|
|
|
2020-04-04 03:48:28 +08:00
|
|
|
# advice severity level is completely non-disruptive.
|
|
|
|
# switch to warning or error if you want to prompt the user.
|
|
|
|
if ! hash clang-format >/dev/null; then
|
Setup clang-format as an Arcanist linter
Summary:
This uses clang-format-diff as a linter for Arcanist.
`arc lint` flow, also run as part of `arc diff` unless skipped with
`--nolint`, will now run the linter shell script on the changed files,
and prompt the user to accept the suggested changes.
Message when clang-format-diff is not installed:
{F6654094}
Example of the noise during code review when clang-format-diff is not installed:
https://reviews.llvm.org/differential/changeset/?ref=1115809
Prompt when clang-format-diff is installed and suggests edits:
{F6650223}
Reviewers: probinson, scott.linder
Reviewed By: scott.linder
Subscribers: scott.linder, MyDeveloperDay, JonasToth, danilaml, JDevlieghere, dberris, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D49116
2020-03-31 02:20:58 +08:00
|
|
|
echo "advice"
|
2020-04-04 03:48:28 +08:00
|
|
|
echo "clang-format not found in user's PATH; not linting file."
|
|
|
|
echo "===="
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
if ! git rev-parse --git-dir >/dev/null; then
|
|
|
|
echo "advice"
|
|
|
|
echo "not in git repostitory; not linting file."
|
Setup clang-format as an Arcanist linter
Summary:
This uses clang-format-diff as a linter for Arcanist.
`arc lint` flow, also run as part of `arc diff` unless skipped with
`--nolint`, will now run the linter shell script on the changed files,
and prompt the user to accept the suggested changes.
Message when clang-format-diff is not installed:
{F6654094}
Example of the noise during code review when clang-format-diff is not installed:
https://reviews.llvm.org/differential/changeset/?ref=1115809
Prompt when clang-format-diff is installed and suggests edits:
{F6650223}
Reviewers: probinson, scott.linder
Reviewed By: scott.linder
Subscribers: scott.linder, MyDeveloperDay, JonasToth, danilaml, JDevlieghere, dberris, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D49116
2020-03-31 02:20:58 +08:00
|
|
|
echo "===="
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
src_file="${1}"
|
|
|
|
original_file="$(mktemp)"
|
|
|
|
formatted_file="$(mktemp)"
|
|
|
|
readonly src_file
|
|
|
|
readonly original_file
|
|
|
|
readonly formatted_file
|
|
|
|
cp -p "${src_file}" "${original_file}"
|
|
|
|
cp -p "${src_file}" "${formatted_file}"
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
rc=$?
|
|
|
|
rm "${formatted_file}" "${original_file}"
|
|
|
|
exit ${rc}
|
|
|
|
}
|
|
|
|
trap 'cleanup' INT HUP QUIT TERM EXIT
|
|
|
|
|
|
|
|
# Arcanist can filter out lint messages for unchanged lines, but for that, we
|
|
|
|
# need to generate line by line lint messages. Instead, we generate one lint
|
2020-04-04 03:48:28 +08:00
|
|
|
# message on line 1, char 1 with file content edited using clang-format-diff.py
|
|
|
|
#
|
|
|
|
# We do not use git-clang-format because it wants to modify the index,
|
|
|
|
# and arc is already holding the lock.
|
|
|
|
#
|
|
|
|
# We do not look for clang-format-diff or clang-format-diff.py in the PATH
|
|
|
|
# because whether/how these are installed differs between distributions,
|
|
|
|
# and we have an executable copy in the tree anyway.
|
|
|
|
arc_base_commit=$(arc which --show-base)
|
2020-10-12 05:20:01 +08:00
|
|
|
git diff-index -U0 "${arc_base_commit}" "${src_file}" \
|
2020-04-04 03:48:28 +08:00
|
|
|
| clang/tools/clang-format/clang-format-diff.py -style file -i -p1
|
Setup clang-format as an Arcanist linter
Summary:
This uses clang-format-diff as a linter for Arcanist.
`arc lint` flow, also run as part of `arc diff` unless skipped with
`--nolint`, will now run the linter shell script on the changed files,
and prompt the user to accept the suggested changes.
Message when clang-format-diff is not installed:
{F6654094}
Example of the noise during code review when clang-format-diff is not installed:
https://reviews.llvm.org/differential/changeset/?ref=1115809
Prompt when clang-format-diff is installed and suggests edits:
{F6650223}
Reviewers: probinson, scott.linder
Reviewed By: scott.linder
Subscribers: scott.linder, MyDeveloperDay, JonasToth, danilaml, JDevlieghere, dberris, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D49116
2020-03-31 02:20:58 +08:00
|
|
|
|
|
|
|
cp -p "${src_file}" "${formatted_file}"
|
|
|
|
cp -p "${original_file}" "${src_file}"
|
|
|
|
if ! diff -q "${src_file}" "${formatted_file}" > /dev/null ; then
|
|
|
|
echo "autofix"
|
|
|
|
echo "clang-format suggested style edits found:"
|
|
|
|
echo "1,1" # line,char of start of replacement.
|
|
|
|
cat "${src_file}"
|
|
|
|
echo ">>>>"
|
|
|
|
cat "${formatted_file}"
|
|
|
|
echo "<<<<"
|
|
|
|
fi
|