已合并
refactor: 统一头文件 include guard 为 #pragma once 并添加 CI lint 规则 #173
refactor: 统一头文件 include guard 为 #pragma once 并添加 CI lint 规则 #173
已合并
zhanghua创建于 6月16日
zhanghua
zhanghua成员
6月16日

描述

统一仓库中所有头文件的 include guard 风格为 #pragma once,解决当前 #ifndef/#define/#endif#pragma once 混用导致的风格不一致问题。

关联 Issue

关联 Issue #162

变更内容

1. 统一现有头文件(125 个文件,+456/-698)

类别 数量 说明
#ifndef#pragma once 215 个 传统宏保护替换为 #pragma once
补充 #pragma once 1 个 cgerc_kernel_impl.h 原先缺少 include guard
agent 模板同步 2 个 模板文件同步转换
净减少行数 242 行 456 行增加,698 行删除

2. 添加 CI lint 检查脚本

新增 scripts/ci/check_pragma_once.sh,用于检查 PR 中新增/修改的 .h/.hpp 文件是否使用 #pragma once

调用方式(与现有 scripts/ci/run_example.sh 一致,接收 PR 文件列表):

bash scripts/ci/check_pragma_once.sh <pr_filelist.txt>

pr_filelist.txt 格式为每行一个文件路径(相对仓库根目录),仅检查 .h/.hpp 文件,其他文件类型自动跳过。

检查规则

  • 必须使用 #pragma once,拒绝 #ifndef include guard 和无 guard 的文件
  • 自动跳过第三方目录(.opencode/asc-devkit/ 等)

⚠️ 需要维护者操作:接入 CI 流水线

本 PR 提供了 scripts/ci/check_pragma_once.sh 检查脚本,但尚未接入 CI 流水线。需要维护者在 GitCode 平台侧的流水线配置中新增一个步骤,在编译前调用该脚本。

建议的接入方式(参考现有 run_example.sh 的调用模式):

# CI 流水线中新增 lint 步骤(在编译步骤之前)
- name: check-pragma-once
  script: |
    # pr_filelist.txt 由平台自动生成,包含 PR 变更文件列表
    bash scripts/ci/check_pragma_once.sh pr_filelist.txt

在维护者接入流水线之前,该脚本也可通过以下方式手动使用:

  • 本地验证bash scripts/ci/check_pragma_once.sh <文件列表>
  • pre-commit hook:已集成到 .pre-commit-config.yaml(需开发者自行安装 pre-commit

关于 Issue 中提到的 3 个缺少 guard 的头文件

Issue #162 提到有 3 个头文件缺少标准 include guard。经逐 commit 追溯历史,原始 3 个文件为:

# 文件 当前状态
1 blas/gerc/cgerc/arch22/cgerc_kernel_impl.h 仍存在,本次已修复
2 blas/cgerc/cgerc_kernel_impl.h 已被删除(cgerc 目录重构,迁移至 blas/gerc/cgerc/arch22/
3 blas/common/kernel_launch/aclblas_kernel_do.h 已被删除(commit cc4d43c "del common kernel launch header")

第 2、3 个文件已在其他 PR 的重构过程中被删除,因此本次只需修复第 1 个文件。

未修改的 #ifndef

仓库中剩余约 114 处 #ifndef 均为条件编译(非 include guard),保持不变:

  • #ifndef __CCE_AICORE__ — 区分 Host/Device 编译目标
  • #ifndef TEST_DEVICE_ID — 测试设备 ID 默认值
  • #ifndef __force_inline__ — 编译器特性检测
  • #ifndef ACLBLAS_OPERATION_DECLARED — 枚举声明保护

这些是功能性的编译条件判断,不属于 include guard,不在本次修改范围内。

验证

  • 测试通过(bash build.sh --run --soc=ascend950,ops_blas + ops_blasLt 均成功)
    64c25ba46bac29e59e1321e4e20794de.png
  • 所有项目头文件(358 个)均已使用 #pragma once
  • 无遗漏的 include guard 宏
  • CI lint 脚本测试通过(正确拒绝 #ifndef guard,放行 #pragma once,仅检查 PR 变更文件)

类型标签

  • Bug 修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他:代码风格统一重构
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 zhanghua 的贡献)
zhanghuazhanghua成员
6月16日 关联了issue:[Requirement|需求建议]: 统一头文件 include guard 风格
CANN-robotCANN-robot成员
6月16日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
6月16日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/ops-blas 王子韬, 宋凯 (2/2) 宋凯, 王子韬 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

zhanghua145, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
zhanghuazhanghua成员
6月16日 修改了pull request 的描述
zhanghua
zhanghua成员
6月16日 评论:

compile

likedislike
此处折叠了43条消息 查看更多
CANN-robotCANN-robot成员
6月16日 添加了label:approved
songkai111成员
6月16日 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
6月16日 添加了label:lgtm
CANN-robotCANN-robot成员
6月16日 关闭了关联的issue
CANN-robotCANN-robot成员
6月16日 合入了pull request