Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .clang-format #599

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ if (ADIAR_DOCS)
add_subdirectory(docs)
endif (ADIAR_DOCS)

# ============================================================================ #
# Formatting
# ============================================================================ #
include(cmake/clang-cxx-dev-tools.cmake)

# ============================================================================ #
# Unit Tests
# ============================================================================ #
Expand Down
9 changes: 4 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 47 additions & 0 deletions cmake/clang-cxx-dev-tools.cmake
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 7 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ============================================================================ #
Expand Down
Loading