[clang] [clang-tools-extra] [clang] Don't crash in HasSideEffects on a result-dependent _Generic (PR #248784)

Yousof Selim via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 6 00:04:51 PST 2026


https://github.com/yeegz updated https://github.com/llvm/llvm-project/pull/248784

>From 2bef38896eae49cd4f8764178c95dc13656b77ce Mon Sep 17 00:00:00 2001
From: Yousof Selim <ygshuh at gmail.com>
Date: Thu, 3 Dec 2026 08:50:59 +0800
Subject: [PATCH 1/2] [clang] Don't crash in HasSideEffects on a
 result-dependent _Generic

Expr::HasSideEffects looked through a GenericSelectionExpr to its
result expression, but a result-dependent selection has none, for
example when the controlling expression contains errors. clang-tidy's
misc-redundant-expression check reached this through
`1 && _Generic(v, default: 1) && 1` where `v` is initialized from an
undeclared identifier, and dereferenced a null result.

Treat a result-dependent selection as possibly having side effects,
like other dependent expressions.

Fixes #245254

Assisted-by: Claude Opus 5.5
---
 .../misc/redundant-expression-generic-no-crash.c      |  9 +++++++++
 clang/docs/ReleaseNotes.md                            |  4 ++++
 clang/lib/AST/Expr.cpp                                | 11 ++++++++---
 3 files changed, 21 insertions(+), 3 deletions(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-generic-no-crash.c

diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-generic-no-crash.c b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-generic-no-crash.c
new file mode 100644
index 00000000000000..0f86d47594551d
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-generic-no-crash.c
@@ -0,0 +1,9 @@
+// RUN: %check_clang_tidy -expect-clang-tidy-error %s misc-redundant-expression %t
+
+// A _Generic whose controlling expression contains errors has no result
+// expression. The check must not crash looking for side effects in it.
+void f(void) {
+  __auto_type v = undeclared;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: error: use of undeclared identifier 'undeclared'
+  (void)(1 && _Generic(v, default: 1) && 1);
+}
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index cd6b483ca7e8c2..44e32162b78c50 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1385,6 +1385,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 #### Bug Fixes to AST Handling
 
+- `Expr::HasSideEffects` no longer crashes on a result-dependent `_Generic`
+  selection, such as one whose controlling expression contains errors.
+  (#GH245254)
+
 - Fixed a non-deterministic ordering of unused local typedefs that made
   serialized PCH/AST files and `-Wunused-local-typedef` diagnostics
   non-reproducible across runs. (#GH209639)
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 83f69c20f2a30a..178531ec92f026 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3899,9 +3899,14 @@ bool Expr::HasSideEffects(const ASTContext &Ctx,
         return true;
     break;
 
-  case GenericSelectionExprClass:
-    return cast<GenericSelectionExpr>(this)->getResultExpr()->HasSideEffects(
-        Ctx, IncludePossibleEffects);
+  case GenericSelectionExprClass: {
+    const auto *GSE = cast<GenericSelectionExpr>(this);
+    // There is no result expression when the selection is result-dependent,
+    // e.g. because the controlling expression contains errors.
+    if (GSE->isResultDependent())
+      return IncludePossibleEffects;
+    return GSE->getResultExpr()->HasSideEffects(Ctx, IncludePossibleEffects);
+  }
 
   case ChooseExprClass:
     return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(

>From e6b12d86813954b25455e1c18e7b3c43adf89a74 Mon Sep 17 00:00:00 2001
From: Yousof Selim <ygshuh at gmail.com>
Date: Sun, 6 Dec 2026 16:04:40 +0800
Subject: [PATCH 2/2] [clang] Add an AST unit test for HasSideEffects on a
 result-dependent _Generic

Clang's own Sema checks dependence before calling HasSideEffects, so
the crash is only reachable from tools. The test calls it directly on
a selection whose controlling expression contains errors.

Assisted-by: Claude Opus 5.5
---
 clang/unittests/AST/ASTExprTest.cpp | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/clang/unittests/AST/ASTExprTest.cpp b/clang/unittests/AST/ASTExprTest.cpp
index 5c9e7928bb804f..e7926ee72f6a04 100644
--- a/clang/unittests/AST/ASTExprTest.cpp
+++ b/clang/unittests/AST/ASTExprTest.cpp
@@ -25,6 +25,7 @@ using namespace clang;
 
 using clang::ast_matchers::cxxRecordDecl;
 using clang::ast_matchers::forStmt;
+using clang::ast_matchers::genericSelectionExpr;
 using clang::ast_matchers::has;
 using clang::ast_matchers::hasName;
 using clang::ast_matchers::initListExpr;
@@ -446,3 +447,22 @@ TEST(ASTExpr, CXXDefaultInitExprHasRewrittenInit) {
   EXPECT_FALSE(CtorDIE->hasRewrittenInit());
   EXPECT_EQ(CtorDIE->getExpr(), CtorDIE->getField()->getInClassInitializer());
 }
+
+TEST(ASTExpr, HasSideEffectsResultDependentGenericSelection) {
+  // The controlling expression contains errors, so the selection is
+  // result-dependent and has no result expression (#GH245254).
+  auto AST = buildASTFromCodeWithArgs(R"c(
+    void f(void) {
+      __auto_type v = undeclared;
+      (void)_Generic(v, default: 1);
+    }
+  )c",
+                                      {"-xc"});
+  ASTContext &Ctx = AST->getASTContext();
+  const auto *GSE = selectFirst<GenericSelectionExpr>(
+      "gse", match(genericSelectionExpr().bind("gse"), Ctx));
+  ASSERT_NE(GSE, nullptr);
+  ASSERT_TRUE(GSE->isResultDependent());
+  EXPECT_FALSE(GSE->HasSideEffects(Ctx, /*IncludePossibleEffects=*/false));
+  EXPECT_TRUE(GSE->HasSideEffects(Ctx, /*IncludePossibleEffects=*/true));
+}



More information about the cfe-commits mailing list