From 7e5c360a8c8eee8ebe8b1c18e49be5b4af8dac57 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 18 Sep 2021 14:25:28 +0100 Subject: Add JT recommended warnings --- libsqlitepp/Jamfile.jam | 15 ++++++++++++ libsqlitepp/sqlite-command.cpp | 46 ++++++++++++++++-------------------- libsqlitepp/sqlite-command.h | 3 +++ libsqlitepp/sqlite-connection.cpp | 2 +- libsqlitepp/sqlite-mock.cpp | 2 +- libsqlitepp/sqlite-modifycommand.cpp | 2 +- libsqlitepp/sqlite-selectcommand.cpp | 14 +++++------ libsqlitepp/unittests/testsqlite.cpp | 4 ++-- 8 files changed, 50 insertions(+), 38 deletions(-) diff --git a/libsqlitepp/Jamfile.jam b/libsqlitepp/Jamfile.jam index 84e8c85..951b648 100644 --- a/libsqlitepp/Jamfile.jam +++ b/libsqlitepp/Jamfile.jam @@ -17,6 +17,21 @@ project release:on debug:extra debug:on + debug:-Wnon-virtual-dtor + debug:-Wold-style-cast + debug:-Wcast-align + debug:-Wunused + debug:-Woverloaded-virtual + debug:-Wpedantic + debug:-Wconversion + debug:-Wsign-conversion + debug:-Wnull-dereference + debug:-Wdouble-promotion + debug:-Wformat=2 + gcc,debug:-Wduplicated-cond + gcc,debug:-Wduplicated-branches + gcc,debug:-Wlogical-op + gcc,debug:-Wuseless-cast coverage:on ; diff --git a/libsqlitepp/sqlite-command.cpp b/libsqlitepp/sqlite-command.cpp index 124b5d0..694c5d2 100644 --- a/libsqlitepp/sqlite-command.cpp +++ b/libsqlitepp/sqlite-command.cpp @@ -5,7 +5,7 @@ SQLite::Command::Command(const Connection * conn, const std::string & sql) : DB::Command(sql), c(conn), stmt(nullptr) { - if (sqlite3_prepare_v2(conn->db, sql.c_str(), sql.length(), &stmt, nullptr) != SQLITE_OK) { + if (sqlite3_prepare_v2(conn->db, sql.c_str(), static_cast(sql.length()), &stmt, nullptr) != SQLITE_OK) { throw Error(conn->db); } } @@ -15,61 +15,54 @@ SQLite::Command::~Command() sqlite3_finalize(stmt); } +template void -SQLite::Command::bindParamI(unsigned int n, int v) +SQLite::Command::bindWith(unsigned int n, const V & v, BinderFunction binder) const { - if (sqlite3_bind_int(stmt, (int)n + 1, v) != SQLITE_OK) { + if (binder(stmt, static_cast(n + 1), v) != SQLITE_OK) { throw Error(c->db); } } + +void +SQLite::Command::bindParamI(unsigned int n, int v) +{ + bindWith(n, v, sqlite3_bind_int); +} void SQLite::Command::bindParamI(unsigned int n, long int v) { - if (sqlite3_bind_int64(stmt, (int)n + 1, v) != SQLITE_OK) { - throw Error(c->db); - } + bindWith(n, v, sqlite3_bind_int64); } void SQLite::Command::bindParamI(unsigned int n, long long int v) { - if (sqlite3_bind_int64(stmt, (int)n + 1, v) != SQLITE_OK) { - throw Error(c->db); - } + bindWith(n, v, sqlite3_bind_int64); } void SQLite::Command::bindParamI(unsigned int n, unsigned int v) { - if (sqlite3_bind_int64(stmt, (int)n + 1, v) != SQLITE_OK) { - throw Error(c->db); - } + bindWith(n, v, sqlite3_bind_int64); } void SQLite::Command::bindParamI(unsigned int n, long unsigned int v) { - if (sqlite3_bind_int64(stmt, (int)n + 1, v) != SQLITE_OK) { - throw Error(c->db); - } + bindWith(n, static_cast(v), sqlite3_bind_int64); } void SQLite::Command::bindParamI(unsigned int n, long long unsigned int v) { - if (sqlite3_bind_int64(stmt, (int)n + 1, v) != SQLITE_OK) { - throw Error(c->db); - } + bindWith(n, static_cast(v), sqlite3_bind_int64); } void SQLite::Command::bindParamF(unsigned int n, double v) { - if (sqlite3_bind_double(stmt, (int)n + 1, v) != SQLITE_OK) { - throw Error(c->db); - } + bindWith(n, v, sqlite3_bind_double); } void SQLite::Command::bindParamF(unsigned int n, float v) { - if (sqlite3_bind_double(stmt, (int)n + 1, v) != SQLITE_OK) { - throw Error(c->db); - } + bindWith(n, static_cast(v), sqlite3_bind_double); } void SQLite::Command::bindParamS(unsigned int n, const Glib::ustring & s) @@ -79,7 +72,8 @@ SQLite::Command::bindParamS(unsigned int n, const Glib::ustring & s) void SQLite::Command::bindParamS(unsigned int n, const std::string_view & s) { - if (sqlite3_bind_text(stmt, (int)n + 1, s.data(), s.length(), SQLITE_STATIC) != SQLITE_OK) { + if (sqlite3_bind_text(stmt, static_cast(n + 1), s.data(), static_cast(s.length()), SQLITE_STATIC) + != SQLITE_OK) { throw Error(c->db); } } @@ -101,7 +95,7 @@ SQLite::Command::bindParamT(unsigned int, const boost::posix_time::ptime &) void SQLite::Command::bindNull(unsigned int n) { - if (sqlite3_bind_null(stmt, (int)n + 1) != SQLITE_OK) { + if (sqlite3_bind_null(stmt, static_cast(n + 1)) != SQLITE_OK) { throw Error(c->db); } } diff --git a/libsqlitepp/sqlite-command.h b/libsqlitepp/sqlite-command.h index abf02ee..1bf93c4 100644 --- a/libsqlitepp/sqlite-command.h +++ b/libsqlitepp/sqlite-command.h @@ -32,6 +32,9 @@ namespace SQLite { void bindNull(unsigned int) override; protected: + template + void bindWith(unsigned int n, const V & v, BinderFunction binder) const; + const Connection * c; sqlite3_stmt * stmt; }; diff --git a/libsqlitepp/sqlite-connection.cpp b/libsqlitepp/sqlite-connection.cpp index 99006dc..e1a6de7 100644 --- a/libsqlitepp/sqlite-connection.cpp +++ b/libsqlitepp/sqlite-connection.cpp @@ -3,7 +3,7 @@ #include "sqlite-modifycommand.h" #include "sqlite-selectcommand.h" -NAMEDFACTORY("sqlite", SQLite::Connection, DB::ConnectionFactory); +NAMEDFACTORY("sqlite", SQLite::Connection, DB::ConnectionFactory) SQLite::ConnectionError::ConnectionError(sqlite3 * db) : SQLite::Error(db) { } diff --git a/libsqlitepp/sqlite-mock.cpp b/libsqlitepp/sqlite-mock.cpp index 4772bcc..11f4f90 100644 --- a/libsqlitepp/sqlite-mock.cpp +++ b/libsqlitepp/sqlite-mock.cpp @@ -2,7 +2,7 @@ #include "sqlite-connection.h" #include -NAMEDFACTORY("sqlite", SQLite::Mock, DB::MockDatabaseFactory); +NAMEDFACTORY("sqlite", SQLite::Mock, DB::MockDatabaseFactory) namespace SQLite { diff --git a/libsqlitepp/sqlite-modifycommand.cpp b/libsqlitepp/sqlite-modifycommand.cpp index 868f187..df859ca 100644 --- a/libsqlitepp/sqlite-modifycommand.cpp +++ b/libsqlitepp/sqlite-modifycommand.cpp @@ -15,7 +15,7 @@ SQLite::ModifyCommand::execute(bool anc) sqlite3_reset(stmt); throw Error(c->db); } - unsigned int rows = sqlite3_changes(c->db); + unsigned int rows = static_cast(sqlite3_changes(c->db)); sqlite3_reset(stmt); if (rows == 0 && !anc) { throw DB::NoRowsAffected(); diff --git a/libsqlitepp/sqlite-selectcommand.cpp b/libsqlitepp/sqlite-selectcommand.cpp index c4a1096..e360dcd 100644 --- a/libsqlitepp/sqlite-selectcommand.cpp +++ b/libsqlitepp/sqlite-selectcommand.cpp @@ -13,23 +13,23 @@ namespace SQLite { [[nodiscard]] bool isNull() const override { - return (SQLITE_NULL == sqlite3_column_type(stmt, (int)colNo)); + return (SQLITE_NULL == sqlite3_column_type(stmt, static_cast(colNo))); } void apply(DB::HandleField & h) const override { - switch (sqlite3_column_type(stmt, (int)colNo)) { + switch (sqlite3_column_type(stmt, static_cast(colNo))) { case SQLITE_INTEGER: - h.integer(sqlite3_column_int64(stmt, (int)colNo)); + h.integer(sqlite3_column_int64(stmt, static_cast(colNo))); return; case SQLITE_FLOAT: - h.floatingpoint(sqlite3_column_double(stmt, (int)colNo)); + h.floatingpoint(sqlite3_column_double(stmt, static_cast(colNo))); return; case SQLITE_TEXT: { - auto t = sqlite3_column_text(stmt, (int)colNo); - auto l = sqlite3_column_bytes(stmt, (int)colNo); - h.string({reinterpret_cast(t), (std::size_t)l}); + auto t = sqlite3_column_text(stmt, static_cast(colNo)); + auto l = sqlite3_column_bytes(stmt, static_cast(colNo)); + h.string({reinterpret_cast(t), static_cast(l)}); return; } case SQLITE_NULL: diff --git a/libsqlitepp/unittests/testsqlite.cpp b/libsqlitepp/unittests/testsqlite.cpp index 81127c8..37f17ce 100644 --- a/libsqlitepp/unittests/testsqlite.cpp +++ b/libsqlitepp/unittests/testsqlite.cpp @@ -16,7 +16,7 @@ public: BOOST_GLOBAL_FIXTURE(StandardMockDatabase); -BOOST_FIXTURE_TEST_SUITE(Core, DB::TestCore); +BOOST_FIXTURE_TEST_SUITE(Core, DB::TestCore) BOOST_AUTO_TEST_CASE(transactions) { @@ -63,4 +63,4 @@ BOOST_AUTO_TEST_CASE(bindAndSelect) BOOST_REQUIRE_EQUAL(1, rows); } -BOOST_AUTO_TEST_SUITE_END(); +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3