[all-commits] [llvm/llvm-project] 407b1e: [StringExtras] Add a helper class for comma-separa...

llvmbot via All-commits all-commits at lists.llvm.org
Sun Jan 10 14:37:09 PST 2021


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 407b1e65a464081e28c325273b65e8eafdfad1d4
      https://github.com/llvm/llvm-project/commit/407b1e65a464081e28c325273b65e8eafdfad1d4
  Author: Kazu Hirata <kazu at google.com>
  Date:   2021-01-10 (Sun, 10 Jan 2021)

  Changed paths:
    M llvm/include/llvm/ADT/StringExtras.h
    M llvm/lib/CodeGen/MachineBasicBlock.cpp
    M llvm/unittests/ADT/StringExtrasTest.cpp

  Log Message:
  -----------
  [StringExtras] Add a helper class for comma-separated lists

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.

Differential Revision: https://reviews.llvm.org/D94377




More information about the All-commits mailing list