简体   繁体   中英

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?

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

At a quick glance over the style options it looks like there is currently no option for this. The only option which deals with preprocessing that I see is IndentPPDirectives which deals with indentations of #if blocks.

I couldn't find any option for this either. Clang has trashed all of my defines that have been automatically aligned by emacs..

Note that clang-format-9 and above have an option of AlignConsecutiveMacros: true which can do the trick for you.

To install clang-format-9 on ubuntu/Debian you need to check http://apt.llvm.org/ .

As an example, if you have Ubuntu 16.04/xenial, you need to do the following:

sudo sh -c 'echo "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-9 main" >> /etc/apt/sources.list'
sudo sh -c 'echo "deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-9 main" >> /etc/apt/sources.list'
sudo apt update
sudo apt install clang-format-9

Possible to handle Trusty/Ubuntu 14.04 the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM