【问题标题】:clang-format breaks lint annotationsclang-format 打破 lint 注释
【发布时间】:2026-01-31 23:30:01
【问题描述】:

我们在 C/C++ 的代码库中使用 lint,我也在尝试将 clang-format 集成到我的工作流程中。

不幸的是,lint 有时需要注释来忽略特定检查,格式如下:

/*lint -[annotation] */

//lint -[annotation]

具体来说,如果注释的开始标记和“lint”之间有空格,它不会将其识别为注释指令。不幸的是,我对 clang-format 的默认设置将其视为错误并有助于插入空格。

有没有办法让 clang-format 识别与该模式匹配的 cmets 并让它们不理会?目前我使用的是 3.4,但如果需要可以升级。

【问题讨论】:

    标签: c++ lint clang-format


    【解决方案1】:

    您可以使用以下命令禁用文件该部分的 clang-format:

    int formatted_code;
    // clang-format off
        void    unformatted_code  ;
    // clang-format on
    void formatted_code_again;
    

    请参阅Disabling formating on a piece of code 部分。

    【讨论】:

    【解决方案2】:

    Clang-format 有一个 `CommentPragmas' 选项

    描述具有特殊含义的 cmets 的正则表达式,不应分割成行或以其他方式更改。

    当我将以下行放入 .clang 格式文件中时,我的 Lint cmets 保持不变。

    CommentPragmas:  '^lint'
    

    其中仍有“lint”但不是 Lint cmets 的其他 cmets 仍会被格式化。

    【讨论】: