Skip to content

Commit bd26c05

Browse files
committed
[flang] Implement IERRNO intrinsic
1 parent 95ff3b5 commit bd26c05

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

flang/docs/Intrinsics.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,3 +1064,14 @@ This intrinsic is an alias for `LEN_TRIM`, without the optional KIND argument.
10641064
- **Arguments:** `TIME` - a REAL value into which the elapsed CPU time in
10651065
seconds is written
10661066
- **RETURN value:** same as TIME argument
1067+
1068+
### Non-Standard Intrinsics: IERRNO
1069+
1070+
#### Description
1071+
`IERRNO()` returns the last system error number, as given by the C `errno` variable.
1072+
1073+
#### Usage and Info
1074+
1075+
- **Standard:** GNU extension
1076+
- **Class:** function
1077+
- **Syntax:** `RESULT = IERRNO()`

flang/include/flang/Runtime/extensions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,8 @@ std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name,
6969
std::int64_t nameLength, const char *mode, std::int64_t modeLength);
7070
#endif
7171

72+
// GNU extension function IERRNO()
73+
int FORTRAN_PROCEDURE_NAME(ierrno)();
74+
7275
} // extern "C"
7376
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_

flang/runtime/extensions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,7 @@ std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name,
248248
}
249249
#endif
250250

251+
int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
252+
251253
} // namespace Fortran::runtime
252254
} // extern "C"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck --check-prefixes=CHECK %s
2+
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck --check-prefixes=CHECK %s
3+
4+
! CHECK-LABEL: func @_QPtest_ierrno(
5+
subroutine test_ierrno()
6+
integer :: i
7+
i = ierrno()
8+
! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_ierrnoEi"}
9+
! CHECK: %[[VAL_1:.*]] = fir.call @_QPierrno() fastmath<contract> : () -> i32
10+
! CHECK: fir.store %[[VAL_1]] to %[[VAL_0]] : !fir.ref<i32>
11+
! CHECK: return
12+
end subroutine test_ierrno

0 commit comments

Comments
 (0)