FormattedStream.h revision 62fe47a33755719ab9c6e8c239e0dd01fc87e6f9
1//===-- llvm/CodeGen/FormattedStream.h - 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 raw_ostream implementations for streams to do
11// things like pretty-print comments.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_ASMSTREAM_H
16#define LLVM_CODEGEN_ASMSTREAM_H
17
18#include "llvm/Support/raw_ostream.h"
19
20namespace llvm
21{
22  /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle
23  /// asm-specific constructs
24  ///
25  class formatted_raw_ostream : public raw_ostream {
26  private:
27    /// TheStream - The real stream we output to
28    ///
29    raw_ostream &TheStream;
30
31    /// Column - The current output column of the stream
32    ///
33    unsigned Column;
34
35    virtual void write_impl(const char *Ptr, unsigned Size) {
36      ComputeColumn(Ptr, Size);
37      TheStream.write(Ptr, Size);
38    }
39
40    /// current_pos - Return the current position within the stream,
41    /// not counting the bytes currently in the buffer.
42    virtual uint64_t current_pos() {
43      // This has the same effect as calling TheStream.current_pos(),
44      // but that interface is private.
45      return TheStream.tell() - TheStream.GetNumBytesInBuffer();
46    }
47
48    /// ComputeColumn - Examine the current output and figure out
49    /// which column we end up in after output.
50    ///
51    void ComputeColumn(const char *Ptr, unsigned Size);
52
53  public:
54    /// formatted_raw_ostream - Open the specified file for
55    /// writing. If an error occurs, information about the error is
56    /// put into ErrorInfo, and the stream should be immediately
57    /// destroyed; the string will be empty if no error occurred.
58    ///
59    /// \param Filename - The file to open. If this is "-" then the
60    /// stream will use stdout instead.
61    /// \param Binary - The file should be opened in binary mode on
62    /// platforms that support this distinction.
63    formatted_raw_ostream(raw_ostream &Stream)
64        : raw_ostream(), TheStream(Stream), Column(0) {}
65
66    /// PadToColumn - Align the output to some column number
67    ///
68    /// \param NewCol - The column to move to
69    /// \param MinPad - The minimum space to give after the most
70    /// recent I/O, even if the current column + minpad > newcol
71    ///
72    void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
73  };
74
75  /// Column - An I/O manipulator to advance the output to a certain column
76  ///
77  class Column {
78  private:
79    /// Col - The column to move to
80    ///
81    unsigned int Col;
82
83  public:
84    explicit Column(unsigned int c)
85        : Col(c) {}
86
87    /// operator() - Make Column a functor invokable by a stream
88    /// output operator.
89    ///
90    formatted_raw_ostream &operator()(formatted_raw_ostream &Out) const {
91      // Make at least one space before the next output
92      Out.PadToColumn(Col, 1);
93      return(Out);
94    }
95  };
96
97  /// operator<< - Support coulmn-setting in formatted streams.
98  ///
99  inline formatted_raw_ostream &operator<<(formatted_raw_ostream &Out,
100                                           const Column &Func)
101  {
102    return(Func(Out));
103  }
104}
105
106#endif
107