[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.
This commit is contained in:
Pavel Labath 2022-03-30 09:28:19 +02:00
parent e59335e891
commit 1410a4860e
5 changed files with 28 additions and 120 deletions

View file

@ -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; }

View file

@ -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

View file

@ -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 {

View file

@ -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

View file

@ -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);
if (!log)
return;
char *logmsg;
if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == nullptr) {
if (logmsg)
free(logmsg);
va_end(args);
return;
}
va_end(args);
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);
if (!log || !log->GetVerbose())
return;
char *logmsg;
if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == nullptr) {
if (logmsg)
free(logmsg);
va_end(args);
return;
}
va_end(args);
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);
}