[llvm] [Support] Add format object for interleaved ranges (PR #135517)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 15 11:03:31 PDT 2025


================
@@ -0,0 +1,99 @@
+//===- InterleavedRange.h - Output stream formatting for ranges -----------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements format objects for printing ranges to output streams.
+// For example:
+// ```c++
+//    ArrayRef<Type> Types = ...;
+//    OS << "Types: " << interleaved(Types); // ==> "Types: i32, f16, i8"
+//    ArrayRef<int> Values = ...;
+//    OS << "Values: " << interleaved_array(Values); // ==> "Values: [1, 2, 3]"
+// ```
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_INTERLEAVED_RANGE_H
+#define LLVM_SUPPORT_INTERLEAVED_RANGE_H
+
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+
+/// Format object class for interleaved ranges. Supports specifying the
+/// separator and, optionally, the prefix and suffix to be printed surrounding
+/// the range.
+/// Uses the operator '<<' of the range element type for printing. The range
+/// type itself does not have to have an '<<' operator defined.
+template <typename Range> class InterleavedRange {
+  const Range &TheRange;
+  StringRef Separator;
+  StringRef Prefix;
+  StringRef Suffix;
+
+public:
+  InterleavedRange(const Range &R, StringRef Separator, StringRef Prefix,
+                   StringRef Suffix)
+      : TheRange(R), Separator(Separator), Prefix(Prefix), Suffix(Suffix) {}
+
+  friend raw_ostream &operator<<(raw_ostream &OS,
+                                 const InterleavedRange &Interleaved) {
+    if (!Interleaved.Prefix.empty())
+      OS << Interleaved.Prefix;
+    llvm::interleave(Interleaved.TheRange, OS, Interleaved.Separator);
+    if (!Interleaved.Suffix.empty())
+      OS << Interleaved.Suffix;
+    return OS;
+  }
+
+  std::string str() const {
+    std::string Result;
+    raw_string_ostream Stream(Result);
+    Stream << *this;
+    Stream.flush();
+    return Result;
+  }
+
+  operator std::string() const { return str(); }
----------------
kuhar wrote:

`llvm:'formatv` doesn't use explicit either and it seems to work fine. Do you see some footguns here? Because this is not a reference/handle/pointer type, I think it's OK.

https://github.com/llvm/llvm-project/pull/135517


More information about the llvm-commits mailing list