blob: c6a200854b65e17000aa93ef9548cffe42df6b8c [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útia2947ac2020-08-19 16:37:36 +02003# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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úti51b41d52020-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útif744bd72020-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# **********
Gilles Peskine902a1f32019-05-22 18:22:45 +020044
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020045set -eu
46
Gilles Peskine902a1f32019-05-22 18:22:45 +020047if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
48 cat <<EOF
49$0 [-v]
50This script confirms that the naming of all symbols and identifiers in mbed
51TLS are consistent with the house style and are also self-consistent.
52
53 -v If the script fails unexpectedly, print a command trace.
54EOF
55 exit
56fi
57
Gilles Peskine5e525fb2019-05-15 17:29:15 +020058trace=
59if [ $# -ne 0 ] && [ "$1" = "-v" ]; then
60 shift
61 trace='-x'
62 exec 2>check-names.err
63 trap 'echo "FAILED UNEXPECTEDLY, status=$?";
64 cat check-names.err' EXIT
65 set -x
66fi
67
Simon Butcher71ebc582016-06-23 20:02:07 +010068printf "Analysing source code...\n"
69
Gilles Peskine5e525fb2019-05-15 17:29:15 +020070sh $trace tests/scripts/list-macros.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020071tests/scripts/list-enum-consts.pl
Gilles Peskine5e525fb2019-05-15 17:29:15 +020072sh $trace tests/scripts/list-identifiers.sh
73sh $trace tests/scripts/list-symbols.sh
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020074
75FAIL=0
76
Simon Butcher71ebc582016-06-23 20:02:07 +010077printf "\nExported symbols declared in header: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020078UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' )
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +020079if [ "x$UNDECLARED" = "x" ]; then
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020080 echo "PASS"
81else
82 echo "FAIL"
83 echo "$UNDECLARED"
84 FAIL=1
85fi
86
87diff macros identifiers | sed -n -e 's/< //p' > actual-macros
88
89for THING in actual-macros enum-consts; do
Gilles Peskineffdcadf2020-08-26 20:05:11 +020090 printf 'Names of %s: ' "$THING"
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020091 test -r $THING
Simon Butcher3ad2efd2018-05-02 14:49:38 +010092 BAD=$( grep -v '^MBEDTLS_[0-9A-Z_]*[0-9A-Z]$' $THING || true )
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +020093 if [ "x$BAD" = "x" ]; then
94 echo "PASS"
95 else
96 echo "FAIL"
97 echo "$BAD"
98 FAIL=1
99 fi
100done
101
102for THING in identifiers; do
Gilles Peskineffdcadf2020-08-26 20:05:11 +0200103 printf 'Names of %s: ' "$THING"
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +0200104 test -r $THING
105 BAD=$( grep -v '^mbedtls_[0-9a-z_]*[0-9a-z]$' $THING || true )
106 if [ "x$BAD" = "x" ]; then
107 echo "PASS"
108 else
109 echo "FAIL"
110 echo "$BAD"
111 FAIL=1
112 fi
113done
114
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200115printf "Likely typos: "
116sort -u actual-macros enum-consts > _caps
117HEADERS=$( ls include/mbedtls/*.h | egrep -v 'compat-1\.3\.h' )
118NL='
119'
Manuel Pégourié-Gonnard6ad5d352015-05-28 15:08:28 +0200120sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200121 $HEADERS library/*.c \
122 | grep MBEDTLS | sort -u > _MBEDTLS_XXX
123TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
Manuel Pégourié-Gonnard32da9f62015-07-31 15:52:30 +0200124 | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200125rm _MBEDTLS_XXX _caps
Manuel Pégourié-Gonnard9afdc832015-08-04 17:15:13 +0200126if [ "x$TYPOS" = "x" ]; then
Manuel Pégourié-Gonnardf9aae832015-04-09 12:20:53 +0200127 echo "PASS"
128else
129 echo "FAIL"
130 echo "$TYPOS"
131 FAIL=1
132fi
133
Gilles Peskine5e525fb2019-05-15 17:29:15 +0200134if [ -n "$trace" ]; then
135 set +x
136 trap - EXIT
137 rm check-names.err
138fi
139
Simon Butcher71ebc582016-06-23 20:02:07 +0100140printf "\nOverall: "
Manuel Pégourié-Gonnarde137ea62015-04-09 10:47:44 +0200141if [ "$FAIL" -eq 0 ]; then
142 rm macros actual-macros enum-consts identifiers exported-symbols
143 echo "PASSED"
144 exit 0
145else
146 echo "FAILED"
147 exit 1
148fi