Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Sema] Warn about omitting deprecated enumerator in switch #138562

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 3 commits into
base: main
Choose a base branch
from

Conversation

zmodem
Copy link
Collaborator

@zmodem zmodem commented May 5, 2025

This undoes part of 3e4e3b1 which added the "Omitting a deprecated constant is ok; it should never materialize." logic.

That seems wrong: deprecated means the enumerator is likely to be removed in future versions, not that it cannot materialize.

This undoes part of 3e4e3b1 which
added the "Omitting a deprecated constant is ok; it should never
materialize." logic.

That seems wrong: deprecated means the enumerator is likely to be
removed in future versions, not that it canot materialize.
@zmodem zmodem requested review from rnk, hyp, AaronBallman and dwblaikie May 5, 2025 18:30
@zmodem zmodem added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label May 5, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 5, 2025
@llvmbot
Copy link
Member

llvmbot commented May 5, 2025

@llvm/pr-subscribers-clang

Author: Hans Wennborg (zmodem)

Changes

This undoes part of 3e4e3b1 which added the "Omitting a deprecated constant is ok; it should never materialize." logic.

That seems wrong: deprecated means the enumerator is likely to be removed in future versions, not that it cannot materialize.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaStmt.cpp (+5-1)
  • (modified) clang/test/Sema/switch-availability.c (+1-1)
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index e8c1f8490342a..990d2fadaf5aa 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -1667,8 +1667,12 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
         // Don't warn about omitted unavailable EnumConstantDecls.
         switch (EI->second->getAvailability()) {
         case AR_Deprecated:
-          // Omitting a deprecated constant is ok; it should never materialize.
+          // Deprecated enumerators still need to be handled: they may be
+          // deprecated, but can still occur.
+          break;
+
         case AR_Unavailable:
+          // Omitting an unavailable enumerator is ok; it should never occur.
           continue;
 
         case AR_NotYetIntroduced:
diff --git a/clang/test/Sema/switch-availability.c b/clang/test/Sema/switch-availability.c
index 888edddac463d..b4f8726addc0b 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -15,7 +15,7 @@ enum SwitchTwo {
 };
 
 void testSwitchTwo(enum SwitchTwo st) {
-  switch (st) {} // expected-warning{{enumeration values 'Vim' and 'Emacs' not handled in switch}}
+  switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim' and 'Emacs' not handled in switch}}
 }
 
 enum SwitchThree {

Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

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

Thanks! I think this is sufficiently niche that we don't need a flag flip to manage the diagnostic change fallout.

@@ -15,7 +15,7 @@ enum SwitchTwo {
};

void testSwitchTwo(enum SwitchTwo st) {
switch (st) {} // expected-warning{{enumeration values 'Vim' and 'Emacs' not handled in switch}}
switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim' and 'Emacs' not handled in switch}}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should have some test showing that the statement after the switch is considered reachable. You can build one with -Wreturn-type and an int return:

int doswitch(SwitchTwo e) {
  switch (e) {
  case Emacs: return 42;
  }
} // expected-warning {{asdf}}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@ReticulateSplines
Copy link

Fly-by comment: I drafted the following alternative comment in another thread. Please feel free to disregard as I don't know the tone and style of comments in these files. (But I appreciate your consideration for the contents in the message I'd like to see!)

      // We currently treat deprecated constants as if they don't exist;
      // however, note that this current behavior leads to compile-time
      // false negatives for coverage checking of switch statements. If
      // a switch is missing a case for a deprecated constant, we will not
      // emit a diagnostic, even though the deprecated constant might still
      // be present in legacy use.

@zmodem
Copy link
Collaborator Author

zmodem commented May 6, 2025

Fly-by comment: I drafted the following alternative comment in another thread. Please feel free to disregard as I don't know the tone and style of comments in these files. (But I appreciate your consideration for the contents in the message I'd like to see!)

      // We currently treat deprecated constants as if they don't exist;
      // however, note that this current behavior leads to compile-time
      // false negatives for coverage checking of switch statements. If
      // a switch is missing a case for a deprecated constant, we will not
      // emit a diagnostic, even though the deprecated constant might still
      // be present in legacy use.

My patch changes the behavior so that deprecated constants are not ignored by the switch coverage warning.

@zmodem
Copy link
Collaborator Author

zmodem commented May 6, 2025

I wonder if we should also do something special about the -Wdeprecated-declarations diagnostic in switches.

If the user fixes the -Wswitch / -Wreturn-type warnings from testSwitchFour the natural way:

int testSwitchFour(enum SwitchFour e) {
  switch (e) {
  case Red:   return 1;
  case Green: return 2;
  case Blue:  return 3;
  }
} 

They will get warning: 'Blue' is deprecated [-Wdeprecated-declarations] instead.

Maybe we should suppress that warning for switch cases? Or suggest adding a default label instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer 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.

4 participants