diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..49ebda2e5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,55 @@ +BasedOnStyle: LLVM +Language: Cpp + +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: true + PadOperators: true +AlignOperands: AlignAfterOperator +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: All +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterControlStatement: Never + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Custom +BreakBeforeTernaryOperators: true +ColumnLimit: 100 +ConstructorInitializerAllOnOneLineOrOnePerLine: true +Cpp11BracedListStyle: true +FixNamespaceComments: false +IndentWidth: 2 +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: All +PointerAlignment: Left +SpaceAfterTemplateKeyword: false +# type above function name +# short if on same line diff --git a/CMakeLists.txt b/CMakeLists.txt index b8cc278f5..7e9db1699 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,11 @@ if (ADIAR_DOCS) add_subdirectory(docs) endif (ADIAR_DOCS) +# ============================================================================ # +# Formatting +# ============================================================================ # +include(cmake/clang-cxx-dev-tools.cmake) + # ============================================================================ # # Unit Tests # ============================================================================ # diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 02d532d41..e8ef77baf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -125,12 +125,11 @@ The following branch prefixes are reserved. Most of Adiar has been developed with the [Spacemacs](https://www.spacemacs.org/) extension of the Emacs editor. Hence, -code in Adiar is (for the most part) indented as dictated by Emacs (version 28 -or later). There are exceptions to the rule, but if two people disagree, Emacs -gets the last word. +code in Adiar is indented as dictated by Emacs (version 28 or later). We have +set up *Clang Format* to follow the same style (together with other formatting +requirements). -The most basic parts of these formatting rules are also reflected in the -*editorconfig* that most editors can be set up to follow. +Before committing anything, please ensure it is properly formatted. ## Design Principles diff --git a/README.md b/README.md index 9c3af2ee2..ee9e3b1bc 100644 --- a/README.md +++ b/README.md @@ -125,16 +125,18 @@ The project is build with *CMake*, though for convenience I have simplified the The *Makefile* provides the following targets -| target | effect | -|------------|-------------------------------------------------------| -| `build` | Build the source files | -| `docs` | Build the documentation files | -| `clean` | Remove all build files | -| | | -| `test` | Build and run all unit tests | -| `test/...` | Build and run a subset of the unit tests | -| | | -| `coverage` | Build and run all unit tests and create *lcov* report | +| target | effect | +|----------------|-------------------------------------------------------| +| `build` | Build the source files | +| `docs` | Build the documentation files | +| `clean` | Remove all build files | +| | | +| `test` | Build and run all unit tests | +| `test/...` | Build and run a subset of the unit tests | +| | | +| `coverage` | Build and run all unit tests and create *lcov* report | +| | | +| `clang/format` | Format all files in *src/* and *test/* | ### Playground diff --git a/cmake/clang-cxx-dev-tools.cmake b/cmake/clang-cxx-dev-tools.cmake new file mode 100644 index 000000000..609c63eee --- /dev/null +++ b/cmake/clang-cxx-dev-tools.cmake @@ -0,0 +1,47 @@ +# From: +# https://www.labri.fr/perso/fleury/posts/programming/using-clang-tidy-and-clang-format.html + +# Get all src/ files +file(GLOB_RECURSE ALL_SRC_FILES + ${PROJECT_SOURCE_DIR}/src/*.[chi]pp + ${PROJECT_SOURCE_DIR}/src/*.[chi]xx + ${PROJECT_SOURCE_DIR}/src/*.cc + ${PROJECT_SOURCE_DIR}/src/*.hh + ${PROJECT_SOURCE_DIR}/src/*.ii + ${PROJECT_SOURCE_DIR}/src/*.[CHI] +) + +# Get all test/ files +file(GLOB_RECURSE ALL_TEST_FILES + ${PROJECT_SOURCE_DIR}/test/*.[chi]pp + ${PROJECT_SOURCE_DIR}/test/*.[chi]xx + ${PROJECT_SOURCE_DIR}/test/*.cc + ${PROJECT_SOURCE_DIR}/test/*.hh + ${PROJECT_SOURCE_DIR}/test/*.ii + ${PROJECT_SOURCE_DIR}/test/*.[CHI] +) + +# Adding clang-format target if executable is found +find_program(CLANG_FORMAT "clang-format") +if(CLANG_FORMAT) + add_custom_target( + clang-format + COMMAND /usr/bin/clang-format + ${ALL_SRC_FILES} ${ALL_TEST_FILES} + -i + ) +endif() + +# Adding clang-tidy target if executable is found +find_program(CLANG_TIDY "clang-tidy") +if(CLANG_TIDY) + add_custom_target( + clang-tidy + COMMAND /usr/bin/clang-tidy + ${ALL_SRC_SOURCE_FILES} ${ALL_TEST_SOURCE_FILES} + -config='' + -- + -std=c++11 + ${INCLUDE_DIRECTORIES} + ) +endif() diff --git a/makefile b/makefile index d0de69fd3..2d3f06055 100644 --- a/makefile +++ b/makefile @@ -28,6 +28,13 @@ clean/files: clean: | clean/files @rm -r -f build/ +# ============================================================================ # +# CLANG TOOLS +# ============================================================================ # +clang/format: + @mkdir -p build/ && cd build/ && cmake -D CMAKE_BUILD_TYPE=$(BUILD_TYPE) .. + @cd build/ && make clang-format + # ============================================================================ # # UNIT TESTING # ============================================================================ #