Skip to content

Commit fac56d9

Browse files
authored
Merge pull request #70 from diffblue/verilog_preprocessor_error
Verilog: add a class for exceptions in the preprocessor
2 parents 811979e + b581a03 commit fac56d9

File tree

2 files changed

+84
-31
lines changed

2 files changed

+84
-31
lines changed

src/verilog/verilog_preprocessor.cpp

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9-
#include <fstream>
9+
#include "verilog_preprocessor.h"
1010

1111
#include <util/config.h>
1212
#include <util/file_util.h>
1313

14-
#include "verilog_preprocessor.h"
14+
#include "verilog_preprocessor_error.h"
15+
16+
#include <fstream>
1517

1618
/*******************************************************************\
1719
@@ -221,44 +223,55 @@ Function: verilog_preprocessort::preprocessor
221223

222224
void verilog_preprocessort::preprocessor()
223225
{
224-
files.emplace_back(false, &in, filename);
225-
226-
while(!files.empty())
226+
try
227227
{
228-
files.back().print_line(out, files.size()==1?0:2);
228+
files.emplace_back(false, &in, filename);
229229

230-
char ch, last_out=0;
231-
232-
while(files.back().get(ch))
230+
while(!files.empty())
233231
{
234-
switch(ch)
235-
{
236-
case '`':
237-
directive();
238-
break;
232+
files.back().print_line(out, files.size() == 1 ? 0 : 2);
239233

240-
default:
241-
if(condition)
234+
char ch, last_out = 0;
235+
236+
while(files.back().get(ch))
237+
{
238+
switch(ch)
242239
{
243-
filet &file=files.back();
240+
case '`':
241+
directive();
242+
break;
244243

245-
if(last_out=='\n' && file.last_line!=file.line &&
246-
ch!='\n')
244+
default:
245+
if(condition)
247246
{
248-
file.print_line(out, 0);
249-
file.last_line=file.line;
250-
}
247+
filet &file = files.back();
248+
249+
if(last_out == '\n' && file.last_line != file.line && ch != '\n')
250+
{
251+
file.print_line(out, 0);
252+
file.last_line = file.line;
253+
}
251254

252-
out << ch;
253-
last_out=ch;
255+
out << ch;
256+
last_out = ch;
254257

255-
if(ch=='\n') file.last_line++;
258+
if(ch == '\n')
259+
file.last_line++;
260+
}
256261
}
257262
}
258-
}
259263

260-
if(last_out!='\n') out << '\n';
261-
files.pop_back();
264+
if(last_out != '\n')
265+
out << '\n';
266+
files.pop_back();
267+
}
268+
}
269+
catch(const verilog_preprocessor_errort &e)
270+
{
271+
if(!files.empty())
272+
error().source_location = files.back().make_source_location();
273+
error() << e.what() << eom;
274+
throw 0;
262275
}
263276
}
264277

@@ -567,9 +580,8 @@ void verilog_preprocessort::directive()
567580

568581
if(it==defines.end())
569582
{
570-
error().source_location = source_location;
571-
error() << "unknown preprocessor directive \"" << text << "\"" << eom;
572-
throw 0;
583+
throw verilog_preprocessor_errort()
584+
<< "unknown preprocessor directive \"" << text << "\"";
573585
}
574586

575587
// found it! replace it!
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*******************************************************************\
2+
3+
Module: Verilog Preprocessor Error Class
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef VERILOG_PREPROCESSOR_ERROR_H
10+
#define VERILOG_PREPROCESSOR_ERROR_H
11+
12+
#include <sstream>
13+
#include <string>
14+
15+
class verilog_preprocessor_errort
16+
{
17+
public:
18+
std::string what() const
19+
{
20+
return message.str();
21+
}
22+
23+
std::ostringstream &message_ostream()
24+
{
25+
return message;
26+
}
27+
28+
protected:
29+
std::ostringstream message;
30+
};
31+
32+
/// add to the diagnostic information in the given verilog_preprocessor_error exception
33+
template <typename T>
34+
verilog_preprocessor_errort
35+
operator<<(verilog_preprocessor_errort &&e, const T &message)
36+
{
37+
e.message_ostream() << message;
38+
return std::move(e);
39+
}
40+
41+
#endif // VERILOG_PREPROCESSOR_ERROR_H

0 commit comments

Comments
 (0)