check-names.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/sh
  2. #
  3. # Copyright The Mbed TLS Contributors
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. #
  6. # This file is provided under the Apache License 2.0, or the
  7. # GNU General Public License v2.0 or later.
  8. #
  9. # **********
  10. # Apache License 2.0:
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. # not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. #
  24. # **********
  25. #
  26. # **********
  27. # GNU General Public License v2.0 or later:
  28. #
  29. # This program is free software; you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation; either version 2 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. #
  39. # You should have received a copy of the GNU General Public License along
  40. # with this program; if not, write to the Free Software Foundation, Inc.,
  41. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  42. #
  43. # **********
  44. set -eu
  45. if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
  46. cat <<EOF
  47. $0 [-v]
  48. This script confirms that the naming of all symbols and identifiers in mbed
  49. TLS are consistent with the house style and are also self-consistent.
  50. -v If the script fails unexpectedly, print a command trace.
  51. EOF
  52. exit
  53. fi
  54. trace=
  55. if [ $# -ne 0 ] && [ "$1" = "-v" ]; then
  56. shift
  57. trace='-x'
  58. exec 2>check-names.err
  59. trap 'echo "FAILED UNEXPECTEDLY, status=$?";
  60. cat check-names.err' EXIT
  61. set -x
  62. fi
  63. printf "Analysing source code...\n"
  64. sh $trace tests/scripts/list-macros.sh
  65. tests/scripts/list-enum-consts.pl
  66. sh $trace tests/scripts/list-identifiers.sh
  67. sh $trace tests/scripts/list-symbols.sh
  68. FAIL=0
  69. printf "\nExported symbols declared in header: "
  70. UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' )
  71. if [ "x$UNDECLARED" = "x" ]; then
  72. echo "PASS"
  73. else
  74. echo "FAIL"
  75. echo "$UNDECLARED"
  76. FAIL=1
  77. fi
  78. diff macros identifiers | sed -n -e 's/< //p' > actual-macros
  79. for THING in actual-macros enum-consts; do
  80. printf 'Names of %s: ' "$THING"
  81. test -r $THING
  82. BAD=$( grep -v '^MBEDTLS_[0-9A-Z_]*[0-9A-Z]$' $THING || true )
  83. if [ "x$BAD" = "x" ]; then
  84. echo "PASS"
  85. else
  86. echo "FAIL"
  87. echo "$BAD"
  88. FAIL=1
  89. fi
  90. done
  91. for THING in identifiers; do
  92. printf 'Names of %s: ' "$THING"
  93. test -r $THING
  94. BAD=$( grep -v '^mbedtls_[0-9a-z_]*[0-9a-z]$' $THING || true )
  95. if [ "x$BAD" = "x" ]; then
  96. echo "PASS"
  97. else
  98. echo "FAIL"
  99. echo "$BAD"
  100. FAIL=1
  101. fi
  102. done
  103. printf "Likely typos: "
  104. sort -u actual-macros enum-consts > _caps
  105. HEADERS=$( ls include/mbedtls/*.h | egrep -v 'compat-1\.3\.h' )
  106. NL='
  107. '
  108. sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
  109. $HEADERS library/*.c \
  110. | grep MBEDTLS | sort -u > _MBEDTLS_XXX
  111. TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
  112. | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
  113. rm _MBEDTLS_XXX _caps
  114. if [ "x$TYPOS" = "x" ]; then
  115. echo "PASS"
  116. else
  117. echo "FAIL"
  118. echo "$TYPOS"
  119. FAIL=1
  120. fi
  121. if [ -n "$trace" ]; then
  122. set +x
  123. trap - EXIT
  124. rm check-names.err
  125. fi
  126. printf "\nOverall: "
  127. if [ "$FAIL" -eq 0 ]; then
  128. rm macros actual-macros enum-consts identifiers exported-symbols
  129. echo "PASSED"
  130. exit 0
  131. else
  132. echo "FAILED"
  133. exit 1
  134. fi