blob: 3cbb450f43e5a8a12ae87bc0875a53d6dcdd234a [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úti4e9f7122020-06-05 13:02:18 +02004# 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úti09b4f192020-05-26 01:54:15 +020011#
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úti4e9f7122020-06-05 13:02:18 +020024# **********
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# **********
44#
Bence Szépkúti09b4f192020-05-26 01:54:15 +020045# This file is part of Mbed TLS (https://tls.mbed.org)
Gilles Peskine017adc72019-05-22 18:22:45 +020046
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020047set -eu
48
Gilles Peskine017adc72019-05-22 18:22:45 +020049if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
50 cat <<EOF
51$0 [-v]
52This script confirms that the naming of all symbols and identifiers in mbed
53TLS are consistent with the house style and are also self-consistent.
54
55 -v If the script fails unexpectedly, print a command trace.
56EOF
57 exit
58fi
59
Simon Butcher71ebc582016-06-23 20:02:07 +010060if grep --version|head -n1|grep GNU >/dev/null; then :; else
Ron Eldorbf007d22016-12-15 14:42:37 +020061 echo "This script requires GNU grep.">&2
Simon Butcher71ebc582016-06-23 20:02:07 +010062 exit 1
63fi
64
Gilles Peskineef39c492019-05-15 17:29:15 +020065trace=
66if [ $# -ne 0 ] && [ "$1" = "-v" ]; then
67 shift
68 trace='-x'
69 exec 2>check-names.err
70 trap 'echo "FAILED UNEXPECTEDLY, status=$?";
71 cat check-names.err' EXIT
72 set -x
73fi
74
Simon Butcher71ebc582016-06-23 20:02:07 +010075printf "Analysing source code...\n"
76
Gilles Peskineef39c492019-05-15 17:29:15 +020077sh $trace tests/scripts/list-macros.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020078tests/scripts/list-enum-consts.pl
Gilles Peskineef39c492019-05-15 17:29:15 +020079sh $trace tests/scripts/list-identifiers.sh
80sh $trace tests/scripts/list-symbols.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020081
82FAIL=0
83
Simon Butcher71ebc582016-06-23 20:02:07 +010084printf "\nExported symbols declared in header: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020085UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' )
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +020086if [ "x$UNDECLARED" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020087 echo "PASS"
88else
89 echo "FAIL"
90 echo "$UNDECLARED"
91 FAIL=1
92fi
93
94diff macros identifiers | sed -n -e 's/< //p' > actual-macros
95
96for THING in actual-macros enum-consts; do
97 printf "Names of $THING: "
98 test -r $THING
Simon Butcher71ebc582016-06-23 20:02:07 +010099 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 +0200100 if [ "x$BAD" = "x" ]; then
101 echo "PASS"
102 else
103 echo "FAIL"
104 echo "$BAD"
105 FAIL=1
106 fi
107done
108
109for THING in identifiers; do
110 printf "Names of $THING: "
111 test -r $THING
112 BAD=$( grep -v '^mbedtls_[0-9a-z_]*[0-9a-z]$' $THING || true )
113 if [ "x$BAD" = "x" ]; then
114 echo "PASS"
115 else
116 echo "FAIL"
117 echo "$BAD"
118 FAIL=1
119 fi
120done
121
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200122printf "Likely typos: "
123sort -u actual-macros enum-consts > _caps
124HEADERS=$( ls include/mbedtls/*.h | egrep -v 'compat-1\.3\.h' )
125NL='
126'
Manuel Pégourié-Gonnard6ad5d352015-05-28 15:08:28 +0200127sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200128 $HEADERS library/*.c \
129 | grep MBEDTLS | sort -u > _MBEDTLS_XXX
130TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
Manuel Pégourié-Gonnard32da9f62015-07-31 15:52:30 +0200131 | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200132rm _MBEDTLS_XXX _caps
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +0200133if [ "x$TYPOS" = "x" ]; then
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200134 echo "PASS"
135else
136 echo "FAIL"
137 echo "$TYPOS"
138 FAIL=1
139fi
140
Gilles Peskineef39c492019-05-15 17:29:15 +0200141if [ -n "$trace" ]; then
142 set +x
143 trap - EXIT
144 rm check-names.err
145fi
146
Simon Butcher71ebc582016-06-23 20:02:07 +0100147printf "\nOverall: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +0200148if [ "$FAIL" -eq 0 ]; then
149 rm macros actual-macros enum-consts identifiers exported-symbols
150 echo "PASSED"
151 exit 0
152else
153 echo "FAILED"
154 exit 1
155fi