Skip to content

Commit

Permalink
Applying some clang-tidyness
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <[email protected]>
  • Loading branch information
christianparpart committed Oct 26, 2024
1 parent c0d82f5 commit 7ba0c41
Show file tree
Hide file tree
Showing 33 changed files with 210 additions and 261 deletions.
12 changes: 12 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@ CompileFlags:
Add:
- "-std=c++23"
- "-D__cpp_concepts=202002L"
Diagnostics:
ClangTidy:
FastCheckFilter: None

InlayHints:
Enabled: Yes
BlockEnd: No
Designators: Yes
ParameterNames: Yes
DeducedTypes: Yes
TypeNameLimit: 24

3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ jobs:
-DCMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}" \
-DCMAKE_INSTALL_PREFIX="/usr/local" \
-DPEDANTIC_COMPILER_WERROR=OFF \
-DLIGHTWEIGHT_BUILD_SHARED=ON \
--preset linux-gcc-release
- name: "build"
run: cmake --build --preset linux-gcc-release -- -j3
Expand Down Expand Up @@ -226,7 +227,7 @@ jobs:
- name: "Dump SQL connection string"
run: echo "ODBC_CONNECTION_STRING=${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"
- name: "Run SQL Core tests"
run: LightweightTest --trace-sql --trace-model -s # --odbc-connection-string="${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"
run: LightweightTest --trace-sql -s # --odbc-connection-string="${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"
env:
ODBC_CONNECTION_STRING: "${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"

Expand Down
27 changes: 8 additions & 19 deletions src/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ Checks: >-
-*,
bugprone-*,
-bugprone-branch-clone,
-bugprone-easily-swappable-parameters,
-bugprone-exception-escape,
-bugprone-implicit-widening-of-multiplication-result,
-bugprone-reserved-identifier,
-bugprone-suspicious-include,
-bugprone-unchecked-optional-access,
clang-analyzer-core.*,
clang-analyzer-cplusplus.*,
clang-analyzer-deadcode.*,
Expand All @@ -28,49 +26,40 @@ Checks: >-
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-no-malloc,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-prefer-member-initializer,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-const-cast,
-cppcoreguidelines-pro-type-cstyle-cast,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-static-cast-downcast,
-cppcoreguidelines-pro-type-union-access,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-special-member-functions,
modernize-*,
-modernize-avoid-bind,
-modernize-avoid-c-arrays,
-modernize-return-braced-init-list,
-modernize-use-bool-literals,
-modernize-use-trailing-return-type,
performance-*,
-performance-no-int-to-ptr,
readability-*,
-readability-avoid-unconditional-preprocessor-if,
-readability-braces-around-statements,
-readability-container-contains,
-readability-else-after-return,
-readability-function-cognitive-complexity,
-readability-identifier-length,
-readability-implicit-bool-conversion,
-readability-magic-numbers,
-readability-named-parameter,
-readability-redundant-access-specifiers,
-readability-redundant-member-init,
-readability-simplify-boolean-expr,
-readability-uppercase-literal-suffix,
-readability-use-anyofallof,
misc-const-correctness,
WarningsAsErrors: >-
modernize-use-nullptr,
performance-move-const-arg,
readability-function-cognitive-complexity,
cppcoreguidelines-rvalue-reference-param-not-moved,
UseColor: true
HeaderFilterRegex: 'src/'
FormatStyle: none
CheckOptions:
- key: readability-function-cognitive-complexity.Threshold
value: '16'
- key: readability-function-cognitive-complexity.IgnoreMacros
value: true
- key: bugprone-easily-swappable-parameters.MinimumLength
value: '3'
- key: cert-dcl16-c.NewSuffixes
Expand Down
2 changes: 1 addition & 1 deletion src/Lightweight/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ else()
target_compile_options(Lightweight PUBLIC ${ODBC_CFLAGS})
target_link_libraries(Lightweight PUBLIC ${ODBC_LDFLAGS})

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_link_libraries(Lightweight PUBLIC stdc++exp) # GCC >= 14
endif()
endif()
Expand Down
6 changes: 2 additions & 4 deletions src/Lightweight/DataBinder/BasicStringBinder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "Core.hpp"

#include <format>
#include <iostream>
#include <utility>

template <SqlCommonStringBinderConcept StringType>
Expand Down Expand Up @@ -126,8 +124,8 @@ struct LIGHTWEIGHT_API SqlDataBinder<StringType>
using ValueType = StringType;
using StringTraits = SqlCommonStringBinder<ValueType>;

constexpr static inline auto CType = SQL_C_WCHAR;
constexpr static inline auto SqlType = SQL_WVARCHAR;
static constexpr auto CType = SQL_C_WCHAR;
static constexpr auto SqlType = SQL_WVARCHAR;

static SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, ValueType const& value) noexcept
{
Expand Down
6 changes: 6 additions & 0 deletions src/Lightweight/DataBinder/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
class LIGHTWEIGHT_API SqlDataBinderCallback
{
public:
SqlDataBinderCallback() = default;
SqlDataBinderCallback(SqlDataBinderCallback&&) = default;
SqlDataBinderCallback(SqlDataBinderCallback const&) = default;
SqlDataBinderCallback& operator=(SqlDataBinderCallback&&) = default;
SqlDataBinderCallback& operator=(SqlDataBinderCallback const&) = default;

virtual ~SqlDataBinderCallback() = default;

virtual void PlanPostExecuteCallback(std::function<void()>&&) = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/Lightweight/DataBinder/Primitives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
template <typename T, SQLSMALLINT TheCType, SQLINTEGER TheSqlType, SqlColumnType TheColumnType>
struct LIGHTWEIGHT_API SqlSimpleDataBinder
{
static constexpr inline SqlColumnType ColumnType = TheColumnType;
static constexpr SqlColumnType ColumnType = TheColumnType;

static SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, T const& value) noexcept
{
return SQLBindParameter(stmt, column, SQL_PARAM_INPUT, TheCType, TheSqlType, 0, 0, (SQLPOINTER) &value, 0, nullptr);
}

static SQLRETURN OutputColumn(SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback&) noexcept
static SQLRETURN OutputColumn(SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback& /*unused*/) noexcept
{
return SQLBindCol(stmt, column, TheCType, result, 0, indicator);
}
Expand Down Expand Up @@ -49,6 +49,6 @@ template <typename T>
requires(std::is_same_v<SqlDataBinder<T>, SqlDataBinder<T>>) // Ensure that an SqlDataBinder<T> exists
struct SqlDataTraits<T>
{
static constexpr inline unsigned Size = 0;
static constexpr inline SqlColumnType Type = SqlDataBinder<T>::ColumnType;
static constexpr unsigned Size = 0;
static constexpr SqlColumnType Type = SqlDataBinder<T>::ColumnType;
};
32 changes: 16 additions & 16 deletions src/Lightweight/DataBinder/SqlFixedString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class LIGHTWEIGHT_API SqlFixedString
--_size;
}

constexpr std::basic_string_view<T> substr(
[[nodiscard]] constexpr std::basic_string_view<T> substr(
std::size_t offset = 0, std::size_t count = (std::numeric_limits<std::size_t>::max)()) const noexcept
{
if (offset >= _size)
Expand All @@ -136,19 +136,19 @@ class LIGHTWEIGHT_API SqlFixedString
}

// clang-format off
constexpr pointer_type c_str() noexcept { _data[_size] = '\0'; return _data; }
constexpr pointer_type data() noexcept { return _data; }
constexpr iterator begin() noexcept { return _data; }
constexpr iterator end() noexcept { return _data + size(); }
constexpr T& at(std::size_t i) noexcept { return _data[i]; }
constexpr T& operator[](std::size_t i) noexcept { return _data[i]; }

constexpr const_pointer_type c_str() const noexcept { const_cast<T*>(_data)[_size] = '\0'; return _data; }
constexpr const_pointer_type data() const noexcept { return _data; }
constexpr const_iterator begin() const noexcept { return _data; }
constexpr const_iterator end() const noexcept { return _data + size(); }
constexpr T const& at(std::size_t i) const noexcept { return _data[i]; }
constexpr T const& operator[](std::size_t i) const noexcept { return _data[i]; }
[[nodiscard]] constexpr pointer_type c_str() noexcept { _data[_size] = '\0'; return _data; }
[[nodiscard]] constexpr pointer_type data() noexcept { return _data; }
[[nodiscard]] constexpr iterator begin() noexcept { return _data; }
[[nodiscard]] constexpr iterator end() noexcept { return _data + size(); }
[[nodiscard]] constexpr T& at(std::size_t i) noexcept { return _data[i]; }
[[nodiscard]] constexpr T& operator[](std::size_t i) noexcept { return _data[i]; }

[[nodiscard]] constexpr const_pointer_type c_str() const noexcept { const_cast<T*>(_data)[_size] = '\0'; return _data; }
[[nodiscard]] constexpr const_pointer_type data() const noexcept { return _data; }
[[nodiscard]] constexpr const_iterator begin() const noexcept { return _data; }
[[nodiscard]] constexpr const_iterator end() const noexcept { return _data + size(); }
[[nodiscard]] constexpr T const& at(std::size_t i) const noexcept { return _data[i]; }
[[nodiscard]] constexpr T const& operator[](std::size_t i) const noexcept { return _data[i]; }
// clang-format on

template <std::size_t OtherSize, SqlStringPostRetrieveOperation OtherPostOp>
Expand Down Expand Up @@ -285,8 +285,8 @@ struct LIGHTWEIGHT_API SqlCommonStringBinder<SqlFixedString<N, T, PostOp>>
template <std::size_t N, SqlStringPostRetrieveOperation PostOp>
struct LIGHTWEIGHT_API SqlDataTraits<SqlFixedString<N, char, PostOp>>
{
static constexpr inline unsigned Size = N;
static constexpr inline SqlColumnType Type = SqlColumnType::CHAR;
static constexpr unsigned Size = N;
static constexpr SqlColumnType Type = SqlColumnType::CHAR;
};

template <std::size_t N, typename T, SqlStringPostRetrieveOperation P>
Expand Down
6 changes: 3 additions & 3 deletions src/Lightweight/DataBinder/SqlNullValue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct SqlNullType
};

// Used to indicate a NULL value in a SQL query.
constexpr inline auto SqlNullValue = SqlNullType {};
constexpr auto SqlNullValue = SqlNullType {};

template <>
struct SqlDataBinder<SqlNullType>
Expand All @@ -34,6 +34,6 @@ struct SqlDataBinder<SqlNullType>
template <>
struct SqlDataTraits<SqlNullType>
{
static constexpr inline unsigned Size = 0;
static constexpr inline SqlColumnType Type = SqlColumnType::UNKNOWN;
static constexpr unsigned Size = 0;
static constexpr SqlColumnType Type = SqlColumnType::UNKNOWN;
};
4 changes: 2 additions & 2 deletions src/Lightweight/DataBinder/SqlTrimmedString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct std::formatter<SqlTrimmedString>: std::formatter<std::string>
template <>
struct SqlDataTraits<SqlTrimmedString>
{
static constexpr inline unsigned Size = 0;
static constexpr inline SqlColumnType Type = SqlColumnType::STRING;
static constexpr unsigned Size = 0;
static constexpr SqlColumnType Type = SqlColumnType::STRING;
};

template <>
Expand Down
5 changes: 3 additions & 2 deletions src/Lightweight/DataBinder/SqlVariant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ struct SqlVariant
SqlVariant(SqlVariant&&) noexcept = default;
SqlVariant& operator=(SqlVariant const&) = default;
SqlVariant& operator=(SqlVariant&&) noexcept = default;
~SqlVariant() = default;

SqlVariant(InnerType const& other):
value(other)
Expand Down Expand Up @@ -170,10 +171,10 @@ struct SqlVariant
[[nodiscard]] T ValueOr(T&& defaultValue) const noexcept
{
if constexpr (std::is_integral_v<T>)
return TryGetIntegral<T>().value_or(defaultValue);
return TryGetIntegral<T>().value_or(std::forward<T>(defaultValue));

if (IsNull())
return defaultValue;
return std::forward<T>(defaultValue);

return std::get<T>(value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lightweight/DataBinder/StdString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ struct SqlCommonStringBinder<std::basic_string<CharT>>
template <>
struct SqlDataTraits<std::string>
{
static constexpr inline unsigned Size = 0;
static constexpr inline SqlColumnType Type = SqlColumnType::STRING;
static constexpr unsigned Size = 0;
static constexpr SqlColumnType Type = SqlColumnType::STRING;
};
9 changes: 5 additions & 4 deletions src/Lightweight/SqlConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using namespace std::chrono_literals;
using namespace std::string_view_literals;

static std::optional<SqlConnectInfo> gDefaultConnectInfo {};
static SqlConnectInfo gDefaultConnectInfo {};
static std::atomic<uint64_t> gNextConnectionId { 1 };
static std::function<void(SqlConnection&)> gPostConnectedHook {};

Expand Down Expand Up @@ -85,7 +85,7 @@ SqlConnection::~SqlConnection() noexcept

SqlConnectInfo const& SqlConnection::DefaultConnectInfo() noexcept
{
return gDefaultConnectInfo.value();
return gDefaultConnectInfo;
}

void SqlConnection::SetDefaultConnectInfo(SqlConnectInfo connectInfo) noexcept
Expand Down Expand Up @@ -163,6 +163,7 @@ bool SqlConnection::Connect(SqlConnectInfo connectInfo) noexcept

if (auto const* info = std::get_if<SqlConnectionDataSource>(&m_data->connectInfo))
{
// NOLINTNEXTLINE(performance-no-int-to-ptr)
SQLRETURN sqlReturn = SQLSetConnectAttrA(m_hDbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER) info->timeout.count(), 0);
if (!SQL_SUCCEEDED(sqlReturn))
{
Expand Down Expand Up @@ -280,7 +281,7 @@ std::string SqlConnection::ServerVersion() const
bool SqlConnection::TransactionActive() const noexcept
{
SQLUINTEGER state {};
SQLRETURN sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_AUTOCOMMIT, &state, 0, nullptr);
SQLRETURN const sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_AUTOCOMMIT, &state, 0, nullptr);
return sqlResult == SQL_SUCCESS && state == SQL_AUTOCOMMIT_OFF;
}

Expand All @@ -295,7 +296,7 @@ bool SqlConnection::TransactionsAllowed() const noexcept
bool SqlConnection::IsAlive() const noexcept
{
SQLUINTEGER state {};
SQLRETURN sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_CONNECTION_DEAD, &state, 0, nullptr);
SQLRETURN const sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_CONNECTION_DEAD, &state, 0, nullptr);
return SQL_SUCCEEDED(sqlResult) && state == SQL_CD_FALSE;
}

Expand Down
9 changes: 4 additions & 5 deletions src/Lightweight/SqlConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "SqlError.hpp"
#include "SqlLogger.hpp"
#include "SqlTraits.hpp"
#include "SqlUtils.hpp"

#include <atomic>
#include <chrono>
Expand Down Expand Up @@ -46,10 +45,10 @@ class LIGHTWEIGHT_API SqlConnection final
// Constructs a new SQL connection to the given connect informaton.
explicit SqlConnection(std::optional<SqlConnectInfo> connectInfo);

SqlConnection(SqlConnection&&) noexcept;
SqlConnection& operator=(SqlConnection&&) noexcept;
SqlConnection(SqlConnection const&) = delete;
SqlConnection& operator=(SqlConnection const&) = delete;
SqlConnection(SqlConnection&& /*other*/) noexcept;
SqlConnection& operator=(SqlConnection&& /*other*/) noexcept;
SqlConnection(SqlConnection const& /*other*/) = delete;
SqlConnection& operator=(SqlConnection const& /*other*/) = delete;

// Destructs this SQL connection object,
~SqlConnection() noexcept;
Expand Down
3 changes: 3 additions & 0 deletions src/Lightweight/SqlError.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ enum class SqlError : std::int16_t
NO_DATA_FOUND = SQL_NO_DATA_FOUND,
UNSUPPORTED_TYPE = 1'000,
INVALID_ARGUMENT = 1'001,
TRANSACTION_ERROR = 1'002,
};

struct SqlErrorCategory: std::error_category
Expand Down Expand Up @@ -126,6 +127,8 @@ struct SqlErrorCategory: std::error_category
return "SQL_UNSUPPORTED_TYPE"s;
case SqlError::INVALID_ARGUMENT:
return "SQL_INVALID_ARGUMENT"s;
case SqlError::TRANSACTION_ERROR:
return "SQL_TRANSACTION_ERROR"s;
}
return std::format("SQL error code {}", code);
}
Expand Down
18 changes: 9 additions & 9 deletions src/Lightweight/SqlLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace
class SqlStandardLogger: public SqlLogger
{
private:
std::chrono::time_point<std::chrono::system_clock> m_currentTime {};
std::string m_currentTimeStr {};
std::chrono::time_point<std::chrono::system_clock> m_currentTime;
std::string m_currentTimeStr;

public:
void Tick()
Expand Down Expand Up @@ -66,13 +66,13 @@ class SqlStandardLogger: public SqlLogger
WriteMessage(" Message: {}", errorInfo.message);
}

void OnConnectionOpened(SqlConnection const&) override {}
void OnConnectionClosed(SqlConnection const&) override {}
void OnConnectionIdle(SqlConnection const&) override {}
void OnConnectionReuse(SqlConnection const&) override {}
void OnExecuteDirect(std::string_view const&) override {}
void OnPrepare(std::string_view const&) override {}
void OnExecute(std::string_view const&) override {}
void OnConnectionOpened(SqlConnection const& /*connection*/) override {}
void OnConnectionClosed(SqlConnection const& /*connection*/) override {}
void OnConnectionIdle(SqlConnection const& /*connection*/) override {}
void OnConnectionReuse(SqlConnection const& /*connection*/) override {}
void OnExecuteDirect(std::string_view const& /*query*/) override {}
void OnPrepare(std::string_view const& /*query*/) override {}
void OnExecute(std::string_view const& /*query*/) override {}
void OnExecuteBatch() override {}
void OnFetchedRow() override {}
};
Expand Down
Loading

0 comments on commit 7ba0c41

Please sign in to comment.