Skip to content

[clang-format] Incorrectly identifies empty function as bracedList #58251

Closed
@mydeveloperday

Description

@mydeveloperday
Contributor

Because clang-format cannot determine {} as an empty function is leaves out a space between the ) and the { as it doesn't identify the { as a TT_FunctionLBrace

Add an issue for tracking a potential change to fix that.

void function(){};
void function() { int a; };

Activity

llvmbot

llvmbot commented on Oct 9, 2022

@llvmbot
Member

@llvm/issue-subscribers-bug

llvmbot

llvmbot commented on Oct 9, 2022

@llvmbot
Member

@llvm/issue-subscribers-clang-format

mydeveloperday

mydeveloperday commented on Oct 9, 2022

@mydeveloperday
ContributorAuthor

While working on a fix for #58217 (namely https://reviews.llvm.org/D135466) we identified that foo(){} was not being identified the same as foo() { return; }

rymiel

rymiel commented on Oct 9, 2022

@rymiel
Member

Also see #57305 (comment). Not exactly a duplicate but the same underlying bug, i'm assuming

mydeveloperday

mydeveloperday commented on Oct 9, 2022

@mydeveloperday
ContributorAuthor

The more I look at this the more I think this is harder than we first think, simply assuming ) { to be a function would change, would then identify the { as a FunctionLBrace, which in turn RemoveSemicolons would remove the trailing ; which is needed i.e.

this

auto i = decltype(x) {};

to this

auto i = decltype(x) {}

which would be a compile error

So we'd really need yet more semantic (or derived semantic) information about what makes {}; a function body.

The unwrappered parser when we are determining the block type is before the annotator which identified the ')' as a TypeDeclarationParen, but this would be useful information to have.

I wonder if there is something we can do after the function name has been identified as FunctionDeclarationName

At present we get this..

Expected & Input

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

Clang-Format current output

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

rymiel commented on Oct 9, 2022

@rymiel
Member

The more I look at this the more I think this is harder than we first think

I definitely couldn't fix it. I concluded that determining if a function declaration is being dealt with (i.e. Line.MightBeFunctionDecl) happens way later and I couldn't find a non-invasive heuristic that didn't involve lots of duplicated work

mydeveloperday

mydeveloperday commented on Oct 10, 2022

@mydeveloperday
ContributorAuthor

feel free to have a go.

self-assigned this
on Nov 17, 2023
added a commit that references this issue on Nov 18, 2023
519c21d
added a commit that references this issue on Nov 19, 2023
edad025

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorclang-format

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @owenca@mydeveloperday@rymiel@llvmbot

    Issue actions

      [clang-format] Incorrectly identifies empty function as bracedList · Issue #58251 · llvm/llvm-project