[cfe-dev] Clang Format: Add option to align types?

Marcus Johnson via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 9 15:41:31 PDT 2018


I'm not sure if this is even the right place to ask, I did a quick search
looking for a clang-format specific list but couldn't find one, does anyone
know if there even is one?

I'm trying to add an option to Clang-Format that would allow users to align
variables in structs/classes and functions based on the width of the type
identifier.

Here's a couple examples to better illustrate what I'm talking about:

typedef struct Unaligned_Struct {
int wat;
uint8_t huh;
uint64_t who;
} Unaligned_Struct;

int Unaligned_Average(int A, int B, int num) {
int Average = A + B;
int32_t Divided = Average / num;
return Divided;
}

vs

typedef struct Aligned_Struct {
int      wat;
uint8_t  huh;
uint64_t who;
} Aligned_Struct;

int Aligned_Average(int A, int B, int num) {
int     Average = A + B;
int32_t Divided = Average / num;
return Divided;
}

I'm not sure if the formatting is showing up, so I geuss I'l have to
describe it.

The Aligned blocks all of the variables have X number of spaces added
between the type and the variable name, so that they're all aligned in a
single column.

it makes reading the code a lot easier, I've been doing thi manually with
my code for a while, and Clang-Format not having this feature is the
biggest reason I don't currently use it.

As for my questions, they are:

How exactly do I do this?

Should I write a new enum (I've been thinking about calling it
"AlignTypeDeclarationStyle", but that seems a little clunky, and I'm not
sure if it's the best way to describe what exactly is going on)

and how exactly would I hook this into the Clang-Format library?

I've never contributed to the LLVM project before, I've read the documents
a few times, and I've tried reading the code, but it's very intertwined and
it's hard to drill down as deep as I need.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20181009/9cc7982d/attachment-0001.html>


More information about the cfe-dev mailing list