Skip to content

Commit

Permalink
Add cmake targets for code formatting using clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed May 13, 2016
1 parent 5131609 commit bdb9f0b
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
96 changes: 96 additions & 0 deletions .clang-format
@@ -0,0 +1,96 @@
---
BasedOnStyle: LLVM
IndentWidth: 2
UseTab: Never
...

# ---
# Language: Cpp
# BasedOnStyle: LLVM
# AccessModifierOffset: -2
# AlignAfterOpenBracket: Align
# AlignConsecutiveAssignments: false
# AlignConsecutiveDeclarations: false
# AlignEscapedNewlinesLeft: false
# AlignOperands: true
# AlignTrailingComments: true
# AllowAllParametersOfDeclarationOnNextLine: true
# AllowShortBlocksOnASingleLine: false
# AllowShortCaseLabelsOnASingleLine: false
# AllowShortFunctionsOnASingleLine: All
# AllowShortIfStatementsOnASingleLine: false
# AllowShortLoopsOnASingleLine: false
# AlwaysBreakAfterDefinitionReturnType: None
# AlwaysBreakAfterReturnType: None
# AlwaysBreakBeforeMultilineStrings: false
# AlwaysBreakTemplateDeclarations: false
# BinPackArguments: true
# BinPackParameters: true
# BraceWrapping:
# AfterClass: false
# AfterControlStatement: false
# AfterEnum: false
# AfterFunction: false
# AfterNamespace: false
# AfterObjCDeclaration: false
# AfterStruct: false
# AfterUnion: false
# BeforeCatch: false
# BeforeElse: false
# IndentBraces: false
# BreakBeforeBinaryOperators: None
# BreakBeforeBraces: Attach
# BreakBeforeTernaryOperators: true
# BreakConstructorInitializersBeforeComma: false
# ColumnLimit: 80
# CommentPragmas: '^ IWYU pragma:'
# ConstructorInitializerAllOnOneLineOrOnePerLine: false
# ConstructorInitializerIndentWidth: 4
# ContinuationIndentWidth: 4
# Cpp11BracedListStyle: true
# DerivePointerAlignment: false
# DisableFormat: false
# ExperimentalAutoDetectBinPacking: false
# ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
# IncludeCategories:
# - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
# Priority: 2
# - Regex: '^(<|"(gtest|isl|json)/)'
# Priority: 3
# - Regex: '.*'
# Priority: 1
# IndentCaseLabels: false
# IndentWidth: 2
# IndentWrappedFunctionNames: false
# KeepEmptyLinesAtTheStartOfBlocks: true
# MacroBlockBegin: ''
# MacroBlockEnd: ''
# MaxEmptyLinesToKeep: 1
# NamespaceIndentation: None
# ObjCBlockIndentWidth: 2
# ObjCSpaceAfterProperty: false
# ObjCSpaceBeforeProtocolList: true
# PenaltyBreakBeforeFirstCallParameter: 19
# PenaltyBreakComment: 300
# PenaltyBreakFirstLessLess: 120
# PenaltyBreakString: 1000
# PenaltyExcessCharacter: 1000000
# PenaltyReturnTypeOnItsOwnLine: 60
# PointerAlignment: Right
# ReflowComments: true
# SortIncludes: true
# SpaceAfterCStyleCast: false
# SpaceBeforeAssignmentOperators: true
# SpaceBeforeParens: ControlStatements
# SpaceInEmptyParentheses: false
# SpacesBeforeTrailingComments: 1
# SpacesInAngles: false
# SpacesInContainerLiterals: true
# SpacesInCStyleCastParentheses: false
# SpacesInParentheses: false
# SpacesInSquareBrackets: false
# Standard: Cpp11
# TabWidth: 8
# UseTab: Never
# ...

36 changes: 36 additions & 0 deletions CMakeLists.txt
Expand Up @@ -333,6 +333,42 @@ install(EXPORT DARTTargets DESTINATION share/dart)
configure_file("${PROJECT_SOURCE_DIR}/cmake/uninstall_target.cmake.in" "${PROJECT_BINARY_DIR}/uninstall_target.cmake" IMMEDIATE @ONLY)
add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${PROJECT_BINARY_DIR}/uninstall_target.cmake")

#===============================================================================
# Code Formatting
#===============================================================================
message(STATUS "")
message(STATUS "[ Code Formatting ]")

find_program(CLANG_FORMAT_EXECUTABLE NAMES clang-format clang-format-3.6)

if (CLANG_FORMAT_EXECUTABLE)

message(STATUS "Looking for clang-format - found")

file(GLOB_RECURSE FORMAT_FILES
${CMAKE_SOURCE_DIR}/dart/common/*.hpp
${CMAKE_SOURCE_DIR}/dart/common/*.cpp)

add_custom_target(format
COMMAND ${CMAKE_COMMAND} -E echo "Formatting code... "
COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file -i ${FORMAT_FILES}
COMMAND ${CMAKE_COMMAND} -E echo "Done."
DEPENDS ${CLANG_FORMAT_EXECUTABLE}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dart)

add_custom_target(check-format
COMMAND ${CMAKE_COMMAND} -E echo "Checking code format... "
COMMAND ${CMAKE_SOURCE_DIR}/tools/check_format.sh ${CLANG_FORMAT_EXECUTABLE} ${FORMAT_FILES}
COMMAND ${CMAKE_COMMAND} -E echo "Done."
DEPENDS ${CLANG_FORMAT_EXECUTABLE}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dart)

else()

message(STATUS "Looking for clang-format - NOT found, please install clang-format to enable automatic code formatting")

endif()

#===============================================================================
# API Document using Doxygen
# References:
Expand Down
10 changes: 10 additions & 0 deletions tools/check_format.sh
@@ -0,0 +1,10 @@
#!/bin/bash
num_changes=`$1 -style=file -output-replacements-xml "${@:2}" | grep -c "<replacement "`

if [ "$num_changes" = "0" ]; then
echo "Every file seems to comply with our code convention."
exit 0
else
echo "Found" $num_changes "necessary changes."
exit 1
fi

0 comments on commit bdb9f0b

Please sign in to comment.