banach-space / clang-tutor

A collection of out-of-tree Clang plugins for teaching and learning
The Unlicense
692 stars 62 forks source link

[IDEA] const-qualify tool #22

Open SamuelMarks opened 2 years ago

SamuelMarks commented 2 years ago

I want to work on a const qualify tool… concentrating on C with C++ as an afterthought (so don't want to get caught up on the constexpr, const ref, and const method conditions).

Whenever I go to a new codebase I'm [usually] annoyed at the lack of care taken to differentiate changing (var) names and unchanging (val/binding-only) names. Probably all those functional languages rubbing off on me!

I'd expect this to aid in compiler optimisations also…

So what I'm thinking is to use libclang and/or libtooling to modify code automatically from:

int f(int n) {
    int a = 5, b = 6, c;
    puts("nop");
    c = b;
    return n;
}

To:

const int f(const int n) {
    const int a = 5, b = 6;
    int c;
    puts("nop");
    c = b;
    return n;
}

…and get it to work nicely on enums, structs, objects (instantiations of classes and structs), functions, globals, and { scoped bodies (e.g., function bodies).

On the surface I'm not thinking this would be particularly difficult… at least for the majority of cases.

What do you think? - I don't have much experience here so happy to write it myself but could use some oversight 😅 (happy to release under CC0 to be compliant with your philosophy)

Thanks for your consideration

EDIT: Oh and I was watching this CppCon talk from a guy who made some progress in this direction: https://www.youtube.com/watch?v=tUBUqJSGr54

banach-space commented 2 years ago

Probably all those functional languages rubbing off on me!

haha, I knew!

On the surface I'm not thinking this would be particularly difficult… at least for the majority of cases.

The devil with these things is often in the details. Once you start testing the corner cases :) But if you ignore C++ ... it sounds doable!

EDIT: Oh and I was watching this CppCon talk from a guy who made some progress in this direction: https://www.youtube.com/watch?v=tUBUqJSGr54

Yeah, you could try re-implementing it? It would be a great example for clang-tutor.

SamuelMarks commented 2 years ago

Sure happy to give it a go. I'm using CC0 to be compliant with your project, even though (Apache-2.0 OR MIT) is my goto.

Yeah C++ is full of weird edge cases and extended syntax and semantics that I'd like to ignore. Not to say this tool shouldn't run on C++ codebases, but just that if I could ignore its fancier features and do the bare minimum to get it working on C++ then I'd be happy.

Will go over that video with a fine-toothed comb, maybe even contacting the speaker, and port it to LLVM 13 in a monorepo… focussing on just the features I care about (but maybe doing his const ref stuff if he just shares the code along).

I think I'll do #23 first since that's heavy on my mind and will allow me to give a large LoC PR to a few open-source projects I have my eye on.

Watch this space!