From 0061d1595a19078edba05c66f69aa1f3ad68a7d1 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 25 Aug 2026 17:35:51 +0800 Subject: [PATCH] refactor(examples): replace file reading with hardcoded rounds value - Removed file reading logic from leibniz.cpp and hardcoded rounds as 100000000u + 2u - Removed fs module import and file reading from leibniz.js, replaced with constant 10000000 - Removed file reading code from pi.go and hardcoded rounds as 100000000 - Removed file reading from pi.php and replaced with hardcoded value 10000000 - Deleted rounds.txt file that contained the input value - Updated version numbers in project.yml from 0.1.0.1052/0.1.0 to 0.6.3.1112/0.6.3 - Removed package.json and package-lock.json files with node dependencies --- app.manifest | 52 ----- ast.py | 495 ------------------------------------------- examples/leibniz.cpp | 14 +- examples/leibniz.js | 3 +- examples/pi.go | 12 +- examples/pi.php | 2 +- package-lock.json | 116 ---------- package.json | 25 --- project.yml | 4 +- rounds.txt | 1 - 10 files changed, 8 insertions(+), 716 deletions(-) delete mode 100644 app.manifest delete mode 100644 ast.py delete mode 100644 package-lock.json delete mode 100644 package.json delete mode 100644 rounds.txt diff --git a/app.manifest b/app.manifest deleted file mode 100644 index 2c598dbb..00000000 --- a/app.manifest +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - PerMonitorV2 - - - - diff --git a/ast.py b/ast.py deleted file mode 100644 index b3a53ca2..00000000 --- a/ast.py +++ /dev/null @@ -1,495 +0,0 @@ -#!/usr/bin/env python3 -""" -使用 libclang 解析 C++ 代码并添加头文件路径 -""" - -import clang.cindex -from clang.cindex import Index, CursorKind, TypeKind, StorageClass -import json -import sys -import os -from pathlib import Path -import subprocess -import argparse - - -class PHPConfigHelper: - """PHP 配置辅助类""" - - def __init__(self, php_config_path='php-config'): - self.php_config = php_config_path - self._check_availability() - - def _check_availability(self): - """检查 php-config 是否可用""" - try: - result = subprocess.run( - [self.php_config, '--version'], - capture_output=True, - text=True, - check=True - ) - print(f"✓ 找到 PHP {result.stdout.strip()}") - except (FileNotFoundError, subprocess.CalledProcessError) as e: - print(f"警告: php-config 不可用: {e}") - # 尝试使用常见路径 - common_paths = [ - '/usr/bin/php-config', - '/usr/local/bin/php-config', - '/opt/php/bin/php-config' - ] - for path in common_paths: - if os.path.exists(path): - self.php_config = path - try: - result = subprocess.run( - [self.php_config, '--version'], - capture_output=True, - text=True, - check=True - ) - print(f"✓ 找到 PHP {result.stdout.strip()} 在 {path}") - return - except (FileNotFoundError, subprocess.CalledProcessError): - continue - print("警告: php-config 在任何常见路径都不可用,使用默认路径") - - def get_includes(self): - """ - 获取 include 路径列表 - - Returns: - list: 头文件路径列表(不带 -I 前缀) - """ - try: - result = subprocess.run( - [self.php_config, '--includes'], - capture_output=True, - text=True, - check=True - ) - - # 解析输出: "-I/path1 -I/path2" -> ['/path1', '/path2'] - includes = [] - for flag in result.stdout.strip().split(): - if flag.startswith('-I'): - includes.append(flag[2:]) - - return includes - except (subprocess.CalledProcessError, FileNotFoundError): - print("警告: 无法获取 PHP includes,使用默认路径") - return ['/usr/include/php', '/usr/include/php/20210902'] # 默认路径 - - def get_include_dir(self): - """获取主 include 目录""" - try: - result = subprocess.run( - [self.php_config, '--include-dir'], - capture_output=True, - text=True, - check=True - ) - return result.stdout.strip() - except (subprocess.CalledProcessError, FileNotFoundError): - return '/usr/include/php' - - def get_extension_dir(self): - """获取扩展目录""" - try: - result = subprocess.run( - [self.php_config, '--extension-dir'], - capture_output=True, - text=True, - check=True - ) - return result.stdout.strip() - except (subprocess.CalledProcessError, FileNotFoundError): - return '/usr/lib/php' - - def get_version(self): - """获取 PHP 版本""" - try: - result = subprocess.run( - [self.php_config, '--version'], - capture_output=True, - text=True, - check=True - ) - return result.stdout.strip() - except (subprocess.CalledProcessError, FileNotFoundError): - return 'unknown' - - def get_php_binary(self): - """获取 PHP 二进制路径""" - try: - result = subprocess.run( - [self.php_config, '--php-binary'], - capture_output=True, - text=True, - check=True - ) - return result.stdout.strip() - except (subprocess.CalledProcessError, FileNotFoundError): - return 'php' - - def get_configure_options(self): - """获取配置选项""" - try: - result = subprocess.run( - [self.php_config, '--configure-options'], - capture_output=True, - text=True, - check=True - ) - return result.stdout.strip() - except (subprocess.CalledProcessError, FileNotFoundError): - return '' - - def get_all_info(self): - """获取所有配置信息""" - return { - 'version': self.get_version(), - 'includes': self.get_includes(), - 'include_dir': self.get_include_dir(), - 'extension_dir': self.get_extension_dir(), - 'php_binary': self.get_php_binary(), - 'configure_options': self.get_configure_options(), - } - - -class ClangParser: - def __init__(self, libclang_path=None): - """ - 初始化 Clang 解析器 - - Args: - libclang_path: libclang 库的路径(可选) - """ - if libclang_path: - try: - clang.cindex.Config.set_library_file(libclang_path) - except Exception as e: - print(f"警告: 无法设置 libclang 路径 {libclang_path}: {e}") - print("尝试使用默认路径...") - - try: - self.index = Index.create() - except Exception as e: - print(f"错误: 无法创建 Clang 索引: {e}") - print("请确保已安装 python3-clang 和 clang 库") - raise - - def parse_file(self, filename, include_paths=None, defines=None, - compiler_args=None, language='c++'): - """ - 解析 C++ 文件 - - Args: - filename: 要解析的文件路径 - include_paths: 头文件搜索路径列表 - defines: 宏定义列表 ['MACRO=value', 'DEBUG'] - compiler_args: 额外的编译器参数 - language: 语言类型 ('c', 'c++', 'objective-c') - - Returns: - TranslationUnit 对象 - """ - if not os.path.exists(filename): - raise FileNotFoundError(f"文件不存在: {filename}") - - args = [] - - # 1. 设置语言标准 - if language == 'c++': - args.extend([ - '-x', 'c++', - '-std=c++14', # 更标准的 C++ 版本 - ]) - elif language == 'c': - args.extend(['-x', 'c', '-std=c11']) - - # 2. 添加头文件搜索路径 - if include_paths: - for path in include_paths: - if os.path.exists(path): # 检查路径是否存在 - args.append(f'-I{path}') - else: - print(f"警告: 包含路径不存在: {path}") - - # 3. 添加宏定义 - if defines: - for define in defines: - args.append(f'-D{define}') - - # 4. 添加额外的编译器参数 - if compiler_args: - args.extend(compiler_args) - - # 5. 常用的编译选项 - args.extend([ - '-Wno-pragma-once-outside-header', # 忽略警告 - '-ferror-limit=0', # 不限制错误数量 - '-fno-delayed-template-parsing', # 避免某些 C++ 模板解析问题 - '-w', # 禁用所有警告以减少输出 - ]) - - print(f"编译参数: {' '.join(args)}") - - # 解析文件 - try: - tu = self.index.parse( - filename, - args=args, - options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD - ) - except Exception as e: - print(f"解析文件时出错: {e}") - print("尝试使用最小参数集...") - # 尝试使用最小参数集 - minimal_args = ['-x', 'c++', '-std=c++14', '-w'] - if include_paths: - for path in include_paths: - if os.path.exists(path): - minimal_args.append(f'-I{path}') - try: - tu = self.index.parse( - filename, - args=minimal_args, - options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD - ) - print("使用最小参数集成功解析") - except Exception as e2: - print(f"使用最小参数集也失败: {e2}") - print("尝试解析不包含头文件的简化版本...") - # 创建一个临时文件,移除头文件包含行 - temp_filename = filename + ".tmp" - with open(filename, 'r') as original: - lines = original.readlines() - - # 移除 #include 行 - filtered_lines = [line for line in lines if not line.strip().startswith('#include')] - - with open(temp_filename, 'w') as temp: - temp.writelines(filtered_lines) - - try: - tu = self.index.parse( - temp_filename, - args=minimal_args, - options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD - ) - print("解析简化版本成功") - # 清理临时文件 - os.remove(temp_filename) - except Exception as e3: - print(f"简化版本也失败: {e3}") - # 清理临时文件 - if os.path.exists(temp_filename): - os.remove(temp_filename) - raise - - # 检查诊断信息 - if tu.diagnostics: - print(f"\n诊断信息 ({len(tu.diagnostics)} 个):") - error_count = 0 - warning_count = 0 - - for diag in tu.diagnostics: - if diag.severity >= 3: # 错误级别 - error_count += 1 - else: # 警告级别 - warning_count += 1 - - print(f"错误: {error_count}, 警告: {warning_count}") - - # 只显示前几个诊断信息,避免输出过多 - for i, diag in enumerate(tu.diagnostics): - if i >= 5: # 只显示前5个 - print("... 还有更多诊断信息") - break - print(f" [{diag.severity}] {diag.spelling}") - if diag.location.file: - print(f" at {diag.location.file.name}:{diag.location.line}") - - return tu - - def extract_functions(self, tu, name_prefixes=None): - """ - 提取函数定义 - - Args: - tu: TranslationUnit 对象 - name_prefixes: 函数名前缀过滤列表 - - Returns: - 函数信息列表 - """ - functions = [] - - def visit_node(node, depth=0): - # 只处理函数声明/定义 - if node.kind == CursorKind.FUNCTION_DECL: - try: - func_info = self.parse_function(node) - - # 过滤函数名 - if name_prefixes: - if any(func_info['name'].startswith(prefix) - for prefix in name_prefixes): - functions.append(func_info) - else: - functions.append(func_info) - except Exception as e: - print(f"解析函数时出错: {e}") - - # 递归访问子节点 - for child in node.get_children(): - visit_node(child, depth + 1) - - visit_node(tu.cursor) - return functions - - def parse_function(self, cursor): - """ - 解析函数详细信息 - """ - # 检查方法是否存在 - def safe_call(method, default_value=None): - try: - return method() - except AttributeError: - return default_value - - # 基本信息 - func_info = { - 'name': cursor.spelling, - 'displayName': cursor.displayname, - 'mangledName': cursor.mangled_name, - 'returnType': cursor.result_type.spelling, - 'isStatic': cursor.storage_class == StorageClass.STATIC, - 'isInline': safe_call(lambda: cursor.is_inline_function(), False), - 'isVirtual': safe_call(lambda: cursor.is_virtual_method(), False), - 'isConst': safe_call(lambda: cursor.is_const_method(), False), - 'location': { - 'file': str(cursor.location.file) if cursor.location.file else None, - 'line': cursor.location.line, - 'column': cursor.location.column, - }, - 'parameters': [], - 'namespaces': self.get_namespaces(cursor), - } - - # 解析参数 - for arg in cursor.get_arguments(): - param_info = { - 'name': arg.spelling or f'arg{len(func_info["parameters"])}', - 'type': arg.type.spelling, - 'canonicalType': arg.type.get_canonical().spelling, - } - - # 检查是否有默认值 - try: - for token in arg.get_tokens(): - if token.spelling == '=': - # 有默认值 - param_info['hasDefault'] = True - break - except: - # 如果无法获取 tokens,跳过默认值检查 - pass - - func_info['parameters'].append(param_info) - - return func_info - - def get_namespaces(self, cursor): - """ - 获取函数所在的命名空间 - """ - namespaces = [] - parent = cursor.semantic_parent - - while parent and parent.kind != CursorKind.TRANSLATION_UNIT: - if parent.kind == CursorKind.NAMESPACE: - namespaces.insert(0, parent.spelling) - parent = parent.semantic_parent - - return namespaces - - -def main(): - parser = argparse.ArgumentParser(description='使用 libclang 解析 C++ 代码并提取函数信息') - parser.add_argument('filename', help='要解析的 C++ 文件路径') - parser.add_argument('--libclang-path', help='libclang 库路径') - parser.add_argument('--include-paths', nargs='*', help='额外的包含路径') - parser.add_argument('--function-prefixes', nargs='*', help='函数名前缀过滤器') - - args = parser.parse_args() - - if not os.path.exists(args.filename): - print(f"错误: 文件不存在: {args.filename}") - sys.exit(1) - - try: - # 创建解析器 - parser_obj = ClangParser(libclang_path=args.libclang_path) - - # 配置头文件路径 - include_paths = args.include_paths or [ - "/usr/include/linux", - "/home/swoole/workspace/projects/phpx/include" - ] - - # 尝试获取 PHP 配置的头文件路径 - try: - php_config = PHPConfigHelper() - php_includes = php_config.get_includes() - include_paths.extend(php_includes) - except Exception as e: - print(f"警告: 无法获取 PHP 配置: {e}") - print("继续使用默认路径...") - - # 配置宏定义 - defines = [ - 'HAVE_CONFIG_H', - 'ZEND_ENABLE_STATIC_TSRMLS_CACHE=1', - ] - - # 额外的编译器参数 - compiler_args = [ - '-fparse-all-comments', # 解析所有注释 - '-Wno-unknown-pragmas', - ] - - # 解析文件 - tu = parser_obj.parse_file( - args.filename, - include_paths=include_paths, - defines=defines, - compiler_args=compiler_args, - language='c++' - ) - - # 提取函数 - name_prefixes = args.function_prefixes or None - functions = parser_obj.extract_functions(tu, name_prefixes=name_prefixes) - - # 输出结果 - output = { - 'file': args.filename, - 'functions': functions, - 'total': len(functions), - } - - print(json.dumps(output, indent=2, ensure_ascii=False)) - - except clang.cindex.TranslationUnitLoadError as e: - print(f"翻译单元加载错误: {e}") - print("这通常意味着 C++ 代码包含语法错误或缺少必要的头文件") - sys.exit(1) - except Exception as e: - print(f"错误: {e}") - sys.exit(1) - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/examples/leibniz.cpp b/examples/leibniz.cpp index 4db17953..16f9926f 100644 --- a/examples/leibniz.cpp +++ b/examples/leibniz.cpp @@ -1,23 +1,11 @@ #include #include -unsigned rounds; double pi = 1.0; int main() { - auto infile = std::fopen("rounds.txt", "r"); // open file - if (infile == NULL) { - perror("open file"); - return EXIT_FAILURE; - } - if (std::fscanf(infile, "%u", &rounds) != 1) { // read from file - perror("read file"); - return EXIT_FAILURE; - } - std::fclose(infile); // close file - - rounds += 2u; // do this outside the loop + const unsigned rounds = 100000000u + 2u; // rounds + 2, moved out of the loop for (long i=2u ; i < rounds ; ++i) // use ++i instead of i++ { diff --git a/examples/leibniz.js b/examples/leibniz.js index b1855a26..45af905e 100644 --- a/examples/leibniz.js +++ b/examples/leibniz.js @@ -1,7 +1,6 @@ "use strict"; -let fs = require("fs"); -let rounds = parseInt(fs.readFileSync("./rounds.txt", "utf8")); +const rounds = 100000000; let x = 1.0; let pi = 1.0; diff --git a/examples/pi.go b/examples/pi.go index 55c9d2f8..585b8940 100644 --- a/examples/pi.go +++ b/examples/pi.go @@ -1,15 +1,9 @@ package main -import ( - "fmt" - "os" - "strconv" - "strings" -) +import "fmt" func main() { - file, _ := os.ReadFile("rounds.txt") - rounds, _ := strconv.Atoi(strings.TrimSpace(string(file))) + const rounds = 100000000 x := 1.0 pi := 1.0 @@ -22,4 +16,4 @@ func main() { pi *= 4.0 fmt.Println(pi) -} \ No newline at end of file +} diff --git a/examples/pi.php b/examples/pi.php index 363831b6..d3de90dc 100644 --- a/examples/pi.php +++ b/examples/pi.php @@ -4,7 +4,7 @@ use native_types; function main() { ini_set("precision", 17); - $rounds = (int) file_get_contents("./rounds.txt", true); + $rounds = 100000000; $stop = $rounds + 2; var_dump($stop); diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 9fc652a7..00000000 --- a/package-lock.json +++ /dev/null @@ -1,116 +0,0 @@ -{ - "name": "typephp-compiler", - "version": "1.0.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "typephp-compiler", - "version": "1.0.0", - "license": "GPL-3.0-or-later", - "dependencies": { - "sharp": "^0.34.5" - } - }, - "node_modules/@img/colour": { - "version": "1.1.0", - "license": "MIT", - "engines": { - "node": ">=18" - } - }, - "node_modules/@img/sharp-libvips-linux-x64": { - "version": "1.2.4", - "cpu": [ - "x64" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@img/sharp-linux-x64": { - "version": "0.34.5", - "cpu": [ - "x64" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linux-x64": "1.2.4" - } - }, - "node_modules/detect-libc": { - "version": "2.1.2", - "license": "Apache-2.0", - "engines": { - "node": ">=8" - } - }, - "node_modules/semver": { - "version": "7.8.4", - "license": "ISC", - "bin": { - "semver": "bin/semver.js" - }, - "engines": { - "node": ">=10" - } - }, - "node_modules/sharp": { - "version": "0.34.5", - "hasInstallScript": true, - "license": "Apache-2.0", - "dependencies": { - "@img/colour": "^1.0.0", - "detect-libc": "^2.1.2", - "semver": "^7.7.3" - }, - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-darwin-arm64": "0.34.5", - "@img/sharp-darwin-x64": "0.34.5", - "@img/sharp-libvips-darwin-arm64": "1.2.4", - "@img/sharp-libvips-darwin-x64": "1.2.4", - "@img/sharp-libvips-linux-arm": "1.2.4", - "@img/sharp-libvips-linux-arm64": "1.2.4", - "@img/sharp-libvips-linux-ppc64": "1.2.4", - "@img/sharp-libvips-linux-riscv64": "1.2.4", - "@img/sharp-libvips-linux-s390x": "1.2.4", - "@img/sharp-libvips-linux-x64": "1.2.4", - "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", - "@img/sharp-libvips-linuxmusl-x64": "1.2.4", - "@img/sharp-linux-arm": "0.34.5", - "@img/sharp-linux-arm64": "0.34.5", - "@img/sharp-linux-ppc64": "0.34.5", - "@img/sharp-linux-riscv64": "0.34.5", - "@img/sharp-linux-s390x": "0.34.5", - "@img/sharp-linux-x64": "0.34.5", - "@img/sharp-linuxmusl-arm64": "0.34.5", - "@img/sharp-linuxmusl-x64": "0.34.5", - "@img/sharp-wasm32": "0.34.5", - "@img/sharp-win32-arm64": "0.34.5", - "@img/sharp-win32-ia32": "0.34.5", - "@img/sharp-win32-x64": "0.34.5" - } - } - } -} diff --git a/package.json b/package.json deleted file mode 100644 index c7697082..00000000 --- a/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "typephp-compiler", - "version": "1.0.0", - "description": "- 需要 PHP-8.2 以上版本 - 需要 GCC-9 以上版本,支持 C++17 标准 - 需要 CMake-3.24 以上版本", - "main": "index.js", - "directories": { - "doc": "docs", - "example": "examples", - "test": "tests" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "git@git.code-galaxy.net:aot/compiler.git" - }, - "keywords": [], - "author": "", - "license": "GPL-3.0-or-later", - "type": "commonjs", - "dependencies": { - "sharp": "^0.34.5" - } -} diff --git a/project.yml b/project.yml index 18a7add3..c2d43266 100644 --- a/project.yml +++ b/project.yml @@ -12,8 +12,8 @@ resource: icon: swoole-logo.ico # 版本信息 version-info: - file-version: 0.1.0.1052 - product-version: 0.1.0 + file-version: 0.6.3.1112 + product-version: 0.6.3 company-name: "上海识沃网络科技有限公司" file-description: "TypePHP Compiler" internal-name: "typephp" diff --git a/rounds.txt b/rounds.txt deleted file mode 100644 index ea600cb6..00000000 --- a/rounds.txt +++ /dev/null @@ -1 +0,0 @@ -100000000