Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 1 | #!/bin/sh |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 2 | # |
Gilles Peskine | 017adc7 | 2019-05-22 18:22:45 +0200 | [diff] [blame] | 3 | # Copyright (c) 2015-2019, ARM Limited, All Rights Reserved |
Bence Szépkúti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame^] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Gilles Peskine | 017adc7 | 2019-05-22 18:22:45 +0200 | [diff] [blame] | 19 | |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 20 | set -eu |
| 21 | |
Gilles Peskine | 017adc7 | 2019-05-22 18:22:45 +0200 | [diff] [blame] | 22 | if [ $# -ne 0 ] && [ "$1" = "--help" ]; then |
| 23 | cat <<EOF |
| 24 | $0 [-v] |
| 25 | This script confirms that the naming of all symbols and identifiers in mbed |
| 26 | TLS are consistent with the house style and are also self-consistent. |
| 27 | |
| 28 | -v If the script fails unexpectedly, print a command trace. |
| 29 | EOF |
| 30 | exit |
| 31 | fi |
| 32 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 33 | if grep --version|head -n1|grep GNU >/dev/null; then :; else |
Ron Eldor | bf007d2 | 2016-12-15 14:42:37 +0200 | [diff] [blame] | 34 | echo "This script requires GNU grep.">&2 |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 35 | exit 1 |
| 36 | fi |
| 37 | |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 38 | trace= |
| 39 | if [ $# -ne 0 ] && [ "$1" = "-v" ]; then |
| 40 | shift |
| 41 | trace='-x' |
| 42 | exec 2>check-names.err |
| 43 | trap 'echo "FAILED UNEXPECTEDLY, status=$?"; |
| 44 | cat check-names.err' EXIT |
| 45 | set -x |
| 46 | fi |
| 47 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 48 | printf "Analysing source code...\n" |
| 49 | |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 50 | sh $trace tests/scripts/list-macros.sh |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 51 | tests/scripts/list-enum-consts.pl |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 52 | sh $trace tests/scripts/list-identifiers.sh |
| 53 | sh $trace tests/scripts/list-symbols.sh |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 54 | |
| 55 | FAIL=0 |
| 56 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 57 | printf "\nExported symbols declared in header: " |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 58 | UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' ) |
Manuel Pégourié-Gonnard | 9afdc83 | 2015-08-04 17:15:13 +0200 | [diff] [blame] | 59 | if [ "x$UNDECLARED" = "x" ]; then |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 60 | echo "PASS" |
| 61 | else |
| 62 | echo "FAIL" |
| 63 | echo "$UNDECLARED" |
| 64 | FAIL=1 |
| 65 | fi |
| 66 | |
| 67 | diff macros identifiers | sed -n -e 's/< //p' > actual-macros |
| 68 | |
| 69 | for THING in actual-macros enum-consts; do |
| 70 | printf "Names of $THING: " |
| 71 | test -r $THING |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 72 | BAD=$( grep -v '^MBEDTLS_[0-9A-Z_]*[0-9A-Z]$\|^YOTTA_[0-9A-Z_]*[0-9A-Z]$' $THING || true ) |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 73 | if [ "x$BAD" = "x" ]; then |
| 74 | echo "PASS" |
| 75 | else |
| 76 | echo "FAIL" |
| 77 | echo "$BAD" |
| 78 | FAIL=1 |
| 79 | fi |
| 80 | done |
| 81 | |
| 82 | for THING in identifiers; do |
| 83 | printf "Names of $THING: " |
| 84 | test -r $THING |
| 85 | BAD=$( grep -v '^mbedtls_[0-9a-z_]*[0-9a-z]$' $THING || true ) |
| 86 | if [ "x$BAD" = "x" ]; then |
| 87 | echo "PASS" |
| 88 | else |
| 89 | echo "FAIL" |
| 90 | echo "$BAD" |
| 91 | FAIL=1 |
| 92 | fi |
| 93 | done |
| 94 | |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 95 | printf "Likely typos: " |
| 96 | sort -u actual-macros enum-consts > _caps |
| 97 | HEADERS=$( ls include/mbedtls/*.h | egrep -v 'compat-1\.3\.h' ) |
| 98 | NL=' |
| 99 | ' |
Manuel Pégourié-Gonnard | 6ad5d35 | 2015-05-28 15:08:28 +0200 | [diff] [blame] | 100 | sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \ |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 101 | $HEADERS library/*.c \ |
| 102 | | grep MBEDTLS | sort -u > _MBEDTLS_XXX |
| 103 | TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \ |
Manuel Pégourié-Gonnard | 32da9f6 | 2015-07-31 15:52:30 +0200 | [diff] [blame] | 104 | | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true ) |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 105 | rm _MBEDTLS_XXX _caps |
Manuel Pégourié-Gonnard | 9afdc83 | 2015-08-04 17:15:13 +0200 | [diff] [blame] | 106 | if [ "x$TYPOS" = "x" ]; then |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 107 | echo "PASS" |
| 108 | else |
| 109 | echo "FAIL" |
| 110 | echo "$TYPOS" |
| 111 | FAIL=1 |
| 112 | fi |
| 113 | |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 114 | if [ -n "$trace" ]; then |
| 115 | set +x |
| 116 | trap - EXIT |
| 117 | rm check-names.err |
| 118 | fi |
| 119 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 120 | printf "\nOverall: " |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 121 | if [ "$FAIL" -eq 0 ]; then |
| 122 | rm macros actual-macros enum-consts identifiers exported-symbols |
| 123 | echo "PASSED" |
| 124 | exit 0 |
| 125 | else |
| 126 | echo "FAILED" |
| 127 | exit 1 |
| 128 | fi |