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 | # |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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: |
Bence Szépkúti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 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 | # |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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 | # ********** |
Gilles Peskine | 017adc7 | 2019-05-22 18:22:45 +0200 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 45 | set -eu |
| 46 | |
Gilles Peskine | 017adc7 | 2019-05-22 18:22:45 +0200 | [diff] [blame] | 47 | if [ $# -ne 0 ] && [ "$1" = "--help" ]; then |
| 48 | cat <<EOF |
| 49 | $0 [-v] |
| 50 | This script confirms that the naming of all symbols and identifiers in mbed |
| 51 | TLS are consistent with the house style and are also self-consistent. |
| 52 | |
| 53 | -v If the script fails unexpectedly, print a command trace. |
| 54 | EOF |
| 55 | exit |
| 56 | fi |
| 57 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 58 | if grep --version|head -n1|grep GNU >/dev/null; then :; else |
Ron Eldor | bf007d2 | 2016-12-15 14:42:37 +0200 | [diff] [blame] | 59 | echo "This script requires GNU grep.">&2 |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 60 | exit 1 |
| 61 | fi |
| 62 | |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 63 | trace= |
| 64 | if [ $# -ne 0 ] && [ "$1" = "-v" ]; then |
| 65 | shift |
| 66 | trace='-x' |
| 67 | exec 2>check-names.err |
| 68 | trap 'echo "FAILED UNEXPECTEDLY, status=$?"; |
| 69 | cat check-names.err' EXIT |
| 70 | set -x |
| 71 | fi |
| 72 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 73 | printf "Analysing source code...\n" |
| 74 | |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 75 | sh $trace tests/scripts/list-macros.sh |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 76 | tests/scripts/list-enum-consts.pl |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 77 | sh $trace tests/scripts/list-identifiers.sh |
| 78 | sh $trace tests/scripts/list-symbols.sh |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 79 | |
| 80 | FAIL=0 |
| 81 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 82 | printf "\nExported symbols declared in header: " |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 83 | UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' ) |
Manuel Pégourié-Gonnard | 9afdc83 | 2015-08-04 17:15:13 +0200 | [diff] [blame] | 84 | if [ "x$UNDECLARED" = "x" ]; then |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 85 | echo "PASS" |
| 86 | else |
| 87 | echo "FAIL" |
| 88 | echo "$UNDECLARED" |
| 89 | FAIL=1 |
| 90 | fi |
| 91 | |
| 92 | diff macros identifiers | sed -n -e 's/< //p' > actual-macros |
| 93 | |
| 94 | for THING in actual-macros enum-consts; do |
| 95 | printf "Names of $THING: " |
| 96 | test -r $THING |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 97 | 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] | 98 | if [ "x$BAD" = "x" ]; then |
| 99 | echo "PASS" |
| 100 | else |
| 101 | echo "FAIL" |
| 102 | echo "$BAD" |
| 103 | FAIL=1 |
| 104 | fi |
| 105 | done |
| 106 | |
| 107 | for THING in identifiers; do |
| 108 | printf "Names of $THING: " |
| 109 | test -r $THING |
| 110 | BAD=$( grep -v '^mbedtls_[0-9a-z_]*[0-9a-z]$' $THING || true ) |
| 111 | if [ "x$BAD" = "x" ]; then |
| 112 | echo "PASS" |
| 113 | else |
| 114 | echo "FAIL" |
| 115 | echo "$BAD" |
| 116 | FAIL=1 |
| 117 | fi |
| 118 | done |
| 119 | |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 120 | printf "Likely typos: " |
| 121 | sort -u actual-macros enum-consts > _caps |
| 122 | HEADERS=$( ls include/mbedtls/*.h | egrep -v 'compat-1\.3\.h' ) |
| 123 | NL=' |
| 124 | ' |
Manuel Pégourié-Gonnard | 6ad5d35 | 2015-05-28 15:08:28 +0200 | [diff] [blame] | 125 | sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \ |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 126 | $HEADERS library/*.c \ |
| 127 | | grep MBEDTLS | sort -u > _MBEDTLS_XXX |
| 128 | TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \ |
Manuel Pégourié-Gonnard | 32da9f6 | 2015-07-31 15:52:30 +0200 | [diff] [blame] | 129 | | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true ) |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 130 | rm _MBEDTLS_XXX _caps |
Manuel Pégourié-Gonnard | 9afdc83 | 2015-08-04 17:15:13 +0200 | [diff] [blame] | 131 | if [ "x$TYPOS" = "x" ]; then |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 132 | echo "PASS" |
| 133 | else |
| 134 | echo "FAIL" |
| 135 | echo "$TYPOS" |
| 136 | FAIL=1 |
| 137 | fi |
| 138 | |
Gilles Peskine | ef39c49 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 139 | if [ -n "$trace" ]; then |
| 140 | set +x |
| 141 | trap - EXIT |
| 142 | rm check-names.err |
| 143 | fi |
| 144 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 145 | printf "\nOverall: " |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 146 | if [ "$FAIL" -eq 0 ]; then |
| 147 | rm macros actual-macros enum-consts identifiers exported-symbols |
| 148 | echo "PASSED" |
| 149 | exit 0 |
| 150 | else |
| 151 | echo "FAILED" |
| 152 | exit 1 |
| 153 | fi |