-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
2a07414
commit 8d69576
Showing
28 changed files
with
615 additions
and
460 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.