Skip to content

Commit

Permalink
Add ClangFormat rules
Browse files Browse the repository at this point in the history
  • Loading branch information
louib committed Mar 31, 2018
1 parent 410d88b commit 07bc9a7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Expand Up @@ -30,7 +30,7 @@ BraceWrapping:
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
Expand Down
7 changes: 5 additions & 2 deletions .github/CONTRIBUTING.md
Expand Up @@ -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`
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions cmake/CLangFormat.cmake
@@ -0,0 +1,59 @@
# Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
#
# 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 <http://www.gnu.org/licenses/>.

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}
)

0 comments on commit 07bc9a7

Please sign in to comment.