blob: 839ad7cd006ab4ba80184817df36bf822f007ca0 [file] [log] [blame]
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +02001#!/bin/sh
Simon Butcher71ebc582016-06-23 20:02:07 +01002#
Gilles Peskine017adc72019-05-22 18:22:45 +02003# Copyright (c) 2015-2019, ARM Limited, All Rights Reserved
Bence Szépkúti09b4f192020-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 Peskine017adc72019-05-22 18:22:45 +020019
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020020set -eu
21
Gilles Peskine017adc72019-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 Peskineef39c492019-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 Peskineef39c492019-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 Peskineef39c492019-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
Simon Butcher71ebc582016-06-23 20:02:07 +010072 BAD=$( grep -v '^MBEDTLS_[0-9A-Z_]*[0-9A-Z]$\|^YOTTA_[0-9A-Z_]*[0-9A-Z]$' $THING || true )
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020073 if [ "x$BAD" = "x" ]; then
74 echo "PASS"
75 else
76 echo "FAIL"
77 echo "$BAD"
78 FAIL=1
79 fi
80done
81
82for 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
93done
94
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +020095printf "Likely typos: "
96sort -u actual-macros enum-consts > _caps
97HEADERS=$( ls include/mbedtls/*.h | egrep -v 'compat-1\.3\.h' )
98NL='
99'
Manuel Pégourié-Gonnard6ad5d352015-05-28 15:08:28 +0200100sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200101 $HEADERS library/*.c \
102 | grep MBEDTLS | sort -u > _MBEDTLS_XXX
103TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
Manuel Pégourié-Gonnard32da9f62015-07-31 15:52:30 +0200104 | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200105rm _MBEDTLS_XXX _caps
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +0200106if [ "x$TYPOS" = "x" ]; then
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200107 echo "PASS"
108else
109 echo "FAIL"
110 echo "$TYPOS"
111 FAIL=1
112fi
113
Gilles Peskineef39c492019-05-15 17:29:15 +0200114if [ -n "$trace" ]; then
115 set +x
116 trap - EXIT
117 rm check-names.err
118fi
119
Simon Butcher71ebc582016-06-23 20:02:07 +0100120printf "\nOverall: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +0200121if [ "$FAIL" -eq 0 ]; then
122 rm macros actual-macros enum-consts identifiers exported-symbols
123 echo "PASSED"
124 exit 0
125else
126 echo "FAILED"
127 exit 1
128fi