Skip to content
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

clang-format mismatches braces with nested unnamed scopes #47649

Closed
mateuszzz88 mannequin opened this issue Nov 25, 2020 · 4 comments
Closed

clang-format mismatches braces with nested unnamed scopes #47649

mateuszzz88 mannequin opened this issue Nov 25, 2020 · 4 comments
Labels
bugzilla Issues migrated from bugzilla clang-format duplicate Resolved as duplicate

Comments

@mateuszzz88
Copy link
Mannequin

mateuszzz88 mannequin commented Nov 25, 2020

Bugzilla Link 48305
Version 11.0
OS Linux
CC @CaseyCarter,@cpplearner,@mateuszzz88

Extended Description

Hello.
With clang-format -10 and -11 installed from repo on ubuntu 20.11 following c++ code is obviously formatted wrong:

clang-format-11 -style=llvm test.cpp 
void foo() {
  {// asdf
   {int a;
}
}
{
  { int b; }
}
}

Removing {// asdf is sufficient to format it ok.
I expect it to be formatted like this (possibly with comment in new line, but this may be llvm style):

void foo() {
  {// asdf
    { int a; }
  }
  {
    { int b; }
  }
}

@cpplearner
Copy link
Mannequin

cpplearner mannequin commented Nov 23, 2021

It seems that clang-format erroneously treats {{int a; as a malformed initializer list. (The first brace in the repro is on a separate line only because it's followed by a single line comment.)

The problem appears to be caused by UnwrappedLineParser.

  • UnwrappedLineParser::tryToParseBracedList calls calculateBraceTypes to see whether a left brace starts an initializer list or a block. If the left brace starts an initializer list, parseBracedList is called to consume it.
  • UnwrappedLineParser::calculateBraceTypes thinks {// asdf starts an initializer list, because its matching right brace is followed by a left brace (which makes sense in a constructor, e.g. in class Foo { int mem; Foo() : mem{} {};, the {} after mem is an initializer list, but does not make sense otherwise).
  • UnwrappedLineParser::parseBracedList consumes the initializer list and stops at the semicolon after int a (because it was called with ContinueOnSemicolons being the default, i.e. false). It is unaware that the left brace has a matching right brace.

@mateuszzz88
Copy link
Mannequin Author

mateuszzz88 mannequin commented Nov 27, 2021

mentioned in issue llvm/llvm-bugzilla-archive#48438

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@AtnNn
Copy link

AtnNn commented Apr 22, 2022

This issue still exists in version 14:

bash$ echo $'void foo(){{//asdf\n{int a;}}{{int b;}}}' | clang-format -style LLVM
void foo() {
  {// asdf
   {int a;
}
}
{
  { int b; }
}
}

@rymiel
Copy link
Member

rymiel commented Feb 27, 2023

Duplicate of #33891

@rymiel rymiel marked this as a duplicate of #33891 Feb 27, 2023
@rymiel rymiel closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
@rymiel rymiel added the duplicate Resolved as duplicate label Feb 27, 2023
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 duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

2 participants