blob: bdbf797ac81c4addfc235101ed5847d6a028c03b [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
Mateusz Starzykd30917c2021-08-04 11:14:25 +020018exit 0
19
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020020set -eu
21
Gilles Peskine0c12e302019-05-22 18:22:45 +020022if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
23 cat <<EOF
24$0 [-v]
25This script confirms that the naming of all symbols and identifiers in mbed
26TLS are consistent with the house style and are also self-consistent.
27
28 -v If the script fails unexpectedly, print a command trace.
29EOF
30 exit
31fi
32
Gilles Peskine36428d32019-05-15 17:29:15 +020033trace=
34if [ $# -ne 0 ] && [ "$1" = "-v" ]; then
35 shift
36 trace='-x'
37 exec 2>check-names.err
38 trap 'echo "FAILED UNEXPECTEDLY, status=$?";
39 cat check-names.err' EXIT
40 set -x
41fi
42
Simon Butcher71ebc582016-06-23 20:02:07 +010043printf "Analysing source code...\n"
44
Gilles Peskine36428d32019-05-15 17:29:15 +020045sh $trace tests/scripts/list-macros.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020046tests/scripts/list-enum-consts.pl
Gilles Peskine36428d32019-05-15 17:29:15 +020047sh $trace tests/scripts/list-identifiers.sh
48sh $trace tests/scripts/list-symbols.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020049
50FAIL=0
51
Simon Butcher71ebc582016-06-23 20:02:07 +010052printf "\nExported symbols declared in header: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020053UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' )
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +020054if [ "x$UNDECLARED" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020055 echo "PASS"
56else
57 echo "FAIL"
58 echo "$UNDECLARED"
59 FAIL=1
60fi
61
62diff macros identifiers | sed -n -e 's/< //p' > actual-macros
63
64for THING in actual-macros enum-consts; do
Gilles Peskine231befa2020-08-26 20:05:11 +020065 printf 'Names of %s: ' "$THING"
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020066 test -r $THING
Nir Sonnenschein03091d12019-01-08 18:15:50 +020067 BAD=$( grep -E -v '^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$' $THING || true )
Unknown712f7a82019-08-21 03:34:00 -040068 UNDERSCORES=$( grep -E '.*__.*' $THING || true )
69
70 if [ "x$BAD" = "x" ] && [ "x$UNDERSCORES" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020071 echo "PASS"
72 else
73 echo "FAIL"
74 echo "$BAD"
Unknown712f7a82019-08-21 03:34:00 -040075 echo "$UNDERSCORES"
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020076 FAIL=1
77 fi
78done
79
80for THING in identifiers; do
Gilles Peskine231befa2020-08-26 20:05:11 +020081 printf 'Names of %s: ' "$THING"
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020082 test -r $THING
Gilles Peskine2d9d6db2018-06-18 23:25:28 +020083 BAD=$( grep -E -v '^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$' $THING || true )
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020084 if [ "x$BAD" = "x" ]; then
85 echo "PASS"
86 else
87 echo "FAIL"
88 echo "$BAD"
89 FAIL=1
90 fi
91done
92
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +020093printf "Likely typos: "
94sort -u actual-macros enum-consts > _caps
TRodziewicz26371e42021-06-08 16:45:41 +020095HEADERS=$( ls include/mbedtls/*.h include/psa/*.h | egrep -v 'compat-2\.x\.h' )
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +020096HEADERS="$HEADERS library/*.h"
Christoph M. Wintersteiger8a0f5bb2018-12-14 15:46:34 +000097HEADERS="$HEADERS 3rdparty/everest/include/everest/everest.h 3rdparty/everest/include/everest/x25519.h"
Gilles Peskine9ab96212020-02-12 21:06:45 +010098LIBRARY="$( ls library/*.c )"
99LIBRARY="$LIBRARY 3rdparty/everest/library/everest.c 3rdparty/everest/library/x25519.c"
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200100NL='
101'
Gilles Peskineb6837762021-04-21 18:45:08 +0200102cat $HEADERS $LIBRARY \
103 | grep -v -e '//no-check-names' -e '#error' \
104 | sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200105 | grep MBEDTLS | sort -u > _MBEDTLS_XXX
106TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
Manuel Pégourié-Gonnard32da9f62015-07-31 15:52:30 +0200107 | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200108rm _MBEDTLS_XXX _caps
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +0200109if [ "x$TYPOS" = "x" ]; then
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200110 echo "PASS"
111else
112 echo "FAIL"
113 echo "$TYPOS"
114 FAIL=1
115fi
116
Gilles Peskine36428d32019-05-15 17:29:15 +0200117if [ -n "$trace" ]; then
118 set +x
119 trap - EXIT
120 rm check-names.err
121fi
122
Simon Butcher71ebc582016-06-23 20:02:07 +0100123printf "\nOverall: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +0200124if [ "$FAIL" -eq 0 ]; then
125 rm macros actual-macros enum-consts identifiers exported-symbols
126 echo "PASSED"
127 exit 0
128else
129 echo "FAILED"
130 exit 1
131fi