check_GNU_style.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/sh
  2. # Checks some of the GNU style formatting rules in a set of patches.
  3. # Copyright (C) 2010, 2012 Free Software Foundation, Inc.
  4. # Contributed by Sebastian Pop <sebastian.pop@amd.com>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. usage() {
  17. cat <<EOF
  18. check_GNU_style.sh [patch]...
  19. Checks the patches for some of the GNU style formatting problems.
  20. When FILE is -, read standard input.
  21. Please note that these checks are not always accurate, and
  22. complete. The reference documentation of the GNU Coding Standards
  23. can be found here: http://www.gnu.org/prep/standards_toc.html
  24. and there are also some additional coding conventions for GCC:
  25. http://gcc.gnu.org/codingconventions.html
  26. EOF
  27. exit 1
  28. }
  29. test $# -eq 0 && usage
  30. inp=check_GNU_style.inp
  31. tmp=check_GNU_style.tmp
  32. # Remove $tmp on exit and various signals.
  33. trap "rm -f $inp $tmp" 0
  34. trap "rm -f $inp $tmp ; exit 1" 1 2 3 5 9 13 15
  35. grep -nH '^+' $* \
  36. | grep -v ':+++' \
  37. > $inp
  38. # Grep
  39. g (){
  40. msg="$1"
  41. arg="$2"
  42. cat $inp \
  43. | egrep --color=always -- "$arg" \
  44. > $tmp && printf "\n$msg\n"
  45. cat $tmp
  46. }
  47. # And Grep
  48. ag (){
  49. msg="$1"
  50. arg1="$2"
  51. arg2="$3"
  52. cat $inp \
  53. | egrep --color=always -- "$arg1" \
  54. | egrep --color=always -- "$arg2" \
  55. > $tmp && printf "\n$msg\n"
  56. cat $tmp
  57. }
  58. # reVerse Grep
  59. vg (){
  60. msg="$1"
  61. varg="$2"
  62. arg="$3"
  63. cat $inp \
  64. | egrep -v -- "$varg" \
  65. | egrep --color=always -- "$arg" \
  66. > $tmp && printf "\n$msg\n"
  67. cat $tmp
  68. }
  69. col (){
  70. msg="$1"
  71. cat $inp \
  72. | awk -F':\\+' '{ if (length($2) > 80) print $0}' \
  73. > $tmp
  74. if [ -s $tmp ]; then
  75. printf "\n$msg\n"
  76. cat $tmp
  77. fi
  78. }
  79. col 'Lines should not exceed 80 characters.'
  80. g 'Blocks of 8 spaces should be replaced with tabs.' \
  81. ' {8}'
  82. g 'Trailing whitespace.' \
  83. '[[:space:]]$'
  84. g 'Space before dot.' \
  85. '[[:alnum:]][[:blank:]]+\.'
  86. g 'Dot, space, space, new sentence.' \
  87. '[[:alnum:]]\.([[:blank:]]|[[:blank:]]{3,})[A-Z0-9]'
  88. g 'Dot, space, space, end of comment.' \
  89. '[[:alnum:]]\.([[:blank:]]{0,1}|[[:blank:]]{3,})\*/'
  90. g 'Sentences should end with a dot. Dot, space, space, end of the comment.' \
  91. '[[:alnum:]][[:blank:]]*\*/'
  92. vg 'There should be exactly one space between function name and parentheses.' \
  93. '\#define' '[[:alnum:]]([[:blank:]]{2,})?\('
  94. g 'There should be no space before closing parentheses.' \
  95. '[[:graph:]][[:blank:]]+\)'
  96. ag 'Braces should be on a separate line.' \
  97. '\{' 'if[[:blank:]]\(|while[[:blank:]]\(|switch[[:blank:]]\('