Skip to content

[clang] Add -Wimplicit-fallthrough to -Wextra #97926

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vegerot
Copy link
Contributor

@vegerot vegerot commented Jul 7, 2024

This patch adds -Wimplicit-fallthrough to -Wextra. GCC already includes it in -Wextra.

This patch also adds a test to check that -Wimplicit-fallthrough is included in -Wextra.

Note: This patch may regress performance when building with -Wextra.
This is because -Wextra requires forming a CFG for every function.

Copy link

github-actions bot commented Jul 7, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@vegerot vegerot marked this pull request as ready for review July 7, 2024 00:29
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 7, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 7, 2024

@llvm/pr-subscribers-libcxx

@llvm/pr-subscribers-clang

Author: Max Coplan (vegerot)

Changes

This patch adds -Wimplicit-fallthrough to -Wextra. GCC already includes it in -Wextra.

This patch also adds a test to check that -Wimplicit-fallthrough is included in -Wextra.

Note: This patch may regress performance when building with -Wextra.
This is because -Wextra requires forming a CFG for every function.


Full diff: https://github.com/llvm/llvm-project/pull/97926.diff

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticGroups.td (+1)
  • (modified) clang/test/Sema/fallthrough-attr.c (+1)
  • (modified) clang/test/SemaCXX/switch-implicit-fallthrough-macro.cpp (+1)
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..a875f665b10f9 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1063,6 +1063,7 @@ def Extra : DiagGroup<"extra", [
     StringConcatation,
     FUseLdPath,
     CastFunctionTypeMismatch,
+    ImplicitFallthrough,
   ]>;
 
 def Most : DiagGroup<"most", [
diff --git a/clang/test/Sema/fallthrough-attr.c b/clang/test/Sema/fallthrough-attr.c
index de50ebf39d42f..6cc19136f30a7 100644
--- a/clang/test/Sema/fallthrough-attr.c
+++ b/clang/test/Sema/fallthrough-attr.c
@@ -2,6 +2,7 @@
 // RUN: %clang_cc1 -fsyntax-only -std=gnu99 -verify -Wimplicit-fallthrough %s
 // RUN: %clang_cc1 -fsyntax-only -std=c99 -verify -Wimplicit-fallthrough %s
 // RUN: %clang_cc1 -fsyntax-only -std=c11 -verify -Wimplicit-fallthrough %s
+// RUN: %clang_cc1 -fsyntax-only -std=c11 -verify -Wextra %s
 // RUN: %clang_cc1 -fsyntax-only -std=c2x -DC2X -verify -Wimplicit-fallthrough %s
 
 int fallthrough_attribute_spelling(int n) {
diff --git a/clang/test/SemaCXX/switch-implicit-fallthrough-macro.cpp b/clang/test/SemaCXX/switch-implicit-fallthrough-macro.cpp
index 11df2cbfb53f0..cbbff1f1793b8 100644
--- a/clang/test/SemaCXX/switch-implicit-fallthrough-macro.cpp
+++ b/clang/test/SemaCXX/switch-implicit-fallthrough-macro.cpp
@@ -3,6 +3,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z -Wimplicit-fallthrough -DCLANG_PREFIX -DCOMMAND_LINE_FALLTHROUGH=[[clang::fallthrough]] %s
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z -Wimplicit-fallthrough -DCOMMAND_LINE_FALLTHROUGH=[[clang::fallthrough]] %s
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z -Wimplicit-fallthrough -DCOMMAND_LINE_FALLTHROUGH=[[fallthrough]] -DUNCHOSEN=[[clang::fallthrough]] %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z -Wextra -DCOMMAND_LINE_FALLTHROUGH=[[fallthrough]] -DUNCHOSEN=[[clang::fallthrough]] %s
 
 int fallthrough_compatibility_macro_from_command_line(int n) {
   switch (n) {

@vegerot
Copy link
Contributor Author

vegerot commented Jul 7, 2024

@AaronBallman please review

I don't know how to go about measuring if there even is a performance regression. How would I do that?

@vegerot vegerot force-pushed the add-implicit-fallthrough-to-wextra branch from 2ba0fe2 to c05d739 Compare July 7, 2024 00:33
@AaronBallman
Copy link
Collaborator

@AaronBallman please review

I don't know how to go about measuring if there even is a performance regression. How would I do that?

I would put up a branch on http://llvm-compile-time-tracker.com (instructions in the About page) and see how this impacts compile times. We've always left diagnostics out of -Wextra that require a CFG, so getting performance measurements is important.

@vegerot
Copy link
Contributor Author

vegerot commented Jul 20, 2024

I would put up a branch on http://llvm-compile-time-tracker.com (instructions in the About page) and see how this impacts compile times. We've always left diagnostics out of -Wextra that require a CFG, so getting performance measurements is important.

Thanks! The About page says

If you are an LLVM contributor who regularly does compile-time sensitive work

Do you think I should still ask for permission or should I leave it to someone else?

Update: I reached out to @nikic and CCd you

@vegerot vegerot force-pushed the add-implicit-fallthrough-to-wextra branch from c05d739 to 9640f00 Compare July 20, 2024 21:00
@nikic
Copy link
Contributor

nikic commented Jul 21, 2024

CTMark is not compiled with -Wextra, so we'd not get any useful data out of this patch. What changes does one have to do to enable -Wimplicit-fallthrough by default (not just for -Wextra)?

@vegerot
Copy link
Contributor Author

vegerot commented Jul 22, 2024

CTMark is not compiled with -Wextra, so we'd not get any useful data out of this patch. What changes does one have to do to enable -Wimplicit-fallthrough by default (not just for -Wextra)?

@nikic done! At vegerot/llvm-project/perf/add-implicit-fallthrough-to-wextra

@nikic
Copy link
Contributor

nikic commented Jul 25, 2024

I'm not seeing any compile time impact from dropping the DefaultIgnores on warn_unannotated_fallthrough/warn_unannotated_fallthrough_per_function. So either it's free or I'm still doing something wrong. (http://llvm-compile-time-tracker.com/compare.php?from=6d12b3f67df429bffff6e1953d9f55867d7e2469&to=4117b087cc607686c472f54f82f1222688b5f2bf&stat=instructions:u)

@vegerot
Copy link
Contributor Author

vegerot commented Jul 25, 2024

I'm pretty sure I enabled the warning correctly. @AaronBallman are we good to land?

@AaronBallman
Copy link
Collaborator

I'm not seeing any compile time impact from dropping the DefaultIgnores on warn_unannotated_fallthrough/warn_unannotated_fallthrough_per_function. So either it's free or I'm still doing something wrong. (http://llvm-compile-time-tracker.com/compare.php?from=6d12b3f67df429bffff6e1953d9f55867d7e2469&to=4117b087cc607686c472f54f82f1222688b5f2bf&stat=instructions:u)

Yeah, that logic should suffice:

  bool FallThroughDiagFull =
      !Diags.isIgnored(diag::warn_unannotated_fallthrough, D->getBeginLoc());
  bool FallThroughDiagPerFunction = !Diags.isIgnored(
      diag::warn_unannotated_fallthrough_per_function, D->getBeginLoc());
  if (FallThroughDiagFull || FallThroughDiagPerFunction ||
      fscope->HasFallthroughStmt) {
    DiagnoseSwitchLabelsFallthrough(S, AC, !FallThroughDiagFull);
  }

so unless something explicitly ignores the fallthrough warning in the setup, it seems like a valid test.

@AaronBallman
Copy link
Collaborator

I'm pretty sure I enabled the warning correctly. @AaronBallman are we good to land?

Not quite -- it looks like this change is causing libc++ test failures that were caught by precommit CI:

# | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-jltnz-1/llvm-project/github-pull-requests/build-runtimes/include/c++/v1/regex:3925:5: note: insert '[[clang::fallthrough]];' to silence this warning
# |  3925 |     case 'x':
# |       |     ^
# |       |     [[clang::fallthrough]];
# | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-jltnz-1/llvm-project/github-pull-requests/build-runtimes/include/c++/v1/regex:3925:5: note: insert 'break;' to avoid fall-through
# |  3925 |     case 'x':
# |       |     ^
# |       |     break;
# | 1 error generated.

CC @ldionne

@vegerot
Copy link
Contributor Author

vegerot commented Jul 25, 2024

I'm pretty sure I enabled the warning correctly. @AaronBallman are we good to land?

Not quite -- it looks like this change is causing libc++ test failures that were caught by precommit CI:

Gotcha. Should I work on fixing the code?

@vegerot vegerot force-pushed the add-implicit-fallthrough-to-wextra branch from 9640f00 to cec4051 Compare July 26, 2024 22:03
@vegerot vegerot requested a review from a team as a code owner July 26, 2024 22:03
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jul 26, 2024
@vegerot
Copy link
Contributor Author

vegerot commented Jul 26, 2024

@AaronBallman I stacked this diff on top of #100821 to remove that build error.

question for reviewer: what is the proper etiquette for stacked diffs? Should I open two PRs or have one PR with two commits?

@vegerot vegerot marked this pull request as draft July 27, 2024 23:48
vegerot added a commit to vegerot/llvm-project that referenced this pull request Jul 28, 2024
…ning

Summary:
The diff llvm#97926 is stacked on top of this patch because this file
reports an error when enabling `-Wimplicit-fallthrough` in `-Wextra`.

Test plan:

```sh
$ time (mkdir build_runtimes && cd build_runtimes && set -x && CC=../build/bin/clang CXX=../build/bin/clang++ cmake -G Ninja ../runtimes -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RUNTIMES='libcxx;libcxxabi;libunwind'  && ninja && bin/llvm-lit -sv ../libcxx/test/std/re )
```

note: whether I put a `break;` or fallthrough, the tests pass anyways
which is sus.
@vegerot vegerot force-pushed the add-implicit-fallthrough-to-wextra branch from cec4051 to 5767b5f Compare July 28, 2024 21:02
@vegerot vegerot force-pushed the add-implicit-fallthrough-to-wextra branch from 5767b5f to 55e3359 Compare August 23, 2024 16:44
@vegerot vegerot marked this pull request as ready for review August 23, 2024 16:44
@vegerot
Copy link
Contributor Author

vegerot commented Aug 23, 2024

@mordante @AaronBallman This diff is ready for review 🙂

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Sorry for the long wait on this review! A related issue cropped up regarding compile time performance of -Wall (#105959) and it seems to be caused by CFG creation.

I think we should run the test case from that issue against the changes in this patch (-Wall includes -Wextra) to see if it makes an even more noticeable change in compile times, just to be sure before we go ahead and land this. I was hopeful that CFG building got faster, but now I worry there may be edge cases that make CFG building particularly slow that we may need to investigate.

@vegerot
Copy link
Contributor Author

vegerot commented Sep 15, 2024

@AaronBallman sure. Should I still use http://llvm-compile-time-tracker.com/ , or do I run the benchmark another way?

This patch adds -Wimplicit-fallthrough to -Wextra. GCC already includes it in -Wextra.

This patch also adds a test to check that -Wimplicit-fallthrough is included in -Wextra.

Note: This patch may regress performance when building with -Wextra.
This is because -Wextra requires forming a CFG for every function.
@vegerot vegerot force-pushed the add-implicit-fallthrough-to-wextra branch from 55e3359 to 7d221cb Compare September 15, 2024 02:04
@AaronBallman
Copy link
Collaborator

@AaronBallman sure. Should I still use http://llvm-compile-time-tracker.com/ , or do I run the benchmark another way?

Sorry, your question completely fell off my radar! llvm-compile-time-tracker would be fine, but even just a local test would suffice -- basically, I expect the test case from #105959 to be slow before you apply your patch and am just wondering whether that gets even slower with your patch.

@philnik777 philnik777 removed the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants