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