Skip to content

clang-format ContinuationIndentWidth disobeyed for if statements #28052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
llvmbot opened this issue May 8, 2016 · 1 comment
Open

clang-format ContinuationIndentWidth disobeyed for if statements #28052

llvmbot opened this issue May 8, 2016 · 1 comment
Labels
bugzilla Issues migrated from bugzilla clang-format

Comments

@llvmbot
Copy link
Member

llvmbot commented May 8, 2016

Bugzilla Link 27678
Version unspecified
OS Windows NT
Reporter LLVM Bugzilla Contributor

Extended Description

If I write a sample line like:

if (strcmp("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
    b=1;

and tell clang-format to format it; when I have "IndentWidth: 4" and "ContinuationIndentWidth: 2", I get:

if (strcmp("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
      "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
    b = 1;

Since my Continuation Indent Width is 2, I should get:

if (strcmp("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
    b = 1;

Note that if I simply remove the "if" and the following space, and put a semicolon at the end of the line, the expected indentation is produced. It is evidently keying off this being a conditional, since this statement is formatted thusly:

a = strcmp("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
      "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
  ? 1
  : 0;

I understand if some users feel that the way it currently operates is more readable; but others like me may prefer to have the extra room on the line, in what not uncommonly, long if conditions. If on the other hand, this special exception is to be made for the if statement alone (or whatever other statements are currently special cases like this), I propose you create a new continuation indent setting to override it, e.g.: "ConditionalContinuationIndentWidth: 2". I suppose this bug is being exposed because I am not aligning my function arguments on the next line - opting again for more room on the line.

My current config:

BasedOnStyle: LLVM
Language: Cpp
AlignAfterOpenBracket: DontAlign
BreakBeforeBraces: Allman
IndentWidth: 4
ContinuationIndentWidth: 2
ColumnLimit: 80
AlignOperands: false
BreakBeforeBinaryOperators: false

@llvmbot
Copy link
Member Author

llvmbot commented May 30, 2016

I'm also confused by this behaviour. I don't want to fight the tool but it seems inconsistent as it indents differently based on a function's open paren inside a control statement. See A vs. B below.

Using the config commented before of:

BasedOnStyle: LLVM
Language: Cpp
AlignAfterOpenBracket: DontAlign
BreakBeforeBraces: Allman
IndentWidth: 4
ContinuationIndentWidth: 2
ColumnLimit: 80
AlignOperands: false
BreakBeforeBinaryOperators: false

I get:

// A:
if (strcmp(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb))
b = 1;

// B:
if (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&
(cccccccccccccccccccccccccccccccccc || dddddddddddddddddddddddddddddddddd))
b = 1;

// C:
if (strcmp(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) &&
(cccccccccccccccccccccccccccccccccc || dddddddddddddddddddddddddddddddddd))
b = 1;

// D:
strcmp(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);

I don't understand the reasoning behind why A is formatted differently than B. I would find it more intuitive if either all control statements indent wrapped lines like A with 6 spaces:

// E:
if (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&
(cccccccccccccccccccccccccccccccccc || dddddddddddddddddddddddddddddddddd))
b = 1;

Or like the above B case:

// B:
if (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&
(cccccccccccccccccccccccccccccccccc || dddddddddddddddddddddddddddddddddd))
b = 1;

Given the choice, I'd like it to respect the AlignAfterOpenBracket + ContinuationIndentWidth setting, but consistency is most important to me.

I'd also be happy with reasoning behind the choice if it was made consciously. I'm all for learning something new :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang-format
Projects
None yet
Development

No branches or pull requests

2 participants