[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-22 Thread Reid Kleckner via cfe-commits

https://github.com/rnk approved this pull request.

Thanks!

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-22 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem updated 
https://github.com/llvm/llvm-project/pull/138562

>From e221ba3b0f7b08bcfc56bf75f7505265c332637d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Mon, 5 May 2025 20:24:15 +0200
Subject: [PATCH 1/9] [Sema] Warn about omitting deprecated enumerator in
 switch

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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.
---
 clang/lib/Sema/SemaStmt.cpp   | 6 +-
 clang/test/Sema/switch-availability.c | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

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 {

>From 6bc923d27d77009b37de12c9e33d2e83835d6a4d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 09:31:50 +0200
Subject: [PATCH 2/9] check for -Wreturn-type

---
 clang/test/Sema/switch-availability.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index b4f8726addc0b..137cdc976ec2d 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -Wswitch -triple x86_64-apple-macosx10.12 %s
+// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -triple 
x86_64-apple-macosx10.12 %s
 
 enum SwitchOne {
   Unavail __attribute__((availability(macos, unavailable))),
@@ -25,3 +25,16 @@ enum SwitchThree {
 void testSwitchThree(enum SwitchThree st) {
   switch (st) {} // expected-warning{{enumeration value 'New' not handled in 
switch}}
 }
+
+enum SwitchFour {
+  Red,
+  Green,
+  Blue [[deprecated]]
+};
+
+int testSwitchFour(enum SwitchFour e) {
+  switch (e) { // expected-warning{{enumeration value 'Blue' not handled in 
switch}}
+  case Red:   return 1;
+  case Green: return 2;
+  }
+} // expected-warning{{non-void function does not return a value in all 
control paths}}

>From 432ab6cb5bc2fe11a575146e53af1083a96c8405 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 16:30:25 +0200
Subject: [PATCH 3/9] don't forget the oxford comma

---
 clang/test/Sema/switch-availability.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index 137cdc976ec2d..fc44375fc83a0 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 'Ed', 'Vim' and 
'Emacs' not handled in switch}}
+  switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim', and 
'Emacs' not handled in switch}}
 }
 
 enum SwitchThree {

>From 6530e7faae5be297bdfcaae7baee44b7321eb3c4 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Thu, 15 May 2025 12:56:05 +0200
Subject: [PATCH 4/9] suppress 'deprecated' warning in case expressions

---
 clang/include/clang/Sema/Sema.h  | 3 +++
 clang/lib/Parse/ParseExpr.cpp| 3 +++
 clang/lib/Sema/SemaAvailability.cpp  | 5 +
 clang/lib/Sema/SemaStmt.cpp  | 4 ++--
 clang/test/Sema/switch-availability.c| 8 
 clang/test/SemaObjC/unguarded-availability.m | 6 +++---
 6 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 19343eb0af092..bb69930446177 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -6767,6 +6767,9 @

[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-22 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman approved this pull request.

Please add a release note to `clang/docs/ReleaseNotes.rst` so users know about 
the new functionality and flag.

Otherwise, LGTM!

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-22 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> Thanks Aaron, that's a good example.
> 
> This is a pickle; it doesn't seem like there's an obviously Right 
> Solution(tm) here.

That's the same conclusion I'm coming to. These situations are kind of mutually 
exclusive.

> I think we're agreeing on the first part, that unhandled deprecated enums 
> should trigger the "not covered" warning.

Yup!

> Some users will prefer to handle that with a `default:` to avoid using the 
> deprecated enum, while others will want to handle the enum explicitly and 
> suppress the warning.
> 
> Corentin's flag suggestion seems like the best way to satisfy both camps. 
> I've updated the PR.

I agree, thank you for heading that direction (and good suggestion @cor3ntin)!

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-22 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem updated 
https://github.com/llvm/llvm-project/pull/138562

>From e221ba3b0f7b08bcfc56bf75f7505265c332637d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Mon, 5 May 2025 20:24:15 +0200
Subject: [PATCH 1/8] [Sema] Warn about omitting deprecated enumerator in
 switch

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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.
---
 clang/lib/Sema/SemaStmt.cpp   | 6 +-
 clang/test/Sema/switch-availability.c | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

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 {

>From 6bc923d27d77009b37de12c9e33d2e83835d6a4d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 09:31:50 +0200
Subject: [PATCH 2/8] check for -Wreturn-type

---
 clang/test/Sema/switch-availability.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index b4f8726addc0b..137cdc976ec2d 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -Wswitch -triple x86_64-apple-macosx10.12 %s
+// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -triple 
x86_64-apple-macosx10.12 %s
 
 enum SwitchOne {
   Unavail __attribute__((availability(macos, unavailable))),
@@ -25,3 +25,16 @@ enum SwitchThree {
 void testSwitchThree(enum SwitchThree st) {
   switch (st) {} // expected-warning{{enumeration value 'New' not handled in 
switch}}
 }
+
+enum SwitchFour {
+  Red,
+  Green,
+  Blue [[deprecated]]
+};
+
+int testSwitchFour(enum SwitchFour e) {
+  switch (e) { // expected-warning{{enumeration value 'Blue' not handled in 
switch}}
+  case Red:   return 1;
+  case Green: return 2;
+  }
+} // expected-warning{{non-void function does not return a value in all 
control paths}}

>From 432ab6cb5bc2fe11a575146e53af1083a96c8405 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 16:30:25 +0200
Subject: [PATCH 3/8] don't forget the oxford comma

---
 clang/test/Sema/switch-availability.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index 137cdc976ec2d..fc44375fc83a0 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 'Ed', 'Vim' and 
'Emacs' not handled in switch}}
+  switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim', and 
'Emacs' not handled in switch}}
 }
 
 enum SwitchThree {

>From 6530e7faae5be297bdfcaae7baee44b7321eb3c4 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Thu, 15 May 2025 12:56:05 +0200
Subject: [PATCH 4/8] suppress 'deprecated' warning in case expressions

---
 clang/include/clang/Sema/Sema.h  | 3 +++
 clang/lib/Parse/ParseExpr.cpp| 3 +++
 clang/lib/Sema/SemaAvailability.cpp  | 5 +
 clang/lib/Sema/SemaStmt.cpp  | 4 ++--
 clang/test/Sema/switch-availability.c| 8 
 clang/test/SemaObjC/unguarded-availability.m | 6 +++---
 6 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 19343eb0af092..bb69930446177 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -6767,6 +6767,9 @

[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-22 Thread Hans Wennborg via cfe-commits


@@ -6009,6 +6009,8 @@ def note_not_found_by_two_phase_lookup : Note<"%0 should 
be declared prior to th
 def err_undeclared_use : Error<"use of undeclared %0">;
 def warn_deprecated : Warning<"%0 is deprecated">,
 InGroup;
+def warn_deprecated_switch_case : Warning<"%0 is deprecated">,

zmodem wrote:

Thanks! Done.

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-21 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem updated 
https://github.com/llvm/llvm-project/pull/138562

>From e221ba3b0f7b08bcfc56bf75f7505265c332637d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Mon, 5 May 2025 20:24:15 +0200
Subject: [PATCH 1/7] [Sema] Warn about omitting deprecated enumerator in
 switch

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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.
---
 clang/lib/Sema/SemaStmt.cpp   | 6 +-
 clang/test/Sema/switch-availability.c | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

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 {

>From 6bc923d27d77009b37de12c9e33d2e83835d6a4d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 09:31:50 +0200
Subject: [PATCH 2/7] check for -Wreturn-type

---
 clang/test/Sema/switch-availability.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index b4f8726addc0b..137cdc976ec2d 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -Wswitch -triple x86_64-apple-macosx10.12 %s
+// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -triple 
x86_64-apple-macosx10.12 %s
 
 enum SwitchOne {
   Unavail __attribute__((availability(macos, unavailable))),
@@ -25,3 +25,16 @@ enum SwitchThree {
 void testSwitchThree(enum SwitchThree st) {
   switch (st) {} // expected-warning{{enumeration value 'New' not handled in 
switch}}
 }
+
+enum SwitchFour {
+  Red,
+  Green,
+  Blue [[deprecated]]
+};
+
+int testSwitchFour(enum SwitchFour e) {
+  switch (e) { // expected-warning{{enumeration value 'Blue' not handled in 
switch}}
+  case Red:   return 1;
+  case Green: return 2;
+  }
+} // expected-warning{{non-void function does not return a value in all 
control paths}}

>From 432ab6cb5bc2fe11a575146e53af1083a96c8405 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 16:30:25 +0200
Subject: [PATCH 3/7] don't forget the oxford comma

---
 clang/test/Sema/switch-availability.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index 137cdc976ec2d..fc44375fc83a0 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 'Ed', 'Vim' and 
'Emacs' not handled in switch}}
+  switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim', and 
'Emacs' not handled in switch}}
 }
 
 enum SwitchThree {

>From 6530e7faae5be297bdfcaae7baee44b7321eb3c4 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Thu, 15 May 2025 12:56:05 +0200
Subject: [PATCH 4/7] suppress 'deprecated' warning in case expressions

---
 clang/include/clang/Sema/Sema.h  | 3 +++
 clang/lib/Parse/ParseExpr.cpp| 3 +++
 clang/lib/Sema/SemaAvailability.cpp  | 5 +
 clang/lib/Sema/SemaStmt.cpp  | 4 ++--
 clang/test/Sema/switch-availability.c| 8 
 clang/test/SemaObjC/unguarded-availability.m | 6 +++---
 6 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 19343eb0af092..bb69930446177 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -6767,6 +6767,9 @

[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-21 Thread Reid Kleckner via cfe-commits


@@ -6009,6 +6009,8 @@ def note_not_found_by_two_phase_lookup : Note<"%0 should 
be declared prior to th
 def err_undeclared_use : Error<"use of undeclared %0">;
 def warn_deprecated : Warning<"%0 is deprecated">,
 InGroup;
+def warn_deprecated_switch_case : Warning<"%0 is deprecated">,

rnk wrote:

You can reuse diagnostic text with `warn_deprecated.Summary`. See other 
examples like:
```
def ext_vla_cxx_in_gnu_mode : Extension,
```

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-21 Thread Hans Wennborg via cfe-commits

zmodem wrote:

Thanks Aaron, that's a good example.

This is a pickle; it doesn't seem like there's an obviously Right Solution(tm) 
here.

I think we're agreeing on the first part, that unhandled deprecated enums 
should trigger the "not covered" warning.

Some users will prefer to handle that with a `default:` to avoid using the 
deprecated enum, while others will want to handle the enum explicitly and 
suppress the warning.

Corentin's flag suggestion seems like the best way to satisfy both camps. I've 
updated the PR.

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-20 Thread Reid Kleckner via cfe-commits

rnk wrote:

> I think silencing the warning is better than suggesting a default case, which 
> may not be considered good practice.

I'm not sure, I think our perspectives as compiler people might be a bit off 
base. We're always forming closed algebraic sum types, like variants, AST 
nodes, that kind of thing. I think normal programs dealing with untrusted input 
coming from a file or over the wire typically need to be more conservative, and 
pushing users towards adding a `default` case or code to the fallthrough block 
without explicitly mentioning the deprecated enumerator might be the best 
outcome. Consider that -Wcovered-switch-default is not part of -Wall/-Wextra: 
https://godbolt.org/z/PrjqqxKrP

> That code will continue to work great right up until the enumerator is 
> removed.

To Aaron's point about deprecation being a tool for removal, if the goal of 
deprecation is to remove the enumerator, then we *do* want folks to use 
`default`/fallthrough code patterns that don't explicitly mention the enum.


https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-20 Thread Hans Wennborg via cfe-commits

zmodem wrote:

> Now we have a special Wdeprecated-declarations carveouts for switches, but if 
> you unpack the switch into if / else chain comparisons, you get deprecation 
> warnings.

Agreed, that it is a bit inconsistent. There is prior art though: 
ba87c626f9b7c48a79673d3fd682c2a1e80a7200 makes exactly the same carve-out for 
-Wunguarded-availability (though the mechanism is different).

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-20 Thread Hans Wennborg via cfe-commits


@@ -6767,6 +6767,9 @@ class Sema final : public SemaBase {
 };
 std::optional DelayedDefaultInitializationContext;
 
+/// Whether evaluating an expression for a switch case label.

zmodem wrote:

Done.

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-20 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem updated 
https://github.com/llvm/llvm-project/pull/138562

>From e221ba3b0f7b08bcfc56bf75f7505265c332637d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Mon, 5 May 2025 20:24:15 +0200
Subject: [PATCH 1/6] [Sema] Warn about omitting deprecated enumerator in
 switch

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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.
---
 clang/lib/Sema/SemaStmt.cpp   | 6 +-
 clang/test/Sema/switch-availability.c | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

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 {

>From 6bc923d27d77009b37de12c9e33d2e83835d6a4d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 09:31:50 +0200
Subject: [PATCH 2/6] check for -Wreturn-type

---
 clang/test/Sema/switch-availability.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index b4f8726addc0b..137cdc976ec2d 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -Wswitch -triple x86_64-apple-macosx10.12 %s
+// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -triple 
x86_64-apple-macosx10.12 %s
 
 enum SwitchOne {
   Unavail __attribute__((availability(macos, unavailable))),
@@ -25,3 +25,16 @@ enum SwitchThree {
 void testSwitchThree(enum SwitchThree st) {
   switch (st) {} // expected-warning{{enumeration value 'New' not handled in 
switch}}
 }
+
+enum SwitchFour {
+  Red,
+  Green,
+  Blue [[deprecated]]
+};
+
+int testSwitchFour(enum SwitchFour e) {
+  switch (e) { // expected-warning{{enumeration value 'Blue' not handled in 
switch}}
+  case Red:   return 1;
+  case Green: return 2;
+  }
+} // expected-warning{{non-void function does not return a value in all 
control paths}}

>From 432ab6cb5bc2fe11a575146e53af1083a96c8405 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 16:30:25 +0200
Subject: [PATCH 3/6] don't forget the oxford comma

---
 clang/test/Sema/switch-availability.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index 137cdc976ec2d..fc44375fc83a0 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 'Ed', 'Vim' and 
'Emacs' not handled in switch}}
+  switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim', and 
'Emacs' not handled in switch}}
 }
 
 enum SwitchThree {

>From 6530e7faae5be297bdfcaae7baee44b7321eb3c4 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Thu, 15 May 2025 12:56:05 +0200
Subject: [PATCH 4/6] suppress 'deprecated' warning in case expressions

---
 clang/include/clang/Sema/Sema.h  | 3 +++
 clang/lib/Parse/ParseExpr.cpp| 3 +++
 clang/lib/Sema/SemaAvailability.cpp  | 5 +
 clang/lib/Sema/SemaStmt.cpp  | 4 ++--
 clang/test/Sema/switch-availability.c| 8 
 clang/test/SemaObjC/unguarded-availability.m | 6 +++---
 6 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 19343eb0af092..bb69930446177 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -6767,6 +6767,9 @

[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-20 Thread Reid Kleckner via cfe-commits

https://github.com/rnk edited https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-20 Thread Reid Kleckner via cfe-commits

https://github.com/rnk commented:

I'm OK with this, but I feel like this is creating scope creep. Now we have a 
special Wdeprecated-declarations carveouts for switches, but if you unpack the 
switch into if / else chain comparisons, you get deprecation warnings. Should 
we disable deprecation warnings that directly compare enumerators? I wouldn't 
want to expand scope that much.

It is possible to write code that avoids mentioning the deprecated enum (add a 
default, and/or fallthrough code) and silences -Wreturn-type, so I have a soft 
preference for going back to the old version.

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-20 Thread Reid Kleckner via cfe-commits


@@ -6767,6 +6767,9 @@ class Sema final : public SemaBase {
 };
 std::optional DelayedDefaultInitializationContext;
 
+/// Whether evaluating an expression for a switch case label.

rnk wrote:

supernit: pack it with the contextual bools above, for both layout reasons and 
because it shows there's prior art

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-15 Thread Hans Wennborg via cfe-commits

zmodem wrote:

I've added suppression of deprecated values in case expressions. Please take 
another look!

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-15 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem updated 
https://github.com/llvm/llvm-project/pull/138562



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-15 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem updated 
https://github.com/llvm/llvm-project/pull/138562

>From e221ba3b0f7b08bcfc56bf75f7505265c332637d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Mon, 5 May 2025 20:24:15 +0200
Subject: [PATCH 1/4] [Sema] Warn about omitting deprecated enumerator in
 switch

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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.
---
 clang/lib/Sema/SemaStmt.cpp   | 6 +-
 clang/test/Sema/switch-availability.c | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

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 {

>From 6bc923d27d77009b37de12c9e33d2e83835d6a4d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 09:31:50 +0200
Subject: [PATCH 2/4] check for -Wreturn-type

---
 clang/test/Sema/switch-availability.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index b4f8726addc0b..137cdc976ec2d 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -Wswitch -triple x86_64-apple-macosx10.12 %s
+// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -triple 
x86_64-apple-macosx10.12 %s
 
 enum SwitchOne {
   Unavail __attribute__((availability(macos, unavailable))),
@@ -25,3 +25,16 @@ enum SwitchThree {
 void testSwitchThree(enum SwitchThree st) {
   switch (st) {} // expected-warning{{enumeration value 'New' not handled in 
switch}}
 }
+
+enum SwitchFour {
+  Red,
+  Green,
+  Blue [[deprecated]]
+};
+
+int testSwitchFour(enum SwitchFour e) {
+  switch (e) { // expected-warning{{enumeration value 'Blue' not handled in 
switch}}
+  case Red:   return 1;
+  case Green: return 2;
+  }
+} // expected-warning{{non-void function does not return a value in all 
control paths}}

>From 432ab6cb5bc2fe11a575146e53af1083a96c8405 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 16:30:25 +0200
Subject: [PATCH 3/4] don't forget the oxford comma

---
 clang/test/Sema/switch-availability.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index 137cdc976ec2d..fc44375fc83a0 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 'Ed', 'Vim' and 
'Emacs' not handled in switch}}
+  switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim', and 
'Emacs' not handled in switch}}
 }
 
 enum SwitchThree {

>From 6530e7faae5be297bdfcaae7baee44b7321eb3c4 Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Thu, 15 May 2025 12:56:05 +0200
Subject: [PATCH 4/4] suppress 'deprecated' warning in case expressions

---
 clang/include/clang/Sema/Sema.h  | 3 +++
 clang/lib/Parse/ParseExpr.cpp| 3 +++
 clang/lib/Sema/SemaAvailability.cpp  | 5 +
 clang/lib/Sema/SemaStmt.cpp  | 4 ++--
 clang/test/Sema/switch-availability.c| 8 
 clang/test/SemaObjC/unguarded-availability.m | 6 +++---
 6 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 19343eb0af092..bb69930446177 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -6767,6 +6767,9 @

[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-14 Thread via cfe-commits

cor3ntin wrote:

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

I think silencing the warning is better than suggesting a default case, which 
may not be considered good practice.
Maybe we can add a -Wdeprecated-switch-case warning that people can control 
independently of  -Wdeprecated-declarations

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-06 Thread Hans Wennborg via cfe-commits

zmodem wrote:

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?

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-06 Thread Hans Wennborg via cfe-commits

zmodem wrote:

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

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-06 Thread Hans Wennborg via cfe-commits


@@ -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}}
 }
 

zmodem wrote:

Done.

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-06 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem updated 
https://github.com/llvm/llvm-project/pull/138562

>From e221ba3b0f7b08bcfc56bf75f7505265c332637d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Mon, 5 May 2025 20:24:15 +0200
Subject: [PATCH 1/2] [Sema] Warn about omitting deprecated enumerator in
 switch

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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.
---
 clang/lib/Sema/SemaStmt.cpp   | 6 +-
 clang/test/Sema/switch-availability.c | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

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 {

>From 6bc923d27d77009b37de12c9e33d2e83835d6a4d Mon Sep 17 00:00:00 2001
From: Hans Wennborg 
Date: Tue, 6 May 2025 09:31:50 +0200
Subject: [PATCH 2/2] check for -Wreturn-type

---
 clang/test/Sema/switch-availability.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/switch-availability.c 
b/clang/test/Sema/switch-availability.c
index b4f8726addc0b..137cdc976ec2d 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -Wswitch -triple x86_64-apple-macosx10.12 %s
+// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -triple 
x86_64-apple-macosx10.12 %s
 
 enum SwitchOne {
   Unavail __attribute__((availability(macos, unavailable))),
@@ -25,3 +25,16 @@ enum SwitchThree {
 void testSwitchThree(enum SwitchThree st) {
   switch (st) {} // expected-warning{{enumeration value 'New' not handled in 
switch}}
 }
+
+enum SwitchFour {
+  Red,
+  Green,
+  Blue [[deprecated]]
+};
+
+int testSwitchFour(enum SwitchFour e) {
+  switch (e) { // expected-warning{{enumeration value 'Blue' not handled in 
switch}}
+  case Red:   return 1;
+  case Green: return 2;
+  }
+} // expected-warning{{non-void function does not return a value in all 
control paths}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-05 Thread via cfe-commits

ReticulateSplines wrote:

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.

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-05 Thread Reid Kleckner via cfe-commits

https://github.com/rnk commented:

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

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-05 Thread Reid Kleckner via cfe-commits


@@ -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}}
 }
 

rnk wrote:

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}}
```

https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-05 Thread Reid Kleckner via cfe-commits

https://github.com/rnk edited https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-05 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Hans Wennborg (zmodem)


Changes

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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
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 {

``




https://github.com/llvm/llvm-project/pull/138562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema] Warn about omitting deprecated enumerator in switch (PR #138562)

2025-05-05 Thread Hans Wennborg via cfe-commits

https://github.com/zmodem created 
https://github.com/llvm/llvm-project/pull/138562

This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 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.



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits