llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Adi Salimgereyev (adislm)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/246892.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGDecl.cpp (+1-1) 
- (added) clang/test/CodeGenCXX/block-scope-out-of-line-member-decl.cpp (+11) 


``````````diff
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 76e374db97679..c34facbd50e5c 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 0000000000000..e3d2bee7cf853
--- /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

``````````

</details>


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

Reply via email to