[flang-commits] [flang] [llvm] [flang] Implement 'F_C_STRING' library function (Fortran 2023) (PR #174474)

via flang-commits flang-commits at lists.llvm.org
Tue Jan 6 09:38:10 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Caroline Newcombe (cenewcombe)

<details>
<summary>Changes</summary>

Implement `F_C_STRING` to convert a Fortran string to a C null-terminated string. Documented in F2023 Standard: 18.2.3.9 `F_C_STRING (STRING [, ASIS])`.

 

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


3 Files Affected:

- (modified) flang-rt/lib/runtime/CMakeLists.txt (+1) 
- (modified) flang/module/iso_c_binding.f90 (+15-1) 
- (added) flang/test/Semantics/f_c_string.f90 (+39) 


``````````diff
diff --git a/flang-rt/lib/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
index 7fa8c2cb95417..d18f24e6c786a 100644
--- a/flang-rt/lib/runtime/CMakeLists.txt
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -75,6 +75,7 @@ set(supported_sources
 # List of source not used for GPU offloading.
 set(host_sources
   ${FLANG_SOURCE_DIR}/module/iso_fortran_env_impl.f90
+  ${FLANG_SOURCE_DIR}/module/iso_c_binding.f90
   command.cpp
   complex-powi.cpp
   complex-reduction.c
diff --git a/flang/module/iso_c_binding.f90 b/flang/module/iso_c_binding.f90
index 8e3f78cea51b7..34e7b6280f54d 100644
--- a/flang/module/iso_c_binding.f90
+++ b/flang/module/iso_c_binding.f90
@@ -29,7 +29,7 @@ module iso_c_binding
   private
 
   public :: c_associated, c_funloc, c_funptr, c_f_pointer, c_loc, &
-    c_null_funptr, c_null_ptr, c_ptr, c_sizeof, &
+    c_null_funptr, c_null_ptr, c_ptr, c_sizeof, f_c_string, &
     operator(==), operator(/=)
 
   ! Table 18.2 (in clause 18.3.1)
@@ -145,4 +145,18 @@ subroutine c_f_procpointer(cptr, fptr)
     ! TODO: implement
   end subroutine c_f_procpointer
 
+  ! F_C_STRING - Convert Fortran string to C null-terminated string
+  ! Fortran 2023 standard intrinsic
+  pure function f_c_string(string, asis) result(res)
+    character(kind=c_char, len=*), intent(in) :: string
+    logical, optional, intent(in) :: asis
+    character(kind=c_char, len=:), allocatable :: res
+
+    if (present(asis) .and. asis) then
+      res = string // c_null_char
+    else
+      res = trim(string) // c_null_char
+    end if
+  end function f_c_string
+
 end module iso_c_binding
diff --git a/flang/test/Semantics/f_c_string.f90 b/flang/test/Semantics/f_c_string.f90
new file mode 100644
index 0000000000000..172cbc49c7c8c
--- /dev/null
+++ b/flang/test/Semantics/f_c_string.f90
@@ -0,0 +1,39 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! Test semantic checking of F_C_STRING from ISO_C_BINDING
+
+program test
+  use iso_c_binding
+  implicit none
+  
+  character(len=20) :: str
+  character(len=:), allocatable :: result
+  logical :: flag
+  integer :: n
+  
+  ! Valid usages
+  result = f_c_string('hello')
+  result = f_c_string(str)
+  result = f_c_string(str, .true.)
+  result = f_c_string(str, .false.)
+  result = f_c_string(str, flag)
+  result = f_c_string(string=str)
+  result = f_c_string(string=str, asis=.true.)
+  result = f_c_string(asis=.false., string=str)
+  
+  ! Invalid: missing required argument
+  !ERROR: missing mandatory 'string=' argument
+  result = f_c_string()
+  
+  ! Invalid: too many arguments
+  !ERROR: No intrinsic or generic 'f_c_string' matches the actual arguments
+  result = f_c_string(str, .true., .false.)
+  
+  ! Invalid: non-character first argument
+  !ERROR: No intrinsic or generic 'f_c_string' matches the actual arguments
+  result = f_c_string(n)
+  
+  ! Invalid: non-logical second argument
+  !ERROR: No intrinsic or generic 'f_c_string' matches the actual arguments
+  result = f_c_string(str, n)
+  
+end program

``````````

</details>


https://github.com/llvm/llvm-project/pull/174474


More information about the flang-commits mailing list