Skip to content

Commit b581a03

Browse files
committed
Verilog: add a class for exceptions in the preprocessor
This adds a class for exceptions in the Verilog preprocessor, as opposed to throwing the integer 0.
1 parent b01409e commit b581a03

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,11 +6,13 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

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

1111
#include <util/config.h>
1212

13-
#include "verilog_preprocessor.h"
13+
#include "verilog_preprocessor_error.h"
14+
15+
#include <fstream>
1416

1517
/*******************************************************************\
1618
@@ -245,44 +247,55 @@ Function: verilog_preprocessort::preprocessor
245247

246248
void verilog_preprocessort::preprocessor()
247249
{
248-
files.emplace_back(false, &in, filename);
249-
250-
while(!files.empty())
250+
try
251251
{
252-
files.back().print_line(out, files.size()==1?0:2);
252+
files.emplace_back(false, &in, filename);
253253

254-
char ch, last_out=0;
255-
256-
while(files.back().get(ch))
254+
while(!files.empty())
257255
{
258-
switch(ch)
259-
{
260-
case '`':
261-
directive();
262-
break;
256+
files.back().print_line(out, files.size() == 1 ? 0 : 2);
263257

264-
default:
265-
if(condition)
258+
char ch, last_out = 0;
259+
260+
while(files.back().get(ch))
261+
{
262+
switch(ch)
266263
{
267-
filet &file=files.back();
264+
case '`':
265+
directive();
266+
break;
268267

269-
if(last_out=='\n' && file.last_line!=file.line &&
270-
ch!='\n')
268+
default:
269+
if(condition)
271270
{
272-
file.print_line(out, 0);
273-
file.last_line=file.line;
274-
}
271+
filet &file = files.back();
272+
273+
if(last_out == '\n' && file.last_line != file.line && ch != '\n')
274+
{
275+
file.print_line(out, 0);
276+
file.last_line = file.line;
277+
}
275278

276-
out << ch;
277-
last_out=ch;
279+
out << ch;
280+
last_out = ch;
278281

279-
if(ch=='\n') file.last_line++;
282+
if(ch == '\n')
283+
file.last_line++;
284+
}
280285
}
281286
}
282-
}
283287

284-
if(last_out!='\n') out << '\n';
285-
files.pop_back();
288+
if(last_out != '\n')
289+
out << '\n';
290+
files.pop_back();
291+
}
292+
}
293+
catch(const verilog_preprocessor_errort &e)
294+
{
295+
if(!files.empty())
296+
error().source_location = files.back().make_source_location();
297+
error() << e.what() << eom;
298+
throw 0;
286299
}
287300
}
288301

@@ -591,9 +604,8 @@ void verilog_preprocessort::directive()
591604

592605
if(it==defines.end())
593606
{
594-
error().source_location = source_location;
595-
error() << "unknown preprocessor directive \"" << text << "\"" << eom;
596-
throw 0;
607+
throw verilog_preprocessor_errort()
608+
<< "unknown preprocessor directive \"" << text << "\"";
597609
}
598610

599611
// 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)