Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //===- FormatCommon.h - Formatters for common LLVM types --------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SUPPORT_FORMATCOMMON_H
0010 #define LLVM_SUPPORT_FORMATCOMMON_H
0011 
0012 #include "llvm/ADT/SmallString.h"
0013 #include "llvm/Support/FormatVariadicDetails.h"
0014 #include "llvm/Support/raw_ostream.h"
0015 
0016 namespace llvm {
0017 enum class AlignStyle { Left, Center, Right };
0018 
0019 /// Helper class to format to a \p Width wide field, with alignment \p Where
0020 /// within that field.
0021 struct FmtAlign {
0022   support::detail::format_adapter &Adapter;
0023   AlignStyle Where;
0024   unsigned Width;
0025   char Fill;
0026 
0027   FmtAlign(support::detail::format_adapter &Adapter, AlignStyle Where,
0028            unsigned Width, char Fill = ' ')
0029       : Adapter(Adapter), Where(Where), Width(Width), Fill(Fill) {}
0030 
0031   void format(raw_ostream &S, StringRef Options) {
0032     // If we don't need to align, we can format straight into the underlying
0033     // stream.  Otherwise we have to go through an intermediate stream first
0034     // in order to calculate how long the output is so we can align it.
0035     // TODO: Make the format method return the number of bytes written, that
0036     // way we can also skip the intermediate stream for left-aligned output.
0037     if (Width == 0) {
0038       Adapter.format(S, Options);
0039       return;
0040     }
0041     SmallString<64> Item;
0042     raw_svector_ostream Stream(Item);
0043 
0044     Adapter.format(Stream, Options);
0045     if (Width <= Item.size()) {
0046       S << Item;
0047       return;
0048     }
0049 
0050     unsigned PadAmount = Width - static_cast<unsigned>(Item.size());
0051     switch (Where) {
0052     case AlignStyle::Left:
0053       S << Item;
0054       fill(S, PadAmount);
0055       break;
0056     case AlignStyle::Center: {
0057       unsigned X = PadAmount / 2;
0058       fill(S, X);
0059       S << Item;
0060       fill(S, PadAmount - X);
0061       break;
0062     }
0063     default:
0064       fill(S, PadAmount);
0065       S << Item;
0066       break;
0067     }
0068   }
0069 
0070 private:
0071   void fill(llvm::raw_ostream &S, unsigned Count) {
0072     for (unsigned I = 0; I < Count; ++I)
0073       S << Fill;
0074   }
0075 };
0076 }
0077 
0078 #endif