Skip to content

Conversation

@erichkeane
Copy link
Collaborator

Like the NamespaceDecl, this is just emitted as a decl-context, where all its internal declarations get emitted for it. The incubator doesn't seem to have any good tests for this, so I wrote what I could think of as a half-decent test for this one, though the lowering doesn't manage whether these should be mangled, so the test is mostly just for spot-checking purposes.

I'm implementing this as it will make writing further tests for future features a little easier.

Like the NamespaceDecl, this is just emitted as a decl-context, where
all its internal declarations get emitted for it. The incubator doesn't
seem to have any good tests for this, so I wrote what I could think of
as a half-decent test for this one, though the lowering doesn't manage
whether these should be mangled, so the test is mostly just for
spot-checking purposes.

I'm implementing this as it will make writing further tests for future
features a little easier.
@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Apr 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clangir

Author: Erich Keane (erichkeane)

Changes

Like the NamespaceDecl, this is just emitted as a decl-context, where all its internal declarations get emitted for it. The incubator doesn't seem to have any good tests for this, so I wrote what I could think of as a half-decent test for this one, though the lowering doesn't manage whether these should be mangled, so the test is mostly just for spot-checking purposes.

I'm implementing this as it will make writing further tests for future features a little easier.


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

3 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenDecl.cpp (+1)
  • (modified) clang/lib/CIR/CodeGen/CIRGenModule.cpp (+2-1)
  • (added) clang/test/CIR/CodeGen/linkage-spec.cpp (+42)
diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
index 8026f22b00117..f16671cc3f522 100644
--- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
@@ -260,6 +260,7 @@ void CIRGenFunction::emitExprAsInit(const Expr *init, const ValueDecl *d,
 
 void CIRGenFunction::emitDecl(const Decl &d) {
   switch (d.getKind()) {
+  case Decl::LinkageSpec:
   case Decl::Namespace:
     llvm_unreachable("Declaration should not be in declstmts!");
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 8aa57d1dbf9b3..8a493501392dc 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -677,8 +677,9 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) {
     break;
 
   // C++ Decls
+  case Decl::LinkageSpec:
   case Decl::Namespace:
-    emitDeclContext(cast<NamespaceDecl>(decl));
+    emitDeclContext(Decl::castToDeclContext(decl));
     break;
   }
 }
diff --git a/clang/test/CIR/CodeGen/linkage-spec.cpp b/clang/test/CIR/CodeGen/linkage-spec.cpp
new file mode 100644
index 0000000000000..01c4e3fbe181d
--- /dev/null
+++ b/clang/test/CIR/CodeGen/linkage-spec.cpp
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s
+
+extern "C" void TopLevelC(){}
+// CHECK: cir.func @TopLevelC() {
+extern "C++" void TopLevelCpp(){}
+// CHECK: cir.func @_Z11TopLevelCppv() {
+
+extern "C++" {
+  void ExternCppEmpty(){}
+  // CHECK: cir.func @_Z14ExternCppEmptyv() {
+  extern "C" void ExternCpp_C(){}
+  // CHECK: cir.func @ExternCpp_C() {
+  extern "C++" void ExternCpp_Cpp(){}
+  // CHECK: cir.func @_Z13ExternCpp_Cppv() {
+
+  extern "C" {
+  void ExternCpp_CEmpty(){}
+  // CHECK: cir.func @ExternCpp_CEmpty() {
+  extern "C" void ExternCpp_C_C(){}
+  // CHECK: cir.func @ExternCpp_C_C() {
+  extern "C++" void ExternCpp_C_Cpp(){}
+  // CHECK: cir.func @_Z15ExternCpp_C_Cppv() {
+  }
+}
+
+extern "C" {
+  void ExternCEmpty(){}
+  // CHECK: cir.func @ExternCEmpty() {
+  extern "C" void ExternC_C(){}
+  // CHECK: cir.func @ExternC_C() {
+  extern "C++" void ExternC_Cpp(){}
+  // CHECK: cir.func @_Z11ExternC_Cppv() {
+  extern "C++" {
+  void ExternC_CppEmpty(){}
+  // CHECK: cir.func @_Z16ExternC_CppEmptyv() {
+  extern "C" void ExternC_Cpp_C(){}
+  // CHECK: cir.func @ExternC_Cpp_C() {
+  extern "C++" void ExternC_Cpp_Cpp(){}
+  // CHECK: cir.func @_Z15ExternC_Cpp_Cppv() {
+  }
+}
+

Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@erichkeane erichkeane merged commit 0e07478 into llvm:main Apr 28, 2025
14 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Like the NamespaceDecl, this is just emitted as a decl-context, where
all its internal declarations get emitted for it. The incubator doesn't
seem to have any good tests for this, so I wrote what I could think of
as a half-decent test for this one, though the lowering doesn't manage
whether these should be mangled, so the test is mostly just for
spot-checking purposes.

I'm implementing this as it will make writing further tests for future
features a little easier.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Like the NamespaceDecl, this is just emitted as a decl-context, where
all its internal declarations get emitted for it. The incubator doesn't
seem to have any good tests for this, so I wrote what I could think of
as a half-decent test for this one, though the lowering doesn't manage
whether these should be mangled, so the test is mostly just for
spot-checking purposes.

I'm implementing this as it will make writing further tests for future
features a little easier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants