Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 1 | //===- Errno.cpp - errno support --------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the errno wrappers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Errno.h" |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" // Get autoconf configuration settings |
Dmitri Gribenko | 91c06da | 2012-09-28 14:15:28 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 1760dc2 | 2016-04-05 20:19:49 +0000 | [diff] [blame^] | 17 | #include <cerrno> |
| 18 | #include <cstring> |
| 19 | #include <string> |
Jeffrey Yasskin | 5a2e521 | 2009-07-06 16:50:27 +0000 | [diff] [blame] | 20 | |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 23 | //=== independent code. |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | namespace llvm { |
| 27 | namespace sys { |
| 28 | |
| 29 | #if HAVE_ERRNO_H |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 30 | std::string StrError() { |
| 31 | return StrError(errno); |
| 32 | } |
Eugene Zelenko | 1760dc2 | 2016-04-05 20:19:49 +0000 | [diff] [blame^] | 33 | #endif // HAVE_ERRNO_H |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 34 | |
| 35 | std::string StrError(int errnum) { |
Dmitri Gribenko | 91c06da | 2012-09-28 14:15:28 +0000 | [diff] [blame] | 36 | std::string str; |
Chandler Carruth | 14aae04 | 2013-09-02 05:55:10 +0000 | [diff] [blame] | 37 | if (errnum == 0) |
| 38 | return str; |
Yaron Keren | d908941 | 2014-12-04 21:36:38 +0000 | [diff] [blame] | 39 | #if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S |
| 40 | const int MaxErrStrLen = 2000; |
| 41 | char buffer[MaxErrStrLen]; |
| 42 | buffer[0] = '\0'; |
| 43 | #endif |
Chandler Carruth | 14aae04 | 2013-09-02 05:55:10 +0000 | [diff] [blame] | 44 | |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 45 | #ifdef HAVE_STRERROR_R |
| 46 | // strerror_r is thread-safe. |
Chandler Carruth | 14aae04 | 2013-09-02 05:55:10 +0000 | [diff] [blame] | 47 | #if defined(__GLIBC__) && defined(_GNU_SOURCE) |
| 48 | // glibc defines its own incompatible version of strerror_r |
| 49 | // which may not use the buffer supplied. |
| 50 | str = strerror_r(errnum, buffer, MaxErrStrLen - 1); |
| 51 | #else |
| 52 | strerror_r(errnum, buffer, MaxErrStrLen - 1); |
| 53 | str = buffer; |
| 54 | #endif |
NAKAMURA Takumi | 18911180 | 2011-02-09 04:18:48 +0000 | [diff] [blame] | 55 | #elif HAVE_DECL_STRERROR_S // "Windows Secure API" |
Chandler Carruth | 14aae04 | 2013-09-02 05:55:10 +0000 | [diff] [blame] | 56 | strerror_s(buffer, MaxErrStrLen - 1, errnum); |
| 57 | str = buffer; |
Duncan Sands | f9cf4ff | 2009-07-02 12:09:50 +0000 | [diff] [blame] | 58 | #elif defined(HAVE_STRERROR) |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 59 | // Copy the thread un-safe result of strerror into |
| 60 | // the buffer as fast as possible to minimize impact |
| 61 | // of collision of strerror in multiple threads. |
Chandler Carruth | 14aae04 | 2013-09-02 05:55:10 +0000 | [diff] [blame] | 62 | str = strerror(errnum); |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 63 | #else |
| 64 | // Strange that this system doesn't even have strerror |
| 65 | // but, oh well, just use a generic message |
Dmitri Gribenko | 91c06da | 2012-09-28 14:15:28 +0000 | [diff] [blame] | 66 | raw_string_ostream stream(str); |
| 67 | stream << "Error #" << errnum; |
| 68 | stream.flush(); |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 69 | #endif |
| 70 | return str; |
| 71 | } |
| 72 | |
Eugene Zelenko | 1760dc2 | 2016-04-05 20:19:49 +0000 | [diff] [blame^] | 73 | } // namespace sys |
| 74 | } // namespace llvm |