[llvm-commits] [llvm] r74621 - in /llvm/trunk: autoconf/configure.ac cmake/config-ix.cmake include/llvm/Config/config.h.cmake include/llvm/System/Errno.h lib/System/CMakeLists.txt lib/System/Errno.cpp lib/System/Unix/Unix.h tools/gold/gold-plugin.cpp

Jeffrey Yasskin jyasskin at google.com
Wed Jul 1 11:11:23 PDT 2009


Author: jyasskin
Date: Wed Jul  1 13:11:20 2009
New Revision: 74621

URL: http://llvm.org/viewvc/llvm-project?rev=74621&view=rev
Log:
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.

Added:
    llvm/trunk/include/llvm/System/Errno.h
    llvm/trunk/lib/System/Errno.cpp
Modified:
    llvm/trunk/autoconf/configure.ac
    llvm/trunk/cmake/config-ix.cmake
    llvm/trunk/include/llvm/Config/config.h.cmake
    llvm/trunk/lib/System/CMakeLists.txt
    llvm/trunk/lib/System/Unix/Unix.h
    llvm/trunk/tools/gold/gold-plugin.cpp

Modified: llvm/trunk/autoconf/configure.ac
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/autoconf/configure.ac?rev=74621&r1=74620&r2=74621&view=diff

==============================================================================
--- llvm/trunk/autoconf/configure.ac (original)
+++ llvm/trunk/autoconf/configure.ac Wed Jul  1 13:11:20 2009
@@ -914,7 +914,8 @@
 AC_CHECK_FUNCS([powf fmodf strtof round ])
 AC_CHECK_FUNCS([getpagesize getrusage getrlimit setrlimit gettimeofday ])
 AC_CHECK_FUNCS([isatty mkdtemp mkstemp ])
-AC_CHECK_FUNCS([mktemp realpath sbrk setrlimit strdup strerror strerror_r ])
+AC_CHECK_FUNCS([mktemp realpath sbrk setrlimit strdup ])
+AC_CHECK_FUNCS([strerror strerror_r strerror_s ])
 AC_CHECK_FUNCS([strtoll strtoq sysconf malloc_zone_statistics ])
 AC_CHECK_FUNCS([setjmp longjmp sigsetjmp siglongjmp])
 AC_C_PRINTF_A

Modified: llvm/trunk/cmake/config-ix.cmake
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/cmake/config-ix.cmake?rev=74621&r1=74620&r2=74621&view=diff

==============================================================================
--- llvm/trunk/cmake/config-ix.cmake (original)
+++ llvm/trunk/cmake/config-ix.cmake Wed Jul  1 13:11:20 2009
@@ -67,6 +67,9 @@
                     HAVE_MALLOC_ZONE_STATISTICS)
 check_symbol_exists(pthread_mutex_lock pthread.h HAVE_PTHREAD_MUTEX_LOCK)
 check_symbol_exists(strtoll stdlib.h HAVE_STRTOLL)
+check_symbol_exists(strerror string.h HAVE_STRERROR)
+check_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
+check_symbol_exists(strerror_s string.h HAVE_STRERROR_S)
 
 check_symbol_exists(__GLIBC__ stdio.h LLVM_USING_GLIBC)
 if( LLVM_USING_GLIBC )

Modified: llvm/trunk/include/llvm/Config/config.h.cmake
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Config/config.h.cmake?rev=74621&r1=74620&r2=74621&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Config/config.h.cmake (original)
+++ llvm/trunk/include/llvm/Config/config.h.cmake Wed Jul  1 13:11:20 2009
@@ -364,10 +364,13 @@
 #undef HAVE_STRDUP
 
 /* Define to 1 if you have the `strerror' function. */
-#undef HAVE_STRERROR
+#cmakedefine HAVE_STRERROR
 
 /* Define to 1 if you have the `strerror_r' function. */
-#undef HAVE_STRERROR_R
+#cmakedefine HAVE_STRERROR_R
+
+/* Define to 1 if you have the `strerror_s' function. */
+#cmakedefine HAVE_STRERROR_S
 
 /* Define to 1 if you have the <strings.h> header file. */
 #undef HAVE_STRINGS_H

Added: llvm/trunk/include/llvm/System/Errno.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/System/Errno.h?rev=74621&view=auto

==============================================================================
--- llvm/trunk/include/llvm/System/Errno.h (added)
+++ llvm/trunk/include/llvm/System/Errno.h Wed Jul  1 13:11:20 2009
@@ -0,0 +1,34 @@
+//===- llvm/System/Errno.h - Portable+convenient errno handling -*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares some portable and convenient functions to deal with errno.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SYSTEM_ERRNO_H
+#define LLVM_SYSTEM_ERRNO_H
+
+#include <string>
+
+namespace llvm {
+namespace sys {
+
+/// Returns a string representation of the errno value, using whatever
+/// thread-safe variant of strerror() is available.  Be sure to call this
+/// immediately after the function that set errno, or errno may have been
+/// overwritten by an intervening call.
+std::string StrError();
+
+/// Like the no-argument version above, but uses \p errnum instead of errno.
+std::string StrError(int errnum);
+
+}  // namespace sys
+}  // namespace llvm
+
+#endif  // LLVM_SYSTEM_ERRNO_H

Modified: llvm/trunk/lib/System/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/CMakeLists.txt?rev=74621&r1=74620&r2=74621&view=diff

==============================================================================
--- llvm/trunk/lib/System/CMakeLists.txt (original)
+++ llvm/trunk/lib/System/CMakeLists.txt Wed Jul  1 13:11:20 2009
@@ -3,6 +3,7 @@
   Atomic.cpp
   Disassembler.cpp
   DynamicLibrary.cpp
+  Errno.cpp
   Host.cpp
   IncludeFile.cpp
   Memory.cpp

Added: llvm/trunk/lib/System/Errno.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Errno.cpp?rev=74621&view=auto

==============================================================================
--- llvm/trunk/lib/System/Errno.cpp (added)
+++ llvm/trunk/lib/System/Errno.cpp Wed Jul  1 13:11:20 2009
@@ -0,0 +1,71 @@
+//===- Errno.cpp - errno support --------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the errno wrappers.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/System/Errno.h"
+#include "llvm/Config/config.h"     // Get autoconf configuration settings
+
+#if HAVE_STRING_H
+#include <string.h>
+
+//===----------------------------------------------------------------------===//
+//=== WARNING: Implementation here must contain only TRULY operating system
+//===          independent code.
+//===----------------------------------------------------------------------===//
+
+namespace llvm {
+namespace sys {
+
+#if HAVE_ERRNO_H
+#include <errno.h>
+std::string StrError() {
+  return StrError(errno);
+}
+#endif  // HAVE_ERRNO_H
+
+std::string StrError(int errnum) {
+  const int MaxErrStrLen = 2000;
+  char buffer[MaxErrStrLen];
+  buffer[0] = '\0';
+  char* str = buffer;
+#ifdef HAVE_STRERROR_R
+  // strerror_r is thread-safe.
+  if (errnum)
+# if defined(__GLIBC__) && defined(_GNU_SOURCE)
+    // glibc defines its own incompatible version of strerror_r
+    // which may not use the buffer supplied.
+    str = strerror_r(errnum,buffer,MaxErrStrLen-1);
+# else
+    strerror_r(errnum,buffer,MaxErrStrLen-1);
+# endif
+#elif HAVE_STRERROR_S  // Windows.
+    if (errnum)
+      strerror_s(buffer, errnum);
+#elif HAVE_STRERROR
+  // Copy the thread un-safe result of strerror into
+  // the buffer as fast as possible to minimize impact
+  // of collision of strerror in multiple threads.
+  if (errnum)
+    strncpy(buffer,strerror(errnum),MaxErrStrLen-1);
+  buffer[MaxErrStrLen-1] = '\0';
+#else
+  // Strange that this system doesn't even have strerror
+  // but, oh well, just use a generic message
+  sprintf(buffer, "Error #%d", errnum);
+#endif
+  return str;
+}
+
+}  // namespace sys
+}  // namespace llvm
+
+#endif  // HAVE_STRING_H

Modified: llvm/trunk/lib/System/Unix/Unix.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Unix/Unix.h?rev=74621&r1=74620&r2=74621&view=diff

==============================================================================
--- llvm/trunk/lib/System/Unix/Unix.h (original)
+++ llvm/trunk/lib/System/Unix/Unix.h Wed Jul  1 13:11:20 2009
@@ -20,6 +20,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Config/config.h"     // Get autoconf configuration settings
+#include "llvm/System/Errno.h"
 #include <cstdlib>
 #include <cstdio>
 #include <cstring>
@@ -77,34 +78,9 @@
   std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
   if (!ErrMsg)
     return true;
-  char buffer[MAXPATHLEN];
-  buffer[0] = 0;
-  char* str = buffer;
   if (errnum == -1)
     errnum = errno;
-#ifdef HAVE_STRERROR_R
-  // strerror_r is thread-safe.
-  if (errnum)
-# if defined(__GLIBC__) && defined(_GNU_SOURCE)
-    // glibc defines its own incompatible version of strerror_r
-    // which may not use the buffer supplied.
-    str = strerror_r(errnum,buffer,MAXPATHLEN-1);
-# else
-    strerror_r(errnum,buffer,MAXPATHLEN-1);
-# endif
-#elif HAVE_STRERROR
-  // Copy the thread un-safe result of strerror into
-  // the buffer as fast as possible to minimize impact
-  // of collision of strerror in multiple threads.
-  if (errnum)
-    strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
-  buffer[MAXPATHLEN-1] = 0;
-#else
-  // Strange that this system doesn't even have strerror
-  // but, oh well, just use a generic message
-  sprintf(buffer, "Error #%d", errnum);
-#endif
-  *ErrMsg = prefix + ": " + str;
+  *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
   return true;
 }
 

Modified: llvm/trunk/tools/gold/gold-plugin.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/gold/gold-plugin.cpp?rev=74621&r1=74620&r2=74621&view=diff

==============================================================================
--- llvm/trunk/tools/gold/gold-plugin.cpp (original)
+++ llvm/trunk/tools/gold/gold-plugin.cpp Wed Jul  1 13:11:20 2009
@@ -17,6 +17,7 @@
 #include "llvm-c/lto.h"
 
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Errno.h"
 #include "llvm/System/Path.h"
 #include "llvm/System/Program.h"
 
@@ -183,7 +184,7 @@
       (*message)(LDPL_ERROR,
                  "Failed to seek to archive member of %s at offset %d: %s\n", 
                  file->name,
-                 file->offset, strerror(errno));
+                 file->offset, sys::StrError(errno).c_str());
       return LDPS_ERR;
     }
     buf = malloc(file->filesize);
@@ -198,7 +199,7 @@
                  "Failed to read archive member of %s at offset %d: %s\n",
                  file->name,
                  file->offset,
-                 strerror(errno));
+                 sys::StrError(errno).c_str());
       free(buf);
       return LDPS_ERR;
     }





More information about the llvm-commits mailing list