[Lldb-commits] [lldb] 1410a48 - [lldb] Remove vasprintf windows-compat implementation

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 30 00:33:33 PDT 2022


Author: Pavel Labath
Date: 2022-03-30T09:32:35+02:00
New Revision: 1410a4860eb2355f0c6e3274a937f7e382a59e5c

URL: https://github.com/llvm/llvm-project/commit/1410a4860eb2355f0c6e3274a937f7e382a59e5c
DIFF: https://github.com/llvm/llvm-project/commit/1410a4860eb2355f0c6e3274a937f7e382a59e5c.diff

LOG: [lldb] Remove vasprintf windows-compat implementation

We already have a VASprintf function for this purpose, so I'm switching
the remaining few users to that.

Added: 
    

Modified: 
    lldb/include/lldb/Host/windows/PosixApi.h
    lldb/source/Host/CMakeLists.txt
    lldb/source/Host/common/File.cpp
    lldb/source/Target/RegisterContextUnwind.cpp

Removed: 
    lldb/source/Host/windows/Windows.cpp


################################################################################
diff  --git a/lldb/include/lldb/Host/windows/PosixApi.h b/lldb/include/lldb/Host/windows/PosixApi.h
index 85e828c80eef1..981261a3ea358 100644
--- a/lldb/include/lldb/Host/windows/PosixApi.h
+++ b/lldb/include/lldb/Host/windows/PosixApi.h
@@ -86,10 +86,6 @@ typedef uint32_t pid_t;
 
 #endif // _MSC_VER
 
-// Various useful posix functions that are not present in Windows.  We provide
-// custom implementations.
-int vasprintf(char **ret, const char *fmt, va_list ap);
-
 // empty functions
 inline int posix_openpt(int flag) { LLVM_BUILTIN_UNREACHABLE; }
 

diff  --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt
index 4374abca05066..70942e0772aa6 100644
--- a/lldb/source/Host/CMakeLists.txt
+++ b/lldb/source/Host/CMakeLists.txt
@@ -66,7 +66,6 @@ if (CMAKE_SYSTEM_NAME MATCHES "Windows")
     windows/PipeWindows.cpp
     windows/ProcessLauncherWindows.cpp
     windows/ProcessRunLock.cpp
-    windows/Windows.cpp
     )
 else()
   add_host_subdirectory(posix

diff  --git a/lldb/source/Host/common/File.cpp b/lldb/source/Host/common/File.cpp
index daac1fef2f36d..760fb98fb496e 100644
--- a/lldb/source/Host/common/File.cpp
+++ b/lldb/source/Host/common/File.cpp
@@ -23,17 +23,17 @@
 #include <unistd.h>
 #endif
 
-#include "llvm/Support/ConvertUTF.h"
-#include "llvm/Support/Errno.h"
-#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/Process.h"
-
 #include "lldb/Host/Config.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Host.h"
 #include "lldb/Utility/DataBufferHeap.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
+#include "lldb/Utility/VASPrintf.h"
+#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Process.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -215,18 +215,13 @@ size_t File::Printf(const char *format, ...) {
 }
 
 size_t File::PrintfVarArg(const char *format, va_list args) {
-  size_t result = 0;
-  char *s = nullptr;
-  result = vasprintf(&s, format, args);
-  if (s != nullptr) {
-    if (result > 0) {
-      size_t s_len = result;
-      Write(s, s_len);
-      result = s_len;
-    }
-    free(s);
+  llvm::SmallString<0> s;
+  if (VASprintf(s, format, args)) {
+    size_t written = s.size();;
+    Write(s.data(), written);
+    return written;
   }
-  return result;
+  return 0;
 }
 
 Expected<File::OpenOptions> File::GetOptions() const {

diff  --git a/lldb/source/Host/windows/Windows.cpp b/lldb/source/Host/windows/Windows.cpp
deleted file mode 100644
index a74858301ee5a..0000000000000
--- a/lldb/source/Host/windows/Windows.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-//===-- Windows.cpp -------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// This file provides Windows support functions
-
-#include "lldb/Host/PosixApi.h"
-#include "lldb/Host/windows/windows.h"
-
-#include "llvm/Support/ConvertUTF.h"
-
-#include <cassert>
-#include <cctype>
-#include <cerrno>
-#include <cstdarg>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
-#include <io.h>
-
-int vasprintf(char **ret, const char *fmt, va_list ap) {
-  char *buf;
-  int len;
-  size_t buflen;
-  va_list ap2;
-
-  va_copy(ap2, ap);
-  len = vsnprintf(NULL, 0, fmt, ap2);
-
-  if (len >= 0 &&
-      (buf = (char *)malloc((buflen = (size_t)(len + 1)))) != NULL) {
-    len = vsnprintf(buf, buflen, fmt, ap);
-    *ret = buf;
-  } else {
-    *ret = NULL;
-    len = -1;
-  }
-
-  va_end(ap2);
-  return len;
-}
-
-#ifdef _MSC_VER
-
-#if _MSC_VER < 1900
-namespace lldb_private {
-int vsnprintf(char *buffer, size_t count, const char *format, va_list argptr) {
-  int old_errno = errno;
-  int r = ::vsnprintf(buffer, count, format, argptr);
-  int new_errno = errno;
-  buffer[count - 1] = '\0';
-  if (r == -1 || r == count) {
-    FILE *nul = fopen("nul", "w");
-    int bytes_written = ::vfprintf(nul, format, argptr);
-    fclose(nul);
-    if (bytes_written < count)
-      errno = new_errno;
-    else {
-      errno = old_errno;
-      r = bytes_written;
-    }
-  }
-  return r;
-}
-} // namespace lldb_private
-#endif
-
-#endif // _MSC_VER

diff  --git a/lldb/source/Target/RegisterContextUnwind.cpp b/lldb/source/Target/RegisterContextUnwind.cpp
index b90c7225b98ec..98bfdf07f6cea 100644
--- a/lldb/source/Target/RegisterContextUnwind.cpp
+++ b/lldb/source/Target/RegisterContextUnwind.cpp
@@ -35,8 +35,8 @@
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/RegisterValue.h"
+#include "lldb/Utility/VASPrintf.h"
 #include "lldb/lldb-private.h"
-
 #include <memory>
 
 using namespace lldb;
@@ -2326,44 +2326,34 @@ bool RegisterContextUnwind::ReadPC(addr_t &pc) {
 
 void RegisterContextUnwind::UnwindLogMsg(const char *fmt, ...) {
   Log *log = GetLog(LLDBLog::Unwind);
-  if (log) {
-    va_list args;
-    va_start(args, fmt);
-
-    char *logmsg;
-    if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == nullptr) {
-      if (logmsg)
-        free(logmsg);
-      va_end(args);
-      return;
-    }
-    va_end(args);
+  if (!log)
+    return;
 
+  va_list args;
+  va_start(args, fmt);
+
+  llvm::SmallString<0> logmsg;
+  if (VASprintf(logmsg, fmt, args)) {
     LLDB_LOGF(log, "%*sth%d/fr%u %s",
               m_frame_number < 100 ? m_frame_number : 100, "",
-              m_thread.GetIndexID(), m_frame_number, logmsg);
-    free(logmsg);
+              m_thread.GetIndexID(), m_frame_number, logmsg.c_str());
   }
+  va_end(args);
 }
 
 void RegisterContextUnwind::UnwindLogMsgVerbose(const char *fmt, ...) {
   Log *log = GetLog(LLDBLog::Unwind);
-  if (log && log->GetVerbose()) {
-    va_list args;
-    va_start(args, fmt);
-
-    char *logmsg;
-    if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == nullptr) {
-      if (logmsg)
-        free(logmsg);
-      va_end(args);
-      return;
-    }
-    va_end(args);
+  if (!log || !log->GetVerbose())
+    return;
+
+  va_list args;
+  va_start(args, fmt);
 
+  llvm::SmallString<0> logmsg;
+  if (VASprintf(logmsg, fmt, args)) {
     LLDB_LOGF(log, "%*sth%d/fr%u %s",
               m_frame_number < 100 ? m_frame_number : 100, "",
-              m_thread.GetIndexID(), m_frame_number, logmsg);
-    free(logmsg);
+              m_thread.GetIndexID(), m_frame_number, logmsg.c_str());
   }
+  va_end(args);
 }


        


More information about the lldb-commits mailing list