[llvm-bugs] [Bug 48569] New: clang-format fails to align case label with `switch`

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Dec 21 13:16:27 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48569

            Bug ID: 48569
           Summary: clang-format fails to align case label with `switch`
           Product: clang
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Formatter
          Assignee: unassignedclangbugs at nondot.org
          Reporter: dmlorenzetti at lbl.gov
                CC: djasper at google.com, klimek at google.com,
                    llvm-bugs at lists.llvm.org

Overview
--------

With `clang-format` v 11.0.0, when using option `IndentCaseLabels: false`, the
actual behavior does not always match the documented behavior.

In particular, the documentation states that this option is to "indent case
labels one level from the switch statement. When false, use the same
indentation level as for the switch statement."

Setting the option to `false` works as expected for some cases (including all
the cases I have seen used as examples online).  However, it fails when the
opening brace is on the next line from the `switch`, and the opening brace is
indented.  With that configuration, the case labels are indented (i.e., aligned
to the brace, rather than to the switch statement).


Steps to reproduce
------------------

The source code file below defines a simple demonstration case.  Call this file
`input.c`.  This file is formatted as I would expect `clang-format` to do in
the failing case:

```
int demonstrate_case_label_alignment(const int control)
  {
  int ret_val = 0;
  switch (control)
    {
  case (0):
    ret_val = 1;
    break;
  default:
    ret_val = 2;
    break;
    }
  return ret_val;
  }
```

Here is a case in which `clang-format` correctly aligns the case labels with
the `switch`:

```
works_style="{BasedOnStyle: llvm, IndentCaseLabels: false}"
clang-format -style="${works_style}"  input.c
```

Here is a case in which `clang-format` indents the case labels, even though
told not to:

```
fails_style="{BasedOnStyle: llvm, BreakBeforeBraces: Whitesmiths,
IndentCaseLabels: false}"
clang-format -style="${fails_style}"  input.c
```

Here, the `Whitesmiths` option moves the brace to the line after the switch,
and indents it.  And in the output, one finds that the case labels are
indented, to align with the open-brace rather than to the switch.  Here are
three lines from the failing output, to make this clear without having to run
the formatter:

```
  switch (control)
    {
    case (0):
```

Note that the `case` is not aligned with the `switch`.


Build date and hardware
-----------------------

clang-format version 11.0.0

Mac OS 10.14.6 on Intel core i7

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201221/ece8d21f/attachment.html>


More information about the llvm-bugs mailing list