-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
programmemory.cpp: avoid calls to non-const at()
/ added utils::as_const()
#6651
Conversation
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. |
lib/programmemory.cpp
Outdated
@@ -1327,7 +1327,8 @@ namespace { | |||
ValueFlow::Value executeMultiCondition(bool b, const Token* expr) | |||
{ | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just remove the non-const overload for at
? I dont think its ever used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used in three places in Executor::executeImpl()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.