STYLE: Replace non-POD string constants with constexpr QLatin1String - #1410
STYLE: Replace non-POD string constants with constexpr QLatin1String#1410hjmjohnson wants to merge 1 commit into
Conversation
640dab9 to
1e01702
Compare
Static QString variables have non-trivial constructors and destructors,
triggering clazy:non-pod-global-static and introducing Static
Initialization Order Fiasco (SIOF) risk. All affected constants are
pure compile-time string literals convertible to constexpr QLatin1String.
Qt5 / Apple-clang compatibility: QLatin1String(const char*) calls
strlen(), which is not constexpr under Apple clang. A local
make_constexpr_QLatin1String_ helper in each anonymous namespace deduces
the string length at compile time via a const-array-reference parameter,
avoiding sizeof/static_cast repetition at every definition site:
template<std::size_t N>
constexpr QLatin1String make_constexpr_QLatin1String_(const char (&str)[N])
{ return QLatin1String{str, static_cast<int>(N - 1)}; }
The redundant 'static' specifier is omitted on all anonymous-namespace
members (internal linkage is already implied by the enclosing namespace).
For ctkPluginFrameworkDebug, the ten OPTION_DEBUG_* class static members
are converted to inline static constexpr in the private header using the
helper and a #define CTK_OSGI prefix macro (scoped with #undef); the
out-of-line QString definitions and CTK_OSGI QString variable are removed
from the .cpp entirely. No call-site changes are required -- QLatin1String
implicitly converts to QString for all existing uses.
Files changed:
- Libs/CommandLineModules/Frontend/QtGui/ctkCmdLineModuleObjectTreeWalker.cpp
- Libs/PluginFramework/ctkBasicLocation.cpp
- Libs/PluginFramework/ctkLocationManager.cpp
- Libs/PluginFramework/ctkPluginFrameworkDebug_p.h
- Libs/PluginFramework/ctkPluginFrameworkDebug.cpp
- Libs/PluginFramework/ctkPluginFrameworkLauncher.cpp
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
CTK Validation Report for PR #1410Date: 2026-04-03T13:37:14Z
Validation Actions Performed
CTK-in-Slicer Build Detail (classified warnings)
Slicer Test Detail (classified failures)
CTK Test Failures (proposed
|
1e01702 to
3e92050
Compare
|
@jamesobutler I've been developing a method to more completely test each proposed change to CTK. See the commit message: #1410 (comment). I built the CTK source code
The report indicates no regressions to tests or builds. If there are other bits of information you think would be helpful, let me know and I'll try to add them. |
3e92050 to
d67254c
Compare
8aed437 to
f9060b7
Compare
There was a problem hiding this comment.
The proposed syntax and the repetitive utility function is somewhat noisy. According to Claude, there were also some potential ODR violations in ctkPluginFrameworkDebug_p.h.
I've submitted a pull request that describes an alternative solution (I could not push to this branch) - please have a look: #1440
|
Closing in favor of #1440, which solves the same problem with a cleaner approach. @lassoan's Thanks for the review and the alternative. |
|
Thanks! Could you add a review to #1440 (to fulfill the review requirement)? |
Summary
Fixes
clazy:non-pod-global-staticwarnings on file-scope and class-static string constants by converting them tostatic constexpr QLatin1String.This is one of two PRs split from a combined approach (the companion PR #1409 handles
ctkLoggerglobals viaQ_GLOBAL_STATIC_WITH_ARGS).Problem
static QStringat file scope or as class static members have non-trivial constructors and destructors, triggering thenon-pod-global-staticclazy warning. These are subject to the Static Initialization Order Fiasco (SIOF) and add unnecessary startup/teardown overhead.Approach
QLatin1Stringis a lightweight wrapper around aconst char*+ length — no heap allocation, trivially constructible/destructible. All affected constants are pure compile-time string literals, making them idealconstexprcandidates.Qt5 / Apple-clang compatibility:
QLatin1String(const char*)callsstrlen(), which Apple clang does not treat asconstexpr. The 2-argument constructor withsizeof("literal") - 1is used instead:For class static members (
ctkPluginFrameworkDebug), theconstexprdefinition moves entirely into the header (C++17static constexpris implicitlyinline), the out-of-lineQStringdefinitions in the.cppare removed, and theCTK_OSGIintermediate variable becomes unnecessary:No call-site changes required —
QLatin1Stringimplicitly converts toQStringfor all existing uses.Files Changed
Libs/CommandLineModules/Frontend/QtGui/ctkCmdLineModuleObjectTreeWalker.cppLibs/PluginFramework/ctkBasicLocation.cppLibs/PluginFramework/ctkLocationManager.cppLibs/PluginFramework/ctkPluginFrameworkDebug_p.hconstexprinlineLibs/PluginFramework/ctkPluginFrameworkDebug.cppCTK_OSGIvariableLibs/PluginFramework/ctkPluginFrameworkLauncher.cppComparison with PR #1398
const T& name()constexpr QLatin1String name()suffixTesting
Built successfully against Qt5 (
cmake-build-clazy-qt5, Apple clang) and Qt6 (cmake-build-clazy-qt6) on macOS ARM64.Part of the fix for #1407. See also companion PR #1409 for
Q_GLOBAL_STATIC_WITH_ARGSlogger fixes, and draft PR #1411 for thectkDICOMModalitiesAPI deprecation strategy.🤖 Generated with Claude Code