From 8dfb9554b8941ff1cd466af634591f92bf2bfb79 Mon Sep 17 00:00:00 2001 From: Jason Mobarak Date: Fri, 15 Apr 2022 18:46:10 -0700 Subject: [PATCH 1/6] write clang-tidy to build dir --- ClangTidy.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ClangTidy.cmake b/ClangTidy.cmake index 154169e..ccf769d 100644 --- a/ClangTidy.cmake +++ b/ClangTidy.cmake @@ -166,7 +166,7 @@ function(swift_create_clang_tidy_targets) string(REPLACE ";" "," comma_checks "${all_checks}") # Generate .clang-tidy in project root dir - file(WRITE ${CMAKE_SOURCE_DIR}/.clang-tidy "\ + file(WRITE ${CMAKE_BINARY_DIR}/.clang-tidy "\ # Automatically generated, do not edit # Enabled checks are generated from SwiftClangTidy.cmake Checks: \"${comma_checks}\" From fe45c2c1282c3fd8f22eb651560abc0e22d56bbf Mon Sep 17 00:00:00 2001 From: Jason Mobarak Date: Tue, 19 Apr 2022 16:27:09 -0700 Subject: [PATCH 2/6] port clang-tidy enable/disable from legacy module --- ClangTidy.cmake | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ClangTidy.cmake b/ClangTidy.cmake index ccf769d..3c4b7c4 100644 --- a/ClangTidy.cmake +++ b/ClangTidy.cmake @@ -68,6 +68,23 @@ function(swift_create_clang_tidy_targets) message(FATAL_ERROR "Unparsed arguments to swift_create_clang_tidy_targets ${ARGN}") endif() + # Global clang-tidy enable option, influences the default project specific enable option + option(ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy globally" ON) + if(NOT ENABLE_CLANG_TIDY) + early_exit(STATUS "auto-linting is disabled globally") + endif() + + # Create a cmake option to enable linting of this specific project + if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME}) + option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project ${PROJECT_NAME}" ON) + else() + option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project ${PROJECT_NAME}" OFF) + endif() + + if(NOT ${PROJECT_NAME}_ENABLE_CLANG_TIDY) + early_exit(STATUS "${PROJECT_NAME} clang-tidy support is DISABLED") + endif() + # This is required so that clang-tidy can work out what compiler options to use for each file set(CMAKE_EXPORT_COMPILE_COMMANDS ON From 35975f8e808b02856027229d07cfde3fe7c63ea3 Mon Sep 17 00:00:00 2001 From: Jason Mobarak Date: Tue, 19 Apr 2022 16:27:35 -0700 Subject: [PATCH 3/6] Revert "write clang-tidy to build dir" This reverts commit 8dfb9554b8941ff1cd466af634591f92bf2bfb79. --- ClangTidy.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ClangTidy.cmake b/ClangTidy.cmake index 3c4b7c4..cd93d97 100644 --- a/ClangTidy.cmake +++ b/ClangTidy.cmake @@ -183,7 +183,7 @@ function(swift_create_clang_tidy_targets) string(REPLACE ";" "," comma_checks "${all_checks}") # Generate .clang-tidy in project root dir - file(WRITE ${CMAKE_BINARY_DIR}/.clang-tidy "\ + file(WRITE ${CMAKE_SOURCE_DIR}/.clang-tidy "\ # Automatically generated, do not edit # Enabled checks are generated from SwiftClangTidy.cmake Checks: \"${comma_checks}\" From 1a3414d9eb384c44c2d5005ef822987c422d972b Mon Sep 17 00:00:00 2001 From: Jason Mobarak Date: Tue, 19 Apr 2022 16:44:25 -0700 Subject: [PATCH 4/6] update docs --- ClangTidy.cmake | 59 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/ClangTidy.cmake b/ClangTidy.cmake index cd93d97..03a57e3 100644 --- a/ClangTidy.cmake +++ b/ClangTidy.cmake @@ -13,31 +13,60 @@ # # A module to create custom targets to lint source files using clang-tidy # -# Call swift_create_clang_tidy_targets at the end of the top level CMakeLists.txt to create targets for running clang-tidy +# Call swift_create_clang_tidy_targets at the end of the top level +# CMakeLists.txt to create targets for running clang-tidy # -# clang-tidy version 6 must be available on the system for this module to work properly. If an appropriate clang-tidy can't be found no targets will be created and a warning will -# be logged +# clang-tidy version 6 must be available on the system for this module to work +# properly. If an appropriate clang-tidy can't be found no targets will be +# created and a warning will be logged # -# swift_create_clang_tidy_targets will only have an effect for top level projects. If called within a subproject it will return without taking any action +# swift_create_clang_tidy_targets will only have an effect for top level +# projects. If called within a subproject it will return without taking any +# action # -# For every compilable target defined within the calling repository two targets will be created. +# For every compilable target defined within the calling repository two targets +# will be created. # -# The first target 'clang-tidy-${target}' will invoke clang-tidy for all source files which make up the given target. It will export fixes to a file called 'fixes-${target}.yaml' -# in the top level project source directory. +# The first target 'clang-tidy-${target}' will invoke clang-tidy for all source +# files which make up the given target. It will export fixes to a file called +# 'fixes-${target}.yaml' in the top level project source directory. # -# The second target 'clang-tidy-${target}-check' will run clang-tidy as the target described above and then return an error code if any warning/errors were generated +# The second target 'clang-tidy-${target}-check' will run clang-tidy as the +# target described above and then return an error code if any warning/errors +# were generated # -# In addition there are two other targets created which lint multiple targets at the same time +# In addition there are two other targets created which lint multiple targets +# at the same time # -# clang-tidy-all runs clang-tidy on all "core" targets in the repository (targets which were added with swift_add_executable or swift_add_library) +# clang-tidy-all runs clang-tidy on all "core" targets in the repository +# (targets which were added with swift_add_executable or swift_add_library) # -# clang-tidy-world runs clang-tidy on all compilable targets in the repository including all test suites +# clang-tidy-world runs clang-tidy on all compilable targets in the repository +# including all test suites # -# clang-tidy-all and clang-tidy-world each have a "check" variant which returns an error code should any warning/errors be generated +# clang-tidy-all and clang-tidy-world each have a "check" variant which returns +# an error code should any warning/errors be generated # -# swift_create_clang_tidy_targets will generate a .clang-tidy file in the project source directory which contains the Swift master config for clang-tidy. There is no need for -# repositories to maintain their own version of .clang-tidy, it should be added to .gitignore in each repository to prevent being checked in. Pass the option -# DONT_GENERATE_CLANG_TIDY_CONFIG to disable the autogenerated config +# swift_create_clang_tidy_targets will generate a .clang-tidy file in the +# project source directory which contains the Swift master config for +# clang-tidy. There is no need for repositories to maintain their own version +# of .clang-tidy, it should be added to .gitignore in each repository to +# prevent being checked in. Pass the option DONT_GENERATE_CLANG_TIDY_CONFIG to +# disable the autogenerated config +# +# In addition this function sets up a cmake option which can be used to control +# whether the targets are created either on the command line or by a super project. +# The option has the name +# +# ${PROJECT_NAME}_ENABLE_CLANG_TIDY +# +# The default value is ON for top level projects, and OFF for any others. +# +# Running +# +# cmake -D_ENABLE_CLANG_TIDY=OFF .. +# +# will explicitly disable these targets from the command line at configure time # # Helper function to actually create the targets, not to be used outside this file From 87b9c9ba693aec4c6570ff5416d10835791fc4a0 Mon Sep 17 00:00:00 2001 From: Jason Mobarak Date: Wed, 20 Apr 2022 10:28:21 -0700 Subject: [PATCH 5/6] Update ClangTidy.cmake Co-authored-by: Isaac Torres --- ClangTidy.cmake | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ClangTidy.cmake b/ClangTidy.cmake index 03a57e3..3a42d5e 100644 --- a/ClangTidy.cmake +++ b/ClangTidy.cmake @@ -104,11 +104,7 @@ function(swift_create_clang_tidy_targets) endif() # Create a cmake option to enable linting of this specific project - if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME}) - option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project ${PROJECT_NAME}" ON) - else() - option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project ${PROJECT_NAME}" OFF) - endif() +option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project" ON) if(NOT ${PROJECT_NAME}_ENABLE_CLANG_TIDY) early_exit(STATUS "${PROJECT_NAME} clang-tidy support is DISABLED") From 7faa0fb6a9add5ee892ba45406ab1f7e0de0d01c Mon Sep 17 00:00:00 2001 From: Jason Mobarak Date: Wed, 20 Apr 2022 10:32:38 -0700 Subject: [PATCH 6/6] define early_exit macro --- ClangTidy.cmake | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ClangTidy.cmake b/ClangTidy.cmake index 3a42d5e..858db49 100644 --- a/ClangTidy.cmake +++ b/ClangTidy.cmake @@ -83,6 +83,11 @@ function(create_clang_tidy_targets key fixes) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) endfunction() +macro(early_exit level msg) + message(${level} "${msg}") + return() +endmacro() + function(swift_create_clang_tidy_targets) if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) return() @@ -100,11 +105,11 @@ function(swift_create_clang_tidy_targets) # Global clang-tidy enable option, influences the default project specific enable option option(ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy globally" ON) if(NOT ENABLE_CLANG_TIDY) - early_exit(STATUS "auto-linting is disabled globally") + early_exit(STATUS "clang-tidy is disabled globally") endif() # Create a cmake option to enable linting of this specific project -option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project" ON) + option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project" ON) if(NOT ${PROJECT_NAME}_ENABLE_CLANG_TIDY) early_exit(STATUS "${PROJECT_NAME} clang-tidy support is DISABLED")