Skip to content

Commit b714fc7

Browse files
authored
Move format internal code from llvm::detail to llvm::support::detail. (llvm#87288)
Some support code, e.g. llvm/Support/Endian.h, uses llvm::support::detail, but the format-related code uses llvm::detail. On VS2019, when a C++ file includes both headers, a `detail::` from `namespace llvm { ... }` becomes ambiguous. 44253a9 breaks TensorFlow and [JAX](https://github.com/google/jax/actions/runs/8507773013/job/23300219405) build because of this. Since llvm::X::detail seems like a cleaner solution and is used in other places as well (e.g. llvm::yaml::detail), we should probably migrate all llvm::detail usages to llvm::X::detail.
1 parent 6b7b18a commit b714fc7

File tree

9 files changed

+73
-58
lines changed

9 files changed

+73
-58
lines changed

llvm/include/llvm/Support/FormatAdapters.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
#include "llvm/Support/raw_ostream.h"
1717

1818
namespace llvm {
19-
template <typename T> class FormatAdapter : public detail::format_adapter {
19+
template <typename T>
20+
class FormatAdapter : public support::detail::format_adapter {
2021
protected:
2122
explicit FormatAdapter(T &&Item) : Item(std::forward<T>(Item)) {}
2223

2324
T Item;
2425
};
2526

27+
namespace support {
2628
namespace detail {
2729
template <typename T> class AlignAdapter final : public FormatAdapter<T> {
2830
AlignStyle Where;
@@ -80,29 +82,31 @@ class ErrorAdapter : public FormatAdapter<Error> {
8082
Stream << Item;
8183
}
8284
};
83-
}
85+
} // namespace detail
86+
} // namespace support
8487

8588
template <typename T>
86-
detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount,
87-
char Fill = ' ') {
88-
return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
89+
support::detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where,
90+
size_t Amount, char Fill = ' ') {
91+
return support::detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount,
92+
Fill);
8993
}
9094

9195
template <typename T>
92-
detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
93-
return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
96+
support::detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
97+
return support::detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
9498
}
9599

96100
template <typename T>
97-
detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
98-
return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
101+
support::detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
102+
return support::detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
99103
}
100104

101105
// llvm::Error values must be consumed before being destroyed.
102106
// Wrapping an error in fmt_consume explicitly indicates that the formatv_object
103107
// should take ownership and consume it.
104-
inline detail::ErrorAdapter fmt_consume(Error &&Item) {
105-
return detail::ErrorAdapter(std::move(Item));
108+
inline support::detail::ErrorAdapter fmt_consume(Error &&Item) {
109+
return support::detail::ErrorAdapter(std::move(Item));
106110
}
107111
}
108112

llvm/include/llvm/Support/FormatCommon.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ namespace llvm {
1717
enum class AlignStyle { Left, Center, Right };
1818

1919
struct FmtAlign {
20-
detail::format_adapter &Adapter;
20+
support::detail::format_adapter &Adapter;
2121
AlignStyle Where;
2222
size_t Amount;
2323
char Fill;
2424

25-
FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount,
26-
char Fill = ' ')
25+
FmtAlign(support::detail::format_adapter &Adapter, AlignStyle Where,
26+
size_t Amount, char Fill = ' ')
2727
: Adapter(Adapter), Where(Where), Amount(Amount), Fill(Fill) {}
2828

2929
void format(raw_ostream &S, StringRef Options) {

llvm/include/llvm/Support/FormatProviders.h

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <type_traits>
2626

2727
namespace llvm {
28+
namespace support {
2829
namespace detail {
2930
template <typename T>
3031
struct use_integral_formatter
@@ -98,7 +99,8 @@ class HelperFunctions {
9899
return Default;
99100
}
100101
};
101-
}
102+
} // namespace detail
103+
} // namespace support
102104

103105
/// Implementation of format_provider<T> for integral arithmetic types.
104106
///
@@ -125,8 +127,8 @@ class HelperFunctions {
125127

126128
template <typename T>
127129
struct format_provider<
128-
T, std::enable_if_t<detail::use_integral_formatter<T>::value>>
129-
: public detail::HelperFunctions {
130+
T, std::enable_if_t<support::detail::use_integral_formatter<T>::value>>
131+
: public support::detail::HelperFunctions {
130132
private:
131133
public:
132134
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
@@ -174,8 +176,8 @@ struct format_provider<
174176
/// cases indicates the minimum number of nibbles to print.
175177
template <typename T>
176178
struct format_provider<
177-
T, std::enable_if_t<detail::use_pointer_formatter<T>::value>>
178-
: public detail::HelperFunctions {
179+
T, std::enable_if_t<support::detail::use_pointer_formatter<T>::value>>
180+
: public support::detail::HelperFunctions {
179181
private:
180182
public:
181183
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
@@ -199,7 +201,7 @@ struct format_provider<
199201

200202
template <typename T>
201203
struct format_provider<
202-
T, std::enable_if_t<detail::use_string_formatter<T>::value>> {
204+
T, std::enable_if_t<support::detail::use_string_formatter<T>::value>> {
203205
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
204206
size_t N = StringRef::npos;
205207
if (!Style.empty() && Style.getAsInteger(10, N)) {
@@ -231,8 +233,8 @@ template <> struct format_provider<Twine> {
231233
/// character. Otherwise, it is treated as an integer options string.
232234
///
233235
template <typename T>
234-
struct format_provider<T,
235-
std::enable_if_t<detail::use_char_formatter<T>::value>> {
236+
struct format_provider<
237+
T, std::enable_if_t<support::detail::use_char_formatter<T>::value>> {
236238
static void format(const char &V, llvm::raw_ostream &Stream,
237239
StringRef Style) {
238240
if (Style.empty())
@@ -297,9 +299,9 @@ template <> struct format_provider<bool> {
297299
/// else.
298300

299301
template <typename T>
300-
struct format_provider<T,
301-
std::enable_if_t<detail::use_double_formatter<T>::value>>
302-
: public detail::HelperFunctions {
302+
struct format_provider<
303+
T, std::enable_if_t<support::detail::use_double_formatter<T>::value>>
304+
: public support::detail::HelperFunctions {
303305
static void format(const T &V, llvm::raw_ostream &Stream, StringRef Style) {
304306
FloatStyle S;
305307
if (Style.consume_front("P") || Style.consume_front("p"))
@@ -321,15 +323,18 @@ struct format_provider<T,
321323
}
322324
};
323325

326+
namespace support {
324327
namespace detail {
325328
template <typename IterT>
326329
using IterValue = typename std::iterator_traits<IterT>::value_type;
327330

328331
template <typename IterT>
329332
struct range_item_has_provider
330333
: public std::integral_constant<
331-
bool, !uses_missing_provider<IterValue<IterT>>::value> {};
332-
}
334+
bool,
335+
!support::detail::uses_missing_provider<IterValue<IterT>>::value> {};
336+
} // namespace detail
337+
} // namespace support
333338

334339
/// Implementation of format_provider<T> for ranges.
335340
///
@@ -393,7 +398,7 @@ template <typename IterT> class format_provider<llvm::iterator_range<IterT>> {
393398
}
394399

395400
public:
396-
static_assert(detail::range_item_has_provider<IterT>::value,
401+
static_assert(support::detail::range_item_has_provider<IterT>::value,
397402
"Range value_type does not have a format provider!");
398403
static void format(const llvm::iterator_range<IterT> &V,
399404
llvm::raw_ostream &Stream, StringRef Style) {
@@ -403,18 +408,18 @@ template <typename IterT> class format_provider<llvm::iterator_range<IterT>> {
403408
auto Begin = V.begin();
404409
auto End = V.end();
405410
if (Begin != End) {
406-
auto Adapter = detail::build_format_adapter(*Begin);
411+
auto Adapter = support::detail::build_format_adapter(*Begin);
407412
Adapter.format(Stream, ArgStyle);
408413
++Begin;
409414
}
410415
while (Begin != End) {
411416
Stream << Sep;
412-
auto Adapter = detail::build_format_adapter(*Begin);
417+
auto Adapter = support::detail::build_format_adapter(*Begin);
413418
Adapter.format(Stream, ArgStyle);
414419
++Begin;
415420
}
416421
}
417422
};
418-
}
423+
} // namespace llvm
419424

420425
#endif

llvm/include/llvm/Support/FormatVariadic.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct ReplacementItem {
6666
class formatv_object_base {
6767
protected:
6868
StringRef Fmt;
69-
ArrayRef<detail::format_adapter *> Adapters;
69+
ArrayRef<support::detail::format_adapter *> Adapters;
7070

7171
static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
7272
size_t &Align, char &Pad);
@@ -75,7 +75,7 @@ class formatv_object_base {
7575
splitLiteralAndReplacement(StringRef Fmt);
7676

7777
formatv_object_base(StringRef Fmt,
78-
ArrayRef<detail::format_adapter *> Adapters)
78+
ArrayRef<support::detail::format_adapter *> Adapters)
7979
: Fmt(Fmt), Adapters(Adapters) {}
8080

8181
formatv_object_base(formatv_object_base const &rhs) = delete;
@@ -130,7 +130,7 @@ template <typename Tuple> class formatv_object : public formatv_object_base {
130130
// of the parameters, we have to own the storage for the parameters here, and
131131
// have the base class store type-erased pointers into this tuple.
132132
Tuple Parameters;
133-
std::array<detail::format_adapter *, std::tuple_size<Tuple>::value>
133+
std::array<support::detail::format_adapter *, std::tuple_size<Tuple>::value>
134134
ParameterPointers;
135135

136136
// The parameters are stored in a std::tuple, which does not provide runtime
@@ -142,8 +142,8 @@ template <typename Tuple> class formatv_object : public formatv_object_base {
142142
// std::array<Base*>.
143143
struct create_adapters {
144144
template <typename... Ts>
145-
std::array<detail::format_adapter *, std::tuple_size<Tuple>::value>
146-
operator()(Ts &... Items) {
145+
std::array<support::detail::format_adapter *, std::tuple_size<Tuple>::value>
146+
operator()(Ts &...Items) {
147147
return {{&Items...}};
148148
}
149149
};
@@ -248,13 +248,14 @@ template <typename Tuple> class formatv_object : public formatv_object_base {
248248
// the details of what that is are undefined.
249249
//
250250
template <typename... Ts>
251-
inline auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object<decltype(
252-
std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...))> {
253-
using ParamTuple = decltype(
254-
std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...));
251+
inline auto formatv(const char *Fmt, Ts &&...Vals)
252+
-> formatv_object<decltype(std::make_tuple(
253+
support::detail::build_format_adapter(std::forward<Ts>(Vals))...))> {
254+
using ParamTuple = decltype(std::make_tuple(
255+
support::detail::build_format_adapter(std::forward<Ts>(Vals))...));
255256
return formatv_object<ParamTuple>(
256-
Fmt,
257-
std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...));
257+
Fmt, std::make_tuple(support::detail::build_format_adapter(
258+
std::forward<Ts>(Vals))...));
258259
}
259260

260261
} // end namespace llvm

llvm/include/llvm/Support/FormatVariadicDetails.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace llvm {
1919
template <typename T, typename Enable = void> struct format_provider {};
2020
class Error;
2121

22+
namespace support {
2223
namespace detail {
2324
class format_adapter {
2425
virtual void anchor();
@@ -156,7 +157,8 @@ std::enable_if_t<uses_missing_provider<T>::value, missing_format_adapter<T>>
156157
build_format_adapter(T &&) {
157158
return missing_format_adapter<T>();
158159
}
159-
}
160-
}
160+
} // namespace detail
161+
} // namespace support
162+
} // namespace llvm
161163

162164
#endif

llvm/lib/Support/FormatVariadic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,4 @@ formatv_object_base::parseFormatString(StringRef Fmt) {
152152
return Replacements;
153153
}
154154

155-
void detail::format_adapter::anchor() { }
155+
void support::detail::format_adapter::anchor() {}

llvm/unittests/Support/FormatVariadicTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ struct Format : public FormatAdapter<int> {
2020
void format(raw_ostream &OS, StringRef Opt) override { OS << "Format"; }
2121
};
2222

23-
using detail::uses_format_member;
24-
using detail::uses_missing_provider;
23+
using support::detail::uses_format_member;
24+
using support::detail::uses_missing_provider;
2525

2626
static_assert(uses_format_member<Format>::value, "");
2727
static_assert(uses_format_member<Format &>::value, "");

mlir/include/mlir/TableGen/Format.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ class FmtObjectBase {
133133
// std::vector<Base*>.
134134
struct CreateAdapters {
135135
template <typename... Ts>
136-
std::vector<llvm::detail::format_adapter *> operator()(Ts &...items) {
137-
return std::vector<llvm::detail::format_adapter *>{&items...};
136+
std::vector<llvm::support::detail::format_adapter *>
137+
operator()(Ts &...items) {
138+
return std::vector<llvm::support::detail::format_adapter *>{&items...};
138139
}
139140
};
140141

141142
StringRef fmt;
142143
const FmtContext *context;
143-
std::vector<llvm::detail::format_adapter *> adapters;
144+
std::vector<llvm::support::detail::format_adapter *> adapters;
144145
std::vector<FmtReplacement> replacements;
145146

146147
public:
@@ -205,8 +206,8 @@ class FmtObject : public FmtObjectBase {
205206

206207
class FmtStrVecObject : public FmtObjectBase {
207208
public:
208-
using StrFormatAdapter =
209-
decltype(llvm::detail::build_format_adapter(std::declval<std::string>()));
209+
using StrFormatAdapter = decltype(llvm::support::detail::build_format_adapter(
210+
std::declval<std::string>()));
210211

211212
FmtStrVecObject(StringRef fmt, const FmtContext *ctx,
212213
ArrayRef<std::string> params);
@@ -259,14 +260,15 @@ class FmtStrVecObject : public FmtObjectBase {
259260
/// in C++ code generation.
260261
template <typename... Ts>
261262
inline auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&...vals)
262-
-> FmtObject<decltype(std::make_tuple(
263-
llvm::detail::build_format_adapter(std::forward<Ts>(vals))...))> {
263+
-> FmtObject<
264+
decltype(std::make_tuple(llvm::support::detail::build_format_adapter(
265+
std::forward<Ts>(vals))...))> {
264266
using ParamTuple = decltype(std::make_tuple(
265-
llvm::detail::build_format_adapter(std::forward<Ts>(vals))...));
267+
llvm::support::detail::build_format_adapter(std::forward<Ts>(vals))...));
266268
return FmtObject<ParamTuple>(
267269
fmt, ctx,
268-
std::make_tuple(
269-
llvm::detail::build_format_adapter(std::forward<Ts>(vals))...));
270+
std::make_tuple(llvm::support::detail::build_format_adapter(
271+
std::forward<Ts>(vals))...));
270272
}
271273

272274
inline FmtStrVecObject tgfmt(StringRef fmt, const FmtContext *ctx,

mlir/lib/TableGen/Format.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ FmtStrVecObject::FmtStrVecObject(StringRef fmt, const FmtContext *ctx,
203203
: FmtObjectBase(fmt, ctx, params.size()) {
204204
parameters.reserve(params.size());
205205
for (std::string p : params)
206-
parameters.push_back(llvm::detail::build_format_adapter(std::move(p)));
206+
parameters.push_back(
207+
llvm::support::detail::build_format_adapter(std::move(p)));
207208

208209
adapters.reserve(parameters.size());
209210
for (auto &p : parameters)

0 commit comments

Comments
 (0)