programmemory.cpp: avoid calls to non-const at() / added utils::as_const()#6651
programmemory.cpp: avoid calls to non-const at() / added utils::as_const()#6651chrchr-github merged 2 commits intocppcheck-opensource:mainfrom
at() / added utils::as_const()#6651Conversation
|
Clang 17 The example from https://trac.cppcheck.net/ticket/10765#comment:4: Clang 17 |
Executor::executeImpl()at()
at()at()
|
There is still one case I have to look at. |
|
And we really need an |
I will leave that out for now because the impact is currently quite neglectable. |
|
Why would this have a performance impact? |
As we are working in a mutable context it will chose the non-const implementation. And if we return something mutable we need to do copy-on-write first so the (potential) changes are only applied to the copy. |
I see. I somehow confused this with |
Yeah, it should have probably been called |
It would make the intent more clear and maybe prevent someone from "cleaning up" in the future. |
I ran into issue with that and need to check how the actual implementation behaves so it will be drop-in in the future. Also it would be great to have tooling which tells you where to use it because checking the code manually is quite tedious even with IDE tooltips. |
| if (pm->hasValue(expr->exprId())) { | ||
| const ValueFlow::Value& v = pm->at(expr->exprId()); | ||
| const auto& pm2 = *pm; | ||
| const ValueFlow::Value& v = pm2.at(expr->exprId()); |
There was a problem hiding this comment.
Can we just remove the non-const overload for at? I dont think its ever used.
There was a problem hiding this comment.
It is used in three places in Executor::executeImpl().
There was a problem hiding this comment.
Maybe we can rename the mutable version as mutate_at to make it explicit when we want to mutate.
There was a problem hiding this comment.
I think that is unnecessary. I will add a barebones as_const() to clean up the code.
at()at() / added utils::as_const()
| { | ||
| C c; | ||
| C* cp = &c; | ||
| utils::as_const(cp)->f(); // (correctly) calls non-const version |
There was a problem hiding this comment.
That's why we need to dereference the pointers in the actual code.
I filed tickets about detecting this: llvm/llvm-project#101580 / https://trac.cppcheck.net/ticket/12977.
No description provided.