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

Larry Evans via cfe-dev cfe-dev at lists.llvm.org
Wed Oct 10 05:36:22 PDT 2018


On 10/10/2018 04:10 AM, Whisperity via cfe-dev wrote:

 > While I am not exactly able to help on the technical
 > details as I’m also new on Clang grounds, if you or anyone
 > else steps forward to implement this, I think it would be
 > nice to early on consider a few more rules, such as:
 > • Aligning the cv-qualifiers together and indent the real
 >   type name into a separate column

Do you mean something like this:

   //cv-qualifiers       type         varid
   //-------------       ----         -----
     const               int          ci;
                         int          i;
     const               T<int,char>  tic;

?

 > • Within some reasonable boundary, align template
 >   arguments.

A problem with OP requirements and the above is deciding the
"reasonable boundary".  For example, if the last line in the
above example were:

SomeLongTemplateName<SomeLongTemplateArg1,SomeLongTemplateArg2,...>

then there'd be a problem finding enough room one 1 line to
contain the whole declaration.

OTOH, the "reasonable boundary" problem largely disappears
if the declaration is "structure aligned" over several lines
just as now, most people "structure align" compound
statements over several lines.  For example, instead of:

   if(C1)S1;else if(C2)S2;else S3;

most people "structure align" the if statement to:

   if(C1)
     S1;
   else if(C2)
     S2;
   else
     S3;

where the keywords (the most important part of the
structure), are all aligned in the same leftmost column and
the "details" (the sub-statements in the branches of the if)
are indented right to signify their lesser importance.

Similarly, the declarations could be "structure aligned" by
aligning the most important part of the declaration in the
leftmost column and the lesser import parts indented right.
For example:

     SomeLongTemplateName1
     < SomeLongTemplateArg1_1
     , SomeLongTemplateArg1_2
     >
     SomeCvQuals1
   SomeLongVarName2
     ;
     SomeLongTemplateName2
     < SomeLongTemplateArg2_1
     , SomeLongTemplateArg2_2
     >
     SomeCvQuals2
   SomeLongVarName2
     ;


Unfortunately, I don't know how to do this with clang-format
:(

-regards,
Larry





More information about the cfe-dev mailing list