-
Notifications
You must be signed in to change notification settings - Fork 13.7k
cmake : enable -Wshadow for C++ code #11193
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
Conversation
|
|
||
| // mutable variable, needed during the last layer of the computation to skip unused tokens | ||
| int32_t n_tokens = this->n_tokens; | ||
|
|
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.
This variable is mutated at il == n_layer - 1, which IIUC has practically no effect on the computation. Am I missing something? Maybe the intent was to mutate it at il == n_layer - 2?
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.
Seems like historically it was used by llm_build_moe_ffn, but was replaced by int64_t n_tokens = cur->ne[1] at some point. It's safe to remove it here though
039f77d to
f67d7ec
Compare
f67d7ec to
82caffa
Compare
ggml-ci
ggml-ci
d58f1d3 to
a59ee7c
Compare
ggml-ci
ggml-ci
| if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") | ||
| list(APPEND CXX_FLAGS -Wshadow-field-in-constructor) | ||
| endif() |
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.
Decided to add the -Wshadow-field-in-constructor flag because there is no way to disable it with GCC, so it would be more consistent to have it in Clang as well. This requires to add underscore _ suffix to constructor arguments that have the same name as the members in order to disambiguate the two.
ggml/include/ggml-backend.h
Outdated
|
|
||
| // Copy a graph to a different backend | ||
| GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph); | ||
| GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy_init(ggml_backend_t backend, struct ggml_cgraph * graph); |
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.
@slaren This function caused a shadow warning with GCC, so I renamed it to ggml_backend_graph_copy_init:
llama.cpp/ggml/src/../include/ggml-backend.h:334:119: warning: ‘ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t, ggml_cgraph*)’ hides constructor for ‘struct ggml_backend_graph_copy’ [-Wshadow]
334 | GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph);
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 would rather rename the struct to something like ggml_backend_graph_copy_state, since this is not really a constructor, it's just that it returns multiple objects to the caller.
|
This change increases the difficulty of naming things since we can no longer reuse symbol names in nested contexts. IMO it's a useful change, but I'm having doubts about the extra effort that would be required to come up with meaningful and consistent names. An alternative solution is to keep I'm leaning more toward the former option of enabling |
|
My opinion is that shadowing is rarely a problem by itself. The cases when shadowing can become a source of bugs are often caused by having functions that are too big and complex to really understand. When functions are brief and concise, with clear and simple responsibilities, shadowing is rarely a problem because the variable declaration is always close to the location where it is used. |
|
I'd agree with @slaren that shadowing is rarely a problem. Even if there are bugs caused by that, most of the time they will produce obvious bugs that are easy to pinpoint. IMO Also, this makes new (and even existing) contributors to spend more time on fixing naming, which may result in a poor DX overall. |
|
Yes, this change is probably not really worth it. Let's shelve it for now. |
Sometimes I like to discourage unnecessary nesting. Like C++ lamdas, why use them when you can write a static function with a nice descriptive name? Or random scope blocks (which again could be a little static function with a nice descriptive name).
some people do a trailing _ suffix also, saves a character. But I really don't mind. Too many standards can slow down PRs getting merged significantly also, which is not good 😄 I'm glad we got the .clang-format file in. I struggle to remember the little nitpicks of each and every C/C++ codebase, one less thing to worry about 😄
|
This flag would be useful to prevent shadowing class members.
TODO:
clang-tidysupport this?