Skip to content

Commit 9a5e5bf

Browse files
authored
Merge pull request #48 from UCBoulder/maint/add-clang-linters
Maint/add clang linters
2 parents 188e65d + e547bba commit 9a5e5bf

15 files changed

Lines changed: 3489 additions & 3757 deletions

‎.clang-format‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
BasedOnStyle: "Google"
2+
3+
AlignAfterOpenBracket: Align
4+
AlignArrayOfStructures: Left
5+
AlignConsecutiveAssignments: AcrossComments
6+
AlignConsecutiveDeclarations: Consecutive
7+
AlignEscapedNewlines: true
8+
AlignOperands: Align
9+
AlignTrailingComments: true
10+
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
11+
ColumnLimit: 120
12+
PenaltyBreakOpenParenthesis: 100
13+
CommentPragmas: 'TESTARGS'
14+
TabWidth: 4
15+
IndentWidth: 4
16+
UseTab: Never
17+
NamespaceIndentation: All
18+
PointerAlignment: Right

‎.clang-tidy‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Checks: "clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-optin.performance.Padding,readability-inconsistent-declaration-parameter-name"
2+
HeaderFilterRegex: .*
3+
WarningsAsErrors: "clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-optin.performance.Padding,readability-inconsistent-declaration-parameter-name"

‎.github/workflows/github-test.yml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ jobs:
4141
if: ${{ matrix.os == 'ubuntu-latest' }}
4242
shell: bash -el {0}
4343
run: gersemi CMakeLists.txt src/ docs/ --check --diff
44+
- name: Configure
45+
shell: bash -el {0}
46+
run: cmake -S . -B build -DTARDIGRADE_HEADER_ONLY=${{ matrix.header_only }} -DTARDIGRADE_ERROR_TOOLS_OPT=${{ matrix.tardigrade_opt }} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
47+
- name: clang-tidy Lint
48+
if: ${{ matrix.os == 'ubuntu-latest'}}
49+
shell: bash -el {0}
50+
run: run-clang-tidy -config-file=.clang-tidy -header-filter=*.h -p build -extra-arg="-mno-sse2"
51+
- name: clang-format Lint
52+
if: ${{ matrix.os == 'ubuntu-latest' && matrix.tardigrade_opt == 'OFF' && matrix.header_only == 'ON' }}
53+
shell: bash -el {0}
54+
run: cmake --build build --target cpp-format-check
4455
- name: Build
4556
shell: bash -el {0}
46-
run: cmake -S . -B build -DTARDIGRADE_HEADER_ONLY=${{ matrix.header_only }} -DTARDIGRADE_ERROR_TOOLS_OPT=${{ matrix.tardigrade_opt }} && cmake --build build --target all
57+
run: cmake --build build --target all
4758
- name: Test
4859
shell: bash -el {0}
4960
run: conda activate test-environment && ctest --test-dir build -V

‎CMakeLists.txt‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ if(${not_conda_test} STREQUAL "true")
5050
project(${PROJECT_NAME} VERSION ${${PROJECT_NAME}_VERSION} LANGUAGES CXX)
5151
endif()
5252

53+
if(CMAKE_EXPORT_COMPILE_COMMANDS)
54+
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
55+
endif()
56+
5357
# Add installation directory variables
5458
include(GNUInstallDirs)
5559

@@ -58,13 +62,21 @@ set(CMAKE_CXX_STANDARD 17)
5862
set(CMAKE_CXX_STANDARD_REQUIRED True)
5963
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
6064
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
61-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lrt -O3 -DNDEBUG")
65+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -DNDEBUG")
6266
endif()
6367

6468
# Enable CTest
6569
enable_testing()
6670

6771
#================================================================================================= FIND DEPENDENCIES ===
72+
73+
#=============================================================================================== FIND PROGRAMS
74+
75+
find_program(CLANG_FORMAT_EXECUTABLE NAMES clang-format REQUIRED)
76+
set_property(GLOBAL PROPERTY CLANG_FORMAT_SOURCE_FILES)
77+
78+
#=============================================================================================== ADD PROJECT TARGETS ===
79+
6880
# Find eigen
6981
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
7082
include_directories(${EIGEN3_INCLUDE_DIR})
@@ -165,3 +177,25 @@ if(${not_conda_test} STREQUAL "true")
165177
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake
166178
)
167179
endif()
180+
181+
#================================================================================== SETUP CLANG FORMATTING TARGETS ===
182+
183+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
184+
get_property(CLANG_FORMAT_SOURCE_FILES_VALUE GLOBAL PROPERTY CLANG_FORMAT_SOURCE_FILES)
185+
186+
add_custom_target(
187+
cpp-format-check
188+
COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file --dry-run -Werror ${CLANG_FORMAT_SOURCE_FILES_VALUE}
189+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
190+
COMMENT "Check the C++ code format with clang-format"
191+
)
192+
set_target_properties(cpp-format-check PROPERTIES EXCLUDE_FROM_ALL TRUE)
193+
194+
add_custom_target(
195+
cpp-format
196+
COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file -i ${CLANG_FORMAT_SOURCE_FILES_VALUE}
197+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
198+
COMMENT "Format the C++ code with clang-format"
199+
)
200+
set_target_properties(cpp-format PROPERTIES EXCLUDE_FROM_ALL TRUE)
201+
endif()

‎README.rst‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
.. _`Sphinx style guide`: https://documentation-style-guide-sphinx.readthedocs.io/en/latest/style-guide.html
1212
.. _`PEP-8`: https://www.python.org/dev/peps/pep-0008/
1313
.. _`gersemi`: https://github.com/BlankSpruce/gersemi
14+
.. _`clang-tidy`: https://clang.llvm.org/extra/clang-tidy/
15+
.. _`clang-format`: https://clang.llvm.org/docs/ClangFormat.html
1416

1517
.. targets-end-do-not-remove
1618
@@ -124,6 +126,8 @@ Build the Conda package
124126
Contribution Guidelines
125127
***********************
126128

129+
.. contribution-start-do-not-remove
130+
127131
Git Commit Message
128132
==================
129133

@@ -172,41 +176,45 @@ and any automatic fixes may be reviewed and then applied by developers with the
172176
$ gersemi CMakeLists.txt src/ docs/ --diff
173177
$ gersemi CMakeLists.txt src/ docs/ --in-place
174178
175-
This project does not yet have a full style guide. Generally, wherever a style can't be inferred from surrounding code
176-
this project falls back to `PEP-8`_ -like styles. There are two notable exceptions to the notional PEP-8 fall back:
179+
This project enforces its style using `clang-tidy`_ and `clang-format`_ as configured with the
180+
`.clang-format` and `.clang-tidy` files in the root directory. The formatting of the project can be
181+
checked using `clang-tidy`_ by first configuring the project using
177182

178-
1. `Doxygen`_ style docstrings are required for automated, API from source documentation.
179-
2. This project prefers expansive whitespace surrounding parentheses, braces, and brackets.
183+
.. code-block:
180184
181-
* No leading space between a function and the argument list.
182-
* One space following an open paranthesis ``(``, brace ``{``, or bracket ``[``
183-
* One space leading a close paranthesis ``)``, brace ``}``, or bracket ``]``
185+
$ cmake -S . -B build ... -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
184186
185-
An example of the whitespace style:
187+
where `...` are the other configuration flags specified. After this clang-tidy can be run on the
188+
full project from the source directory via
186189

187-
.. code-block:: bash
190+
.. CAUTION::
191+
Commit all changes prior to running the clang tidy command. This will edit all source files.
188192

189-
my_function( arg1, { arg2, arg3 }, arg4 );
193+
.. code-block:
194+
195+
$ run-clang-tidy -config-file=.clang-tidy -header-filter=*.h -p build
190196
191-
The following ``sed`` commands may be useful for updating white space, but must
192-
be used with care. The developer is recommended to use a unique git commit
193-
between each command with a corresponding review of the changes and a unit test
194-
run.
197+
The formatting can be checked using `clang-format`_ by running
195198

196-
* Trailing space for open paren/brace/bracket
199+
.. code-block:
197200
198-
.. code-block:: bash
201+
$ cmake -S . -B build ...
202+
$ cmake --build build --target cpp-format-check
199203
200-
sed -i 's/\([({[]\)\([^ ]\)/\1 \2/g' <list of files to update>
204+
which will indicate if the formatting is correct. The c++ files can be re-formatted to match the
205+
style guidance by running
201206

202-
* Leading space for close paren/brace/bracket
207+
.. CAUTION::
208+
Commit all changes prior to running the format command. This will edit all source files.
203209

204-
.. code-block:: bash
210+
.. code-block
205211
206-
sed -i 's/\([^ ]\)\([)}\]]\)/\1 \2/g' <list of files to update>
212+
$ cmake --build build --target cpp-format
207213
208-
* White space between adjacent paren/brace/bracket
214+
If the style is not constrained by the above, it should be inferred by the surrounding code.
215+
Wherever a style can't be inferred from surrounding code this project falls back to `PEP-8`_-like
216+
styles the exceptions to the notional PEP-8 fall back:
209217

210-
.. code-block:: bash
218+
1. `Doxygen`_ style docstrings are required for automated, API from source documentation.
211219

212-
sed -i 's/\([)}\]]\)\([)}\]]\)/\1 \2/g' <list of files to update>
220+
.. contribution-end-do-not-remove

‎environment.txt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
boa
22
breathe>=4.30.0
3-
clang-tools
43
cmake>=3.30
54
compilers
65
cython
@@ -15,7 +14,9 @@ pytest-cov
1514
pytest-xdist
1615
python
1716
python-build # required to build tardigrade_error_tools from source
18-
setuptools_scm>=6.2
17+
setuptools >=77
18+
setuptools_scm>=8
19+
clang-tools
1920
sphinx>=3.0.4
2021
sphinx_rtd_theme>=0.4.3
2122
sphinxcontrib-bibtex

‎recipe/meta.yaml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ requirements:
2525
- {{ compiler('cxx') }}
2626
- make
2727
- cmake >=3.30
28+
- clang-tools
2829
- breathe >=4.3.0
2930
- doxygen
3031
- libboost-python-devel >=1.82
31-
- setuptools >=62
32-
- setuptools_scm >=6.2
32+
- setuptools >=77
33+
- setuptools_scm >=8
3334
- sphinx >=3.0.4
3435
- sphinx_rtd_theme >=0.4.3
3536
- sphinxcontrib-bibtex
@@ -45,6 +46,7 @@ test:
4546
- {{ compiler('cxx') }}
4647
- make
4748
- cmake >=3.30
49+
- clang-tools
4850
- libboost-python-devel >=1.82
4951
- doxygen
5052
- sphinx >=3.0.4

‎src/cpp/CMakeLists.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ install(
1212
)
1313
install(FILES ${PROJECT_NAME}.h ${PROJECT_NAME}.cpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
1414

15+
set_property(GLOBAL APPEND PROPERTY CLANG_FORMAT_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.h")
16+
set_property(GLOBAL APPEND PROPERTY CLANG_FORMAT_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.cpp")
17+
1518
foreach(package ${ADDITIONAL_HEADER_ONLY_LIBRARIES})
1619
target_include_directories(
1720
${package}
@@ -26,4 +29,9 @@ foreach(package ${ADDITIONAL_HEADER_ONLY_LIBRARIES})
2629
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
2730
)
2831
install(FILES ${package}.h ${package}.cpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
32+
33+
get_target_property(target_sources ${package} SOURCES)
34+
foreach(source ${target_sources})
35+
set_property(GLOBAL APPEND PROPERTY CLANG_FORMAT_SOURCE_FILES "${PROJECT_SOURCE_DIR}/${source}")
36+
endforeach(source)
2937
endforeach(package)

0 commit comments

Comments
 (0)