AutoMoc incorrectly detects macros in comments
The problem
If a macro from AUTOMOC_MACRO_NAMES is found at the start of a line (ignoring whitespace) within a comment in a source file processed by AutoMoc, AutoMoc attempts to process it, likely resulting in an error.
Example syntax:
// These are fine
/* Q_OBJECT */
// Q_OBJECT
/*
//Q_OBJECT
*/
/*
Q_OBJECTfoo!
*/
// These cause problems
/*
Q_OBJECT
*/
/*
Q_OBJECT
*/
/*
Q_OBJECT foo!
*/
Minimal Example
Tool versions:
- Qt 5.15.2
- CMake 3.21.1 (using the Ninja generator)
- Clang 12.0.0
CMakeLists.txt:
project(MinExample)
cmake_minimum_required(VERSION 3.21.1)
set(CMAKE_AUTOMOC ON)
find_package(Qt5 5.15.2 COMPONENTS REQUIRED Core)
add_library(${CMAKE_PROJECT_NAME} main.cpp)
main.cpp:
/*
Q_OBJECT
*/
int main(int,char**)
{
return 0;
}
Resulting console output:
====================[ Build | MinExample | Debug ]==============================
"C:\Program Files\JetBrains\CLion 2021.1.1\bin\cmake\win\bin\cmake.exe" --build C:\Workspace\C++\temp\automoc_example\cmake-build-debug --target MinExample
[0/1] Re-running CMake...
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Workspace/C++/temp/automoc_example/cmake-build-debug
[1/4] Automatic MOC for target MinExample
FAILED: MinExample_autogen/timestamp MinExample_autogen/mocs_compilation.cpp C:/Workspace/C++/temp/automoc_example/cmake-build-debug/MinExample_autogen/timestamp C:/Workspace/C++/temp/automoc_example/cmake-build-debug/MinExample_autogen/mocs_compilation.cpp
cmd.exe /C "cd /D C:\Workspace\C++\temp\automoc_example\cmake-build-debug && "C:\Program Files\JetBrains\CLion 2021.1.1\bin\cmake\win\bin\cmake.exe" -E cmake_autogen C:/Workspace/C++/temp/automoc_example/cmake-build-debug/CMakeFiles/MinExample_autogen.dir/AutogenInfo.json Debug && "C:\Program Files\JetBrains\CLion 2021.1.1\bin\cmake\win\bin\cmake.exe" -E touch C:/Workspace/C++/temp/automoc_example/cmake-build-debug/MinExample_autogen/timestamp && "C:\Program Files\JetBrains\CLion 2021.1.1\bin\cmake\win\bin\cmake.exe" -E cmake_transform_depfile Ninja gccdepfile C:/Workspace/C++/temp/automoc_example C:/Workspace/C++/temp/automoc_example C:/Workspace/C++/temp/automoc_example/cmake-build-debug C:/Workspace/C++/temp/automoc_example/cmake-build-debug C:/Workspace/C++/temp/automoc_example/cmake-build-debug/MinExample_autogen/deps C:/Workspace/C++/temp/automoc_example/cmake-build-debug/CMakeFiles/d/cd4e1f4518d35b01e36b67351091d58e7a4fad7cf7ba6644e2c5294e65abb69f.d"
AutoMoc error
-------------
"SRC:/main.cpp"
contains a "Q_OBJECT" macro, but does not include "main.moc"!
Consider to
- add #include "main.moc"
- enable SKIP_AUTOMOC for this file
ninja: build stopped: subcommand failed.
Use Case
This issue came up while I was attempting to wrap an external library with a CMakeLists.txt. The developers had added their qdoc comments to the source file that included a code snippet example containing Q_OBJECT on a standalone line.
The issue I logged against it can be found here