blob: e2019ccadb58b807c3aa11b68d6efb0fcbdc9ea8 [file] [log] [blame]
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +02001#!/bin/sh
Simon Butcher71ebc582016-06-23 20:02:07 +01002#
Gilles Peskine0c12e302019-05-22 18:22:45 +02003# Copyright (c) 2015-2019, ARM Limited, All Rights Reserved
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.
17#
18# This file is part of Mbed TLS (https://tls.mbed.org)
Gilles Peskine0c12e302019-05-22 18:22:45 +020019
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
Simon Butcher71ebc582016-06-23 20:02:07 +010033if grep --version|head -n1|grep GNU >/dev/null; then :; else
Ron Eldorbf007d22016-12-15 14:42:37 +020034 echo "This script requires GNU grep.">&2
Simon Butcher71ebc582016-06-23 20:02:07 +010035 exit 1
36fi
37
Gilles Peskine36428d32019-05-15 17:29:15 +020038trace=
39if [ $# -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
46fi
47
Simon Butcher71ebc582016-06-23 20:02:07 +010048printf "Analysing source code...\n"
49
Gilles Peskine36428d32019-05-15 17:29:15 +020050sh $trace tests/scripts/list-macros.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020051tests/scripts/list-enum-consts.pl
Gilles Peskine36428d32019-05-15 17:29:15 +020052sh $trace tests/scripts/list-identifiers.sh
53sh $trace tests/scripts/list-symbols.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020054
55FAIL=0
56
Simon Butcher71ebc582016-06-23 20:02:07 +010057printf "\nExported symbols declared in header: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020058UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' )
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +020059if [ "x$UNDECLARED" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020060 echo "PASS"
61else
62 echo "FAIL"
63 echo "$UNDECLARED"
64 FAIL=1
65fi
66
67diff macros identifiers | sed -n -e 's/< //p' > actual-macros
68
69for THING in actual-macros enum-consts; do
70 printf "Names of $THING: "
71 test -r $THING
Nir Sonnenschein03091d12019-01-08 18:15:50 +020072 BAD=$( grep -E -v '^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$' $THING || true )
Unknown712f7a82019-08-21 03:34:00 -040073 UNDERSCORES=$( grep -E '.*__.*' $THING || true )
74
75 if [ "x$BAD" = "x" ] && [ "x$UNDERSCORES" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020076 echo "PASS"
77 else
78 echo "FAIL"
79 echo "$BAD"
Unknown712f7a82019-08-21 03:34:00 -040080 echo "$UNDERSCORES"
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020081 FAIL=1
82 fi
83done
84
85for THING in identifiers; do
86 printf "Names of $THING: "
87 test -r $THING
Gilles Peskine2d9d6db2018-06-18 23:25:28 +020088 BAD=$( grep -E -v '^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$' $THING || true )
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020089 if [ "x$BAD" = "x" ]; then
90 echo "PASS"
91 else
92 echo "FAIL"
93 echo "$BAD"
94 FAIL=1
95 fi
96done
97
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +020098printf "Likely typos: "
99sort -u actual-macros enum-consts > _caps
Nir Sonnenschein03091d12019-01-08 18:15:50 +0200100HEADERS=$( ls include/mbedtls/*.h include/psa/*.h | egrep -v 'compat-1\.3\.h' )
Christoph M. Wintersteiger8a0f5bb2018-12-14 15:46:34 +0000101HEADERS="$HEADERS 3rdparty/everest/include/everest/everest.h 3rdparty/everest/include/everest/x25519.h"
Gilles Peskine9ab96212020-02-12 21:06:45 +0100102LIBRARY="$( ls library/*.c )"
103LIBRARY="$LIBRARY 3rdparty/everest/library/everest.c 3rdparty/everest/library/x25519.c"
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200104NL='
105'
Manuel Pégourié-Gonnard6ad5d352015-05-28 15:08:28 +0200106sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
Christoph M. Wintersteiger8a0f5bb2018-12-14 15:46:34 +0000107 $HEADERS $LIBRARY \
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200108 | grep MBEDTLS | sort -u > _MBEDTLS_XXX
109TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
Manuel Pégourié-Gonnard32da9f62015-07-31 15:52:30 +0200110 | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200111rm _MBEDTLS_XXX _caps
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +0200112if [ "x$TYPOS" = "x" ]; then
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200113 echo "PASS"
114else
115 echo "FAIL"
116 echo "$TYPOS"
117 FAIL=1
118fi
119
Gilles Peskine36428d32019-05-15 17:29:15 +0200120if [ -n "$trace" ]; then
121 set +x
122 trap - EXIT
123 rm check-names.err
124fi
125
Simon Butcher71ebc582016-06-23 20:02:07 +0100126printf "\nOverall: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +0200127if [ "$FAIL" -eq 0 ]; then
128 rm macros actual-macros enum-consts identifiers exported-symbols
129 echo "PASSED"
130 exit 0
131else
132 echo "FAILED"
133 exit 1
134fi