check-git-clang-tidy-output.sh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. # TODO: integrate this script into pull request workflow.
  3. printError() {
  4. printf '\033[31mERROR:\033[0m %s\n' "$@"
  5. }
  6. printInfo() {
  7. printf '\033[32mINFO:\033[0m %s\n' "$@"
  8. }
  9. log_err() {
  10. printError "Setting up clang-tidy encountered an error"
  11. }
  12. set -eo pipefail
  13. trap '[ $? -eq 0 ] || log_err' EXIT
  14. printInfo "Fetching workspace info ..."
  15. WORKSPACE=$(bazel info workspace)
  16. BAZEL_ROOT=$(bazel info execution_root)
  17. printInfo "Generating compilation database ..."
  18. case "${OSTYPE}" in
  19. linux*)
  20. printInfo "Running on Linux, using clang to build C++ targets. Please make sure it is installed with install-llvm-binaries.sh"
  21. bazel build //ci/lint/generate_compile_commands:extract_compile_command //:ray_pkg --config=llvm \
  22. --experimental_action_listener=//ci/lint/generate_compile_commands:compile_command_listener;;
  23. darwin*)
  24. printInfo "Running on MacOS, assuming default C++ compiler is clang."
  25. bazel build //ci/lint/generate_compile_commands:extract_compile_command //:ray_pkg \
  26. --experimental_action_listener=//ci/lint/generate_compile_commands:compile_command_listener;;
  27. msys*)
  28. printInfo "Running on Windows, using clang-cl to build C++ targets. Please make sure it is installed."
  29. CC=clang-cl bazel build //ci/lint/generate_compile_commands:extract_compile_command //:ray_pkg \
  30. --experimental_action_listener=//ci/lint/generate_compile_commands:compile_command_listener;;
  31. esac
  32. printInfo "Assembling compilation database ..."
  33. TMPFILE=$(mktemp)
  34. printf '[\n' >"$TMPFILE"
  35. find "$BAZEL_ROOT" -name '*.compile_command.json' -exec cat {} + >>"$TMPFILE"
  36. printf '\n]\n' >>"$TMPFILE"
  37. if [[ "${OSTYPE}" =~ darwin* ]]; then
  38. sed -i '' "s|@BAZEL_ROOT@|$BAZEL_ROOT|g" "$TMPFILE"
  39. sed -i '' "s/}{/},\n{/g" "$TMPFILE"
  40. else
  41. sed -i "s|@BAZEL_ROOT@|$BAZEL_ROOT|g" "$TMPFILE"
  42. sed -i "s/}{/},\n{/g" "$TMPFILE"
  43. fi
  44. OUTFILE=$WORKSPACE/compile_commands.json
  45. if hash jq 2>/dev/null; then
  46. printInfo "Formatting compilation database ..."
  47. jq . "$TMPFILE" >"$OUTFILE"
  48. else
  49. printInfo "Can not find jq. Skip formatting compilation database."
  50. cp --no-preserve=mode "$TMPFILE" "$OUTFILE"
  51. fi
  52. # Compare against the master branch, because most development is done against it.
  53. base_commit="$(git merge-base HEAD master)"
  54. if [ "$base_commit" = "$(git rev-parse HEAD)" ]; then
  55. # Prefix of master branch, so compare against parent commit
  56. base_commit="$(git rev-parse HEAD^)"
  57. printInfo "Running clang-tidy against parent commit $base_commit"
  58. else
  59. printInfo "Running clang-tidy against parent commit $base_commit from master branch"
  60. fi
  61. trap - EXIT
  62. if git diff -U0 "$base_commit" | ci/lint/clang-tidy-diff.py -p1 -fix; then
  63. printInfo "clang-tidy passed."
  64. else
  65. printError "clang-tidy failed. See above for details including suggested fixes."
  66. printError
  67. printError "If you think the warning is too aggressive, the proposed fix is incorrect or are unsure about how to"
  68. printError "fix, feel free to raise the issue on the PR or Anyscale #learning-cplusplus Slack channel."
  69. printError
  70. printError "To run clang-tidy locally with fix suggestions, make sure clang and clang-tidy are installed and"
  71. printError "available in PATH (version 12 is preferred). Then run"
  72. printError "ci/lint/check-git-clang-tidy-output.sh"
  73. printError "from repo root."
  74. fi