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 : Mon May 8 12:08:00 2017
|
---|
13 | // Update Count : 0
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "LineStream.h"
|
---|
17 |
|
---|
18 | namespace CodeGen {
|
---|
19 |
|
---|
20 | void LineStream::printLineDirective(CodeLocation const & location) {
|
---|
21 | baseStream << "\n# " << location.linenumber;
|
---|
22 | if (std::string("") != location.filename) {
|
---|
23 | baseStream << " \"" << location.filename << '"';
|
---|
24 | }
|
---|
25 | baseStream << '\n';
|
---|
26 | }
|
---|
27 |
|
---|
28 | bool LineStream::actualDiffersFromExpected() const {
|
---|
29 | return actualLocation.isSet() &&
|
---|
30 | // actualLocation & expectedLocation must match at the line.
|
---|
31 | (actualLocation.linenumber != expectedLocation.linenumber ||
|
---|
32 | actualLocation.filename != expectedLocation.filename);
|
---|
33 | }
|
---|
34 |
|
---|
35 | void LineStream::emptyBuffer(bool addNewline) {
|
---|
36 | if (actualDiffersFromExpected()) {
|
---|
37 | printLineDirective(actualLocation);
|
---|
38 | expectedLocation = actualLocation;
|
---|
39 | }
|
---|
40 | actualLocation.unset();
|
---|
41 |
|
---|
42 | if (addNewline) {
|
---|
43 | expectedLocation.linenumber += 1;
|
---|
44 | buffer.put('\n');
|
---|
45 | }
|
---|
46 |
|
---|
47 | baseStream << buffer.str() << std::flush;
|
---|
48 | buffer.str("");
|
---|
49 | }
|
---|
50 |
|
---|
51 | void LineStream::setLoc(CodeLocation const & location) {
|
---|
52 | if (insertLines) {
|
---|
53 | if (expectedLocation.isUnset()) {
|
---|
54 | expectedLocation = actualLocation = location;
|
---|
55 | } else if (actualLocation.isUnset()) {
|
---|
56 | actualLocation = location;
|
---|
57 | } else if (actualLocation.filename != location.filename) {
|
---|
58 | emptyBuffer(true);
|
---|
59 | actualLocation = location;
|
---|
60 | } else if (location.linenumber <= actualLocation.linenumber){
|
---|
61 | actualLocation.linenumber = location.linenumber;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | LineStream & LineStream::operator<<(char const * str) {
|
---|
67 | buffer << str;
|
---|
68 | return *this;
|
---|
69 | }
|
---|
70 |
|
---|
71 | LineStream & LineStream::operator<<(std::string const & str) {
|
---|
72 | buffer << str;
|
---|
73 | return *this;
|
---|
74 | }
|
---|
75 |
|
---|
76 | LineStream & LineStream::operator<<(StreamFlag flag) {
|
---|
77 | static StreamFlag const endlCopy = std::endl;
|
---|
78 | if (!insertLines) {
|
---|
79 | baseStream << flag;
|
---|
80 | } else if (endlCopy == flag) {
|
---|
81 | emptyBuffer(true);
|
---|
82 | } else {
|
---|
83 | buffer << flag;
|
---|
84 | }
|
---|
85 | return *this;
|
---|
86 | }
|
---|
87 |
|
---|
88 | LineStream & LineStream::flush() {
|
---|
89 | if (insertLines) {
|
---|
90 | emptyBuffer(false);
|
---|
91 | } else {
|
---|
92 | baseStream.flush();
|
---|
93 | }
|
---|
94 | return *this;
|
---|
95 | }
|
---|
96 |
|
---|
97 | } // CodeGen
|
---|