llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.64k stars 11.84k forks source link

[clang-format] member functions are not processed as functions while using SpaceBeforeParensOptions::AfterFunctionDefinitionName #76396

Open pohaha opened 10 months ago

pohaha commented 10 months ago

While trying to create a clang-format for our section, I have met with some difficulities, we usually add space after function definition names and after function declaration names (before parens), for both member functions and free functions, while not putting space after function name in function call

I have tried this clang-format configuration clang-format.txt

the needed parameters are the following

SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
  AfterControlStatements: true
  AfterForeachMacros: true
  AfterFunctionDefinitionName: true
  AfterFunctionDeclarationName: true
  AfterIfMacros:   true
  AfterOverloadedOperator: false
  AfterRequiresInClause: true
  AfterRequiresInExpression: true
  BeforeNonEmptyParentheses: false

exactly:

AfterFunctionDefinitionName: true
AfterFunctionDeclarationName: true

preferred output is:

struct my_struct {
    int some_field = 0;
    void some_member_function ();
} test_obj;

void my_struct::some_member_function () {
    printf("hello from member function");
}

void some_free_function () {
    printf("hello from free function");
}

void do_stuff () {
    // use member function
    test_obj.some_member_function();

    // use free function
    some_free_function();
}

output i have (using clang-15, ubuntu-22.04)

struct my_struct {
    int some_field = 0;
    void some_member_function ();
} test_obj;

void my_struct::some_member_function() {
    printf("hello from member function");
}

void some_free_function () {
    printf("hello from free function");
}

void do_stuff () {
    // use member function
    test_obj.some_member_function();

    // use free function
    some_free_function();
}

note line 6:

void my_struct::some_member_function() {

which has no space after member function definition name

parrishmyers commented 7 months ago

I second this request. I would like both functions and member functions to have the spacing, i.e.

prefered output:

void dog (int bark) {}
void Animal::Dog (int bark) {}

and what I get with version 17.0.5

void dog (int bark) {}
void Animal::Dog(int bark) {}

notice how the space on the class member definition doesn't have the space.

Ren911 commented 1 month ago

Clang-Format version 18.1.8. This problem still exists now. When function is "free" , settings, like

SpaceBeforeParensOptions:
  AfterFunctionDefinitionName: true
  AfterFunctionDeclarationName: true

work fine. But when function is inside a class (almost everywhere), it doesn't work...