[PATCH] D94377: [StringExtras] Add a helper class for comma-separated lists

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 10 13:23:18 PST 2021


kazu created this revision.
kazu added a reviewer: dblaikie.
Herald added subscribers: dexonsmith, hiraditya.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch introduces a helper class SubsequentDelim to simplify loops
that generate a comma-separated lists.

For example, consider the following loop, taken from
llvm/lib/CodeGen/MachineBasicBlock.cpp:

  for (auto I = pred_begin(), E = pred_end(); I != E; ++I) {
    if (I != pred_begin())
      OS << ", ";
    OS << printMBBReference(**I);
  }

The new class allows us to rewrite the loop as:

  SubsequentDelim SD;
  for (auto I = pred_begin(), E = pred_end(); I != E; ++I)
    OS << SD << printMBBReference(**I);

where SD evaluates to the empty string for the first time and ", " for
subsequent iterations.

Unlike interleaveComma, defined in llvm/include/llvm/ADT/STLExtras.h,
SubsequentDelim can accommodate a wider variety of loops, including:

- those that conditionally skip certain items,
- those that need iterators to call getSuccProbability(I), and
- those that iterate over integer ranges.

As an example, this patch cleans up MachineBasicBlock::print.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94377

Files:
  llvm/include/llvm/ADT/StringExtras.h
  llvm/lib/CodeGen/MachineBasicBlock.cpp
  llvm/unittests/ADT/StringExtrasTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94377.315676.patch
Type: text/x-patch
Size: 3582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210110/275b5e13/attachment-0001.bin>


More information about the llvm-commits mailing list