FormattedStream.cpp 3.4 KB

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