Skip to content

Commit 8a8afdb

Browse files
committed
introduce an exception class for typechecking
This adds typecheckt::errort(), which is a class that designed to be thrown as exception. It offers streaming via << and a 'fluent API'-style .with_location() method to set the source location.
1 parent aed4d77 commit 8a8afdb

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/util/typecheck.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,16 @@ bool typecheckt::typecheck_main()
4444
error() << e.get_reason() << messaget::eom;
4545
}
4646

47+
catch(const errort &e)
48+
{
49+
if(e.what().empty())
50+
error();
51+
else
52+
{
53+
error().source_location = e.source_location();
54+
error() << e.what() << messaget::eom;
55+
}
56+
}
57+
4758
return message_handler->get_message_count(messaget::M_ERROR)!=errors_before;
4859
}

src/util/typecheck.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ class typecheckt:public messaget
2222

2323
virtual ~typecheckt() { }
2424

25+
class errort final
26+
{
27+
public:
28+
std::string what() const
29+
{
30+
return message.str();
31+
}
32+
33+
std::ostringstream &message_ostream()
34+
{
35+
return message;
36+
}
37+
38+
errort with_location(source_locationt _location) &&
39+
{
40+
__location = std::move(_location);
41+
return std::move(*this);
42+
}
43+
44+
const source_locationt &source_location() const
45+
{
46+
return __location;
47+
}
48+
49+
protected:
50+
std::ostringstream message;
51+
source_locationt __location = source_locationt::nil();
52+
53+
template <typename T>
54+
friend errort operator<<(errort &&e, const T &);
55+
};
56+
2557
protected:
2658
// main function -- overload this one
2759
virtual void typecheck()=0;
@@ -31,4 +63,12 @@ class typecheckt:public messaget
3163
virtual bool typecheck_main();
3264
};
3365

66+
/// add to the diagnostic information in the given typecheckt::errort exception
67+
template <typename T>
68+
typecheckt::errort operator<<(typecheckt::errort &&e, const T &message)
69+
{
70+
e.message_ostream() << message;
71+
return std::move(e);
72+
}
73+
3474
#endif // CPROVER_UTIL_TYPECHECK_H

0 commit comments

Comments
 (0)