Skip to content

Commit 6e536ab

Browse files
committed
Ensure that plugin class name is a valid C indentifier
We should prevent using the invalid plugin class names at CMake configure stage already, so users receive the early error. Fixes: QTBUG-135860 Change-Id: I259539f6cce70a035ccf458a62d9e5a02f238ef8 Reviewed-by: Alexandru Croitor <[email protected]>
1 parent 6295e78 commit 6e536ab

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

cmake/QtPublicCMakeHelpers.cmake

+23
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,26 @@ function(_qt_internal_configure_file mode)
918918

919919
configure_file("${input_file}" "${arg_OUTPUT}" @ONLY)
920920
endfunction()
921+
922+
# The function checks if `value` is a valid C indentifier.
923+
#
924+
# Synopsis
925+
#
926+
# _qt_internal_is_c_identifier(<out_var> <value>)
927+
#
928+
# Arguments
929+
#
930+
# `out_var`
931+
# Variable name for the evaluation result.
932+
#
933+
# `value`
934+
# The string for the evaluation.
935+
function(_qt_internal_is_c_identifier out_var value)
936+
string(MAKE_C_IDENTIFIER "${value}" value_valid)
937+
938+
if(value AND "${value}" STREQUAL "${value_valid}")
939+
set(${out_var} "TRUE" PARENT_SCOPE)
940+
else()
941+
set(${out_var} "FALSE" PARENT_SCOPE)
942+
endif()
943+
endfunction()

src/corelib/Qt6CoreMacros.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,14 @@ function(qt6_add_plugin target)
26262626
endif()
26272627
endif()
26282628

2629+
_qt_internal_is_c_identifier(is_c_indentifier "${plugin_class_name}")
2630+
if(NOT is_c_indentifier)
2631+
message(FATAL_ERROR "The provided or calculated plugin CLASS_NAME '${plugin_class_name}' of"
2632+
" the '${target}' target is not a valid C++ class name. Please use only valid C++"
2633+
" identifiers."
2634+
)
2635+
endif()
2636+
26292637
set_target_properties(${target} PROPERTIES QT_PLUGIN_CLASS_NAME "${plugin_class_name}")
26302638

26312639
# Create a plugin initializer object library for static plugins.

tests/auto/cmake/CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -482,5 +482,35 @@ if(NOT QNX AND NOT WASM AND NOT (WIN32 AND QT_BUILD_MINIMAL_STATIC_TESTS)
482482
_qt_internal_test_expect_pass(test_qt_add_ui_11)
483483
endif()
484484

485+
# Valid plugin names
486+
set(valid_plugin_class_names
487+
TestPluginNameUpper
488+
Test_Plugin_Name
489+
_Test_plugin_name
490+
testpluginnamelower
491+
Test0PluginName
492+
)
493+
foreach(plugin_class_name IN LISTS valid_plugin_class_names)
494+
_qt_internal_test_expect_pass(test_plugin_class_name
495+
TESTNAME test_plugin_class_name_${plugin_class_name}
496+
BUILD_OPTIONS
497+
-DPLUGIN_CLASS_NAME=${plugin_class_name}
498+
)
499+
endforeach()
500+
501+
# Invalid plugin names
502+
set(invalid_plugin_class_names
503+
0TestPluginName
504+
Test-Plugin-Name
505+
Test.plugin.name
506+
)
507+
foreach(plugin_class_name IN LISTS invalid_plugin_class_names)
508+
_qt_internal_test_expect_fail(test_plugin_class_name
509+
TESTNAME test_plugin_class_name_${plugin_class_name}
510+
BUILD_OPTIONS
511+
-DPLUGIN_CLASS_NAME=${plugin_class_name}
512+
)
513+
endforeach()
514+
485515
# Add all tests using CMake's RunCMake test module
486516
add_subdirectory(RunCMake)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2025 The Qt Company Ltd.
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
cmake_minimum_required(VERSION 3.20)
5+
6+
project(test_plugin_class_name_${PLUGIN_CLASS_NAME} LANGUAGES CXX)
7+
8+
find_package(Qt6Core REQUIRED)
9+
10+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/plugin.cpp.in"
11+
"${CMAKE_CURRENT_BINARY_DIR}/plugin.cpp" @ONLY)
12+
13+
qt_add_plugin(test_plugin CLASS_NAME ${PLUGIN_CLASS_NAME} "${CMAKE_CURRENT_BINARY_DIR}/plugin.cpp")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (C) 2025 The Qt Company Ltd.
2+
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3+
4+
class @PLUGIN_CLASS_NAME@ {
5+
// noop
6+
};

0 commit comments

Comments
 (0)