Skip to content

Commit 5020574

Browse files
committed
Enhance -Wreturn-type to not warn when control-flow is most likely limited by a switch statement explicitly covering
all the cases for an enum value. llvm-svn: 113450
1 parent b037185 commit 5020574

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ static ControlFlowKind CheckFallThrough(AnalysisContext &AC) {
108108
bool HasFakeEdge = false;
109109
bool HasPlainEdge = false;
110110
bool HasAbnormalEdge = false;
111-
for (CFGBlock::pred_iterator I=cfg->getExit().pred_begin(),
112-
E = cfg->getExit().pred_end();
113-
I != E;
114-
++I) {
115-
CFGBlock& B = **I;
111+
112+
// Ignore default cases that aren't likely to be reachable because all
113+
// enums in a switch(X) have explicit case statements.
114+
CFGBlock::FilterOptions FO;
115+
FO.IgnoreDefaultsWithCoveredEnums = 1;
116+
117+
for (CFGBlock::filtered_pred_iterator
118+
I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) {
119+
const CFGBlock& B = **I;
116120
if (!live[B.getBlockID()])
117121
continue;
118122
if (B.size() == 0) {

clang/test/Sema/return.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,16 @@ static inline int si_forward() {} // expected-warning{{control reaches end of no
242242
// Test warnings on ignored qualifiers on return types.
243243
const int ignored_c_quals(); // expected-warning{{'const' type qualifier on return type has no effect}}
244244
const volatile int ignored_cv_quals(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
245+
246+
// Test that for switch(enum) that if the switch statement covers all the cases
247+
// that we don't consider that for -Wreturn-type.
248+
enum Cases { C1, C2, C3, C4 };
249+
int test_enum_cases(enum Cases C) {
250+
switch (C) {
251+
case C1: return 1;
252+
case C2: return 2;
253+
case C4: return 3;
254+
case C3: return 4;
255+
}
256+
} // no-warning
257+

0 commit comments

Comments
 (0)