From e36aaa65e0795622e36a7e76f208fa48b953797f 2017-05-16 07:58:22 From: mperrinel Date: 2017-05-16 07:58:22 Subject: [PATCH] Ajout de clangformat --- diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..02acebe --- /dev/null +++ b/.clang-format @@ -0,0 +1,26 @@ +--- +# See http://clang.llvm.org/docs/ClangFormatStyleOptions.html for a definition +# of the options +Language: Cpp +BasedOnStyle: LLVM + +# Line length +ColumnLimit: 100 + +# Indent with 4 spaces +IndentWidth: 4 +AccessModifierOffset: -4 # -IndentWidth +ConstructorInitializerIndentWidth: 8 # 2 * IndentWidth + +# Break only before function braces +BreakBeforeBraces: Stroustrup + +AllowShortFunctionsOnASingleLine: Inline +AlwaysBreakTemplateDeclarations: true +AlwaysBreakBeforeMultilineStrings: true +BreakBeforeBinaryOperators: true +ConstructorInitializerAllOnOneLineOrOnePerLine: true +IndentCaseLabels: true +MaxEmptyLinesToKeep: 2 +Standard: Cpp03 + diff --git a/formatting/.clang-format b/formatting/.clang-format new file mode 100644 index 0000000..02acebe --- /dev/null +++ b/formatting/.clang-format @@ -0,0 +1,26 @@ +--- +# See http://clang.llvm.org/docs/ClangFormatStyleOptions.html for a definition +# of the options +Language: Cpp +BasedOnStyle: LLVM + +# Line length +ColumnLimit: 100 + +# Indent with 4 spaces +IndentWidth: 4 +AccessModifierOffset: -4 # -IndentWidth +ConstructorInitializerIndentWidth: 8 # 2 * IndentWidth + +# Break only before function braces +BreakBeforeBraces: Stroustrup + +AllowShortFunctionsOnASingleLine: Inline +AlwaysBreakTemplateDeclarations: true +AlwaysBreakBeforeMultilineStrings: true +BreakBeforeBinaryOperators: true +ConstructorInitializerAllOnOneLineOrOnePerLine: true +IndentCaseLabels: true +MaxEmptyLinesToKeep: 2 +Standard: Cpp03 + diff --git a/formatting/cmake/FindClangFormat.cmake b/formatting/cmake/FindClangFormat.cmake new file mode 100644 index 0000000..d3fde1d --- /dev/null +++ b/formatting/cmake/FindClangFormat.cmake @@ -0,0 +1,52 @@ +# - try to find clang-format tool +# +# Cache Variables: +# CLANGFORMAT_ROOT_DIR +# CLANGFORMAT_EXECUTABLE +# CLANGFORMAT_USE_FILE +# +# Non-cache variables you might use in your CMakeLists.txt: +# CLANGFORMAT_FOUND +# +# Requires these CMake modules: +# FindPackageHandleStandardArgs (known included with CMake >=2.6.2) + +file(TO_CMAKE_PATH "${CLANGFORMAT_ROOT_DIR}" CLANGFORMAT_ROOT_DIR) +set(CLANGFORMAT_ROOT_DIR + "${CLANGFORMAT_ROOT_DIR}" + CACHE + PATH + "Path to search for clang-format") + +if(CLANGFORMAT_EXECUTABLE AND NOT EXISTS "${CLANGFORMAT_EXECUTABLE}") + set(CLANGFORMAT_EXECUTABLE "notfound" CACHE PATH FORCE "") +endif() + +# If we have a custom path, look there first. +if(CLANGFORMAT_ROOT_DIR) + find_program(CLANGFORMAT_EXECUTABLE + NAMES + clang-format + PATHS + "${CLANGFORMAT_ROOT_DIR}" + PATH_SUFFIXES + bin + NO_DEFAULT_PATH) +endif() + +find_program(CLANGFORMAT_EXECUTABLE NAMES clang-format) + +# Find the use file for clang-format +GET_FILENAME_COMPONENT(CLANGFORMAT_MODULE_DIR ${CMAKE_CURRENT_LIST_FILE} PATH) +SET(CLANGFORMAT_USE_FILE "${CLANGFORMAT_MODULE_DIR}/use_clangformat.cmake") + +SET(CLANGFORMAT_ALL ${CLANGFORMAT_EXECUTABLE} ${CLANGFORMAT_USE_FILE}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(ClangFormat + DEFAULT_MSG + CLANGFORMAT_ALL + CLANGFORMAT_EXECUTABLE + CLANGFORMAT_USE_FILE) + +mark_as_advanced(CLANGFORMAT_EXECUTABLE) diff --git a/formatting/cmake/use_clangformat.cmake b/formatting/cmake/use_clangformat.cmake new file mode 100644 index 0000000..7545eb7 --- /dev/null +++ b/formatting/cmake/use_clangformat.cmake @@ -0,0 +1,128 @@ +# +# use_clangformat.cmake +# +# The following functions are defined in this document: +# + +# ADD_CLANGFORMAT_TARGETS( ... +# [ADD_TO_ALL] +# [NAME ] +# [NAME_ALL ] +# [STYLE style] +# [RECURSE]) +# +# Two custom targets will be created: +# * format_reports is run as part of the build, and is not rerun unless one of +# the file formatted is modified (only created if ADD_TO_ALL is provided); +# * format must be explicitely called (make format) and is rerun even if the +# files to format have not been modified. To achieve this behavior, the commands +# used in this target pretend to produce a file without actually producing it. +# Because the output file is not there after the run, the command will be rerun +# again at the next target build. +# +# If ADD_TO_ALL is provided then a target will be added to the default build +# targets so that each time a source file is compiled, it is formatted with +# clang-format. +# +# NAME and NAME_ALL customize the name of the targets (format and format_reports +# by default respectively). +# +# STYLE sets the style used by clang-format (default to "file"). +# +# RECURSE selects if the glob expressions should be applied recursively or not. +FUNCTION(ADD_CLANGFORMAT_TARGETS) + # Default values + SET(target "format") + SET(target_all "format_all") + SET(style "file") + SET(recurse OFF) + SET(addToAll OFF) + SET(globs) + + # Parse the options + MATH(EXPR lastIdx "${ARGC} - 1") + SET(i 0) + WHILE(i LESS ${ARGC}) + SET(arg "${ARGV${i}}") + IF("${arg}" STREQUAL "ADD_TO_ALL") + SET(addToAll ON) + ELSEIF("${arg}" STREQUAL "NAME") + clangformat_incr(i) + SET(target "${ARGV${i}}") + ELSEIF("${arg}" STREQUAL "NAME_ALL") + clangformat_incr(i) + SET(target_all "${ARGV${i}}") + ELSEIF("${arg}" STREQUAL "STYLE") + clangformat_incr(i) + SET(style "${ARGV${i}}") + ELSEIF("${arg}" STREQUAL "RECURSE") + SET(recurse ON) + ELSE() + LIST(APPEND globs ${arg}) + ENDIF() + clangformat_incr(i) + ENDWHILE() + + # Retrieve source files to format + IF(recurse) + FILE(GLOB_RECURSE srcs ${globs}) + ELSE() + FILE(GLOB srcs ${globs}) + ENDIF() + + IF(NOT CLANGFORMAT_EXECUTABLE) + MESSAGE(FATAL_ERROR "Unable to find clang-format executable") + ENDIF() + + # Format each source file with clang-format + SET(touchedFiles) + SET(fakedTouchedFiles) + SET(reportNb 0) + # Create the directory where the touched files will be saved + SET(formatDirectory "${CMAKE_CURRENT_BINARY_DIR}/format") + FILE(MAKE_DIRECTORY ${formatDirectory}) + FOREACH (s ${srcs}) + SET(touchedFile ${formatDirectory}/format_touchedfile_${reportNb}) + IF(addToAll) + ADD_CUSTOM_COMMAND( + OUTPUT ${touchedFile} + COMMAND ${CLANGFORMAT_EXECUTABLE} + -style="${style}" + -i + ${s} + # Create a file so that this command is executed only if the source + # file is modified + COMMAND ${CMAKE_COMMAND} -E touch ${touchedFile} + DEPENDS ${s} + COMMENT "Formatting code with clang-format of ${s}" + ) + ENDIF() + + SET(fakedTouchedFile ${formatDirectory}/format_fakedtouchedfile_${reportNb}) + ADD_CUSTOM_COMMAND( + OUTPUT ${fakedTouchedFile} + COMMAND ${CLANGFORMAT_EXECUTABLE} + -style="${style}" + -i + ${s} + DEPENDS ${s} + COMMENT "Formatting code with clang-format of ${s}" + ) + + LIST(APPEND touchedFiles ${touchedFile}) + LIST(APPEND fakedTouchedFiles ${fakedTouchedFile}) + clangformat_incr(reportNb) + ENDFOREACH() + + # Create the custom targets that will trigger the custom command created + # previously + IF(addToAll) + ADD_CUSTOM_TARGET(${target_all} ALL DEPENDS ${touchedFiles}) + ENDIF() + ADD_CUSTOM_TARGET(${target} DEPENDS ${fakedTouchedFiles}) + +ENDFUNCTION(ADD_CLANGFORMAT_TARGETS) + +macro(clangformat_incr var_name) + math(EXPR ${var_name} "${${var_name}} + 1") +endmacro()