False warnings on code generated by QT Automoc
I ported a code base from qmake to cmake and have started cleaning up compiler warnings. In doing so, I noticed that the cpp files generated by QT's Automoc triggers warnings on -Wundefined-reinterpret-cast and -Wuseless-cast.
There isn't much that can be done to fix the warnings since the code is autogenerated by qt. Since I reference this repo for handy defaults I thought I would post this to see if it would help others as a good default.
To fix this in my project I added the following lines to my CMake file:
# Ignore warnings related to autogenerated code from MOC
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/[project_name]_autogen/mocs_compilation.cpp PROPERTIES COMPILE_FLAGS "-Wno-undefined-reinterpret-cast")
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/[project_name]_autogen/mocs_compilation.cpp PROPERTIES COMPILE_FLAGS "-Wno-useless-cast")
The solution you posted unfortunately doesn't seem to work for me (with clang). Maybe I just did it wrong. What helped me was to "kind of" disable clang-tidy on those files, more or less like it was done here: https://github.com/Kitware/CMake/commit/b13bc8659f87567b1b091806d42f5023b2a6b48b
For example:
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/[project_name]_autogen/.clang-tidy" "
---
Checks: '-*,llvm-twine-local'
...
")
It works until cppcheck or other checks come in, though. (still trying to figure it out). Probably a similar approach/solution will work.
EDIT: For cppcheck I had to do the following:
- Create a file in the project root, e.g., "cppcheck-suppressions.txt":
*:*_autogen/*
- Add this property:
dynamic_project_options(
....
CPPCHECK_OPTIONS "--suppressions-list=${PROJECT_SOURCE_DIR}/cppcheck-suppressions.txt"
)
and it skipped all the files in the build/*/*_autogen/ folders.