-
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
Add -Wmissing-prototypes
and fix issues it finds.
#4019
Conversation
namespace Carbon::Parse { | ||
|
||
// Declare handlers for each parse state. | ||
#define CARBON_PARSE_STATE(Name) auto Handle##Name(Context& context) -> void; |
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.
@jonmeow I think this is worth discussion to make sure we're all on the same page:
Previously, we defined these functions in the relevant handle_*.cpp
files with no forward declaration, which I think was probably intentional. Adding the forward declarations means we get the substantial advantage of being able to detect missing static
, but also means the Handle*
functions are all in scope in all handle_*.cpp
files, whereas we probably don't want them to be able to invoke each other.
Similarly in check/
, where we had the header file already but didn't include it from the handle_*.cpp
files.
I think this change is a good thing nonetheless. (It would also catch issues where the return type of a Handle*
function is wrong.)
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.
Now that you remind, me I remember this being intentional. Sorry I forgot about that.
But it is definitely something to discuss -- not at all sure what the exact balance of tradeoffs is here. I was honestly surprised how many real issues -Wmissing-prototypes
found (completely outside of any handle.h
stuff) where we forgot headers and didn't use static
. So mostly sending this to see if there is a good pattern we could follow here. Not at all attached to specifically having all the Handle*
functions in one header though, very happy if we want some different pattern or way of approaching this.
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 there's probably more benefit to prototype matching than avoiding the risk of recursive calls this way. I'd probably have made these functions on a class if I'd been allowed to, just to get the declaration match checking (it's a nuisance when a typo on names is only detected at link time, and I'm a little surprised this didn't seem to find any unused code where a state had been deleted but the handler left behind). Not needing to comment for static
as much will be nice, too.
namespace Carbon::Parse { | ||
|
||
// Declare handlers for each parse state. | ||
#define CARBON_PARSE_STATE(Name) auto Handle##Name(Context& context) -> void; |
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 there's probably more benefit to prototype matching than avoiding the risk of recursive calls this way. I'd probably have made these functions on a class if I'd been allowed to, just to get the declaration match checking (it's a nuisance when a typo on names is only detected at link time, and I'm a little surprised this didn't seem to find any unused code where a state had been deleted but the handler left behind). Not needing to comment for static
as much will be nice, too.
@@ -110,6 +110,7 @@ cc_fuzz_test( | |||
name = "explorer_fuzzer", | |||
size = "small", | |||
srcs = ["explorer_fuzzer.cpp"], | |||
copts = ["-Wno-missing-prototypes"], |
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.
Why this approach here, versus what's done for other fuzzers?
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.
Because the explorer uses macros and such to instantiate the proto-based fuzzer. I didn't want to guess at what the right or wrong declarations would be, so I just turned off the warning. It also seemed more in line with disabling clang-tidy before spending more time trying to figure out a principled solution.
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.
Thanks for explaining, would it be worth adding a small comment to this effect?
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.
Adding comment.
87b266b
to
9f363af
Compare
Most of these are places where we failed to include a header file and simply never got an error about this. The fix is to include the header file. Most other cases are functions that should have been marked `static` but were not. Finding all of these was a main motivation for me enabling the warning despite how much work it is. One complicating factor was that we weren't including the `handle.h` for all the state-based handler functions. While this isn't a tiny amount of code, it is just declarations and doesn't add any extra dependencies. It also lets us have the checking for which functions need to be `static` and which don't. For the `parse` library I had to add the `handle.h` header as well, I tried to match the design of it in `check`. I have also had to work around a bug in the warning, but given the value it seems to be providing, that seems reasonable. I've filed the bug upstream: llvm/llvm-project#94138 I also had to use some hacks to work around limitations of Bazel rules that wrap `cc_library` rules and don't expose `copts`. I filed a bug for `cc_proto_library` specifically: bazelbuild/bazel#22610
9f363af
to
8375962
Compare
PTAL and lemme know if this is good, or a different approach. Happy to also look into class methods or something else if more appropriate. |
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.
Note there's still a build failure, maybe the changes caused a clang-tidy issue to be flagged?
But feel free to merge once that's fixed.
@@ -110,6 +110,7 @@ cc_fuzz_test( | |||
name = "explorer_fuzzer", | |||
size = "small", | |||
srcs = ["explorer_fuzzer.cpp"], | |||
copts = ["-Wno-missing-prototypes"], |
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.
Thanks for explaining, would it be worth adding a small comment to this effect?
Most of these are places where we failed to include a header file and
simply never got an error about this. The fix is to include the header
file.
Most other cases are functions that should have been marked
static
butwere not. Finding all of these was a main motivation for me enabling the
warning despite how much work it is.
One complicating factor was that we weren't including the
handle.h
forall the state-based handler functions. While this isn't a tiny amount of
code, it is just declarations and doesn't add any extra dependencies. It
also lets us have the checking for which functions need to be
static
and which don't. For the
parse
library I had to add thehandle.h
header as well, I tried to match the design of it in
check
.I have also had to work around a bug in the warning, but given the value
it seems to be providing, that seems reasonable. I've filed the bug
upstream: llvm/llvm-project#94138
I also had to use some hacks to work around limitations of Bazel rules
that wrap
cc_library
rules and don't exposecopts
. I filed a bug forcc_proto_library
specifically:bazelbuild/bazel#22610bazelbuild/bazel#4446