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 removes space between 'override' and curly brace when there's a trailing semicolon #57305

Closed
zmodem opened this issue Aug 23, 2022 · 7 comments · Fixed by #72733
Closed
Assignees

Comments

@zmodem
Copy link
Collaborator

zmodem commented Aug 23, 2022

Consider:

$ echo 'struct S { void f() override {}; };' | clang-format -style=chromium
struct S {
  void f() override{};
};

Note the missing space between override and {.

This only seems to happen when there's a trailing semicolon --- which is of course unnecessary and -Wextra-semi warns about it --- but it would be nice if clang-format could handle it.

@llvmbot
Copy link
Member

llvmbot commented Aug 23, 2022

@llvm/issue-subscribers-clang-format

@rymiel
Copy link
Member

rymiel commented Aug 23, 2022

Issue is around here:

ProbablyBracedList = ProbablyBracedList ||
(NextTok->is(tok::semi) &&
(!ExpectClassBody || LBraceStack.size() != 1));

It is infact doing what the comment says, but I guess it needs extra context that it's parsing a declaration

FWIW, all that's needed to reproduce this is void f() {};, which gets formatted into void f(){};. Putting any statement in the curly braces correctly makes the braces act as FunctionLBrace

Also, a trailing annotation such as noexcept manages to pull the parens and braces apart *somehow*, but the braces are still mis-annotated

@mydeveloperday
Copy link
Contributor

Oh! definitely related

void a() {}
void a() {}
void a() { return a; }
void a() { return a; }
void a() override {}
void a() override{};

@mydeveloperday
Copy link
Contributor

Very much seems to come down to what tryToParseBracedList() thinks...

image

@mydeveloperday
Copy link
Contributor

Perhaps we need another pass, after the assignment of TT_FunctionDeclarationName, where we go back over and set the TT_FunctionLBrace, and find the unnecessary ; after the empty {}.

I think this could fix the spacing and mark the ; optional so they get removed later

@owenca any thoughts?

TokenAnnotator Annotator(Style, Keywords);
 Annotator.annotate(Line);
 Annotator.calculateFormattingInformation(Line);

 Annotator.enusreFunctionLBrace();
 ^^^^^

@mydeveloperday
Copy link
Contributor

More test cases

void a() {}
void a(){};
void a() { return a; }
void a() { return a; }
void a() override {}
void a() override{};
void a() noexcept {}
void a() noexcept(false){};
auto a() -> void {}
auto a() -> void{};

@owenca
Copy link
Contributor

owenca commented Oct 10, 2022

Perhaps we need another pass, after the assignment of TT_FunctionDeclarationName, where we go back over and set the TT_FunctionLBrace, and find the unnecessary ; after the empty {}.

I think this could fix the spacing and mark the ; optional so they get removed later

@owenca any thoughts?

It might work, but I don't think it's worth fixing, especially at the cost of adding another pass.

BTW I succeeded in fixing all of the above test cases (except void a() noexcept(false){};) and passing FormatTests (including the empty function test cases for RemoveSemicolon) without calling the annotator in the parser. However, I abandoned the work because it didn't cover function definitions with a trailing return type, etc.

@owenca owenca self-assigned this Nov 17, 2023
owenca added a commit to owenca/llvm-project that referenced this issue Nov 18, 2023
Also fixed some existing test cases.

Fixed llvm#57305.
Fixed llvm#58251.
owenca added a commit that referenced this issue Nov 19, 2023
Also fixed some existing test cases.

Fixed #57305.
Fixed #58251.
sr-tream pushed a commit to sr-tream/llvm-project that referenced this issue Nov 20, 2023
zahiraam pushed a commit to zahiraam/llvm-project that referenced this issue Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants