Skip to content

Commit 3fa2e68

Browse files
trenoufFlakebi
authored andcommitted
Fix for LLVM format_adapter -> FormatFunctorRef
Required from LLVM 3aec6a40bb4e [Support] Remove virtual functions from formatv (#207516) but keeping support for earlier LLVM.
1 parent 5c443e2 commit 3fa2e68

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎include/llvm-dialects/TableGen/Format.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ class FmtObjectBase {
187187
// indexing capabilities. In order to enable runtime indexing, we use this
188188
// structure to put the parameters into a std::vector. Since the parameters
189189
// are not all the same type, we use some type-erasure by wrapping the
190+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
191+
// parameters in a template class and refer to them using function_refs.
192+
struct CreateAdapters {
193+
template <typename... Ts>
194+
std::vector<llvm::support::detail::FormatFunctorRef>
195+
operator()(Ts &... items) {
196+
return std::vector<llvm::support::detail::FormatFunctorRef>{items...};
197+
}
198+
};
199+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
190200
// parameters in a template class that derives from a non-template superclass.
191201
// Essentially, we are converting a std::tuple<Derived<Ts...>> to a
192202
// std::vector<Base*>.
@@ -197,10 +207,15 @@ class FmtObjectBase {
197207
return std::vector<llvm::support::detail::format_adapter *>{&items...};
198208
}
199209
};
210+
#endif // LLVM_MAIN_REVISION
200211

201212
llvm::StringRef fmt;
202213
const FmtContext *context;
214+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
215+
std::vector<llvm::support::detail::FormatFunctorRef> adapters;
216+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
203217
std::vector<llvm::support::detail::format_adapter *> adapters;
218+
#endif // LLVM_MAIN_REVISION
204219
std::vector<FmtReplacement> replacements;
205220

206221
public:
@@ -260,8 +275,13 @@ template <typename Tuple> class FmtObject : public FmtObjectBase {
260275

261276
class FmtStrVecObject : public FmtObjectBase {
262277
public:
278+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
279+
using StrFormatAdapter = decltype(
280+
llvm::support::detail::FormatFunctor(std::declval<std::string>()));
281+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
263282
using StrFormatAdapter = decltype(
264283
llvm::support::detail::build_format_adapter(std::declval<std::string>()));
284+
#endif // LLVM_MAIN_REVISION
265285

266286
FmtStrVecObject(llvm::StringRef fmt, const FmtContext *ctx,
267287
llvm::ArrayRef<std::string> params);
@@ -318,6 +338,20 @@ class FmtStrVecObject : public FmtObjectBase {
318338
/// because '{' and '}' are frequently used in C++ code.
319339
/// 2. This utility does not support format layout because it is rarely needed
320340
/// in C++ code generation.
341+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
342+
template <typename... Ts>
343+
inline auto tgfmt(llvm::StringRef fmt, const FmtContext *ctx, Ts &&... vals)
344+
-> FmtObject<
345+
decltype(std::make_tuple(llvm::support::detail::FormatFunctor(
346+
std::forward<Ts>(vals))...))> {
347+
using ParamTuple = decltype(std::make_tuple(
348+
llvm::support::detail::FormatFunctor(std::forward<Ts>(vals))...));
349+
return FmtObject<ParamTuple>(
350+
fmt, ctx,
351+
std::make_tuple(llvm::support::detail::FormatFunctor(
352+
std::forward<Ts>(vals))...));
353+
}
354+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
321355
template <typename... Ts>
322356
inline auto tgfmt(llvm::StringRef fmt, const FmtContext *ctx, Ts &&... vals)
323357
-> FmtObject<
@@ -330,6 +364,7 @@ inline auto tgfmt(llvm::StringRef fmt, const FmtContext *ctx, Ts &&... vals)
330364
std::make_tuple(llvm::support::detail::build_format_adapter(
331365
std::forward<Ts>(vals))...));
332366
}
367+
#endif // LLVM_MAIN_REVISION
333368

334369
inline FmtStrVecObject tgfmt(llvm::StringRef fmt, const FmtContext *ctx,
335370
llvm::ArrayRef<std::string> params) {

‎lib/TableGen/Format.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,17 @@ void FmtObjectBase::format(raw_ostream &s) const {
218218
s << repl.spec << kMarkerForNoSubst;
219219
continue;
220220
}
221+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
222+
auto range = ArrayRef<llvm::support::detail::FormatFunctorRef>(adapters);
223+
range = range.drop_front(repl.index);
224+
llvm::interleaveComma(range, s,
225+
[&](auto &x) { x(s, /*Options=*/""); });
226+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
221227
auto range = ArrayRef<llvm::support::detail::format_adapter *>(adapters);
222228
range = range.drop_front(repl.index);
223229
llvm::interleaveComma(range, s,
224230
[&](auto &x) { x->format(s, /*Options=*/""); });
231+
#endif // LLVM_MAIN_REVISION
225232
continue;
226233
}
227234

@@ -233,7 +240,11 @@ void FmtObjectBase::format(raw_ostream &s) const {
233240
if (repl.type == FmtReplacement::Type::IndirectSpecialPH) {
234241
std::string indirectSpec;
235242
raw_string_ostream indirectSpecStream(indirectSpec);
243+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
244+
adapters[repl.index](indirectSpecStream, /*Options=*/"");
245+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
236246
adapters[repl.index]->format(indirectSpecStream, /*Options=*/"");
247+
#endif // LLVM_MAIN_REVISION
237248

238249
auto subst = context->getSubstFor(indirectSpec);
239250
if (subst.has_value())
@@ -245,26 +256,46 @@ void FmtObjectBase::format(raw_ostream &s) const {
245256

246257
assert(repl.type == FmtReplacement::Type::PositionalPH);
247258

259+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
260+
adapters[repl.index](s, /*Options=*/"");
261+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
248262
adapters[repl.index]->format(s, /*Options=*/"");
263+
#endif // LLVM_MAIN_REVISION
249264
}
250265
}
251266

252267
FmtStrVecObject::FmtStrVecObject(StringRef fmt, const FmtContext *ctx,
253268
ArrayRef<std::string> params)
254269
: FmtObjectBase(fmt, ctx, params.size()) {
255270
parameters.reserve(params.size());
271+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
256272
for (std::string p : params)
257273
parameters.push_back(
274+
llvm::support::detail::FormatFunctor(std::move(p)));
275+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
276+
for (std::string p : params)
277+
parameters.push_back(
258278
llvm::support::detail::build_format_adapter(std::move(p)));
279+
#endif // LLVM_MAIN_REVISION
259280

260281
adapters.reserve(parameters.size());
282+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
283+
for (auto &p : parameters)
284+
adapters.push_back(p);
285+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
261286
for (auto &p : parameters)
262287
adapters.push_back(&p);
288+
#endif // LLVM_MAIN_REVISION
263289
}
264290

265291
FmtStrVecObject::FmtStrVecObject(FmtStrVecObject &&that)
266292
: FmtObjectBase(std::move(that)), parameters(std::move(that.parameters)) {
267293
adapters.reserve(parameters.size());
294+
#if !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
295+
for (auto &p : parameters)
296+
adapters.push_back(p);
297+
#else // !LLVM_MAIN_REVISION || LLVM_MAIN_REVISION >= 586890
268298
for (auto &p : parameters)
269299
adapters.push_back(&p);
300+
#endif // LLVM_MAIN_REVISION
270301
}

0 commit comments

Comments
 (0)