34

Is it possible to have clang-format align variable assignments in columns? For example:

int someInteger             = 42;
std::string someString      = "string";
const unsigned someUnsigned = 42;

#define SOME_INTEGER        42
#define SOME_STRING_LITERAL "string"
#define SOME_CONSTANT       42

enum Enum {
    ONE   = 1,
    TWO   = 2,
    THREE = 3,
    FOUR  = 4,
    FIVE  = 5,
    SIX   = 6,
    SEVEN = 7
};

is more readable than:

int someInteger = 42;
const unsigned someUnsigned = 42;
std::string someString = "string";

#define SOME_INTEGER 42
#define SOME_STRING_LITERAL "string"
#define SOME_CONSTANT 42

enum Enum {
    ONE = 1,
    TWO = 2,
    THREE = 3,
    FOUR = 4,
    FIVE = 5,
    SIX = 6,
    SEVEN = 7
};

I realize that it may not be practical for clang-format to always do this, but when code as already been manually formatted like said code, it would be nice for clang-format to leave the formatting in place.

3
  • uncrustify has option for that, but not clang-format. – Jarod42 Nov 24 '14 at 18:10
  • 8
    It's shocking to me that it doesn't support this. Basically makes clang format unusable IMO. – David Dec 10 '14 at 18:15
  • uncrustify's corresponding option is: align_pp_define_span – Cofyc Jan 7 '15 at 5:48
15

It looks like 3.7 supports something like this (haven't tested yet).

From the docs

AlignConsecutiveAssignments (bool)
If true, aligns consecutive assignments.

This will align the assignment operators of consecutive lines. This will result in formattings like code int aaaa = 12; int b = 23; int ccc = 23; endcode

(sic)

6
  • 2
    @syvex, did you have to do anything special to get this working please? I get error: unknown key 'AlignConsecutiveAssignments' – James Webster Jun 30 '15 at 11:29
  • 4
    This actually doesn't work for preprocessor macros. I'm currently trying to add this as a separate option, see the latest commit on this branch github.com/eriknyquist/clang/tree/align_defines – Erik Nyquist Jan 5 '17 at 4:25
  • 2
    Did Eriks commit ever make it to clang? It is ridiculous they have no option to align macros. Why is it taking so long to review and accept? – SFbay007 Jul 13 '17 at 8:40
  • 1
    @ErikNyquist Did you get anywhere getting that into LLVM? – Derick Feb 13 '18 at 15:13
  • 1
    No, still open for review. There's just been a lot of churn, if you look over that link I posted to the review you'll see all the changes that have been requested (and that I have implemented). Then there was silence for a long, long time, and someone's just come back in the last month and essentially asked for the whole thing to be reworked, and I just don't have the time/energy anymore..... – Erik Nyquist Jun 10 '18 at 20:09
3

Clang-format does not have any option to do this.

If you want to tell clang-format to leave certain lines alone, you can make it do so with // clang-format off and // clang-format on comments.

1
  • 1
    @syvex What version are you using? It looks like it might be a new feature since the 3.5 release. – bames53 Nov 24 '14 at 19:16
1

I tested it using https://github.com/mattga/ClangFormat-Xcode/tree/clang_3.7 which is branch of ClangFormat-Xcode supporting 3.7.

I could format a = 9999; type list as I wanted by option

AlignConsecutiveAssignments = true

. But definitions were not aligned. Is there any indication to align them?

1
  • AlignConsecutiveDeclarations may be what you're looking for. – orion elenzil Feb 19 '20 at 17:09
1

For macros: looks like you will be able to accomplish this once clang 10 is released, by adding AlignConsecutiveMacros: true to you .clang-format

https://reviews.llvm.org/D28462

0

you can use this options:AlignConsecutiveMacros: true ref:https://clang.llvm.org/docs/ClangFormatStyleOptions.html support llvm version: >=10.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.