14

We use lint in our codebase at work for C/C++, I'm trying to start integrating clang-format in my workflow as well.

Unfortunately, lint occasionally requires annotations to ignore a specific check, either of the format:

/*lint -[annotation] */

or

//lint -[annotation]

Specifically, if there's a space between the opening token for the comment and 'lint', it doesn't recognize it as an annotation directive. Unfortunately, the default settings I have for clang-format see that as an error and helpfully insert the space.

Is there any way to get clang-format to recognize comments matching that pattern and leave them alone? Right now I'm using 3.4, but could upgrade if needed.

4

2 回答 2

18

Clang-format 有一个 `CommentPragmas' 选项,即

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

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

CommentPragmas:  '^lint'

其他仍然有“lint”但不是 Lint 评论的评论仍然会被格式化。

于 2014-12-03T18:33:56.767 回答
16

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

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

请参阅在一段代码上禁用格式部分。

于 2014-11-23T16:18:56.210 回答