-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpp_output_handler.cpp
250 lines (229 loc) · 5.04 KB
/
cpp_output_handler.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* ap_cpp_output_handler.cpp -
* @author: Jonathan Beard
* @version: Sun Jul 7 18:24:28 2013
*/
#include <cassert>
#include <algorithm>
#include "data.hpp"
#include "signalhooks.hpp"
#include "cpp_output_handler.hpp"
using namespace Raft;
CPP_OutputHandler::CPP_OutputHandler( Data &d) :
comment( false ),
data( d )
{
/* nothing really to do */
}
CPP_OutputHandler::~CPP_OutputHandler()
{
for( File *f: cpp_access_queue )
{
/* call destructor */
delete( f );
}
}
void
CPP_OutputHandler::AddUpdate( int64_t lineno,
std::string name,
int8_t flag )
{
/* handle all cases here */
/**
* flag == 1, start of new file
* flag == 2, return to previous file
* flag == 3, this is a system header file
* flag == 4, text should be wrapped in explicit extern "C"
*/
enum flags { NEW = 1, RETURN, SYSTEM, EXTERN };
switch( flag )
{
case( NEW ):
{
add_file_object( new File( lineno, name ) );
}
break;
case( RETURN ):
{
remove_head();
assert( has_head() == true );
File &f( get_head() );
f.lineno = (--lineno);
}
break;
case( SYSTEM ):
{
/* unimplemented, I don't think we'll need this one */
}
break;
case( EXTERN ):
{
/* unimplemented, I don't think we'll need this one */
}
break;
default:
{
data.get_errorstream() << "Invalid input detected within "
<< "the output from the C Pre-Processor, suggest you re-"
<< "run with -dump_cpp_output true to debug." << std::endl;
raise( TERM_ERR_SIG );
}
}
}
void
CPP_OutputHandler::AddUpdate( int64_t lineno,
std::string name )
{
/**
* We want to ignore the following:
* name eq "<built-in>"
* name eq "<command-line>"
*/
const int8_t equal(0);
const std::string builtin( "<built-in>" );
const std::string commandline( "<command-line>" );
if( name.compare( builtin ) == equal )
{
/* ignore this guy */
}
else if( name.compare( commandline ) == equal )
{
/* ignore this one too */
}
else
{
/* this is the one we actually need to care about */
/**
* two cases to be handled, the first is if its a new file,
* add it to the stack, the second is if we're looking at
* an update to the line number via CPP, then we need to check
* the name of head
*/
if( /* no */! has_head() )
{
/* simply insert a new File object */
add_file_object( new File( lineno, name ) );
}
else
{
File &f( get_head() );
/* if CPP does its job correctly this will always be true */
assert( f.SameFile( name ) == true );
/* update line no */
f.lineno = lineno;
}
}
}
void
CPP_OutputHandler::IncrementHead()
{
/* if no head, do nothing */
if( has_head() == true )
{
File &f( get_head() );
f.lineno++;
}
}
bool
CPP_OutputHandler::IsHeadIncludedFile()
{
return( has_below_head() );
}
std::string
CPP_OutputHandler::PeekBelowHead()
{
assert( has_below_head() == true );
std::stringstream ss;
File &f( get_below_head() );
f.print( ss );
return( ss.str() );
}
std::string
CPP_OutputHandler::PeekHead()
{
if( has_head() == true )
{
std::stringstream ss;
File &f( get_head() );
f.print( ss );
return( ss.str() );
}
else
{
return( "" );
}
}
std::string
CPP_OutputHandler::GetHeadCurrentLine()
{
if( has_head() == true )
{
std::string output;
File &f( get_head() );
output = f.GetCurrentLine( data );
return(output);
}
else
{
return( "Error, no current file!!" );
}
}
bool
CPP_OutputHandler::IsComment()
{
return( comment );
}
void
CPP_OutputHandler::StartComment()
{
comment = true;
}
void
CPP_OutputHandler::EndComment()
{
comment = false;
}
/**
* below this line are accessor methods for the queue,
* makes it a bit easier to swap data structures in the
* future
*/
void
CPP_OutputHandler::add_file_object( File *f )
{
assert( f != nullptr );
cpp_access_queue.push_back( f );
}
bool
CPP_OutputHandler::has_head()
{
return( cpp_access_queue.size() > 0 );
}
File&
CPP_OutputHandler::get_head()
{
assert( has_head() == true );
return( *cpp_access_queue.back() );
}
bool
CPP_OutputHandler::has_below_head()
{
return( cpp_access_queue.size() > 1 );
}
File&
CPP_OutputHandler::get_below_head()
{
assert( has_below_head() == true );
File *below_head( nullptr );
below_head = *(cpp_access_queue.end() - 2);
assert( below_head != nullptr );
return( *below_head );
}
void
CPP_OutputHandler::remove_head()
{
assert( has_head() == true );
File *end( cpp_access_queue.back() );
delete( end );
cpp_access_queue.erase( --cpp_access_queue.end() );
}