概述: Doxygen 注释及使用C++版

参考文章:

这里展示一些常用到的 Doxygen 注释模板(文件头 / 函数头 / 类 / 结构体 / 成员批注)

0x01 文件头

C++cpp
/**
 * @file 文件名
 * @brief 简介
 * @details 细节
 * @mainpage 工程概览
 * @author 作者
 * @email 邮箱
 * @version 版本号
 * @date 年-月-日
 * @license 版权
 */

0x02 函数头

C++cpp
/**
 * @brief 函数简介
 * @details 详细说明
 * 
 * @param[in]  形参 参数说明(输入参数)
 * @param[out] 形参 参数说明(输出参数)
 * @param[in,out] 形参 参数说明(既输入又输出)
 * @return 返回说明
 *   @retval 返回值说明
 * @note 注解
 * @attention 注意
 * @warning 警告
 * @exception 异常
 */

中文实战示例(对齐团队 C++/MFC 代码风格,描述用中文、方向标记齐全):

C++cpp
/**
 * @brief 批量注入探针 DLL 到目标进程
 * @details 解析 PID 输入并逐目标注入;每个目标独立记录成功/失败日志,
 *          全部成功才返回 true。单个目标的失败不影响其余目标的注入。
 * @param[in]     sTargets 目标 PID 串,支持“;,,空白”分隔的批量写法
 * @param[in]     eWay     注入方式(服务注入 / 注入后启动 Work 等)
 * @param[in]     sDllPath x86 探针 DLL 路径;x64 目标自动换用 x64 版本
 * @param[out]    psErrMsg 失败原因聚合输出,全部成功时为空串,可为 NULL
 * @return true  全部目标注入成功
 * @return false 存在失败目标或未解析到有效 PID(具体原因见 psErrMsg)
 * @note 注入成功不代表功能生效,需结合 RPC 心跳/日志确认探针已工作
 */

0x02.1 注释风格选型(Doxygen 多格式)

Doxygen 同时识别以下格式,跟随各代码库既有风格选型,同一文件内不混用两种块风格:

| 风格 | 形式 | 说明 |
| :--- | :--- | :--- |
| JavaDoc 块 | /** ... */ | 多行详细说明(@brief/@details/@param 全套),适合复杂函数;PhpProbe/IRTool 在用 |
| JavaDoc 单行 | /// 简述 | 连续 /// 行合并为函数描述;Dnrsp 系在用,且带 XML 标签 |
| Qt 块 | /*! ... */ | 与 JavaDoc 块等价 |
| Qt 单行 | //! 简述 | 与 /// 等价 |
| 成员后置 | /**< ... */、///< ... */ | 字段/成员行尾批注(见 0x04) |

Dnrsp 实战样例(/// + XML 标签:C++ 头文件与 C# 侧写法统一,VS IntelliSense 与 Doxygen 双兼容):

C++cpp
/// <summary>
/// 校验 hook 配置(黑白名单统一校验接口)
/// </summary>
/// <param name="hookName">hook 标识名(如 "MyCopyStringToBytes"),指定要查询哪个 hook 的配置;为 NULL 返回 -1</param>
/// <param name="checkType">校验类型(CHECK_TYPE 枚举):
///   CHECK_URL_WHITELIST / CHECK_KEYWORD_WHITELIST / CHECK_STACK_WHITELIST → 白名单命中 skipReport</param>

/// 连续行 Doxygen 视为函数描述;若需醒目简述,可配置 JAVADOC_AUTOBRIEF 或行首显式 @brief。

0x03 类定义

C++cpp
 /**
 * @brief 类的简单概述
 * 类的详细概述
 */

0x04 批注

C++cpp
/*!< ... 批注 ... */
/**< ... 批注 ... */ (推荐)
//!< ... 批注 ...
///< ... 批注 ...  (推荐)
    
Example:
using apollo::common::ErrorCode;              ///<错误码
using apollo::common::Status;                 ///<状态

0x05 API 注释

C++cpp
/* GLOBAL FUNCTIONS */
/**
 * @brief Example showing how to document a function with Doxygen.
 *
 * Description of what the function does. This part may refer to the parameters
 * of the function, like @p param1 or @p param2. A word of code can also be
 * inserted like @c this which is equivalent to <tt>this</tt> and can be useful
 * to say that the function returns a @c void or an @c int. If you want to have
 * more than one word in typewriter font, then just use @<tt@>.
 * We can also include text verbatim,
 * when the language is not the one used in the current source file (but
 * <b>be careful</b> as this may be supported only by recent versions
 * of Doxygen). By the way, <b>this is how you write bold text</b> or,
 * if it is just one word, then you can just do @b this.
 *
 * @param [in] param1 Description of the first parameter of the function.
 * @param [out] param2 The second one, which follows @p param1, and represents output.
 *
 * @return Describe what the function returns.
 * @retval XXX_OK if successful.
 *
 * @see doxygen_theSecondFunction
 * @see Box_The_Last_One
 * @see <http://website/>
 * @note Something to note.
 * @warning Warning.
 */
int doxygen_theFirstFunction(int param1, int param2);

0x06 结构体注释

C++cpp
/**
 * @brief Use brief, otherwise the index won't have a brief explanation.
 *
 * Detailed explanation.
 */
typedef struct BoxStruct
{
    int a;    /**< Some documentation for the member BoxStruct#a. */
    int b;    /**< Some documentation for the member BoxStruct#b. */
    double c; /**< Etc. */
} tBoxStruct;

0x07 版权声明

C++cpp
/*****************************************************************************
 * Portfolio Info
 ****************************************************************************/