1//===-- runtime/io-error.h --------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// Distinguishes I/O error conditions; fatal ones lead to termination,
10// and those that the user program has chosen to handle are recorded
11// so that the highest-priority one can be returned as IOSTAT=.
12// IOSTAT error codes are raw errno values augmented with values for
13// Fortran-specific errors.
14
15#ifndef FORTRAN_RUNTIME_IO_ERROR_H_
16#define FORTRAN_RUNTIME_IO_ERROR_H_
17
18#include "terminator.h"
19#include "flang/Runtime/iostat.h"
20#include "flang/Runtime/memory.h"
21#include <cinttypes>
22
23namespace Fortran::runtime::io {
24
25// See 12.11 in Fortran 2018
26class IoErrorHandler : public Terminator {
27public:
28 using Terminator::Terminator;
29 explicit RT_API_ATTRS IoErrorHandler(const Terminator &that)
30 : Terminator{that} {}
31 RT_API_ATTRS void HasIoStat() { flags_ |= hasIoStat; }
32 RT_API_ATTRS void HasErrLabel() { flags_ |= hasErr; }
33 RT_API_ATTRS void HasEndLabel() { flags_ |= hasEnd; }
34 RT_API_ATTRS void HasEorLabel() { flags_ |= hasEor; }
35 RT_API_ATTRS void HasIoMsg() { flags_ |= hasIoMsg; }
36
37 RT_API_ATTRS bool InError() const {
38 return ioStat_ != IostatOk || pendingError_ != IostatOk;
39 }
40
41 // For I/O statements that detect fatal errors in their
42 // Begin...() API routines before it is known whether they
43 // have error handling control list items. Such statements
44 // have an ErroneousIoStatementState with a pending error.
45 RT_API_ATTRS void SetPendingError(int iostat) { pendingError_ = iostat; }
46
47 RT_API_ATTRS void SignalError(int iostatOrErrno, const char *msg, ...);
48 RT_API_ATTRS void SignalError(int iostatOrErrno);
49 template <typename... X>
50 RT_API_ATTRS void SignalError(const char *msg, X &&...xs) {
51 SignalError(IostatGenericError, msg, std::forward<X>(xs)...);
52 }
53
54 RT_API_ATTRS void Forward(int iostatOrErrno, const char *, std::size_t);
55
56 void SignalErrno(); // SignalError(errno)
57 RT_API_ATTRS void
58 SignalEnd(); // input only; EOF on internal write is an error
59 RT_API_ATTRS void
60 SignalEor(); // non-advancing input only; EOR on write is an error
61 RT_API_ATTRS void SignalPendingError();
62
63 RT_API_ATTRS int GetIoStat() const { return ioStat_; }
64 RT_API_ATTRS bool GetIoMsg(char *, std::size_t);
65
66private:
67 enum Flag : std::uint8_t {
68 hasIoStat = 1, // IOSTAT=
69 hasErr = 2, // ERR=
70 hasEnd = 4, // END=
71 hasEor = 8, // EOR=
72 hasIoMsg = 16, // IOMSG=
73 };
74 std::uint8_t flags_{0};
75 int ioStat_{IostatOk};
76 OwningPtr<char> ioMsg_;
77 int pendingError_{IostatOk};
78};
79
80} // namespace Fortran::runtime::io
81#endif // FORTRAN_RUNTIME_IO_ERROR_H_
82

source code of flang/runtime/io-error.h