-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clang-format tool to check C++ runtime sources
- Loading branch information
Showing
8 changed files
with
255 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Script called from clang_format_utils.cmake to run clang-format and preserve it's output. | ||
# | ||
# Expected definitions: | ||
# CLANG_FORMAT_BIN Clang format binary. | ||
# SOURCE Source to check by clang-format. | ||
# CONFIG_FILE Path to the clang-format config file. | ||
# WERROR Ends with an error in case of any format violation. | ||
cmake_minimum_required(VERSION 3.6.0) | ||
|
||
foreach (ARG CLANG_FORMAT_BIN SOURCE CONFIG_FILE WERROR) | ||
if (NOT DEFINED ${ARG}) | ||
message(FATAL_ERROR "Argument '${ARG}' not defined!") | ||
endif () | ||
endforeach () | ||
|
||
if (WERROR) | ||
set(WERROR_OPTION --Werror) | ||
endif () | ||
|
||
execute_process( | ||
COMMAND ${CLANG_FORMAT_BIN} --style=file:${CONFIG_FILE} --dry-run ${WERROR_OPTION} ${SOURCE} | ||
RESULT_VARIABLE CLANG_FORMAT_RESULT | ||
) | ||
|
||
if (NOT ${CLANG_FORMAT_RESULT} EQUAL 0) | ||
message(NOTICE "Command hints to reformat source using clang-format tool:") | ||
message(NOTICE " git clang-format") | ||
message(NOTICE " clang-format --style=file -i ${SOURCE}") | ||
message(FATAL_ERROR "Clang Format Tool failed!") | ||
endif () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# A function to create clang-format target. | ||
# | ||
# Usage clang_format_add_custom_target | ||
# DEPENDS List of dependencies. | ||
# SOURCES List of source directories for clang-format. | ||
# SOURCES_GLOBS List of sources globbing expressions which will be searched using GLOG_RECURSE. | ||
# CONFIG_FILE Path to .clang-format config file. | ||
# WERROR Ends with an error in case of any format violation. Default is ON. | ||
function(clang_format_add_custom_target CLANG_FORMAT_TARGET) | ||
if (CLANG_FORMAT_BIN) | ||
cmake_parse_arguments(CLANG_FORMAT | ||
"" | ||
"CONFIG_FILE;WERROR" | ||
"DEPENDS;SOURCES;SOURCES_GLOBS" | ||
${ARGN} | ||
) | ||
|
||
# check required arguments | ||
foreach (ARG TARGET CONFIG_FILE) | ||
if (NOT DEFINED CLANG_FORMAT_${ARG}) | ||
message(FATAL_ERROR "No value defined for required argument ${ARG}!") | ||
endif () | ||
endforeach () | ||
|
||
if (NOT DEFINED CLANG_FORMAT_SOURCES AND NOT DEFINED CLANG_FORMAT_SOURCES_GLOBS) | ||
message(FATAL_ERROR "No value defined neither for SOURCES nor SOURCES_GLOBS!") | ||
endif () | ||
|
||
# process optional arguments | ||
if (NOT DEFINED CLANG_FORMAT_WERROR) | ||
set(CLANG_FORMAT_WERROR ON) | ||
endif () | ||
|
||
# process sources | ||
list(APPEND CLANG_FORMAT_SOURCES_LIST ${CLANG_FORMAT_SOURCES}) | ||
foreach (SOURCE_EXPRESSION ${CLANG_FORMAT_SOURCES_GLOBS}) | ||
file(GLOB_RECURSE MATCHING_SOURCES "${SOURCE_EXPRESSION}") | ||
list(APPEND CLANG_FORMAT_SOURCES_LIST ${MATCHING_SOURCES}) | ||
endforeach () | ||
list(REMOVE_DUPLICATES CLANG_FORMAT_SOURCES_LIST) | ||
|
||
# run clang format for each source | ||
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/clang-format") | ||
set(INDEX 0) | ||
foreach (SOURCE_FILE ${CLANG_FORMAT_SOURCES_LIST}) | ||
get_filename_component(SOURCE_FILE_NAME ${SOURCE_FILE} NAME) | ||
set(CLANG_FORMAT_FILE_STAMP "clang-format/${INDEX}_${SOURCE_FILE_NAME}") | ||
add_custom_command( | ||
OUTPUT "${CLANG_FORMAT_FILE_STAMP}" | ||
COMMAND "${CMAKE_COMMAND}" | ||
-DCLANG_FORMAT_BIN="${CLANG_FORMAT_BIN}" | ||
-DSOURCE="${SOURCE_FILE}" | ||
-DCONFIG_FILE="${CLANG_FORMAT_CONFIG_FILE}" | ||
-DWERROR="${CLANG_FORMAT_WERROR}" | ||
-P ${CMAKE_MODULE_PATH}/clang_format_tool.cmake | ||
COMMAND "${CMAKE_COMMAND}" -E touch "${CLANG_FORMAT_FILE_STAMP}" | ||
DEPENDS ${SOURCE_FILE} | ||
COMMENT "Running clang-format on ${SOURCE_FILE}") | ||
list(APPEND CLANG_FORMAT_FILE_STAMPS "${CLANG_FORMAT_FILE_STAMP}") | ||
math(EXPR INDEX "${INDEX} + 1") | ||
endforeach () | ||
add_custom_target(${CLANG_FORMAT_TARGET} ALL | ||
DEPENDS ${CLANG_FORMAT_DEPENDS} ${CLANG_FORMAT_FILE_STAMPS}) | ||
endif () | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
BasedOnStyle: Microsoft | ||
--- | ||
Language: Cpp | ||
|
||
# Access modifier should be aligned to braces. | ||
AccessModifierOffset: -4 | ||
|
||
# Always break before braces. | ||
BreakBeforeBraces: Allman | ||
|
||
# Align pointer to the left (to the type). | ||
PointerAlignment: Left | ||
|
||
# The column limit for Zserio. | ||
ColumnLimit: 112 | ||
|
||
# Always break after template declaration. | ||
AlwaysBreakTemplateDeclarations: Yes | ||
|
||
# Indent width for line continuations. | ||
ContinuationIndentWidth: 8 | ||
|
||
# Settings for constructor initializers. | ||
BreakConstructorInitializers: AfterColon | ||
ConstructorInitializerIndentWidth: 8 | ||
PackConstructorInitializers: Never | ||
|
||
# Don’t align, instead use ContinuationIndentWidth. | ||
AlignAfterOpenBracket: DontAlign | ||
|
||
# Don't align trailing comments. | ||
AlignTrailingComments: false | ||
|
||
# Don't align operands. | ||
AlignOperands: DontAlign | ||
|
||
# Indent preprocessor directives. | ||
IndentPPDirectives: BeforeHash | ||
|
||
# Improve indentation of chained method calls. | ||
PenaltyIndentedWhitespace: 10 | ||
|
||
# Allow brace wrapping for unions . | ||
# Allow empty braces on the same line for functions, records and namespaces. | ||
BreakBeforeBraces: Custom | ||
BraceWrapping: | ||
AfterUnion: true | ||
SplitEmptyFunction: false | ||
SplitEmptyRecord: false | ||
SplitEmptyNamespace: false | ||
|
||
# Regroup and sort includes. | ||
IncludeBlocks: Regroup | ||
IncludeCategories: | ||
# System headers in <>. | ||
- Regex: '<.*>' | ||
Priority: -2 | ||
CaseSensitive: false | ||
# Headers in "" in current directory. | ||
- Regex: '"[^/\]+"' | ||
Priority: 0 | ||
CaseSensitive: false | ||
# Headers in "" in different directory. | ||
- Regex: '".*"' | ||
Priority: -1 | ||
CaseSensitive: false | ||
# All other headers. | ||
- Regex: '.*' | ||
Priority: 1 | ||
CaseSensitive: false | ||
|
||
# Indent case blocks. | ||
IndentCaseBlocks: true | ||
... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.