source: src/CodeGen/LineStream.cc @ f57668a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f57668a was f57668a, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Foundation for adding line numbers to generated output.

  • Property mode set to 100644
File size: 2.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// LineStream.cc -- Modified stream that inserts line directives into output.
8//
9// Author           : Andrew Beach
10// Created On       : Thr May 4 13:15:00 2017
11// Last Modified By : Andrew Beach
12// Last Modified On : Fri May 5 14:29:00 2017
13// Update Count     : 0
14//
15
16#include "LineStream.h"
17
18namespace CodeGen {
19
20        LineStream::LineStream(std::ostream & baseStream, bool insertLines) :
21                baseStream(baseStream), insertLines(insertLines)
22        {}
23
24        void LineStream::printLineDirective(CodeLocation const & location) {
25                baseStream << "\n# " << location.linenumber;
26                if (std::string("") != location.filename) {
27                        baseStream << " \"" << location.filename  << '"';
28                }
29                baseStream << '\n';
30        }
31
32        bool LineStream::actualDiffersFromExpected() const {
33                return actualLocation.isSet() &&
34                        // actualLocation & expectedLocation must match at the line.
35                        (actualLocation.linenumber != expectedLocation.linenumber ||
36                         actualLocation.filename != expectedLocation.filename);
37        }
38
39        void LineStream::emptyBuffer(bool addNewline) {
40                if (actualDiffersFromExpected()) {
41                        printLineDirective(actualLocation);
42                        expectedLocation = actualLocation;
43                }
44                actualLocation.unset();
45
46                if (addNewLine) {
47                        expectedLocation.linenumber += 1;
48                        buffer.put('\n');
49                }
50
51                baseStream << buffer.str() << std::flush;
52                buffer.str("");
53        }
54
55        void LineStream::setLoc(CodeLocation const & location) {
56                if (insertLines) {
57                        if (expectedLocation.isUnset()) {
58                                expectedLocation = actualLocation = location;
59                        } else if (actualLocation.isUnset()) {
60                                actualLocation = location;
61                        } else if (actualLocation.filename != location.filename) {
62                                emptyBuffer(true);
63                                actualLocation = location;
64                        } else if (location.linenumber <= actualLocation.linenumber){
65                                actualLocation.linenumber = location.linenumber;
66                        }
67                }
68        }
69
70        LineStream & LineStream::operator<<(char const * str) {
71                buffer << str;
72                return *this;
73        }
74
75        LineStream & LineStream::operator<<(std::string const & str) {
76                buffer << str;
77                return *this;
78        }
79
80        LineStream & LineStream::operator<<(StreamFlag flag) {
81                static StringFlag const endlCopy = std::endl;
82                if (!insertLines) {
83                        baseStream << flag;
84                } else if (endlCopy == flag) {
85                        emptyBuffer(true);
86                } else {
87                        buffer << flag;
88                }
89                return *this;
90        }
91
92        LineStream & LineStream::flush() {
93                if (insertLines) {
94                        emptyBuffer(false);
95                } else {
96                        baseStream.flush();
97                }
98                return *this;
99        }
100
101} // CodeGen
Note: See TracBrowser for help on using the repository browser.