首页 > 解决方案 > clang-format:禁用宏的格式化?

问题描述

我使用 clang-format 作为我的代码库的自动格式化工具。但是它的一些功能让我很烦。

例如,我不希望它格式化我的宏定义,因为大多数时候,手动格式化它们会更清楚。但我不知道如何以 clang 格式禁用它。

另一个小问题是指针对齐。有时,让它左对齐很清楚,有时它是右对齐。所以我宁愿自己动手。但是从 clang-format 禁用它似乎是不可能的?

对这些问题有帮助吗?

标签: c++clang-format

解决方案


You can wrap your macros in

// clang-format off
#define ... \
   ...
// clang-format on

To escape manually editing of each file you can use the regex

search: ^([ \t]*#[ \t]*define[ \t]+.+?\\\r?\n(?:.*?\\\r?\n)*.*?\r?\n)

replace: // clang-format off\r\n$1// clang-format on\r\n

for instance, in Notepad++, Ctrl+Shift+F - "Find in Files - "Replace in Files".

To date (up to v11) there is no way to disable pointer alignment. You can either set the style, or derive the style (clang-format will analyze a file for the most common alignment of & and * and will use it).


推荐阅读