Skip to content

COMP: Suppress deprecated VariableLengthVector::AllocateElements warnings in MSVC wrapping builds#5863

Closed
thewtex wants to merge 1 commit intoInsightSoftwareConsortium:releasefrom
thewtex:0b54-address-variable
Closed

COMP: Suppress deprecated VariableLengthVector::AllocateElements warnings in MSVC wrapping builds#5863
thewtex wants to merge 1 commit intoInsightSoftwareConsortium:releasefrom
thewtex:0b54-address-variable

Conversation

@thewtex
Copy link
Member

@thewtex thewtex commented Mar 5, 2026

Summary

  • Add /wd4996 to MSVC compile flags for SWIG-generated Python wrapper targets to suppress C4996 (deprecated declaration) warnings

Background

After VariableLengthVector::AllocateElements was deprecated with [[deprecated("Please consider calling std::make_unique<TValue[]>(size) instead.")]], the SWIG-generated Python wrapper code (itkVariableLengthVectorPython.cpp) triggers MSVC warning C4996 for each wrapped template instantiation:

itkVariableLengthVectorPython.cpp(5150): warning C4996: 'itk::VariableLengthVector<std::complex<float>>::AllocateElements'
itkVariableLengthVectorPython.cpp(7765): warning C4996: 'itk::VariableLengthVector<double>::AllocateElements'
itkVariableLengthVectorPython.cpp(10633): warning C4996: 'itk::VariableLengthVector<float>::AllocateElements'
itkVariableLengthVectorPython.cpp(13501): warning C4996: 'itk::VariableLengthVector<short>::AllocateElements'
itkVariableLengthVectorPython.cpp(16369): warning C4996: 'itk::VariableLengthVector<unsigned char>::AllocateElements'

This happens because SWIG auto-generates wrapper functions that directly call the now-deprecated AllocateElements method. The generated code cannot be modified to avoid these calls.

Fix

Add /wd4996 alongside the existing /wd4244 in the MSVC compile flags for wrapping targets in Wrapping/macro_files/itk_end_wrap_module.cmake. This is consistent with GCC/Clang wrapping builds, which already suppress all warnings via the -w flag on the same targets. Deprecation warnings remain active for hand-written ITK C++ code; this only affects auto-generated SWIG wrapper compilation.

Add /wd4996 to MSVC compile flags for SWIG-generated Python wrapper
targets. This suppresses C4996 warnings triggered by deprecated
VariableLengthVector::AllocateElements calls in auto-generated wrapper
code. GCC/Clang wrapping builds already suppress all warnings with -w.
@github-actions github-actions bot added type:Compiler Compiler support or related warnings area:Python wrapping Python bindings for a class labels Mar 5, 2026
Copy link
Member

@blowekamp blowekamp left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@N-Dekker N-Dekker left a comment

Choose a reason for hiding this comment

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

Thanks, but why is AllocateElements wrapped? I don't think it should be part of the Python interface. Here it is:

VariableLengthVector<TValue>::AllocateElements(ElementIdentifier size) const
{
try
{
return new TValue[size];
}
catch (...)
{
// Intercept std::bad_alloc and any exception thrown from TValue
// default constructor.
itkGenericExceptionMacro("Failed to allocate memory of length " << size << " for VariableLengthVector.");
}
}

So it basically just returns a raw pointer created by new TValue[size].

@N-Dekker
Copy link
Contributor

N-Dekker commented Mar 5, 2026

If it's only AllocateElements that is causing the problem, I think it's preferable to just fix it locally for that specific function, instead of globally disabling the warning. The warning was introduced by the [[deprecated]] attribute here:

#ifndef ITK_FUTURE_LEGACY_REMOVE
/** Allocate memory of certain size and return it.
* \return a non-null pointer to an array of \c size elements (0 is a valid
* parameter).
* \deprecated Please consider calling `std::make_unique<TValue[]>(size)` instead.
*/
[[deprecated("Please consider calling `std::make_unique<TValue[]>(size)` instead.")]] [[nodiscard]] TValue *
AllocateElements(ElementIdentifier size) const;

Instead of explicitly using [[deprecated]], shouldn't we use ITK_FUTURE_DEPRECATED(message)? As defined here:

#if defined(ITK_LEGACY_REMOVE) && !defined(ITK_LEGACY_SILENT)
# define ITK_FUTURE_DEPRECATED(message) [[deprecated(message)]]
#else
# define ITK_FUTURE_DEPRECATED(message)
#endif

You see ITK_FUTURE_DEPRECATED(message) respects the ITK_LEGACY_REMOVE and the ITK_LEGACY_SILENT macro.

@blowekamp
Copy link
Member

The wrapping should be compiled with ITK_LEGACY_SILENT enabled. But this function is not respecting that, there should be some macros to support this functionality of respecting deprecation to be silent.

@N-Dekker
Copy link
Contributor

N-Dekker commented Mar 5, 2026

But this function is not respecting that, there should be some macros to support this functionality of respecting deprecation to be silent.

Then we should fix this function, right?

@blowekamp
Copy link
Member

That would make a fine follow up pull request, if you'd like.

N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Mar 6, 2026
This member function is only marked `ITK_FUTURE_LEGACY_REMOVE`, so using this
member function should only trigger a warning when legacy support is removed,
and when `ITK_LEGACY_SILENT` is off.

Aims to fix warnings like:

    itkVariableLengthVectorPython.cpp(5150): warning C4996: 'itk::VariableLengthVector::AllocateElements': Please consider calling `std::make_unique<TValue[]>(size)` instead.

At Windows_NT-Build4935-main-Python (https://open.cdash.org/builds/11095793)

As reported by Matt McCormick at InsightSoftwareConsortium#5863
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Mar 6, 2026
This member function is only marked `ITK_FUTURE_LEGACY_REMOVE`, so using this
member function should only trigger a warning when legacy support is removed
_and_ `ITK_LEGACY_SILENT` is off.

Aims to fix warnings like:

    itkVariableLengthVectorPython.cpp(5150): warning C4996: 'itk::VariableLengthVector::AllocateElements': Please consider calling `std::make_unique<TValue[]>(size)` instead.

At Windows_NT-Build4935-main-Python (https://open.cdash.org/builds/11095793)

As reported by Matt McCormick at pull request InsightSoftwareConsortium#5863
@N-Dekker
Copy link
Contributor

N-Dekker commented Mar 6, 2026

  • Now that PR COMP: Make VariableLengthVector::AllocateElements ITK_FUTURE_DEPRECATED #5869 is merged (thanks Hans), the VariableLengthVector::AllocateElements warnings addressed by this PR (5863) should disappear from the CI.
  • I think we can avoid warnings like these in the future, by following the strategy to consistently use ITK_FUTURE_DEPRECATED(msg) for functions that are marked "ITK_FUTURE_LEGACY_REMOVE". Right?
  • You know, compiler warnings are there to help us to prevent mistakes, so I'd rather not have them globally disabled 🤷

@hjmjohnson
Copy link
Member

@thewtex I think that @N-Dekker alternate fix in #5869 removes the need for this PR. Please close if you agree.

@thewtex
Copy link
Member Author

thewtex commented Mar 6, 2026

@N-Dekker 👍 thanks

@thewtex thewtex closed this Mar 6, 2026
thewtex pushed a commit that referenced this pull request Mar 6, 2026
This member function is only marked `ITK_FUTURE_LEGACY_REMOVE`, so using this
member function should only trigger a warning when legacy support is removed
_and_ `ITK_LEGACY_SILENT` is off.

Aims to fix warnings like:

    itkVariableLengthVectorPython.cpp(5150): warning C4996: 'itk::VariableLengthVector::AllocateElements': Please consider calling `std::make_unique<TValue[]>(size)` instead.

At Windows_NT-Build4935-main-Python (https://open.cdash.org/builds/11095793)

As reported by Matt McCormick at pull request #5863
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Python wrapping Python bindings for a class type:Compiler Compiler support or related warnings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants