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 "iostat.h" 19 #include "memory.h" 20 #include "terminator.h" 21 #include <cinttypes> 22 23 namespace Fortran::runtime::io { 24 25 // See 12.11 in Fortran 2018 26 class IoErrorHandler : public Terminator { 27 public: 28 using Terminator::Terminator; IoErrorHandler(const Terminator & that)29 explicit IoErrorHandler(const Terminator &that) : Terminator{that} {} 30 void Begin(const char *sourceFileName, int sourceLine); HasIoStat()31 void HasIoStat() { flags_ |= hasIoStat; } HasErrLabel()32 void HasErrLabel() { flags_ |= hasErr; } HasEndLabel()33 void HasEndLabel() { flags_ |= hasEnd; } HasEorLabel()34 void HasEorLabel() { flags_ |= hasEor; } HasIoMsg()35 void HasIoMsg() { flags_ |= hasIoMsg; } 36 InError()37 bool InError() const { return ioStat_ != 0; } 38 39 void SignalError(int iostatOrErrno, const char *msg, ...); 40 void SignalError(int iostatOrErrno); SignalError(const char * msg,X &&...xs)41 template <typename... X> void SignalError(const char *msg, X &&...xs) { 42 SignalError(IostatGenericError, msg, std::forward<X>(xs)...); 43 } 44 45 void SignalErrno(); // SignalError(errno) 46 void SignalEnd(); // input only; EOF on internal write is an error 47 void SignalEor(); // non-advancing input only; EOR on write is an error 48 GetIoStat()49 int GetIoStat() const { return ioStat_; } 50 bool GetIoMsg(char *, std::size_t); 51 52 private: 53 enum Flag : std::uint8_t { 54 hasIoStat = 1, // IOSTAT= 55 hasErr = 2, // ERR= 56 hasEnd = 4, // END= 57 hasEor = 8, // EOR= 58 hasIoMsg = 16, // IOMSG= 59 }; 60 std::uint8_t flags_{0}; 61 int ioStat_{0}; 62 OwningPtr<char> ioMsg_; 63 }; 64 65 } // namespace Fortran::runtime::io 66 #endif // FORTRAN_RUNTIME_IO_ERROR_H_ 67