Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can clang-format align a block of #defines for me?

I have a source file containing some lines like this;

#define ARC_V2_LP_START         0x002 #define ARC_V2_LP_END           0x003 #define ARC_V2_STATUS32         0x00a 

With all the values nicely aligned. Unfortunately clang-format does this;

#define ARC_V2_LP_START 0x002 #define ARC_V2_LP_END 0x003 #define ARC_V2_STATUS32 0x00a 

I have found the AlignConsecutiveDeclarations and AlignConsecutiveAssignments options, but nothing that will align consecutive #defines. Is it possible to do this?

like image 338
Erik Nyquist Avatar asked Jul 27 '16 17:07

Erik Nyquist


People also ask

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.

How does clang-format work?

clang-format is a tool to automatically format C/C++/Objective-C code, so that developers don't need to worry about style issues during code reviews. It is highly recommended to format your changed C++ code before opening pull requests, which will save you and the reviewers' time.

What is clang-format style?

Clang-Format is a widely-used C++ code formatter. As it provides an option to define code style options in YAML-formatted files — named . clang-format or _clang-format — these files often become a part of your project where you keep all code style rules.

How do you customize your clang-format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.


1 Answers

[UPDATE]

The op's pull request finally went through and as of clang version 9.0.0 is live. The functionality is enabled by the AlignConsecutiveMacros: true option.

[ORIGINAL]

Weirdly enough this feature is yet to be implemented in clang; the formatting option for consecutive macros is currently missing.

Many developers are interested and there is a working pull request that has been waiting for approval for years: https://reviews.llvm.org/D28462?id=93341

You can integrate it or just wait whether they add it on the official branch, but at this point I doubt they will.

like image 102
Maldus Avatar answered Sep 19 '22 23:09

Maldus