Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format Overriding multi-line comments for WebKit style

I am trying to use clang-format to clean up code in my repository. We are using the WebKit style as the basis for formatting, however we also want to make sure that multi line comments are formatted correctly.

From my understanding it is possible override the formatting rules of given style by define the .clang-format file as such:

BasedOnStyle: WebKit
AlignTrailingComments: true

This way clang-format should align the trailing comments.

Given the input file:

    /**
     * This is a multi-line comment
     */
    void function() {
        /**
         * This is comment inside the function
         */
    }

My expectation is the following output

/**
 * This is a multi-line comment
 */
void function()
{
    /**
     * This is comment inside the function
     */
}

However what I get is:

/**
     * This is a multi-line comment
     */
void function()
{
    /**
         * This is comment inside the function
         */
}

I've tried dumping out the formatting options of Webkit into a .clang-format file and changing the AlignTrailingComments from false to true. This doesn't make and difference either.

Is there some option in the Webkit style that is interfering with the AlignTrailingComments option?

like image 403
illeyezur Avatar asked Mar 01 '16 00:03

illeyezur


2 Answers

AlignTrailingComments aligns comments trailing code in consecutive lines:

int short;        // short
int longlonglong; // long
like image 112
DrPepperJo Avatar answered Nov 13 '22 07:11

DrPepperJo


The real issue here is the pre-defined WebKit style setting ColumnLimit: 0. This somehow disables the indentation of non-first-line-part of multi-line comments. (And this doesn't seem to be documented anywhere - I think it's a bug.)

One workaround would be to set the column limit to something reasonable, like ColumnLimit: 80 or possibly ColumnLimit: 120. Possibly you could do this once, and then switch back to your usual ColumnLimit: 0 - but setting the column limit once will likely change formatting on many code lines, which will not be restored when you change the column limit back to zero, so probably not what you want.

AlignTrailingComments does not relate to this at all. As another answer explains, this is for aligning the trailing comments at the end of code lines. See the documentation for more details.

I don't believe CommentPragmas will help either. I'm pretty sure this only prevents clang-format from adding line breaks to those comments, but does not prevent indentation changes. (And this is not really documented either.) Anyway, what is needed here is adjusting the indentation, not leaving it alone, so CommentPragmas almost seems like the opposite of what is needed.

like image 40
Eric Backus Avatar answered Nov 13 '22 06:11

Eric Backus