Skip to content

Commit 5d0565f

Browse files
committed
Improve clang-tidy support for MSVC and update docs
Added a workaround in cpp-library.cmake to automatically append --extra-arg=/EHsc to CMAKE_CXX_CLANG_TIDY when using MSVC, addressing a known clang-tidy issue with exception handling flags. Updated README with documentation about this issue and the applied solution. Also simplified test executable handling logic in _cpp_library_setup_executables.
1 parent 9903dd3 commit 5d0565f

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,12 @@ cpp_library_map_dependency("opencv_core" "OpenCV 4.5.0")
538538

539539
**Solution**: Repository name must match package name. For package `stlab-enum-ops`, use repository `stlab/stlab-enum-ops`, not `stlab/enum-ops`.
540540

541+
### Clang-Tidy on Windows/MSVC
542+
543+
**Problem**: Clang-tidy reports "exceptions are disabled" when analyzing code on Windows with MSVC
544+
545+
**Solution**: This is a known clang-tidy issue ([CMake #22979](https://gitlab.kitware.com/cmake/cmake/-/issues/22979)) where clang-tidy doesn't properly recognize MSVC's `/EHsc` exception handling flag. cpp-library automatically detects this scenario and adds `--extra-arg=/EHsc` to `CMAKE_CXX_CLANG_TIDY` when both MSVC and clang-tidy are enabled. This workaround is applied transparently and only on MSVC platforms.
546+
541547
## Development
542548

543549
### Running Tests

cpp-library.cmake

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function(_cpp_library_setup_executables)
6262
# Extract the clean library name for linking (strip namespace prefix if present)
6363
string(REPLACE "${ARG_NAMESPACE}-" "" CLEAN_NAME "${ARG_NAME}")
6464

65-
# Note: doctest dependency is downloaded by before deferring
65+
# Note: doctest dependency is downloaded by cpp_library_setup before deferring
6666
# This function assumes doctest::doctest target already exists
6767

6868
# Determine source directory based on type
@@ -94,27 +94,19 @@ function(_cpp_library_setup_executables)
9494
)
9595
set_tests_properties(compile_${executable_base} PROPERTIES WILL_FAIL TRUE)
9696
else()
97-
# Regular executable - conditionally build based on preset
97+
# Regular executable - build and link normally
9898
add_executable(${executable_base} "${source_dir}/${executable}")
9999
target_link_libraries(${executable_base} PRIVATE ${ARG_NAMESPACE}::${CLEAN_NAME} doctest::doctest)
100100

101-
# Only fully build (compile and link) in test preset
102-
# In clang-tidy preset, compile with clang-tidy but don't link
103-
if(CMAKE_CXX_CLANG_TIDY)
104-
# In clang-tidy mode, exclude from all builds but still compile
105-
set_target_properties(${executable_base} PROPERTIES EXCLUDE_FROM_ALL TRUE)
106-
# Don't add as a test in clang-tidy mode since we're not linking
107-
else()
108-
# In test mode, build normally and add as test
109-
add_test(NAME ${executable_base} COMMAND ${executable_base})
110-
111-
# Set test properties for better IDE integration (only for tests)
112-
if(ARG_TYPE STREQUAL "tests")
113-
set_tests_properties(${executable_base} PROPERTIES
114-
LABELS "doctest"
115-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
116-
)
117-
endif()
101+
# Register as CTest test
102+
add_test(NAME ${executable_base} COMMAND ${executable_base})
103+
104+
# Set test properties for better IDE integration (only for tests)
105+
if(ARG_TYPE STREQUAL "tests")
106+
set_tests_properties(${executable_base} PROPERTIES
107+
LABELS "doctest"
108+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
109+
)
118110
endif()
119111
endif()
120112
else()
@@ -162,6 +154,18 @@ function(cpp_library_setup)
162154
endif()
163155
set(ARG_NAME "${PROJECT_NAME}")
164156

157+
# Workaround for known clang-tidy issue on MSVC: clang-tidy doesn't properly recognize
158+
# the /EHsc exception handling flag from compile_commands.json (CMake issue #22979)
159+
# Automatically add --extra-arg=/EHsc when using clang-tidy with MSVC
160+
if(MSVC AND CMAKE_CXX_CLANG_TIDY)
161+
string(FIND "${CMAKE_CXX_CLANG_TIDY}" "/EHsc" EHSC_FOUND)
162+
if(EHSC_FOUND EQUAL -1)
163+
set(CMAKE_CXX_CLANG_TIDY "${CMAKE_CXX_CLANG_TIDY};--extra-arg=/EHsc"
164+
CACHE STRING "clang-tidy command" FORCE)
165+
message(STATUS "cpp-library: Added /EHsc to clang-tidy for MSVC compatibility")
166+
endif()
167+
endif()
168+
165169
# IMPORTANT: If TESTS or EXAMPLES are specified, include(CTest) MUST be called
166170
# at directory scope before cpp_library_setup(). This enables the testing infrastructure
167171
# required for add_test() and defines the BUILD_TESTING option.

0 commit comments

Comments
 (0)