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