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 IoErrorHandler(const Terminator &that) : Terminator{that} {}
30 void HasIoStat() { flags_ |= hasIoStat; }
31 void HasErrLabel() { flags_ |= hasErr; }
32 void HasEndLabel() { flags_ |= hasEnd; }
33 void HasEorLabel() { flags_ |= hasEor; }
34 void HasIoMsg() { flags_ |= hasIoMsg; }
35
36 bool InError() const {
37 return ioStat_ != IostatOk || pendingError_ != IostatOk;
38 }
39
40 // For I/O statements that detect fatal errors in their
41 // Begin...() API routines before it is known whether they
42 // have error handling control list items. Such statements
43 // have an ErroneousIoStatementState with a pending error.
44 void SetPendingError(int iostat) { pendingError_ = iostat; }
45
46 void SignalError(int iostatOrErrno, const char *msg, ...);
47 void SignalError(int iostatOrErrno);
48 template <typename... X> void SignalError(const char *msg, X &&...xs) {
49 SignalError(IostatGenericError, msg, std::forward<X>(xs)...);
50 }
51
52 void Forward(int iostatOrErrno, const char *, std::size_t);
53
54 void SignalErrno(); // SignalError(errno)
55 void SignalEnd(); // input only; EOF on internal write is an error
56 void SignalEor(); // non-advancing input only; EOR on write is an error
57 void SignalPendingError();
58
59 int GetIoStat() const { return ioStat_; }
60 bool GetIoMsg(char *, std::size_t);
61
62private:
63 enum Flag : std::uint8_t {
64 hasIoStat = 1, // IOSTAT=
65 hasErr = 2, // ERR=
66 hasEnd = 4, // END=
67 hasEor = 8, // EOR=
68 hasIoMsg = 16, // IOMSG=
69 };
70 std::uint8_t flags_{0};
71 int ioStat_{IostatOk};
72 OwningPtr<char> ioMsg_;
73 int pendingError_{IostatOk};
74};
75
76} // namespace Fortran::runtime::io
77#endif // FORTRAN_RUNTIME_IO_ERROR_H_
78

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