2021-08-05 20:47:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2018-05-08 16:30:59 +02:00
|
|
|
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
This script confirms that the naming of all symbols and identifiers in Mbed TLS
|
2021-08-06 18:00:28 +02:00
|
|
|
are consistent with the house style and are also self-consistent. It only runs
|
|
|
|
on Linux and macOS since it depends on nm.
|
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
It contains two major Python classes, CodeParser and NameChecker. They both have
|
|
|
|
a comprehensive "run-all" function (comprehensive_parse() and perform_checks())
|
|
|
|
but the individual functions can also be used for specific needs.
|
|
|
|
|
|
|
|
CodeParser makes heavy use of regular expressions to parse the code, and is
|
|
|
|
dependent on the current code formatting. Many Python C parser libraries require
|
|
|
|
preprocessed C code, which means no macro parsing. Compiler tools are also not
|
|
|
|
very helpful when we want the exact location in the original source (which
|
|
|
|
becomes impossible when e.g. comments are stripped).
|
|
|
|
|
|
|
|
NameChecker performs the following checks:
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
- All exported and available symbols in the library object files, are explicitly
|
2021-08-06 18:00:28 +02:00
|
|
|
declared in the header files. This uses the nm command.
|
2021-08-06 17:22:06 +02:00
|
|
|
- All macros, constants, and identifiers (function names, struct names, etc)
|
2021-08-09 16:35:19 +02:00
|
|
|
follow the required regex pattern.
|
2021-08-06 17:22:06 +02:00
|
|
|
- Typo checking: All words that begin with MBED exist as macros or constants.
|
2021-08-07 18:18:28 +02:00
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
The script returns 0 on success, 1 on test failure, and 2 if there is a script
|
2021-08-16 11:37:24 +02:00
|
|
|
error. It must be run from Mbed TLS root.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
2021-08-09 12:56:15 +02:00
|
|
|
import glob
|
2021-08-05 20:47:48 +02:00
|
|
|
import textwrap
|
2018-05-08 16:30:59 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import re
|
2021-08-17 11:30:20 +02:00
|
|
|
import enum
|
2018-05-08 16:30:59 +02:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import logging
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
# Naming patterns to check against. These are defined outside the NameCheck
|
|
|
|
# class for ease of modification.
|
2021-08-05 20:57:58 +02:00
|
|
|
MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$"
|
2021-08-06 17:22:06 +02:00
|
|
|
CONSTANTS_PATTERN = MACRO_PATTERN
|
2021-08-05 20:52:09 +02:00
|
|
|
IDENTIFIER_PATTERN = r"^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$"
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
class Match(): # pylint: disable=too-few-public-methods
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A class representing a match, together with its found position.
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* filename: the file that the match was in.
|
|
|
|
* line: the full line containing the match.
|
2021-08-17 11:41:23 +02:00
|
|
|
* line_no: the line number.
|
|
|
|
* pos: a tuple of (start, end) positions on the line where the match is.
|
2021-08-06 17:22:06 +02:00
|
|
|
* name: the match itself.
|
|
|
|
"""
|
2021-08-17 11:41:23 +02:00
|
|
|
def __init__(self, filename, line, line_no, pos, name):
|
|
|
|
# pylint: disable=too-many-arguments
|
2021-08-05 20:47:48 +02:00
|
|
|
self.filename = filename
|
|
|
|
self.line = line
|
2021-08-17 11:41:23 +02:00
|
|
|
self.line_no = line_no
|
2021-08-05 20:47:48 +02:00
|
|
|
self.pos = pos
|
|
|
|
self.name = name
|
|
|
|
|
2021-08-06 18:23:28 +02:00
|
|
|
def __str__(self):
|
2021-08-16 11:32:40 +02:00
|
|
|
"""
|
|
|
|
Return a formatted code listing representation of the erroneous line.
|
|
|
|
"""
|
2021-08-17 11:41:23 +02:00
|
|
|
gutter = format(self.line_no, "4d")
|
|
|
|
underline = self.pos[0] * " " + (self.pos[1] - self.pos[0]) * "^"
|
2021-08-07 00:37:20 +02:00
|
|
|
|
2021-08-06 18:23:28 +02:00
|
|
|
return (
|
2021-08-16 11:32:40 +02:00
|
|
|
" {0} |\n".format(" " * len(gutter)) +
|
2021-08-07 00:37:20 +02:00
|
|
|
" {0} | {1}".format(gutter, self.line) +
|
2021-08-16 11:32:40 +02:00
|
|
|
" {0} | {1}\n".format(" " * len(gutter), underline)
|
2021-08-06 18:23:28 +02:00
|
|
|
)
|
2021-08-07 00:05:55 +02:00
|
|
|
|
|
|
|
class Problem(): # pylint: disable=too-few-public-methods
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A parent class representing a form of static analysis error.
|
|
|
|
"""
|
2021-08-17 11:14:01 +02:00
|
|
|
# Class variable to control the quietness of all problems
|
|
|
|
quiet = False
|
2021-08-05 20:47:48 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.textwrapper = textwrap.TextWrapper()
|
2021-08-06 17:22:06 +02:00
|
|
|
self.textwrapper.width = 80
|
2021-08-06 18:23:28 +02:00
|
|
|
self.textwrapper.initial_indent = " > "
|
2021-08-06 17:22:06 +02:00
|
|
|
self.textwrapper.subsequent_indent = " "
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
class SymbolNotInHeader(Problem): # pylint: disable=too-few-public-methods
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A problem that occurs when an exported/available symbol in the object file
|
|
|
|
is not explicitly declared in header files. Created with
|
|
|
|
NameCheck.check_symbols_declared_in_header()
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* symbol_name: the name of the symbol.
|
|
|
|
"""
|
2021-08-09 13:45:51 +02:00
|
|
|
def __init__(self, symbol_name):
|
2021-08-05 20:47:48 +02:00
|
|
|
self.symbol_name = symbol_name
|
|
|
|
Problem.__init__(self)
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-08-07 02:00:18 +02:00
|
|
|
if self.quiet:
|
|
|
|
return "{0}".format(self.symbol_name)
|
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
return self.textwrapper.fill(
|
|
|
|
"'{0}' was found as an available symbol in the output of nm, "
|
|
|
|
"however it was not declared in any header files."
|
|
|
|
.format(self.symbol_name))
|
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
class PatternMismatch(Problem): # pylint: disable=too-few-public-methods
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A problem that occurs when something doesn't match the expected pattern.
|
|
|
|
Created with NameCheck.check_match_pattern()
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* pattern: the expected regex pattern
|
|
|
|
* match: the Match object in question
|
|
|
|
"""
|
2021-08-09 13:45:51 +02:00
|
|
|
def __init__(self, pattern, match):
|
2021-08-05 20:47:48 +02:00
|
|
|
self.pattern = pattern
|
|
|
|
self.match = match
|
|
|
|
Problem.__init__(self)
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
def __str__(self):
|
2021-08-07 02:00:18 +02:00
|
|
|
if self.quiet:
|
2021-08-09 13:45:51 +02:00
|
|
|
return (
|
2021-08-10 12:30:43 +02:00
|
|
|
"{0}:{1}:{2}"
|
2021-08-09 13:45:51 +02:00
|
|
|
.format(self.match.filename, self.match.pos[0], self.match.name)
|
|
|
|
)
|
2021-08-07 02:00:18 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
return self.textwrapper.fill(
|
2021-08-06 18:23:28 +02:00
|
|
|
"{0}:{1}: '{2}' does not match the required pattern '{3}'."
|
|
|
|
.format(
|
|
|
|
self.match.filename,
|
2021-08-07 00:05:55 +02:00
|
|
|
self.match.pos[0],
|
2021-08-06 18:23:28 +02:00
|
|
|
self.match.name,
|
2021-08-09 13:45:51 +02:00
|
|
|
self.pattern
|
|
|
|
)
|
|
|
|
) + "\n" + str(self.match)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
class Typo(Problem): # pylint: disable=too-few-public-methods
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
A problem that occurs when a word using MBED doesn't appear to be defined as
|
|
|
|
constants nor enum values. Created with NameCheck.check_for_typos()
|
|
|
|
|
|
|
|
Fields:
|
|
|
|
* match: the Match object of the MBED name in question.
|
|
|
|
"""
|
2021-08-09 13:45:51 +02:00
|
|
|
def __init__(self, match):
|
2021-08-05 20:47:48 +02:00
|
|
|
self.match = match
|
|
|
|
Problem.__init__(self)
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
def __str__(self):
|
2021-08-07 02:00:18 +02:00
|
|
|
if self.quiet:
|
2021-08-09 13:45:51 +02:00
|
|
|
return (
|
|
|
|
"{0}:{1}:{2}"
|
|
|
|
.format(self.match.filename, self.match.pos[0], self.match.name)
|
|
|
|
)
|
2021-08-07 02:00:18 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
return self.textwrapper.fill(
|
2021-08-06 18:23:28 +02:00
|
|
|
"{0}:{1}: '{2}' looks like a typo. It was not found in any "
|
|
|
|
"macros or any enums. If this is not a typo, put "
|
|
|
|
"//no-check-names after it."
|
2021-08-09 13:45:51 +02:00
|
|
|
.format(self.match.filename, self.match.pos[0], self.match.name)
|
|
|
|
) + "\n" + str(self.match)
|
2018-05-08 16:30:59 +02:00
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
class CodeParser():
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
2021-08-09 16:35:19 +02:00
|
|
|
Class for retrieving files and parsing the code. This can be used
|
|
|
|
independently of the checks that NameChecker performs, for example for
|
|
|
|
list_internal_identifiers.py.
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
2021-08-09 16:35:19 +02:00
|
|
|
def __init__(self, log):
|
|
|
|
self.log = log
|
2021-08-07 18:18:28 +02:00
|
|
|
self.check_repo_path()
|
2021-08-09 12:56:15 +02:00
|
|
|
|
2021-08-09 15:48:53 +02:00
|
|
|
# Memo for storing "glob expression": set(filepaths)
|
|
|
|
self.files = {}
|
|
|
|
|
2021-08-09 12:56:15 +02:00
|
|
|
# Globally excluded filenames
|
2021-08-09 15:48:53 +02:00
|
|
|
self.excluded_files = ["**/bn_mul", "**/compat-2.x.h"]
|
2021-08-09 12:56:15 +02:00
|
|
|
|
2021-08-07 18:18:28 +02:00
|
|
|
@staticmethod
|
|
|
|
def check_repo_path():
|
|
|
|
"""
|
|
|
|
Check that the current working directory is the project root, and throw
|
|
|
|
an exception if not.
|
|
|
|
"""
|
|
|
|
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
|
|
|
|
raise Exception("This script must be run from Mbed TLS root")
|
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
def comprehensive_parse(self):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-09 16:35:19 +02:00
|
|
|
Comprehensive ("default") function to call each parsing function and
|
|
|
|
retrieve various elements of the code, together with the source location.
|
2018-05-08 16:30:59 +02:00
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
Returns a dict of parsed item key to the corresponding List of Matches.
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
self.log.info("Parsing source code...")
|
2021-08-06 17:42:33 +02:00
|
|
|
self.log.debug(
|
2021-08-09 15:54:36 +02:00
|
|
|
"The following files are excluded from the search: {}"
|
2021-08-06 17:42:33 +02:00
|
|
|
.format(str(self.excluded_files))
|
|
|
|
)
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-09 15:48:53 +02:00
|
|
|
all_macros = self.parse_macros([
|
|
|
|
"include/mbedtls/*.h",
|
|
|
|
"include/psa/*.h",
|
|
|
|
"library/*.h",
|
|
|
|
"tests/include/test/drivers/*.h",
|
2021-08-09 13:45:51 +02:00
|
|
|
"3rdparty/everest/include/everest/everest.h",
|
|
|
|
"3rdparty/everest/include/everest/x25519.h"
|
2021-08-09 15:48:53 +02:00
|
|
|
])
|
|
|
|
enum_consts = self.parse_enum_consts([
|
|
|
|
"include/mbedtls/*.h",
|
|
|
|
"library/*.h",
|
|
|
|
"3rdparty/everest/include/everest/everest.h",
|
|
|
|
"3rdparty/everest/include/everest/x25519.h"
|
|
|
|
])
|
|
|
|
identifiers = self.parse_identifiers([
|
|
|
|
"include/mbedtls/*.h",
|
|
|
|
"include/psa/*.h",
|
|
|
|
"library/*.h",
|
|
|
|
"3rdparty/everest/include/everest/everest.h",
|
|
|
|
"3rdparty/everest/include/everest/x25519.h"
|
|
|
|
])
|
|
|
|
mbed_words = self.parse_mbed_words([
|
|
|
|
"include/mbedtls/*.h",
|
|
|
|
"include/psa/*.h",
|
|
|
|
"library/*.h",
|
|
|
|
"3rdparty/everest/include/everest/everest.h",
|
|
|
|
"3rdparty/everest/include/everest/x25519.h",
|
|
|
|
"library/*.c",
|
2021-08-06 17:22:06 +02:00
|
|
|
"3rdparty/everest/library/everest.c",
|
2021-08-09 13:45:51 +02:00
|
|
|
"3rdparty/everest/library/x25519.c"
|
2021-08-09 15:48:53 +02:00
|
|
|
])
|
2021-08-06 17:22:06 +02:00
|
|
|
symbols = self.parse_symbols()
|
|
|
|
|
|
|
|
# Remove identifier macros like mbedtls_printf or mbedtls_calloc
|
|
|
|
identifiers_justname = [x.name for x in identifiers]
|
|
|
|
actual_macros = []
|
|
|
|
for macro in all_macros:
|
|
|
|
if macro.name not in identifiers_justname:
|
|
|
|
actual_macros.append(macro)
|
|
|
|
|
|
|
|
self.log.debug("Found:")
|
2021-08-16 11:43:45 +02:00
|
|
|
# Aligns the counts on the assumption that none exceeds 4 digits
|
|
|
|
self.log.debug(" {:4} Total Macros".format(len(all_macros)))
|
|
|
|
self.log.debug(" {:4} Non-identifier Macros".format(len(actual_macros)))
|
|
|
|
self.log.debug(" {:4} Enum Constants".format(len(enum_consts)))
|
|
|
|
self.log.debug(" {:4} Identifiers".format(len(identifiers)))
|
|
|
|
self.log.debug(" {:4} Exported Symbols".format(len(symbols)))
|
2021-08-09 16:35:19 +02:00
|
|
|
return {
|
2021-08-06 17:22:06 +02:00
|
|
|
"macros": actual_macros,
|
|
|
|
"enum_consts": enum_consts,
|
|
|
|
"identifiers": identifiers,
|
|
|
|
"symbols": symbols,
|
2021-08-07 00:05:55 +02:00
|
|
|
"mbed_words": mbed_words
|
2021-08-06 17:22:06 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
def get_files(self, include_wildcards, exclude_wildcards):
|
|
|
|
"""
|
|
|
|
Get all files that match any of the UNIX-style wildcards. While the
|
|
|
|
check_names script is designed only for use on UNIX/macOS (due to nm),
|
|
|
|
this function alone would work fine on Windows even with forward slashes
|
|
|
|
in the wildcard.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
* include_wildcards: a List of shell-style wildcards to match filepaths.
|
|
|
|
* exclude_wildcards: a List of shell-style wildcards to exclude.
|
|
|
|
|
|
|
|
Returns a List of relative filepaths.
|
|
|
|
"""
|
|
|
|
accumulator = set()
|
|
|
|
|
|
|
|
# exclude_wildcards may be None. Also, consider the global exclusions.
|
|
|
|
exclude_wildcards = (exclude_wildcards or []) + self.excluded_files
|
|
|
|
|
2021-08-16 12:38:34 +02:00
|
|
|
# Internal function to hit the memoisation cache or add to it the result
|
|
|
|
# of a glob operation. Used both for inclusion and exclusion since the
|
|
|
|
# only difference between them is whether they perform set union or
|
|
|
|
# difference on the return value of this function.
|
|
|
|
def hit_cache(wildcard):
|
|
|
|
if wildcard not in self.files:
|
|
|
|
self.files[wildcard] = set(glob.glob(wildcard, recursive=True))
|
|
|
|
return self.files[wildcard]
|
2021-08-09 16:35:19 +02:00
|
|
|
|
2021-08-16 12:38:34 +02:00
|
|
|
for include_wildcard in include_wildcards:
|
|
|
|
accumulator = accumulator.union(hit_cache(include_wildcard))
|
2021-08-09 16:35:19 +02:00
|
|
|
|
|
|
|
for exclude_wildcard in exclude_wildcards:
|
2021-08-16 12:38:34 +02:00
|
|
|
accumulator = accumulator.difference(hit_cache(exclude_wildcard))
|
2021-08-09 16:35:19 +02:00
|
|
|
|
|
|
|
return list(accumulator)
|
|
|
|
|
2021-08-09 15:48:53 +02:00
|
|
|
def parse_macros(self, include, exclude=None):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
Parse all macros defined by #define preprocessor directives.
|
|
|
|
|
|
|
|
Args:
|
2021-08-09 15:48:53 +02:00
|
|
|
* include: A List of glob expressions to look for files through.
|
|
|
|
* exclude: A List of glob expressions for excluding files.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Returns a List of Match objects for the found macros.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-07 00:05:55 +02:00
|
|
|
macro_regex = re.compile(r"# *define +(?P<macro>\w+)")
|
|
|
|
exclusions = (
|
2021-08-05 20:47:48 +02:00
|
|
|
"asm", "inline", "EMIT", "_CRT_SECURE_NO_DEPRECATE", "MULADDC_"
|
|
|
|
)
|
|
|
|
|
2021-08-09 15:54:36 +02:00
|
|
|
files = self.get_files(include, exclude)
|
|
|
|
self.log.debug("Looking for macros in {} files".format(len(files)))
|
2021-08-07 00:05:55 +02:00
|
|
|
|
2021-08-09 15:54:36 +02:00
|
|
|
macros = []
|
|
|
|
for header_file in files:
|
2021-08-07 01:25:59 +02:00
|
|
|
with open(header_file, "r", encoding="utf-8") as header:
|
2021-08-06 18:54:58 +02:00
|
|
|
for line_no, line in enumerate(header):
|
2021-08-07 00:05:55 +02:00
|
|
|
for macro in macro_regex.finditer(line):
|
2021-08-09 13:45:51 +02:00
|
|
|
if macro.group("macro").startswith(exclusions):
|
|
|
|
continue
|
|
|
|
|
|
|
|
macros.append(Match(
|
|
|
|
header_file,
|
|
|
|
line,
|
2021-08-17 11:41:23 +02:00
|
|
|
line_no,
|
|
|
|
macro.span("macro"),
|
2021-08-09 13:45:51 +02:00
|
|
|
macro.group("macro")))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
return macros
|
|
|
|
|
2021-08-09 15:48:53 +02:00
|
|
|
def parse_mbed_words(self, include, exclude=None):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-07 01:42:54 +02:00
|
|
|
Parse all words in the file that begin with MBED, in and out of macros,
|
|
|
|
comments, anything.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
Args:
|
2021-08-09 15:48:53 +02:00
|
|
|
* include: A List of glob expressions to look for files through.
|
|
|
|
* exclude: A List of glob expressions for excluding files.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Returns a List of Match objects for words beginning with MBED.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-07 01:42:54 +02:00
|
|
|
# Typos of TLS are common, hence the broader check below than MBEDTLS.
|
2021-08-07 00:05:55 +02:00
|
|
|
mbed_regex = re.compile(r"\bMBED.+?_[A-Z0-9_]*")
|
|
|
|
exclusions = re.compile(r"// *no-check-names|#error")
|
|
|
|
|
2021-08-09 15:54:36 +02:00
|
|
|
files = self.get_files(include, exclude)
|
|
|
|
self.log.debug("Looking for MBED words in {} files".format(len(files)))
|
2021-08-07 00:05:55 +02:00
|
|
|
|
2021-08-09 15:54:36 +02:00
|
|
|
mbed_words = []
|
|
|
|
for filename in files:
|
2021-08-07 01:25:59 +02:00
|
|
|
with open(filename, "r", encoding="utf-8") as fp:
|
2021-08-06 18:54:58 +02:00
|
|
|
for line_no, line in enumerate(fp):
|
2021-08-07 00:05:55 +02:00
|
|
|
if exclusions.search(line):
|
2021-08-05 21:17:07 +02:00
|
|
|
continue
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
for name in mbed_regex.finditer(line):
|
|
|
|
mbed_words.append(Match(
|
2021-08-05 20:47:48 +02:00
|
|
|
filename,
|
|
|
|
line,
|
2021-08-17 11:41:23 +02:00
|
|
|
line_no,
|
|
|
|
name.span(0),
|
|
|
|
name.group(0)))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
return mbed_words
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-09 15:48:53 +02:00
|
|
|
def parse_enum_consts(self, include, exclude=None):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
Parse all enum value constants that are declared.
|
|
|
|
|
|
|
|
Args:
|
2021-08-09 15:48:53 +02:00
|
|
|
* include: A List of glob expressions to look for files through.
|
|
|
|
* exclude: A List of glob expressions for excluding files.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Returns a List of Match objects for the findings.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-09 15:54:36 +02:00
|
|
|
files = self.get_files(include, exclude)
|
|
|
|
self.log.debug("Looking for enum consts in {} files".format(len(files)))
|
2021-08-07 00:05:55 +02:00
|
|
|
|
2021-08-17 11:30:20 +02:00
|
|
|
# Emulate a finite state machine to parse enum declarations.
|
|
|
|
# OUTSIDE_KEYWORD = outside the enum keyword
|
|
|
|
# IN_BRACES = inside enum opening braces
|
|
|
|
# IN_BETWEEN = between enum keyword and opening braces
|
|
|
|
states = enum.Enum("FSM", ["OUTSIDE_KEYWORD", "IN_BRACES", "IN_BETWEEN"])
|
2021-08-09 15:54:36 +02:00
|
|
|
enum_consts = []
|
|
|
|
for header_file in files:
|
2021-08-17 11:30:20 +02:00
|
|
|
state = states.OUTSIDE_KEYWORD
|
2021-08-07 01:25:59 +02:00
|
|
|
with open(header_file, "r", encoding="utf-8") as header:
|
2021-08-06 18:54:58 +02:00
|
|
|
for line_no, line in enumerate(header):
|
2021-08-06 17:56:52 +02:00
|
|
|
# Match typedefs and brackets only when they are at the
|
|
|
|
# beginning of the line -- if they are indented, they might
|
|
|
|
# be sub-structures within structs, etc.
|
2021-08-17 11:30:20 +02:00
|
|
|
if (state == states.OUTSIDE_KEYWORD and
|
|
|
|
re.search(r"^(typedef +)?enum +{", line)):
|
|
|
|
state = states.IN_BRACES
|
|
|
|
elif (state == states.OUTSIDE_KEYWORD and
|
|
|
|
re.search(r"^(typedef +)?enum", line)):
|
|
|
|
state = states.IN_BETWEEN
|
|
|
|
elif (state == states.IN_BETWEEN and
|
|
|
|
re.search(r"^{", line)):
|
|
|
|
state = states.IN_BRACES
|
|
|
|
elif (state == states.IN_BRACES and
|
|
|
|
re.search(r"^}", line)):
|
|
|
|
state = states.OUTSIDE_KEYWORD
|
|
|
|
elif (state == states.IN_BRACES and
|
|
|
|
not re.search(r"^ *#", line)):
|
2021-08-16 12:34:10 +02:00
|
|
|
enum_const = re.search(r"^ *(?P<enum_const>\w+)", line)
|
2021-08-09 13:45:51 +02:00
|
|
|
if not enum_const:
|
|
|
|
continue
|
|
|
|
|
|
|
|
enum_consts.append(Match(
|
|
|
|
header_file,
|
|
|
|
line,
|
2021-08-17 11:41:23 +02:00
|
|
|
line_no,
|
|
|
|
enum_const.span("enum_const"),
|
2021-08-09 13:45:51 +02:00
|
|
|
enum_const.group("enum_const")))
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
return enum_consts
|
|
|
|
|
2021-08-09 15:48:53 +02:00
|
|
|
def parse_identifiers(self, include, exclude=None):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-16 11:37:24 +02:00
|
|
|
Parse all lines of a header where a function/enum/struct/union/typedef
|
2021-08-17 11:30:20 +02:00
|
|
|
identifier is declared, based on some regex and heuristics. Highly
|
|
|
|
dependent on formatting style.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
Args:
|
2021-08-09 15:48:53 +02:00
|
|
|
* include: A List of glob expressions to look for files through.
|
|
|
|
* exclude: A List of glob expressions for excluding files.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Returns a List of Match objects with identifiers.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-07 00:05:55 +02:00
|
|
|
identifier_regex = re.compile(
|
|
|
|
# Match " something(a" or " *something(a". Functions.
|
|
|
|
# Assumptions:
|
|
|
|
# - function definition from return type to one of its arguments is
|
2021-08-09 16:35:19 +02:00
|
|
|
# all on one line
|
2021-08-07 00:05:55 +02:00
|
|
|
# - function definition line only contains alphanumeric, asterisk,
|
|
|
|
# underscore, and open bracket
|
|
|
|
r".* \**(\w+) *\( *\w|"
|
2021-08-09 16:35:19 +02:00
|
|
|
# Match "(*something)(".
|
2021-08-07 00:05:55 +02:00
|
|
|
r".*\( *\* *(\w+) *\) *\(|"
|
|
|
|
# Match names of named data structures.
|
|
|
|
r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$|"
|
|
|
|
# Match names of typedef instances, after closing bracket.
|
2021-08-09 13:45:51 +02:00
|
|
|
r"}? *(\w+)[;[].*"
|
|
|
|
)
|
|
|
|
exclusion_lines = re.compile(
|
|
|
|
r"^("
|
2021-08-16 12:34:10 +02:00
|
|
|
r"extern +\"C\"|"
|
|
|
|
r"(typedef +)?(struct|union|enum)( *{)?$|"
|
|
|
|
r"} *;?$|"
|
|
|
|
r"$|"
|
|
|
|
r"//|"
|
|
|
|
r"#"
|
2021-08-09 13:45:51 +02:00
|
|
|
r")"
|
|
|
|
)
|
2021-08-07 00:05:55 +02:00
|
|
|
|
2021-08-09 15:54:36 +02:00
|
|
|
files = self.get_files(include, exclude)
|
|
|
|
self.log.debug("Looking for identifiers in {} files".format(len(files)))
|
|
|
|
|
|
|
|
identifiers = []
|
|
|
|
for header_file in files:
|
2021-08-07 01:25:59 +02:00
|
|
|
with open(header_file, "r", encoding="utf-8") as header:
|
2021-08-05 20:47:48 +02:00
|
|
|
in_block_comment = False
|
2021-08-09 16:35:19 +02:00
|
|
|
# The previous line variable is used for concatenating lines
|
2021-08-17 11:30:20 +02:00
|
|
|
# when identifiers are formatted and spread across multiple
|
|
|
|
# lines.
|
2021-08-07 00:05:55 +02:00
|
|
|
previous_line = ""
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 18:54:58 +02:00
|
|
|
for line_no, line in enumerate(header):
|
2021-08-06 17:22:06 +02:00
|
|
|
# Skip parsing this line if a block comment ends on it,
|
|
|
|
# but don't skip if it has just started -- there is a chance
|
|
|
|
# it ends on the same line.
|
2021-08-05 20:47:48 +02:00
|
|
|
if re.search(r"/\*", line):
|
2021-08-06 17:22:06 +02:00
|
|
|
in_block_comment = not in_block_comment
|
|
|
|
if re.search(r"\*/", line):
|
|
|
|
in_block_comment = not in_block_comment
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
if in_block_comment:
|
2021-08-07 00:05:55 +02:00
|
|
|
previous_line = ""
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-16 12:34:10 +02:00
|
|
|
if exclusion_lines.search(line):
|
2021-08-07 00:05:55 +02:00
|
|
|
previous_line = ""
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
|
|
|
|
2021-08-06 21:02:32 +02:00
|
|
|
# If the line contains only space-separated alphanumeric
|
|
|
|
# characters (or underscore, asterisk, or, open bracket),
|
|
|
|
# and nothing else, high chance it's a declaration that
|
|
|
|
# continues on the next line
|
2021-08-16 12:34:10 +02:00
|
|
|
if re.search(r"^([\w\*\(]+\s+)+$", line):
|
2021-08-07 00:05:55 +02:00
|
|
|
previous_line += line
|
2021-08-06 17:22:06 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
# If previous line seemed to start an unfinished declaration
|
2021-08-06 21:02:32 +02:00
|
|
|
# (as above), concat and treat them as one.
|
|
|
|
if previous_line:
|
2021-08-16 12:34:10 +02:00
|
|
|
line = previous_line.strip() + " " + line.strip() + "\n"
|
2021-08-07 00:05:55 +02:00
|
|
|
previous_line = ""
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-16 11:37:24 +02:00
|
|
|
# Skip parsing if line has a space in front = heuristic to
|
2021-08-06 17:22:06 +02:00
|
|
|
# skip function argument lines (highly subject to formatting
|
|
|
|
# changes)
|
|
|
|
if line[0] == " ":
|
2021-08-05 20:47:48 +02:00
|
|
|
continue
|
2021-08-05 22:07:14 +02:00
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
identifier = identifier_regex.search(line)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-09 13:45:51 +02:00
|
|
|
if not identifier:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Find the group that matched, and append it
|
|
|
|
for group in identifier.groups():
|
|
|
|
if not group:
|
|
|
|
continue
|
|
|
|
|
|
|
|
identifiers.append(Match(
|
|
|
|
header_file,
|
|
|
|
line,
|
2021-08-17 11:41:23 +02:00
|
|
|
line_no,
|
|
|
|
identifier.span(),
|
2021-08-09 13:45:51 +02:00
|
|
|
group))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
return identifiers
|
|
|
|
|
|
|
|
def parse_symbols(self):
|
|
|
|
"""
|
|
|
|
Compile the Mbed TLS libraries, and parse the TLS, Crypto, and x509
|
|
|
|
object files using nm to retrieve the list of referenced symbols.
|
2021-08-06 17:22:06 +02:00
|
|
|
Exceptions thrown here are rethrown because they would be critical
|
|
|
|
errors that void several tests, and thus needs to halt the program. This
|
|
|
|
is explicitly done for clarity.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Returns a List of unique symbols defined and used in the libraries.
|
|
|
|
"""
|
|
|
|
self.log.info("Compiling...")
|
2021-08-05 20:47:48 +02:00
|
|
|
symbols = []
|
|
|
|
|
|
|
|
# Back up the config and atomically compile with the full configratuion.
|
2021-08-09 13:45:51 +02:00
|
|
|
shutil.copy(
|
|
|
|
"include/mbedtls/mbedtls_config.h",
|
|
|
|
"include/mbedtls/mbedtls_config.h.bak"
|
|
|
|
)
|
2018-05-08 16:30:59 +02:00
|
|
|
try:
|
2021-08-06 17:22:06 +02:00
|
|
|
# Use check=True in all subprocess calls so that failures are raised
|
|
|
|
# as exceptions and logged.
|
2021-08-05 20:47:48 +02:00
|
|
|
subprocess.run(
|
2021-08-06 17:22:06 +02:00
|
|
|
["python3", "scripts/config.py", "full"],
|
2021-08-07 00:14:58 +02:00
|
|
|
universal_newlines=True,
|
2021-08-05 20:47:48 +02:00
|
|
|
check=True
|
2018-05-08 16:30:59 +02:00
|
|
|
)
|
|
|
|
my_environment = os.environ.copy()
|
|
|
|
my_environment["CFLAGS"] = "-fno-asynchronous-unwind-tables"
|
2021-08-17 11:48:22 +02:00
|
|
|
# Run make clean separately to lib to prevent unwanted behavior when
|
|
|
|
# make is invoked with parallelism.
|
2021-08-05 20:47:48 +02:00
|
|
|
subprocess.run(
|
2021-08-17 11:48:22 +02:00
|
|
|
["make", "clean"],
|
|
|
|
universal_newlines=True,
|
|
|
|
check=True
|
|
|
|
)
|
|
|
|
subprocess.run(
|
|
|
|
["make", "lib"],
|
2018-05-08 16:30:59 +02:00
|
|
|
env=my_environment,
|
2021-08-07 00:14:58 +02:00
|
|
|
universal_newlines=True,
|
2021-08-05 20:47:48 +02:00
|
|
|
stdout=subprocess.PIPE,
|
2018-05-08 16:30:59 +02:00
|
|
|
stderr=subprocess.STDOUT,
|
2021-08-05 20:47:48 +02:00
|
|
|
check=True
|
2018-05-08 16:30:59 +02:00
|
|
|
)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
# Perform object file analysis using nm
|
2021-08-09 13:45:51 +02:00
|
|
|
symbols = self.parse_symbols_from_nm([
|
|
|
|
"library/libmbedcrypto.a",
|
|
|
|
"library/libmbedtls.a",
|
|
|
|
"library/libmbedx509.a"
|
|
|
|
])
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
subprocess.run(
|
2018-05-08 16:30:59 +02:00
|
|
|
["make", "clean"],
|
2021-08-07 00:14:58 +02:00
|
|
|
universal_newlines=True,
|
2021-08-05 20:47:48 +02:00
|
|
|
check=True
|
2018-05-08 16:30:59 +02:00
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as error:
|
2021-08-06 22:27:59 +02:00
|
|
|
self.log.debug(error.output)
|
2021-08-06 17:22:06 +02:00
|
|
|
raise error
|
2021-08-05 20:47:48 +02:00
|
|
|
finally:
|
2021-08-07 18:28:23 +02:00
|
|
|
# Put back the original config regardless of there being errors.
|
|
|
|
# Works also for keyboard interrupts.
|
2021-08-09 13:45:51 +02:00
|
|
|
shutil.move(
|
|
|
|
"include/mbedtls/mbedtls_config.h.bak",
|
|
|
|
"include/mbedtls/mbedtls_config.h"
|
|
|
|
)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
return symbols
|
|
|
|
|
|
|
|
def parse_symbols_from_nm(self, object_files):
|
|
|
|
"""
|
|
|
|
Run nm to retrieve the list of referenced symbols in each object file.
|
|
|
|
Does not return the position data since it is of no use.
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Args:
|
2021-08-09 16:35:19 +02:00
|
|
|
* object_files: a List of compiled object filepaths to search through.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Returns a List of unique symbols defined and used in any of the object
|
|
|
|
files.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-07 00:05:55 +02:00
|
|
|
nm_undefined_regex = re.compile(r"^\S+: +U |^$|^\S+:$")
|
|
|
|
nm_valid_regex = re.compile(r"^\S+( [0-9A-Fa-f]+)* . _*(?P<symbol>\w+)")
|
2021-08-07 01:40:29 +02:00
|
|
|
exclusions = ("FStar", "Hacl")
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
symbols = []
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
# Gather all outputs of nm
|
2021-08-05 20:47:48 +02:00
|
|
|
nm_output = ""
|
|
|
|
for lib in object_files:
|
|
|
|
nm_output += subprocess.run(
|
|
|
|
["nm", "-og", lib],
|
2021-08-07 00:14:58 +02:00
|
|
|
universal_newlines=True,
|
2021-08-05 20:47:48 +02:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
check=True
|
|
|
|
).stdout
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
for line in nm_output.splitlines():
|
2021-08-16 12:34:10 +02:00
|
|
|
if not nm_undefined_regex.search(line):
|
|
|
|
symbol = nm_valid_regex.search(line)
|
2021-08-07 01:40:29 +02:00
|
|
|
if (symbol and not symbol.group("symbol").startswith(exclusions)):
|
2021-08-05 21:22:59 +02:00
|
|
|
symbols.append(symbol.group("symbol"))
|
2021-08-05 20:47:48 +02:00
|
|
|
else:
|
|
|
|
self.log.error(line)
|
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
return symbols
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
class NameChecker():
|
|
|
|
"""
|
|
|
|
Representation of the core name checking operation performed by this script.
|
|
|
|
"""
|
|
|
|
def __init__(self, parse_result, log):
|
|
|
|
self.parse_result = parse_result
|
|
|
|
self.log = log
|
|
|
|
|
2021-08-07 02:00:18 +02:00
|
|
|
def perform_checks(self, quiet=False):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-09 16:35:19 +02:00
|
|
|
A comprehensive checker that performs each check in order, and outputs
|
|
|
|
a final verdict.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Args:
|
2021-08-07 02:00:18 +02:00
|
|
|
* quiet: whether to hide detailed problem explanation.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-06 17:22:06 +02:00
|
|
|
self.log.info("=============")
|
2021-08-17 11:14:01 +02:00
|
|
|
Problem.quiet = quiet
|
2021-08-05 20:47:48 +02:00
|
|
|
problems = 0
|
2021-08-17 11:14:01 +02:00
|
|
|
problems += self.check_symbols_declared_in_header()
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-09 13:45:51 +02:00
|
|
|
pattern_checks = [
|
|
|
|
("macros", MACRO_PATTERN),
|
|
|
|
("enum_consts", CONSTANTS_PATTERN),
|
|
|
|
("identifiers", IDENTIFIER_PATTERN)
|
|
|
|
]
|
2021-08-05 20:47:48 +02:00
|
|
|
for group, check_pattern in pattern_checks:
|
2021-08-17 11:14:01 +02:00
|
|
|
problems += self.check_match_pattern(group, check_pattern)
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-17 11:14:01 +02:00
|
|
|
problems += self.check_for_typos()
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
self.log.info("=============")
|
|
|
|
if problems > 0:
|
|
|
|
self.log.info("FAIL: {0} problem(s) to fix".format(str(problems)))
|
2021-08-07 02:00:18 +02:00
|
|
|
if quiet:
|
|
|
|
self.log.info("Remove --quiet to see explanations.")
|
2021-08-07 18:18:28 +02:00
|
|
|
else:
|
|
|
|
self.log.info("Use --quiet for minimal output.")
|
2021-08-09 16:35:19 +02:00
|
|
|
return 1
|
2021-08-05 20:47:48 +02:00
|
|
|
else:
|
|
|
|
self.log.info("PASS")
|
2021-08-09 16:35:19 +02:00
|
|
|
return 0
|
2018-05-08 16:30:59 +02:00
|
|
|
|
2021-08-17 11:14:01 +02:00
|
|
|
def check_symbols_declared_in_header(self):
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
Perform a check that all detected symbols in the library object files
|
|
|
|
are properly declared in headers.
|
2021-08-09 12:56:15 +02:00
|
|
|
Assumes parse_names_in_source() was called before this.
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-06 17:22:06 +02:00
|
|
|
Returns the number of problems that need fixing.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
|
|
|
problems = []
|
2021-08-07 00:05:55 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
for symbol in self.parse_result["symbols"]:
|
|
|
|
found_symbol_declared = False
|
|
|
|
for identifier_match in self.parse_result["identifiers"]:
|
|
|
|
if symbol == identifier_match.name:
|
|
|
|
found_symbol_declared = True
|
|
|
|
break
|
2021-08-06 17:22:06 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
if not found_symbol_declared:
|
2021-08-09 13:45:51 +02:00
|
|
|
problems.append(SymbolNotInHeader(symbol))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-17 11:14:01 +02:00
|
|
|
self.output_check_result("All symbols in header", problems)
|
2021-08-05 20:47:48 +02:00
|
|
|
return len(problems)
|
|
|
|
|
2021-08-17 11:14:01 +02:00
|
|
|
def check_match_pattern(self, group_to_check, check_pattern):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
Perform a check that all items of a group conform to a regex pattern.
|
2021-08-09 12:56:15 +02:00
|
|
|
Assumes parse_names_in_source() was called before this.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
* group_to_check: string key to index into self.parse_result.
|
|
|
|
* check_pattern: the regex to check against.
|
|
|
|
|
|
|
|
Returns the number of problems that need fixing.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
problems = []
|
2021-08-07 00:05:55 +02:00
|
|
|
|
2021-08-05 20:47:48 +02:00
|
|
|
for item_match in self.parse_result[group_to_check]:
|
2021-08-16 12:34:10 +02:00
|
|
|
if not re.search(check_pattern, item_match.name):
|
2021-08-05 20:47:48 +02:00
|
|
|
problems.append(PatternMismatch(check_pattern, item_match))
|
2021-08-16 12:34:10 +02:00
|
|
|
# Double underscore should not be used for names
|
|
|
|
if re.search(r".*__.*", item_match.name):
|
2021-08-17 11:41:23 +02:00
|
|
|
problems.append(
|
|
|
|
PatternMismatch("no double underscore allowed", item_match))
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
self.output_check_result(
|
|
|
|
"Naming patterns of {}".format(group_to_check),
|
2021-08-07 02:00:18 +02:00
|
|
|
problems)
|
2021-08-05 20:47:48 +02:00
|
|
|
return len(problems)
|
2018-05-08 16:30:59 +02:00
|
|
|
|
2021-08-17 11:14:01 +02:00
|
|
|
def check_for_typos(self):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
Perform a check that all words in the soure code beginning with MBED are
|
|
|
|
either defined as macros, or as enum constants.
|
2021-08-09 12:56:15 +02:00
|
|
|
Assumes parse_names_in_source() was called before this.
|
2021-08-06 17:22:06 +02:00
|
|
|
|
|
|
|
Returns the number of problems that need fixing.
|
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
problems = []
|
|
|
|
|
2021-08-09 13:45:51 +02:00
|
|
|
# Set comprehension, equivalent to a list comprehension wrapped by set()
|
2021-08-07 00:05:55 +02:00
|
|
|
all_caps_names = {
|
|
|
|
match.name
|
|
|
|
for match
|
|
|
|
in self.parse_result["macros"] + self.parse_result["enum_consts"]}
|
|
|
|
typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$")
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
for name_match in self.parse_result["mbed_words"]:
|
2021-08-06 17:22:06 +02:00
|
|
|
found = name_match.name in all_caps_names
|
|
|
|
|
|
|
|
# Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the
|
|
|
|
# PSA driver, they will not exist as macros. However, they
|
|
|
|
# should still be checked for typos using the equivalent
|
|
|
|
# BUILTINs that exist.
|
|
|
|
if "MBEDTLS_PSA_ACCEL_" in name_match.name:
|
|
|
|
found = name_match.name.replace(
|
|
|
|
"MBEDTLS_PSA_ACCEL_",
|
|
|
|
"MBEDTLS_PSA_BUILTIN_") in all_caps_names
|
|
|
|
|
2021-08-07 00:05:55 +02:00
|
|
|
if not found and not typo_exclusion.search(name_match.name):
|
2021-08-09 13:45:51 +02:00
|
|
|
problems.append(Typo(name_match))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
2021-08-17 11:14:01 +02:00
|
|
|
self.output_check_result("Likely typos", problems)
|
2021-08-06 17:22:06 +02:00
|
|
|
return len(problems)
|
|
|
|
|
2021-08-17 11:14:01 +02:00
|
|
|
def output_check_result(self, name, problems):
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
|
|
|
Write out the PASS/FAIL status of a performed check depending on whether
|
|
|
|
there were problems.
|
2021-08-09 13:45:51 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
* name: the name of the test
|
|
|
|
* problems: a List of encountered Problems
|
2021-08-06 17:22:06 +02:00
|
|
|
"""
|
2021-08-05 20:47:48 +02:00
|
|
|
if problems:
|
2021-08-07 02:00:18 +02:00
|
|
|
self.log.info("{}: FAIL\n".format(name))
|
|
|
|
for problem in problems:
|
|
|
|
self.log.warning(str(problem))
|
2018-05-08 16:30:59 +02:00
|
|
|
else:
|
2021-08-06 17:22:06 +02:00
|
|
|
self.log.info("{}: PASS".format(name))
|
2021-08-05 20:47:48 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
2021-08-09 16:35:19 +02:00
|
|
|
Perform argument parsing, and create an instance of CodeParser and
|
|
|
|
NameChecker to begin the core operation.
|
2021-08-05 20:47:48 +02:00
|
|
|
"""
|
2021-08-09 14:56:36 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
2021-08-05 20:47:48 +02:00
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
description=(
|
|
|
|
"This script confirms that the naming of all symbols and identifiers "
|
|
|
|
"in Mbed TLS are consistent with the house style and are also "
|
|
|
|
"self-consistent.\n\n"
|
2021-08-09 14:56:36 +02:00
|
|
|
"Expected to be run from the MbedTLS root directory.")
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-v", "--verbose",
|
|
|
|
action="store_true",
|
|
|
|
help="show parse results"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-q", "--quiet",
|
|
|
|
action="store_true",
|
|
|
|
help="hide unnecessary text, explanations, and highlighs"
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2018-05-08 16:30:59 +02:00
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
# Configure the global logger, which is then passed to the classes below
|
|
|
|
log = logging.getLogger()
|
|
|
|
log.setLevel(logging.DEBUG if args.verbose else logging.INFO)
|
|
|
|
log.addHandler(logging.StreamHandler())
|
|
|
|
|
2018-05-08 16:30:59 +02:00
|
|
|
try:
|
2021-08-09 16:35:19 +02:00
|
|
|
code_parser = CodeParser(log)
|
|
|
|
parse_result = code_parser.comprehensive_parse()
|
2021-08-07 00:05:55 +02:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2018-05-08 16:30:59 +02:00
|
|
|
traceback.print_exc()
|
|
|
|
sys.exit(2)
|
|
|
|
|
2021-08-09 16:35:19 +02:00
|
|
|
name_checker = NameChecker(parse_result, log)
|
|
|
|
return_code = name_checker.perform_checks(quiet=args.quiet)
|
|
|
|
|
|
|
|
sys.exit(return_code)
|
|
|
|
|
2018-05-08 16:30:59 +02:00
|
|
|
if __name__ == "__main__":
|
2021-08-05 20:47:48 +02:00
|
|
|
main()
|