about summary refs log tree commit diff homepage
path: root/include/klee/util/PrintContext.h
blob: a72af03314eb0f549c5cd7a7d8d855936f123260 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===-- PrintContext.h ------------------------------------------*- C++ -*-===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef PRINTCONTEXT_H_
#define PRINTCONTEXT_H_

#include "klee/Expr.h"
#include "llvm/Support/raw_ostream.h"
#include <sstream>
#include <string>
#include <stack>

/// PrintContext - Helper class for pretty printing.
/// It provides a basic wrapper around llvm::raw_ostream that keeps track of
/// how many characters have been used on the current line.
///
/// It also provides an optional way keeping track of the various levels of indentation
/// by using a stack.
/// \sa breakLineI() , \sa pushIndent(), \sa popIndent()
class PrintContext {
private:
  llvm::raw_ostream &os;
  std::string newline;

  ///This is used to keep track of the stack of indentations used by
  /// \sa breakLineI()
  /// \sa pushIndent()
  /// \sa popIndent()
  std::stack<unsigned int> indentStack;

public:
  /// Number of characters on the current line.
  unsigned pos;

  PrintContext(llvm::raw_ostream &_os) : os(_os), newline("\n"), indentStack(), pos()
  {
	  indentStack.push(pos);
  }

  void setNewline(const std::string &_newline) {
    newline = _newline;
  }

  void breakLine(unsigned indent=0) {
    os << newline;
    if (indent)
      os.indent(indent) << ' ';
    pos = indent;
  }

  ///Break line using the indent on the top of the indent stack
  /// \return The PrintContext object so the method is chainable
  PrintContext& breakLineI()
  {
	  breakLine(indentStack.top());
	  return *this;
  }

  ///Add the current position on the line to the top of the indent stack
  /// \return The PrintContext object so the method is chainable
  PrintContext& pushIndent()
  {
	  indentStack.push(pos);
	  return *this;
  }

  ///Pop the top off the indent stack
  /// \return The PrintContext object so the method is chainable
  PrintContext& popIndent()
  {
	  indentStack.pop();
	  return *this;
  }

  /// write - Output a string to the stream and update the
  /// position. The stream should not have any newlines.
  void write(const std::string &s) {
    os << s;
    pos += s.length();
  }

  template <typename T>
  PrintContext &operator<<(T elt) {
    std::string str;
    llvm::raw_string_ostream ss(str);
    ss << elt;
    write(ss.str());
    return *this;
  }

};


#endif /* PRINTCONTEXT_H_ */