Skip to content
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

Merged
merged 3 commits into from
Jun 4, 2024

Conversation

chandlerc
Copy link
Contributor

@chandlerc chandlerc commented Jun 2, 2024

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
bazelbuild/bazel#4446

@github-actions github-actions bot added explorer Action items related to Carbon explorer code toolchain labels Jun 2, 2024
@github-actions github-actions bot requested review from jonmeow and zygoloid June 2, 2024 04:36
namespace Carbon::Parse {

// Declare handlers for each parse state.
#define CARBON_PARSE_STATE(Name) auto Handle##Name(Context& context) -> void;
Copy link
Contributor

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.)

Copy link
Contributor Author

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.

Copy link
Contributor

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.

utils/treesitter/src/scanner.c Show resolved Hide resolved
toolchain/parse/parse_fuzzer.cpp Outdated Show resolved Hide resolved
namespace Carbon::Parse {

// Declare handlers for each parse state.
#define CARBON_PARSE_STATE(Name) auto Handle##Name(Context& context) -> void;
Copy link
Contributor

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"],
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding comment.

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
@chandlerc
Copy link
Contributor Author

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.

Copy link
Contributor

@jonmeow jonmeow left a 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"],
Copy link
Contributor

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?

@chandlerc chandlerc enabled auto-merge June 4, 2024 18:09
@chandlerc chandlerc added this pull request to the merge queue Jun 4, 2024
Merged via the queue into carbon-language:trunk with commit 8c64f0b Jun 4, 2024
7 checks passed
@chandlerc chandlerc deleted the missing-prototypes branch June 4, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
explorer Action items related to Carbon explorer code toolchain
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants