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 |
| 20 | are consistent with the house style and are also self-consistent. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 21 | """ |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 22 | |
| 23 | import argparse |
| 24 | import textwrap |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 25 | import os |
| 26 | import sys |
| 27 | import traceback |
| 28 | import re |
| 29 | import shutil |
| 30 | import subprocess |
| 31 | import logging |
| 32 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 33 | # Naming patterns to check against |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame^] | 34 | MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$" |
Yuto Takano | c183893 | 2021-08-05 19:52:09 +0100 | [diff] [blame] | 35 | IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$" |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 36 | |
| 37 | class Match(object): |
| 38 | def __init__(self, filename, line, pos, name): |
| 39 | self.filename = filename |
| 40 | self.line = line |
| 41 | self.pos = pos |
| 42 | self.name = name |
| 43 | |
| 44 | def __str__(self): |
| 45 | return self.name |
| 46 | |
| 47 | class Problem(object): |
| 48 | def __init__(self): |
| 49 | self.textwrapper = textwrap.TextWrapper() |
| 50 | self.textwrapper.initial_indent = " * " |
| 51 | self.textwrapper.subsequent_indent = " " |
| 52 | |
| 53 | class SymbolNotInHeader(Problem): |
| 54 | def __init__(self, symbol_name): |
| 55 | self.symbol_name = symbol_name |
| 56 | Problem.__init__(self) |
| 57 | |
| 58 | def __str__(self): |
| 59 | return self.textwrapper.fill( |
| 60 | "'{0}' was found as an available symbol in the output of nm, " |
| 61 | "however it was not declared in any header files." |
| 62 | .format(self.symbol_name)) |
| 63 | |
| 64 | class PatternMismatch(Problem): |
| 65 | def __init__(self, pattern, match): |
| 66 | self.pattern = pattern |
| 67 | self.match = match |
| 68 | Problem.__init__(self) |
| 69 | |
| 70 | def __str__(self): |
| 71 | return self.textwrapper.fill( |
| 72 | "{0}: '{1}' does not match the required pattern '{2}'." |
| 73 | .format(self.match.filename, self.match.name, self.pattern)) |
| 74 | |
| 75 | class Typo(Problem): |
| 76 | def __init__(self, match): |
| 77 | self.match = match |
| 78 | Problem.__init__(self) |
| 79 | |
| 80 | def __str__(self): |
| 81 | return self.textwrapper.fill( |
| 82 | "{0}: '{1}' looks like a typo. It was not found in any macros or " |
| 83 | "any enums. If this is not a typo, put //no-check-names after it." |
| 84 | .format(self.match.filename, self.match.name)) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 85 | |
| 86 | class NameCheck(object): |
| 87 | def __init__(self): |
| 88 | self.log = None |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 89 | self.check_repo_path() |
| 90 | self.return_code = 0 |
| 91 | self.excluded_files = ["compat-1.3.h"] |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 92 | self.typo_check_pattern = r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$" |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 93 | |
| 94 | def set_return_code(self, return_code): |
| 95 | if return_code > self.return_code: |
| 96 | self.return_code = return_code |
| 97 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 98 | def setup_logger(self, verbose=False): |
| 99 | """ |
| 100 | Set up a logger and set the change the default logging level from |
| 101 | WARNING to INFO. Loggers are better than print statements since their |
| 102 | verbosity can be controlled. |
| 103 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 104 | self.log = logging.getLogger() |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 105 | if verbose: |
| 106 | self.log.setLevel(logging.DEBUG) |
| 107 | else: |
| 108 | self.log.setLevel(logging.INFO) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 109 | self.log.addHandler(logging.StreamHandler()) |
| 110 | |
| 111 | def check_repo_path(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 112 | """ |
| 113 | Check that the current working directory is the project root, and throw |
| 114 | an exception if not. |
| 115 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 116 | current_dir = os.path.realpath('.') |
| 117 | root_dir = os.path.dirname(os.path.dirname( |
| 118 | os.path.dirname(os.path.realpath(__file__)))) |
| 119 | if current_dir != root_dir: |
| 120 | raise Exception("Must be run from Mbed TLS root") |
| 121 | |
| 122 | def get_files(self, directory): |
| 123 | filenames = [] |
| 124 | for root, dirs, files in sorted(os.walk(directory)): |
| 125 | for filename in sorted(files): |
| 126 | if (filename not in self.excluded_files and |
| 127 | filename.endswith((".c", ".h"))): |
| 128 | filenames.append(os.path.join(root, filename)) |
| 129 | return filenames |
| 130 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 131 | def parse_macros(self, header_files): |
| 132 | """ |
| 133 | Parse all macros defined by #define preprocessor directives. |
| 134 | |
| 135 | Args: |
| 136 | header_files: A list of filepaths to look through. |
| 137 | |
| 138 | Returns: |
| 139 | A list of Match objects for the macros. |
| 140 | """ |
| 141 | MACRO_REGEX = r"#define (?P<macro>\w+)" |
| 142 | NON_MACROS = ( |
| 143 | "asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_" |
| 144 | ) |
| 145 | |
| 146 | macros = [] |
| 147 | |
| 148 | for header_file in header_files: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 149 | with open(header_file, "r") as header: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 150 | for line in header: |
| 151 | macro = re.search(MACRO_REGEX, line) |
| 152 | if (macro and |
| 153 | not macro.group("macro").startswith(NON_MACROS)): |
| 154 | macros.append(Match( |
| 155 | header_file, |
| 156 | line, |
| 157 | (macro.start(), macro.end()), |
| 158 | macro.group("macro"))) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 159 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 160 | return macros |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 161 | |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame^] | 162 | def parse_MBED_names(self, files): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 163 | """ |
| 164 | Parse all words in the file that begin with MBED. Includes macros. |
| 165 | |
| 166 | Args: |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame^] | 167 | files: A list of filepaths to look through. |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 168 | |
| 169 | Returns: |
| 170 | A list of Match objects for words beginning with MBED. |
| 171 | """ |
| 172 | MBED_names = [] |
| 173 | |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame^] | 174 | for filename in files: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 175 | with open(filename, "r") as fp: |
| 176 | for line in fp: |
| 177 | for name in re.finditer(r"\bMBED.+?_[A-Z0-9_]*", line): |
| 178 | MBED_names.append(Match( |
| 179 | filename, |
| 180 | line, |
| 181 | (name.start(), name.end()), |
| 182 | name.group(0) |
| 183 | )) |
| 184 | |
| 185 | return MBED_names |
| 186 | |
| 187 | def parse_enum_consts(self, header_files): |
| 188 | """ |
| 189 | Parse all enum value constants that are declared. |
| 190 | |
| 191 | Args: |
| 192 | header_files: A list of filepaths to look through. |
| 193 | |
| 194 | Returns: |
| 195 | A list of (enum constants, containing filename). |
| 196 | """ |
| 197 | |
| 198 | enum_consts = [] |
| 199 | |
| 200 | for header_file in header_files: |
| 201 | # Emulate a finite state machine to parse enum declarations. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 202 | state = 0 |
| 203 | with open(header_file, "r") as header: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 204 | for line in header: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 205 | if state is 0 and re.match(r"^(typedef )?enum {", line): |
| 206 | state = 1 |
| 207 | elif state is 0 and re.match(r"^(typedef )?enum", line): |
| 208 | state = 2 |
| 209 | elif state is 2 and re.match(r"^{", line): |
| 210 | state = 1 |
| 211 | elif state is 1 and re.match(r"^}", line): |
| 212 | state = 0 |
| 213 | elif state is 1: |
| 214 | enum_const = re.match(r"^\s*(?P<enum_const>\w+)", line) |
| 215 | if enum_const: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 216 | enum_consts.append(Match( |
| 217 | header_file, |
| 218 | line, |
| 219 | (enum_const.start(), enum_const.end()), |
| 220 | enum_const.group("enum_const"))) |
| 221 | |
| 222 | return enum_consts |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 223 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 224 | def parse_identifiers(self, header_files): |
| 225 | """ |
| 226 | Parse all lines of a header where a function identifier is declared, |
| 227 | based on some huersitics. Assumes every line that is not a comment or a |
| 228 | preprocessor directive contains some identifier. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 229 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 230 | Args: |
| 231 | header_files: A list of filepaths to look through. |
| 232 | |
| 233 | Returns: |
| 234 | A list of (identifier, containing filename) |
| 235 | """ |
| 236 | EXCLUDED_DECLARATIONS = ( |
| 237 | r"^(extern \"C\"|(typedef )?(struct|enum)( {)?$|};?$|$)" |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 238 | ) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 239 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 240 | identifiers = [] |
| 241 | |
| 242 | for header_file in header_files: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 243 | with open(header_file, "r") as header: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 244 | in_block_comment = False |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 245 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 246 | for line in header: |
| 247 | # Skip parsing this line if it begins or ends a block |
| 248 | # comment, and set the state machine's state. |
| 249 | if re.search(r"/\*", line): |
| 250 | in_block_comment = True |
| 251 | continue |
| 252 | elif re.search(r"\*/", line) and in_block_comment: |
| 253 | in_block_comment = False |
| 254 | continue |
| 255 | |
| 256 | # Skip parsing this line if it's a line comment, or if it |
| 257 | # begins with a preprocessor directive |
| 258 | if in_block_comment or re.match(r"(//|#)", line): |
| 259 | continue |
| 260 | |
| 261 | if re.match(EXCLUDED_DECLARATIONS, line): |
| 262 | continue |
| 263 | |
| 264 | identifier = re.search( |
| 265 | # Matches: "mbedtls_aes_init(" |
| 266 | r"([a-zA-Z_][a-zA-Z0-9_]*)\(|" |
| 267 | # Matches: "(*f_rng)(" |
| 268 | r"\(\*(.+)\)\(|" |
| 269 | # TODO: unknown purpose |
| 270 | r"(\w+)\W*$", |
| 271 | line |
| 272 | ) |
| 273 | |
| 274 | if identifier: |
| 275 | for group in identifier.groups(): |
| 276 | if group: |
| 277 | identifiers.append(Match( |
| 278 | header_file, |
| 279 | line, |
| 280 | (identifier.start(), identifier.end()), |
| 281 | identifier.group(0))) |
| 282 | |
| 283 | return identifiers |
| 284 | |
| 285 | def parse_symbols(self): |
| 286 | """ |
| 287 | Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509 |
| 288 | object files using nm to retrieve the list of referenced symbols. |
| 289 | |
| 290 | Returns: |
| 291 | A list of unique symbols defined and used in the libraries. |
| 292 | """ |
| 293 | |
| 294 | symbols = [] |
| 295 | |
| 296 | # Back up the config and atomically compile with the full configratuion. |
| 297 | shutil.copy("include/mbedtls/mbedtls_config.h", |
| 298 | "include/mbedtls/mbedtls_config.h.bak") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 299 | try: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 300 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 301 | ["perl", "scripts/config.pl", "full"], |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 302 | encoding=sys.stdout.encoding, |
| 303 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 304 | ) |
| 305 | my_environment = os.environ.copy() |
| 306 | my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables" |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 307 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 308 | ["make", "clean", "lib"], |
| 309 | env=my_environment, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 310 | encoding=sys.stdout.encoding, |
| 311 | stdout=subprocess.PIPE, |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 312 | stderr=subprocess.STDOUT, |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 313 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 314 | ) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 315 | |
| 316 | # Perform object file analysis using nm |
| 317 | symbols = self.parse_symbols_from_nm( |
| 318 | ["library/libmbedcrypto.a", |
| 319 | "library/libmbedtls.a", |
| 320 | "library/libmbedx509.a"]) |
| 321 | |
| 322 | symbols.sort() |
| 323 | |
| 324 | subprocess.run( |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 325 | ["make", "clean"], |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 326 | encoding=sys.stdout.encoding, |
| 327 | check=True |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 328 | ) |
| 329 | except subprocess.CalledProcessError as error: |
| 330 | self.log.error(error) |
| 331 | self.set_return_code(2) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 332 | finally: |
| 333 | shutil.move("include/mbedtls/mbedtls_config.h.bak", |
| 334 | "include/mbedtls/mbedtls_config.h") |
| 335 | |
| 336 | return symbols |
| 337 | |
| 338 | def parse_symbols_from_nm(self, object_files): |
| 339 | """ |
| 340 | Run nm to retrieve the list of referenced symbols in each object file. |
| 341 | Does not return the position data since it is of no use. |
| 342 | |
| 343 | Returns: |
| 344 | A list of unique symbols defined and used in any of the object files. |
| 345 | """ |
| 346 | UNDEFINED_SYMBOL = r"^\S+: +U |^$|^\S+:$" |
| 347 | VALID_SYMBOL = r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)" |
| 348 | |
| 349 | symbols = [] |
| 350 | |
| 351 | nm_output = "" |
| 352 | for lib in object_files: |
| 353 | nm_output += subprocess.run( |
| 354 | ["nm", "-og", lib], |
| 355 | encoding=sys.stdout.encoding, |
| 356 | stdout=subprocess.PIPE, |
| 357 | stderr=subprocess.STDOUT, |
| 358 | check=True |
| 359 | ).stdout |
| 360 | for line in nm_output.splitlines(): |
| 361 | if not re.match(UNDEFINED_SYMBOL, line): |
| 362 | symbol = re.match(VALID_SYMBOL, line) |
| 363 | if symbol: |
| 364 | symbols.append(symbol.group('symbol')) |
| 365 | else: |
| 366 | self.log.error(line) |
| 367 | |
| 368 | return symbols |
| 369 | |
| 370 | def parse_names_in_source(self): |
| 371 | """ |
| 372 | Calls each parsing function to retrieve various elements of the code, |
| 373 | together with their source location. Puts the parsed values in the |
| 374 | internal variable self.parse_result. |
| 375 | """ |
| 376 | self.log.info("Parsing source code...") |
| 377 | |
| 378 | m_headers = self.get_files(os.path.join("include", "mbedtls")) |
Yuto Takano | c183893 | 2021-08-05 19:52:09 +0100 | [diff] [blame] | 379 | p_headers = self.get_files(os.path.join("include", "psa")) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 380 | libraries = self.get_files("library") |
| 381 | |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame^] | 382 | all_macros = self.parse_macros( |
| 383 | m_headers + p_headers) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 384 | enum_consts = self.parse_enum_consts(m_headers) |
Yuto Takano | c183893 | 2021-08-05 19:52:09 +0100 | [diff] [blame] | 385 | identifiers = self.parse_identifiers(m_headers + p_headers) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 386 | symbols = self.parse_symbols() |
Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame^] | 387 | mbed_names = self.parse_MBED_names(m_headers + p_headers + libraries) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 388 | |
| 389 | # Remove identifier macros like mbedtls_printf or mbedtls_calloc |
| 390 | macros = list(set(all_macros) - set(identifiers)) |
| 391 | |
| 392 | self.log.info("Found:") |
| 393 | self.log.info(" {} Macros".format(len(all_macros))) |
| 394 | self.log.info(" {} Enum Constants".format(len(enum_consts))) |
| 395 | self.log.info(" {} Identifiers".format(len(identifiers))) |
| 396 | self.log.info(" {} Exported Symbols".format(len(symbols))) |
| 397 | self.log.info("Analysing...") |
| 398 | |
| 399 | self.parse_result = { |
| 400 | "macros": macros, |
| 401 | "enum_consts": enum_consts, |
| 402 | "identifiers": identifiers, |
| 403 | "symbols": symbols, |
| 404 | "mbed_names": mbed_names |
| 405 | } |
| 406 | |
| 407 | def perform_checks(self): |
| 408 | """ |
| 409 | Perform each check in order, output its PASS/FAIL status. Maintain an |
| 410 | overall test status, and output that at the end. |
| 411 | """ |
| 412 | problems = 0 |
| 413 | |
| 414 | problems += self.check_symbols_declared_in_header() |
| 415 | |
| 416 | pattern_checks = [ |
| 417 | ("macros", MACRO_PATTERN), |
| 418 | ("enum_consts", MACRO_PATTERN), |
| 419 | ("identifiers", IDENTIFIER_PATTERN)] |
| 420 | for group, check_pattern in pattern_checks: |
| 421 | problems += self.check_match_pattern(group, check_pattern) |
| 422 | |
| 423 | problems += self.check_for_typos() |
| 424 | |
| 425 | self.log.info("=============") |
| 426 | if problems > 0: |
| 427 | self.log.info("FAIL: {0} problem(s) to fix".format(str(problems))) |
| 428 | else: |
| 429 | self.log.info("PASS") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 430 | |
| 431 | def check_symbols_declared_in_header(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 432 | """ |
| 433 | Perform a check that all detected symbols in the library object files |
| 434 | are properly declared in headers. |
| 435 | |
| 436 | Outputs to the logger the PASS/FAIL status, followed by the location of |
| 437 | problems. |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 438 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 439 | Returns the number of problems that needs fixing. |
| 440 | """ |
| 441 | problems = [] |
| 442 | for symbol in self.parse_result["symbols"]: |
| 443 | found_symbol_declared = False |
| 444 | for identifier_match in self.parse_result["identifiers"]: |
| 445 | if symbol == identifier_match.name: |
| 446 | found_symbol_declared = True |
| 447 | break |
| 448 | |
| 449 | if not found_symbol_declared: |
| 450 | problems.append(SymbolNotInHeader(symbol)) |
| 451 | |
| 452 | if problems: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 453 | self.set_return_code(1) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 454 | self.log.info("All symbols in header: FAIL") |
| 455 | for problem in problems: |
| 456 | self.log.info(str(problem) + "\n") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 457 | else: |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 458 | self.log.info("All symbols in header: PASS") |
| 459 | |
| 460 | return len(problems) |
| 461 | |
| 462 | def check_match_pattern(self, group_to_check, check_pattern): |
| 463 | problems = [] |
| 464 | for item_match in self.parse_result[group_to_check]: |
| 465 | if not re.match(check_pattern, item_match.name): |
| 466 | problems.append(PatternMismatch(check_pattern, item_match)) |
| 467 | |
| 468 | if problems: |
| 469 | self.set_return_code(1) |
| 470 | self.log.info("Naming patterns of {}: FAIL".format(group_to_check)) |
| 471 | for problem in problems: |
| 472 | self.log.info(str(problem) + "\n") |
| 473 | else: |
| 474 | self.log.info("Naming patterns of {}: PASS".format(group_to_check)) |
| 475 | |
| 476 | return len(problems) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 477 | |
| 478 | def check_for_typos(self): |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 479 | problems = [] |
| 480 | all_caps_names = list(set([ |
| 481 | match.name for match |
| 482 | in self.parse_result["macros"] + self.parse_result["enum_consts"]] |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 483 | )) |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 484 | |
| 485 | TYPO_EXCLUSION = r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$" |
| 486 | |
| 487 | for name_match in self.parse_result["mbed_names"]: |
| 488 | if name_match.name not in all_caps_names: |
| 489 | if not re.search(TYPO_EXCLUSION, name_match.name): |
| 490 | problems.append(Typo(name_match)) |
| 491 | |
| 492 | if problems: |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 493 | self.set_return_code(1) |
| 494 | self.log.info("Likely typos: FAIL") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 495 | for problem in problems: |
| 496 | self.log.info(str(problem) + "\n") |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 497 | else: |
| 498 | self.log.info("Likely typos: PASS") |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 499 | |
| 500 | return len(problems) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 501 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 502 | def main(): |
| 503 | """ |
| 504 | Main function, parses command-line arguments. |
| 505 | """ |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 506 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 507 | parser = argparse.ArgumentParser( |
| 508 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 509 | description=( |
| 510 | "This script confirms that the naming of all symbols and identifiers " |
| 511 | "in Mbed TLS are consistent with the house style and are also " |
| 512 | "self-consistent.\n\n" |
| 513 | "Expected to be run from the MbedTLS root directory.")) |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 514 | |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 515 | parser.add_argument("-v", "--verbose", |
| 516 | action="store_true", |
| 517 | help="enable script debug outputs") |
| 518 | |
| 519 | args = parser.parse_args() |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 520 | |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 521 | try: |
| 522 | name_check = NameCheck() |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 523 | name_check.setup_logger(verbose=args.verbose) |
| 524 | name_check.parse_names_in_source() |
| 525 | name_check.perform_checks() |
Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 526 | sys.exit(name_check.return_code) |
| 527 | except Exception: |
| 528 | traceback.print_exc() |
| 529 | sys.exit(2) |
| 530 | |
| 531 | |
| 532 | if __name__ == "__main__": |
Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 533 | main() |