Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 1 | #!/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 Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 18 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 19 | This script confirms that the naming of all symbols and identifiers in Mbed TLS |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 20 | are consistent with the house style and are also self-consistent. It performs |
| 21 | the following checks: |
| 22 | |
| 23 | - All exported and available symbols in the library object files, are explicitly |
| 24 | declared in the header files. |
| 25 | - All macros, constants, and identifiers (function names, struct names, etc) |
| 26 | follow the required pattern. |
| 27 | - Typo checking: All words that begin with MBED exist as macros or constants. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 28 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 29 | |
| 30 | import argparse |
| 31 | import textwrap |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 32 | import os |
| 33 | import sys |
| 34 | import traceback |
| 35 | import re |
| 36 | import shutil |
| 37 | import subprocess |
| 38 | import logging |
| 39 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 40 | # Naming patterns to check against. These are defined outside the NameCheck |
| 41 | # class for ease of modification. |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame] | 42 | MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$" |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 43 | CONSTANTS_PATTERN = MACRO_PATTERN |
Yuto Takano | c183893 | 2021-08-05 19:52:09 +0100 | [diff] [blame] | 44 | IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$" |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 45 | |
| 46 | class Match(object): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 47 | """ |
| 48 | A class representing a match, together with its found position. |
| 49 | |
| 50 | Fields: |
| 51 | * filename: the file that the match was in. |
| 52 | * line: the full line containing the match. |
| 53 | * pos: a tuple of (start, end) positions on the line where the match is. |
| 54 | * name: the match itself. |
| 55 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 56 | def __init__(self, filename, line, pos, name): |
| 57 | self.filename = filename |
| 58 | self.line = line |
| 59 | self.pos = pos |
| 60 | self.name = name |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 61 | |
| 62 | class Problem(object): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 63 | """ |
| 64 | A parent class representing a form of static analysis error. |
| 65 | |
| 66 | Fields: |
| 67 | * textwrapper: a TextWrapper instance to format problems nicely. |
| 68 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 69 | def __init__(self): |
| 70 | self.textwrapper = textwrap.TextWrapper() |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 71 | self.textwrapper.width = 80 |
| 72 | self.textwrapper.initial_indent = " * " |
| 73 | self.textwrapper.subsequent_indent = " " |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 74 | |
| 75 | class SymbolNotInHeader(Problem): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 76 | """ |
| 77 | A problem that occurs when an exported/available symbol in the object file |
| 78 | is not explicitly declared in header files. Created with |
| 79 | NameCheck.check_symbols_declared_in_header() |
| 80 | |
| 81 | Fields: |
| 82 | * symbol_name: the name of the symbol. |
| 83 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 84 | def __init__(self, symbol_name): |
| 85 | self.symbol_name = symbol_name |
| 86 | Problem.__init__(self) |
| 87 | |
| 88 | def __str__(self): |
| 89 | return self.textwrapper.fill( |
| 90 | "'{0}' was found as an available symbol in the output of nm, " |
| 91 | "however it was not declared in any header files." |
| 92 | .format(self.symbol_name)) |
| 93 | |
| 94 | class PatternMismatch(Problem): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 95 | """ |
| 96 | A problem that occurs when something doesn't match the expected pattern. |
| 97 | Created with NameCheck.check_match_pattern() |
| 98 | |
| 99 | Fields: |
| 100 | * pattern: the expected regex pattern |
| 101 | * match: the Match object in question |
| 102 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 103 | def __init__(self, pattern, match): |
| 104 | self.pattern = pattern |
| 105 | self.match = match |
| 106 | Problem.__init__(self) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 107 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 108 | def __str__(self): |
| 109 | return self.textwrapper.fill( |
| 110 | "{0}: '{1}' does not match the required pattern '{2}'." |
| 111 | .format(self.match.filename, self.match.name, self.pattern)) |
| 112 | |
| 113 | class Typo(Problem): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 114 | """ |
| 115 | A problem that occurs when a word using MBED doesn't appear to be defined as |
| 116 | constants nor enum values. Created with NameCheck.check_for_typos() |
| 117 | |
| 118 | Fields: |
| 119 | * match: the Match object of the MBED name in question. |
| 120 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 121 | def __init__(self, match): |
| 122 | self.match = match |
| 123 | Problem.__init__(self) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 124 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 125 | def __str__(self): |
| 126 | return self.textwrapper.fill( |
| 127 | "{0}: '{1}' looks like a typo. It was not found in any macros or " |
| 128 | "any enums. If this is not a typo, put //no-check-names after it." |
| 129 | .format(self.match.filename, self.match.name)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 130 | |
| 131 | class NameCheck(object): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 132 | """ |
| 133 | Representation of the core name checking operation performed by this script. |
| 134 | Shares a common logger, common excluded filenames, and a shared return_code. |
| 135 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 136 | def __init__(self): |
| 137 | self.log = None |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 138 | self.check_repo_path() |
| 139 | self.return_code = 0 |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 140 | self.excluded_files = ["bn_mul", "compat-2.x.h"] |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 141 | |
| 142 | def set_return_code(self, return_code): |
| 143 | if return_code > self.return_code: |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame^] | 144 | self.log.debug("Setting new return code to {}".format(return_code)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 145 | self.return_code = return_code |
| 146 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 147 | def setup_logger(self, verbose=False): |
| 148 | """ |
| 149 | Set up a logger and set the change the default logging level from |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 150 | WARNING to INFO. Loggers are better than print statements since their |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 151 | verbosity can be controlled. |
| 152 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 153 | self.log = logging.getLogger() |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 154 | if verbose: |
| 155 | self.log.setLevel(logging.DEBUG) |
| 156 | else: |
| 157 | self.log.setLevel(logging.INFO) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 158 | self.log.addHandler(logging.StreamHandler()) |
| 159 | |
| 160 | def check_repo_path(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 161 | """ |
| 162 | Check that the current working directory is the project root, and throw |
| 163 | an exception if not. |
| 164 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 165 | current_dir = os.path.realpath('.') |
| 166 | root_dir = os.path.dirname(os.path.dirname( |
| 167 | os.path.dirname(os.path.realpath(__file__)))) |
| 168 | if current_dir != root_dir: |
| 169 | raise Exception("Must be run from Mbed TLS root") |
| 170 | |
Yuto Takano | 157444c | 2021-08-05 20:10:45 +0100 | [diff] [blame] | 171 | def get_files(self, extension, directory): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 172 | """ |
| 173 | Get all files that end with .extension in the specified directory |
| 174 | recursively. |
| 175 | |
| 176 | Args: |
| 177 | * extension: the file extension to search for, without the dot |
| 178 | * directory: the directory to recursively search for |
| 179 | |
| 180 | Returns a List of relative filepaths. |
| 181 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 182 | filenames = [] |
| 183 | for root, dirs, files in sorted(os.walk(directory)): |
| 184 | for filename in sorted(files): |
| 185 | if (filename not in self.excluded_files and |
Yuto Takano | 157444c | 2021-08-05 20:10:45 +0100 | [diff] [blame] | 186 | filename.endswith("." + extension)): |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 187 | filenames.append(os.path.join(root, filename)) |
| 188 | return filenames |
| 189 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 190 | def parse_names_in_source(self): |
| 191 | """ |
| 192 | Calls each parsing function to retrieve various elements of the code, |
| 193 | together with their source location. Puts the parsed values in the |
| 194 | internal variable self.parse_result. |
| 195 | """ |
| 196 | self.log.info("Parsing source code...") |
| 197 | |
| 198 | m_headers = self.get_files("h", os.path.join("include", "mbedtls")) |
| 199 | p_headers = self.get_files("h", os.path.join("include", "psa")) |
| 200 | t_headers = ["3rdparty/everest/include/everest/everest.h", |
| 201 | "3rdparty/everest/include/everest/x25519.h"] |
| 202 | d_headers = self.get_files("h", os.path.join("tests", "include", "test", "drivers")) |
| 203 | l_headers = self.get_files("h", "library") |
| 204 | libraries = self.get_files("c", "library") + [ |
| 205 | "3rdparty/everest/library/everest.c", |
| 206 | "3rdparty/everest/library/x25519.c"] |
| 207 | |
| 208 | all_macros = self.parse_macros( |
| 209 | m_headers + p_headers + t_headers + l_headers + d_headers) |
| 210 | enum_consts = self.parse_enum_consts( |
| 211 | m_headers + l_headers + t_headers) |
| 212 | identifiers = self.parse_identifiers( |
| 213 | m_headers + p_headers + t_headers + l_headers) |
| 214 | mbed_names = self.parse_MBED_names( |
| 215 | m_headers + p_headers + t_headers + l_headers + libraries) |
| 216 | symbols = self.parse_symbols() |
| 217 | |
| 218 | # Remove identifier macros like mbedtls_printf or mbedtls_calloc |
| 219 | identifiers_justname = [x.name for x in identifiers] |
| 220 | actual_macros = [] |
| 221 | for macro in all_macros: |
| 222 | if macro.name not in identifiers_justname: |
| 223 | actual_macros.append(macro) |
| 224 | |
| 225 | self.log.debug("Found:") |
| 226 | self.log.debug(" {} Macros".format(len(all_macros))) |
| 227 | self.log.debug(" {} Non-identifier Macros".format(len(actual_macros))) |
| 228 | self.log.debug(" {} Enum Constants".format(len(enum_consts))) |
| 229 | self.log.debug(" {} Identifiers".format(len(identifiers))) |
| 230 | self.log.debug(" {} Exported Symbols".format(len(symbols))) |
| 231 | self.log.info("Analysing...") |
| 232 | |
| 233 | self.parse_result = { |
| 234 | "macros": actual_macros, |
| 235 | "enum_consts": enum_consts, |
| 236 | "identifiers": identifiers, |
| 237 | "symbols": symbols, |
| 238 | "mbed_names": mbed_names |
| 239 | } |
| 240 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 241 | def parse_macros(self, header_files): |
| 242 | """ |
| 243 | Parse all macros defined by #define preprocessor directives. |
| 244 | |
| 245 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 246 | * header_files: A List of filepaths to look through. |
| 247 | |
| 248 | Returns a List of Match objects for the found macros. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 249 | """ |
| 250 | MACRO_REGEX = r"#define (?P<macro>\w+)" |
| 251 | NON_MACROS = ( |
| 252 | "asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_" |
| 253 | ) |
| 254 | |
| 255 | macros = [] |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame^] | 256 | self.log.debug("Looking for macros in {} files".format(len(header_files))) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 257 | for header_file in header_files: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 258 | with open(header_file, "r") as header: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 259 | for line in header: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 260 | for macro in re.finditer(MACRO_REGEX, line): |
| 261 | if not macro.group("macro").startswith(NON_MACROS): |
| 262 | macros.append(Match( |
| 263 | header_file, |
| 264 | line, |
| 265 | (macro.start(), macro.end()), |
| 266 | macro.group("macro"))) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 267 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 268 | return macros |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 269 | |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame] | 270 | def parse_MBED_names(self, files): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 271 | """ |
| 272 | Parse all words in the file that begin with MBED. Includes macros. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 273 | There have been typos of TLS, hence the broader check than MBEDTLS. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 274 | |
| 275 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 276 | * files: a List of filepaths to look through. |
| 277 | |
| 278 | Returns a List of Match objects for words beginning with MBED. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 279 | """ |
| 280 | MBED_names = [] |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame^] | 281 | self.log.debug("Looking for MBED names in {} files".format(len(files))) |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame] | 282 | for filename in files: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 283 | with open(filename, "r") as fp: |
| 284 | for line in fp: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 285 | # Ignore any names that are deliberately opted-out or in |
| 286 | # legacy error directives |
| 287 | if re.search(r"// *no-check-names|#error", line): |
Yuto Takano | c62b408 | 2021-08-05 20:17:07 +0100 | [diff] [blame] | 288 | continue |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 289 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 290 | for name in re.finditer(r"\bMBED.+?_[A-Z0-9_]*", line): |
| 291 | MBED_names.append(Match( |
| 292 | filename, |
| 293 | line, |
| 294 | (name.start(), name.end()), |
| 295 | name.group(0) |
| 296 | )) |
| 297 | |
| 298 | return MBED_names |
| 299 | |
| 300 | def parse_enum_consts(self, header_files): |
| 301 | """ |
| 302 | Parse all enum value constants that are declared. |
| 303 | |
| 304 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 305 | * header_files: A List of filepaths to look through. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 306 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 307 | Returns a List of Match objects for the findings. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 308 | """ |
| 309 | |
| 310 | enum_consts = [] |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame^] | 311 | self.log.debug("Looking for enum consts in {} files".format(len(header_files))) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 312 | for header_file in header_files: |
| 313 | # Emulate a finite state machine to parse enum declarations. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 314 | # 0 = not in enum |
| 315 | # 1 = inside enum |
| 316 | # 2 = almost inside enum |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 317 | state = 0 |
| 318 | with open(header_file, "r") as header: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 319 | for line in header: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 320 | if state is 0 and re.match(r"^(typedef )?enum {", line): |
| 321 | state = 1 |
| 322 | elif state is 0 and re.match(r"^(typedef )?enum", line): |
| 323 | state = 2 |
| 324 | elif state is 2 and re.match(r"^{", line): |
| 325 | state = 1 |
| 326 | elif state is 1 and re.match(r"^}", line): |
| 327 | state = 0 |
Yuto Takano | 0fd48f7 | 2021-08-05 20:32:55 +0100 | [diff] [blame] | 328 | elif state is 1 and not re.match(r"^#", line): |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 329 | enum_const = re.match(r"^\s*(?P<enum_const>\w+)", line) |
| 330 | if enum_const: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 331 | enum_consts.append(Match( |
| 332 | header_file, |
| 333 | line, |
| 334 | (enum_const.start(), enum_const.end()), |
| 335 | enum_const.group("enum_const"))) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 336 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 337 | return enum_consts |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 338 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 339 | def parse_identifiers(self, header_files): |
| 340 | """ |
| 341 | Parse all lines of a header where a function identifier is declared, |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 342 | based on some huersitics. Highly dependent on formatting style. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 343 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 344 | Args: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 345 | * header_files: A List of filepaths to look through. |
| 346 | |
| 347 | Returns a List of Match objects with identifiers. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 348 | """ |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 349 | EXCLUDED_LINES = ( |
| 350 | r"^(" |
| 351 | r"extern \"C\"|" |
| 352 | r"(typedef )?(struct|union|enum)( {)?$|" |
| 353 | r"};?$|" |
| 354 | r"$|" |
| 355 | r"//|" |
| 356 | r"#" |
| 357 | r")" |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 358 | ) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 359 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 360 | identifiers = [] |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame^] | 361 | self.log.debug("Looking for identifiers in {} files".format(len(header_files))) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 362 | for header_file in header_files: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 363 | with open(header_file, "r") as header: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 364 | in_block_comment = False |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 365 | previous_line = None |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 366 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 367 | for line in header: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 368 | # Skip parsing this line if a block comment ends on it, |
| 369 | # but don't skip if it has just started -- there is a chance |
| 370 | # it ends on the same line. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 371 | if re.search(r"/\*", line): |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 372 | in_block_comment = not in_block_comment |
| 373 | if re.search(r"\*/", line): |
| 374 | in_block_comment = not in_block_comment |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 375 | continue |
| 376 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 377 | if in_block_comment: |
| 378 | previous_line = None |
| 379 | continue |
| 380 | |
| 381 | if re.match(EXCLUDED_LINES, line): |
| 382 | previous_line = None |
| 383 | continue |
| 384 | |
| 385 | # Match "^something something$", with optional inline/static |
| 386 | # This *might* be a function with its argument brackets on |
| 387 | # the next line, or a struct declaration, so keep note of it |
| 388 | if re.match( |
| 389 | r"(inline |static |typedef )*\w+ \w+$", |
| 390 | line): |
| 391 | previous_line = line |
| 392 | continue |
| 393 | |
| 394 | # If previous line seemed to start an unfinished declaration |
| 395 | # (as above), and this line begins with a bracket, concat |
| 396 | # them and treat them as one line. |
| 397 | if previous_line and re.match(" *[\({]", line): |
| 398 | line = previous_line.strip() + line.strip() |
| 399 | previous_line = None |
| 400 | |
| 401 | # Skip parsing if line has a space in front = hueristic to |
| 402 | # skip function argument lines (highly subject to formatting |
| 403 | # changes) |
| 404 | if line[0] == " ": |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 405 | continue |
Yuto Takano | 6f38ab3 | 2021-08-05 21:07:14 +0100 | [diff] [blame] | 406 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 407 | identifier = re.search( |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 408 | # Match something( |
| 409 | r".* \**(\w+)\(|" |
| 410 | # Match (*something)( |
| 411 | r".*\( *\* *(\w+) *\) *\(|" |
| 412 | # Match names of named data structures |
| 413 | r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$|" |
| 414 | # Match names of typedef instances, after closing bracket |
| 415 | r"}? *(\w+)[;[].*", |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 416 | line |
| 417 | ) |
| 418 | |
| 419 | if identifier: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 420 | # Find the group that matched, and append it |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 421 | for group in identifier.groups(): |
| 422 | if group: |
| 423 | identifiers.append(Match( |
| 424 | header_file, |
| 425 | line, |
| 426 | (identifier.start(), identifier.end()), |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 427 | group)) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 428 | |
| 429 | return identifiers |
| 430 | |
| 431 | def parse_symbols(self): |
| 432 | """ |
| 433 | Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509 |
| 434 | object files using nm to retrieve the list of referenced symbols. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 435 | Exceptions thrown here are rethrown because they would be critical |
| 436 | errors that void several tests, and thus needs to halt the program. This |
| 437 | is explicitly done for clarity. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 438 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 439 | Returns a List of unique symbols defined and used in the libraries. |
| 440 | """ |
| 441 | self.log.info("Compiling...") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 442 | symbols = [] |
| 443 | |
| 444 | # Back up the config and atomically compile with the full configratuion. |
| 445 | shutil.copy("include/mbedtls/mbedtls_config.h", |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 446 | "include/mbedtls/mbedtls_config.h.bak") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 447 | try: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 448 | # Use check=True in all subprocess calls so that failures are raised |
| 449 | # as exceptions and logged. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 450 | subprocess.run( |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 451 | ["python3", "scripts/config.py", "full"], |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 452 | encoding=sys.stdout.encoding, |
| 453 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 454 | ) |
| 455 | my_environment = os.environ.copy() |
| 456 | my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables" |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 457 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 458 | ["make", "clean", "lib"], |
| 459 | env=my_environment, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 460 | encoding=sys.stdout.encoding, |
| 461 | stdout=subprocess.PIPE, |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 462 | stderr=subprocess.STDOUT, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 463 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 464 | ) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 465 | |
| 466 | # Perform object file analysis using nm |
| 467 | symbols = self.parse_symbols_from_nm( |
| 468 | ["library/libmbedcrypto.a", |
| 469 | "library/libmbedtls.a", |
| 470 | "library/libmbedx509.a"]) |
| 471 | |
| 472 | symbols.sort() |
| 473 | |
| 474 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 475 | ["make", "clean"], |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 476 | encoding=sys.stdout.encoding, |
| 477 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 478 | ) |
| 479 | except subprocess.CalledProcessError as error: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 480 | self.set_return_code(2) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 481 | raise error |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 482 | finally: |
| 483 | shutil.move("include/mbedtls/mbedtls_config.h.bak", |
| 484 | "include/mbedtls/mbedtls_config.h") |
| 485 | |
| 486 | return symbols |
| 487 | |
| 488 | def parse_symbols_from_nm(self, object_files): |
| 489 | """ |
| 490 | Run nm to retrieve the list of referenced symbols in each object file. |
| 491 | Does not return the position data since it is of no use. |
| 492 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 493 | Args: |
| 494 | * object_files: a List of compiled object files to search through. |
| 495 | |
| 496 | Returns a List of unique symbols defined and used in any of the object |
| 497 | files. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 498 | """ |
| 499 | UNDEFINED_SYMBOL = r"^\S+: +U |^$|^\S+:$" |
| 500 | VALID_SYMBOL = r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)" |
Yuto Takano | e77f699 | 2021-08-05 20:22:59 +0100 | [diff] [blame] | 501 | EXCLUSIONS = ("FStar", "Hacl") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 502 | |
| 503 | symbols = [] |
| 504 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 505 | # Gather all outputs of nm |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 506 | nm_output = "" |
| 507 | for lib in object_files: |
| 508 | nm_output += subprocess.run( |
| 509 | ["nm", "-og", lib], |
| 510 | encoding=sys.stdout.encoding, |
| 511 | stdout=subprocess.PIPE, |
| 512 | stderr=subprocess.STDOUT, |
| 513 | check=True |
| 514 | ).stdout |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 515 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 516 | for line in nm_output.splitlines(): |
| 517 | if not re.match(UNDEFINED_SYMBOL, line): |
| 518 | symbol = re.match(VALID_SYMBOL, line) |
Yuto Takano | e77f699 | 2021-08-05 20:22:59 +0100 | [diff] [blame] | 519 | if symbol and not symbol.group("symbol").startswith(EXCLUSIONS): |
| 520 | symbols.append(symbol.group("symbol")) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 521 | else: |
| 522 | self.log.error(line) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 523 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 524 | return symbols |
| 525 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 526 | def perform_checks(self, show_problems: True): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 527 | """ |
| 528 | Perform each check in order, output its PASS/FAIL status. Maintain an |
| 529 | overall test status, and output that at the end. |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 530 | |
| 531 | Args: |
| 532 | * show_problems: whether to show the problematic examples. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 533 | """ |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 534 | self.log.info("=============") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 535 | problems = 0 |
| 536 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 537 | problems += self.check_symbols_declared_in_header(show_problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 538 | |
| 539 | pattern_checks = [ |
| 540 | ("macros", MACRO_PATTERN), |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 541 | ("enum_consts", CONSTANTS_PATTERN), |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 542 | ("identifiers", IDENTIFIER_PATTERN)] |
| 543 | for group, check_pattern in pattern_checks: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 544 | problems += self.check_match_pattern( |
| 545 | show_problems, group, check_pattern) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 546 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 547 | problems += self.check_for_typos(show_problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 548 | |
| 549 | self.log.info("=============") |
| 550 | if problems > 0: |
| 551 | self.log.info("FAIL: {0} problem(s) to fix".format(str(problems))) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 552 | if not show_problems: |
| 553 | self.log.info("Remove --quiet to show the problems.") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 554 | else: |
| 555 | self.log.info("PASS") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 556 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 557 | def check_symbols_declared_in_header(self, show_problems): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 558 | """ |
| 559 | Perform a check that all detected symbols in the library object files |
| 560 | are properly declared in headers. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 561 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 562 | Args: |
| 563 | * show_problems: whether to show the problematic examples. |
| 564 | |
| 565 | Returns the number of problems that need fixing. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 566 | """ |
| 567 | problems = [] |
| 568 | for symbol in self.parse_result["symbols"]: |
| 569 | found_symbol_declared = False |
| 570 | for identifier_match in self.parse_result["identifiers"]: |
| 571 | if symbol == identifier_match.name: |
| 572 | found_symbol_declared = True |
| 573 | break |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 574 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 575 | if not found_symbol_declared: |
| 576 | problems.append(SymbolNotInHeader(symbol)) |
| 577 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 578 | self.output_check_result("All symbols in header", problems, show_problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 579 | return len(problems) |
| 580 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 581 | |
| 582 | def check_match_pattern(self, show_problems, group_to_check, check_pattern): |
| 583 | """ |
| 584 | Perform a check that all items of a group conform to a regex pattern. |
| 585 | |
| 586 | Args: |
| 587 | * show_problems: whether to show the problematic examples. |
| 588 | * group_to_check: string key to index into self.parse_result. |
| 589 | * check_pattern: the regex to check against. |
| 590 | |
| 591 | Returns the number of problems that need fixing. |
| 592 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 593 | problems = [] |
| 594 | for item_match in self.parse_result[group_to_check]: |
| 595 | if not re.match(check_pattern, item_match.name): |
| 596 | problems.append(PatternMismatch(check_pattern, item_match)) |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame^] | 597 | # Double underscore is a reserved identifier, never to be used |
Yuto Takano | c763cc3 | 2021-08-05 20:06:34 +0100 | [diff] [blame] | 598 | if re.match(r".*__.*", item_match.name): |
| 599 | problems.append(PatternMismatch("double underscore", item_match)) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 600 | |
| 601 | self.output_check_result( |
| 602 | "Naming patterns of {}".format(group_to_check), |
| 603 | problems, |
| 604 | show_problems) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 605 | return len(problems) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 606 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 607 | def check_for_typos(self, show_problems): |
| 608 | """ |
| 609 | Perform a check that all words in the soure code beginning with MBED are |
| 610 | either defined as macros, or as enum constants. |
| 611 | |
| 612 | Args: |
| 613 | * show_problems: whether to show the problematic examples. |
| 614 | |
| 615 | Returns the number of problems that need fixing. |
| 616 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 617 | problems = [] |
| 618 | all_caps_names = list(set([ |
| 619 | match.name for match |
| 620 | in self.parse_result["macros"] + self.parse_result["enum_consts"]] |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 621 | )) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 622 | |
| 623 | TYPO_EXCLUSION = r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$" |
| 624 | |
| 625 | for name_match in self.parse_result["mbed_names"]: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 626 | found = name_match.name in all_caps_names |
| 627 | |
| 628 | # Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the |
| 629 | # PSA driver, they will not exist as macros. However, they |
| 630 | # should still be checked for typos using the equivalent |
| 631 | # BUILTINs that exist. |
| 632 | if "MBEDTLS_PSA_ACCEL_" in name_match.name: |
| 633 | found = name_match.name.replace( |
| 634 | "MBEDTLS_PSA_ACCEL_", |
| 635 | "MBEDTLS_PSA_BUILTIN_") in all_caps_names |
| 636 | |
| 637 | if not found and not re.search(TYPO_EXCLUSION, name_match.name): |
Yuto Takano | 201f9e8 | 2021-08-06 16:36:54 +0100 | [diff] [blame^] | 638 | problems.append(Typo(name_match)) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 639 | |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 640 | self.output_check_result("Likely typos", problems, show_problems) |
| 641 | return len(problems) |
| 642 | |
| 643 | def output_check_result(self, name, problems, show_problems): |
| 644 | """ |
| 645 | Write out the PASS/FAIL status of a performed check depending on whether |
| 646 | there were problems. |
| 647 | |
| 648 | Args: |
| 649 | * show_problems: whether to show the problematic examples. |
| 650 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 651 | if problems: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 652 | self.set_return_code(1) |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 653 | self.log.info("{}: FAIL".format(name)) |
| 654 | if show_problems: |
| 655 | self.log.info("") |
| 656 | for problem in problems: |
| 657 | self.log.warn(str(problem) + "\n") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 658 | else: |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 659 | self.log.info("{}: PASS".format(name)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 660 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 661 | def main(): |
| 662 | """ |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 663 | Perform argument parsing, and create an instance of NameCheck to begin the |
| 664 | core operation. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 665 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 666 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 667 | parser = argparse.ArgumentParser( |
| 668 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 669 | description=( |
| 670 | "This script confirms that the naming of all symbols and identifiers " |
| 671 | "in Mbed TLS are consistent with the house style and are also " |
| 672 | "self-consistent.\n\n" |
| 673 | "Expected to be run from the MbedTLS root directory.")) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 674 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 675 | parser.add_argument("-v", "--verbose", |
| 676 | action="store_true", |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 677 | help="show parse results") |
| 678 | |
| 679 | parser.add_argument("-q", "--quiet", |
| 680 | action="store_true", |
| 681 | help="hide unnecessary text and problematic examples") |
| 682 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 683 | args = parser.parse_args() |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 684 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 685 | try: |
| 686 | name_check = NameCheck() |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 687 | name_check.setup_logger(verbose=args.verbose) |
| 688 | name_check.parse_names_in_source() |
Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 689 | name_check.perform_checks(show_problems=not args.quiet) |
| 690 | sys.exit(name_check.return_code) |
| 691 | except subprocess.CalledProcessError as error: |
| 692 | traceback.print_exc() |
| 693 | print("!! Compilation faced a critical error, " |
| 694 | "check-names can't continue further.") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 695 | sys.exit(name_check.return_code) |
| 696 | except Exception: |
| 697 | traceback.print_exc() |
| 698 | sys.exit(2) |
| 699 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 700 | if __name__ == "__main__": |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 701 | main() |