tau icon indicating copy to clipboard operation
tau copied to clipboard

Make parameters and variables constant when they are not mutated

Open tomasz-lisowski opened this issue 3 years ago • 2 comments

tomasz-lisowski avatar Jun 06 '22 14:06 tomasz-lisowski

Hey, can you please merge this?

tomasz-lisowski avatar Oct 24 '22 20:10 tomasz-lisowski

Please address my review changes from June.

jasmcaus avatar Oct 25 '22 06:10 jasmcaus

@jasmcaus I don't see any code reviews from you on this pull request? What are you referring to?

tomasz-lisowski avatar Oct 25 '22 13:10 tomasz-lisowski

Strange. Just made some updates. LMK if you can see those.

jasmcaus avatar Oct 25 '22 13:10 jasmcaus

I still don't see it. Are you sure you are submitting the code review? (big green "submit review" button :>)

tomasz-lisowski avatar Oct 25 '22 14:10 tomasz-lisowski

Yes. Strange why it isn't visible to you.

I'll add below what I'd added:

  • What's the difference between const char*..., char* const... and const char* const...? -- in tauShouldDecomposeMacro (tau.h:435)
  • Why the const* const? -- in tauCmdLineRead (tau.h:1019)

jasmcaus avatar Oct 28 '22 09:10 jasmcaus

Sorry about that. Please see above reply.

jasmcaus avatar Oct 28 '22 09:10 jasmcaus

const char*... and char const*... are equivalent, they mean that the value of the pointer can change, but the pointer may not be used to modify the value being pointed-to (no matter if it's a constant or not).

char* const... means that the value of the pointer cannot change, but it can be used to modify the pointed-to value.

const char* const... and char const* const... combines the two, so you get both a pointer which cannot change, and the value pointed-to which cannot be modified using this pointer.

int foo = 2;
int const bar = 3;

int *const foo_ptr = &foo;
int const* bar_ptr = &bar;

foo_ptr = world_ptr; // Invalid since foo_ptr variable is constant.
*foo_ptr = 0; // Valid since the foo int is not constant.

*bar_ptr = 1; // Invalid since the value pointed to by bar_ptr is constant.
bar_ptr = foo_ptr; // Valid since the bar_ptr variable is not constant.

// You can go a step further though:
int const* const foo_ptr2 = &foo; // or const int* const foo_ptr2 = &foo;
// This prevents us from modifying the foo int using this pointer even though
// the foo variable itself is not a constant.

// You can also do this:
int const* test_ptr = foo_ptr;
test_ptr = bar_ptr;

The reason I added const in so many places was because most of the variables do not change and if someone wanted to pass constant values to Tau macros/functions, they wouldn't be able to compile. This essentially allows more of the code of Tau and projects using Tau to be immutable, so it gives more guarantees to a human reader. As an extra, you might get extra compiler optimizations thanks to the immutable guarantee.

P.S. Sorry it took me a while, but had to move and was quite busy in general.

tomasz-lisowski avatar Nov 13 '22 12:11 tomasz-lisowski

Hey, hope you didn't forget about this :).

tomasz-lisowski avatar Jan 12 '23 01:01 tomasz-lisowski

Yup, thanks for this!

jasmcaus avatar Jan 13 '23 08:01 jasmcaus