llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open ilan-schemoul opened this issue 2 years ago • 9 comments

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;

ilan-schemoul avatar May 04 '24 14:05 ilan-schemoul

@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;"

llvmbot avatar May 04 '24 14:05 llvmbot

Clang-format is not supposed to modify code.

EugeneZelenko avatar May 04 '24 14:05 EugeneZelenko

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 ?

ilan-schemoul avatar May 04 '24 14:05 ilan-schemoul

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?

idler66 avatar May 07 '24 08:05 idler66

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

ilan-schemoul avatar May 07 '24 12:05 ilan-schemoul

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

idler66 avatar May 08 '24 00:05 idler66

Why ? Should I close this issue ?

ilan-schemoul avatar May 08 '24 08:05 ilan-schemoul

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.

idler66 avatar May 08 '24 08:05 idler66

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.

ilan-schemoul avatar May 08 '24 08:05 ilan-schemoul