blob: 3f65b44d0cc464d214f042a260417d1a3e4a1cee [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
Yuto Takano55c6c872021-08-09 15:35:19 +010023It contains two major Python classes, CodeParser and NameChecker. They both have
24a comprehensive "run-all" function (comprehensive_parse() and perform_checks())
25but the individual functions can also be used for specific needs.
26
27CodeParser makes heavy use of regular expressions to parse the code, and is
28dependent on the current code formatting. Many Python C parser libraries require
29preprocessed C code, which means no macro parsing. Compiler tools are also not
30very helpful when we want the exact location in the original source (which
31becomes impossible when e.g. comments are stripped).
32
33NameChecker performs the following checks:
Yuto Takano81528c02021-08-06 16:22:06 +010034
35- All exported and available symbols in the library object files, are explicitly
Yuto Takano159255a2021-08-06 17:00:28 +010036 declared in the header files. This uses the nm command.
Yuto Takano81528c02021-08-06 16:22:06 +010037- All macros, constants, and identifiers (function names, struct names, etc)
Yuto Takano55c6c872021-08-09 15:35:19 +010038 follow the required regex pattern.
Yuto Takano81528c02021-08-06 16:22:06 +010039- Typo checking: All words that begin with MBED exist as macros or constants.
Yuto Takanofc54dfb2021-08-07 17:18:28 +010040
Yuto Takano55c6c872021-08-09 15:35:19 +010041The script returns 0 on success, 1 on test failure, and 2 if there is a script
Yuto Takano8246eb82021-08-16 10:37:24 +010042error. It must be run from Mbed TLS root.
Darryl Greend5802922018-05-08 15:30:59 +010043"""
Yuto Takano39639672021-08-05 19:47:48 +010044
45import argparse
Yuto Takano977e07f2021-08-09 11:56:15 +010046import glob
Yuto Takano39639672021-08-05 19:47:48 +010047import textwrap
Darryl Greend5802922018-05-08 15:30:59 +010048import os
49import sys
50import traceback
51import re
Yuto Takanob1417b42021-08-17 10:30:20 +010052import enum
Darryl Greend5802922018-05-08 15:30:59 +010053import shutil
54import subprocess
55import logging
56
Yuto Takano81528c02021-08-06 16:22:06 +010057# Naming patterns to check against. These are defined outside the NameCheck
58# class for ease of modification.
Yuto Takanobb7dca42021-08-05 19:57:58 +010059MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$"
Yuto Takano81528c02021-08-06 16:22:06 +010060CONSTANTS_PATTERN = MACRO_PATTERN
Yuto Takanoc1838932021-08-05 19:52:09 +010061IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$"
Yuto Takano39639672021-08-05 19:47:48 +010062
Yuto Takanod93fa372021-08-06 23:05:55 +010063class Match(): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +010064 """
65 A class representing a match, together with its found position.
66
67 Fields:
68 * filename: the file that the match was in.
69 * line: the full line containing the match.
Yuto Takano704b0f72021-08-17 10:41:23 +010070 * line_no: the line number.
71 * pos: a tuple of (start, end) positions on the line where the match is.
Yuto Takano81528c02021-08-06 16:22:06 +010072 * name: the match itself.
73 """
Yuto Takano704b0f72021-08-17 10:41:23 +010074 def __init__(self, filename, line, line_no, pos, name):
75 # pylint: disable=too-many-arguments
Yuto Takano39639672021-08-05 19:47:48 +010076 self.filename = filename
77 self.line = line
Yuto Takano704b0f72021-08-17 10:41:23 +010078 self.line_no = line_no
Yuto Takano39639672021-08-05 19:47:48 +010079 self.pos = pos
80 self.name = name
Yuto Takano39639672021-08-05 19:47:48 +010081
Yuto Takanoa4e75122021-08-06 17:23:28 +010082 def __str__(self):
Yuto Takanofb86ac72021-08-16 10:32:40 +010083 """
84 Return a formatted code listing representation of the erroneous line.
85 """
Yuto Takano704b0f72021-08-17 10:41:23 +010086 gutter = format(self.line_no, "4d")
87 underline = self.pos[0] * " " + (self.pos[1] - self.pos[0]) * "^"
Yuto Takano381fda82021-08-06 23:37:20 +010088
Yuto Takanoa4e75122021-08-06 17:23:28 +010089 return (
Yuto Takanofb86ac72021-08-16 10:32:40 +010090 " {0} |\n".format(" " * len(gutter)) +
Yuto Takano381fda82021-08-06 23:37:20 +010091 " {0} | {1}".format(gutter, self.line) +
Yuto Takanofb86ac72021-08-16 10:32:40 +010092 " {0} | {1}\n".format(" " * len(gutter), underline)
Yuto Takanoa4e75122021-08-06 17:23:28 +010093 )
Yuto Takanod93fa372021-08-06 23:05:55 +010094
95class Problem(): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +010096 """
97 A parent class representing a form of static analysis error.
Yuto Takano81528c02021-08-06 16:22:06 +010098 """
Yuto Takano5473be22021-08-17 10:14:01 +010099 # Class variable to control the quietness of all problems
100 quiet = False
Yuto Takano39639672021-08-05 19:47:48 +0100101 def __init__(self):
102 self.textwrapper = textwrap.TextWrapper()
Yuto Takano81528c02021-08-06 16:22:06 +0100103 self.textwrapper.width = 80
Yuto Takanoa4e75122021-08-06 17:23:28 +0100104 self.textwrapper.initial_indent = " > "
Yuto Takano81528c02021-08-06 16:22:06 +0100105 self.textwrapper.subsequent_indent = " "
Yuto Takano39639672021-08-05 19:47:48 +0100106
Yuto Takanod93fa372021-08-06 23:05:55 +0100107class SymbolNotInHeader(Problem): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +0100108 """
109 A problem that occurs when an exported/available symbol in the object file
110 is not explicitly declared in header files. Created with
111 NameCheck.check_symbols_declared_in_header()
112
113 Fields:
114 * symbol_name: the name of the symbol.
115 """
Yuto Takanod70d4462021-08-09 12:45:51 +0100116 def __init__(self, symbol_name):
Yuto Takano39639672021-08-05 19:47:48 +0100117 self.symbol_name = symbol_name
118 Problem.__init__(self)
119
120 def __str__(self):
Yuto Takano55614b52021-08-07 01:00:18 +0100121 if self.quiet:
122 return "{0}".format(self.symbol_name)
123
Yuto Takano39639672021-08-05 19:47:48 +0100124 return self.textwrapper.fill(
125 "'{0}' was found as an available symbol in the output of nm, "
126 "however it was not declared in any header files."
127 .format(self.symbol_name))
128
Yuto Takanod93fa372021-08-06 23:05:55 +0100129class PatternMismatch(Problem): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +0100130 """
131 A problem that occurs when something doesn't match the expected pattern.
132 Created with NameCheck.check_match_pattern()
133
134 Fields:
135 * pattern: the expected regex pattern
136 * match: the Match object in question
137 """
Yuto Takanod70d4462021-08-09 12:45:51 +0100138 def __init__(self, pattern, match):
Yuto Takano39639672021-08-05 19:47:48 +0100139 self.pattern = pattern
140 self.match = match
141 Problem.__init__(self)
Yuto Takano81528c02021-08-06 16:22:06 +0100142
Yuto Takano39639672021-08-05 19:47:48 +0100143 def __str__(self):
Yuto Takano55614b52021-08-07 01:00:18 +0100144 if self.quiet:
Yuto Takanod70d4462021-08-09 12:45:51 +0100145 return (
Yuto Takano206b0222021-08-10 11:30:43 +0100146 "{0}:{1}:{2}"
Yuto Takanod70d4462021-08-09 12:45:51 +0100147 .format(self.match.filename, self.match.pos[0], self.match.name)
148 )
Yuto Takano55614b52021-08-07 01:00:18 +0100149
Yuto Takano39639672021-08-05 19:47:48 +0100150 return self.textwrapper.fill(
Yuto Takanoa4e75122021-08-06 17:23:28 +0100151 "{0}:{1}: '{2}' does not match the required pattern '{3}'."
152 .format(
153 self.match.filename,
Yuto Takanod93fa372021-08-06 23:05:55 +0100154 self.match.pos[0],
Yuto Takanoa4e75122021-08-06 17:23:28 +0100155 self.match.name,
Yuto Takanod70d4462021-08-09 12:45:51 +0100156 self.pattern
157 )
158 ) + "\n" + str(self.match)
Yuto Takano39639672021-08-05 19:47:48 +0100159
Yuto Takanod93fa372021-08-06 23:05:55 +0100160class Typo(Problem): # pylint: disable=too-few-public-methods
Yuto Takano81528c02021-08-06 16:22:06 +0100161 """
162 A problem that occurs when a word using MBED doesn't appear to be defined as
163 constants nor enum values. Created with NameCheck.check_for_typos()
164
165 Fields:
166 * match: the Match object of the MBED name in question.
167 """
Yuto Takanod70d4462021-08-09 12:45:51 +0100168 def __init__(self, match):
Yuto Takano39639672021-08-05 19:47:48 +0100169 self.match = match
170 Problem.__init__(self)
Yuto Takano81528c02021-08-06 16:22:06 +0100171
Yuto Takano39639672021-08-05 19:47:48 +0100172 def __str__(self):
Yuto Takano55614b52021-08-07 01:00:18 +0100173 if self.quiet:
Yuto Takanod70d4462021-08-09 12:45:51 +0100174 return (
175 "{0}:{1}:{2}"
176 .format(self.match.filename, self.match.pos[0], self.match.name)
177 )
Yuto Takano55614b52021-08-07 01:00:18 +0100178
Yuto Takano39639672021-08-05 19:47:48 +0100179 return self.textwrapper.fill(
Yuto Takanoa4e75122021-08-06 17:23:28 +0100180 "{0}:{1}: '{2}' looks like a typo. It was not found in any "
181 "macros or any enums. If this is not a typo, put "
182 "//no-check-names after it."
Yuto Takanod70d4462021-08-09 12:45:51 +0100183 .format(self.match.filename, self.match.pos[0], self.match.name)
184 ) + "\n" + str(self.match)
Darryl Greend5802922018-05-08 15:30:59 +0100185
Yuto Takano55c6c872021-08-09 15:35:19 +0100186class CodeParser():
Yuto Takano81528c02021-08-06 16:22:06 +0100187 """
Yuto Takano55c6c872021-08-09 15:35:19 +0100188 Class for retrieving files and parsing the code. This can be used
189 independently of the checks that NameChecker performs, for example for
190 list_internal_identifiers.py.
Yuto Takano81528c02021-08-06 16:22:06 +0100191 """
Yuto Takano55c6c872021-08-09 15:35:19 +0100192 def __init__(self, log):
193 self.log = log
Yuto Takanofc54dfb2021-08-07 17:18:28 +0100194 self.check_repo_path()
Yuto Takano977e07f2021-08-09 11:56:15 +0100195
Yuto Takano8e9a2192021-08-09 14:48:53 +0100196 # Memo for storing "glob expression": set(filepaths)
197 self.files = {}
198
Yuto Takano977e07f2021-08-09 11:56:15 +0100199 # Globally excluded filenames
Yuto Takano8e9a2192021-08-09 14:48:53 +0100200 self.excluded_files = ["**/bn_mul", "**/compat-2.x.h"]
Yuto Takano977e07f2021-08-09 11:56:15 +0100201
Yuto Takanofc54dfb2021-08-07 17:18:28 +0100202 @staticmethod
203 def check_repo_path():
204 """
205 Check that the current working directory is the project root, and throw
206 an exception if not.
207 """
208 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
209 raise Exception("This script must be run from Mbed TLS root")
210
Yuto Takano55c6c872021-08-09 15:35:19 +0100211 def comprehensive_parse(self):
Yuto Takano39639672021-08-05 19:47:48 +0100212 """
Yuto Takano55c6c872021-08-09 15:35:19 +0100213 Comprehensive ("default") function to call each parsing function and
214 retrieve various elements of the code, together with the source location.
Darryl Greend5802922018-05-08 15:30:59 +0100215
Yuto Takano55c6c872021-08-09 15:35:19 +0100216 Returns a dict of parsed item key to the corresponding List of Matches.
Yuto Takano81528c02021-08-06 16:22:06 +0100217 """
218 self.log.info("Parsing source code...")
Yuto Takanod24e0372021-08-06 16:42:33 +0100219 self.log.debug(
Yuto Takano50953432021-08-09 14:54:36 +0100220 "The following files are excluded from the search: {}"
Yuto Takanod24e0372021-08-06 16:42:33 +0100221 .format(str(self.excluded_files))
222 )
Yuto Takano81528c02021-08-06 16:22:06 +0100223
Yuto Takano8e9a2192021-08-09 14:48:53 +0100224 all_macros = self.parse_macros([
225 "include/mbedtls/*.h",
226 "include/psa/*.h",
227 "library/*.h",
228 "tests/include/test/drivers/*.h",
Yuto Takanod70d4462021-08-09 12:45:51 +0100229 "3rdparty/everest/include/everest/everest.h",
230 "3rdparty/everest/include/everest/x25519.h"
Yuto Takano8e9a2192021-08-09 14:48:53 +0100231 ])
232 enum_consts = self.parse_enum_consts([
233 "include/mbedtls/*.h",
234 "library/*.h",
235 "3rdparty/everest/include/everest/everest.h",
236 "3rdparty/everest/include/everest/x25519.h"
237 ])
238 identifiers = self.parse_identifiers([
239 "include/mbedtls/*.h",
240 "include/psa/*.h",
241 "library/*.h",
242 "3rdparty/everest/include/everest/everest.h",
243 "3rdparty/everest/include/everest/x25519.h"
244 ])
245 mbed_words = self.parse_mbed_words([
246 "include/mbedtls/*.h",
247 "include/psa/*.h",
248 "library/*.h",
249 "3rdparty/everest/include/everest/everest.h",
250 "3rdparty/everest/include/everest/x25519.h",
251 "library/*.c",
Yuto Takano81528c02021-08-06 16:22:06 +0100252 "3rdparty/everest/library/everest.c",
Yuto Takanod70d4462021-08-09 12:45:51 +0100253 "3rdparty/everest/library/x25519.c"
Yuto Takano8e9a2192021-08-09 14:48:53 +0100254 ])
Yuto Takano81528c02021-08-06 16:22:06 +0100255 symbols = self.parse_symbols()
256
257 # Remove identifier macros like mbedtls_printf or mbedtls_calloc
258 identifiers_justname = [x.name for x in identifiers]
259 actual_macros = []
260 for macro in all_macros:
261 if macro.name not in identifiers_justname:
262 actual_macros.append(macro)
263
264 self.log.debug("Found:")
Yuto Takano9d9c6dc2021-08-16 10:43:45 +0100265 # Aligns the counts on the assumption that none exceeds 4 digits
266 self.log.debug(" {:4} Total Macros".format(len(all_macros)))
267 self.log.debug(" {:4} Non-identifier Macros".format(len(actual_macros)))
268 self.log.debug(" {:4} Enum Constants".format(len(enum_consts)))
269 self.log.debug(" {:4} Identifiers".format(len(identifiers)))
270 self.log.debug(" {:4} Exported Symbols".format(len(symbols)))
Yuto Takano55c6c872021-08-09 15:35:19 +0100271 return {
Yuto Takano81528c02021-08-06 16:22:06 +0100272 "macros": actual_macros,
273 "enum_consts": enum_consts,
274 "identifiers": identifiers,
275 "symbols": symbols,
Yuto Takanod93fa372021-08-06 23:05:55 +0100276 "mbed_words": mbed_words
Yuto Takano81528c02021-08-06 16:22:06 +0100277 }
278
Yuto Takano55c6c872021-08-09 15:35:19 +0100279 def get_files(self, include_wildcards, exclude_wildcards):
280 """
281 Get all files that match any of the UNIX-style wildcards. While the
282 check_names script is designed only for use on UNIX/macOS (due to nm),
283 this function alone would work fine on Windows even with forward slashes
284 in the wildcard.
285
286 Args:
287 * include_wildcards: a List of shell-style wildcards to match filepaths.
288 * exclude_wildcards: a List of shell-style wildcards to exclude.
289
290 Returns a List of relative filepaths.
291 """
292 accumulator = set()
293
294 # exclude_wildcards may be None. Also, consider the global exclusions.
295 exclude_wildcards = (exclude_wildcards or []) + self.excluded_files
296
Yuto Takano6adb2872021-08-16 11:38:34 +0100297 # Internal function to hit the memoisation cache or add to it the result
298 # of a glob operation. Used both for inclusion and exclusion since the
299 # only difference between them is whether they perform set union or
300 # difference on the return value of this function.
301 def hit_cache(wildcard):
302 if wildcard not in self.files:
303 self.files[wildcard] = set(glob.glob(wildcard, recursive=True))
304 return self.files[wildcard]
305
Yuto Takano55c6c872021-08-09 15:35:19 +0100306 for include_wildcard in include_wildcards:
Yuto Takano6adb2872021-08-16 11:38:34 +0100307 accumulator = accumulator.union(hit_cache(include_wildcard))
Yuto Takano55c6c872021-08-09 15:35:19 +0100308
Yuto Takano55c6c872021-08-09 15:35:19 +0100309 for exclude_wildcard in exclude_wildcards:
Yuto Takano6adb2872021-08-16 11:38:34 +0100310 accumulator = accumulator.difference(hit_cache(exclude_wildcard))
Yuto Takano55c6c872021-08-09 15:35:19 +0100311
312 return list(accumulator)
313
Yuto Takano8e9a2192021-08-09 14:48:53 +0100314 def parse_macros(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100315 """
316 Parse all macros defined by #define preprocessor directives.
317
318 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100319 * include: A List of glob expressions to look for files through.
320 * exclude: A List of glob expressions for excluding files.
Yuto Takano81528c02021-08-06 16:22:06 +0100321
322 Returns a List of Match objects for the found macros.
Yuto Takano39639672021-08-05 19:47:48 +0100323 """
Yuto Takanod93fa372021-08-06 23:05:55 +0100324 macro_regex = re.compile(r"# *define +(?P<macro>\w+)")
325 exclusions = (
Yuto Takano39639672021-08-05 19:47:48 +0100326 "asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_"
327 )
328
Yuto Takano50953432021-08-09 14:54:36 +0100329 files = self.get_files(include, exclude)
330 self.log.debug("Looking for macros in {} files".format(len(files)))
Yuto Takanod93fa372021-08-06 23:05:55 +0100331
Yuto Takano50953432021-08-09 14:54:36 +0100332 macros = []
333 for header_file in files:
Yuto Takanoa083d152021-08-07 00:25:59 +0100334 with open(header_file, "r", encoding="utf-8") as header:
Yuto Takano8f457cf2021-08-06 17:54:58 +0100335 for line_no, line in enumerate(header):
Yuto Takanod93fa372021-08-06 23:05:55 +0100336 for macro in macro_regex.finditer(line):
Yuto Takanod70d4462021-08-09 12:45:51 +0100337 if macro.group("macro").startswith(exclusions):
338 continue
339
340 macros.append(Match(
341 header_file,
342 line,
Yuto Takano704b0f72021-08-17 10:41:23 +0100343 line_no,
344 macro.span("macro"),
Yuto Takanod70d4462021-08-09 12:45:51 +0100345 macro.group("macro")))
Darryl Greend5802922018-05-08 15:30:59 +0100346
Yuto Takano39639672021-08-05 19:47:48 +0100347 return macros
Darryl Greend5802922018-05-08 15:30:59 +0100348
Yuto Takano8e9a2192021-08-09 14:48:53 +0100349 def parse_mbed_words(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100350 """
Yuto Takanob47b5042021-08-07 00:42:54 +0100351 Parse all words in the file that begin with MBED, in and out of macros,
352 comments, anything.
Yuto Takano39639672021-08-05 19:47:48 +0100353
354 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100355 * include: A List of glob expressions to look for files through.
356 * exclude: A List of glob expressions for excluding files.
Yuto Takano81528c02021-08-06 16:22:06 +0100357
358 Returns a List of Match objects for words beginning with MBED.
Yuto Takano39639672021-08-05 19:47:48 +0100359 """
Yuto Takanob47b5042021-08-07 00:42:54 +0100360 # Typos of TLS are common, hence the broader check below than MBEDTLS.
Yuto Takanod93fa372021-08-06 23:05:55 +0100361 mbed_regex = re.compile(r"\bMBED.+?_[A-Z0-9_]*")
362 exclusions = re.compile(r"// *no-check-names|#error")
363
Yuto Takano50953432021-08-09 14:54:36 +0100364 files = self.get_files(include, exclude)
365 self.log.debug("Looking for MBED words in {} files".format(len(files)))
Yuto Takanod93fa372021-08-06 23:05:55 +0100366
Yuto Takano50953432021-08-09 14:54:36 +0100367 mbed_words = []
368 for filename in files:
Yuto Takanoa083d152021-08-07 00:25:59 +0100369 with open(filename, "r", encoding="utf-8") as fp:
Yuto Takano8f457cf2021-08-06 17:54:58 +0100370 for line_no, line in enumerate(fp):
Yuto Takanod93fa372021-08-06 23:05:55 +0100371 if exclusions.search(line):
Yuto Takanoc62b4082021-08-05 20:17:07 +0100372 continue
Yuto Takano81528c02021-08-06 16:22:06 +0100373
Yuto Takanod93fa372021-08-06 23:05:55 +0100374 for name in mbed_regex.finditer(line):
375 mbed_words.append(Match(
Yuto Takano39639672021-08-05 19:47:48 +0100376 filename,
377 line,
Yuto Takano704b0f72021-08-17 10:41:23 +0100378 line_no,
379 name.span(0),
380 name.group(0)))
Yuto Takano39639672021-08-05 19:47:48 +0100381
Yuto Takanod93fa372021-08-06 23:05:55 +0100382 return mbed_words
Yuto Takano39639672021-08-05 19:47:48 +0100383
Yuto Takano8e9a2192021-08-09 14:48:53 +0100384 def parse_enum_consts(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100385 """
386 Parse all enum value constants that are declared.
387
388 Args:
Yuto Takano8e9a2192021-08-09 14:48:53 +0100389 * include: A List of glob expressions to look for files through.
390 * exclude: A List of glob expressions for excluding files.
Yuto Takano39639672021-08-05 19:47:48 +0100391
Yuto Takano81528c02021-08-06 16:22:06 +0100392 Returns a List of Match objects for the findings.
Yuto Takano39639672021-08-05 19:47:48 +0100393 """
Yuto Takano50953432021-08-09 14:54:36 +0100394 files = self.get_files(include, exclude)
395 self.log.debug("Looking for enum consts in {} files".format(len(files)))
Yuto Takanod93fa372021-08-06 23:05:55 +0100396
Yuto Takanob1417b42021-08-17 10:30:20 +0100397 # Emulate a finite state machine to parse enum declarations.
398 # OUTSIDE_KEYWORD = outside the enum keyword
399 # IN_BRACES = inside enum opening braces
400 # IN_BETWEEN = between enum keyword and opening braces
401 states = enum.Enum("FSM", ["OUTSIDE_KEYWORD", "IN_BRACES", "IN_BETWEEN"])
Yuto Takano50953432021-08-09 14:54:36 +0100402 enum_consts = []
403 for header_file in files:
Yuto Takanob1417b42021-08-17 10:30:20 +0100404 state = states.OUTSIDE_KEYWORD
Yuto Takanoa083d152021-08-07 00:25:59 +0100405 with open(header_file, "r", encoding="utf-8") as header:
Yuto Takano8f457cf2021-08-06 17:54:58 +0100406 for line_no, line in enumerate(header):
Yuto Takano13ecd992021-08-06 16:56:52 +0100407 # Match typedefs and brackets only when they are at the
408 # beginning of the line -- if they are indented, they might
409 # be sub-structures within structs, etc.
Yuto Takanob1417b42021-08-17 10:30:20 +0100410 if (state == states.OUTSIDE_KEYWORD and
411 re.search(r"^(typedef +)?enum +{", line)):
412 state = states.IN_BRACES
413 elif (state == states.OUTSIDE_KEYWORD and
414 re.search(r"^(typedef +)?enum", line)):
415 state = states.IN_BETWEEN
416 elif (state == states.IN_BETWEEN and
417 re.search(r"^{", line)):
418 state = states.IN_BRACES
419 elif (state == states.IN_BRACES and
420 re.search(r"^}", line)):
421 state = states.OUTSIDE_KEYWORD
422 elif (state == states.IN_BRACES and
423 not re.search(r"^ *#", line)):
Yuto Takano90bc0262021-08-16 11:34:10 +0100424 enum_const = re.search(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,
Yuto Takano704b0f72021-08-17 10:41:23 +0100431 line_no,
432 enum_const.span("enum_const"),
Yuto Takanod70d4462021-08-09 12:45:51 +0100433 enum_const.group("enum_const")))
Yuto Takano81528c02021-08-06 16:22:06 +0100434
Yuto Takano39639672021-08-05 19:47:48 +0100435 return enum_consts
Darryl Greend5802922018-05-08 15:30:59 +0100436
Yuto Takano8e9a2192021-08-09 14:48:53 +0100437 def parse_identifiers(self, include, exclude=None):
Yuto Takano39639672021-08-05 19:47:48 +0100438 """
Yuto Takano8246eb82021-08-16 10:37:24 +0100439 Parse all lines of a header where a function/enum/struct/union/typedef
Yuto Takanob1417b42021-08-17 10:30:20 +0100440 identifier is declared, based on some regex and heuristics. Highly
441 dependent on formatting style.
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
Yuto Takano55c6c872021-08-09 15:35:19 +0100453 # all on one line
Yuto Takanod93fa372021-08-06 23:05:55 +0100454 # - function definition line only contains alphanumeric, asterisk,
455 # underscore, and open bracket
456 r".* \**(\w+) *\( *\w|"
Yuto Takano55c6c872021-08-09 15:35:19 +0100457 # Match "(*something)(".
Yuto Takanod93fa372021-08-06 23:05:55 +0100458 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"^("
Yuto Takano90bc0262021-08-16 11:34:10 +0100466 r"extern +\"C\"|"
467 r"(typedef +)?(struct|union|enum)( *{)?$|"
468 r"} *;?$|"
469 r"$|"
470 r"//|"
471 r"#"
Yuto Takanod70d4462021-08-09 12:45:51 +0100472 r")"
473 )
Yuto Takanod93fa372021-08-06 23:05:55 +0100474
Yuto Takano50953432021-08-09 14:54:36 +0100475 files = self.get_files(include, exclude)
476 self.log.debug("Looking for identifiers in {} files".format(len(files)))
477
478 identifiers = []
479 for header_file in files:
Yuto Takanoa083d152021-08-07 00:25:59 +0100480 with open(header_file, "r", encoding="utf-8") as header:
Yuto Takano39639672021-08-05 19:47:48 +0100481 in_block_comment = False
Yuto Takano55c6c872021-08-09 15:35:19 +0100482 # The previous line variable is used for concatenating lines
Yuto Takanob1417b42021-08-17 10:30:20 +0100483 # when identifiers are formatted and spread across multiple
484 # lines.
Yuto Takanod93fa372021-08-06 23:05:55 +0100485 previous_line = ""
Darryl Greend5802922018-05-08 15:30:59 +0100486
Yuto Takano8f457cf2021-08-06 17:54:58 +0100487 for line_no, line in enumerate(header):
Yuto Takano81528c02021-08-06 16:22:06 +0100488 # Skip parsing this line if a block comment ends on it,
489 # but don't skip if it has just started -- there is a chance
490 # it ends on the same line.
Yuto Takano39639672021-08-05 19:47:48 +0100491 if re.search(r"/\*", line):
Yuto Takano81528c02021-08-06 16:22:06 +0100492 in_block_comment = not in_block_comment
493 if re.search(r"\*/", line):
494 in_block_comment = not in_block_comment
Yuto Takano39639672021-08-05 19:47:48 +0100495 continue
496
Yuto Takano81528c02021-08-06 16:22:06 +0100497 if in_block_comment:
Yuto Takanod93fa372021-08-06 23:05:55 +0100498 previous_line = ""
Yuto Takano81528c02021-08-06 16:22:06 +0100499 continue
500
Yuto Takano90bc0262021-08-16 11:34:10 +0100501 if exclusion_lines.search(line):
Yuto Takanod93fa372021-08-06 23:05:55 +0100502 previous_line = ""
Yuto Takano81528c02021-08-06 16:22:06 +0100503 continue
504
Yuto Takanocfc9e4a2021-08-06 20:02:32 +0100505 # If the line contains only space-separated alphanumeric
506 # characters (or underscore, asterisk, or, open bracket),
507 # and nothing else, high chance it's a declaration that
508 # continues on the next line
Yuto Takano90bc0262021-08-16 11:34:10 +0100509 if re.search(r"^([\w\*\(]+\s+)+$", line):
Yuto Takanod93fa372021-08-06 23:05:55 +0100510 previous_line += line
Yuto Takano81528c02021-08-06 16:22:06 +0100511 continue
512
513 # If previous line seemed to start an unfinished declaration
Yuto Takanocfc9e4a2021-08-06 20:02:32 +0100514 # (as above), concat and treat them as one.
515 if previous_line:
Yuto Takano90bc0262021-08-16 11:34:10 +0100516 line = previous_line.strip() + " " + line.strip() + "\n"
Yuto Takanod93fa372021-08-06 23:05:55 +0100517 previous_line = ""
Yuto Takano81528c02021-08-06 16:22:06 +0100518
Yuto Takano8246eb82021-08-16 10:37:24 +0100519 # Skip parsing if line has a space in front = heuristic to
Yuto Takano81528c02021-08-06 16:22:06 +0100520 # skip function argument lines (highly subject to formatting
521 # changes)
522 if line[0] == " ":
Yuto Takano39639672021-08-05 19:47:48 +0100523 continue
Yuto Takano6f38ab32021-08-05 21:07:14 +0100524
Yuto Takanod93fa372021-08-06 23:05:55 +0100525 identifier = identifier_regex.search(line)
Yuto Takano39639672021-08-05 19:47:48 +0100526
Yuto Takanod70d4462021-08-09 12:45:51 +0100527 if not identifier:
528 continue
529
530 # Find the group that matched, and append it
531 for group in identifier.groups():
532 if not group:
533 continue
534
535 identifiers.append(Match(
536 header_file,
537 line,
Yuto Takano704b0f72021-08-17 10:41:23 +0100538 line_no,
539 identifier.span(),
Yuto Takanod70d4462021-08-09 12:45:51 +0100540 group))
Yuto Takano39639672021-08-05 19:47:48 +0100541
542 return identifiers
543
544 def parse_symbols(self):
545 """
546 Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509
547 object files using nm to retrieve the list of referenced symbols.
Yuto Takano81528c02021-08-06 16:22:06 +0100548 Exceptions thrown here are rethrown because they would be critical
549 errors that void several tests, and thus needs to halt the program. This
550 is explicitly done for clarity.
Yuto Takano39639672021-08-05 19:47:48 +0100551
Yuto Takano81528c02021-08-06 16:22:06 +0100552 Returns a List of unique symbols defined and used in the libraries.
553 """
554 self.log.info("Compiling...")
Yuto Takano39639672021-08-05 19:47:48 +0100555 symbols = []
556
557 # Back up the config and atomically compile with the full configratuion.
Yuto Takanod70d4462021-08-09 12:45:51 +0100558 shutil.copy(
559 "include/mbedtls/mbedtls_config.h",
560 "include/mbedtls/mbedtls_config.h.bak"
561 )
Darryl Greend5802922018-05-08 15:30:59 +0100562 try:
Yuto Takano81528c02021-08-06 16:22:06 +0100563 # Use check=True in all subprocess calls so that failures are raised
564 # as exceptions and logged.
Yuto Takano39639672021-08-05 19:47:48 +0100565 subprocess.run(
Yuto Takano81528c02021-08-06 16:22:06 +0100566 ["python3", "scripts/config.py", "full"],
Yuto Takanobcc3d992021-08-06 23:14:58 +0100567 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100568 check=True
Darryl Greend5802922018-05-08 15:30:59 +0100569 )
570 my_environment = os.environ.copy()
571 my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables"
Yuto Takano4b7d23d2021-08-17 10:48:22 +0100572 # Run make clean separately to lib to prevent unwanted behavior when
573 # make is invoked with parallelism.
Yuto Takano39639672021-08-05 19:47:48 +0100574 subprocess.run(
Yuto Takano4b7d23d2021-08-17 10:48:22 +0100575 ["make", "clean"],
576 universal_newlines=True,
577 check=True
578 )
579 subprocess.run(
580 ["make", "lib"],
Darryl Greend5802922018-05-08 15:30:59 +0100581 env=my_environment,
Yuto Takanobcc3d992021-08-06 23:14:58 +0100582 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100583 stdout=subprocess.PIPE,
Darryl Greend5802922018-05-08 15:30:59 +0100584 stderr=subprocess.STDOUT,
Yuto Takano39639672021-08-05 19:47:48 +0100585 check=True
Darryl Greend5802922018-05-08 15:30:59 +0100586 )
Yuto Takano39639672021-08-05 19:47:48 +0100587
588 # Perform object file analysis using nm
Yuto Takanod70d4462021-08-09 12:45:51 +0100589 symbols = self.parse_symbols_from_nm([
590 "library/libmbedcrypto.a",
591 "library/libmbedtls.a",
592 "library/libmbedx509.a"
593 ])
Yuto Takano39639672021-08-05 19:47:48 +0100594
595 subprocess.run(
Darryl Greend5802922018-05-08 15:30:59 +0100596 ["make", "clean"],
Yuto Takanobcc3d992021-08-06 23:14:58 +0100597 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100598 check=True
Darryl Greend5802922018-05-08 15:30:59 +0100599 )
600 except subprocess.CalledProcessError as error:
Yuto Takano25eeb7b2021-08-06 21:27:59 +0100601 self.log.debug(error.output)
Yuto Takano81528c02021-08-06 16:22:06 +0100602 raise error
Yuto Takano39639672021-08-05 19:47:48 +0100603 finally:
Yuto Takano6fececf2021-08-07 17:28:23 +0100604 # Put back the original config regardless of there being errors.
605 # Works also for keyboard interrupts.
Yuto Takanod70d4462021-08-09 12:45:51 +0100606 shutil.move(
607 "include/mbedtls/mbedtls_config.h.bak",
608 "include/mbedtls/mbedtls_config.h"
609 )
Yuto Takano39639672021-08-05 19:47:48 +0100610
611 return symbols
612
613 def parse_symbols_from_nm(self, object_files):
614 """
615 Run nm to retrieve the list of referenced symbols in each object file.
616 Does not return the position data since it is of no use.
617
Yuto Takano81528c02021-08-06 16:22:06 +0100618 Args:
Yuto Takano55c6c872021-08-09 15:35:19 +0100619 * object_files: a List of compiled object filepaths to search through.
Yuto Takano81528c02021-08-06 16:22:06 +0100620
621 Returns a List of unique symbols defined and used in any of the object
622 files.
Yuto Takano39639672021-08-05 19:47:48 +0100623 """
Yuto Takanod93fa372021-08-06 23:05:55 +0100624 nm_undefined_regex = re.compile(r"^\S+: +U |^$|^\S+:$")
625 nm_valid_regex = re.compile(r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)")
Yuto Takano12a7ecd2021-08-07 00:40:29 +0100626 exclusions = ("FStar", "Hacl")
Yuto Takano39639672021-08-05 19:47:48 +0100627
628 symbols = []
629
Yuto Takano81528c02021-08-06 16:22:06 +0100630 # Gather all outputs of nm
Yuto Takano39639672021-08-05 19:47:48 +0100631 nm_output = ""
632 for lib in object_files:
633 nm_output += subprocess.run(
634 ["nm", "-og", lib],
Yuto Takanobcc3d992021-08-06 23:14:58 +0100635 universal_newlines=True,
Yuto Takano39639672021-08-05 19:47:48 +0100636 stdout=subprocess.PIPE,
637 stderr=subprocess.STDOUT,
638 check=True
639 ).stdout
Yuto Takano81528c02021-08-06 16:22:06 +0100640
Yuto Takano39639672021-08-05 19:47:48 +0100641 for line in nm_output.splitlines():
Yuto Takano90bc0262021-08-16 11:34:10 +0100642 if not nm_undefined_regex.search(line):
643 symbol = nm_valid_regex.search(line)
Yuto Takano12a7ecd2021-08-07 00:40:29 +0100644 if (symbol and not symbol.group("symbol").startswith(exclusions)):
Yuto Takanoe77f6992021-08-05 20:22:59 +0100645 symbols.append(symbol.group("symbol"))
Yuto Takano39639672021-08-05 19:47:48 +0100646 else:
647 self.log.error(line)
Yuto Takano81528c02021-08-06 16:22:06 +0100648
Yuto Takano39639672021-08-05 19:47:48 +0100649 return symbols
650
Yuto Takano55c6c872021-08-09 15:35:19 +0100651class NameChecker():
652 """
653 Representation of the core name checking operation performed by this script.
654 """
655 def __init__(self, parse_result, log):
656 self.parse_result = parse_result
657 self.log = log
658
Yuto Takano55614b52021-08-07 01:00:18 +0100659 def perform_checks(self, quiet=False):
Yuto Takano39639672021-08-05 19:47:48 +0100660 """
Yuto Takano55c6c872021-08-09 15:35:19 +0100661 A comprehensive checker that performs each check in order, and outputs
662 a final verdict.
Yuto Takano81528c02021-08-06 16:22:06 +0100663
664 Args:
Yuto Takano55614b52021-08-07 01:00:18 +0100665 * quiet: whether to hide detailed problem explanation.
Yuto Takano39639672021-08-05 19:47:48 +0100666 """
Yuto Takano81528c02021-08-06 16:22:06 +0100667 self.log.info("=============")
Yuto Takano5473be22021-08-17 10:14:01 +0100668 Problem.quiet = quiet
Yuto Takano39639672021-08-05 19:47:48 +0100669 problems = 0
Yuto Takano5473be22021-08-17 10:14:01 +0100670 problems += self.check_symbols_declared_in_header()
Yuto Takano39639672021-08-05 19:47:48 +0100671
Yuto Takanod70d4462021-08-09 12:45:51 +0100672 pattern_checks = [
673 ("macros", MACRO_PATTERN),
674 ("enum_consts", CONSTANTS_PATTERN),
675 ("identifiers", IDENTIFIER_PATTERN)
676 ]
Yuto Takano39639672021-08-05 19:47:48 +0100677 for group, check_pattern in pattern_checks:
Yuto Takano5473be22021-08-17 10:14:01 +0100678 problems += self.check_match_pattern(group, check_pattern)
Yuto Takano39639672021-08-05 19:47:48 +0100679
Yuto Takano5473be22021-08-17 10:14:01 +0100680 problems += self.check_for_typos()
Yuto Takano39639672021-08-05 19:47:48 +0100681
682 self.log.info("=============")
683 if problems > 0:
684 self.log.info("FAIL: {0} problem(s) to fix".format(str(problems)))
Yuto Takano55614b52021-08-07 01:00:18 +0100685 if quiet:
686 self.log.info("Remove --quiet to see explanations.")
Yuto Takanofc54dfb2021-08-07 17:18:28 +0100687 else:
688 self.log.info("Use --quiet for minimal output.")
Yuto Takano55c6c872021-08-09 15:35:19 +0100689 return 1
Yuto Takano39639672021-08-05 19:47:48 +0100690 else:
691 self.log.info("PASS")
Yuto Takano55c6c872021-08-09 15:35:19 +0100692 return 0
Darryl Greend5802922018-05-08 15:30:59 +0100693
Yuto Takano5473be22021-08-17 10:14:01 +0100694 def check_symbols_declared_in_header(self):
Yuto Takano39639672021-08-05 19:47:48 +0100695 """
696 Perform a check that all detected symbols in the library object files
697 are properly declared in headers.
Yuto Takano977e07f2021-08-09 11:56:15 +0100698 Assumes parse_names_in_source() was called before this.
Darryl Greend5802922018-05-08 15:30:59 +0100699
Yuto Takano81528c02021-08-06 16:22:06 +0100700 Returns the number of problems that need fixing.
Yuto Takano39639672021-08-05 19:47:48 +0100701 """
702 problems = []
Yuto Takanod93fa372021-08-06 23:05:55 +0100703
Yuto Takano39639672021-08-05 19:47:48 +0100704 for symbol in self.parse_result["symbols"]:
705 found_symbol_declared = False
706 for identifier_match in self.parse_result["identifiers"]:
707 if symbol == identifier_match.name:
708 found_symbol_declared = True
709 break
Yuto Takano81528c02021-08-06 16:22:06 +0100710
Yuto Takano39639672021-08-05 19:47:48 +0100711 if not found_symbol_declared:
Yuto Takanod70d4462021-08-09 12:45:51 +0100712 problems.append(SymbolNotInHeader(symbol))
Yuto Takano39639672021-08-05 19:47:48 +0100713
Yuto Takano5473be22021-08-17 10:14:01 +0100714 self.output_check_result("All symbols in header", problems)
Yuto Takano39639672021-08-05 19:47:48 +0100715 return len(problems)
716
Yuto Takano5473be22021-08-17 10:14:01 +0100717 def check_match_pattern(self, group_to_check, check_pattern):
Yuto Takano81528c02021-08-06 16:22:06 +0100718 """
719 Perform a check that all items of a group conform to a regex pattern.
Yuto Takano977e07f2021-08-09 11:56:15 +0100720 Assumes parse_names_in_source() was called before this.
Yuto Takano81528c02021-08-06 16:22:06 +0100721
722 Args:
Yuto Takano81528c02021-08-06 16:22:06 +0100723 * group_to_check: string key to index into self.parse_result.
724 * check_pattern: the regex to check against.
725
726 Returns the number of problems that need fixing.
727 """
Yuto Takano39639672021-08-05 19:47:48 +0100728 problems = []
Yuto Takanod93fa372021-08-06 23:05:55 +0100729
Yuto Takano39639672021-08-05 19:47:48 +0100730 for item_match in self.parse_result[group_to_check]:
Yuto Takano90bc0262021-08-16 11:34:10 +0100731 if not re.search(check_pattern, item_match.name):
Yuto Takano39639672021-08-05 19:47:48 +0100732 problems.append(PatternMismatch(check_pattern, item_match))
Yuto Takano90bc0262021-08-16 11:34:10 +0100733 # Double underscore should not be used for names
734 if re.search(r".*__.*", item_match.name):
Yuto Takano704b0f72021-08-17 10:41:23 +0100735 problems.append(
736 PatternMismatch("no double underscore allowed", item_match))
Yuto Takano81528c02021-08-06 16:22:06 +0100737
738 self.output_check_result(
739 "Naming patterns of {}".format(group_to_check),
Yuto Takano55614b52021-08-07 01:00:18 +0100740 problems)
Yuto Takano39639672021-08-05 19:47:48 +0100741 return len(problems)
Darryl Greend5802922018-05-08 15:30:59 +0100742
Yuto Takano5473be22021-08-17 10:14:01 +0100743 def check_for_typos(self):
Yuto Takano81528c02021-08-06 16:22:06 +0100744 """
745 Perform a check that all words in the soure code beginning with MBED are
746 either defined as macros, or as enum constants.
Yuto Takano977e07f2021-08-09 11:56:15 +0100747 Assumes parse_names_in_source() was called before this.
Yuto Takano81528c02021-08-06 16:22:06 +0100748
Yuto Takano81528c02021-08-06 16:22:06 +0100749 Returns the number of problems that need fixing.
750 """
Yuto Takano39639672021-08-05 19:47:48 +0100751 problems = []
Yuto Takano39639672021-08-05 19:47:48 +0100752
Yuto Takanod70d4462021-08-09 12:45:51 +0100753 # Set comprehension, equivalent to a list comprehension wrapped by set()
Yuto Takanod93fa372021-08-06 23:05:55 +0100754 all_caps_names = {
755 match.name
756 for match
757 in self.parse_result["macros"] + self.parse_result["enum_consts"]}
758 typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$")
Yuto Takano39639672021-08-05 19:47:48 +0100759
Yuto Takanod93fa372021-08-06 23:05:55 +0100760 for name_match in self.parse_result["mbed_words"]:
Yuto Takano81528c02021-08-06 16:22:06 +0100761 found = name_match.name in all_caps_names
762
763 # Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the
764 # PSA driver, they will not exist as macros. However, they
765 # should still be checked for typos using the equivalent
766 # BUILTINs that exist.
767 if "MBEDTLS_PSA_ACCEL_" in name_match.name:
768 found = name_match.name.replace(
769 "MBEDTLS_PSA_ACCEL_",
770 "MBEDTLS_PSA_BUILTIN_") in all_caps_names
771
Yuto Takanod93fa372021-08-06 23:05:55 +0100772 if not found and not typo_exclusion.search(name_match.name):
Yuto Takanod70d4462021-08-09 12:45:51 +0100773 problems.append(Typo(name_match))
Yuto Takano39639672021-08-05 19:47:48 +0100774
Yuto Takano5473be22021-08-17 10:14:01 +0100775 self.output_check_result("Likely typos", problems)
Yuto Takano81528c02021-08-06 16:22:06 +0100776 return len(problems)
777
Yuto Takano5473be22021-08-17 10:14:01 +0100778 def output_check_result(self, name, problems):
Yuto Takano81528c02021-08-06 16:22:06 +0100779 """
780 Write out the PASS/FAIL status of a performed check depending on whether
781 there were problems.
Yuto Takanod70d4462021-08-09 12:45:51 +0100782
783 Args:
Yuto Takanod70d4462021-08-09 12:45:51 +0100784 * name: the name of the test
785 * problems: a List of encountered Problems
Yuto Takano81528c02021-08-06 16:22:06 +0100786 """
Yuto Takano39639672021-08-05 19:47:48 +0100787 if problems:
Yuto Takano55614b52021-08-07 01:00:18 +0100788 self.log.info("{}: FAIL\n".format(name))
789 for problem in problems:
790 self.log.warning(str(problem))
Darryl Greend5802922018-05-08 15:30:59 +0100791 else:
Yuto Takano81528c02021-08-06 16:22:06 +0100792 self.log.info("{}: PASS".format(name))
Darryl Greend5802922018-05-08 15:30:59 +0100793
Yuto Takano39639672021-08-05 19:47:48 +0100794def main():
795 """
Yuto Takano55c6c872021-08-09 15:35:19 +0100796 Perform argument parsing, and create an instance of CodeParser and
797 NameChecker to begin the core operation.
Yuto Takano39639672021-08-05 19:47:48 +0100798 """
Yuto Takanof005c332021-08-09 13:56:36 +0100799 parser = argparse.ArgumentParser(
Yuto Takano39639672021-08-05 19:47:48 +0100800 formatter_class=argparse.RawDescriptionHelpFormatter,
801 description=(
802 "This script confirms that the naming of all symbols and identifiers "
803 "in Mbed TLS are consistent with the house style and are also "
804 "self-consistent.\n\n"
Yuto Takanof005c332021-08-09 13:56:36 +0100805 "Expected to be run from the MbedTLS root directory.")
806 )
807 parser.add_argument(
808 "-v", "--verbose",
809 action="store_true",
810 help="show parse results"
811 )
812 parser.add_argument(
813 "-q", "--quiet",
814 action="store_true",
815 help="hide unnecessary text, explanations, and highlighs"
816 )
Darryl Greend5802922018-05-08 15:30:59 +0100817
Yuto Takanof005c332021-08-09 13:56:36 +0100818 args = parser.parse_args()
Darryl Greend5802922018-05-08 15:30:59 +0100819
Yuto Takano55c6c872021-08-09 15:35:19 +0100820 # Configure the global logger, which is then passed to the classes below
821 log = logging.getLogger()
822 log.setLevel(logging.DEBUG if args.verbose else logging.INFO)
823 log.addHandler(logging.StreamHandler())
824
Darryl Greend5802922018-05-08 15:30:59 +0100825 try:
Yuto Takano55c6c872021-08-09 15:35:19 +0100826 code_parser = CodeParser(log)
827 parse_result = code_parser.comprehensive_parse()
Yuto Takanod93fa372021-08-06 23:05:55 +0100828 except Exception: # pylint: disable=broad-except
Darryl Greend5802922018-05-08 15:30:59 +0100829 traceback.print_exc()
830 sys.exit(2)
831
Yuto Takano55c6c872021-08-09 15:35:19 +0100832 name_checker = NameChecker(parse_result, log)
833 return_code = name_checker.perform_checks(quiet=args.quiet)
834
835 sys.exit(return_code)
836
Darryl Greend5802922018-05-08 15:30:59 +0100837if __name__ == "__main__":
Yuto Takano39639672021-08-05 19:47:48 +0100838 main()