lint.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2018 Google
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # Lints C++ files for conformance with the Google C++ style guide
  15. # Joins the given arguments with the separator given as the first argument.
  16. function join() {
  17. local IFS="$1"
  18. shift
  19. echo "$*"
  20. }
  21. git_options=(
  22. -z # \0 terminate output
  23. )
  24. objc_lint_filters=(
  25. # Objective-C uses #import and does not use header guards
  26. -build/header_guard
  27. # Inline definitions of Objective-C blocks confuse
  28. -readability/braces
  29. # C-style casts are acceptable in Objective-C++
  30. -readability/casting
  31. # Objective-C needs use type 'long' for interop between types like NSInteger
  32. # and printf-style functions.
  33. -runtime/int
  34. # cpplint is generally confused by Objective-C mixing with C++.
  35. # * Objective-C method invocations in a for loop make it think its a
  36. # range-for
  37. # * Objective-C dictionary literals confuse brace spacing
  38. # * Empty category declarations ("@interface Foo ()") look like function
  39. # invocations
  40. -whitespace
  41. )
  42. objc_lint_options=(
  43. # cpplint normally excludes Objective-C++
  44. --extensions=h,m,mm
  45. # Objective-C style allows longer lines
  46. --linelength=100
  47. --filter=$(join , "${objc_lint_filters[@]}")
  48. )
  49. if [[ $# -gt 0 ]]; then
  50. # Interpret any command-line argument as a revision range
  51. command=(git diff --name-only --diff-filter=ACMR)
  52. git_options+=("$@")
  53. else
  54. # Default to operating on all files that match the pattern
  55. command=(git ls-files)
  56. fi
  57. # POSIX xargs is required to run commands at least once, but cpplint.py fails
  58. # (with huge help text) if no files are supplied. Apple xargs avoids invocation
  59. # if there are no arguments. Use a temporary file to avoid depending on/testing
  60. # for this feature.
  61. TEMP=$(mktemp -t lint-files-$$.XXXXXXXXXX)
  62. trap "rm '$TEMP'" EXIT
  63. # Straight C++ files get regular cpplint
  64. "${command[@]}" "${git_options[@]}" \
  65. -- 'Firestore/core/**/*.'{h,cc} \
  66. > "$TEMP"
  67. if [[ -s "$TEMP" ]]; then
  68. xargs -0 python scripts/cpplint.py --quiet 2>&1 < "$TEMP"
  69. fi
  70. CPP_STATUS=$?
  71. # Objective-C++ files get a looser cpplint
  72. "${command[@]}" "${git_options[@]}" \
  73. -- 'Firestore/Source/**/*.'{h,m,mm} \
  74. 'Firestore/Example/Tests/**/*.'{h,m,mm} \
  75. 'Firestore/core/**/*.mm' \
  76. > "$TEMP"
  77. if [[ -s "$TEMP" ]]; then
  78. xargs -0 python scripts/cpplint.py "${objc_lint_options[@]}" --quiet 2>&1 < "$TEMP"
  79. fi
  80. OBJC_STATUS=$?
  81. if [[ $CPP_STATUS != 0 || $OBJC_STATUS != 0 ]]; then
  82. exit 1
  83. fi