[llvm-bugs] [Bug 48668] New: Issues with formatting comments within switch statements

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jan 5 12:30:40 PST 2021


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

            Bug ID: 48668
           Summary: Issues with formatting comments within switch
                    statements
           Product: clang
           Version: trunk
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: Formatter
          Assignee: unassignedclangbugs at nondot.org
          Reporter: timwoj at gmail.com
                CC: djasper at google.com, klimek at google.com,
                    llvm-bugs at lists.llvm.org

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.

-- 
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/20210105/11e04f8a/attachment.html>


More information about the llvm-bugs mailing list