blob: 37a8be325d5b662f7fc70c2a4b83bd58485184b7 [file] [log] [blame]
Yuto Takano39639672021-08-05 19:47:48 +01001#!/usr/bin/env python3
2#
3# Copyright The Mbed TLS Contributors
4# 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
Darryl Greend5802922018-05-08 15:30:59 +010018"""
Yuto Takano39639672021-08-05 19:47:48 +010019This script confirms that the naming of all symbols and identifiers in Mbed TLS
Yuto Takano159255a2021-08-06 17:00:28 +010020are consistent with the house style and are also self-consistent. It only runs
21on Linux and macOS since it depends on nm.
22
23The script performs the following checks:
Yuto Takano81528c02021-08-06 16:22:06 +010024
25- All exported and available symbols in the library object files, are explicitly
Yuto Takano159255a2021-08-06 17:00:28 +010026 declared in the header files. This uses the nm command.
Yuto Takano81528c02021-08-06 16:22:06 +010027- All macros, constants, and identifiers (function names, struct names, etc)
28 follow the required pattern.
29- Typo checking: All words that begin with MBED exist as macros or constants.
Yuto Takanofc54dfb2021-08-07 17:18:28 +010030
31Returns 0 on success, 1 on test failure, and 2 if there is a script error or a
32subprocess error. Must be run from Mbed TLS root.
Darryl Greend5802922018-05-08 15:30:59 +010033"""
Yuto Takano39639672021-08-05 19:47:48 +010034
35import argparse
Yuto Takano977e07f2021-08-09 11:56:15 +010036import glob
Yuto Takano39639672021-08-05 19:47:48 +010037import textwrap
Darryl Greend5802922018-05-08 15:30:59 +010038import os
39import sys
40import traceback
41import re
42import shutil
43import subprocess
44import logging
45
Yuto Takano81528c02021-08-06 16:22:06 +010046# Naming patterns to check against. These are defined outside the NameCheck
47# class for ease of modification.
Yuto Takanobb7dca42021-08-05 19:57:58 +010048MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$"
Yuto Takano81528c02021-08-06 16:22:06 +010049CONSTANTS_PATTERN = MACRO_PATTERN
Yuto Takanoc1838932021-08-05 19:52:09 +010050IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$"
Yuto Takano39639672021-08-05 19:47:48 +010051
Yuto Takanod93fa372021-08-06 23:05:55 +010052class Match(): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +010053 """
54 A class representing a match, together with its found position.
55
56 Fields:
57 * filename: the file that the match was in.
58 * line: the full line containing the match.
Yuto Takanod93fa372021-08-06 23:05:55 +010059 * pos: a tuple of (line_no, start, end) positions on the file line where the
60 match is.
Yuto Takano81528c02021-08-06 16:22:06 +010061 * name: the match itself.
62 """
Yuto Takanod93fa372021-08-06 23:05:55 +010063 def __init__(self, filename, line, pos, name):
Yuto Takano39639672021-08-05 19:47:48 +010064 self.filename = filename
65 self.line = line
66 self.pos = pos
67 self.name = name
Yuto Takano39639672021-08-05 19:47:48 +010068
Yuto Takanoa4e75122021-08-06 17:23:28 +010069 def __str__(self):
Yuto Takano381fda82021-08-06 23:37:20 +010070 ln_str = str(self.pos[0])
71 gutter_len = max(4, len(ln_str))
72 gutter = (gutter_len - len(ln_str)) * " " + ln_str
73 underline = self.pos[1] * " " + (self.pos[2] - self.pos[1]) * "^"
74
Yuto Takanoa4e75122021-08-06 17:23:28 +010075 return (
Yuto Takano381fda82021-08-06 23:37:20 +010076 " {0} |\n".format(gutter_len * " ") +
77 " {0} | {1}".format(gutter, self.line) +
Yuto Takano55614b52021-08-07 01:00:18 +010078 " {0} | {1}\n".format(gutter_len * " ", underline)
Yuto Takanoa4e75122021-08-06 17:23:28 +010079 )
Yuto Takanod93fa372021-08-06 23:05:55 +010080
81class Problem(): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +010082 """
83 A parent class representing a form of static analysis error.
Yuto Takano81528c02021-08-06 16:22:06 +010084 """
Yuto Takano39639672021-08-05 19:47:48 +010085 def __init__(self):
Yuto Takanod70d4462021-08-09 12:45:51 +010086 self.quiet = False
Yuto Takano39639672021-08-05 19:47:48 +010087 self.textwrapper = textwrap.TextWrapper()
Yuto Takano81528c02021-08-06 16:22:06 +010088 self.textwrapper.width = 80
Yuto Takanoa4e75122021-08-06 17:23:28 +010089 self.textwrapper.initial_indent = " > "
Yuto Takano81528c02021-08-06 16:22:06 +010090 self.textwrapper.subsequent_indent = " "
Yuto Takano39639672021-08-05 19:47:48 +010091
Yuto Takanod93fa372021-08-06 23:05:55 +010092class SymbolNotInHeader(Problem): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +010093 """
94 A problem that occurs when an exported/available symbol in the object file
95 is not explicitly declared in header files. Created with
96 NameCheck.check_symbols_declared_in_header()
97
98 Fields:
99 * symbol_name: the name of the symbol.
100 """
Yuto Takanod70d4462021-08-09 12:45:51 +0100101 def __init__(self, symbol_name):
Yuto Takano39639672021-08-05 19:47:48 +0100102 self.symbol_name = symbol_name
103 Problem.__init__(self)
104
105 def __str__(self):
Yuto Takano55614b52021-08-07 01:00:18 +0100106 if self.quiet:
107 return "{0}".format(self.symbol_name)
108
Yuto Takano39639672021-08-05 19:47:48 +0100109 return self.textwrapper.fill(
110 "'{0}' was found as an available symbol in the output of nm, "
111 "however it was not declared in any header files."
112 .format(self.symbol_name))
113
Yuto Takanod93fa372021-08-06 23:05:55 +0100114class PatternMismatch(Problem): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +0100115 """
116 A problem that occurs when something doesn't match the expected pattern.
117 Created with NameCheck.check_match_pattern()
118
119 Fields:
120 * pattern: the expected regex pattern
121 * match: the Match object in question
122 """
Yuto Takanod70d4462021-08-09 12:45:51 +0100123 def __init__(self, pattern, match):
Yuto Takano39639672021-08-05 19:47:48 +0100124 self.pattern = pattern
125 self.match = match
126 Problem.__init__(self)
Yuto Takano81528c02021-08-06 16:22:06 +0100127
Yuto Takano39639672021-08-05 19:47:48 +0100128 def __str__(self):
Yuto Takano55614b52021-08-07 01:00:18 +0100129 if self.quiet:
Yuto Takanod70d4462021-08-09 12:45:51 +0100130 return (
131 "{0}:{1}:{3}"
132 .format(self.match.filename, self.match.pos[0], self.match.name)
133 )
Yuto Takano55614b52021-08-07 01:00:18 +0100134
Yuto Takano39639672021-08-05 19:47:48 +0100135 return self.textwrapper.fill(
Yuto Takanoa4e75122021-08-06 17:23:28 +0100136 "{0}:{1}: '{2}' does not match the required pattern '{3}'."
137 .format(
138 self.match.filename,
Yuto Takanod93fa372021-08-06 23:05:55 +0100139 self.match.pos[0],
Yuto Takanoa4e75122021-08-06 17:23:28 +0100140 self.match.name,
Yuto Takanod70d4462021-08-09 12:45:51 +0100141 self.pattern
142 )
143 ) + "\n" + str(self.match)
Yuto Takano39639672021-08-05 19:47:48 +0100144
Yuto Takanod93fa372021-08-06 23:05:55 +0100145class Typo(Problem): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +0100146 """
147 A problem that occurs when a word using MBED doesn't appear to be defined as
148 constants nor enum values. Created with NameCheck.check_for_typos()
149
150 Fields:
151 * match: the Match object of the MBED name in question.
152 """
Yuto Takanod70d4462021-08-09 12:45:51 +0100153 def __init__(self, match):
Yuto Takano39639672021-08-05 19:47:48 +0100154 self.match = match
155 Problem.__init__(self)
Yuto Takano81528c02021-08-06 16:22:06 +0100156
Yuto Takano39639672021-08-05 19:47:48 +0100157 def __str__(self):
Yuto Takano55614b52021-08-07 01:00:18 +0100158 if self.quiet:
Yuto Takanod70d4462021-08-09 12:45:51 +0100159 return (
160 "{0}:{1}:{2}"
161 .format(self.match.filename, self.match.pos[0], self.match.name)
162 )
Yuto Takano55614b52021-08-07 01:00:18 +0100163
Yuto Takano39639672021-08-05 19:47:48 +0100164 return self.textwrapper.fill(
Yuto Takanoa4e75122021-08-06 17:23:28 +0100165 "{0}:{1}: '{2}' looks like a typo. It was not found in any "
166 "macros or any enums. If this is not a typo, put "
167 "//no-check-names after it."
Yuto Takanod70d4462021-08-09 12:45:51 +0100168 .format(self.match.filename, self.match.pos[0], self.match.name)
169 ) + "\n" + str(self.match)
Darryl Greend5802922018-05-08 15:30:59 +0100170
Yuto Takanod93fa372021-08-06 23:05:55 +0100171class NameCheck():
Yuto Takano81528c02021-08-06 16:22:06 +0100172 """
173 Representation of the core name checking operation performed by this script.
Yuto Takano977e07f2021-08-09 11:56:15 +0100174 Shares a common logger, and a shared return code.
Yuto Takano81528c02021-08-06 16:22:06 +0100175 """
Yuto Takano977e07f2021-08-09 11:56:15 +0100176 def __init__(self, verbose=False):
Darryl Greend5802922018-05-08 15:30:59 +0100177 self.log = None
Yuto Takanofc54dfb2021-08-07 17:18:28 +0100178 self.check_repo_path()
Darryl Greend5802922018-05-08 15:30:59 +0100179 self.return_code = 0
Yuto Takano977e07f2021-08-09 11:56:15 +0100180 self.setup_logger(verbose)
181
Yuto Takano8e9a2192021-08-09 14:48:53 +0100182 # Memo for storing "glob expression": set(filepaths)
183 self.files = {}
184
Yuto Takano977e07f2021-08-09 11:56:15 +0100185 # Globally excluded filenames
Yuto Takano8e9a2192021-08-09 14:48:53 +0100186 self.excluded_files = ["**/bn_mul", "**/compat-2.x.h"]
Yuto Takano977e07f2021-08-09 11:56:15 +0100187
188 # Will contain the parse result after a comprehensive parse
Yuto Takanod93fa372021-08-06 23:05:55 +0100189 self.parse_result = {}
Darryl Greend5802922018-05-08 15:30:59 +0100190
Yuto Takanofc54dfb2021-08-07 17:18:28 +0100191 @staticmethod
192 def check_repo_path():
193 """
194 Check that the current working directory is the project root, and throw
195 an exception if not.
196 """
197 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
198 raise Exception("This script must be run from Mbed TLS root")
199
Yuto Takanod70d4462021-08-09 12:45:51 +0100200 def set_return_code(self, return_code):
201 if return_code > self.return_code:
202 self.log.debug("Setting new return code to {}".format(return_code))
203 self.return_code = return_code
204
Yuto Takano39639672021-08-05 19:47:48 +0100205 def setup_logger(self, verbose=False):
206 """
207 Set up a logger and set the change the default logging level from
Yuto Takano81528c02021-08-06 16:22:06 +0100208 WARNING to INFO. Loggers are better than print statements since their
Yuto Takano39639672021-08-05 19:47:48 +0100209 verbosity can be controlled.
210 """
Darryl Greend5802922018-05-08 15:30:59 +0100211 self.log = logging.getLogger()
Yuto Takano39639672021-08-05 19:47:48 +0100212 if verbose:
213 self.log.setLevel(logging.DEBUG)
214 else:
215 self.log.setLevel(logging.INFO)
Darryl Greend5802922018-05-08 15:30:59 +0100216 self.log.addHandler(logging.StreamHandler())
217
Yuto Takano8e9a2192021-08-09 14:48:53 +0100218 def get_files(self, include_wildcards, exclude_wildcards):
Yuto Takano81528c02021-08-06 16:22:06 +0100219 """
Yuto Takano8e9a2192021-08-09 14:48:53 +0100220 Get all files that match any of the UNIX-style wildcards. While the
221 check_names script is designed only for use on UNIX/macOS (due to nm),
222 this function alone would work fine on Windows even with forward slashes
223 in the wildcard.
Yuto Takano81528c02021-08-06 16:22:06 +0100224
225 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100226 * include_wildcards: a List of shell-style wildcards to match filepaths.
227 * exclude_wildacrds: a List of shell-style wildcards to exclude.
Yuto Takano81528c02021-08-06 16:22:06 +0100228
229 Returns a List of relative filepaths.
230 """
Yuto Takano8e9a2192021-08-09 14:48:53 +0100231 accumulator = set()
Yuto Takano977e07f2021-08-09 11:56:15 +0100232
Yuto Takano8e9a2192021-08-09 14:48:53 +0100233 # exclude_wildcards may be None. Also, consider the global exclusions.
234 exclude_wildcards = (exclude_wildcards or []) + self.excluded_files
235
236 # Perform set union on the glob results. Memoise individual sets.
237 for include_wildcard in include_wildcards:
238 if include_wildcard not in self.files:
239 self.files[include_wildcard] = set(glob.glob(
240 include_wildcard,
241 recursive=True
242 ))
243
244 accumulator = accumulator.union(self.files[include_wildcard])
245
246 # Perform set difference to exclude. Also use the same memo since their
247 # behaviour is pretty much identical and it can benefit from the cache.
248 for exclude_wildcard in exclude_wildcards:
249 if exclude_wildcard not in self.files:
250 self.files[exclude_wildcard] = set(glob.glob(
251 exclude_wildcard,
252 recursive=True
253 ))
254
255 accumulator = accumulator.difference(self.files[exclude_wildcard])
256
257 return list(accumulator)
Darryl Greend5802922018-05-08 15:30:59 +0100258
Yuto Takano81528c02021-08-06 16:22:06 +0100259 def parse_names_in_source(self):
260 """
Yuto Takano977e07f2021-08-09 11:56:15 +0100261 Comprehensive function to call each parsing function and retrieve
262 various elements of the code, together with their source location.
263 Puts the parsed values in the internal variable self.parse_result, so
264 they can be used from perform_checks().
Yuto Takano81528c02021-08-06 16:22:06 +0100265 """
266 self.log.info("Parsing source code...")
Yuto Takanod24e0372021-08-06 16:42:33 +0100267 self.log.debug(
Yuto Takanod70d4462021-08-09 12:45:51 +0100268 "The following filenames are excluded from the search: {}"
Yuto Takanod24e0372021-08-06 16:42:33 +0100269 .format(str(self.excluded_files))
270 )
Yuto Takano81528c02021-08-06 16:22:06 +0100271
Yuto Takano8e9a2192021-08-09 14:48:53 +0100272 all_macros = self.parse_macros([
273 "include/mbedtls/*.h",
274 "include/psa/*.h",
275 "library/*.h",
276 "tests/include/test/drivers/*.h",
Yuto Takanod70d4462021-08-09 12:45:51 +0100277 "3rdparty/everest/include/everest/everest.h",
278 "3rdparty/everest/include/everest/x25519.h"
Yuto Takano8e9a2192021-08-09 14:48:53 +0100279 ])
280 enum_consts = self.parse_enum_consts([
281 "include/mbedtls/*.h",
282 "library/*.h",
283 "3rdparty/everest/include/everest/everest.h",
284 "3rdparty/everest/include/everest/x25519.h"
285 ])
286 identifiers = self.parse_identifiers([
287 "include/mbedtls/*.h",
288 "include/psa/*.h",
289 "library/*.h",
290 "3rdparty/everest/include/everest/everest.h",
291 "3rdparty/everest/include/everest/x25519.h"
292 ])
293 mbed_words = self.parse_mbed_words([
294 "include/mbedtls/*.h",
295 "include/psa/*.h",
296 "library/*.h",
297 "3rdparty/everest/include/everest/everest.h",
298 "3rdparty/everest/include/everest/x25519.h",
299 "library/*.c",
Yuto Takano81528c02021-08-06 16:22:06 +0100300 "3rdparty/everest/library/everest.c",
Yuto Takanod70d4462021-08-09 12:45:51 +0100301 "3rdparty/everest/library/x25519.c"
Yuto Takano8e9a2192021-08-09 14:48:53 +0100302 ])
Yuto Takano81528c02021-08-06 16:22:06 +0100303 symbols = self.parse_symbols()
304
305 # Remove identifier macros like mbedtls_printf or mbedtls_calloc
306 identifiers_justname = [x.name for x in identifiers]
307 actual_macros = []
308 for macro in all_macros:
309 if macro.name not in identifiers_justname:
310 actual_macros.append(macro)
311
312 self.log.debug("Found:")
Yuto Takanod70d4462021-08-09 12:45:51 +0100313 self.log.debug(" {} Total Macros".format(len(all_macros)))
Yuto Takano81528c02021-08-06 16:22:06 +0100314 self.log.debug(" {} Non-identifier Macros".format(len(actual_macros)))
315 self.log.debug(" {} Enum Constants".format(len(enum_consts)))
316 self.log.debug(" {} Identifiers".format(len(identifiers)))
317 self.log.debug(" {} Exported Symbols".format(len(symbols)))
318 self.log.info("Analysing...")
Yuto Takano81528c02021-08-06 16:22:06 +0100319 self.parse_result = {
320 "macros": actual_macros,
321 "enum_consts": enum_consts,
322 "identifiers": identifiers,
323 "symbols": symbols,
Yuto Takanod93fa372021-08-06 23:05:55 +0100324 "mbed_words": mbed_words
Yuto Takano81528c02021-08-06 16:22:06 +0100325 }
326
Yuto Takano8e9a2192021-08-09 14:48:53 +0100327 def parse_macros(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100328 """
329 Parse all macros defined by #define preprocessor directives.
330
331 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100332 * include: A List of glob expressions to look for files through.
333 * exclude: A List of glob expressions for excluding files.
Yuto Takano81528c02021-08-06 16:22:06 +0100334
335 Returns a List of Match objects for the found macros.
Yuto Takano39639672021-08-05 19:47:48 +0100336 """
Yuto Takanod93fa372021-08-06 23:05:55 +0100337 macro_regex = re.compile(r"# *define +(?P<macro>\w+)")
338 exclusions = (
Yuto Takano39639672021-08-05 19:47:48 +0100339 "asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_"
340 )
341
Yuto Takanod93fa372021-08-06 23:05:55 +0100342 macros = []
343
Yuto Takano8e9a2192021-08-09 14:48:53 +0100344 for header_file in self.get_files(include, exclude):
Yuto Takanoa083d152021-08-07 00:25:59 +0100345 with open(header_file, "r", encoding="utf-8") as header:
Yuto Takano8f457cf2021-08-06 17:54:58 +0100346 for line_no, line in enumerate(header):
Yuto Takanod93fa372021-08-06 23:05:55 +0100347 for macro in macro_regex.finditer(line):
Yuto Takanod70d4462021-08-09 12:45:51 +0100348 if macro.group("macro").startswith(exclusions):
349 continue
350
351 macros.append(Match(
352 header_file,
353 line,
354 (line_no, macro.start(), macro.end()),
355 macro.group("macro")))
Darryl Greend5802922018-05-08 15:30:59 +0100356
Yuto Takano39639672021-08-05 19:47:48 +0100357 return macros
Darryl Greend5802922018-05-08 15:30:59 +0100358
Yuto Takano8e9a2192021-08-09 14:48:53 +0100359 def parse_mbed_words(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100360 """
Yuto Takanob47b5042021-08-07 00:42:54 +0100361 Parse all words in the file that begin with MBED, in and out of macros,
362 comments, anything.
Yuto Takano39639672021-08-05 19:47:48 +0100363
364 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100365 * include: A List of glob expressions to look for files through.
366 * exclude: A List of glob expressions for excluding files.
Yuto Takano81528c02021-08-06 16:22:06 +0100367
368 Returns a List of Match objects for words beginning with MBED.
Yuto Takano39639672021-08-05 19:47:48 +0100369 """
Yuto Takanob47b5042021-08-07 00:42:54 +0100370 # Typos of TLS are common, hence the broader check below than MBEDTLS.
Yuto Takanod93fa372021-08-06 23:05:55 +0100371 mbed_regex = re.compile(r"\bMBED.+?_[A-Z0-9_]*")
372 exclusions = re.compile(r"// *no-check-names|#error")
373
Yuto Takanod93fa372021-08-06 23:05:55 +0100374 mbed_words = []
375
Yuto Takano8e9a2192021-08-09 14:48:53 +0100376 for filename in self.get_files(include, exclude):
Yuto Takanoa083d152021-08-07 00:25:59 +0100377 with open(filename, "r", encoding="utf-8") as fp:
Yuto Takano8f457cf2021-08-06 17:54:58 +0100378 for line_no, line in enumerate(fp):
Yuto Takanod93fa372021-08-06 23:05:55 +0100379 if exclusions.search(line):
Yuto Takanoc62b4082021-08-05 20:17:07 +0100380 continue
Yuto Takano81528c02021-08-06 16:22:06 +0100381
Yuto Takanod93fa372021-08-06 23:05:55 +0100382 for name in mbed_regex.finditer(line):
383 mbed_words.append(Match(
Yuto Takano39639672021-08-05 19:47:48 +0100384 filename,
385 line,
Yuto Takanod93fa372021-08-06 23:05:55 +0100386 (line_no, name.start(), name.end()),
Yuto Takano39639672021-08-05 19:47:48 +0100387 name.group(0)
388 ))
389
Yuto Takanod93fa372021-08-06 23:05:55 +0100390 return mbed_words
Yuto Takano39639672021-08-05 19:47:48 +0100391
Yuto Takano8e9a2192021-08-09 14:48:53 +0100392 def parse_enum_consts(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100393 """
394 Parse all enum value constants that are declared.
395
396 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100397 * include: A List of glob expressions to look for files through.
398 * exclude: A List of glob expressions for excluding files.
Yuto Takano39639672021-08-05 19:47:48 +0100399
Yuto Takano81528c02021-08-06 16:22:06 +0100400 Returns a List of Match objects for the findings.
Yuto Takano39639672021-08-05 19:47:48 +0100401 """
Yuto Takano39639672021-08-05 19:47:48 +0100402 enum_consts = []
Yuto Takanod93fa372021-08-06 23:05:55 +0100403
Yuto Takano8e9a2192021-08-09 14:48:53 +0100404 for header_file in self.get_files(include, exclude):
Yuto Takano39639672021-08-05 19:47:48 +0100405 # Emulate a finite state machine to parse enum declarations.
Yuto Takano81528c02021-08-06 16:22:06 +0100406 # 0 = not in enum
407 # 1 = inside enum
408 # 2 = almost inside enum
Darryl Greend5802922018-05-08 15:30:59 +0100409 state = 0
Yuto Takanoa083d152021-08-07 00:25:59 +0100410 with open(header_file, "r", encoding="utf-8") as header:
Yuto Takano8f457cf2021-08-06 17:54:58 +0100411 for line_no, line in enumerate(header):
Yuto Takano13ecd992021-08-06 16:56:52 +0100412 # Match typedefs and brackets only when they are at the
413 # beginning of the line -- if they are indented, they might
414 # be sub-structures within structs, etc.
Yuto Takanod93fa372021-08-06 23:05:55 +0100415 if state == 0 and re.match(r"^(typedef +)?enum +{", line):
Darryl Greend5802922018-05-08 15:30:59 +0100416 state = 1
Yuto Takanod93fa372021-08-06 23:05:55 +0100417 elif state == 0 and re.match(r"^(typedef +)?enum", line):
Darryl Greend5802922018-05-08 15:30:59 +0100418 state = 2
Yuto Takanod93fa372021-08-06 23:05:55 +0100419 elif state == 2 and re.match(r"^{", line):
Darryl Greend5802922018-05-08 15:30:59 +0100420 state = 1
Yuto Takanod93fa372021-08-06 23:05:55 +0100421 elif state == 1 and re.match(r"^}", line):
Darryl Greend5802922018-05-08 15:30:59 +0100422 state = 0
Yuto Takanod93fa372021-08-06 23:05:55 +0100423 elif state == 1 and not re.match(r" *#", line):
Yuto Takano13ecd992021-08-06 16:56:52 +0100424 enum_const = re.match(r" *(?P<enum_const>\w+)", line)
Yuto Takanod70d4462021-08-09 12:45:51 +0100425 if not enum_const:
426 continue
427
428 enum_consts.append(Match(
429 header_file,
430 line,
431 (line_no, enum_const.start(), enum_const.end()),
432 enum_const.group("enum_const")))
Yuto Takano81528c02021-08-06 16:22:06 +0100433
Yuto Takano39639672021-08-05 19:47:48 +0100434 return enum_consts
Darryl Greend5802922018-05-08 15:30:59 +0100435
Yuto Takano8e9a2192021-08-09 14:48:53 +0100436 def parse_identifiers(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100437 """
438 Parse all lines of a header where a function identifier is declared,
Yuto Takano81528c02021-08-06 16:22:06 +0100439 based on some huersitics. Highly dependent on formatting style.
Yuto Takanod70d4462021-08-09 12:45:51 +0100440 Note: .match() checks at the beginning of the string (implicit ^), while
441 .search() checks throughout.
Darryl Greend5802922018-05-08 15:30:59 +0100442
Yuto Takano39639672021-08-05 19:47:48 +0100443 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100444 * include: A List of glob expressions to look for files through.
445 * exclude: A List of glob expressions for excluding files.
Yuto Takano81528c02021-08-06 16:22:06 +0100446
447 Returns a List of Match objects with identifiers.
Yuto Takano39639672021-08-05 19:47:48 +0100448 """
Yuto Takanod93fa372021-08-06 23:05:55 +0100449 identifier_regex = re.compile(
450 # Match " something(a" or " *something(a". Functions.
451 # Assumptions:
452 # - function definition from return type to one of its arguments is
453 # all on one line (enforced by the previous_line concat below)
454 # - function definition line only contains alphanumeric, asterisk,
455 # underscore, and open bracket
456 r".* \**(\w+) *\( *\w|"
457 # Match "(*something)(". Flexible with spaces.
458 r".*\( *\* *(\w+) *\) *\(|"
459 # Match names of named data structures.
460 r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$|"
461 # Match names of typedef instances, after closing bracket.
Yuto Takanod70d4462021-08-09 12:45:51 +0100462 r"}? *(\w+)[;[].*"
463 )
464 exclusion_lines = re.compile(
465 r"^("
466 r"extern +\"C\"|"
467 r"(typedef +)?(struct|union|enum)( *{)?$|"
468 r"} *;?$|"
469 r"$|"
470 r"//|"
471 r"#"
472 r")"
473 )
Yuto Takano39639672021-08-05 19:47:48 +0100474 identifiers = []
Yuto Takanod93fa372021-08-06 23:05:55 +0100475
Yuto Takano8e9a2192021-08-09 14:48:53 +0100476 for header_file in self.get_files(include, exclude):
Yuto Takanoa083d152021-08-07 00:25:59 +0100477 with open(header_file, "r", encoding="utf-8") as header:
Yuto Takano39639672021-08-05 19:47:48 +0100478 in_block_comment = False
Yuto Takanod70d4462021-08-09 12:45:51 +0100479 # The previous line varibale is used for concatenating lines
480 # when identifiers are formatted and spread across multiple.
Yuto Takanod93fa372021-08-06 23:05:55 +0100481 previous_line = ""
Darryl Greend5802922018-05-08 15:30:59 +0100482
Yuto Takano8f457cf2021-08-06 17:54:58 +0100483 for line_no, line in enumerate(header):
Yuto Takano81528c02021-08-06 16:22:06 +0100484 # Skip parsing this line if a block comment ends on it,
485 # but don't skip if it has just started -- there is a chance
486 # it ends on the same line.
Yuto Takano39639672021-08-05 19:47:48 +0100487 if re.search(r"/\*", line):
Yuto Takano81528c02021-08-06 16:22:06 +0100488 in_block_comment = not in_block_comment
489 if re.search(r"\*/", line):
490 in_block_comment = not in_block_comment
Yuto Takano39639672021-08-05 19:47:48 +0100491 continue
492
Yuto Takano81528c02021-08-06 16:22:06 +0100493 if in_block_comment:
Yuto Takanod93fa372021-08-06 23:05:55 +0100494 previous_line = ""
Yuto Takano81528c02021-08-06 16:22:06 +0100495 continue
496
Yuto Takanod93fa372021-08-06 23:05:55 +0100497 if exclusion_lines.match(line):
498 previous_line = ""
Yuto Takano81528c02021-08-06 16:22:06 +0100499 continue
500
Yuto Takanocfc9e4a2021-08-06 20:02:32 +0100501 # If the line contains only space-separated alphanumeric
502 # characters (or underscore, asterisk, or, open bracket),
503 # and nothing else, high chance it's a declaration that
504 # continues on the next line
505 if re.match(r"^([\w\*\(]+\s+)+$", line):
Yuto Takanod93fa372021-08-06 23:05:55 +0100506 previous_line += line
Yuto Takano81528c02021-08-06 16:22:06 +0100507 continue
508
509 # If previous line seemed to start an unfinished declaration
Yuto Takanocfc9e4a2021-08-06 20:02:32 +0100510 # (as above), concat and treat them as one.
511 if previous_line:
512 line = previous_line.strip() + " " + line.strip()
Yuto Takanod93fa372021-08-06 23:05:55 +0100513 previous_line = ""
Yuto Takano81528c02021-08-06 16:22:06 +0100514
515 # Skip parsing if line has a space in front = hueristic to
516 # skip function argument lines (highly subject to formatting
517 # changes)
518 if line[0] == " ":
Yuto Takano39639672021-08-05 19:47:48 +0100519 continue
Yuto Takano6f38ab32021-08-05 21:07:14 +0100520
Yuto Takanod93fa372021-08-06 23:05:55 +0100521 identifier = identifier_regex.search(line)
Yuto Takano39639672021-08-05 19:47:48 +0100522
Yuto Takanod70d4462021-08-09 12:45:51 +0100523 if not identifier:
524 continue
525
526 # Find the group that matched, and append it
527 for group in identifier.groups():
528 if not group:
529 continue
530
531 identifiers.append(Match(
532 header_file,
533 line,
534 (line_no, identifier.start(), identifier.end()),
535 group))
Yuto Takano39639672021-08-05 19:47:48 +0100536
537 return identifiers
538
539 def parse_symbols(self):
540 """
541 Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509
542 object files using nm to retrieve the list of referenced symbols.
Yuto Takano81528c02021-08-06 16:22:06 +0100543 Exceptions thrown here are rethrown because they would be critical
544 errors that void several tests, and thus needs to halt the program. This
545 is explicitly done for clarity.
Yuto Takano39639672021-08-05 19:47:48 +0100546
Yuto Takano81528c02021-08-06 16:22:06 +0100547 Returns a List of unique symbols defined and used in the libraries.
548 """
549 self.log.info("Compiling...")
Yuto Takano39639672021-08-05 19:47:48 +0100550 symbols = []
551
552 # Back up the config and atomically compile with the full configratuion.
Yuto Takanod70d4462021-08-09 12:45:51 +0100553 shutil.copy(
554 "include/mbedtls/mbedtls_config.h",
555 "include/mbedtls/mbedtls_config.h.bak"
556 )
Darryl Greend5802922018-05-08 15:30:59 +0100557 try:
Yuto Takano81528c02021-08-06 16:22:06 +0100558 # Use check=True in all subprocess calls so that failures are raised
559 # as exceptions and logged.
Yuto Takano39639672021-08-05 19:47:48 +0100560 subprocess.run(
Yuto Takano81528c02021-08-06 16:22:06 +0100561 ["python3", "scripts/config.py", "full"],
Yuto Takanobcc3d992021-08-06 23:14:58 +0100562 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100563 check=True
Darryl Greend5802922018-05-08 15:30:59 +0100564 )
565 my_environment = os.environ.copy()
566 my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables"
Yuto Takano39639672021-08-05 19:47:48 +0100567 subprocess.run(
Darryl Greend5802922018-05-08 15:30:59 +0100568 ["make", "clean", "lib"],
569 env=my_environment,
Yuto Takanobcc3d992021-08-06 23:14:58 +0100570 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100571 stdout=subprocess.PIPE,
Darryl Greend5802922018-05-08 15:30:59 +0100572 stderr=subprocess.STDOUT,
Yuto Takano39639672021-08-05 19:47:48 +0100573 check=True
Darryl Greend5802922018-05-08 15:30:59 +0100574 )
Yuto Takano39639672021-08-05 19:47:48 +0100575
576 # Perform object file analysis using nm
Yuto Takanod70d4462021-08-09 12:45:51 +0100577 symbols = self.parse_symbols_from_nm([
578 "library/libmbedcrypto.a",
579 "library/libmbedtls.a",
580 "library/libmbedx509.a"
581 ])
Yuto Takano39639672021-08-05 19:47:48 +0100582
583 subprocess.run(
Darryl Greend5802922018-05-08 15:30:59 +0100584 ["make", "clean"],
Yuto Takanobcc3d992021-08-06 23:14:58 +0100585 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100586 check=True
Darryl Greend5802922018-05-08 15:30:59 +0100587 )
588 except subprocess.CalledProcessError as error:
Yuto Takano25eeb7b2021-08-06 21:27:59 +0100589 self.log.debug(error.output)
Darryl Greend5802922018-05-08 15:30:59 +0100590 self.set_return_code(2)
Yuto Takano81528c02021-08-06 16:22:06 +0100591 raise error
Yuto Takano39639672021-08-05 19:47:48 +0100592 finally:
Yuto Takano6fececf2021-08-07 17:28:23 +0100593 # Put back the original config regardless of there being errors.
594 # Works also for keyboard interrupts.
Yuto Takanod70d4462021-08-09 12:45:51 +0100595 shutil.move(
596 "include/mbedtls/mbedtls_config.h.bak",
597 "include/mbedtls/mbedtls_config.h"
598 )
Yuto Takano39639672021-08-05 19:47:48 +0100599
600 return symbols
601
602 def parse_symbols_from_nm(self, object_files):
603 """
604 Run nm to retrieve the list of referenced symbols in each object file.
605 Does not return the position data since it is of no use.
606
Yuto Takano81528c02021-08-06 16:22:06 +0100607 Args:
608 * object_files: a List of compiled object files to search through.
609
610 Returns a List of unique symbols defined and used in any of the object
611 files.
Yuto Takano39639672021-08-05 19:47:48 +0100612 """
Yuto Takanod93fa372021-08-06 23:05:55 +0100613 nm_undefined_regex = re.compile(r"^\S+: +U |^$|^\S+:$")
614 nm_valid_regex = re.compile(r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)")
Yuto Takano12a7ecd2021-08-07 00:40:29 +0100615 exclusions = ("FStar", "Hacl")
Yuto Takano39639672021-08-05 19:47:48 +0100616
617 symbols = []
618
Yuto Takano81528c02021-08-06 16:22:06 +0100619 # Gather all outputs of nm
Yuto Takano39639672021-08-05 19:47:48 +0100620 nm_output = ""
621 for lib in object_files:
622 nm_output += subprocess.run(
623 ["nm", "-og", lib],
Yuto Takanobcc3d992021-08-06 23:14:58 +0100624 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100625 stdout=subprocess.PIPE,
626 stderr=subprocess.STDOUT,
627 check=True
628 ).stdout
Yuto Takano81528c02021-08-06 16:22:06 +0100629
Yuto Takano39639672021-08-05 19:47:48 +0100630 for line in nm_output.splitlines():
Yuto Takanod93fa372021-08-06 23:05:55 +0100631 if not nm_undefined_regex.match(line):
632 symbol = nm_valid_regex.match(line)
Yuto Takano12a7ecd2021-08-07 00:40:29 +0100633 if (symbol and not symbol.group("symbol").startswith(exclusions)):
Yuto Takanoe77f6992021-08-05 20:22:59 +0100634 symbols.append(symbol.group("symbol"))
Yuto Takano39639672021-08-05 19:47:48 +0100635 else:
636 self.log.error(line)
Yuto Takano81528c02021-08-06 16:22:06 +0100637
Yuto Takano39639672021-08-05 19:47:48 +0100638 return symbols
639
Yuto Takano55614b52021-08-07 01:00:18 +0100640 def perform_checks(self, quiet=False):
Yuto Takano39639672021-08-05 19:47:48 +0100641 """
642 Perform each check in order, output its PASS/FAIL status. Maintain an
643 overall test status, and output that at the end.
Yuto Takano977e07f2021-08-09 11:56:15 +0100644 Assumes parse_names_in_source() was called before this.
Yuto Takano81528c02021-08-06 16:22:06 +0100645
646 Args:
Yuto Takano55614b52021-08-07 01:00:18 +0100647 * quiet: whether to hide detailed problem explanation.
Yuto Takano39639672021-08-05 19:47:48 +0100648 """
Yuto Takano81528c02021-08-06 16:22:06 +0100649 self.log.info("=============")
Yuto Takano39639672021-08-05 19:47:48 +0100650 problems = 0
651
Yuto Takano55614b52021-08-07 01:00:18 +0100652 problems += self.check_symbols_declared_in_header(quiet)
Yuto Takano39639672021-08-05 19:47:48 +0100653
Yuto Takanod70d4462021-08-09 12:45:51 +0100654 pattern_checks = [
655 ("macros", MACRO_PATTERN),
656 ("enum_consts", CONSTANTS_PATTERN),
657 ("identifiers", IDENTIFIER_PATTERN)
658 ]
Yuto Takano39639672021-08-05 19:47:48 +0100659 for group, check_pattern in pattern_checks:
Yuto Takano55614b52021-08-07 01:00:18 +0100660 problems += self.check_match_pattern(quiet, group, check_pattern)
Yuto Takano39639672021-08-05 19:47:48 +0100661
Yuto Takano55614b52021-08-07 01:00:18 +0100662 problems += self.check_for_typos(quiet)
Yuto Takano39639672021-08-05 19:47:48 +0100663
664 self.log.info("=============")
665 if problems > 0:
666 self.log.info("FAIL: {0} problem(s) to fix".format(str(problems)))
Yuto Takano55614b52021-08-07 01:00:18 +0100667 if quiet:
668 self.log.info("Remove --quiet to see explanations.")
Yuto Takanofc54dfb2021-08-07 17:18:28 +0100669 else:
670 self.log.info("Use --quiet for minimal output.")
Yuto Takano39639672021-08-05 19:47:48 +0100671 else:
672 self.log.info("PASS")
Darryl Greend5802922018-05-08 15:30:59 +0100673
Yuto Takano55614b52021-08-07 01:00:18 +0100674 def check_symbols_declared_in_header(self, quiet):
Yuto Takano39639672021-08-05 19:47:48 +0100675 """
676 Perform a check that all detected symbols in the library object files
677 are properly declared in headers.
Yuto Takano977e07f2021-08-09 11:56:15 +0100678 Assumes parse_names_in_source() was called before this.
Darryl Greend5802922018-05-08 15:30:59 +0100679
Yuto Takano81528c02021-08-06 16:22:06 +0100680 Args:
Yuto Takano55614b52021-08-07 01:00:18 +0100681 * quiet: whether to hide detailed problem explanation.
Yuto Takano81528c02021-08-06 16:22:06 +0100682
683 Returns the number of problems that need fixing.
Yuto Takano39639672021-08-05 19:47:48 +0100684 """
685 problems = []
Yuto Takanod93fa372021-08-06 23:05:55 +0100686
Yuto Takano39639672021-08-05 19:47:48 +0100687 for symbol in self.parse_result["symbols"]:
688 found_symbol_declared = False
689 for identifier_match in self.parse_result["identifiers"]:
690 if symbol == identifier_match.name:
691 found_symbol_declared = True
692 break
Yuto Takano81528c02021-08-06 16:22:06 +0100693
Yuto Takano39639672021-08-05 19:47:48 +0100694 if not found_symbol_declared:
Yuto Takanod70d4462021-08-09 12:45:51 +0100695 problems.append(SymbolNotInHeader(symbol))
Yuto Takano39639672021-08-05 19:47:48 +0100696
Yuto Takanod70d4462021-08-09 12:45:51 +0100697 self.output_check_result(quiet, "All symbols in header", problems)
Yuto Takano39639672021-08-05 19:47:48 +0100698 return len(problems)
699
Yuto Takano55614b52021-08-07 01:00:18 +0100700 def check_match_pattern(self, quiet, group_to_check, check_pattern):
Yuto Takano81528c02021-08-06 16:22:06 +0100701 """
702 Perform a check that all items of a group conform to a regex pattern.
Yuto Takano977e07f2021-08-09 11:56:15 +0100703 Assumes parse_names_in_source() was called before this.
Yuto Takano81528c02021-08-06 16:22:06 +0100704
705 Args:
Yuto Takano55614b52021-08-07 01:00:18 +0100706 * quiet: whether to hide detailed problem explanation.
Yuto Takano81528c02021-08-06 16:22:06 +0100707 * group_to_check: string key to index into self.parse_result.
708 * check_pattern: the regex to check against.
709
710 Returns the number of problems that need fixing.
711 """
Yuto Takano39639672021-08-05 19:47:48 +0100712 problems = []
Yuto Takanod93fa372021-08-06 23:05:55 +0100713
Yuto Takano39639672021-08-05 19:47:48 +0100714 for item_match in self.parse_result[group_to_check]:
715 if not re.match(check_pattern, item_match.name):
716 problems.append(PatternMismatch(check_pattern, item_match))
Yuto Takano201f9e82021-08-06 16:36:54 +0100717 # Double underscore is a reserved identifier, never to be used
Yuto Takanoc763cc32021-08-05 20:06:34 +0100718 if re.match(r".*__.*", item_match.name):
Yuto Takanod70d4462021-08-09 12:45:51 +0100719 problems.append(PatternMismatch("double underscore", item_match))
Yuto Takano81528c02021-08-06 16:22:06 +0100720
721 self.output_check_result(
Yuto Takanod70d4462021-08-09 12:45:51 +0100722 quiet,
Yuto Takano81528c02021-08-06 16:22:06 +0100723 "Naming patterns of {}".format(group_to_check),
Yuto Takano55614b52021-08-07 01:00:18 +0100724 problems)
Yuto Takano39639672021-08-05 19:47:48 +0100725 return len(problems)
Darryl Greend5802922018-05-08 15:30:59 +0100726
Yuto Takano55614b52021-08-07 01:00:18 +0100727 def check_for_typos(self, quiet):
Yuto Takano81528c02021-08-06 16:22:06 +0100728 """
729 Perform a check that all words in the soure code beginning with MBED are
730 either defined as macros, or as enum constants.
Yuto Takano977e07f2021-08-09 11:56:15 +0100731 Assumes parse_names_in_source() was called before this.
Yuto Takano81528c02021-08-06 16:22:06 +0100732
733 Args:
Yuto Takano55614b52021-08-07 01:00:18 +0100734 * quiet: whether to hide detailed problem explanation.
Yuto Takano81528c02021-08-06 16:22:06 +0100735
736 Returns the number of problems that need fixing.
737 """
Yuto Takano39639672021-08-05 19:47:48 +0100738 problems = []
Yuto Takano39639672021-08-05 19:47:48 +0100739
Yuto Takanod70d4462021-08-09 12:45:51 +0100740 # Set comprehension, equivalent to a list comprehension wrapped by set()
Yuto Takanod93fa372021-08-06 23:05:55 +0100741 all_caps_names = {
742 match.name
743 for match
744 in self.parse_result["macros"] + self.parse_result["enum_consts"]}
745 typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$")
Yuto Takano39639672021-08-05 19:47:48 +0100746
Yuto Takanod93fa372021-08-06 23:05:55 +0100747 for name_match in self.parse_result["mbed_words"]:
Yuto Takano81528c02021-08-06 16:22:06 +0100748 found = name_match.name in all_caps_names
749
750 # Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the
751 # PSA driver, they will not exist as macros. However, they
752 # should still be checked for typos using the equivalent
753 # BUILTINs that exist.
754 if "MBEDTLS_PSA_ACCEL_" in name_match.name:
755 found = name_match.name.replace(
756 "MBEDTLS_PSA_ACCEL_",
757 "MBEDTLS_PSA_BUILTIN_") in all_caps_names
758
Yuto Takanod93fa372021-08-06 23:05:55 +0100759 if not found and not typo_exclusion.search(name_match.name):
Yuto Takanod70d4462021-08-09 12:45:51 +0100760 problems.append(Typo(name_match))
Yuto Takano39639672021-08-05 19:47:48 +0100761
Yuto Takanod70d4462021-08-09 12:45:51 +0100762 self.output_check_result(quiet, "Likely typos", problems)
Yuto Takano81528c02021-08-06 16:22:06 +0100763 return len(problems)
764
Yuto Takanod70d4462021-08-09 12:45:51 +0100765 def output_check_result(self, quiet, name, problems):
Yuto Takano81528c02021-08-06 16:22:06 +0100766 """
767 Write out the PASS/FAIL status of a performed check depending on whether
768 there were problems.
Yuto Takanod70d4462021-08-09 12:45:51 +0100769
770 Args:
771 * quiet: whether to hide detailed problem explanation.
772 * name: the name of the test
773 * problems: a List of encountered Problems
Yuto Takano81528c02021-08-06 16:22:06 +0100774 """
Yuto Takano39639672021-08-05 19:47:48 +0100775 if problems:
Darryl Greend5802922018-05-08 15:30:59 +0100776 self.set_return_code(1)
Yuto Takano55614b52021-08-07 01:00:18 +0100777 self.log.info("{}: FAIL\n".format(name))
778 for problem in problems:
Yuto Takanod70d4462021-08-09 12:45:51 +0100779 problem.quiet = quiet
Yuto Takano55614b52021-08-07 01:00:18 +0100780 self.log.warning(str(problem))
Darryl Greend5802922018-05-08 15:30:59 +0100781 else:
Yuto Takano81528c02021-08-06 16:22:06 +0100782 self.log.info("{}: PASS".format(name))
Darryl Greend5802922018-05-08 15:30:59 +0100783
Yuto Takano39639672021-08-05 19:47:48 +0100784def main():
785 """
Yuto Takano81528c02021-08-06 16:22:06 +0100786 Perform argument parsing, and create an instance of NameCheck to begin the
787 core operation.
Yuto Takano39639672021-08-05 19:47:48 +0100788 """
Yuto Takanof005c332021-08-09 13:56:36 +0100789 parser = argparse.ArgumentParser(
Yuto Takano39639672021-08-05 19:47:48 +0100790 formatter_class=argparse.RawDescriptionHelpFormatter,
791 description=(
792 "This script confirms that the naming of all symbols and identifiers "
793 "in Mbed TLS are consistent with the house style and are also "
794 "self-consistent.\n\n"
Yuto Takanof005c332021-08-09 13:56:36 +0100795 "Expected to be run from the MbedTLS root directory.")
796 )
797 parser.add_argument(
798 "-v", "--verbose",
799 action="store_true",
800 help="show parse results"
801 )
802 parser.add_argument(
803 "-q", "--quiet",
804 action="store_true",
805 help="hide unnecessary text, explanations, and highlighs"
806 )
Darryl Greend5802922018-05-08 15:30:59 +0100807
Yuto Takanof005c332021-08-09 13:56:36 +0100808 args = parser.parse_args()
Darryl Greend5802922018-05-08 15:30:59 +0100809
Darryl Greend5802922018-05-08 15:30:59 +0100810 try:
Yuto Takano977e07f2021-08-09 11:56:15 +0100811 name_check = NameCheck(verbose=args.verbose)
Yuto Takano39639672021-08-05 19:47:48 +0100812 name_check.parse_names_in_source()
Yuto Takano55614b52021-08-07 01:00:18 +0100813 name_check.perform_checks(quiet=args.quiet)
Yuto Takano81528c02021-08-06 16:22:06 +0100814 sys.exit(name_check.return_code)
Yuto Takanod93fa372021-08-06 23:05:55 +0100815 except Exception: # pylint: disable=broad-except
Darryl Greend5802922018-05-08 15:30:59 +0100816 traceback.print_exc()
817 sys.exit(2)
818
Darryl Greend5802922018-05-08 15:30:59 +0100819if __name__ == "__main__":
Yuto Takano39639672021-08-05 19:47:48 +0100820 main()