https://github.com/adislm created 
https://github.com/llvm/llvm-project/pull/246892

An out-of-line member declaration such as `void S::bar();` inside a function 
body is accepted by clang as an extension (its diagnostic, 
`-Wout-of-line-declaration`, can be disabled). The parser then wraps the 
resulting `CXXMethodDecl` in a `DeclStmt`, and `CodeGenFunction::EmitDecl()` 
reaches its "Declaration should not be in declstmts!" unreachable, crashing 
clang.

Such a declaration has no code generation effect, so handle it the same way as 
a block-scope function declaration and ignore it.

Test: `clang/test/CodeGenCXX/block-scope-out-of-line-member-decl.cpp`.

Fixes #245118

---
This contribution was developed with AI assistance (Claude Code) and reviewed 
and tested by me.

>From 5b15bb0fa55945b8f880fd9f8e95699138efbda8 Mon Sep 17 00:00:00 2001
From: abs0luty <[email protected]>
Date: Sun, 29 Nov 2026 01:09:10 +0400
Subject: [PATCH] [CodeGen] Ignore out-of-line member declarations at block
 scope

An out-of-line member declaration such as `void S::bar();` inside a
function body is accepted as an extension, but the parser wraps it in a
DeclStmt. CodeGenFunction::EmitDecl() then reaches an unreachable that
claims a CXXMethod can never appear there and crashes.

The declaration has no codegen effect, so handle it like a block-scope
function declaration.

Fixes #245118

Assisted-by: Claude Code
---
 clang/lib/CodeGen/CGDecl.cpp                          |  2 +-
 .../block-scope-out-of-line-member-decl.cpp           | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
 create mode 100644 
clang/test/CodeGenCXX/block-scope-out-of-line-member-decl.cpp

diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 76e374db976796..c34facbd50e5ca 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -64,7 +64,6 @@ void CodeGenFunction::EmitDecl(const Decl &D, bool 
EvaluateConditionDecl) {
   case Decl::UnresolvedUsingValue:
   case Decl::NonTypeTemplateParm:
   case Decl::CXXDeductionGuide:
-  case Decl::CXXMethod:
   case Decl::CXXConstructor:
   case Decl::CXXDestructor:
   case Decl::CXXConversion:
@@ -127,6 +126,7 @@ void CodeGenFunction::EmitDecl(const Decl &D, bool 
EvaluateConditionDecl) {
     }
     return;
   case Decl::Function:     // void X();
+  case Decl::CXXMethod:    // void X::f();
   case Decl::EnumConstant: // enum ? { X = ? }
   case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
   case Decl::ExplicitInstantiation:
diff --git a/clang/test/CodeGenCXX/block-scope-out-of-line-member-decl.cpp 
b/clang/test/CodeGenCXX/block-scope-out-of-line-member-decl.cpp
new file mode 100644
index 00000000000000..e3d2bee7cf853d
--- /dev/null
+++ b/clang/test/CodeGenCXX/block-scope-out-of-line-member-decl.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 
-Wno-out-of-line-declaration -emit-llvm -o - %s | FileCheck %s
+
+void foo() {
+  struct S {
+    void bar();
+  };
+  void S::bar();
+}
+
+// CHECK-LABEL: define {{.*}}void @_Z3foov()
+// CHECK: ret void

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to