Skip to content

Commit

Permalink
clang-tidy (#5)
Browse files Browse the repository at this point in the history
* clang-tidy config and fixes

* tidy script

* CI tidy checks

* Run only on pushes to main
  • Loading branch information
bgs99 authored May 11, 2024
1 parent 7bc3255 commit 0e7542a
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 73 deletions.
5 changes: 2 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ BasedOnStyle: LLVM

IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<pfr-orm/'
- Regex: "^<pfr-orm/"
CaseSensitive: true
Priority: 1
- Regex: '^<(fmt/|boost/|libpq-fe.h>)'
- Regex: "^<(fmt/|boost/|libpq-fe\\.h>)"
CaseSensitive: true
Priority: 3
- Priority: 2


ForEachMacros: []
37 changes: 37 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Checks:
- readability-*
- portability-*
- performance-*
- modernize-*
- misc-*
- cppcoreguidelines-*
- concurrency-*
- cert-*
- bugprone-*
- -misc-include-cleaner # not reliable
- -llvm-header-guard # #pragma once
- -llvm-include-order # managed by clang-format
- -modernize-use-trailing-return-type # TODO
- -misc-no-recursion # simplifies code

CheckOptions:
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.StructCase, value: CamelCase }
- { key: readability-identifier-naming.MemberCase, value: camelBack }
- { key: readability-identifier-naming.FunctionCase, value: camelBack }
- { key: readability-identifier-naming.LocalVariableCase, value: camelBack }
- { key: readability-identifier-naming.ParameterCase, value: camelBack }
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
- { key: readability-identifier-naming.EnumConstantCase, value: CamelCase }
- { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
- { key: readability-identifier-naming.ConstantMemberCase, value: camelBack }
- { key: readability-identifier-length.MinimumVariableNameLength, value: 3 }
- { key: readability-identifier-length.MinimumParameterNameLength, value: 3 }
- {
key: readability-identifier-length.IgnoredParameterNames,
value: "i|j|k|x|y|z|os",
}
- {
key: readability-identifier-length.IgnoredVariableNames,
value: "i|j|k|x|y|z|os",
}
29 changes: 27 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: tests
on:
- push
- pull_request
push:
branches:
- main
pull_request:

jobs:
check-format:
Expand All @@ -22,6 +24,29 @@ jobs:
- name: Check formatting
run: python ${{ github.workspace }}/scripts/check_format.py --clang-format=clang-format-18

check-tidy:
runs-on: ubuntu-22.04
env:
build_dir: ${{ github.workspace }}/build
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Add LLVM apt key
run: wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/llvm-toolchain-focal-18.asc

- name: Add LLVM repo
run: sudo add-apt-repository 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-18 main'

- name: Install dependencies
run: sudo apt-get install libfmt-dev libpq-dev clang-tidy-18

- name: Configure CMake
run: cmake -B ${{ env.build_dir }} -S ${{ github.workspace }}

- name: Run clang-tidy
run: python ${{ github.workspace }}/scripts/tidy.py --clang-tidy=run-clang-tidy-18 --compile-commands=${{ env.build_dir }}

test:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ find_package(PostgreSQL REQUIRED)

add_library(pfr-orm STATIC)
target_compile_features(pfr-orm PUBLIC cxx_std_20)
target_sources(pfr-orm PRIVATE lib/detail/escaping.cpp
lib/detail/operations.cpp)
target_sources(pfr-orm PRIVATE lib/detail/operations.cpp)
target_include_directories(pfr-orm PUBLIC include)
target_link_libraries(
pfr-orm
Expand Down
11 changes: 7 additions & 4 deletions include/pfr-orm/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string_view>

#include <boost/pfr.hpp>
#include <boost/pfr/config.hpp>

static_assert(BOOST_PFR_ENABLED, "Boost.PFR is not supported, cannot build");
static_assert(BOOST_PFR_CORE_NAME_ENABLED,
Expand All @@ -18,14 +19,14 @@ static_assert(BOOST_PFR_CORE_NAME_ENABLED,
namespace pfrorm {

/// Identifier mode
enum class IdMode {
enum class IdMode : std::uint8_t {
Auto, ///< Automatically generated by database
Manual, ///< Provided when persisting
};

template <typename T> class FieldDescriptor {
public:
constexpr std::size_t get() const { return this->field; }
[[nodiscard]] constexpr std::size_t get() const { return this->field; }

private:
std::size_t field;
Expand All @@ -37,6 +38,7 @@ template <typename T> class FieldDescriptor {
detail::getFieldDescriptor(const auto C::*, std::string_view);
};

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage): TODO
#define PFRORM_FIELD(type, field) \
::pfrorm::detail::getFieldDescriptor(&type::field, #field)

Expand All @@ -56,7 +58,7 @@ template <typename T> struct CompositeRegistrationData {};
/// ORM composite value registration
/// @tparam T registered type
template <typename T>
std::optional<CompositeRegistrationData<T>> CompositeRegistration =
constexpr std::optional<CompositeRegistrationData<T>> CompositeRegistration =
std::nullopt;

enum class NativeType : std::uint8_t {
Expand All @@ -71,7 +73,8 @@ template <typename T> struct ValueRegistrationData {
/// Basic value type mapping
/// @tparam T registered value type
template <typename T>
std::optional<ValueRegistrationData<T>> ValueRegistration = std::nullopt;
constexpr std::optional<ValueRegistrationData<T>> ValueRegistration =
std::nullopt;

template <>
inline constexpr auto ValueRegistration<uint64_t> =
Expand Down
3 changes: 2 additions & 1 deletion include/pfr-orm/definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <pfr-orm/detail/span.hpp>
#include <pfr-orm/detail/type_name.hpp>

#include <array>
#include <cstddef>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -134,7 +135,7 @@ getFieldDescriptionOfField(const std::string_view name) {
template <detail::Reflectable T, std::size_t FieldsCount, std::size_t... Idx>
constexpr std::array<FieldDescription, FieldsCount>
getFieldDescriptions(std::array<std::string_view, FieldsCount> names,
std::index_sequence<Idx...>) {
std::index_sequence<Idx...> /*indices*/) {
return {getFieldDescriptionOfField<boost::pfr::tuple_element_t<Idx, T>>(
names[Idx])...};
}
Expand Down
11 changes: 0 additions & 11 deletions include/pfr-orm/detail/escaping.hpp

This file was deleted.

3 changes: 3 additions & 0 deletions include/pfr-orm/detail/field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include <pfr-orm/detail/pfr.hpp>

#include <algorithm>
#include <array>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <string_view>

#include <boost/pfr/core_name.hpp>
Expand Down
12 changes: 6 additions & 6 deletions include/pfr-orm/detail/type_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace pfrorm::detail {

template <typename T> constexpr std::string_view wrappedTypeNameImpl() {
#ifdef __clang__
return __PRETTY_FUNCTION__;
return static_cast<const char *>(__PRETTY_FUNCTION__);
#elif defined(__GNUC__)
return __PRETTY_FUNCTION__;
return static_cast<const char *>(__PRETTY_FUNCTION__);
#elif defined(_MSC_VER)
return __FUNCSIG__;
return static_cast<const char *>(__FUNCSIG__);
#else
#error "Unsupported compiler"
#endif
Expand All @@ -20,14 +20,14 @@ template <typename T> constexpr std::string_view wrappedTypeNameImpl() {
template <typename T>
constexpr std::string_view WrappedTypeName = wrappedTypeNameImpl<T>();

constexpr std::string_view voidTypeName = "void";
constexpr std::string_view VoidTypeName = "void";

constexpr std::size_t WrappedTypeNamePrefixLength =
wrappedTypeNameImpl<void>().find(voidTypeName);
wrappedTypeNameImpl<void>().find(VoidTypeName);

constexpr std::size_t WrappedTypeNameSuffixLength =
wrappedTypeNameImpl<void>().length() - WrappedTypeNamePrefixLength -
voidTypeName.length();
VoidTypeName.length();

template <typename T>
constexpr std::size_t TypeNameLength =
Expand Down
3 changes: 0 additions & 3 deletions include/pfr-orm/postges-helpers.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#pragma once

#include <arpa/inet.h>
#include <cstdint>
#include <memory>
#include <postgres_ext.h>
#include <stdexcept>
#include <string>
#include <string_view>

#include <fmt/core.h>
#include <fmt/format.h>
#include <libpq-fe.h>

namespace pfrorm::postgres {
Expand Down
13 changes: 0 additions & 13 deletions lib/detail/escaping.cpp

This file was deleted.

10 changes: 7 additions & 3 deletions lib/detail/operations.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <pfr-orm/api.hpp>
#include <pfr-orm/definitions.hpp>
#include <pfr-orm/postges-helpers.hpp>

#include "multilambda.hpp"
#include "pfr-orm/api.hpp"
#include "pfr-orm/definitions.hpp"
#include "pfr-orm/postges-helpers.hpp"
#include <cstddef>
#include <string_view>
#include <type_traits>
#include <variant>
#include <vector>

#include <fmt/core.h>
#include <fmt/format.h>
Expand Down Expand Up @@ -39,6 +42,7 @@ void createTableFields(const FieldDescription &description,
toString(descr.nativeType), isPrimaryKey ? " PRIMARY KEY" : "");
first = false;
};

const auto createCompositeField =
[&prefixes, &connection, &appender,
&first](const CompositeFieldDescription &descr) {
Expand Down
24 changes: 24 additions & 0 deletions scripts/tidy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/python

from pathlib import Path
import subprocess
import argparse

from common.project import cpp_files


if __name__ == '__main__':
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('--clang-tidy',
help='clang-tidy executable to use',
type=str,
default='run-clang-tidy')
parser.add_argument('--compile-commands',
help='Path to the directory with compile commands',
type=Path,
default='build')

args = parser.parse_args()

subprocess.run([args.clang_tidy, '-quiet', '-p', args.compile_commands.as_posix(),
*[file.as_posix() for file in cpp_files()]])
21 changes: 0 additions & 21 deletions test/Person.hpp

This file was deleted.

1 change: 1 addition & 0 deletions test/nested.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <pfr-orm/api.hpp>
#include <pfr-orm/definitions.hpp>

#include <array>
#include <cstdint>

namespace {
Expand Down
1 change: 1 addition & 0 deletions test/simple.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <pfr-orm/api.hpp>
#include <pfr-orm/definitions.hpp>

#include <array>
#include <cstdint>
#include <string>

Expand Down
24 changes: 20 additions & 4 deletions test/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#include <pfr-orm/api.hpp>
#include <pfr-orm/definitions.hpp>
#include <pfr-orm/operations.hpp>
#include <pfr-orm/postges-helpers.hpp>

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <span>
#include <string>

#include <fmt/core.h>

Expand All @@ -28,11 +34,21 @@ constexpr auto pfrorm::EntityRegistration<Person> =
static_assert(pfrorm::DatabaseEntity<Person>);

int main(int argc, char **argv) {
fmt::println("{}", pfrorm::DatabaseEntityDescription<Person>.name);
const std::span<char *> args{argv, static_cast<std::size_t>(argc)};

pg_orm::Connection conn = pg_orm::connect(argv[1]);
try {
pg_orm::Connection conn = pg_orm::connect(args[1]);

pg_orm::createTable<Person>(*conn);
pg_orm::createTable<Person>(*conn);

assert(!pg_orm::exists<Person>(*conn));
assert(!pg_orm::exists<Person>(*conn));

return 0;
} catch (const std::exception &ex) {
fmt::print("{}\n", ex.what());
return 1;
} catch (...) {
fmt::print("Unknown exception\n");
return 1;
}
}

0 comments on commit 0e7542a

Please sign in to comment.