llvm.org GIT mirror llvm / ed1c0ff
Add a portable strerror*() wrapper, llvm::sys::StrError(). This includes the Windows variant, strerror_s, but I couldn't test that. I'll update configure and config.h.in in a subsequent patch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74621 91177308-0d34-0410-b5e6-96231b3b80d8 Jeffrey Yasskin 11 years ago
8 changed file(s) with 121 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
913913 AC_CHECK_FUNCS([powf fmodf strtof round ])
914914 AC_CHECK_FUNCS([getpagesize getrusage getrlimit setrlimit gettimeofday ])
915915 AC_CHECK_FUNCS([isatty mkdtemp mkstemp ])
916 AC_CHECK_FUNCS([mktemp realpath sbrk setrlimit strdup strerror strerror_r ])
916 AC_CHECK_FUNCS([mktemp realpath sbrk setrlimit strdup ])
917 AC_CHECK_FUNCS([strerror strerror_r strerror_s ])
917918 AC_CHECK_FUNCS([strtoll strtoq sysconf malloc_zone_statistics ])
918919 AC_CHECK_FUNCS([setjmp longjmp sigsetjmp siglongjmp])
919920 AC_C_PRINTF_A
6666 HAVE_MALLOC_ZONE_STATISTICS)
6767 check_symbol_exists(pthread_mutex_lock pthread.h HAVE_PTHREAD_MUTEX_LOCK)
6868 check_symbol_exists(strtoll stdlib.h HAVE_STRTOLL)
69 check_symbol_exists(strerror string.h HAVE_STRERROR)
70 check_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
71 check_symbol_exists(strerror_s string.h HAVE_STRERROR_S)
6972
7073 check_symbol_exists(__GLIBC__ stdio.h LLVM_USING_GLIBC)
7174 if( LLVM_USING_GLIBC )
363363 #undef HAVE_STRDUP
364364
365365 /* Define to 1 if you have the `strerror' function. */
366 #undef HAVE_STRERROR
366 #cmakedefine HAVE_STRERROR
367367
368368 /* Define to 1 if you have the `strerror_r' function. */
369 #undef HAVE_STRERROR_R
369 #cmakedefine HAVE_STRERROR_R
370
371 /* Define to 1 if you have the `strerror_s' function. */
372 #cmakedefine HAVE_STRERROR_S
370373
371374 /* Define to 1 if you have the header file. */
372375 #undef HAVE_STRINGS_H
0 //===- llvm/System/Errno.h - Portable+convenient errno handling -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares some portable and convenient functions to deal with errno.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SYSTEM_ERRNO_H
14 #define LLVM_SYSTEM_ERRNO_H
15
16 #include
17
18 namespace llvm {
19 namespace sys {
20
21 /// Returns a string representation of the errno value, using whatever
22 /// thread-safe variant of strerror() is available. Be sure to call this
23 /// immediately after the function that set errno, or errno may have been
24 /// overwritten by an intervening call.
25 std::string StrError();
26
27 /// Like the no-argument version above, but uses \p errnum instead of errno.
28 std::string StrError(int errnum);
29
30 } // namespace sys
31 } // namespace llvm
32
33 #endif // LLVM_SYSTEM_ERRNO_H
22 Atomic.cpp
33 Disassembler.cpp
44 DynamicLibrary.cpp
5 Errno.cpp
56 Host.cpp
67 IncludeFile.cpp
78 Memory.cpp
0 //===- Errno.cpp - errno support --------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the errno wrappers.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/System/Errno.h"
14 #include "llvm/Config/config.h" // Get autoconf configuration settings
15
16 #if HAVE_STRING_H
17 #include
18
19 //===----------------------------------------------------------------------===//
20 //=== WARNING: Implementation here must contain only TRULY operating system
21 //=== independent code.
22 //===----------------------------------------------------------------------===//
23
24 namespace llvm {
25 namespace sys {
26
27 #if HAVE_ERRNO_H
28 #include
29 std::string StrError() {
30 return StrError(errno);
31 }
32 #endif // HAVE_ERRNO_H
33
34 std::string StrError(int errnum) {
35 const int MaxErrStrLen = 2000;
36 char buffer[MaxErrStrLen];
37 buffer[0] = '\0';
38 char* str = buffer;
39 #ifdef HAVE_STRERROR_R
40 // strerror_r is thread-safe.
41 if (errnum)
42 # if defined(__GLIBC__) && defined(_GNU_SOURCE)
43 // glibc defines its own incompatible version of strerror_r
44 // which may not use the buffer supplied.
45 str = strerror_r(errnum,buffer,MaxErrStrLen-1);
46 # else
47 strerror_r(errnum,buffer,MaxErrStrLen-1);
48 # endif
49 #elif HAVE_STRERROR_S // Windows.
50 if (errnum)
51 strerror_s(buffer, errnum);
52 #elif HAVE_STRERROR
53 // Copy the thread un-safe result of strerror into
54 // the buffer as fast as possible to minimize impact
55 // of collision of strerror in multiple threads.
56 if (errnum)
57 strncpy(buffer,strerror(errnum),MaxErrStrLen-1);
58 buffer[MaxErrStrLen-1] = '\0';
59 #else
60 // Strange that this system doesn't even have strerror
61 // but, oh well, just use a generic message
62 sprintf(buffer, "Error #%d", errnum);
63 #endif
64 return str;
65 }
66
67 } // namespace sys
68 } // namespace llvm
69
70 #endif // HAVE_STRING_H
1919 //===----------------------------------------------------------------------===//
2020
2121 #include "llvm/Config/config.h" // Get autoconf configuration settings
22 #include "llvm/System/Errno.h"
2223 #include
2324 #include
2425 #include
7677 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
7778 if (!ErrMsg)
7879 return true;
79 char buffer[MAXPATHLEN];
80 buffer[0] = 0;
81 char* str = buffer;
8280 if (errnum == -1)
8381 errnum = errno;
84 #ifdef HAVE_STRERROR_R
85 // strerror_r is thread-safe.
86 if (errnum)
87 # if defined(__GLIBC__) && defined(_GNU_SOURCE)
88 // glibc defines its own incompatible version of strerror_r
89 // which may not use the buffer supplied.
90 str = strerror_r(errnum,buffer,MAXPATHLEN-1);
91 # else
92 strerror_r(errnum,buffer,MAXPATHLEN-1);
93 # endif
94 #elif HAVE_STRERROR
95 // Copy the thread un-safe result of strerror into
96 // the buffer as fast as possible to minimize impact
97 // of collision of strerror in multiple threads.
98 if (errnum)
99 strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
100 buffer[MAXPATHLEN-1] = 0;
101 #else
102 // Strange that this system doesn't even have strerror
103 // but, oh well, just use a generic message
104 sprintf(buffer, "Error #%d", errnum);
105 #endif
106 *ErrMsg = prefix + ": " + str;
82 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
10783 return true;
10884 }
10985
1616 #include "llvm-c/lto.h"
1717
1818 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/System/Errno.h"
1920 #include "llvm/System/Path.h"
2021 #include "llvm/System/Program.h"
2122
182183 (*message)(LDPL_ERROR,
183184 "Failed to seek to archive member of %s at offset %d: %s\n",
184185 file->name,
185 file->offset, strerror(errno));
186 file->offset, sys::StrError(errno).c_str());
186187 return LDPS_ERR;
187188 }
188189 buf = malloc(file->filesize);
197198 "Failed to read archive member of %s at offset %d: %s\n",
198199 file->name,
199200 file->offset,
200 strerror(errno));
201 sys::StrError(errno).c_str());
201202 free(buf);
202203 return LDPS_ERR;
203204 }