SDL
261 строка · 5.8 Кб
1#!/usr/bin/env python3
2#
3# Simple DirectMedia Layer
4# Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
5#
6# This software is provided 'as-is', without any express or implied
7# warranty. In no event will the authors be held liable for any damages
8# arising from the use of this software.
9#
10# Permission is granted to anyone to use this software for any purpose,
11# including commercial applications, and to alter it and redistribute it
12# freely, subject to the following restrictions:
13#
14# 1. The origin of this software must not be misrepresented; you must not
15# claim that you wrote the original software. If you use this software
16# in a product, an acknowledgment in the product documentation would be
17# appreciated but is not required.
18# 2. Altered source versions must be plainly marked as such, and must not be
19# misrepresented as being the original software.
20# 3. This notice may not be removed or altered from any source distribution.
21#
22# This script detects use of stdlib function in SDL code
23
24import argparse25import os26import pathlib27import re28import sys29
30SDL_ROOT = pathlib.Path(__file__).resolve().parents[1]31
32words = [33'abs',34'acos',35'acosf',36'asin',37'asinf',38'asprintf',39'atan',40'atan2',41'atan2f',42'atanf',43'atof',44'atoi',45'bsearch',46'calloc',47'ceil',48'ceilf',49'copysign',50'copysignf',51'cos',52'cosf',53'crc32',54'exp',55'expf',56'fabs',57'fabsf',58'floor',59'floorf',60'fmod',61'fmodf',62'free',63'getenv',64'isalnum',65'isalpha',66'isblank',67'iscntrl',68'isdigit',69'isgraph',70'islower',71'isprint',72'ispunct',73'isspace',74'isupper',75'isxdigit',76'itoa',77'lltoa',78'log10',79'log10f',80'logf',81'lround',82'lroundf',83'ltoa',84'malloc',85'memalign',86'memcmp',87'memcpy',88'memcpy4',89'memmove',90'memset',91'pow',92'powf',93'qsort',94'qsort_r',95'qsort_s',96'realloc',97'round',98'roundf',99'scalbn',100'scalbnf',101'setenv',102'sin',103'sinf',104'snprintf',105'sqrt',106'sqrtf',107'sscanf',108'strcasecmp',109'strchr',110'strcmp',111'strdup',112'strlcat',113'strlcpy',114'strlen',115'strlwr',116'strncasecmp',117'strncmp',118'strrchr',119'strrev',120'strstr',121'strtod',122'strtokr',123'strtol',124'strtoll',125'strtoul',126'strupr',127'tan',128'tanf',129'tolower',130'toupper',131'trunc',132'truncf',133'uitoa',134'ulltoa',135'ultoa',136'utf8strlcpy',137'utf8strlen',138'vasprintf',139'vsnprintf',140'vsscanf',141'wcscasecmp',142'wcscmp',143'wcsdup',144'wcslcat',145'wcslcpy',146'wcslen',147'wcsncasecmp',148'wcsncmp',149'wcsstr' ]150
151
152reg_comment_remove_content = re.compile('\/\*.*\*/')153reg_comment_remove_content2 = re.compile('".*"')154reg_comment_remove_content3 = re.compile(':strlen')155reg_comment_remove_content4 = re.compile('->free')156
157def find_symbols_in_file(file, regex):158
159allowed_extensions = [ ".c", ".cpp", ".m", ".h", ".hpp", ".cc" ]160
161excluded_paths = [162"src/stdlib",163"src/libm",164"src/hidapi",165"src/video/khronos",166"include/SDL3",167"build-scripts/gen_audio_resampler_filter.c",168"build-scripts/gen_audio_channel_conversion.c" ]169
170filename = pathlib.Path(file)171
172for ep in excluded_paths:173if ep in filename.as_posix():174# skip175return176
177if filename.suffix not in allowed_extensions:178# skip179return180
181# print("Parse %s" % file)182
183try:184with file.open("r", encoding="UTF-8", newline="") as rfp:185parsing_comment = False186for l in rfp:187l = l.strip()188
189# Get the comment block /* ... */ across several lines190match_start = "/*" in l191match_end = "*/" in l192if match_start and match_end:193continue194if match_start:195parsing_comment = True196continue197if match_end:198parsing_comment = False199continue200if parsing_comment:201continue202
203if regex.match(l):204
205# free() allowed here206if "This should NOT be SDL_" in l:207continue208
209# double check210# Remove one line comment /* ... */211# eg: extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path, int bExclusive /* = false */);212l = reg_comment_remove_content.sub('', l)213
214# Remove strings " ... "215l = reg_comment_remove_content2.sub('', l)216
217# :strlen218l = reg_comment_remove_content3.sub('', l)219
220# ->free221l = reg_comment_remove_content4.sub('', l)222
223if regex.match(l):224print("File %s" % filename)225print(" %s" % l)226print("")227
228except UnicodeDecodeError:229print("%s is not text, skipping" % file)230except Exception as err:231print("%s" % err)232
233def find_symbols_in_dir(path, regex):234
235for entry in path.glob("*"):236if entry.is_dir():237find_symbols_in_dir(entry, regex)238else:239find_symbols_in_file(entry, regex)240
241def main():242str = ".*\\b("243for w in words:244str += w + "|"245str = str[:-1]246str += ")\("247regex = re.compile(str)248find_symbols_in_dir(SDL_ROOT, regex)249
250if __name__ == "__main__":251
252parser = argparse.ArgumentParser(fromfile_prefix_chars='@')253args = parser.parse_args()254
255try:256main()257except Exception as e:258print(e)259exit(-1)260
261exit(0)262
263
264