[clang] [CFG] Add a BuildOption to consider default branch of switch on covered enumerations. (PR #161345)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 2 00:40:17 PDT 2025
================
@@ -93,6 +93,158 @@ TEST(CFG, DependantBaseAddImplicitDtors) {
.getStatus());
}
+TEST(CFG, SwitchCoveredEnumNoDefault) {
+ const char *Code = R"(
+ enum class E {E1, E2};
+ int f(E e) {
+ switch(e) {
+ case E::E1:
+ return 1;
+ case E::E2:
+ return 2;
+ }
+ return 0;
+ }
+ )";
+ CFG::BuildOptions Options;
+ Options.SwitchKeepDefaultCoveredEnum = true;
+ BuildResult B = BuildCFG(Code, Options);
+ EXPECT_EQ(BuildResult::BuiltCFG, B.getStatus());
+
+ // [B5 (ENTRY)]
+ // Succs (1): B2
+ //
+ // [B1]
+ // 1: 0
+ // 2: return [B1.1];
+ // Preds (1): B2
+ // Succs (1): B0
+ //
+ // [B2]
+ // 1: e (ImplicitCastExpr, LValueToRValue, E)
+ // T: switch [B2.1]
+ // Preds (1): B5
+ // Succs (3): B3 B4 B1
+ //
+ // [B3]
+ // case E::E2:
+ // 1: 2
+ // 2: return [B3.1];
+ // Preds (1): B2
+ // Succs (1): B0
+ //
+ // [B4]
+ // case E::E1:
+ // 1: 1
+ // 2: return [B4.1];
+ // Preds (1): B2
+ // Succs (1): B0
+ //
+ // [B0 (EXIT)]
+ // Preds (3): B1 B3 B4
+
+ const auto &Entry = B.getCFG()->getEntry();
+ EXPECT_EQ(1u, Entry.succ_size());
----------------
steakhal wrote:
If this assertion is violated, the test moves on and trips on UB, potentially crashing the test process, bringing down the whole gtest.
I'd suggest swapping the `EXPECT_*` assertions to `ASSERT_*` to end the test case with a failure. This should prevent subsequent UBs to materialize.
As a side note, I can't resist to mention that such size checks are actively harmful in the sense that in case of a failure they only tell us that something was wrong, but holds back all other context. Ideally, if a test fails, the output should have everything to judge what went wrong and how to fix it.
But such size checks holds back the rest of the valuable expectation checks from materializing.
That's why dumping and diffing leads to better devX.
In this case, it would tell us, what entries are missing or what entries were unexpected in case of a failure.
Disregard this rant, as the rest of the tests anyway don't obey this principle; and changing any of this is out of scope here.
https://github.com/llvm/llvm-project/pull/161345
More information about the cfe-commits
mailing list