Skip to content

STYLE/COMP: Silence 611 -Wunnecessary-virtual-specifier warnings via itkNonVirtual* macros#1972

Merged
hjmjohnson merged 2 commits into
ANTsX:masterfrom
hjmjohnson:final-class-macro-migration
May 13, 2026
Merged

STYLE/COMP: Silence 611 -Wunnecessary-virtual-specifier warnings via itkNonVirtual* macros#1972
hjmjohnson merged 2 commits into
ANTsX:masterfrom
hjmjohnson:final-class-macro-migration

Conversation

@hjmjohnson

Copy link
Copy Markdown
Collaborator

Silence 611 of 626 -Wunnecessary-virtual-specifier warnings reported by Clang on a clean ANTs build, by migrating 14 final classes from itk{Set,Get,Boolean}Macro to their new itkNonVirtual* equivalents (from upcoming ITK PR InsightSoftwareConsortium/ITK#6253). A backward-compatibility shim header makes the work usable against current ITK 5.x and ITK 6.x master today, without waiting for #6253 to merge.

Why the warning fires (Clang's -Wunnecessary-virtual-specifier)

The legacy itkSetMacro / itkGetMacro / itkBooleanMacro families emit virtual on every generated member function. When the enclosing class is marked final, that virtual is meaningless — C++ only permits final on functions that override a base virtual, so a virtual function declared inside a final class with no matching base method cannot be further overridden. Clang flags every such occurrence.

The new itkNonVirtual* family emits the same function body without virtual, which is the correct spelling for a freshly-introduced member function inside a final class.

Scope of the cleanup

14 headers migrated (15 files touched once the shim include is added to each):

File Class
ImageRegistration/itkANTSAffine3DTransform.h ANTSAffine3DTransform
Temporary/antsFastMarchingImageFilter.h FastMarchingImageFilter
Utilities/antsCommandLineOption.h CommandLineOption
Utilities/antsCommandLineParser.h CommandLineParser
Utilities/antsMatrixUtilities.h antsMatrixUtilities
Utilities/antsSCCANObject.h antsSCCANObject
Utilities/itkAlternatingValueDifferenceImageFilter.h AlternatingValueDifferenceImageFilter
Utilities/itkAlternatingValueSimpleSubtractionImageFilter.h AlternatingValueSimpleSubtractionImageFilter
Utilities/itkAverageOverDimensionImageFilter.h AverageOverDimensionImageFilter
Utilities/itkLabeledPointSetFileReader.h LabeledPointSetFileReader
Utilities/itkLabeledPointSetFileWriter.h LabeledPointSetFileWriter
Utilities/itkMultiScaleLaplacianBlobDetectorImageFilter.h MultiScaleLaplacianBlobDetectorImageFilter
Utilities/itkPulsedArterialSpinLabeledCerebralBloodFlowImageFilter.h PulsedArterialSpinLabeledCerebralBloodFlowImageFilter
Utilities/itkSliceTimingCorrectionImageFilter.h SliceTimingCorrectionImageFilter
Utilities/itkSurfaceImageCurvature.h SurfaceImageCurvature

Also fixed: a latent typo itkNotVirtualGetConstReferenceMacroitkNonVirtualGetConstReferenceMacro.

Backward compatibility: Utilities/ITKGetterSetterMacroShims.h

itkNonVirtual* and its itkVirtual* / itkFinal* cousins are introduced by ITK PR #6253 and are absent from every released ITK to date — both the 5.x series and current 6.x master.

To keep ANTs buildable while that PR matures upstream, this PR adds a shim header that supplies the new macro family. The sentinel:

#if !defined(itkSetMacroImpl)
// ... 27 macro families, parametrized by (virtualKeyword, finalKeyword) ...
#endif

uses itkSetMacroImpl — a brand-new identifier introduced by #6253 — as the trip-wire. Once ITK ships with #6253, the entire shim collapses to an empty translation unit and the file can be deleted in a one-line follow-up commit. No source churn at the migrated call sites.

Design notes:

  • The shim only provides the new public variants (itkVirtual*, itkFinal*, itkNonVirtual*) plus the internal *Impl(virtualKeyword, finalKeyword, ...) helpers.
  • The legacy itkSetMacro/itkGetMacro/etc. names are not redefined — pre-6253 ITK already provides them as virtual-emitting forms, and redefinition would trigger -Wmacro-redefined.
  • Macro families covered: Set/GetInput, Set/GetDecoratedInput, SetGetDecoratedInput, Set/GetDecoratedObjectInput, SetGetDecoratedObjectInput, Set, Get, GetConst, GetConstReference, SetEnum, GetEnum, SetString, GetString, SetClamp, SetObject, GetObject, GetConstObject, GetModifiableObject (honoring ITK_FUTURE_LEGACY_REMOVE), GetConstReferenceObject, SetConstObject, Boolean, SetVector, GetVector, Set/GetDecoratedOutput.
Local validation

Two clean SuperBuilds from scratch, both against the SuperBuild's pinned pre-6253 ITK GIT_TAG=18d89fd6 (2026-03-18):

Build Inner targets SuperBuild steps Errors
build-ITKv5/ 424/424 17/17 0
build-ITKv6/ 424/424 17/17 0

(The "ITKv5" name is documentary — the ANTs SuperBuild only supports ITK_VERSION_MAJOR=6. Both builds exercise the shim's sentinel-false branch since the pinned ITK predates #6253.)

ccache was used to share dependency objects between the two builds. ANTs has no .pre-commit-config.yaml, so the per-PR pre-commit gate does not apply.

Migrate 14 final-class headers from itkSetMacro/itkGetMacro/... to
their itkNonVirtual* equivalents. This silences 611 of the 626
-Wunnecessary-virtual-specifier warnings reported by Clang on a
clean build against ITK main (with the new macro variants added in
InsightSoftwareConsortium/ITK).

The legacy itkSetMacro/itkGetMacro/itkBooleanMacro families emit
'virtual' on the generated member function. When the enclosing class
is marked 'final', that 'virtual' is meaningless and Clang flags it.
The new itkNonVirtual* family emits the same body without 'virtual',
which is the correct form for new member functions in a final class
(C++ only permits 'final' on functions that override a base virtual).

Also fix the latent typo itkNotVirtualGetConstReferenceMacro ->
itkNonVirtualGetConstReferenceMacro in itkANTSAffine3DTransform.h;
the misspelled name never existed in ITK.

Migrated files (167 macro call sites converted):

  Utilities/antsCommandLineOption.h
  Utilities/antsSCCANObject.h
  Utilities/antsCommandLineParser.h
  Temporary/antsFastMarchingImageFilter.h
  Utilities/itkMultiScaleLaplacianBlobDetectorImageFilter.h
  Utilities/itkLabeledPointSetFileWriter.h
  Utilities/itkSurfaceImageCurvature.h
  Utilities/itkPulsedArterialSpinLabeledCerebralBloodFlowImageFilter.h
  Utilities/itkAlternatingValueDifferenceImageFilter.h
  Utilities/itkLabeledPointSetFileReader.h
  Utilities/itkSliceTimingCorrectionImageFilter.h
  Utilities/antsMatrixUtilities.h
  Utilities/itkAlternatingValueSimpleSubtractionImageFilter.h
  Utilities/itkAverageOverDimensionImageFilter.h
  ImageRegistration/itkANTSAffine3DTransform.h

The 15 remaining -Wunnecessary-virtual-specifier warnings are on
hand-written 'virtual' keywords (not macro calls); those will be
addressed separately.

Requires the four-variant macro additions in ITK (itkVirtual*,
itkOverride*, itkFinal*, itkNonVirtual* Get/Set families) merging
upstream first.
The preceding STYLE commit migrates 14 final-class headers from the
legacy itkSetMacro/itkGetMacro/itkBooleanMacro families to their
itkNonVirtual* equivalents. Those macro variants are introduced by
InsightSoftwareConsortium/ITK PR #6253 and are not present in any
released ITK (5.x series, nor early 6.x master before that PR).

To keep ANTs buildable against both ITK 5.x and pre-6253 ITK 6.x
while the PR matures upstream, add a backward-compatibility shim
header that provides the itkVirtual* / itkFinal* / itkNonVirtual*
families. The sentinel `#if !defined(itkSetMacroImpl)` collapses
the shim to an empty translation unit once ITK ships with PR #6253,
so the file can be retired without source churn at that time.

Wire the shim into all 15 headers that consume the new macros. The
legacy itkSetMacro/itkGetMacro/itkBooleanMacro names are intentionally
NOT redefined here; pre-6253 ITK already provides them as virtual-
emitting forms, and redefinition would trigger -Wmacro-redefined.

Validated by two clean SuperBuilds from scratch (build-ITKv5/ and
build-ITKv6/, both against the pinned pre-6253 ITK GIT_TAG
18d89fd6, 2026-03-18): 424/424 inner targets + 17/17 SuperBuild
steps, 0 errors in each.
@hjmjohnson hjmjohnson marked this pull request as ready for review May 12, 2026 20:19
@hjmjohnson hjmjohnson requested a review from ntustison May 12, 2026 20:20
@hjmjohnson hjmjohnson self-assigned this May 12, 2026

@ntustison ntustison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me. Thanks @hjmjohnson .

@hjmjohnson hjmjohnson merged commit 9e4f789 into ANTsX:master May 13, 2026
2 checks passed
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.

2 participants