llvm / llvm-project

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

[feat] Clang format detect when using struct XYZ instead of XYZ (typedef) #91063

Open ilan-schemoul opened 5 months ago

ilan-schemoul commented 5 months ago

Hey, It would be awesome to have an option (the name could be UseTypedefInsteafOfFullStructName) so when we have this

struct abc{};
typedef struct abc a;
int main() {
   struct abc b;
}

struct abc b; gets replaced by a b;

llvmbot commented 5 months ago

@llvm/issue-subscribers-clang-tidy

Author: Ilan Schemoul (ilan-schemoul)

Hey, It would be awesome to have an option (the name could be UseTypedefInsteafOfFullStructName) so when we have this ```c struct abc{}; typedef struct abc a; int main() { struct abc b; } ``` `struct abc b;` gets replaced by "a b;"
EugeneZelenko commented 5 months ago

Clang-format is not supposed to modify code.

ilan-schemoul commented 5 months ago

Oh okay I thought as typedef was to define an alias clang-format was okay with that but I understand that you don't want a formatter to modify code. Should this issue be closed or is there any other tool that could take care of this (clang warning flag + clangd code action) or this should be closed ? Do you have any idea how I could implement this if it's stricly out of scope for this repo ?

idler66 commented 5 months ago

struct abc{}; typedef struct abc a; typedef struct abc x; int main() { struct abc b; } abc, a, x have different semantics. How to do they replace each other?

ilan-schemoul commented 5 months ago

You can specify the preference order and for example choose that if there's multiples one you always pick the first.

idler66 commented 5 months ago

"always pick the first" leads to a semantic error.

ilan-schemoul commented 4 months ago

Why ? Should I close this issue ?

idler66 commented 4 months ago

In my opinion, the issue can be closed. Different meanings may be assigned to struct abs via a and x in projects. And the definitions of a and x can evolve over time too.

ilan-schemoul commented 4 months ago

Different meanings may be assigned to struct abs via a and x in projects.

Well then it's not a problem anymore ? If one of a or x definition is not struct abc then we don't pick it when we format ? I am missing the point ?

You're talking about a very specific edge case "what if someone would give two typedef to the same struct and then modify a single one of them" and even then I don't see your point still. Well if someone modify one typedef they known what they're doing I don't see the problem.