Skip to content

Commit

Permalink
Add clang format (#47)
Browse files Browse the repository at this point in the history
* Add clang format rule file

* Add CMake targets to run clang-format

* Add github action for c++ analysis

* Clang format all files
  • Loading branch information
Alex-PLACET authored Apr 9, 2024
1 parent 2a07414 commit 8d69576
Show file tree
Hide file tree
Showing 28 changed files with 615 additions and 460 deletions.
90 changes: 90 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
BasedOnStyle: Mozilla

AccessModifierOffset: '-4'
AlignAfterOpenBracket: BlockIndent
AlignEscapedNewlines: Left
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
# Forbid one line lambdas because clang-format makes a weird split when
# single instructions lambdas are too long.
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: AfterComma
BreakStringLiterals: false
ColumnLimit: '110'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Always
EmptyLineBeforeAccessModifier: Always
ExperimentalAutoDetectBinPacking: true
IncludeBlocks: Regroup
IncludeCategories:
- Regex: <[^.]+>
Priority: 1
- Regex: <sparrow/.+>
Priority: 3
- Regex: <.+>
Priority: 2
- Regex: '"sparrow/.+"'
Priority: 4
- Regex: '".+"'
Priority: 5
IndentCaseLabels: true
IndentWidth: '4'
IndentWrappedFunctionNames: false
InsertBraces: true
InsertTrailingCommas: Wrapped
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
Language: Cpp
MaxEmptyLinesToKeep: '2'
NamespaceIndentation: All
ObjCBlockIndentWidth: '4'
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PackConstructorInitializers: Never
PenaltyBreakAssignment: 100000
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyBreakComment: 10
PenaltyBreakOpenParenthesis: 0
PenaltyBreakTemplateDeclaration: 0
PenaltyExcessCharacter: 10
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 10
PointerAlignment: Left
QualifierAlignment: Custom # Experimental
QualifierOrder: [inline, static, constexpr, const, volatile, type]
ReflowComments: true
SeparateDefinitionBlocks: Always
SortIncludes: CaseInsensitive
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: '2'
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++20
TabWidth: '4'
UseTab: Never
43 changes: 43 additions & 0 deletions .github/workflows/analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: cpp-linter

on: [push, pull_request]
defaults:
run:
shell: bash -e -l {0}
jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Install LLVM and Clang
uses: egor-tensin/setup-clang@v1
with:
version: 18
platform: x64

- name: Checkout repository
uses: actions/checkout@v4

- name: Set conda environment
uses: mamba-org/setup-micromamba@main
with:
environment-name: myenv
environment-file: environment-dev.yml
init-shell: bash
cache-downloads: true

- name: Configure using CMake
run: cmake -G Ninja -Bbuild -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DBUILD_TESTS=ON

- name: Run C++ analysis
uses: cpp-linter/cpp-linter-action@v2
id: linter
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
version: 18
# lines-changed-only: true
database: 'build'
style: 'file' # Use .clang-format config file
tidy-checks: '-*' # disable clang-tidy checks.
step-summary: true
4 changes: 1 addition & 3 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defaults:
shell: bash -e -l {0}
jobs:
build:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
name: ${{ matrix.sys.compiler }}-${{ matrix.sys.version }}-${{ matrix.sys.stdlib }}-${{ matrix.config.name }}
strategy:
fail-fast: false
Expand All @@ -30,7 +30,6 @@ jobs:

steps:


- name: Install GCC
if: matrix.sys.compiler == 'gcc'
uses: egor-tensin/setup-gcc@v1
Expand All @@ -49,7 +48,6 @@ jobs:
if: matrix.sys.compiler == 'clang'
run: sudo apt install ${{matrix.sys.stdlib}}-dev -y


- name: Checkout code
uses: actions/checkout@v3

Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ message(STATUS "Building sparrow v${${PROJECT_NAME}_VERSION}")

OPTION(BUILD_TESTS "sparrow test suite" OFF)

# Linter options
# =============

OPTION(ACTIVATE_LINTER "Create targets to run clang-format" OFF)

if(ACTIVATE_LINTER)
include(cmake/clang-format.cmake)
endif()

# Dependencies
# ============

Expand Down
84 changes: 84 additions & 0 deletions cmake/clang-format.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
set(CLANG-FORMAT_MINIMUM_MAJOR_VERSION 18)

function(get_clang_format_version clang_format_path)
set(CLANG_FORMAT_VERSION_OUTPUT "")
execute_process(
COMMAND ${clang_format_path} --version
OUTPUT_VARIABLE CLANG_FORMAT_VERSION_OUTPUT
)
string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" CLANG_FORMAT_VERSION_OUTPUT ${CLANG_FORMAT_VERSION_OUTPUT})
set(CLANG_FORMAT_MAJOR_VERSION ${CMAKE_MATCH_1} PARENT_SCOPE)
set(CLANG_FORMAT_MINOR_VERSION ${CMAKE_MATCH_2} PARENT_SCOPE)
set(CLANG_FORMAT_PATCH_VERSION ${CMAKE_MATCH_3} PARENT_SCOPE)
endfunction()

function(check_clang-format_version validator_result_var item)
set(${validator_result_var} FALSE PARENT_SCOPE)
get_clang_format_version(${item})
if (CLANG_FORMAT_MAJOR_VERSION LESS CLANG-FORMAT_MINIMUM_MAJOR_VERSION)
message(DEBUG "clang-format found at ${item} | version: ${CLANG_FORMAT_MAJOR_VERSION}.${CLANG_FORMAT_MINOR_VERSION}.${CLANG_FORMAT_PATCH_VERSION}")
message(DEBUG "but version is lower than ${CLANG-FORMAT_MINIMUM_MAJOR_VERSION}")
set(${validator_result_var} FALSE PARENT_SCOPE)
else()
set(${validator_result_var} TRUE PARENT_SCOPE)
endif()
endfunction()

function(print_clang_format_install_instructions)
message(STATUS "🛠️ Please install clang-format to enable code formatting")
message(STATUS "Can be installed via conda-forge: https://prefix.dev/channels/conda-forge/packages/clang-format")
if(UNIX)
if(APPLE)
message(STATUS "🍎 On MacOS, you can install clang-format with:")
message(STATUS "\t> brew install clang-format")
else()
message(STATUS "🐧 On Ubuntu, you can install clang-format with:")
message(STATUS "\t> sudo apt-get install clang-format")
endif()
elseif(WIN32)
message(STATUS "🪟 On Windows, you can install clang-format with:")
message(STATUS "\t> winget llvm")
endif()
endfunction()

find_program(CLANG_FORMAT clang-format
VALIDATOR check_clang-format_version)

if(NOT CLANG_FORMAT)
message(WARNING "❗ clang-format not found")

print_clang_format_install_instructions()
else()
get_clang_format_version(${CLANG_FORMAT})
message(STATUS "✅ clang-format (version: ${CLANG_FORMAT_MAJOR_VERSION}.${CLANG_FORMAT_MINOR_VERSION}.${CLANG_FORMAT_PATCH_VERSION}) found at ${CLANG_FORMAT}")

# list all files to format
set(
FORMAT_PATTERNS
include/*.hpp
test/*.cpp
test/*.hpp
CACHE STRING
"; separated patterns relative to the project source dir to format"
)

set(ALL_FILES_TO_FORMAT "")
foreach(PATTERN ${FORMAT_PATTERNS})
file(GLOB_RECURSE FILES_TO_FORMAT ${CMAKE_SOURCE_DIR}/${PATTERN})
list(APPEND ALL_FILES_TO_FORMAT ${FILES_TO_FORMAT})
endforeach()

add_custom_target(
clang-format
COMMAND ${CLANG_FORMAT} -i -style=file ${ALL_FILES_TO_FORMAT}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Running clang-format on all files"
)

add_custom_target(
clang-format_dry_run
COMMAND ${CLANG_FORMAT} --dry-run -style=file ${ALL_FILES_TO_FORMAT}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Running dry clang-format on all files"
)
endif()
39 changes: 13 additions & 26 deletions include/sparrow/array_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ namespace sparrow
self_type& operator=(U&& value);

void swap(self_type& rhs);

private:

void reset();
Expand All @@ -205,34 +205,26 @@ namespace sparrow
* return reference proxies when it is dereferenced.
*/
template <class L, bool is_const>
class layout_iterator : public iterator_base
<
layout_iterator<L, is_const>,
mpl::constify_t<typename L::value_type, is_const>,
typename L::iterator_tag,
std::conditional_t<is_const, const_reference_proxy<L>, reference_proxy<L>>
>
class layout_iterator : public iterator_base<
layout_iterator<L, is_const>,
mpl::constify_t<typename L::value_type, is_const>,
typename L::iterator_tag,
std::conditional_t<is_const, const_reference_proxy<L>, reference_proxy<L>>>
{
public:

using self_type = layout_iterator<L, is_const>;
using base_type = iterator_base
<
using base_type = iterator_base<
self_type,
mpl::constify_t<typename L::value_type, is_const>,
typename L::iterator_tag,
std::conditional_t<is_const, const_reference_proxy<L>, reference_proxy<L>>
>;
std::conditional_t<is_const, const_reference_proxy<L>, reference_proxy<L>>>;
using reference = typename base_type::reference;
using difference_type = typename base_type::difference_type;

using value_iterator = std::conditional_t<
is_const, typename L::const_value_iterator, typename L::value_iterator
>;
using value_iterator = std::conditional_t<is_const, typename L::const_value_iterator, typename L::value_iterator>;

using bitmap_iterator = std::conditional_t<
is_const, typename L::const_bitmap_iterator, typename L::bitmap_iterator
>;
using bitmap_iterator = std::conditional_t<is_const, typename L::const_bitmap_iterator, typename L::bitmap_iterator>;

layout_iterator() noexcept = default;
layout_iterator(value_iterator value_iter, bitmap_iterator bitmap_iter);
Expand Down Expand Up @@ -275,7 +267,6 @@ namespace sparrow
const D1& dlhs = lhs.derived_cast();
const D2& drhs = rhs.derived_cast();
return (dlhs && drhs && (dlhs.value() == drhs.value())) || (!dlhs && !drhs);

}

template <class D, not_ref_proxy T>
Expand Down Expand Up @@ -400,15 +391,15 @@ namespace sparrow

template <class L>
template <std::convertible_to<typename L::inner_value_type> U>
auto reference_proxy<L>::operator=(const std::optional<U>& rhs) -> self_type&
auto reference_proxy<L>::operator=(const std::optional<U>& rhs) -> self_type&
{
update(rhs);
return *this;
}

template <class L>
template <std::convertible_to<typename L::inner_value_type> U>
auto reference_proxy<L>::operator=(std::optional<U>&& rhs) -> self_type&
auto reference_proxy<L>::operator=(std::optional<U>&& rhs) -> self_type&
{
update(std::move(rhs));
return *this;
Expand Down Expand Up @@ -487,10 +478,7 @@ namespace sparrow
**********************************/

template <class L, bool is_const>
layout_iterator<L, is_const>::layout_iterator(
value_iterator value_iter,
bitmap_iterator bitmap_iter
)
layout_iterator<L, is_const>::layout_iterator(value_iterator value_iter, bitmap_iterator bitmap_iter)
: m_value_iter(value_iter)
, m_bitmap_iter(bitmap_iter)
{
Expand Down Expand Up @@ -541,4 +529,3 @@ namespace sparrow
return m_value_iter < rhs.m_value_iter && m_bitmap_iter < rhs.m_bitmap_iter;
}
}

3 changes: 1 addition & 2 deletions include/sparrow/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include <concepts>
#include <cstdint>

#include "sparrow/mp_utils.hpp"
#include "sparrow/iterator.hpp"
#include "sparrow/mp_utils.hpp"

namespace sparrow
{
Expand Down Expand Up @@ -469,4 +469,3 @@ namespace sparrow
{
}
}

Loading

0 comments on commit 8d69576

Please sign in to comment.