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 <alexandru.croitor@qt.io>
This commit is contained in:
Alexey Edelev 2025-04-16 11:12:36 +02:00
parent 6295e78ade
commit 6e536aba73
5 changed files with 80 additions and 0 deletions

View File

@ -918,3 +918,26 @@ function(_qt_internal_configure_file mode)
configure_file("${input_file}" "${arg_OUTPUT}" @ONLY) configure_file("${input_file}" "${arg_OUTPUT}" @ONLY)
endfunction() endfunction()
# The function checks if `value` is a valid C indentifier.
#
# Synopsis
#
# _qt_internal_is_c_identifier(<out_var> <value>)
#
# Arguments
#
# `out_var`
# Variable name for the evaluation result.
#
# `value`
# The string for the evaluation.
function(_qt_internal_is_c_identifier out_var value)
string(MAKE_C_IDENTIFIER "${value}" value_valid)
if(value AND "${value}" STREQUAL "${value_valid}")
set(${out_var} "TRUE" PARENT_SCOPE)
else()
set(${out_var} "FALSE" PARENT_SCOPE)
endif()
endfunction()

View File

@ -2626,6 +2626,14 @@ function(qt6_add_plugin target)
endif() endif()
endif() endif()
_qt_internal_is_c_identifier(is_c_indentifier "${plugin_class_name}")
if(NOT is_c_indentifier)
message(FATAL_ERROR "The provided or calculated plugin CLASS_NAME '${plugin_class_name}' of"
" the '${target}' target is not a valid C++ class name. Please use only valid C++"
" identifiers."
)
endif()
set_target_properties(${target} PROPERTIES QT_PLUGIN_CLASS_NAME "${plugin_class_name}") set_target_properties(${target} PROPERTIES QT_PLUGIN_CLASS_NAME "${plugin_class_name}")
# Create a plugin initializer object library for static plugins. # Create a plugin initializer object library for static plugins.

View File

@ -482,5 +482,35 @@ if(NOT QNX AND NOT WASM AND NOT (WIN32 AND QT_BUILD_MINIMAL_STATIC_TESTS)
_qt_internal_test_expect_pass(test_qt_add_ui_11) _qt_internal_test_expect_pass(test_qt_add_ui_11)
endif() endif()
# Valid plugin names
set(valid_plugin_class_names
TestPluginNameUpper
Test_Plugin_Name
_Test_plugin_name
testpluginnamelower
Test0PluginName
)
foreach(plugin_class_name IN LISTS valid_plugin_class_names)
_qt_internal_test_expect_pass(test_plugin_class_name
TESTNAME test_plugin_class_name_${plugin_class_name}
BUILD_OPTIONS
-DPLUGIN_CLASS_NAME=${plugin_class_name}
)
endforeach()
# Invalid plugin names
set(invalid_plugin_class_names
0TestPluginName
Test-Plugin-Name
Test.plugin.name
)
foreach(plugin_class_name IN LISTS invalid_plugin_class_names)
_qt_internal_test_expect_fail(test_plugin_class_name
TESTNAME test_plugin_class_name_${plugin_class_name}
BUILD_OPTIONS
-DPLUGIN_CLASS_NAME=${plugin_class_name}
)
endforeach()
# Add all tests using CMake's RunCMake test module # Add all tests using CMake's RunCMake test module
add_subdirectory(RunCMake) add_subdirectory(RunCMake)

View File

@ -0,0 +1,13 @@
# Copyright (C) 2025 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.20)
project(test_plugin_class_name_${PLUGIN_CLASS_NAME} LANGUAGES CXX)
find_package(Qt6Core REQUIRED)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/plugin.cpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/plugin.cpp" @ONLY)
qt_add_plugin(test_plugin CLASS_NAME ${PLUGIN_CLASS_NAME} "${CMAKE_CURRENT_BINARY_DIR}/plugin.cpp")

View File

@ -0,0 +1,6 @@
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
class @PLUGIN_CLASS_NAME@ {
// noop
};