From e32bee2533f758f3a3cf2073569fc0b3b2fc5243 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 22:39:03 +0000 Subject: [PATCH] test(tidy): check the fairmq-* checks with FileCheck - Add test/tidy/RunCheck.cmake, a small check_clang_tidy.py: run one check with --fix on a copy of the input, then match the diagnostics against CHECK-MESSAGES lines and the fixed file against CHECK-FIXES lines. Any other warning or error fails the test. - Register one test per test/tidy/fairmq-*.cxx file. The inputs use the real FairMQ headers via the usage requirements of the FairMQ target. - Skip the tests with a warning if FileCheck is not found. - Add the test for fairmq-modernize-nonnamespaced-types. Co-Authored-By: Claude Opus 5.5 --- test/CMakeLists.txt | 4 ++ test/tidy/CMakeLists.txt | 46 +++++++++++++ test/tidy/RunCheck.cmake | 68 +++++++++++++++++++ .../fairmq-modernize-nonnamespaced-types.cxx | 46 +++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 test/tidy/CMakeLists.txt create mode 100644 test/tidy/RunCheck.cmake create mode 100644 test/tidy/fairmq-modernize-nonnamespaced-types.cxx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9172d4b5..f16cd661 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -329,3 +329,7 @@ add_testsuite(MemoryResources ${definitions} ${environment} ) + +if(BUILD_TIDY_TOOL) + add_subdirectory(tidy) +endif() diff --git a/test/tidy/CMakeLists.txt b/test/tidy/CMakeLists.txt new file mode 100644 index 00000000..8095a5b1 --- /dev/null +++ b/test/tidy/CMakeLists.txt @@ -0,0 +1,46 @@ +################################################################################ +# Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # +# # +# This software is distributed under the terms of the # +# GNU Lesser General Public Licence (LGPL) version 3, # +# copied verbatim in the file "LICENSE" # +################################################################################ + +find_program(FAIRMQ_TIDY_FILECHECK NAMES FileCheck FileCheck-${LLVM_VERSION_MAJOR} + HINTS "${LLVM_TOOLS_BINARY_DIR}" + DOC "FileCheck executable for the fairmq-tidy tests" +) +if(NOT FAIRMQ_TIDY_FILECHECK) + message(WARNING "FileCheck not found (install the LLVM tools, e.g. " + "llvm-${LLVM_VERSION_MAJOR}-tools). Skipping the fairmq-tidy tests.") + return() +endif() + +# The test inputs include the real FairMQ headers. Pass the usage requirements +# of the FairMQ target to the clang frontend, one flag per line. +# Drop the default system include directory: a second copy in the search +# path breaks the #include_next chain of the C++ standard library headers. +set(incs "$") +set(incs "$>") +set(defs "$") +file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/flags.txt" CONTENT +"-std=c++17 +$<$:-isystem\n$> +$<$:-D$> +") + +file(GLOB inputs CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/fairmq-*.cxx") +foreach(input IN LISTS inputs) + get_filename_component(check "${input}" NAME_WE) + add_test(NAME "tidy.${check}" + COMMAND ${CMAKE_COMMAND} + "-DTIDY=$" + "-DFILECHECK=${FAIRMQ_TIDY_FILECHECK}" + "-DCHECK=${check}" + "-DINPUT=${input}" + "-DFLAGS=${CMAKE_CURRENT_BINARY_DIR}/flags.txt" + "-DWORKDIR=${CMAKE_CURRENT_BINARY_DIR}/${check}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/RunCheck.cmake" + ) + set_tests_properties("tidy.${check}" PROPERTIES TIMEOUT 120) +endforeach() diff --git a/test/tidy/RunCheck.cmake b/test/tidy/RunCheck.cmake new file mode 100644 index 00000000..5089f732 --- /dev/null +++ b/test/tidy/RunCheck.cmake @@ -0,0 +1,68 @@ +################################################################################ +# Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # +# # +# This software is distributed under the terms of the # +# GNU Lesser General Public Licence (LGPL) version 3, # +# copied verbatim in the file "LICENSE" # +################################################################################ + +# Runs one fairmq-tidy check on one test input and verifies the result with +# FileCheck, like check_clang_tidy.py of the LLVM project: +# +# // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: [] +# +# // CHECK-FIXES: +# +# Any warning or error without a CHECK-MESSAGES line fails the test. +# +# Usage: cmake -DTIDY= -DFILECHECK= -DCHECK= -DINPUT= +# -DFLAGS= -DWORKDIR= -P RunCheck.cmake + +foreach(var IN ITEMS TIDY FILECHECK CHECK INPUT FLAGS WORKDIR) + if(NOT DEFINED ${var}) + message(FATAL_ERROR "${var} is not set") + endif() +endforeach() + +file(MAKE_DIRECTORY "${WORKDIR}") +get_filename_component(name "${INPUT}" NAME) +set(fixed "${WORKDIR}/${name}") +set(messages "${WORKDIR}/${name}.messages") + +# Remove the CHECK lines from the copy, but keep the line numbers. +file(READ "${INPUT}" content) +string(REGEX REPLACE "// *CHECK-[A-Z-]+:[^\n]*" "//" content "${content}") +file(WRITE "${fixed}" "${content}") + +file(STRINGS "${FLAGS}" flags) +execute_process( + COMMAND "${TIDY}" "--checks=-*,${CHECK}" --fix "${fixed}" -- ${flags} + OUTPUT_FILE "${messages}" + ERROR_VARIABLE stderr + RESULT_VARIABLE result +) +if(NOT result EQUAL 0) + file(READ "${messages}" out) + message(FATAL_ERROR "fairmq-tidy failed (${result}):\n${out}\n${stderr}") +endif() + +execute_process( + COMMAND "${FILECHECK}" "${INPUT}" --check-prefix=CHECK-MESSAGES + "--input-file=${messages}" "--implicit-check-not={{warning|error}}:" + RESULT_VARIABLE result +) +if(NOT result EQUAL 0) + message(FATAL_ERROR "The diagnostics do not match the CHECK-MESSAGES lines.") +endif() + +file(STRINGS "${INPUT}" fixes REGEX "// *CHECK-FIXES:") +if(fixes) + execute_process( + COMMAND "${FILECHECK}" "${INPUT}" --check-prefix=CHECK-FIXES + "--input-file=${fixed}" --match-full-lines + RESULT_VARIABLE result + ) + if(NOT result EQUAL 0) + message(FATAL_ERROR "The fixed file does not match the CHECK-FIXES lines.") + endif() +endif() diff --git a/test/tidy/fairmq-modernize-nonnamespaced-types.cxx b/test/tidy/fairmq-modernize-nonnamespaced-types.cxx new file mode 100644 index 00000000..1153b639 --- /dev/null +++ b/test/tidy/fairmq-modernize-nonnamespaced-types.cxx @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +#include +#include + +// CHECK-MESSAGES: :[[@LINE+1]]:18: warning: 'FairMQDevice' is deprecated; use 'fair::mq::Device' [fairmq-modernize-nonnamespaced-types] +struct Sampler : FairMQDevice +// CHECK-FIXES: struct Sampler : fair::mq::Device +{ + bool ConditionalRun() override + { + // CHECK-MESSAGES: :[[@LINE+1]]:9: warning: 'FairMQMessagePtr' is deprecated; use 'fair::mq::MessagePtr' [fairmq-modernize-nonnamespaced-types] + FairMQMessagePtr msg(NewMessage(10)); + // CHECK-FIXES: fair::mq::MessagePtr msg(NewMessage(10)); + // CHECK-MESSAGES: :[[@LINE+1]]:11: warning: 'FairMQParts' is deprecated; use 'fair::mq::Parts' [fairmq-modernize-nonnamespaced-types] + ::FairMQParts parts; + // CHECK-FIXES: ::fair::mq::Parts parts; + parts.AddPart(std::move(msg)); + return false; + } +}; + +// CHECK-MESSAGES: :[[@LINE+1]]:23: warning: 'FairMQChannel' is deprecated; use 'fair::mq::Channel' [fairmq-modernize-nonnamespaced-types] +void useChannel(const FairMQChannel&); +// CHECK-FIXES: void useChannel(const fair::mq::Channel&); + +// CHECK-MESSAGES: :[[@LINE+1]]:29: warning: 'FairMQMessage' is deprecated; use 'fair::mq::Message' [fairmq-modernize-nonnamespaced-types] +std::vector> messages; +// CHECK-FIXES: std::vector> messages; + +// Deprecated global aliases without the FairMQ prefix. +// CHECK-MESSAGES: :[[@LINE+1]]:1: warning: 'InputMsgCallback' is deprecated; use 'fair::mq::InputMsgCallback' [fairmq-modernize-nonnamespaced-types] +InputMsgCallback callback; +// CHECK-FIXES: fair::mq::InputMsgCallback callback; + +// Namespaced types and aliases of the user are not reported. +using MyDevice = fair::mq::Device; +fair::mq::MessagePtr namespacedMsg; +MyDevice* device = nullptr; + +// Spellings in macro expansions are not reported. +#define DEVICE_PTR FairMQDevice* +DEVICE_PTR macroDevice = nullptr;