Skip to content

clang-tidy: resolve cppcoreguidelines-avoid-non-const-global-variables#544

Merged
knoepfel merged 3 commits intomainfrom
maintenance/clang-tidy/cppcoreguidelines-cppcoreguidelines-avoid-non-const-global-variables
Apr 23, 2026
Merged

clang-tidy: resolve cppcoreguidelines-avoid-non-const-global-variables#544
knoepfel merged 3 commits intomainfrom
maintenance/clang-tidy/cppcoreguidelines-cppcoreguidelines-avoid-non-const-global-variables

Conversation

@greenc-FNAL
Copy link
Copy Markdown
Contributor

  • Audit all occurrences
  • Fix unnecessary occurrence in test/type_deduction.cpp
  • Add justification comments and ignore via NOLINT* annotations

…les`

- Audit all occurrences
- Fix unnecessary occurrence in `test/type_deduction.cpp`
- Add justification comments and ignore via `NOLINT*` annotations
Copilot AI review requested due to automatic review settings April 22, 2026 19:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Resolves clang-tidy findings from cppcoreguidelines-avoid-non-const-global-variables by making one unnecessary test-global const and documenting/suppressing the remaining intentional non-const globals introduced by plugin-registration macros and Python C-API requirements.

Changes:

  • Make the type_deduction test’s global closure const.
  • Add targeted // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) suppressions with justification for intentional non-const globals in tests/plugins.
  • Add similar suppressions/justifications for required non-const Python C-API globals (PyTypeObject, PyMethodDef, PyMappingMethods) in the Python plugin code.

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/type_deduction.cpp Makes the file-scope lambda closure const to avoid the non-const global warning.
test/replicated.cpp Documents and suppresses the intentional non-const global atomic counter used across concurrent callbacks.
test/python/source.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/plugins/output.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/plugins/module.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/plugins/ij_source.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/mock-workflow/three_tuple_algorithm.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/mock-workflow/pd_fast_sim.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/mock-workflow/largeant.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/mock-workflow/ion_and_scint.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/mock-workflow/id_provider.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/mock-workflow/MC_truth_algorithm.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/max-parallelism/provide_parallelism.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/max-parallelism/check_parallelism.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/verify_even_fibonacci_numbers.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/verify_difference.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/read_index.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/read_id.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/plus_one.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/plus_101.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/last_index.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/benchmarks_provider.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/accept_fibonacci_numbers.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/accept_even_numbers.cpp Suppresses macro-introduced non-const global created by plugin registration.
test/benchmarks/accept_even_ids.cpp Suppresses macro-introduced non-const global created by plugin registration.
plugins/python/src/wrap.hpp Adds justification + suppression for exported non-const PyTypeObject declarations required by Python C API.
plugins/python/src/pymodule.cpp Suppresses macro-introduced non-const globals created by plugin registration inside separate namespaces.
plugins/python/src/modulewrap.cpp Suppresses required non-const PyMethodDef arrays and PyTypeObject definitions used by Python C API.
plugins/python/src/lifelinewrap.cpp Suppresses required non-const PyTypeObject definition used by Python C API.
plugins/python/src/dciwrap.cpp Suppresses required non-const PyMethodDef array and PyTypeObject definition used by Python C API.
plugins/python/src/configwrap.cpp Suppresses required non-const PyMappingMethods and PyTypeObject used by Python C API.
plugins/generate_layers.cpp Suppresses macro-introduced non-const global created by driver registration.
phlex/app/load_module.cpp Suppresses intentional file-scope non-const globals that retain plugin factory callables to prevent unloading.
form/form_module.cpp Suppresses macro-introduced non-const global created by plugin registration.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@            Coverage Diff             @@
##             main     #544      +/-   ##
==========================================
- Coverage   86.74%   85.62%   -1.13%     
==========================================
  Files         142      143       +1     
  Lines        5033     3665    -1368     
  Branches      631      631              
==========================================
- Hits         4366     3138    -1228     
+ Misses        452      313     -139     
+ Partials      215      214       -1     
Flag Coverage Δ
unittests 85.62% <100.00%> (-1.13%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
phlex/app/load_module.cpp 86.95% <100.00%> (-3.05%) ⬇️
phlex/detail/plugin_macros.hpp 100.00% <100.00%> (ø)
phlex/driver.hpp 100.00% <ø> (ø)
plugins/python/src/configwrap.cpp 75.60% <ø> (-2.25%) ⬇️
plugins/python/src/dciwrap.cpp 100.00% <ø> (ø)
plugins/python/src/lifelinewrap.cpp 56.25% <ø> (+4.25%) ⬆️
plugins/python/src/modulewrap.cpp 80.35% <ø> (+0.67%) ⬆️
plugins/python/src/wrap.hpp 100.00% <ø> (ø)

... and 108 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cfe0e73...8841be5. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@greenc-FNAL greenc-FNAL changed the title clang-tidy: resolve cppcoreguidelines-avoid-non-const-global-varibles clang-tidy: resolve cppcoreguidelines-avoid-non-const-global-variables Apr 22, 2026
Comment thread form/form_module.cpp Outdated
The clang-tidy check cppcoreguidelines-avoid-non-const-global-variables
fired at every PHLEX_REGISTER_ALGORITHMS, PHLEX_REGISTER_PROVIDERS, and
PHLEX_REGISTER_DRIVER call site because BOOST_DLL_ALIAS — which those
macros previously expanded to — creates a const-pointer-to-void alias
variable at namespace scope.  A pointer-to-const is not a const object,
so the check fires.  Suppressing it with a per-site NOLINTNEXTLINE forced
every user of those macros (plugins, sources, tests) to carry an
explanation comment they did not write and had nothing to do with.

Root-cause fix
--------------
Replace BOOST_DLL_ALIAS with direct symbol export:

* Modules / sources (PHLEX_REGISTER_ALGORITHMS, PHLEX_REGISTER_PROVIDERS):
  The macro now expands to a single extern "C" function definition named
  after the DLL alias token.  boost::dll::import_symbol<T> can find a
  function by name directly; no intermediate alias variable is needed.
  The forwarding declaration that preceded BOOST_DLL_ALIAS is removed
  because the entry-point function is now defined in one step.

* Driver (PHLEX_REGISTER_DRIVER):
  The driver entry-point cannot use extern "C" with a direct return type
  because driver_bundle is a C++ type and the compiler rejects
  -Wreturn-type-c-linkage.  Instead the macro now:
    1. Forward-declares the user's C++ implementation.
    2. Defines a thin extern "C" void shim that accepts the same proxy
       and config arguments plus a driver_bundle* out-parameter, calls
       the C++ implementation, and writes the result through the pointer.
    3. Opens the user's C++ implementation definition for the body that
       follows the macro.
  The shim has a void return type and C-compatible parameter types, so
  no linkage warning is triggered.

Loader changes (load_module.cpp)
---------------------------------
* import_alias<T> is replaced with import_symbol<T> throughout.
  import_alias reads a void* variable; import_symbol resolves a function
  directly by name, matching the new extern "C" export scheme.
* create_driver's stored type changes from
  std::function<detail::driver_creator_t> to
  std::function<detail::driver_shim_t> to match the shim signature.
* The driver invocation is updated to pass an out-parameter:
    driver_bundle result;
    create_driver(proxy, config, &result);
    return result;

Supporting type alias (driver.hpp)
-----------------------------------
detail::driver_shim_t is added alongside the existing driver_creator_t:
    using driver_shim_t = void(driver_proxy const&, configuration const&, driver_bundle*);

The include of boost/dll/alias.hpp is removed from driver.hpp, module.hpp,
and source.hpp; those headers no longer need it.

Call-site cleanup (26 files)
------------------------------
Every PHLEX_REGISTER_* call site previously carried:
    // BOOST_DLL_ALIAS creates a non-const exported function pointer; ...
    // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Both lines are removed from all 26 affected files because the macro no
longer produces any non-const global variable.

All 85 tests continue to pass.
@knoepfel knoepfel merged commit 68a1763 into main Apr 23, 2026
36 checks passed
@knoepfel knoepfel deleted the maintenance/clang-tidy/cppcoreguidelines-cppcoreguidelines-avoid-non-const-global-variables branch April 23, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants