LCOV - code coverage report
Current view: top level - include/llvm/Support - FormatCommon.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 29 29 100.0 %
Date: 2017-09-14 15:23:50 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #ifndef LLVM_SUPPORT_FORMATCOMMON_H
      11             : #define LLVM_SUPPORT_FORMATCOMMON_H
      12             : 
      13             : #include "llvm/ADT/SmallString.h"
      14             : #include "llvm/Support/FormatVariadicDetails.h"
      15             : #include "llvm/Support/raw_ostream.h"
      16             : 
      17             : namespace llvm {
      18             : enum class AlignStyle { Left, Center, Right };
      19             : 
      20             : struct FmtAlign {
      21             :   detail::format_adapter &Adapter;
      22             :   AlignStyle Where;
      23             :   size_t Amount;
      24             :   char Fill;
      25             : 
      26             :   FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount,
      27             :            char Fill = ' ')
      28       17930 :       : Adapter(Adapter), Where(Where), Amount(Amount), Fill(Fill) {}
      29             : 
      30       17930 :   void format(raw_ostream &S, StringRef Options) {
      31             :     // If we don't need to align, we can format straight into the underlying
      32             :     // stream.  Otherwise we have to go through an intermediate stream first
      33             :     // in order to calculate how long the output is so we can align it.
      34             :     // TODO: Make the format method return the number of bytes written, that
      35             :     // way we can also skip the intermediate stream for left-aligned output.
      36       17930 :     if (Amount == 0) {
      37       15394 :       Adapter.format(S, Options);
      38       15394 :       return;
      39             :     }
      40        4165 :     SmallString<64> Item;
      41        4165 :     raw_svector_ostream Stream(Item);
      42             : 
      43        2536 :     Adapter.format(Stream, Options);
      44        5072 :     if (Amount <= Item.size()) {
      45         907 :       S << Item;
      46         907 :       return;
      47             :     }
      48             : 
      49        3258 :     size_t PadAmount = Amount - Item.size();
      50        1629 :     switch (Where) {
      51         188 :     case AlignStyle::Left:
      52         188 :       S << Item;
      53         188 :       fill(S, PadAmount);
      54         188 :       break;
      55         220 :     case AlignStyle::Center: {
      56         220 :       size_t X = PadAmount / 2;
      57         220 :       fill(S, X);
      58         220 :       S << Item;
      59         220 :       fill(S, PadAmount - X);
      60         220 :       break;
      61             :     }
      62        1221 :     default:
      63        1221 :       fill(S, PadAmount);
      64             :       S << Item;
      65             :       break;
      66             :     }
      67             :   }
      68             : 
      69             : private:
      70        1849 :   void fill(llvm::raw_ostream &S, uint32_t Count) {
      71       21912 :     for (uint32_t I = 0; I < Count; ++I)
      72       40126 :       S << Fill;
      73        1849 :   }
      74             : };
      75             : }
      76             : 
      77             : #endif

Generated by: LCOV version 1.13