Skip to content

Commit 24c8728

Browse files
author
Chao Liu
committed
add tidy
1 parent 61487e0 commit 24c8728

File tree

8 files changed

+955
-4
lines changed

8 files changed

+955
-4
lines changed

.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CheckOptions:
2+
- key: bugprone-reserved-identifier.AllowedIdentifiers
3+
value: '__HIP_PLATFORM_HCC__;__HIP_ROCclr__'

CMakeLists.txt

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
cmake_minimum_required(VERSION 2.8.3)
1+
cmake_minimum_required(VERSION 3.5)
22
project(composable_kernel)
33

44
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
55

6+
include(CheckCXXCompilerFlag)
7+
68
## C++
79
enable_language(CXX)
810
set(CMAKE_CXX_STANDARD 17)
@@ -36,4 +38,160 @@ link_libraries(${OpenMP_pthread_LIBRARY})
3638
find_package(HIP REQUIRED)
3739
message(STATUS "Build with HIP ${hip_VERSION}")
3840

41+
## tidy
42+
include(EnableCompilerWarnings)
43+
set(MIOPEN_TIDY_ERRORS ERRORS * -readability-inconsistent-declaration-parameter-name)
44+
if(CMAKE_CXX_COMPILER MATCHES ".*hcc" OR CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+")
45+
set(MIOPEN_TIDY_CHECKS -modernize-use-override -readability-non-const-parameter)
46+
# Enable tidy on hip
47+
elseif(MIOPEN_BACKEND STREQUAL "HIP" OR MIOPEN_BACKEND STREQUAL "HIPNOGPU")
48+
set(MIOPEN_TIDY_ERRORS ALL)
49+
50+
endif()
51+
52+
include(ClangTidy)
53+
enable_clang_tidy(
54+
CHECKS
55+
*
56+
-abseil-*
57+
-android-cloexec-fopen
58+
# Yea we shouldn't be using rand()
59+
-cert-msc30-c
60+
-bugprone-exception-escape
61+
-bugprone-macro-parentheses
62+
-cert-env33-c
63+
-cert-msc32-c
64+
-cert-msc50-cpp
65+
-cert-msc51-cpp
66+
-cert-dcl37-c
67+
-cert-dcl51-cpp
68+
-clang-analyzer-alpha.core.CastToStruct
69+
-clang-analyzer-optin.performance.Padding
70+
-clang-diagnostic-deprecated-declarations
71+
-clang-diagnostic-extern-c-compat
72+
-clang-diagnostic-unused-command-line-argument
73+
-cppcoreguidelines-avoid-c-arrays
74+
-cppcoreguidelines-avoid-magic-numbers
75+
-cppcoreguidelines-explicit-virtual-functions
76+
-cppcoreguidelines-init-variables
77+
-cppcoreguidelines-macro-usage
78+
-cppcoreguidelines-non-private-member-variables-in-classes
79+
-cppcoreguidelines-pro-bounds-array-to-pointer-decay
80+
-cppcoreguidelines-pro-bounds-constant-array-index
81+
-cppcoreguidelines-pro-bounds-pointer-arithmetic
82+
-cppcoreguidelines-pro-type-member-init
83+
-cppcoreguidelines-pro-type-reinterpret-cast
84+
-cppcoreguidelines-pro-type-union-access
85+
-cppcoreguidelines-pro-type-vararg
86+
-cppcoreguidelines-special-member-functions
87+
-fuchsia-*
88+
-google-explicit-constructor
89+
-google-readability-braces-around-statements
90+
-google-readability-todo
91+
-google-runtime-int
92+
-google-runtime-references
93+
-hicpp-vararg
94+
-hicpp-braces-around-statements
95+
-hicpp-explicit-conversions
96+
-hicpp-named-parameter
97+
-hicpp-no-array-decay
98+
# We really shouldn't use bitwise operators with signed integers, but
99+
# opencl leaves us no choice
100+
-hicpp-avoid-c-arrays
101+
-hicpp-signed-bitwise
102+
-hicpp-special-member-functions
103+
-hicpp-uppercase-literal-suffix
104+
-hicpp-use-auto
105+
-hicpp-use-equals-default
106+
-hicpp-use-override
107+
-llvm-header-guard
108+
-llvm-include-order
109+
#-llvmlibc-*
110+
-llvmlibc-restrict-system-libc-headers
111+
-llvmlibc-callee-namespace
112+
-llvmlibc-implementation-in-namespace
113+
-llvm-else-after-return
114+
-llvm-qualified-auto
115+
-misc-misplaced-const
116+
-misc-non-private-member-variables-in-classes
117+
-misc-no-recursion
118+
-modernize-avoid-bind
119+
-modernize-avoid-c-arrays
120+
-modernize-pass-by-value
121+
-modernize-use-auto
122+
-modernize-use-default-member-init
123+
-modernize-use-equals-default
124+
-modernize-use-trailing-return-type
125+
-modernize-use-transparent-functors
126+
-performance-unnecessary-value-param
127+
-readability-braces-around-statements
128+
-readability-else-after-return
129+
# we are not ready to use it, but very useful
130+
-readability-function-cognitive-complexity
131+
-readability-isolate-declaration
132+
-readability-magic-numbers
133+
-readability-named-parameter
134+
-readability-uppercase-literal-suffix
135+
-readability-convert-member-functions-to-static
136+
-readability-qualified-auto
137+
-readability-redundant-string-init
138+
# too many narrowing conversions in our code
139+
-bugprone-narrowing-conversions
140+
-cppcoreguidelines-narrowing-conversions
141+
-altera-struct-pack-align
142+
-cppcoreguidelines-prefer-member-initializer
143+
144+
${MIOPEN_TIDY_CHECKS}
145+
${MIOPEN_TIDY_ERRORS}
146+
HEADER_FILTER
147+
"\.hpp$"
148+
EXTRA_ARGS
149+
-DMIOPEN_USE_CLANG_TIDY
150+
151+
)
152+
153+
include(CppCheck)
154+
enable_cppcheck(
155+
CHECKS
156+
warning
157+
style
158+
performance
159+
portability
160+
SUPPRESS
161+
ConfigurationNotChecked
162+
constStatement
163+
duplicateCondition
164+
noExplicitConstructor
165+
passedByValue
166+
# preprocessorErrorDirective
167+
shadowVariable
168+
unusedFunction
169+
unusedPrivateFunction
170+
unusedStructMember
171+
# Ignore initializer lists in the tests
172+
useInitializationList:*test/*.cpp
173+
*:*src/sqlite/*.cpp
174+
*:*.cl
175+
*:*src/kernels/*.h
176+
knownConditionTrueFalse:*src/kernels/composable_kernel/*/*
177+
redundantAssignment:*src/kernels/composable_kernel/*/*
178+
unreadVariable:*src/kernels/composable_kernel/*/*
179+
unusedScopedObject:*src/kernels/composable_kernel/*/*
180+
wrongPrintfScanfArgNum:*src/kernels/composable_kernel/*/*
181+
unmatchedSuppression
182+
FORCE
183+
SOURCES
184+
host/host_tensor/src
185+
host/driver_offline
186+
composable_kernel/src
187+
INCLUDE
188+
${CMAKE_CURRENT_SOURCE_DIR}/include
189+
${CMAKE_CURRENT_BINARY_DIR}/include
190+
${CMAKE_CURRENT_SOURCE_DIR}/src/include
191+
DEFINE
192+
CPPCHECK=1
193+
MIOPEN_USE_MIOPENGEMM=1
194+
__linux__=1
195+
)
196+
39197
add_subdirectory(host)

cmake/Analyzers.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
################################################################################
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2017 Advanced Micro Devices, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
################################################################################
26+
27+
if(NOT TARGET analyze)
28+
add_custom_target(analyze)
29+
endif()
30+
31+
function(mark_as_analyzer)
32+
add_dependencies(analyze ${ARGN})
33+
endfunction()
34+

cmake/ClangTidy.cmake

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
################################################################################
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2017 Advanced Micro Devices, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
################################################################################
26+
include(CMakeParseArguments)
27+
include(Analyzers)
28+
29+
get_filename_component(CLANG_TIDY_EXE_HINT "${CMAKE_CXX_COMPILER}" PATH)
30+
31+
find_program(CLANG_TIDY_EXE
32+
NAMES
33+
clang-tidy
34+
clang-tidy-5.0
35+
clang-tidy-4.0
36+
clang-tidy-3.9
37+
clang-tidy-3.8
38+
clang-tidy-3.7
39+
clang-tidy-3.6
40+
clang-tidy-3.5
41+
HINTS
42+
${CLANG_TIDY_EXE_HINT}
43+
PATH_SUFFIXES
44+
compiler/bin
45+
PATHS
46+
/opt/rocm/llvm/bin
47+
/opt/rocm/hcc
48+
/usr/local/opt/llvm/bin
49+
)
50+
51+
function(find_clang_tidy_version VAR)
52+
execute_process(COMMAND ${CLANG_TIDY_EXE} -version OUTPUT_VARIABLE VERSION_OUTPUT)
53+
separate_arguments(VERSION_OUTPUT_LIST UNIX_COMMAND "${VERSION_OUTPUT}")
54+
list(FIND VERSION_OUTPUT_LIST "version" VERSION_INDEX)
55+
if(VERSION_INDEX GREATER 0)
56+
math(EXPR VERSION_INDEX "${VERSION_INDEX} + 1")
57+
list(GET VERSION_OUTPUT_LIST ${VERSION_INDEX} VERSION)
58+
set(${VAR} ${VERSION} PARENT_SCOPE)
59+
else()
60+
set(${VAR} "0.0" PARENT_SCOPE)
61+
endif()
62+
63+
endfunction()
64+
65+
if( NOT CLANG_TIDY_EXE )
66+
message( STATUS "Clang tidy not found" )
67+
set(CLANG_TIDY_VERSION "0.0")
68+
else()
69+
find_clang_tidy_version(CLANG_TIDY_VERSION)
70+
message( STATUS "Clang tidy found: ${CLANG_TIDY_VERSION}")
71+
endif()
72+
73+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
74+
75+
set(CLANG_TIDY_FIXIT_DIR ${CMAKE_BINARY_DIR}/fixits)
76+
file(MAKE_DIRECTORY ${CLANG_TIDY_FIXIT_DIR})
77+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${CLANG_TIDY_FIXIT_DIR})
78+
79+
macro(enable_clang_tidy)
80+
set(options ANALYZE_TEMPORARY_DTORS ALL)
81+
set(oneValueArgs HEADER_FILTER)
82+
set(multiValueArgs CHECKS ERRORS EXTRA_ARGS)
83+
84+
cmake_parse_arguments(PARSE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
85+
string(REPLACE ";" "," CLANG_TIDY_CHECKS "${PARSE_CHECKS}")
86+
string(REPLACE ";" "," CLANG_TIDY_ERRORS "${PARSE_ERRORS}")
87+
set(CLANG_TIDY_EXTRA_ARGS)
88+
foreach(ARG ${PARSE_EXTRA_ARGS})
89+
list(APPEND CLANG_TIDY_EXTRA_ARGS "-extra-arg=${ARG}")
90+
endforeach()
91+
92+
set(CLANG_TIDY_ALL)
93+
if(PARSE_ALL)
94+
set(CLANG_TIDY_ALL ALL)
95+
endif()
96+
97+
message(STATUS "Clang tidy checks: ${CLANG_TIDY_CHECKS}")
98+
99+
if (${PARSE_ANALYZE_TEMPORARY_DTORS})
100+
set(CLANG_TIDY_ANALYZE_TEMPORARY_DTORS "-analyze-temporary-dtors")
101+
endif()
102+
103+
if (${CLANG_TIDY_VERSION} VERSION_LESS "3.9.0")
104+
set(CLANG_TIDY_ERRORS_ARG "")
105+
else()
106+
set(CLANG_TIDY_ERRORS_ARG "-warnings-as-errors='${CLANG_TIDY_ERRORS}'")
107+
endif()
108+
109+
if (${CLANG_TIDY_VERSION} VERSION_LESS "3.9.0")
110+
set(CLANG_TIDY_QUIET_ARG "")
111+
else()
112+
set(CLANG_TIDY_QUIET_ARG "-quiet")
113+
endif()
114+
115+
if(PARSE_HEADER_FILTER)
116+
string(REPLACE "$" "$$" CLANG_TIDY_HEADER_FILTER "${PARSE_HEADER_FILTER}")
117+
else()
118+
set(CLANG_TIDY_HEADER_FILTER ".*")
119+
endif()
120+
121+
set(CLANG_TIDY_COMMAND
122+
${CLANG_TIDY_EXE}
123+
${CLANG_TIDY_QUIET_ARG}
124+
-p ${CMAKE_BINARY_DIR}
125+
-checks='${CLANG_TIDY_CHECKS}'
126+
${CLANG_TIDY_ERRORS_ARG}
127+
${CLANG_TIDY_EXTRA_ARGS}
128+
${CLANG_TIDY_ANALYZE_TEMPORARY_DTORS}
129+
-header-filter='${CLANG_TIDY_HEADER_FILTER}'
130+
)
131+
add_custom_target(tidy ${CLANG_TIDY_ALL})
132+
mark_as_analyzer(tidy)
133+
add_custom_target(tidy-base)
134+
add_custom_target(tidy-make-fixit-dir COMMAND ${CMAKE_COMMAND} -E make_directory ${CLANG_TIDY_FIXIT_DIR})
135+
add_custom_target(tidy-rm-fixit-dir COMMAND ${CMAKE_COMMAND} -E remove_directory ${CLANG_TIDY_FIXIT_DIR})
136+
add_dependencies(tidy-make-fixit-dir tidy-rm-fixit-dir)
137+
add_dependencies(tidy-base tidy-make-fixit-dir)
138+
endmacro()
139+
140+
function(clang_tidy_check TARGET)
141+
get_target_property(SOURCES ${TARGET} SOURCES)
142+
# TODO: Use generator expressions instead
143+
# COMMAND ${CLANG_TIDY_COMMAND} $<TARGET_PROPERTY:${TARGET},SOURCES>
144+
# COMMAND ${CLANG_TIDY_COMMAND} $<JOIN:$<TARGET_PROPERTY:${TARGET},SOURCES>, >
145+
foreach(SOURCE ${SOURCES})
146+
if((NOT "${SOURCE}" MATCHES "(h|hpp|hxx)$") AND (NOT "${SOURCE}" MATCHES "TARGET_OBJECTS"))
147+
string(MAKE_C_IDENTIFIER "${SOURCE}" tidy_file)
148+
set(tidy_target tidy-target-${TARGET}-${tidy_file})
149+
add_custom_target(${tidy_target}
150+
# for some targets clang-tidy not able to get information from .clang-tidy
151+
DEPENDS ${SOURCE}
152+
COMMAND ${CLANG_TIDY_COMMAND} "-config=\{CheckOptions: \[\{key: bugprone-reserved-identifier.AllowedIdentifiers,value: __HIP_PLATFORM_HCC__\; __HIP_ROCclr__\}\]\}" ${SOURCE} "-export-fixes=${CLANG_TIDY_FIXIT_DIR}/${TARGET}-${tidy_file}.yaml"
153+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
154+
COMMENT "clang-tidy: Running clang-tidy on target ${SOURCE}..."
155+
)
156+
add_dependencies(${tidy_target} ${TARGET})
157+
add_dependencies(${tidy_target} tidy-base)
158+
add_dependencies(tidy ${tidy_target})
159+
endif()
160+
endforeach()
161+
endfunction()
162+

0 commit comments

Comments
 (0)