-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dba4ef1
commit a4204f2
Showing
7 changed files
with
250 additions
and
2 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,116 @@ | ||
# Copyright (C) 2018-2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
if(ENABLE_CLANG_TIDY) | ||
set(CLANG_TIDY_REQUIRED_VERSION 15 CACHE STRING "clang-tidy version to use") | ||
set(CLANG_TIDY_FILENAME clang-tidy-${CLANG_TIDY_REQUIRED_VERSION} clang-tidy) | ||
find_host_program(CLANG_TIDY NAMES ${CLANG_TIDY_FILENAME} PATHS ENV PATH) | ||
if(CLANG_TIDY) | ||
execute_process(COMMAND ${CLANG_TIDY} ${CMAKE_CURRENT_SOURCE_DIR} ARGS --version OUTPUT_VARIABLE CLANG_VERSION) | ||
if(NOT CLANG_VERSION) | ||
message(WARNING "Supported clang-tidy version is ${CLANG_TIDY_REQUIRED_VERSION}!") | ||
set(ENABLE_CLANG_TIDY OFF) | ||
else() | ||
string(REGEX REPLACE "[^0-9]+([0-9]+)\\..*" "\\1" CLANG_TIDY_MAJOR_VERSION ${CLANG_VERSION}) | ||
if(NOT CLANG_TIDY_MAJOR_VERSION EQUAL CLANG_TIDY_REQUIRED_VERSION) | ||
message(WARNING "Supported clang-tidy version is ${CLANG_TIDY_REQUIRED_VERSION}! Provided version ${CLANG_TIDY_MAJOR_VERSION}") | ||
set(ENABLE_CLANG_TIDY OFF) | ||
endif() | ||
endif() | ||
else() | ||
message(WARNING "Supported clang-tidy-${CLANG_TIDY_REQUIRED_VERSION} is not found!") | ||
set(ENABLE_CLANG_TIDY OFF) | ||
endif() | ||
endif() | ||
|
||
if(ENABLE_CLANG_TIDY AND NOT TARGET clang_tidy_check_all) | ||
add_custom_target(clang_tidy_check_all) | ||
set_target_properties(clang_tidy_check_all | ||
PROPERTIES FOLDER clang_tidy) | ||
endif() | ||
|
||
# | ||
# ov_add_clang_tidy_target(FOR_TARGETS <target1 target2 ...> | FOR_SOURCES <source1 source2 ...> | ||
# [EXCLUDE_PATTERNS <pattern1 pattern2 ...>]) | ||
# | ||
function(ov_add_clang_tidy_target TARGET_NAME) | ||
if(NOT ENABLE_CLANG_TIDY) | ||
return() | ||
endif() | ||
|
||
set(options ALL) | ||
set(oneValueArgs "") | ||
set(multiValueArgs "FOR_TARGETS" "FOR_SOURCES" "EXCLUDE_PATTERNS") | ||
cmake_parse_arguments(CLANG_TIDY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) | ||
|
||
foreach(target IN LISTS CLANG_TIDY_FOR_TARGETS) | ||
get_target_property(target_sources "${target}" SOURCES) | ||
list(APPEND CLANG_TIDY_FOR_SOURCES ${target_sources}) | ||
endforeach() | ||
list(REMOVE_DUPLICATES CLANG_TIDY_FOR_SOURCES) | ||
|
||
set(all_output_files "") | ||
foreach(source_file IN LISTS CLANG_TIDY_FOR_SOURCES) | ||
set(exclude FALSE) | ||
foreach(pattern IN LISTS CLANG_TIDY_EXCLUDE_PATTERNS) | ||
if(source_file MATCHES "${pattern}") | ||
set(exclude ON) | ||
break() | ||
endif() | ||
endforeach() | ||
|
||
if(exclude) | ||
continue() | ||
endif() | ||
|
||
# ignore object libraries | ||
if(NOT EXISTS "${source_file}") | ||
continue() | ||
endif() | ||
|
||
if(IS_DIRECTORY "${source_file}") | ||
message(FATAL_ERROR "Directory ${source_file} cannot be passed to clang-tidy") | ||
endif() | ||
|
||
file(RELATIVE_PATH source_file_relative "${CMAKE_CURRENT_SOURCE_DIR}" "${source_file}") | ||
set(output_file "${CMAKE_CURRENT_BINARY_DIR}/clang_tidy/${source_file_relative}.clang") | ||
string(REPLACE ".." "__" output_file "${output_file}") | ||
get_filename_component(output_dir "${output_file}" DIRECTORY) | ||
file(MAKE_DIRECTORY "${output_dir}") | ||
|
||
add_custom_command( | ||
OUTPUT | ||
"${output_file}" | ||
COMMAND | ||
"${CMAKE_COMMAND}" | ||
-D "CLANG_TIDY=${CLANG_TIDY}" | ||
-D "INPUT_FILE=${source_file}" | ||
-D "OUTPUT_FILE=${output_file}" | ||
-P "${OpenVINODeveloperScripts_DIR}/clang_tidy/clang_tidy_check.cmake" | ||
DEPENDS | ||
"${source_file}" | ||
"${OpenVINODeveloperScripts_DIR}/clang_tidy/clang_tidy_check.cmake" | ||
COMMENT | ||
"[clang-tidy] ${source_file}" | ||
VERBATIM) | ||
|
||
list(APPEND all_input_sources "${source_file}") | ||
list(APPEND all_output_files "${output_file}") | ||
endforeach() | ||
|
||
add_custom_target(${TARGET_NAME}_check | ||
DEPENDS ${all_output_files} | ||
COMMENT "[clang-tidy] ${TARGET_NAME}_check") | ||
|
||
set_target_properties(${TARGET_NAME}_check | ||
PROPERTIES FOLDER clang_tidy) | ||
|
||
# if(CLANG_TIDY_FOR_TARGETS) | ||
# foreach(target IN LISTS CLANG_TIDY_FOR_TARGETS) | ||
# add_dependencies(${target} ${TARGET_NAME}) | ||
# endforeach() | ||
# endif() | ||
|
||
add_dependencies(clang_tidy_check_all ${TARGET_NAME}_check) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright (C) 2018-2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
file(REMOVE "${OUTPUT_FILE}") | ||
|
||
execute_process(COMMAND ${CLANG_TIDY} -p=${CMAKE_BINARY_DIR} ${INPUT_FILE} | ||
OUTPUT_VARIABLE STYLE_CHECK_RESULT RESULT_VARIABLE STYLE_CHECK_RETURN_CODE | ||
) | ||
|
||
if(NOT STYLE_CHECK_RETURN_CODE EQUAL 0) | ||
message(SEND_ERROR "Clang-tidy check failed for ${INPUT_FILE}") | ||
endif() | ||
|
||
file(WRITE "${OUTPUT_FILE}" "${STYLE_CHECK_RESULT}") | ||
|
||
if(NOT SKIP_RETURN_CODE) | ||
if("${STYLE_CHECK_RESULT}" MATCHES ".*<replacement .*") | ||
message(FATAL_ERROR "[clang-tidy] Code style check failed for: ${INPUT_FILE}") | ||
endif() | ||
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
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
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,81 @@ | ||
--- | ||
|
||
### NOTE: | ||
# The 'Checks: >' is a multiline string here. Comment must not be moved into the string. | ||
# | ||
### Scopes to be enabled: | ||
# | ||
# cppcoreguidelines-*, | ||
# google-*, | ||
# readability-*, | ||
# modernize-*, | ||
# bugprone-*, | ||
# misc-*, | ||
# | ||
### Checks that are turned off for a reason: | ||
# | ||
# -cppcoreguidelines-pro-bounds-pointer-arithmetic | ||
# -google-readability-todo. No big reason to enforce | ||
# -modernize-use-trailing-return-type. Just stylistic preference | ||
# -readability-identifier-length. A lot of code use short names for readability, i.e. 'B' for batch | ||
# -readability-uppercase-literal-suffix. | ||
# | ||
### Checks that are turned off but better be enabled: | ||
# -bugprone-narrowing-conversions | ||
# -bugprone-easily-swappable-parameters | ||
# -bugprone-fold-init-type | ||
# -bugprone-implicit-widening-of-multiplication-result | ||
# -cppcoreguidelines-narrowing-conversions | ||
# -google-readability-braces-around-statements | ||
# -readability-implicit-bool-conversion, | ||
# -readability-magic-numbers, cppcoreguidelines-avoid-magic-numbers | ||
# -readability-function-cognitive-complexity. Reasonable way to enforce splitting complex code into simple functions | ||
# -modernize-concat-nested-namespaces. More compact way when C++17 is available | ||
|
||
Checks: > | ||
-*, | ||
performance-*, | ||
-bugprone-easily-swappable-parameters, | ||
-bugprone-fold-init-type, | ||
-bugprone-implicit-widening-of-multiplication-result, | ||
-bugprone-narrowing-conversions, | ||
-cppcoreguidelines-narrowing-conversions, | ||
-cppcoreguidelines-pro-bounds-pointer-arithmetic, | ||
-google-build-using-namespace, | ||
-google-readability-todo, | ||
-readability-braces-around-statements, | ||
-google-readability-braces-around-statements, | ||
-modernize-use-trailing-return-type, | ||
-readability-identifier-length, | ||
-readability-implicit-bool-conversion, | ||
-readability-magic-numbers, | ||
-cppcoreguidelines-avoid-magic-numbers, | ||
-readability-uppercase-literal-suffix, | ||
-readability-function-cognitive-complexity, | ||
-modernize-concat-nested-namespaces, | ||
# Treat warnings as errors | ||
WarningsAsErrors: '*' | ||
# Use clang-format for applied fixes | ||
FormatStyle: file | ||
HeaderFilterRegex: '' | ||
CheckOptions: | ||
- key: cppcoreguidelines-avoid-do-while.IgnoreMacros | ||
value: true | ||
# matches with corresponding cpplink check | ||
- key: google-readability-namespace-comments.ShortNamespaceLines | ||
value: "10" | ||
# matches with corresponding cpplink check | ||
- key: google-readability-namespace-comments.SpacesBeforeComments | ||
value: "2" | ||
- key: modernize-loop-convert.MinConfidence | ||
value: reasonable | ||
- key: modernize-pass-by-value.IncludeStyle | ||
value: google | ||
### To be considered to enable: | ||
# # Unifies the usage of the statements | ||
# - key: readability-braces-around-statements.ShortStatementLines | ||
# value: "1" | ||
# Reasonable way to enforce splitting complex code into simple functions | ||
# - key: google-readability-function-size.StatementThreshold | ||
# value: "800" | ||
--- |