Skip to content

Commit e441298

Browse files
author
git apple-llvm automerger
committed
Merge commit 'ea251536d55f' from llvm.org/main into next
2 parents f667bb0 + ea25153 commit e441298

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

llvm/include/llvm/Support/Format.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,20 @@ class LLVM_ABI format_object_base {
7878
/// printed, this synthesizes the string into a temporary buffer provided and
7979
/// returns whether or not it is big enough.
8080

81+
namespace detail {
82+
template <typename T> struct decay_if_c_char_array {
83+
using type = T;
84+
};
85+
template <std::size_t N> struct decay_if_c_char_array<char[N]> {
86+
using type = const char *;
87+
};
88+
template <typename T>
89+
using decay_if_c_char_array_t = typename decay_if_c_char_array<T>::type;
90+
} // namespace detail
91+
8192
template <typename... Ts>
8293
class format_object final : public format_object_base {
83-
std::tuple<Ts...> Vals;
94+
std::tuple<detail::decay_if_c_char_array_t<Ts>...> Vals;
8495

8596
template <std::size_t... Is>
8697
int snprint_tuple(char *Buffer, unsigned BufferSize,
@@ -96,7 +107,7 @@ class format_object final : public format_object_base {
96107
format_object(const char *fmt, const Ts &... vals)
97108
: format_object_base(fmt), Vals(vals...) {
98109
static_assert(
99-
(std::is_scalar_v<Ts> && ...),
110+
(std::is_scalar_v<detail::decay_if_c_char_array_t<Ts>> && ...),
100111
"format can't be used with non fundamental / non pointer type");
101112
}
102113

llvm/unittests/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_llvm_unittest(SupportTests
4444
ExtensibleRTTITest.cpp
4545
FileCollectorTest.cpp
4646
FileOutputBufferTest.cpp
47+
Format.cpp
4748
FormatVariadicTest.cpp
4849
FSUniqueIDTest.cpp
4950
GenericDomTreeTest.cpp

llvm/unittests/Support/Format.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Support/Format.h"
10+
#include "gtest/gtest.h"
11+
12+
using namespace llvm;
13+
14+
namespace {
15+
16+
template <typename FormatTy>
17+
std::string printToString(unsigned MaxN, FormatTy &&Fmt) {
18+
std::vector<char> Dst(MaxN + 2);
19+
int N = Fmt.snprint(Dst.data(), Dst.size());
20+
Dst.back() = 0;
21+
return N < 0 ? "" : Dst.data();
22+
}
23+
24+
template <typename Expected, typename Arg>
25+
constexpr bool checkDecayTypeEq(const Arg &arg) {
26+
return std::is_same_v<detail::decay_if_c_char_array_t<Arg>, Expected>;
27+
}
28+
29+
TEST(Format, DecayIfCCharArray) {
30+
char Array[] = "Array";
31+
const char ConstArray[] = "ConstArray";
32+
char PtrBuf[] = "Ptr";
33+
char *Ptr = PtrBuf;
34+
const char *PtrToConst = "PtrToConst";
35+
36+
EXPECT_EQ(" Literal", printToString(20, format("%15s", "Literal")));
37+
EXPECT_EQ(" Array", printToString(20, format("%15s", Array)));
38+
EXPECT_EQ(" ConstArray", printToString(20, format("%15s", ConstArray)));
39+
EXPECT_EQ(" Ptr", printToString(20, format("%15s", Ptr)));
40+
EXPECT_EQ(" PtrToConst", printToString(20, format("%15s", PtrToConst)));
41+
42+
EXPECT_TRUE(checkDecayTypeEq<const char *>("Literal"));
43+
EXPECT_TRUE(checkDecayTypeEq<const char *>(Array));
44+
EXPECT_TRUE(checkDecayTypeEq<const char *>(ConstArray));
45+
EXPECT_TRUE(checkDecayTypeEq<char *>(Ptr));
46+
EXPECT_TRUE(checkDecayTypeEq<const char *>(PtrToConst));
47+
EXPECT_TRUE(checkDecayTypeEq<char>(PtrToConst[0]));
48+
EXPECT_TRUE(
49+
checkDecayTypeEq<const char *>(static_cast<const char *>("Literal")));
50+
51+
wchar_t WCharArray[] = L"WCharArray";
52+
EXPECT_TRUE(checkDecayTypeEq<wchar_t[11]>(WCharArray));
53+
EXPECT_TRUE(checkDecayTypeEq<wchar_t>(WCharArray[0]));
54+
}
55+
56+
} // namespace

0 commit comments

Comments
 (0)