Fix a bug summing two full sets. The overflow checking doesn't handle sets as
[oota-llvm.git] / lib / Support / FormattedStream.cpp
1 //===-- llvm/CodeGen/AsmStream.cpp - AsmStream Framework --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains instantiations of "standard" AsmOStreams.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/FormattedStream.h"
15
16 namespace llvm {
17   /// ComputeColumn - Examine the current output and figure out which
18   /// column we end up in after output.
19   ///
20   void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size)
21   {
22     // Keep track of the current column by scanning the string for
23     // special characters
24
25     // Find the last newline.  This is our column start.  If there
26     // is no newline, start with the current column.
27     const char *nlpos = NULL;
28     for (const char *pos = Ptr + Size, *epos = Ptr; pos > epos; --pos) {
29       if (*(pos-1) == '\n') {
30         nlpos = pos-1;
31         // The newline will be counted, setting this to zero.  We
32         // need to do it this way in case nlpos is Ptr.
33         Column = -1;
34         break;
35       }
36     }
37
38     if (nlpos == NULL) {
39       nlpos = Ptr;
40     }
41
42     // Walk through looking for tabs and advance column as appropriate
43     for (const char *pos = nlpos, *epos = Ptr + Size; pos != epos; ++pos) {
44       ++Column;
45       if (*pos == '\t') {
46         // Advance to next tab stop (every eight characters)
47         Column += ((8 - (Column & 0x7)) & 0x7);
48         assert(!(Column & 0x3) && "Column out of alignment");
49       }
50     }
51   }
52
53   /// PadToColumn - Align the output to some column number
54   ///
55   /// \param NewCol - The column to move to
56   /// \param MinPad - The minimum space to give after the most recent
57   /// I/O, even if the current column + minpad > newcol
58   ///
59   void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) 
60   {
61     flush();
62
63     // Output spaces until we reach the desired column
64     unsigned num = NewCol - Column;
65     if (NewCol < Column || num < MinPad) {
66       num = MinPad;
67     }
68
69     // TODO: Write a whole string at a time
70     while (num-- > 0) {
71       write(' ');
72     }
73   }
74 }