From b480315e0c229454c335290b19cc689930d7849f Mon Sep 17 00:00:00 2001 From: Orkun Tokdemir Date: Fri, 12 May 2023 16:49:03 +0200 Subject: TargetGenerator: Add SKIP_LINTING source property The `SKIP_LINTING` source property was added to disable code check for desired source files. The `SKIP_LINTING`includes `cpplint`, `clang-tidy`, \ `cppcheck` and `include-what-you-use`. If `SKIP_LINTING` is set on a source file, the tools mentioned above will not be run on that source file. --- Auxiliary/vim/syntax/cmake.vim | 1 + Help/manual/cmake-properties.7.rst | 1 + Help/prop_sf/SKIP_LINTING.rst | 41 ++++++++++++++++++++++ Help/prop_tgt/LANG_CLANG_TIDY.rst | 7 ++++ Help/prop_tgt/LANG_CPPCHECK.rst | 7 ++++ Help/prop_tgt/LANG_CPPLINT.rst | 7 ++++ Help/prop_tgt/LANG_INCLUDE_WHAT_YOU_USE.rst | 7 ++++ Help/release/dev/skip-linting.rst | 5 +++ Source/cmMakefileTargetGenerator.cxx | 11 +++--- Source/cmNinjaTargetGenerator.cxx | 18 ++++++---- .../MultiLint/C-launch_skip_linting_ON.cmake | 3 ++ Tests/RunCMake/MultiLint/CMakeLists copy.txt | 3 ++ .../MultiLint/CXX-launch_skip_linting_ON.cmake | 3 ++ .../CXX_skip_linting_OFF-Build-result.txt | 1 + .../RunCMake/MultiLint/CXX_skip_linting_OFF.cmake | 7 ++++ Tests/RunCMake/MultiLint/CXX_skip_linting_ON.cmake | 7 ++++ .../MultiLint/C_skip_linting_OFF-Build-result.txt | 1 + Tests/RunCMake/MultiLint/C_skip_linting_OFF.cmake | 7 ++++ Tests/RunCMake/MultiLint/C_skip_linting_ON.cmake | 7 ++++ Tests/RunCMake/MultiLint/RunCMakeTest.cmake | 19 ++++++++++ 20 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 Help/prop_sf/SKIP_LINTING.rst create mode 100644 Help/release/dev/skip-linting.rst create mode 100644 Tests/RunCMake/MultiLint/C-launch_skip_linting_ON.cmake create mode 100644 Tests/RunCMake/MultiLint/CMakeLists copy.txt create mode 100644 Tests/RunCMake/MultiLint/CXX-launch_skip_linting_ON.cmake create mode 100644 Tests/RunCMake/MultiLint/CXX_skip_linting_OFF-Build-result.txt create mode 100644 Tests/RunCMake/MultiLint/CXX_skip_linting_OFF.cmake create mode 100644 Tests/RunCMake/MultiLint/CXX_skip_linting_ON.cmake create mode 100644 Tests/RunCMake/MultiLint/C_skip_linting_OFF-Build-result.txt create mode 100644 Tests/RunCMake/MultiLint/C_skip_linting_OFF.cmake create mode 100644 Tests/RunCMake/MultiLint/C_skip_linting_ON.cmake diff --git a/Auxiliary/vim/syntax/cmake.vim b/Auxiliary/vim/syntax/cmake.vim index aefdcee..2e83a07 100644 --- a/Auxiliary/vim/syntax/cmake.vim +++ b/Auxiliary/vim/syntax/cmake.vim @@ -329,6 +329,7 @@ syn keyword cmakeProperty contained \ SKIP_REGULAR_EXPRESSION \ SKIP_RETURN_CODE \ SKIP_UNITY_BUILD_INCLUSION + \ SKIP_LINTING \ SOURCES \ SOURCE_DIR \ SOVERSION diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst index 8ee5573..b8026ef 100644 --- a/Help/manual/cmake-properties.7.rst +++ b/Help/manual/cmake-properties.7.rst @@ -559,6 +559,7 @@ Properties on Source Files /prop_sf/SKIP_AUTOUIC /prop_sf/SKIP_PRECOMPILE_HEADERS /prop_sf/SKIP_UNITY_BUILD_INCLUSION + /prop_sf/SKIP_LINTING /prop_sf/Swift_DEPENDENCIES_FILE /prop_sf/Swift_DIAGNOSTICS_FILE /prop_sf/SYMBOLIC diff --git a/Help/prop_sf/SKIP_LINTING.rst b/Help/prop_sf/SKIP_LINTING.rst new file mode 100644 index 0000000..2be47cb --- /dev/null +++ b/Help/prop_sf/SKIP_LINTING.rst @@ -0,0 +1,41 @@ +SKIP_LINTING +------------ + +.. versionadded:: 3.27 + +This property allows you to exclude a specific source file +from the linting process. The linting process involves running +tools such as :prop_tgt:`_CPPLINT`, :prop_tgt:`_CLANG_TIDY`, +:prop_tgt:`_CPPCHECK`, and :prop_tgt:`_INCLUDE_WHAT_YOU_USE` +on the source files. By setting `SKIP_LINTING` on a source file, +the mentioned linting tools will not be executed for that +particular file. + +EXAMPLE +^^^^^^^ + +Consider a `C++` project that includes multiple source files, +such as `main.cpp`, `things.cpp`, and `generatedBindings.cpp`. +In this example, you want to exclude the `generatedBindings.cpp` +file from the linting process. To achieve this, you can utilize +the `SKIP_LINTING` property with the `set_source_files_properties` +command as shown below: + +.. code-block:: cmake + + add_executable(MyApp main.cpp things.cpp generatedBindings.cpp) + + set_source_files_properties(generatedBindings.cpp PROPERTIES + SKIP_LINTING ON + ) + +In the provided code snippet, the `SKIP_LINTING` property is set to `ON` +for the `generatedBindings.cpp` source file. As a result, when the linting +tools, such as :prop_tgt:`_CPPLINT`, :prop_tgt:`_CLANG_TIDY`, +:prop_tgt:`_CPPCHECK`, and :prop_tgt:`_INCLUDE_WHAT_YOU_USE`, +are executed, they will skip analyzing the `generatedBindings.cpp` file. + +By using the `SKIP_LINTING` property, you can selectively exclude specific +source files from the linting process. This allows you to focus the +linting tools on the relevant parts of your project, enhancing the efficiency +and effectiveness of the linting workflow. diff --git a/Help/prop_tgt/LANG_CLANG_TIDY.rst b/Help/prop_tgt/LANG_CLANG_TIDY.rst index 1e10933..0e46643 100644 --- a/Help/prop_tgt/LANG_CLANG_TIDY.rst +++ b/Help/prop_tgt/LANG_CLANG_TIDY.rst @@ -30,3 +30,10 @@ when a target is created. This property supports :manual:`generator expressions `. + + :prop_sf:`SKIP_LINTING` can be set on individual source files to exclude + them from the linting process, which includes tools like + :prop_tgt:`_CPPLINT`, :prop_tgt:`_CLANG_TIDY`, + :prop_tgt:`_CPPCHECK`, and :prop_tgt:`_INCLUDE_WHAT_YOU_USE`. + When :prop_sf:`SKIP_LINTING` is set on a source file, the mentioned tools + will not be run on that specific file. diff --git a/Help/prop_tgt/LANG_CPPCHECK.rst b/Help/prop_tgt/LANG_CPPCHECK.rst index 0b2dee6..3ab2b58 100644 --- a/Help/prop_tgt/LANG_CPPCHECK.rst +++ b/Help/prop_tgt/LANG_CPPCHECK.rst @@ -20,3 +20,10 @@ created. This property supports :manual:`generator expressions `. + + :prop_sf:`SKIP_LINTING` can be set on individual source files to exclude + them from the linting process, which includes tools like + :prop_tgt:`_CPPLINT`, :prop_tgt:`_CLANG_TIDY`, + :prop_tgt:`_CPPCHECK`, and :prop_tgt:`_INCLUDE_WHAT_YOU_USE`. + When :prop_sf:`SKIP_LINTING` is set on a source file, the mentioned tools + will not be run on that specific file. diff --git a/Help/prop_tgt/LANG_CPPLINT.rst b/Help/prop_tgt/LANG_CPPLINT.rst index 38a1669..cb230d3 100644 --- a/Help/prop_tgt/LANG_CPPLINT.rst +++ b/Help/prop_tgt/LANG_CPPLINT.rst @@ -18,3 +18,10 @@ created. This property supports :manual:`generator expressions `. + + :prop_sf:`SKIP_LINTING` can be set on individual source files to exclude + them from the linting process, which includes tools like + :prop_tgt:`_CPPLINT`, :prop_tgt:`_CLANG_TIDY`, + :prop_tgt:`_CPPCHECK`, and :prop_tgt:`_INCLUDE_WHAT_YOU_USE`. + When :prop_sf:`SKIP_LINTING` is set on a source file, the mentioned tools + will not be run on that specific file. diff --git a/Help/prop_tgt/LANG_INCLUDE_WHAT_YOU_USE.rst b/Help/prop_tgt/LANG_INCLUDE_WHAT_YOU_USE.rst index 19b97f7..8fbe84a 100644 --- a/Help/prop_tgt/LANG_INCLUDE_WHAT_YOU_USE.rst +++ b/Help/prop_tgt/LANG_INCLUDE_WHAT_YOU_USE.rst @@ -18,3 +18,10 @@ when a target is created. This property supports :manual:`generator expressions `. + + :prop_sf:`SKIP_LINTING` can be set on individual source files to exclude + them from the linting process, which includes tools like + :prop_tgt:`_CPPLINT`, :prop_tgt:`_CLANG_TIDY`, + :prop_tgt:`_CPPCHECK`, and :prop_tgt:`_INCLUDE_WHAT_YOU_USE`. + When :prop_sf:`SKIP_LINTING` is set on a source file, the mentioned tools + will not be run on that specific file. diff --git a/Help/release/dev/skip-linting.rst b/Help/release/dev/skip-linting.rst new file mode 100644 index 0000000..199571c --- /dev/null +++ b/Help/release/dev/skip-linting.rst @@ -0,0 +1,5 @@ +skip-linting +------------ + +* The :prop_sf:`SKIP_LINTING` source file property was added to suppress + target-wide code checks on specific sources. diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 1cf3b8b..c915e26 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -1057,10 +1057,13 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles( compilerLauncher = GetCompilerLauncher(lang, config); } - std::string const codeCheck = this->GenerateCodeCheckRules( - source, compilerLauncher, "$(CMAKE_COMMAND)", config, nullptr); - if (!codeCheck.empty()) { - compileCommands.front().insert(0, codeCheck); + cmValue const skipCodeCheck = source.GetProperty("SKIP_LINTING"); + if (!skipCodeCheck.IsOn()) { + std::string const codeCheck = this->GenerateCodeCheckRules( + source, compilerLauncher, "$(CMAKE_COMMAND)", config, nullptr); + if (!codeCheck.empty()) { + compileCommands.front().insert(0, codeCheck); + } } // If compiler launcher was specified and not consumed above, it diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index cad4bf4..25e00d3 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -1219,15 +1219,19 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement( vars["FLAGS"] = this->ComputeFlagsForObject(source, language, config); vars["DEFINES"] = this->ComputeDefines(source, language, config); vars["INCLUDES"] = this->ComputeIncludes(source, language, config); - auto const cmakeCmd = this->GetLocalGenerator()->ConvertToOutputFormat( - cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL); auto compilerLauncher = this->GetCompilerLauncher(language, config); - vars["CODE_CHECK"] = - this->GenerateCodeCheckRules(*source, compilerLauncher, cmakeCmd, config, - [this](std::string const& path) { - return this->ConvertToNinjaPath(path); - }); + + cmValue const skipCodeCheck = source->GetProperty("SKIP_LINTING"); + if (!skipCodeCheck.IsOn()) { + auto const cmakeCmd = this->GetLocalGenerator()->ConvertToOutputFormat( + cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL); + vars["CODE_CHECK"] = + this->GenerateCodeCheckRules(*source, compilerLauncher, cmakeCmd, config, + [this](const std::string& path) { + return this->ConvertToNinjaPath(path); + }); + } // If compiler launcher was specified and not consumed above, it // goes to the beginning of the command line. diff --git a/Tests/RunCMake/MultiLint/C-launch_skip_linting_ON.cmake b/Tests/RunCMake/MultiLint/C-launch_skip_linting_ON.cmake new file mode 100644 index 0000000..d0d9866 --- /dev/null +++ b/Tests/RunCMake/MultiLint/C-launch_skip_linting_ON.cmake @@ -0,0 +1,3 @@ +set(CTEST_USE_LAUNCHERS 1) +include(CTestUseLaunchers) +include(C_skip_linting_ON.cmake) diff --git a/Tests/RunCMake/MultiLint/CMakeLists copy.txt b/Tests/RunCMake/MultiLint/CMakeLists copy.txt new file mode 100644 index 0000000..93ee9df --- /dev/null +++ b/Tests/RunCMake/MultiLint/CMakeLists copy.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.5) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/MultiLint/CXX-launch_skip_linting_ON.cmake b/Tests/RunCMake/MultiLint/CXX-launch_skip_linting_ON.cmake new file mode 100644 index 0000000..6347e60 --- /dev/null +++ b/Tests/RunCMake/MultiLint/CXX-launch_skip_linting_ON.cmake @@ -0,0 +1,3 @@ +set(CTEST_USE_LAUNCHERS 1) +include(CTestUseLaunchers) +include(CXX_skip_linting_ON.cmake) diff --git a/Tests/RunCMake/MultiLint/CXX_skip_linting_OFF-Build-result.txt b/Tests/RunCMake/MultiLint/CXX_skip_linting_OFF-Build-result.txt new file mode 100644 index 0000000..3beecb0 --- /dev/null +++ b/Tests/RunCMake/MultiLint/CXX_skip_linting_OFF-Build-result.txt @@ -0,0 +1 @@ +(1|2) diff --git a/Tests/RunCMake/MultiLint/CXX_skip_linting_OFF.cmake b/Tests/RunCMake/MultiLint/CXX_skip_linting_OFF.cmake new file mode 100644 index 0000000..a0311a6 --- /dev/null +++ b/Tests/RunCMake/MultiLint/CXX_skip_linting_OFF.cmake @@ -0,0 +1,7 @@ +enable_language(CXX) +set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "$<1:${PSEUDO_IWYU}>" -some -args) +set(CMAKE_CXX_CLANG_TIDY "$<1:${PSEUDO_TIDY}>" -bad) +set(CMAKE_CXX_CPPLINT "$<1:${PSEUDO_CPPLINT}>" --error) +set(CMAKE_CXX_CPPCHECK "$<1:${PSEUDO_CPPCHECK}>" -error) +add_executable(main main.cxx) +set_source_files_properties(main.cxx PROPERTIES SKIP_LINTING OFF) diff --git a/Tests/RunCMake/MultiLint/CXX_skip_linting_ON.cmake b/Tests/RunCMake/MultiLint/CXX_skip_linting_ON.cmake new file mode 100644 index 0000000..39cfe87 --- /dev/null +++ b/Tests/RunCMake/MultiLint/CXX_skip_linting_ON.cmake @@ -0,0 +1,7 @@ +enable_language(CXX) +set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "$<1:${PSEUDO_IWYU}>" -some -args) +set(CMAKE_CXX_CLANG_TIDY "$<1:${PSEUDO_TIDY}>" -bad) +set(CMAKE_CXX_CPPLINT "$<1:${PSEUDO_CPPLINT}>" --error) +set(CMAKE_CXX_CPPCHECK "$<1:${PSEUDO_CPPCHECK}>" -error) +add_executable(main main.cxx) +set_source_files_properties(main.cxx PROPERTIES SKIP_LINTING ON) diff --git a/Tests/RunCMake/MultiLint/C_skip_linting_OFF-Build-result.txt b/Tests/RunCMake/MultiLint/C_skip_linting_OFF-Build-result.txt new file mode 100644 index 0000000..3beecb0 --- /dev/null +++ b/Tests/RunCMake/MultiLint/C_skip_linting_OFF-Build-result.txt @@ -0,0 +1 @@ +(1|2) diff --git a/Tests/RunCMake/MultiLint/C_skip_linting_OFF.cmake b/Tests/RunCMake/MultiLint/C_skip_linting_OFF.cmake new file mode 100644 index 0000000..2968a21 --- /dev/null +++ b/Tests/RunCMake/MultiLint/C_skip_linting_OFF.cmake @@ -0,0 +1,7 @@ +enable_language(C) +set(CMAKE_C_INCLUDE_WHAT_YOU_USE "${PSEUDO_IWYU}" -some -args) +set(CMAKE_C_CLANG_TIDY "${PSEUDO_TIDY}" -bad) +set(CMAKE_C_CPPLINT "${PSEUDO_CPPLINT}" --error) +set(CMAKE_C_CPPCHECK "${PSEUDO_CPPCHECK}" -error) +add_executable(main main.c) +set_source_files_properties(main.c PROPERTIES SKIP_LINTING OFF) diff --git a/Tests/RunCMake/MultiLint/C_skip_linting_ON.cmake b/Tests/RunCMake/MultiLint/C_skip_linting_ON.cmake new file mode 100644 index 0000000..09fc761 --- /dev/null +++ b/Tests/RunCMake/MultiLint/C_skip_linting_ON.cmake @@ -0,0 +1,7 @@ +enable_language(C) +set(CMAKE_C_INCLUDE_WHAT_YOU_USE "${PSEUDO_IWYU}" -some -args) +set(CMAKE_C_CLANG_TIDY "${PSEUDO_TIDY}" -bad) +set(CMAKE_C_CPPLINT "${PSEUDO_CPPLINT}" --error) +set(CMAKE_C_CPPCHECK "${PSEUDO_CPPCHECK}" -error) +add_executable(main main.c) +set_source_files_properties(main.c PROPERTIES SKIP_LINTING ON) diff --git a/Tests/RunCMake/MultiLint/RunCMakeTest.cmake b/Tests/RunCMake/MultiLint/RunCMakeTest.cmake index afd98fd..9b7a6a9 100644 --- a/Tests/RunCMake/MultiLint/RunCMakeTest.cmake +++ b/Tests/RunCMake/MultiLint/RunCMakeTest.cmake @@ -25,3 +25,22 @@ if(NOT RunCMake_GENERATOR STREQUAL "Watcom WMake") run_multilint(C-launch) run_multilint(CXX-launch) endif() + +function(run_skip_linting test_name) + set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/${test_name}-build") + set(RunCMake_TEST_NO_CLEAN 1) + + run_cmake(${test_name}) + set(RunCMake_TEST_OUTPUT_MERGE 1) + run_cmake_command(${test_name}-Build ${CMAKE_COMMAND} --build .) +endfunction() + +run_skip_linting(C_skip_linting_ON) +run_skip_linting(CXX_skip_linting_ON) +run_skip_linting(C_skip_linting_OFF) +run_skip_linting(CXX_skip_linting_OFF) + +if(NOT RunCMake_GENERATOR STREQUAL "Watcom WMake") + run_skip_linting(C-launch_skip_linting_ON) + run_skip_linting(CXX-launch_skip_linting_ON) +endif() -- cgit v0.12