| 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 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 23 | It contains two major Python classes, CodeParser and NameChecker. They both have | 
|  | 24 | a comprehensive "run-all" function (comprehensive_parse() and perform_checks()) | 
|  | 25 | but the individual functions can also be used for specific needs. | 
|  | 26 |  | 
|  | 27 | CodeParser makes heavy use of regular expressions to parse the code, and is | 
|  | 28 | dependent on the current code formatting. Many Python C parser libraries require | 
|  | 29 | preprocessed C code, which means no macro parsing. Compiler tools are also not | 
|  | 30 | very helpful when we want the exact location in the original source (which | 
|  | 31 | becomes impossible when e.g. comments are stripped). | 
|  | 32 |  | 
|  | 33 | NameChecker performs the following checks: | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 34 |  | 
|  | 35 | - All exported and available symbols in the library object files, are explicitly | 
| Yuto Takano | 159255a | 2021-08-06 17:00:28 +0100 | [diff] [blame] | 36 | declared in the header files. This uses the nm command. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 37 | - All macros, constants, and identifiers (function names, struct names, etc) | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 38 | follow the required regex pattern. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 39 | - Typo checking: All words that begin with MBED exist as macros or constants. | 
| Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 40 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 41 | The script returns 0 on success, 1 on test failure, and 2 if there is a script | 
| Yuto Takano | 8246eb8 | 2021-08-16 10:37:24 +0100 | [diff] [blame] | 42 | error. It must be run from Mbed TLS root. | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 43 | """ | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 44 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 45 | import abc | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 46 | import argparse | 
| Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 47 | import glob | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 48 | import textwrap | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 49 | import os | 
|  | 50 | import sys | 
|  | 51 | import traceback | 
|  | 52 | import re | 
| Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 53 | import enum | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 54 | import shutil | 
|  | 55 | import subprocess | 
|  | 56 | import logging | 
|  | 57 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 58 | # Naming patterns to check against. These are defined outside the NameCheck | 
|  | 59 | # class for ease of modification. | 
| Yuto Takano | bb7dca4 | 2021-08-05 19:57:58 +0100 | [diff] [blame] | 60 | MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$" | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 61 | CONSTANTS_PATTERN = MACRO_PATTERN | 
| Yuto Takano | c183893 | 2021-08-05 19:52:09 +0100 | [diff] [blame] | 62 | IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$" | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 63 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 64 | class Match(): # pylint: disable=too-few-public-methods | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 65 | """ | 
|  | 66 | A class representing a match, together with its found position. | 
|  | 67 |  | 
|  | 68 | Fields: | 
|  | 69 | * filename: the file that the match was in. | 
|  | 70 | * line: the full line containing the match. | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 71 | * line_no: the line number. | 
|  | 72 | * pos: a tuple of (start, end) positions on the line where the match is. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 73 | * name: the match itself. | 
|  | 74 | """ | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 75 | def __init__(self, filename, line, line_no, pos, name): | 
|  | 76 | # pylint: disable=too-many-arguments | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 77 | self.filename = filename | 
|  | 78 | self.line = line | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 79 | self.line_no = line_no | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 80 | self.pos = pos | 
|  | 81 | self.name = name | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 82 |  | 
| Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 83 | def __str__(self): | 
| Yuto Takano | fb86ac7 | 2021-08-16 10:32:40 +0100 | [diff] [blame] | 84 | """ | 
|  | 85 | Return a formatted code listing representation of the erroneous line. | 
|  | 86 | """ | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 87 | gutter = format(self.line_no, "4d") | 
|  | 88 | underline = self.pos[0] * " " + (self.pos[1] - self.pos[0]) * "^" | 
| Yuto Takano | 381fda8 | 2021-08-06 23:37:20 +0100 | [diff] [blame] | 89 |  | 
| Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 90 | return ( | 
| Yuto Takano | fb86ac7 | 2021-08-16 10:32:40 +0100 | [diff] [blame] | 91 | " {0} |\n".format(" " * len(gutter)) + | 
| Yuto Takano | 381fda8 | 2021-08-06 23:37:20 +0100 | [diff] [blame] | 92 | " {0} | {1}".format(gutter, self.line) + | 
| Yuto Takano | fb86ac7 | 2021-08-16 10:32:40 +0100 | [diff] [blame] | 93 | " {0} | {1}\n".format(" " * len(gutter), underline) | 
| Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 94 | ) | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 95 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 96 | class Problem(abc.ABC): # pylint: disable=too-few-public-methods | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 97 | """ | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 98 | An abstract parent class representing a form of static analysis error. | 
|  | 99 | It extends an Abstract Base Class, which means it is not instantiable, and | 
|  | 100 | it also mandates certain abstract methods to be implemented in subclasses. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 101 | """ | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 102 | # Class variable to control the quietness of all problems | 
|  | 103 | quiet = False | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 104 | def __init__(self): | 
|  | 105 | self.textwrapper = textwrap.TextWrapper() | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 106 | self.textwrapper.width = 80 | 
| Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 107 | self.textwrapper.initial_indent = "    > " | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 108 | self.textwrapper.subsequent_indent = "      " | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 109 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 110 | def __str__(self): | 
|  | 111 | """ | 
|  | 112 | Unified string representation method for all Problems. | 
|  | 113 | """ | 
|  | 114 | if self.__class__.quiet: | 
|  | 115 | return self.quiet_output() | 
|  | 116 | return self.verbose_output() | 
|  | 117 |  | 
|  | 118 | @abc.abstractmethod | 
|  | 119 | def quiet_output(self): | 
|  | 120 | """ | 
|  | 121 | The output when --quiet is enabled. | 
|  | 122 | """ | 
|  | 123 | pass | 
|  | 124 |  | 
|  | 125 | @abc.abstractmethod | 
|  | 126 | def verbose_output(self): | 
|  | 127 | """ | 
|  | 128 | The default output with explanation and code snippet if appropriate. | 
|  | 129 | """ | 
|  | 130 | pass | 
|  | 131 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 132 | class SymbolNotInHeader(Problem): # pylint: disable=too-few-public-methods | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 133 | """ | 
|  | 134 | A problem that occurs when an exported/available symbol in the object file | 
|  | 135 | is not explicitly declared in header files. Created with | 
|  | 136 | NameCheck.check_symbols_declared_in_header() | 
|  | 137 |  | 
|  | 138 | Fields: | 
|  | 139 | * symbol_name: the name of the symbol. | 
|  | 140 | """ | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 141 | def __init__(self, symbol_name): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 142 | self.symbol_name = symbol_name | 
|  | 143 | Problem.__init__(self) | 
|  | 144 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 145 | def quiet_output(self): | 
|  | 146 | return "{0}".format(self.symbol_name) | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 147 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 148 | def verbose_output(self): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 149 | return self.textwrapper.fill( | 
|  | 150 | "'{0}' was found as an available symbol in the output of nm, " | 
|  | 151 | "however it was not declared in any header files." | 
|  | 152 | .format(self.symbol_name)) | 
|  | 153 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 154 | class PatternMismatch(Problem): # pylint: disable=too-few-public-methods | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 155 | """ | 
|  | 156 | A problem that occurs when something doesn't match the expected pattern. | 
|  | 157 | Created with NameCheck.check_match_pattern() | 
|  | 158 |  | 
|  | 159 | Fields: | 
|  | 160 | * pattern: the expected regex pattern | 
|  | 161 | * match: the Match object in question | 
|  | 162 | """ | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 163 | def __init__(self, pattern, match): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 164 | self.pattern = pattern | 
|  | 165 | self.match = match | 
|  | 166 | Problem.__init__(self) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 167 |  | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 168 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 169 | def quiet_output(self): | 
|  | 170 | return ( | 
|  | 171 | "{0}:{1}:{2}" | 
|  | 172 | .format(self.match.filename, self.match.line_no, self.match.name) | 
|  | 173 | ) | 
|  | 174 |  | 
|  | 175 | def verbose_output(self): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 176 | return self.textwrapper.fill( | 
| Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 177 | "{0}:{1}: '{2}' does not match the required pattern '{3}'." | 
|  | 178 | .format( | 
|  | 179 | self.match.filename, | 
| Yuto Takano | 5f83171 | 2021-08-18 18:03:24 +0100 | [diff] [blame] | 180 | self.match.line_no, | 
| Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 181 | self.match.name, | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 182 | self.pattern | 
|  | 183 | ) | 
|  | 184 | ) + "\n" + str(self.match) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 185 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 186 | class Typo(Problem): # pylint: disable=too-few-public-methods | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 187 | """ | 
|  | 188 | A problem that occurs when a word using MBED doesn't appear to be defined as | 
|  | 189 | constants nor enum values. Created with NameCheck.check_for_typos() | 
|  | 190 |  | 
|  | 191 | Fields: | 
|  | 192 | * match: the Match object of the MBED name in question. | 
|  | 193 | """ | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 194 | def __init__(self, match): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 195 | self.match = match | 
|  | 196 | Problem.__init__(self) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 197 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 198 | def quiet_output(self): | 
|  | 199 | return ( | 
|  | 200 | "{0}:{1}:{2}" | 
|  | 201 | .format(self.match.filename, self.match.line_no, self.match.name) | 
|  | 202 | ) | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 203 |  | 
| Yuto Takano | fc1e9ff | 2021-08-23 13:54:56 +0100 | [diff] [blame] | 204 | def verbose_output(self): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 205 | return self.textwrapper.fill( | 
| Yuto Takano | a4e7512 | 2021-08-06 17:23:28 +0100 | [diff] [blame] | 206 | "{0}:{1}: '{2}' looks like a typo. It was not found in any " | 
|  | 207 | "macros or any enums. If this is not a typo, put " | 
|  | 208 | "//no-check-names after it." | 
| Yuto Takano | 5f83171 | 2021-08-18 18:03:24 +0100 | [diff] [blame] | 209 | .format(self.match.filename, self.match.line_no, self.match.name) | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 210 | ) + "\n" + str(self.match) | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 211 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 212 | class CodeParser(): | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 213 | """ | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 214 | Class for retrieving files and parsing the code. This can be used | 
|  | 215 | independently of the checks that NameChecker performs, for example for | 
|  | 216 | list_internal_identifiers.py. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 217 | """ | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 218 | def __init__(self, log): | 
|  | 219 | self.log = log | 
| Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 220 | self.check_repo_path() | 
| Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 221 |  | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 222 | # Memo for storing "glob expression": set(filepaths) | 
|  | 223 | self.files = {} | 
|  | 224 |  | 
| Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 225 | # Globally excluded filenames | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 226 | self.excluded_files = ["**/bn_mul", "**/compat-2.x.h"] | 
| Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 227 |  | 
| Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 228 | @staticmethod | 
|  | 229 | def check_repo_path(): | 
|  | 230 | """ | 
|  | 231 | Check that the current working directory is the project root, and throw | 
|  | 232 | an exception if not. | 
|  | 233 | """ | 
|  | 234 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): | 
|  | 235 | raise Exception("This script must be run from Mbed TLS root") | 
|  | 236 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 237 | def comprehensive_parse(self): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 238 | """ | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 239 | Comprehensive ("default") function to call each parsing function and | 
|  | 240 | retrieve various elements of the code, together with the source location. | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 241 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 242 | Returns a dict of parsed item key to the corresponding List of Matches. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 243 | """ | 
|  | 244 | self.log.info("Parsing source code...") | 
| Yuto Takano | d24e037 | 2021-08-06 16:42:33 +0100 | [diff] [blame] | 245 | self.log.debug( | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 246 | "The following files are excluded from the search: {}" | 
| Yuto Takano | d24e037 | 2021-08-06 16:42:33 +0100 | [diff] [blame] | 247 | .format(str(self.excluded_files)) | 
|  | 248 | ) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 249 |  | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 250 | all_macros = self.parse_macros([ | 
|  | 251 | "include/mbedtls/*.h", | 
|  | 252 | "include/psa/*.h", | 
|  | 253 | "library/*.h", | 
|  | 254 | "tests/include/test/drivers/*.h", | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 255 | "3rdparty/everest/include/everest/everest.h", | 
|  | 256 | "3rdparty/everest/include/everest/x25519.h" | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 257 | ]) | 
|  | 258 | enum_consts = self.parse_enum_consts([ | 
|  | 259 | "include/mbedtls/*.h", | 
|  | 260 | "library/*.h", | 
|  | 261 | "3rdparty/everest/include/everest/everest.h", | 
|  | 262 | "3rdparty/everest/include/everest/x25519.h" | 
|  | 263 | ]) | 
|  | 264 | identifiers = self.parse_identifiers([ | 
|  | 265 | "include/mbedtls/*.h", | 
|  | 266 | "include/psa/*.h", | 
|  | 267 | "library/*.h", | 
|  | 268 | "3rdparty/everest/include/everest/everest.h", | 
|  | 269 | "3rdparty/everest/include/everest/x25519.h" | 
|  | 270 | ]) | 
|  | 271 | mbed_words = self.parse_mbed_words([ | 
|  | 272 | "include/mbedtls/*.h", | 
|  | 273 | "include/psa/*.h", | 
|  | 274 | "library/*.h", | 
|  | 275 | "3rdparty/everest/include/everest/everest.h", | 
|  | 276 | "3rdparty/everest/include/everest/x25519.h", | 
|  | 277 | "library/*.c", | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 278 | "3rdparty/everest/library/everest.c", | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 279 | "3rdparty/everest/library/x25519.c" | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 280 | ]) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 281 | symbols = self.parse_symbols() | 
|  | 282 |  | 
|  | 283 | # Remove identifier macros like mbedtls_printf or mbedtls_calloc | 
|  | 284 | identifiers_justname = [x.name for x in identifiers] | 
|  | 285 | actual_macros = [] | 
|  | 286 | for macro in all_macros: | 
|  | 287 | if macro.name not in identifiers_justname: | 
|  | 288 | actual_macros.append(macro) | 
|  | 289 |  | 
|  | 290 | self.log.debug("Found:") | 
| Yuto Takano | 9d9c6dc | 2021-08-16 10:43:45 +0100 | [diff] [blame] | 291 | # Aligns the counts on the assumption that none exceeds 4 digits | 
|  | 292 | self.log.debug("  {:4} Total Macros".format(len(all_macros))) | 
|  | 293 | self.log.debug("  {:4} Non-identifier Macros".format(len(actual_macros))) | 
|  | 294 | self.log.debug("  {:4} Enum Constants".format(len(enum_consts))) | 
|  | 295 | self.log.debug("  {:4} Identifiers".format(len(identifiers))) | 
|  | 296 | self.log.debug("  {:4} Exported Symbols".format(len(symbols))) | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 297 | return { | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 298 | "macros": actual_macros, | 
|  | 299 | "enum_consts": enum_consts, | 
|  | 300 | "identifiers": identifiers, | 
|  | 301 | "symbols": symbols, | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 302 | "mbed_words": mbed_words | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 305 | def get_files(self, include_wildcards, exclude_wildcards): | 
|  | 306 | """ | 
|  | 307 | Get all files that match any of the UNIX-style wildcards. While the | 
|  | 308 | check_names script is designed only for use on UNIX/macOS (due to nm), | 
|  | 309 | this function alone would work fine on Windows even with forward slashes | 
|  | 310 | in the wildcard. | 
|  | 311 |  | 
|  | 312 | Args: | 
|  | 313 | * include_wildcards: a List of shell-style wildcards to match filepaths. | 
|  | 314 | * exclude_wildcards: a List of shell-style wildcards to exclude. | 
|  | 315 |  | 
|  | 316 | Returns a List of relative filepaths. | 
|  | 317 | """ | 
|  | 318 | accumulator = set() | 
|  | 319 |  | 
|  | 320 | # exclude_wildcards may be None. Also, consider the global exclusions. | 
|  | 321 | exclude_wildcards = (exclude_wildcards or []) + self.excluded_files | 
|  | 322 |  | 
| Yuto Takano | 6adb287 | 2021-08-16 11:38:34 +0100 | [diff] [blame] | 323 | # Internal function to hit the memoisation cache or add to it the result | 
|  | 324 | # of a glob operation. Used both for inclusion and exclusion since the | 
|  | 325 | # only difference between them is whether they perform set union or | 
|  | 326 | # difference on the return value of this function. | 
|  | 327 | def hit_cache(wildcard): | 
|  | 328 | if wildcard not in self.files: | 
|  | 329 | self.files[wildcard] = set(glob.glob(wildcard, recursive=True)) | 
|  | 330 | return self.files[wildcard] | 
|  | 331 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 332 | for include_wildcard in include_wildcards: | 
| Yuto Takano | 6adb287 | 2021-08-16 11:38:34 +0100 | [diff] [blame] | 333 | accumulator = accumulator.union(hit_cache(include_wildcard)) | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 334 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 335 | for exclude_wildcard in exclude_wildcards: | 
| Yuto Takano | 6adb287 | 2021-08-16 11:38:34 +0100 | [diff] [blame] | 336 | accumulator = accumulator.difference(hit_cache(exclude_wildcard)) | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 337 |  | 
|  | 338 | return list(accumulator) | 
|  | 339 |  | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 340 | def parse_macros(self, include, exclude=None): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 341 | """ | 
|  | 342 | Parse all macros defined by #define preprocessor directives. | 
|  | 343 |  | 
|  | 344 | Args: | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 345 | * include: A List of glob expressions to look for files through. | 
|  | 346 | * exclude: A List of glob expressions for excluding files. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 347 |  | 
|  | 348 | Returns a List of Match objects for the found macros. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 349 | """ | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 350 | macro_regex = re.compile(r"# *define +(?P<macro>\w+)") | 
|  | 351 | exclusions = ( | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 352 | "asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_" | 
|  | 353 | ) | 
|  | 354 |  | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 355 | files = self.get_files(include, exclude) | 
|  | 356 | self.log.debug("Looking for macros in {} files".format(len(files))) | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 357 |  | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 358 | macros = [] | 
|  | 359 | for header_file in files: | 
| Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 360 | with open(header_file, "r", encoding="utf-8") as header: | 
| Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 361 | for line_no, line in enumerate(header): | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 362 | for macro in macro_regex.finditer(line): | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 363 | if macro.group("macro").startswith(exclusions): | 
|  | 364 | continue | 
|  | 365 |  | 
|  | 366 | macros.append(Match( | 
|  | 367 | header_file, | 
|  | 368 | line, | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 369 | line_no, | 
|  | 370 | macro.span("macro"), | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 371 | macro.group("macro"))) | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 372 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 373 | return macros | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 374 |  | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 375 | def parse_mbed_words(self, include, exclude=None): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 376 | """ | 
| Yuto Takano | b47b504 | 2021-08-07 00:42:54 +0100 | [diff] [blame] | 377 | Parse all words in the file that begin with MBED, in and out of macros, | 
|  | 378 | comments, anything. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 379 |  | 
|  | 380 | Args: | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 381 | * include: A List of glob expressions to look for files through. | 
|  | 382 | * exclude: A List of glob expressions for excluding files. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 383 |  | 
|  | 384 | Returns a List of Match objects for words beginning with MBED. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 385 | """ | 
| Yuto Takano | b47b504 | 2021-08-07 00:42:54 +0100 | [diff] [blame] | 386 | # Typos of TLS are common, hence the broader check below than MBEDTLS. | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 387 | mbed_regex = re.compile(r"\bMBED.+?_[A-Z0-9_]*") | 
|  | 388 | exclusions = re.compile(r"// *no-check-names|#error") | 
|  | 389 |  | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 390 | files = self.get_files(include, exclude) | 
|  | 391 | self.log.debug("Looking for MBED words in {} files".format(len(files))) | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 392 |  | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 393 | mbed_words = [] | 
|  | 394 | for filename in files: | 
| Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 395 | with open(filename, "r", encoding="utf-8") as fp: | 
| Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 396 | for line_no, line in enumerate(fp): | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 397 | if exclusions.search(line): | 
| Yuto Takano | c62b408 | 2021-08-05 20:17:07 +0100 | [diff] [blame] | 398 | continue | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 399 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 400 | for name in mbed_regex.finditer(line): | 
|  | 401 | mbed_words.append(Match( | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 402 | filename, | 
|  | 403 | line, | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 404 | line_no, | 
|  | 405 | name.span(0), | 
|  | 406 | name.group(0))) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 407 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 408 | return mbed_words | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 409 |  | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 410 | def parse_enum_consts(self, include, exclude=None): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 411 | """ | 
|  | 412 | Parse all enum value constants that are declared. | 
|  | 413 |  | 
|  | 414 | Args: | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 415 | * include: A List of glob expressions to look for files through. | 
|  | 416 | * exclude: A List of glob expressions for excluding files. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 417 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 418 | Returns a List of Match objects for the findings. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 419 | """ | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 420 | files = self.get_files(include, exclude) | 
|  | 421 | self.log.debug("Looking for enum consts in {} files".format(len(files))) | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 422 |  | 
| Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 423 | # Emulate a finite state machine to parse enum declarations. | 
|  | 424 | # OUTSIDE_KEYWORD = outside the enum keyword | 
|  | 425 | # IN_BRACES = inside enum opening braces | 
|  | 426 | # IN_BETWEEN = between enum keyword and opening braces | 
|  | 427 | states = enum.Enum("FSM", ["OUTSIDE_KEYWORD", "IN_BRACES", "IN_BETWEEN"]) | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 428 | enum_consts = [] | 
|  | 429 | for header_file in files: | 
| Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 430 | state = states.OUTSIDE_KEYWORD | 
| Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 431 | with open(header_file, "r", encoding="utf-8") as header: | 
| Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 432 | for line_no, line in enumerate(header): | 
| Yuto Takano | 13ecd99 | 2021-08-06 16:56:52 +0100 | [diff] [blame] | 433 | # Match typedefs and brackets only when they are at the | 
|  | 434 | # beginning of the line -- if they are indented, they might | 
|  | 435 | # be sub-structures within structs, etc. | 
| Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 436 | if (state == states.OUTSIDE_KEYWORD and | 
| Yuto Takano | 3590691 | 2021-08-17 11:05:43 +0100 | [diff] [blame] | 437 | re.search(r"^(typedef +)?enum +{", line)): | 
| Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 438 | state = states.IN_BRACES | 
|  | 439 | elif (state == states.OUTSIDE_KEYWORD and | 
|  | 440 | re.search(r"^(typedef +)?enum", line)): | 
|  | 441 | state = states.IN_BETWEEN | 
|  | 442 | elif (state == states.IN_BETWEEN and | 
|  | 443 | re.search(r"^{", line)): | 
|  | 444 | state = states.IN_BRACES | 
|  | 445 | elif (state == states.IN_BRACES and | 
|  | 446 | re.search(r"^}", line)): | 
|  | 447 | state = states.OUTSIDE_KEYWORD | 
|  | 448 | elif (state == states.IN_BRACES and | 
|  | 449 | not re.search(r"^ *#", line)): | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 450 | enum_const = re.search(r"^ *(?P<enum_const>\w+)", line) | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 451 | if not enum_const: | 
|  | 452 | continue | 
|  | 453 |  | 
|  | 454 | enum_consts.append(Match( | 
|  | 455 | header_file, | 
|  | 456 | line, | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 457 | line_no, | 
|  | 458 | enum_const.span("enum_const"), | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 459 | enum_const.group("enum_const"))) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 460 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 461 | return enum_consts | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 462 |  | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 463 | def parse_identifiers(self, include, exclude=None): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 464 | """ | 
| Yuto Takano | 8246eb8 | 2021-08-16 10:37:24 +0100 | [diff] [blame] | 465 | Parse all lines of a header where a function/enum/struct/union/typedef | 
| Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 466 | identifier is declared, based on some regex and heuristics. Highly | 
|  | 467 | dependent on formatting style. | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 468 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 469 | Args: | 
| Yuto Takano | 8e9a219 | 2021-08-09 14:48:53 +0100 | [diff] [blame] | 470 | * include: A List of glob expressions to look for files through. | 
|  | 471 | * exclude: A List of glob expressions for excluding files. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 472 |  | 
|  | 473 | Returns a List of Match objects with identifiers. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 474 | """ | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 475 | identifier_regex = re.compile( | 
|  | 476 | # Match " something(a" or " *something(a". Functions. | 
|  | 477 | # Assumptions: | 
|  | 478 | # - function definition from return type to one of its arguments is | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 479 | #   all on one line | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 480 | # - function definition line only contains alphanumeric, asterisk, | 
|  | 481 | #   underscore, and open bracket | 
|  | 482 | r".* \**(\w+) *\( *\w|" | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 483 | # Match "(*something)(". | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 484 | r".*\( *\* *(\w+) *\) *\(|" | 
|  | 485 | # Match names of named data structures. | 
|  | 486 | r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$|" | 
|  | 487 | # Match names of typedef instances, after closing bracket. | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 488 | r"}? *(\w+)[;[].*" | 
|  | 489 | ) | 
| Yuto Takano | 3590691 | 2021-08-17 11:05:43 +0100 | [diff] [blame] | 490 | # The regex below is indented for clarity. | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 491 | exclusion_lines = re.compile( | 
|  | 492 | r"^(" | 
| Yuto Takano | 3590691 | 2021-08-17 11:05:43 +0100 | [diff] [blame] | 493 | r"extern +\"C\"|" # pylint: disable=bad-continuation | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 494 | r"(typedef +)?(struct|union|enum)( *{)?$|" | 
|  | 495 | r"} *;?$|" | 
|  | 496 | r"$|" | 
|  | 497 | r"//|" | 
|  | 498 | r"#" | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 499 | r")" | 
|  | 500 | ) | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 501 |  | 
| Yuto Takano | 5095343 | 2021-08-09 14:54:36 +0100 | [diff] [blame] | 502 | files = self.get_files(include, exclude) | 
|  | 503 | self.log.debug("Looking for identifiers in {} files".format(len(files))) | 
|  | 504 |  | 
|  | 505 | identifiers = [] | 
|  | 506 | for header_file in files: | 
| Yuto Takano | a083d15 | 2021-08-07 00:25:59 +0100 | [diff] [blame] | 507 | with open(header_file, "r", encoding="utf-8") as header: | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 508 | in_block_comment = False | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 509 | # The previous line variable is used for concatenating lines | 
| Yuto Takano | b1417b4 | 2021-08-17 10:30:20 +0100 | [diff] [blame] | 510 | # when identifiers are formatted and spread across multiple | 
|  | 511 | # lines. | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 512 | previous_line = "" | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 513 |  | 
| Yuto Takano | 8f457cf | 2021-08-06 17:54:58 +0100 | [diff] [blame] | 514 | for line_no, line in enumerate(header): | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 515 | # Skip parsing this line if a block comment ends on it, | 
|  | 516 | # but don't skip if it has just started -- there is a chance | 
|  | 517 | # it ends on the same line. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 518 | if re.search(r"/\*", line): | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 519 | in_block_comment = not in_block_comment | 
|  | 520 | if re.search(r"\*/", line): | 
|  | 521 | in_block_comment = not in_block_comment | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 522 | continue | 
|  | 523 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 524 | if in_block_comment: | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 525 | previous_line = "" | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 526 | continue | 
|  | 527 |  | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 528 | if exclusion_lines.search(line): | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 529 | previous_line = "" | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 530 | continue | 
|  | 531 |  | 
| Yuto Takano | cfc9e4a | 2021-08-06 20:02:32 +0100 | [diff] [blame] | 532 | # If the line contains only space-separated alphanumeric | 
|  | 533 | # characters (or underscore, asterisk, or, open bracket), | 
|  | 534 | # and nothing else, high chance it's a declaration that | 
|  | 535 | # continues on the next line | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 536 | if re.search(r"^([\w\*\(]+\s+)+$", line): | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 537 | previous_line += line | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 538 | continue | 
|  | 539 |  | 
|  | 540 | # If previous line seemed to start an unfinished declaration | 
| Yuto Takano | cfc9e4a | 2021-08-06 20:02:32 +0100 | [diff] [blame] | 541 | # (as above), concat and treat them as one. | 
|  | 542 | if previous_line: | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 543 | line = previous_line.strip() + " " + line.strip() + "\n" | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 544 | previous_line = "" | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 545 |  | 
| Yuto Takano | 8246eb8 | 2021-08-16 10:37:24 +0100 | [diff] [blame] | 546 | # Skip parsing if line has a space in front = heuristic to | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 547 | # skip function argument lines (highly subject to formatting | 
|  | 548 | # changes) | 
|  | 549 | if line[0] == " ": | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 550 | continue | 
| Yuto Takano | 6f38ab3 | 2021-08-05 21:07:14 +0100 | [diff] [blame] | 551 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 552 | identifier = identifier_regex.search(line) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 553 |  | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 554 | if not identifier: | 
|  | 555 | continue | 
|  | 556 |  | 
|  | 557 | # Find the group that matched, and append it | 
|  | 558 | for group in identifier.groups(): | 
|  | 559 | if not group: | 
|  | 560 | continue | 
|  | 561 |  | 
|  | 562 | identifiers.append(Match( | 
|  | 563 | header_file, | 
|  | 564 | line, | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 565 | line_no, | 
|  | 566 | identifier.span(), | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 567 | group)) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 568 |  | 
|  | 569 | return identifiers | 
|  | 570 |  | 
|  | 571 | def parse_symbols(self): | 
|  | 572 | """ | 
|  | 573 | Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509 | 
|  | 574 | object files using nm to retrieve the list of referenced symbols. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 575 | Exceptions thrown here are rethrown because they would be critical | 
|  | 576 | errors that void several tests, and thus needs to halt the program. This | 
|  | 577 | is explicitly done for clarity. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 578 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 579 | Returns a List of unique symbols defined and used in the libraries. | 
|  | 580 | """ | 
|  | 581 | self.log.info("Compiling...") | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 582 | symbols = [] | 
|  | 583 |  | 
|  | 584 | # Back up the config and atomically compile with the full configratuion. | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 585 | shutil.copy( | 
|  | 586 | "include/mbedtls/mbedtls_config.h", | 
|  | 587 | "include/mbedtls/mbedtls_config.h.bak" | 
|  | 588 | ) | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 589 | try: | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 590 | # Use check=True in all subprocess calls so that failures are raised | 
|  | 591 | # as exceptions and logged. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 592 | subprocess.run( | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 593 | ["python3", "scripts/config.py", "full"], | 
| Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 594 | universal_newlines=True, | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 595 | check=True | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 596 | ) | 
|  | 597 | my_environment = os.environ.copy() | 
|  | 598 | my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables" | 
| Yuto Takano | 4b7d23d | 2021-08-17 10:48:22 +0100 | [diff] [blame] | 599 | # Run make clean separately to lib to prevent unwanted behavior when | 
|  | 600 | # make is invoked with parallelism. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 601 | subprocess.run( | 
| Yuto Takano | 4b7d23d | 2021-08-17 10:48:22 +0100 | [diff] [blame] | 602 | ["make", "clean"], | 
|  | 603 | universal_newlines=True, | 
|  | 604 | check=True | 
|  | 605 | ) | 
|  | 606 | subprocess.run( | 
|  | 607 | ["make", "lib"], | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 608 | env=my_environment, | 
| Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 609 | universal_newlines=True, | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 610 | stdout=subprocess.PIPE, | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 611 | stderr=subprocess.STDOUT, | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 612 | check=True | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 613 | ) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 614 |  | 
|  | 615 | # Perform object file analysis using nm | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 616 | symbols = self.parse_symbols_from_nm([ | 
|  | 617 | "library/libmbedcrypto.a", | 
|  | 618 | "library/libmbedtls.a", | 
|  | 619 | "library/libmbedx509.a" | 
|  | 620 | ]) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 621 |  | 
|  | 622 | subprocess.run( | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 623 | ["make", "clean"], | 
| Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 624 | universal_newlines=True, | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 625 | check=True | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 626 | ) | 
|  | 627 | except subprocess.CalledProcessError as error: | 
| Yuto Takano | 25eeb7b | 2021-08-06 21:27:59 +0100 | [diff] [blame] | 628 | self.log.debug(error.output) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 629 | raise error | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 630 | finally: | 
| Yuto Takano | 6fececf | 2021-08-07 17:28:23 +0100 | [diff] [blame] | 631 | # Put back the original config regardless of there being errors. | 
|  | 632 | # Works also for keyboard interrupts. | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 633 | shutil.move( | 
|  | 634 | "include/mbedtls/mbedtls_config.h.bak", | 
|  | 635 | "include/mbedtls/mbedtls_config.h" | 
|  | 636 | ) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 637 |  | 
|  | 638 | return symbols | 
|  | 639 |  | 
|  | 640 | def parse_symbols_from_nm(self, object_files): | 
|  | 641 | """ | 
|  | 642 | Run nm to retrieve the list of referenced symbols in each object file. | 
|  | 643 | Does not return the position data since it is of no use. | 
|  | 644 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 645 | Args: | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 646 | * object_files: a List of compiled object filepaths to search through. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 647 |  | 
|  | 648 | Returns a List of unique symbols defined and used in any of the object | 
|  | 649 | files. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 650 | """ | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 651 | nm_undefined_regex = re.compile(r"^\S+: +U |^$|^\S+:$") | 
|  | 652 | nm_valid_regex = re.compile(r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)") | 
| Yuto Takano | 12a7ecd | 2021-08-07 00:40:29 +0100 | [diff] [blame] | 653 | exclusions = ("FStar", "Hacl") | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 654 |  | 
|  | 655 | symbols = [] | 
|  | 656 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 657 | # Gather all outputs of nm | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 658 | nm_output = "" | 
|  | 659 | for lib in object_files: | 
|  | 660 | nm_output += subprocess.run( | 
|  | 661 | ["nm", "-og", lib], | 
| Yuto Takano | bcc3d99 | 2021-08-06 23:14:58 +0100 | [diff] [blame] | 662 | universal_newlines=True, | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 663 | stdout=subprocess.PIPE, | 
|  | 664 | stderr=subprocess.STDOUT, | 
|  | 665 | check=True | 
|  | 666 | ).stdout | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 667 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 668 | for line in nm_output.splitlines(): | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 669 | if not nm_undefined_regex.search(line): | 
|  | 670 | symbol = nm_valid_regex.search(line) | 
| Yuto Takano | 12a7ecd | 2021-08-07 00:40:29 +0100 | [diff] [blame] | 671 | if (symbol and not symbol.group("symbol").startswith(exclusions)): | 
| Yuto Takano | e77f699 | 2021-08-05 20:22:59 +0100 | [diff] [blame] | 672 | symbols.append(symbol.group("symbol")) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 673 | else: | 
|  | 674 | self.log.error(line) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 675 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 676 | return symbols | 
|  | 677 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 678 | class NameChecker(): | 
|  | 679 | """ | 
|  | 680 | Representation of the core name checking operation performed by this script. | 
|  | 681 | """ | 
|  | 682 | def __init__(self, parse_result, log): | 
|  | 683 | self.parse_result = parse_result | 
|  | 684 | self.log = log | 
|  | 685 |  | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 686 | def perform_checks(self, quiet=False): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 687 | """ | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 688 | A comprehensive checker that performs each check in order, and outputs | 
|  | 689 | a final verdict. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 690 |  | 
|  | 691 | Args: | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 692 | * quiet: whether to hide detailed problem explanation. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 693 | """ | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 694 | self.log.info("=============") | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 695 | Problem.quiet = quiet | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 696 | problems = 0 | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 697 | problems += self.check_symbols_declared_in_header() | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 698 |  | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 699 | pattern_checks = [ | 
|  | 700 | ("macros", MACRO_PATTERN), | 
|  | 701 | ("enum_consts", CONSTANTS_PATTERN), | 
|  | 702 | ("identifiers", IDENTIFIER_PATTERN) | 
|  | 703 | ] | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 704 | for group, check_pattern in pattern_checks: | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 705 | problems += self.check_match_pattern(group, check_pattern) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 706 |  | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 707 | problems += self.check_for_typos() | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 708 |  | 
|  | 709 | self.log.info("=============") | 
|  | 710 | if problems > 0: | 
|  | 711 | self.log.info("FAIL: {0} problem(s) to fix".format(str(problems))) | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 712 | if quiet: | 
|  | 713 | self.log.info("Remove --quiet to see explanations.") | 
| Yuto Takano | fc54dfb | 2021-08-07 17:18:28 +0100 | [diff] [blame] | 714 | else: | 
|  | 715 | self.log.info("Use --quiet for minimal output.") | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 716 | return 1 | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 717 | else: | 
|  | 718 | self.log.info("PASS") | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 719 | return 0 | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 720 |  | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 721 | def check_symbols_declared_in_header(self): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 722 | """ | 
|  | 723 | Perform a check that all detected symbols in the library object files | 
|  | 724 | are properly declared in headers. | 
| Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 725 | Assumes parse_names_in_source() was called before this. | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 726 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 727 | Returns the number of problems that need fixing. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 728 | """ | 
|  | 729 | problems = [] | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 730 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 731 | for symbol in self.parse_result["symbols"]: | 
|  | 732 | found_symbol_declared = False | 
|  | 733 | for identifier_match in self.parse_result["identifiers"]: | 
|  | 734 | if symbol == identifier_match.name: | 
|  | 735 | found_symbol_declared = True | 
|  | 736 | break | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 737 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 738 | if not found_symbol_declared: | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 739 | problems.append(SymbolNotInHeader(symbol)) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 740 |  | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 741 | self.output_check_result("All symbols in header", problems) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 742 | return len(problems) | 
|  | 743 |  | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 744 | def check_match_pattern(self, group_to_check, check_pattern): | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 745 | """ | 
|  | 746 | Perform a check that all items of a group conform to a regex pattern. | 
| Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 747 | Assumes parse_names_in_source() was called before this. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 748 |  | 
|  | 749 | Args: | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 750 | * group_to_check: string key to index into self.parse_result. | 
|  | 751 | * check_pattern: the regex to check against. | 
|  | 752 |  | 
|  | 753 | Returns the number of problems that need fixing. | 
|  | 754 | """ | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 755 | problems = [] | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 756 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 757 | for item_match in self.parse_result[group_to_check]: | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 758 | if not re.search(check_pattern, item_match.name): | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 759 | problems.append(PatternMismatch(check_pattern, item_match)) | 
| Yuto Takano | 90bc026 | 2021-08-16 11:34:10 +0100 | [diff] [blame] | 760 | # Double underscore should not be used for names | 
|  | 761 | if re.search(r".*__.*", item_match.name): | 
| Yuto Takano | 704b0f7 | 2021-08-17 10:41:23 +0100 | [diff] [blame] | 762 | problems.append( | 
|  | 763 | PatternMismatch("no double underscore allowed", item_match)) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 764 |  | 
|  | 765 | self.output_check_result( | 
|  | 766 | "Naming patterns of {}".format(group_to_check), | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 767 | problems) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 768 | return len(problems) | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 769 |  | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 770 | def check_for_typos(self): | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 771 | """ | 
|  | 772 | Perform a check that all words in the soure code beginning with MBED are | 
|  | 773 | either defined as macros, or as enum constants. | 
| Yuto Takano | 977e07f | 2021-08-09 11:56:15 +0100 | [diff] [blame] | 774 | Assumes parse_names_in_source() was called before this. | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 775 |  | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 776 | Returns the number of problems that need fixing. | 
|  | 777 | """ | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 778 | problems = [] | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 779 |  | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 780 | # Set comprehension, equivalent to a list comprehension wrapped by set() | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 781 | all_caps_names = { | 
|  | 782 | match.name | 
|  | 783 | for match | 
|  | 784 | in self.parse_result["macros"] + self.parse_result["enum_consts"]} | 
|  | 785 | typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$") | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 786 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 787 | for name_match in self.parse_result["mbed_words"]: | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 788 | found = name_match.name in all_caps_names | 
|  | 789 |  | 
|  | 790 | # Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the | 
|  | 791 | # PSA driver, they will not exist as macros. However, they | 
|  | 792 | # should still be checked for typos using the equivalent | 
|  | 793 | # BUILTINs that exist. | 
|  | 794 | if "MBEDTLS_PSA_ACCEL_" in name_match.name: | 
|  | 795 | found = name_match.name.replace( | 
|  | 796 | "MBEDTLS_PSA_ACCEL_", | 
|  | 797 | "MBEDTLS_PSA_BUILTIN_") in all_caps_names | 
|  | 798 |  | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 799 | if not found and not typo_exclusion.search(name_match.name): | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 800 | problems.append(Typo(name_match)) | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 801 |  | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 802 | self.output_check_result("Likely typos", problems) | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 803 | return len(problems) | 
|  | 804 |  | 
| Yuto Takano | 5473be2 | 2021-08-17 10:14:01 +0100 | [diff] [blame] | 805 | def output_check_result(self, name, problems): | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 806 | """ | 
|  | 807 | Write out the PASS/FAIL status of a performed check depending on whether | 
|  | 808 | there were problems. | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 809 |  | 
|  | 810 | Args: | 
| Yuto Takano | d70d446 | 2021-08-09 12:45:51 +0100 | [diff] [blame] | 811 | * name: the name of the test | 
|  | 812 | * problems: a List of encountered Problems | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 813 | """ | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 814 | if problems: | 
| Yuto Takano | 55614b5 | 2021-08-07 01:00:18 +0100 | [diff] [blame] | 815 | self.log.info("{}: FAIL\n".format(name)) | 
|  | 816 | for problem in problems: | 
|  | 817 | self.log.warning(str(problem)) | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 818 | else: | 
| Yuto Takano | 81528c0 | 2021-08-06 16:22:06 +0100 | [diff] [blame] | 819 | self.log.info("{}: PASS".format(name)) | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 820 |  | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 821 | def main(): | 
|  | 822 | """ | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 823 | Perform argument parsing, and create an instance of CodeParser and | 
|  | 824 | NameChecker to begin the core operation. | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 825 | """ | 
| Yuto Takano | f005c33 | 2021-08-09 13:56:36 +0100 | [diff] [blame] | 826 | parser = argparse.ArgumentParser( | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 827 | formatter_class=argparse.RawDescriptionHelpFormatter, | 
|  | 828 | description=( | 
|  | 829 | "This script confirms that the naming of all symbols and identifiers " | 
|  | 830 | "in Mbed TLS are consistent with the house style and are also " | 
|  | 831 | "self-consistent.\n\n" | 
| Yuto Takano | f005c33 | 2021-08-09 13:56:36 +0100 | [diff] [blame] | 832 | "Expected to be run from the MbedTLS root directory.") | 
|  | 833 | ) | 
|  | 834 | parser.add_argument( | 
|  | 835 | "-v", "--verbose", | 
|  | 836 | action="store_true", | 
|  | 837 | help="show parse results" | 
|  | 838 | ) | 
|  | 839 | parser.add_argument( | 
|  | 840 | "-q", "--quiet", | 
|  | 841 | action="store_true", | 
|  | 842 | help="hide unnecessary text, explanations, and highlighs" | 
|  | 843 | ) | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 844 |  | 
| Yuto Takano | f005c33 | 2021-08-09 13:56:36 +0100 | [diff] [blame] | 845 | args = parser.parse_args() | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 846 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 847 | # Configure the global logger, which is then passed to the classes below | 
|  | 848 | log = logging.getLogger() | 
|  | 849 | log.setLevel(logging.DEBUG if args.verbose else logging.INFO) | 
|  | 850 | log.addHandler(logging.StreamHandler()) | 
|  | 851 |  | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 852 | try: | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 853 | code_parser = CodeParser(log) | 
|  | 854 | parse_result = code_parser.comprehensive_parse() | 
| Yuto Takano | d93fa37 | 2021-08-06 23:05:55 +0100 | [diff] [blame] | 855 | except Exception: # pylint: disable=broad-except | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 856 | traceback.print_exc() | 
|  | 857 | sys.exit(2) | 
|  | 858 |  | 
| Yuto Takano | 55c6c87 | 2021-08-09 15:35:19 +0100 | [diff] [blame] | 859 | name_checker = NameChecker(parse_result, log) | 
|  | 860 | return_code = name_checker.perform_checks(quiet=args.quiet) | 
|  | 861 |  | 
|  | 862 | sys.exit(return_code) | 
|  | 863 |  | 
| Darryl Green | d580292 | 2018-05-08 15:30:59 +0100 | [diff] [blame] | 864 | if __name__ == "__main__": | 
| Yuto Takano | 3963967 | 2021-08-05 19:47:48 +0100 | [diff] [blame] | 865 | main() |