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