LCOV - code coverage report
Current view: top level - lib/Support - FormattedStream.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 30 39 76.9 %
Date: 2017-09-14 15:23:50 Functions: 4 7 57.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- 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             : // This file contains the implementation of formatted_raw_ostream.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Support/FormattedStream.h"
      15             : #include "llvm/Support/Debug.h"
      16             : #include "llvm/Support/raw_ostream.h"
      17             : #include <algorithm>
      18             : 
      19             : using namespace llvm;
      20             : 
      21             : /// UpdatePosition - Examine the given char sequence and figure out which
      22             : /// column we end up in after output, and how many line breaks are contained.
      23             : ///
      24   169653420 : static void UpdatePosition(std::pair<unsigned, unsigned> &Position, const char *Ptr, size_t Size) {
      25   169653420 :   unsigned &Column = Position.first;
      26   169653420 :   unsigned &Line = Position.second;
      27             : 
      28             :   // Keep track of the current column and line by scanning the string for
      29             :   // special characters
      30  3149606179 :   for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
      31  2979952759 :     ++Column;
      32  2979952759 :     switch (*Ptr) {
      33    41541032 :     case '\n':
      34    41541032 :       Line += 1;
      35             :       LLVM_FALLTHROUGH;
      36    41541032 :     case '\r':
      37    41541032 :       Column = 0;
      38    41541032 :       break;
      39     5606345 :     case '\t':
      40             :       // Assumes tab stop = 8 characters.
      41     5606345 :       Column += (8 - (Column & 0x7)) & 0x7;
      42     5606345 :       break;
      43             :     }
      44             :   }
      45   169653420 : }
      46             : 
      47             : /// ComputePosition - Examine the current output and update line and column
      48             : /// counts.
      49   169653420 : void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
      50             :   // If our previous scan pointer is inside the buffer, assume we already
      51             :   // scanned those bytes. This depends on raw_ostream to not change our buffer
      52             :   // in unexpected ways.
      53   169653420 :   if (Ptr <= Scanned && Scanned <= Ptr + Size)
      54             :     // Scan all characters added since our last scan to determine the new
      55             :     // column.
      56    14955029 :     UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
      57             :   else
      58   154698391 :     UpdatePosition(Position, Ptr, Size);
      59             : 
      60             :   // Update the scanning pointer.
      61   169653420 :   Scanned = Ptr + Size;
      62   169653420 : }
      63             : 
      64             : /// PadToColumn - Align the output to some column number.
      65             : ///
      66             : /// \param NewCol - The column to move to.
      67             : ///
      68    14955037 : formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) { 
      69             :   // Figure out what's in the buffer and add it to the column count.
      70    29910074 :   ComputePosition(getBufferStart(), GetNumBytesInBuffer());
      71             : 
      72             :   // Output spaces until we reach the desired column.
      73    29910074 :   indent(std::max(int(NewCol - getColumn()), 1));
      74    14955037 :   return *this;
      75             : }
      76             : 
      77   154698383 : void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
      78             :   // Figure out what's in the buffer and add it to the column count.
      79   154698383 :   ComputePosition(Ptr, Size);
      80             : 
      81             :   // Write the data to the underlying stream (which is unbuffered, so
      82             :   // the data will be immediately written out).
      83   154698383 :   TheStream->write(Ptr, Size);
      84             : 
      85             :   // Reset the scanning pointer.
      86   154698383 :   Scanned = nullptr;
      87   154698383 : }
      88             : 
      89             : /// fouts() - This returns a reference to a formatted_raw_ostream for
      90             : /// standard output.  Use it like: fouts() << "foo" << "bar";
      91           0 : formatted_raw_ostream &llvm::fouts() {
      92           0 :   static formatted_raw_ostream S(outs());
      93           0 :   return S;
      94             : }
      95             : 
      96             : /// ferrs() - This returns a reference to a formatted_raw_ostream for
      97             : /// standard error.  Use it like: ferrs() << "foo" << "bar";
      98           0 : formatted_raw_ostream &llvm::ferrs() {
      99           0 :   static formatted_raw_ostream S(errs());
     100           0 :   return S;
     101             : }
     102             : 
     103             : /// fdbgs() - This returns a reference to a formatted_raw_ostream for
     104             : /// the debug stream.  Use it like: fdbgs() << "foo" << "bar";
     105           0 : formatted_raw_ostream &llvm::fdbgs() {
     106           0 :   static formatted_raw_ostream S(dbgs());
     107           0 :   return S;
     108             : }

Generated by: LCOV version 1.13