Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format: disable formatting for macros?

I am using clang-format as the auto-formatting tool for my codebase. But some of its features are bugging me.

For instance, I don't want it to format my macro definitions, since most of the time, it's more clear to just formatting them manually. But I don't know how to disable it in clang-format.

Another minor issue is pointer alignment. Sometimes, it's clear to make it aligned left, sometimes it's right. So I'd rather do it by hand. But disable it from clang-format seems impossible?

Any help on these issues?

like image 464
blackball Avatar asked Jan 24 '19 13:01

blackball


People also ask

How do I disable clang formatting in Visual Studio?

Tools > Options configuration By default, Visual Studio will use any existing ClangFormat file in your source tree for formatting operations. To turn this off, you can uncheck Enable ClangFormat support.

How do I get rid of clang-format?

There is no functionality in clang-format for undoing a format operation.

What is clang-format off?

Disabling Formatting on a Piece of Code Clang-format understands also special comments that switch formatting in a delimited range. The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted.

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.


1 Answers

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).

like image 178
Dmitry Sokolov Avatar answered Nov 15 '22 11:11

Dmitry Sokolov