[cfe-dev] clang-format: adding new style option for C/C++ macro alignment

Erik Nyquist via cfe-dev cfe-dev at lists.llvm.org
Sun Jan 1 10:27:10 PST 2017


Hello,

clang newbie here, playing with clang-format, learning by adding a new style
option which I've wanted for a while:

https://github.com/eriknyquist/clang/commit/12a8577f8b2bc9d6d20099340728ab03cbe1058d

(hopefully the addition to docs/ClangFormatStyleOptions.rst is sufficient to
explain my intentions)

Now, the discussion about whether or not this option is something needed by
clang-format, is a discussion I'd like to save for later. Right now I'm just
figuring things out :)

I looked at existing alignConsecutive* functions in
lib/Format/WhitespaceManager.cpp, and sort of guessed my way into a new
alignConsecutiveMacros function. It works, probably by accident, with simple
test files, but I'm making several assumptions. I'm hoping somebody here can
point me in the right direction to resolve these assumptions!

alignConsecutiveMacros(): Right now, I am calling AlignTokens to align on
"any numerical constant that occurs 3 Changes after a tok::hash". I see two
issues with this:

    1. I am not sure if 1 Change == 1 Token; can there be multiple changes to
       a Token?

    2. Instead of tok::hash, I want to specifically check for tok::pp_define,
       however this is different token type (PPKeywordKind) which is not
       tracked by a Change. So I'm not sure how to (or if I can) access PP
       tokens here.

To summarise, instead of aligning on "any numerical constant 3 Changes
after a tok::hash", like the linked code above is doing, I want to align on
"the 4th token in a #define directive".

So, does anyone have any pointers on how I might put into code "the 4th token in
a #define directive"?

Happy New Year,
Erik



More information about the cfe-dev mailing list