xref: /src/contrib/llvm-project/lldb/source/Utility/VASprintf.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1cfca06d7SDimitry Andric //===-- VASprintf.cpp -----------------------------------------------------===//
274a628f7SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
674a628f7SDimitry Andric //
774a628f7SDimitry Andric //===----------------------------------------------------------------------===//
874a628f7SDimitry Andric 
974a628f7SDimitry Andric #include "lldb/Utility/VASPrintf.h"
1074a628f7SDimitry Andric 
1174a628f7SDimitry Andric #include "llvm/ADT/SmallString.h"
1294994d37SDimitry Andric #include "llvm/ADT/SmallVector.h"
1394994d37SDimitry Andric #include "llvm/ADT/StringRef.h"
1474a628f7SDimitry Andric 
15344a3780SDimitry Andric #include <cassert>
16344a3780SDimitry Andric #include <cstdarg>
17344a3780SDimitry Andric #include <cstdio>
1874a628f7SDimitry Andric 
VASprintf(llvm::SmallVectorImpl<char> & buf,const char * fmt,va_list args)1974a628f7SDimitry Andric bool lldb_private::VASprintf(llvm::SmallVectorImpl<char> &buf, const char *fmt,
2074a628f7SDimitry Andric                              va_list args) {
2174a628f7SDimitry Andric   llvm::SmallString<16> error("<Encoding error>");
2274a628f7SDimitry Andric   bool result = true;
2374a628f7SDimitry Andric 
2474a628f7SDimitry Andric   // Copy in case our first call to vsnprintf doesn't fit into our buffer
2574a628f7SDimitry Andric   va_list copy_args;
2674a628f7SDimitry Andric   va_copy(copy_args, args);
2774a628f7SDimitry Andric 
2874a628f7SDimitry Andric   buf.resize(buf.capacity());
2974a628f7SDimitry Andric   // Write up to `capacity` bytes, ignoring the current size.
3074a628f7SDimitry Andric   int length = ::vsnprintf(buf.data(), buf.size(), fmt, args);
3174a628f7SDimitry Andric   if (length < 0) {
3274a628f7SDimitry Andric     buf = error;
3374a628f7SDimitry Andric     result = false;
3474a628f7SDimitry Andric     goto finish;
3574a628f7SDimitry Andric   }
3674a628f7SDimitry Andric 
3774a628f7SDimitry Andric   if (size_t(length) >= buf.size()) {
38f73363f1SDimitry Andric     // The error formatted string didn't fit into our buffer, resize it to the
39f73363f1SDimitry Andric     // exact needed size, and retry
4074a628f7SDimitry Andric     buf.resize(length + 1);
4174a628f7SDimitry Andric     length = ::vsnprintf(buf.data(), buf.size(), fmt, copy_args);
4274a628f7SDimitry Andric     if (length < 0) {
4374a628f7SDimitry Andric       buf = error;
4474a628f7SDimitry Andric       result = false;
4574a628f7SDimitry Andric       goto finish;
4674a628f7SDimitry Andric     }
4774a628f7SDimitry Andric     assert(size_t(length) < buf.size());
4874a628f7SDimitry Andric   }
4974a628f7SDimitry Andric   buf.resize(length);
5074a628f7SDimitry Andric 
5174a628f7SDimitry Andric finish:
5274a628f7SDimitry Andric   va_end(args);
5374a628f7SDimitry Andric   va_end(copy_args);
5474a628f7SDimitry Andric   return result;
5574a628f7SDimitry Andric }
56