2020-01-17 05:51:25 +08:00
|
|
|
//===-- runtime/io-error.h --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Distinguishes I/O error conditions; fatal ones lead to termination,
|
|
|
|
// and those that the user program has chosen to handle are recorded
|
|
|
|
// so that the highest-priority one can be returned as IOSTAT=.
|
2020-02-14 06:41:56 +08:00
|
|
|
// IOSTAT error codes are raw errno values augmented with values for
|
|
|
|
// Fortran-specific errors.
|
2020-01-17 05:51:25 +08:00
|
|
|
|
|
|
|
#ifndef FORTRAN_RUNTIME_IO_ERROR_H_
|
|
|
|
#define FORTRAN_RUNTIME_IO_ERROR_H_
|
|
|
|
|
|
|
|
#include "terminator.h"
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/iostat.h"
|
|
|
|
#include "flang/Runtime/memory.h"
|
2020-01-17 05:51:25 +08:00
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
// See 12.11 in Fortran 2018
|
2020-02-05 08:55:45 +08:00
|
|
|
class IoErrorHandler : public Terminator {
|
2020-01-17 05:51:25 +08:00
|
|
|
public:
|
|
|
|
using Terminator::Terminator;
|
2020-02-05 08:55:45 +08:00
|
|
|
explicit IoErrorHandler(const Terminator &that) : Terminator{that} {}
|
2020-01-17 05:51:25 +08:00
|
|
|
void HasIoStat() { flags_ |= hasIoStat; }
|
|
|
|
void HasErrLabel() { flags_ |= hasErr; }
|
|
|
|
void HasEndLabel() { flags_ |= hasEnd; }
|
|
|
|
void HasEorLabel() { flags_ |= hasEor; }
|
2020-02-14 06:41:56 +08:00
|
|
|
void HasIoMsg() { flags_ |= hasIoMsg; }
|
2021-06-26 01:40:08 +08:00
|
|
|
void HandleAnything() {
|
|
|
|
flags_ = hasIoStat | hasErr | hasEnd | hasEor | hasIoMsg;
|
|
|
|
}
|
2020-01-17 05:51:25 +08:00
|
|
|
|
2021-04-14 07:07:58 +08:00
|
|
|
bool InError() const { return ioStat_ != IostatOk; }
|
2020-02-14 06:41:56 +08:00
|
|
|
|
|
|
|
void SignalError(int iostatOrErrno, const char *msg, ...);
|
2020-01-17 05:51:25 +08:00
|
|
|
void SignalError(int iostatOrErrno);
|
2020-08-04 02:35:29 +08:00
|
|
|
template <typename... X> void SignalError(const char *msg, X &&...xs) {
|
2020-02-14 06:41:56 +08:00
|
|
|
SignalError(IostatGenericError, msg, std::forward<X>(xs)...);
|
|
|
|
}
|
|
|
|
|
2021-06-26 01:40:08 +08:00
|
|
|
void Forward(int iostatOrErrno, const char *, std::size_t);
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
void SignalErrno(); // SignalError(errno)
|
|
|
|
void SignalEnd(); // input only; EOF on internal write is an error
|
|
|
|
void SignalEor(); // non-advancing input only; EOR on write is an error
|
2020-01-17 05:51:25 +08:00
|
|
|
|
2020-01-24 08:10:00 +08:00
|
|
|
int GetIoStat() const { return ioStat_; }
|
2020-02-14 06:41:56 +08:00
|
|
|
bool GetIoMsg(char *, std::size_t);
|
2020-01-17 05:51:25 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum Flag : std::uint8_t {
|
2020-03-29 12:00:16 +08:00
|
|
|
hasIoStat = 1, // IOSTAT=
|
|
|
|
hasErr = 2, // ERR=
|
|
|
|
hasEnd = 4, // END=
|
|
|
|
hasEor = 8, // EOR=
|
|
|
|
hasIoMsg = 16, // IOMSG=
|
2020-01-17 05:51:25 +08:00
|
|
|
};
|
|
|
|
std::uint8_t flags_{0};
|
2021-04-14 07:07:58 +08:00
|
|
|
int ioStat_{IostatOk};
|
2020-02-14 06:41:56 +08:00
|
|
|
OwningPtr<char> ioMsg_;
|
2020-01-17 05:51:25 +08:00
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|
|
|
|
#endif // FORTRAN_RUNTIME_IO_ERROR_H_
|