Skip to content

Commit 0e6e0e2

Browse files
committed
[flang/flang-rt] Implement PERROR intrinsic form GNU Extension
1 parent 924c7ea commit 0e6e0e2

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>
@@ -268,5 +269,8 @@ void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize,
268269
qsort(array, *len, *isize, compar);
269270
}
270271

272+
// PERROR(STRING)
273+
void FORTRAN_PROCEDURE_NAME(perror)(const char *str) { perror(str); }
274+
271275
} // namespace Fortran::runtime
272276
} // extern "C"

flang/docs/Intrinsics.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,3 +1137,15 @@ by `ISIZE`.
11371137
- **Standard:** lib3f (section 3f of old man pages).
11381138
- **Class:** subroutine
11391139
- **Syntax:** `CALL QSORT(ARRAY, LEN, ISIZE, COMPAR)`
1140+
1141+
### Non-Standard Intrinsics: PERROR
1142+
1143+
#### Description
1144+
`PERROR(STRING)` prints (on the C stderr stream) a newline-terminated error message corresponding to the last system error.
1145+
This is prefixed by `STRING`, a colon and a space.
1146+
1147+
#### Usage and Info
1148+
1149+
- **Standard:** GNU extension
1150+
- **Class:** subroutine
1151+
- **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)