Skip to content

Commit 4b8ab03

Browse files
author
Michael Penick
committed
Add basic support for clang-format
1 parent a90986b commit 4b8ab03

4 files changed

Lines changed: 76 additions & 1 deletion

File tree

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ Cpp11BracedListStyle: false
2424
AlwaysBreakTemplateDeclarations: true
2525
BreakBeforeInheritanceComma: true
2626
...
27-

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ list(APPEND CMAKE_MODULE_PATH ${CASS_ROOT_DIR}/cmake/modules
1717
)
1818

1919
include(CppDriver)
20+
include(ClangFormat)
2021

2122
CassInitProject(dse)
2223
CassRapidJson()

cpp-driver/.clang-format

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
BasedOnStyle: LLVM
3+
Language: Cpp
4+
AccessModifierOffset: -2
5+
IndentWidth: 2
6+
TabWidth: 8
7+
ColumnLimit: 100
8+
UseTab: Never
9+
IndentCaseLabels: true
10+
AlignAfterOpenBracket: true
11+
AlignEscapedNewlines: Left
12+
BreakConstructorInitializers: BeforeComma
13+
AllowShortBlocksOnASingleLine: false
14+
DerivePointerAlignment: false
15+
PointerAlignment: Left
16+
BinPackParameters: true
17+
BinPackArguments: true
18+
AllowShortIfStatementsOnASingleLine: true
19+
CompactNamespaces: true
20+
AlignOperands: true
21+
SpacesInContainerLiterals: true
22+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
23+
Cpp11BracedListStyle: false
24+
AlwaysBreakTemplateDeclarations: true
25+
BreakBeforeInheritanceComma: true
26+
...
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# Format and verify formatting using clang-format
3+
#
4+
cmake_minimum_required(VERSION 2.6.4)
5+
6+
include(FindPackageHandleStandardArgs)
7+
8+
if(NOT CLANG_FORMAT_EXE_NAME)
9+
set(CLANG_FORMAT_EXE_NAME clang-format)
10+
endif()
11+
12+
if(CLANG_FORMAT_ROOT_DIR)
13+
find_program(CLANG_FORMAT_EXE
14+
NAMES ${CLANG_FORMAT_EXE_NAME}
15+
PATHS ${CLANG_FORMAT_ROOT_DIR}
16+
NO_DEFAULT_PATH)
17+
endif()
18+
19+
find_program(CLANG_FORMAT_EXE NAMES ${CLANG_FORMAT_EXE_NAME})
20+
21+
find_package_handle_standard_args(CLANG_FORMAT DEFAULT_MSG CLANG_FORMAT_EXE)
22+
23+
mark_as_advanced(CLANG_FORMAT_EXE)
24+
25+
if(CLANG_FORMAT_FOUND)
26+
set(CLANG_FORMAT_FILE_EXTENSIONS ${CLANG_FORMAT_CXX_FILE_EXTENSIONS} *.cpp *.hpp *.c *.h)
27+
file(GLOB_RECURSE CLANG_FORMAT_ALL_SOURCE_FILES ${CLANG_FORMAT_FILE_EXTENSIONS})
28+
29+
set(CLANG_FORMAT_EXCLUDE_PATTERNS ${CLANG_FORMAT_EXCLUDE_PATTERNS} "/CMakeFiles/" "cmake" "/build/" "/vendor/" "/third_party/" "cassandra.h" "dse.h")
30+
31+
foreach (SOURCE_FILE ${CLANG_FORMAT_ALL_SOURCE_FILES})
32+
foreach (EXCLUDE_PATTERN ${CLANG_FORMAT_EXCLUDE_PATTERNS})
33+
string(FIND ${SOURCE_FILE} ${EXCLUDE_PATTERN} EXCLUDE_FOUND)
34+
if (NOT ${EXCLUDE_FOUND} EQUAL -1)
35+
list(REMOVE_ITEM CLANG_FORMAT_ALL_SOURCE_FILES ${SOURCE_FILE})
36+
endif ()
37+
endforeach ()
38+
endforeach ()
39+
40+
add_custom_target(format
41+
COMMENT "Format source files using clang-format"
42+
COMMAND ${CLANG_FORMAT_EXE} -i -fallback-style=none -style=file ${CLANG_FORMAT_ALL_SOURCE_FILES})
43+
44+
add_custom_target(format-check
45+
COMMENT "Verify source files formatting using clang-format"
46+
COMMAND ! ${CLANG_FORMAT_EXE} -output-replacements-xml -fallback-style=none -style=file ${CLANG_FORMAT_ALL_SOURCE_FILES} | tee replacements.xml | grep -q "replacement offset")
47+
else()
48+
message(STATUS "Unable to find clang-format. Not creating format targets.")
49+
endif()

0 commit comments

Comments
 (0)