Issue 91206
Summary Clang-tidy crash in ASTMatcher(cppcoreguidelines-owning-memory) with GMock/GTest and lambdas.
Labels clang-tidy
Assignees
Reporter MichaelFeistETC
    Running clang-tidy on the following piece of code causes clang-tidy to crash:
`
#include "gmock/gmock.h"
#include <cstdint>
class Mock
{
public:
  MOCK_METHOD(int, Foo, (uint8_t x));
};

TEST(BreakClangTidy, BreakClangTidy)
{
  Mock mock;
  auto foo = [mock](uint8_t x) { return mock.Foo(x); };
}
`

Resulting clang-tidy crash output:
`
Stack dump:
0.      Program arguments: clang-tidy --extra-arg-before=--driver-mode=g++ PROJECT_DIR\\tests\\break_clang_tidy.cpp -- C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -IPROJECT_DIR/tests -isystem PROJECT_DIR/build/_deps/googletest-src/googletest/include -isystem PROJECT_DIR/build/_deps/googletest-src/googletest -isystem PROJECT_DIR/build/_deps/googletest-src/googlemock/include -isystem PROJECT_DIR/build/_deps/googletest-src/googlemock -O0 -g -Xclang -gcodeview -std=c++20 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -Wall -Wno-sign-conversion -Werror -MD -MT tests/CMakeFiles/lm75_test.dir/break_clang_tidy.cpp.obj -MF tests\\CMakeFiles\\lm75_test.dir\\break_clang_tidy.cpp.obj.d -o tests/CMakeFiles/lm75_test.dir/break_clang_tidy.cpp.obj -c PROJECT_DIR/tests/break_clang_tidy.cpp
1.      <eof> parser at end of file
2.      ASTMatcher: Matching 'cppcoreguidelines-owning-memory' against:
        CXXMethodDecl BreakClangTidy_BreakClangTidy_Test::TestBody : <PROJECT_DIR\tests\break_clang_tidy.cpp:9:1 <Spelling={PROJECT_DIR/build/_deps/googletest-src/googletest/include\gtest/internal/gtest-internal.h:1518:3>, PROJECT_DIR\tests\break_clang_tidy.cpp:13:1>
Exception Code: 0xC0000005
 #0 0x00007ff7c6b90043 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1310043)
 #1 0x00007ff7c6ab7f79 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1237f79)
 #2 0x00007ff7c6aba4b2 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123a4b2)
 #3 0x00007ff7c6ab830a (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123830a)
 #4 0x00007ff7c6ab76ea (C:\Program Files\LLVM\bin\clang-tidy.exe+0x12376ea)
 #5 0x00007ff7c6abebc4 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123ebc4)
 #6 0x00007ff7c6ab5741 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1235741)
 #7 0x00007ff7c6ab4479 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1234479)
 #8 0x00007ff7c6ab3ef6 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1233ef6)
 #9 0x00007ff7c6abcadc (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123cadc)
#10 0x00007ff7c6abc302 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123c302)
#11 0x00007ff7c6ab87da (C:\Program Files\LLVM\bin\clang-tidy.exe+0x12387da)
#12 0x00007ff7c6ab76ea (C:\Program Files\LLVM\bin\clang-tidy.exe+0x12376ea)
#13 0x00007ff7c6abea56 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123ea56)
#14 0x00007ff7c6ab56c1 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x12356c1)
#15 0x00007ff7c6ab4677 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1234677)
#16 0x00007ff7c6ab3ef6 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1233ef6)
#17 0x00007ff7c6ab384d (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123384d)
#18 0x00007ff7c6ab365f (C:\Program Files\LLVM\bin\clang-tidy.exe+0x123365f)
#19 0x00007ff7c6ab1e93 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1231e93)
#20 0x00007ff7c6b7022c (C:\Program Files\LLVM\bin\clang-tidy.exe+0x12f022c)
#21 0x00007ff7c6b8d9d7 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x130d9d7)
#22 0x00007ff7c6b8f8b2 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x130f8b2)
#23 0x00007ff7c6b8f41b (C:\Program Files\LLVM\bin\clang-tidy.exe+0x130f41b)
#24 0x00007ff7c6b8d8aa (C:\Program Files\LLVM\bin\clang-tidy.exe+0x130d8aa)
#25 0x00007ff7c6ac40f3 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x12440f3)
#26 0x00007ff7c6b8d8aa (C:\Program Files\LLVM\bin\clang-tidy.exe+0x130d8aa)
#27 0x00007ff7c6ac2d49 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1242d49)
#28 0x00007ff7c6ac42fd (C:\Program Files\LLVM\bin\clang-tidy.exe+0x12442fd)
#29 0x00007ff7c6ad074c (C:\Program Files\LLVM\bin\clang-tidy.exe+0x125074c)
#30 0x00007ff7c6ac4a3c (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1244a3c)
#31 0x00007ff7c6ac4327 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1244327)
#32 0x00007ff7c6ab0ee4 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1230ee4)
#33 0x00007ff7c6ae9443 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1269443)
#34 0x00007ff7c6587538 (C:\Program Files\LLVM\bin\clang-tidy.exe+0xd07538)
#35 0x00007ff7c589d981 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1d981)
#36 0x00007ff7c589d3f2 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1d3f2)
#37 0x00007ff7c5d7dc17 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x4fdc17)
#38 0x00007ff7c5bfa365 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x37a365)
#39 0x00007ff7c5d7dab5 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x4fdab5)
#40 0x00007ff7c5d7c8fb (C:\Program Files\LLVM\bin\clang-tidy.exe+0x4fc8fb)
#41 0x00007ff7c5d7f1ac (C:\Program Files\LLVM\bin\clang-tidy.exe+0x4ff1ac)
#42 0x00007ff7c5bf61c9 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x3761c9)
#43 0x00007ff7c5bc0c21 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x340c21)
#44 0x00007ff7c6a19220 (C:\Program Files\LLVM\bin\clang-tidy.exe+0x1199220)
#45 0x00007ff81bc5257d (C:\windows\System32\KERNEL32.DLL+0x1257d)
#46 0x00007ff81c8caa48 (C:\windows\SYSTEM32\ntdll.dll+0x5aa48)
Access violation
`

The example is running using a combination of GTest and GMock tagged at commit hash 5197b1a8e6a1ef9f214f4aa537b0be17cbf91946.

If the piece of code is not wrapped in a TEST block, clang-tidy doesn't crash. Seems to be a combination of using a GMock class, a GTest test block, and trying to improperly capture the instance of the GMock class in a lambda.

The project is being built using CMake 3.29 and using the built in CXX_CLANG_TIDY property. I'm using the LLVM tools at version 18.1.4 downloaded and installed from Chocolatey.

CMake looks like this:
`
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

FetchContent(GTest) // Using some helper functions here instead of directly using FetchContent. Not the actual line I have here.

mark_as_advanced(
  gmock_build_tests
 gtest_build_samples
  gtest_build_tests
  gtest_disable_pthreads
 gtest_force_shared_crt
  gtest_hide_internal_symbols
  BUILD_GMOCK
 BUILD_GTEST 
)

set_target_properties(gtest gtest_main gmock gmock_main
  PROPERTIES FOLDER "tests"
)

enable_testing()

include(GoogleTest)

add_executable(test)

set_target_properties(test PROPERITES
  C_CLANG_TIDY "clang-tidy"
  CXX_CLANG_TIDY "clang-tidy"
)

target_sources(test PRIVATE break_clang_tidy.cpp)
target_link_libraries(test PRIVATE GTest::gtest_main GTest::gmock_main)

gtest_discover_tests(test EXTRA_ARGS "--gtest_output=xml:test-results)
`
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to