diff --git a/.clang-format b/.clang-format index 3838b6b1f..64ab822b6 100644 --- a/.clang-format +++ b/.clang-format @@ -30,7 +30,7 @@ BraceWrapping: BeforeCatch: false BeforeElse: false IndentBraces: false -BreakBeforeBinaryOperators: None +BreakBeforeBinaryOperators: NonAssignment BreakBeforeBraces: Custom BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: true diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 4f937513c..f82f75088 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -108,9 +108,12 @@ The Branch Strategy is based on [git-flow-lite](http://nvie.com/posts/a-successf ### Coding styleguide -This project follows the [Qt Coding Style](https://wiki.qt.io/Qt_Coding_Style). All submissions are expected to follow this style. +The coding style of the project is enforced using llvm's `clang-format` formatting tool. A thorough description +of the coding style can be found in the `.clang-format` file, but the main conventions are presented here. -In particular, code must stick to the following rules: +Formatting can be performed automatically by calling `make format` from the `build/` directory. + +Note that [formatting can be disabled on a piece of code](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code) if manual formatting is deemed more readable. #### Naming convention `lowerCamelCase` diff --git a/CMakeLists.txt b/CMakeLists.txt index 188873b95..871f5d638 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -279,6 +279,8 @@ if(WITH_TESTS) enable_testing() endif(WITH_TESTS) +include(CLangFormat) + if(UNIX AND NOT APPLE) find_package(Qt5 COMPONENTS Core Network Concurrent Widgets Test LinguistTools DBus REQUIRED) elseif(APPLE) diff --git a/cmake/CLangFormat.cmake b/cmake/CLangFormat.cmake new file mode 100644 index 000000000..6c26ef7cf --- /dev/null +++ b/cmake/CLangFormat.cmake @@ -0,0 +1,59 @@ +# Copyright (C) 2017 KeePassXC Team +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 or (at your option) +# version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set(EXCLUDED_DIRS + # third-party directories + zxcvbn/ + http/qhttp/ + streams/QtIOCompressor + # objective-c directories + autotype/mac +) + +set(EXCLUDED_FILES + # third-party files + streams/qtiocompressor.cpp + streams/qtiocompressor.h + gui/KMessageWidget.h + gui/KMessageWidget.cpp + tests/modeltest.cpp + tests/modeltest.h + # objective-c files + core/ScreenLockListenerMac.h + core/ScreenLockListenerMac.cpp +) + +file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.h) +foreach (SOURCE_FILE ${ALL_SOURCE_FILES}) + foreach (EXCLUDED_DIR ${EXCLUDED_DIRS}) + string(FIND ${SOURCE_FILE} ${EXCLUDED_DIR} SOURCE_FILE_EXCLUDED) + if (NOT ${SOURCE_FILE_EXCLUDED} EQUAL -1) + list(REMOVE_ITEM ALL_SOURCE_FILES ${SOURCE_FILE}) + endif () + endforeach () + foreach (EXCLUDED_FILE ${EXCLUDED_FILES}) + if (${SOURCE_FILE} MATCHES ".*${EXCLUDED_FILE}$") + list(REMOVE_ITEM ALL_SOURCE_FILES ${SOURCE_FILE}) + endif () + endforeach () +endforeach () + +add_custom_target( + format + COMMAND clang-format + -style=file + -i + ${ALL_SOURCE_FILES} +)