clang-format.bash 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bash
  2. #=============================================================================
  3. # Copyright 2015-2016 Kitware, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #=============================================================================
  17. usage='usage: clang-format.bash [<options>] [--]
  18. --clang-format <tool> Use given clang-format tool.
  19. '
  20. die() {
  21. echo "$@" 1>&2; exit 1
  22. }
  23. #-----------------------------------------------------------------------------
  24. # Parse command-line arguments.
  25. clang_format=''
  26. while test "$#" != 0; do
  27. case "$1" in
  28. --clang-format) shift; clang_format="$1" ;;
  29. --) shift ; break ;;
  30. -*) die "$usage" ;;
  31. *) break ;;
  32. esac
  33. shift
  34. done
  35. test "$#" = 0 || die "$usage"
  36. # Find a default tool.
  37. tools='
  38. clang-format
  39. clang-format-3.8
  40. '
  41. if test "x$clang_format" = "x"; then
  42. for tool in $tools; do
  43. if type -p "$tool" >/dev/null; then
  44. clang_format="$tool"
  45. break
  46. fi
  47. done
  48. fi
  49. # Verify that we have a tool.
  50. if ! type -p "$clang_format" >/dev/null; then
  51. echo "Unable to locate '$clang_format'"
  52. exit 1
  53. fi
  54. # Filter sources to which our style should apply.
  55. git ls-files -z -- '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hh' '*.hpp' '*.hxx' |
  56. # Exclude lexer/parser generator input and output.
  57. egrep -z -v '^Source/cmCommandArgumentLexer\.' |
  58. egrep -z -v '^Source/cmCommandArgumentParser(\.y|\.cxx|Tokens\.h)' |
  59. egrep -z -v '^Source/cmDependsJavaLexer\.' |
  60. egrep -z -v '^Source/cmDependsJavaParser(\.y|\.cxx|Tokens\.h)' |
  61. egrep -z -v '^Source/cmExprLexer\.' |
  62. egrep -z -v '^Source/cmExprParser(\.y|\.cxx|Tokens\.h)' |
  63. egrep -z -v '^Source/cmFortranLexer\.' |
  64. egrep -z -v '^Source/cmFortranParser(\.y|\.cxx|Tokens\.h)' |
  65. egrep -z -v '^Source/cmListFileLexer(\.in\.l|\.c)' |
  66. # Exclude third-party sources.
  67. egrep -z -v '^Source/(cm_sha2|bindexplib)' |
  68. egrep -z -v '^Source/(kwsys|CursesDialog/form)/' |
  69. egrep -z -v '^Utilities/(KW|cm).*/' |
  70. # Exclude reference content.
  71. egrep -z -v '^Tests/Module/GenerateExportHeader/reference/' |
  72. # Exclude manually-formatted sources (e.g. with long lines).
  73. egrep -z -v '^Tests/PositionIndependentTargets/pic_test.h' |
  74. # Exclude sources with encoding not suported by clang-format.
  75. egrep -z -v '^Tests/RunCMake/CommandLine/cmake_depends/test_UTF-16LE.h' |
  76. # Update sources in-place.
  77. xargs -0 "$clang_format" -i