Skip to content

Commit 5df86da

Browse files
committed
[flang/flang-rt] Implement PERROR intrinsic form GNU Extension
1 parent 575fde0 commit 5df86da

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

flang-rt/lib/runtime/extensions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "flang/Runtime/entry-names.h"
1818
#include "flang/Runtime/io-api.h"
1919
#include <chrono>
20+
#include <cstdio>
2021
#include <cstring>
2122
#include <ctime>
2223
#include <signal.h>
@@ -262,5 +263,8 @@ int RTNAME(Chdir)(const char *name) {
262263

263264
int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
264265

266+
// PERROR(STRING)
267+
void FORTRAN_PROCEDURE_NAME(perror)(const char *str) { perror(str); }
268+
265269
} // namespace Fortran::runtime
266270
} // extern "C"

flang/docs/Intrinsics.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,3 +1106,15 @@ end program chdir_func
11061106
- **Standard:** GNU extension
11071107
- **Class:** function
11081108
- **Syntax:** `RESULT = IERRNO()`
1109+
1110+
### Non-Standard Intrinsics: PERROR
1111+
1112+
#### Description
1113+
`PERROR(STRING)` prints (on the C stderr stream) a newline-terminated error message corresponding to the last system error.
1114+
This is prefixed by `STRING`, a colon and a space.
1115+
1116+
#### Usage and Info
1117+
1118+
- **Standard:** GNU extension
1119+
- **Class:** subroutine
1120+
- **Syntax:** `CALL PERROR(STRING)`

flang/include/flang/Runtime/extensions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,8 @@ int RTNAME(Chdir)(const char *name);
7575
// GNU extension function IERRNO()
7676
int FORTRAN_PROCEDURE_NAME(ierrno)();
7777

78+
// GNU extension subroutine PERROR(STRING)
79+
void FORTRAN_PROCEDURE_NAME(perror)(const char *str);
80+
7881
} // extern "C"
7982
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s
2+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s
3+
4+
! CHECK-LABEL: func @_QPtest_perror(
5+
subroutine test_perror()
6+
character(len=10) :: string
7+
call perror(string)
8+
! CHECK: %[[C10:.*]] = arith.constant 10 : index
9+
! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.char<1,10> {bindc_name = "string", uniq_name = "_QFtest_perrorEstring"}
10+
! CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] typeparams %[[C10]] {uniq_name = "_QFtest_perrorEstring"} : (!fir.ref<!fir.char<1,10>>, index) -> !fir.ref<!fir.char<1,10>>
11+
! CHECK: %[[VAL_2:.*]] = fir.emboxchar %[[VAL_1]], %[[C10]] : (!fir.ref<!fir.char<1,10>>, index) -> !fir.boxchar<1>
12+
! CHECK: fir.call @_QPperror(%[[VAL_2]]) fastmath<contract> : (!fir.boxchar<1>) -> ()
13+
! CHECK: return
14+
end subroutine test_perror

0 commit comments

Comments
 (0)