<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Issues with formatting comments within switch statements"
   href="https://bugs.llvm.org/show_bug.cgi?id=48668">48668</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Issues with formatting comments within switch statements
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Formatter
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>timwoj@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>djasper@google.com, klimek@google.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I ran into what I'm pretty sure is a bug in clang-format with handling comments
within switch statements. I initially discovered this with the Whitesmiths
mode, but it appears to happen in Allman mode as well. I tested it with the
following configuration:

Language: Cpp
Standard: c++17
BreakBeforeBraces: Whitesmiths
IndentCaseLabels: true
IndentCaseBlocks: false
IndentWidth: 4
TabWidth: 4
UseTab: AlignWithSpaces

I tested it with the following input (test case 1):

int main(int argc, char** argv)
        {
        switch ( value )
                {
        // Comment
                case 0:
                        break;
                }
        }

I would expect clang-format to take that comment and align it with the the
block and the case statement, but this is what I get:

int main(int argc, char **argv)
        {
        switch (value)
                {
                        // Comment
                case 0:
                        break;
                }
        }

As a second test, I indented the comment to where I’d expect, such as (test
case 2):

int main(int argc, char** argv)
        {
        switch ( value )
                {
                // Comment
                case 0:
                        break;
                }
        }

Running clang-format on this input, I get something completely different:

int main(int argc, char **argv)
        {
        switch (value)
                {
        // Comment
                case 0:
                        break;
                }
        }

I tried to trace through the code around this a bit. Comments are complicated,
but with test case 1 it appears that Line->Level gets set to 3 during
UnwrappedLineParser::parse. This flows down to the formatter later, and it
misinterprets what the indentation should be for that line. I modified the
configuration listed above changing the BreakBeforeBraces option to Allman, but
leaving everything else the same. With that configuration it properly indents
test case 2 but not test case 1. I get the following output with that test:

int main(int argc, char **argv)
{
        switch (value)
        {
                        // Comment
                case 0:
                        break;
        }
}

That mirrors the output problem that I get from Whitesmiths. This seems to
indicate to me that it’s not something about the Whitesmiths setting itself,
but something deeper than that.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>