From 20436c5e4104801f9be9f53b8b69ab27b36aae11 Mon Sep 17 00:00:00 2001 From: rango Date: Thu, 30 Apr 2026 17:49:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=20MSVC=20?= =?UTF-8?q?=E8=AD=A6=E5=91=8A=E5=B1=8F=E8=94=BD=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将硬编码的警告屏蔽改为从 Constants 配置中动态读取 - 添加 MSVC_SUPPRESSED_WARNINGS 常量数组管理所有警告代码 - 扩展警告屏蔽范围,涵盖 Windows SDK 和 PHP SDK 的常见警告 - 在示例项目中添加 MSVC 警告屏蔽说明文档链接 --- examples/win32-hello/README.md | 1 + src/Php/CompilerBase.php | 8 +++++--- src/Php/Constants.php | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/examples/win32-hello/README.md b/examples/win32-hello/README.md index e6ddaa8b..957d89d8 100644 --- a/examples/win32-hello/README.md +++ b/examples/win32-hello/README.md @@ -326,6 +326,7 @@ win32-hello/ ├── DEBUG_GUIDE.md # 调试指南(重要!) ├── DEBUG_MODE_GUIDE.md # 调试模式使用指南 ├── SANITIZER_GUIDE.md # AddressSanitizer 使用指南 +├── MSVC_WARNINGS_SUPPRESSION.md # MSVC 警告屏蔽说明 └── README.md # 本文件 ``` diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 6a692090..5836f987 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2439,9 +2439,11 @@ class CompilerBase extends \PhpAot\Core\Translator // 警告级别 $cmd .= ' /W3'; - // 禁用 PHP SDK 头文件中的常见警告 - $cmd .= ' /wd4244'; // 禁用类型转换警告(__int64 到 int) - $cmd .= ' /wd4146'; // 禁用一元负运算符应用于无符号类型的警告 + // 禁用 PHP SDK 和 Windows SDK 头文件中的常见警告 + // 这些警告都是编译器噪音,不影响功能(从 Constants 配置中读取) + foreach (Constants::MSVC_SUPPRESSED_WARNINGS as $code => $description) { + $cmd .= " /wd{$code}"; // C{$code}: {$description} + } // 启用 C++ 异常处理(消除 C4530 警告) $cmd .= ' /EHsc'; diff --git a/src/Php/Constants.php b/src/Php/Constants.php index d22da58b..f90f2f89 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -150,4 +150,26 @@ class Constants 'defaultValue' => '', ], ]; + + /** + * MSVC 编译器警告屏蔽列表 + * 这些警告来自 Windows SDK 和 PHP SDK 头文件,都是编译器噪音,不影响功能 + * + * @var array 键为警告编号,值为说明 + */ + public const array MSVC_SUPPRESSED_WARNINGS = [ + '4244' => '类型转换可能丢失数据 (int -> smaller type)', + '4242' => '类型转换可能丢失数据 (similar to C4244)', + '4146' => '一元负运算符应用于无符号类型', + '4820' => '结构体成员后有填充字节(内存对齐)', + '4464' => '相对包含路径含 ".."', + '4365' => '有符号/无符号转换', + '4127' => '条件表达式是常量(如 while(1))', + '4668' => '未定义的宏当 0 处理(#ifdef __GNUC__)', + '4626' => '赋值运算符被隐式删除(const 成员)', + '5027' => '移动赋值运算符被隐式删除', + '5219' => '隐式转换警告', + '5220' => 'volatile 成员警告', + '4100' => '未使用的参数', + ]; }