Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I align function parameter names in clang-format?

Is it possible to use clang-format to format struct members and function parameter names into columns?

For example:

struct
{
   int           alpha; //aligned to 'b' of "beta"
   unsigned int  beta;
   MyObject     *gamma; //aligned with 'g' not '*'
};

void foobar (int           alpha, //aligned to 'b' of "beta"
             unsigned int  beta
             MyObject     *gamma) //aligned with 'g' not '*'
{
}

If it's not possible, could I extend clang-format somehow to achieve this?

like image 268
lanoxx Avatar asked Sep 20 '15 17:09

lanoxx


People also ask

How do I change my clang format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

Can clang format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.

How do I apply a clang file format?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

Where does clang format look for clang format?

Standalone Tool. clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.


3 Answers

clang-format recently (3.7 or 3.8) gained some more options to adjust alignment, what I was asking in my question is still not fully supported, but we can now get a little closer:

AlignConsecutiveDeclarations: true

which aligns declarations like shown above.

Unfortunately there still seems to be limited control for formatting of the pointer asterisk, see clang-format: Align asterisk (*) of pointer declaration with variable name

like image 200
lanoxx Avatar answered Nov 15 '22 06:11

lanoxx


Well, you can get close.

For function declarations:

You can set BinPackParameters=false, which will force that all the parameters of a function declaration are either on one line or each on a different line, and they will be aligned as you show.

(But not with tab stops in between types and identifiers. That is not possible in clang format right now afaik.)

Also, see the option AllowAllParametersOfDeclarationOnNextLine

Allow putting all parameters of a function declaration onto the next line even if BinPackParameters is false.

For structs, I don't think you can achieve this.

Having written patches for clang-format myself in the past, I think it would be a lot of work to get the alignment like you are suggesting. You would have to write a fair bit of C++ yourself into clang lib format to support this.

like image 30
Chris Beck Avatar answered Nov 15 '22 06:11

Chris Beck


Following up on your post, I'm able to get the following:

struct
{
  int          alpha;
  unsigned int beta;
  MyObject *   gamma;
};

void foobar( int          alphaXXXXXXXXXX,
             unsigned int betaXXXXXXXXX,
             MyObject *   gammaXXXXXXXX )
{
}

with BinPackArguments and BinPackParameters both false, and AlignConsecutiveAssignments and AlignConsecutiveDeclarations both set to true (documentation of these parameters). I had to extend the length of the variables to the function, because the original could fit all of them on one line.

like image 43
Ahmed Fasih Avatar answered Nov 15 '22 06:11

Ahmed Fasih