Store Location::lineno as size_t

By storing it as size_t we explicitly disallow negative line numbers,
and keep 0 as an error/empty/undefined location.

A small problem with this is that MessageContainer.error and friends
still accept negative numbers (often without warning) via size_t
implicit conversion and wrap-around.
This commit is contained in:
Jørgen Kvalsvik
2016-04-06 11:41:22 +02:00
parent 3efa5fb90c
commit 0d24ac2d4a
3 changed files with 23 additions and 16 deletions

View File

@@ -24,10 +24,17 @@
namespace Opm {
Location::Location( const std::string& fn, size_t ln ) :
filename( fn ), lineno( ln )
{
if( ln == 0 )
throw std::invalid_argument( "Invalid line number 0 for file '"
+ fn + "'" );
}
void MessageContainer::error( const std::string& msg,
const std::string& filename,
const int lineno ) {
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Error, msg, Location { filename, lineno } }
);
@@ -41,7 +48,7 @@ namespace Opm {
void MessageContainer::bug( const std::string& msg,
const std::string& filename,
const int lineno ) {
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Bug, msg, { filename, lineno } }
);
@@ -55,7 +62,7 @@ namespace Opm {
void MessageContainer::warning( const std::string& msg,
const std::string& filename,
const int lineno ) {
const size_t lineno ) {
m_messages.emplace_back( Message {
Message::Warning, msg, { filename, lineno }
}
@@ -73,7 +80,7 @@ namespace Opm {
void MessageContainer::info( const std::string& msg,
const std::string& filename,
const int lineno ) {
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Info, msg, { filename, lineno } }
);
@@ -87,7 +94,7 @@ namespace Opm {
void MessageContainer::debug( const std::string& msg,
const std::string& filename,
const int lineno ) {
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Debug, msg, { filename, lineno } }
);
@@ -101,7 +108,7 @@ namespace Opm {
void MessageContainer::problem( const std::string& msg,
const std::string& filename,
const int lineno ) {
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Problem, msg, { filename, lineno } }
);

View File

@@ -28,14 +28,13 @@ namespace Opm {
struct Location {
Location() = default;
Location( const std::string& fn, int ln ) :
filename( fn ), lineno( ln ) {}
Location( const std::string&, size_t );
std::string filename;
int lineno = -1;
size_t lineno = 0;
explicit operator bool() const {
return lineno > 0;
return lineno != 0;
}
};
@@ -68,22 +67,22 @@ namespace Opm {
using const_iterator = std::vector< Message >::const_iterator;
void error(const std::string& msg, const std::string& filename, const int lineno);
void error(const std::string& msg, const std::string& filename, const size_t lineno);
void error(const std::string& msg);
void bug(const std::string& msg, const std::string& filename, const int lineno);
void bug(const std::string& msg, const std::string& filename, const size_t lineno);
void bug(const std::string& msg);
void warning(const std::string& msg, const std::string& filename, const int lineno);
void warning(const std::string& msg, const std::string& filename, const size_t lineno);
void warning(const std::string& msg);
void info(const std::string& msg, const std::string& filename, const int lineno);
void info(const std::string& msg, const std::string& filename, const size_t lineno);
void info(const std::string& msg);
void debug(const std::string& msg, const std::string& filename, const int lineno);
void debug(const std::string& msg, const std::string& filename, const size_t lineno);
void debug(const std::string& msg);
void problem(const std::string& msg, const std::string& filename, const int lineno);
void problem(const std::string& msg, const std::string& filename, const size_t lineno);
void problem(const std::string& msg);
void add( const Message& );

View File

@@ -60,4 +60,5 @@ BOOST_AUTO_TEST_CASE(LocationImplicitConversion) {
BOOST_CHECK( !mc.begin()->location );
BOOST_CHECK( (mc.begin() + 1)->location );
BOOST_CHECK_THROW( mc.info( "msg", "filename", 0 ), std::invalid_argument );
}