blob: 6c9195bb0c8e24c4d3baa65c92307f0bb49d61b0 [file] [log] [blame]
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +02001#!/bin/sh
Simon Butcher71ebc582016-06-23 20:02:07 +01002#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# 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.
Gilles Peskine0c12e302019-05-22 18:22:45 +020017
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020018set -eu
19
Gilles Peskine0c12e302019-05-22 18:22:45 +020020if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
21 cat <<EOF
22$0 [-v]
23This script confirms that the naming of all symbols and identifiers in mbed
24TLS are consistent with the house style and are also self-consistent.
25
26 -v If the script fails unexpectedly, print a command trace.
27EOF
28 exit
29fi
30
Simon Butcher71ebc582016-06-23 20:02:07 +010031if grep --version|head -n1|grep GNU >/dev/null; then :; else
Ron Eldorbf007d22016-12-15 14:42:37 +020032 echo "This script requires GNU grep.">&2
Simon Butcher71ebc582016-06-23 20:02:07 +010033 exit 1
34fi
35
Gilles Peskine36428d32019-05-15 17:29:15 +020036trace=
37if [ $# -ne 0 ] && [ "$1" = "-v" ]; then
38 shift
39 trace='-x'
40 exec 2>check-names.err
41 trap 'echo "FAILED UNEXPECTEDLY, status=$?";
42 cat check-names.err' EXIT
43 set -x
44fi
45
Simon Butcher71ebc582016-06-23 20:02:07 +010046printf "Analysing source code...\n"
47
Gilles Peskine36428d32019-05-15 17:29:15 +020048sh $trace tests/scripts/list-macros.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020049tests/scripts/list-enum-consts.pl
Gilles Peskine36428d32019-05-15 17:29:15 +020050sh $trace tests/scripts/list-identifiers.sh
51sh $trace tests/scripts/list-symbols.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020052
53FAIL=0
54
Simon Butcher71ebc582016-06-23 20:02:07 +010055printf "\nExported symbols declared in header: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020056UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' )
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +020057if [ "x$UNDECLARED" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020058 echo "PASS"
59else
60 echo "FAIL"
61 echo "$UNDECLARED"
62 FAIL=1
63fi
64
65diff macros identifiers | sed -n -e 's/< //p' > actual-macros
66
67for THING in actual-macros enum-consts; do
68 printf "Names of $THING: "
69 test -r $THING
Nir Sonnenschein03091d12019-01-08 18:15:50 +020070 BAD=$( grep -E -v '^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$' $THING || true )
Unknown712f7a82019-08-21 03:34:00 -040071 UNDERSCORES=$( grep -E '.*__.*' $THING || true )
72
73 if [ "x$BAD" = "x" ] && [ "x$UNDERSCORES" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020074 echo "PASS"
75 else
76 echo "FAIL"
77 echo "$BAD"
Unknown712f7a82019-08-21 03:34:00 -040078 echo "$UNDERSCORES"
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020079 FAIL=1
80 fi
81done
82
83for THING in identifiers; do
84 printf "Names of $THING: "
85 test -r $THING
Gilles Peskine2d9d6db2018-06-18 23:25:28 +020086 BAD=$( grep -E -v '^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$' $THING || true )
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020087 if [ "x$BAD" = "x" ]; then
88 echo "PASS"
89 else
90 echo "FAIL"
91 echo "$BAD"
92 FAIL=1
93 fi
94done
95
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +020096printf "Likely typos: "
97sort -u actual-macros enum-consts > _caps
Nir Sonnenschein03091d12019-01-08 18:15:50 +020098HEADERS=$( ls include/mbedtls/*.h include/psa/*.h | egrep -v 'compat-1\.3\.h' )
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +020099HEADERS="$HEADERS library/*.h"
Christoph M. Wintersteiger8a0f5bb2018-12-14 15:46:34 +0000100HEADERS="$HEADERS 3rdparty/everest/include/everest/everest.h 3rdparty/everest/include/everest/x25519.h"
Gilles Peskine9ab96212020-02-12 21:06:45 +0100101LIBRARY="$( ls library/*.c )"
102LIBRARY="$LIBRARY 3rdparty/everest/library/everest.c 3rdparty/everest/library/x25519.c"
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200103NL='
104'
Manuel Pégourié-Gonnard6ad5d352015-05-28 15:08:28 +0200105sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
Christoph M. Wintersteiger8a0f5bb2018-12-14 15:46:34 +0000106 $HEADERS $LIBRARY \
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200107 | grep MBEDTLS | sort -u > _MBEDTLS_XXX
108TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
Manuel Pégourié-Gonnard32da9f62015-07-31 15:52:30 +0200109 | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200110rm _MBEDTLS_XXX _caps
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +0200111if [ "x$TYPOS" = "x" ]; then
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200112 echo "PASS"
113else
114 echo "FAIL"
115 echo "$TYPOS"
116 FAIL=1
117fi
118
Gilles Peskine36428d32019-05-15 17:29:15 +0200119if [ -n "$trace" ]; then
120 set +x
121 trap - EXIT
122 rm check-names.err
123fi
124
Simon Butcher71ebc582016-06-23 20:02:07 +0100125printf "\nOverall: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +0200126if [ "$FAIL" -eq 0 ]; then
127 rm macros actual-macros enum-consts identifiers exported-symbols
128 echo "PASSED"
129 exit 0
130else
131 echo "FAILED"
132 exit 1
133fi