diff --git a/Modules/Core/Common/include/itkExtractImageFilter.hxx b/Modules/Core/Common/include/itkExtractImageFilter.hxx index 430b7877d68e..2b03120c2e20 100644 --- a/Modules/Core/Common/include/itkExtractImageFilter.hxx +++ b/Modules/Core/Common/include/itkExtractImageFilter.hxx @@ -62,7 +62,6 @@ ExtractImageFilter::SetExtractionRegion(InputImageReg "InputImageDimension must be greater than OutputImageDimension"); m_ExtractionRegion = extractRegion; - unsigned int nonzeroSizeCount = 0; InputImageSizeType inputSize = extractRegion.GetSize(); OutputImageSizeType outputSize; outputSize.Fill(0); @@ -73,12 +72,16 @@ ExtractImageFilter::SetExtractionRegion(InputImageReg * check to see if the number of non-zero entries in the extraction region * matches the number of dimensions in the output image. */ + unsigned int nonzeroSizeCount = 0; for (unsigned int i = 0; i < InputImageDimension; ++i) { if (inputSize[i]) { - outputSize[nonzeroSizeCount] = inputSize[i]; - outputIndex[nonzeroSizeCount] = extractRegion.GetIndex()[i]; + if (nonzeroSizeCount < OutputImageDimension) + { + outputSize[nonzeroSizeCount] = inputSize[i]; + outputIndex[nonzeroSizeCount] = extractRegion.GetIndex()[i]; + } ++nonzeroSizeCount; } } @@ -116,11 +119,7 @@ ExtractImageFilter::GenerateOutputInformation() outputPtr->SetLargestPossibleRegion(m_OutputImageRegion); // Set the output spacing and origin - const ImageBase * phyData; - - phyData = dynamic_cast *>(this->GetInput()); - - if (phyData) + if (this->GetInput()) { // Copy what we can from the image from spacing and origin of the input // This logic needs to be augmented with logic that select which diff --git a/Modules/Core/Common/include/itkMacro.h b/Modules/Core/Common/include/itkMacro.h index ebfba083331c..df2c284540e2 100644 --- a/Modules/Core/Common/include/itkMacro.h +++ b/Modules/Core/Common/include/itkMacro.h @@ -272,10 +272,20 @@ namespace itk # define itkExposeEnumValue(name) static_cast(name) #endif -#if !defined(ITK_FUTURE_LEGACY_REMOVE) -# define ITK_FALLTHROUGH [[fallthrough]] -#else -# define ITK_FALLTHROUGH static_assert(false, "ERROR: ITK_FALLTHROUGH must be replaced with [[fallthrough]]") +// Use "ITK_FALLTHROUGH;" to annotate deliberate fall-through in switches, +// use it analogously to "break;". The trailing semi-colon is required. +#if defined(__GNUC__) && !defined(__INTEL_COMPILER) +# if (__GNUC__ >= 7) +# define ITK_FALLTHROUGH __attribute__((fallthrough)) +# endif +#elif defined(__has_warning) +# if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") +# define ITK_FALLTHROUGH [[clang::fallthrough]] +# endif +#endif + +#ifndef ITK_FALLTHROUGH +# define ITK_FALLTHROUGH ((void)0) #endif /** Define two object creation methods. The first method, New(), @@ -483,7 +493,6 @@ OutputWindowDisplayGenericOutputText(const char *); extern ITKCommon_EXPORT void OutputWindowDisplayDebugText(const char *); - } // end namespace itk // The itkDebugStatement is to be used to protect code that is only used in the itkDebugMacro diff --git a/Modules/Core/Common/include/itkMultiThreaderBase.h b/Modules/Core/Common/include/itkMultiThreaderBase.h index 299d72cfcd83..4e4c0184479d 100644 --- a/Modules/Core/Common/include/itkMultiThreaderBase.h +++ b/Modules/Core/Common/include/itkMultiThreaderBase.h @@ -238,16 +238,12 @@ ITK_GCC_PRAGMA_DIAG_PUSH() ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes") INTEL_PRAGMA_WARN_PUSH INTEL_SUPPRESS_warning_1292 -CLANG_PRAGMA_PUSH -CLANG_SUPPRESS_Wcpp14_extensions - // clang-format on # ifdef ITK_LEGACY_SILENT struct ThreadInfoStruct # else struct [[deprecated("Use WorkUnitInfo, ThreadInfoStruct is deprecated since ITK 5.0")]] ThreadInfoStruct # endif // clang-format off -CLANG_PRAGMA_POP INTEL_PRAGMA_WARN_POP // clang-format on { diff --git a/Modules/Core/Common/include/itkNeighborhood.h b/Modules/Core/Common/include/itkNeighborhood.h index 511f50314cdb..ed1a16b277e8 100644 --- a/Modules/Core/Common/include/itkNeighborhood.h +++ b/Modules/Core/Common/include/itkNeighborhood.h @@ -136,6 +136,11 @@ class ITK_TEMPLATE_EXPORT Neighborhood SizeValueType GetRadius(DimensionValueType n) const { + if (n >= VDimension) + { + itkExceptionMacro(<< " Can not get radius for dimension " << n << " greater than dimensionality of neighborhood " + << VDimension); + } return m_Radius[n]; } @@ -144,6 +149,11 @@ class ITK_TEMPLATE_EXPORT Neighborhood SizeValueType GetSize(DimensionValueType n) const { + if (n >= VDimension) + { + itkExceptionMacro(<< " Can not get size for dimension " << n << " greater than dimensionality of neighborhood " + << VDimension); + } return m_Size[n]; } diff --git a/Modules/Core/Common/include/itkNeighborhoodOperator.h b/Modules/Core/Common/include/itkNeighborhoodOperator.h index a95d39f3f7b2..55232f9e8454 100644 --- a/Modules/Core/Common/include/itkNeighborhoodOperator.h +++ b/Modules/Core/Common/include/itkNeighborhoodOperator.h @@ -93,6 +93,11 @@ class ITK_TEMPLATE_EXPORT NeighborhoodOperator : public Neighborhood= VDimension) + { + itkExceptionMacro(<< " Can not set direction " << direction << " greater than dimensionality of neighborhood " + << VDimension); + } m_Direction = direction; } diff --git a/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx b/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx index 8c39d45396bb..c7c0b17fd89d 100644 --- a/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx +++ b/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx @@ -350,7 +350,7 @@ TEST(ShapedImageNeighborhoodRange, IteratorsCanBePassedToStdVectorConstructor) // second argument of std::reverse (which requires bidirectional iterators). TEST(ShapedImageNeighborhoodRange, IteratorsCanBePassedToStdReverseCopy) { - using PixelType = unsigned char; + using PixelType = unsigned short; using ImageType = itk::Image; enum { @@ -366,14 +366,16 @@ TEST(ShapedImageNeighborhoodRange, IteratorsCanBePassedToStdReverseCopy) itk::ShapedImageNeighborhoodRange range{ *image, location, offsets }; const unsigned int numberOfNeighborhoodPixels = 3; + ASSERT_EQ(numberOfNeighborhoodPixels, range.size()); const std::vector stdVector(range.begin(), range.end()); - std::vector reversedStdVector1(numberOfNeighborhoodPixels); - std::vector reversedStdVector2(numberOfNeighborhoodPixels); - std::vector reversedStdVector3(numberOfNeighborhoodPixels); + ASSERT_EQ(stdVector.size(), numberOfNeighborhoodPixels); + std::vector reversedStdVector1(numberOfNeighborhoodPixels); + std::vector reversedStdVector2(numberOfNeighborhoodPixels); + std::vector reversedStdVector3(numberOfNeighborhoodPixels); // Checks bidirectionality of the ShapedImageNeighborhoodRange iterators! - std::reverse_copy(stdVector.cbegin(), stdVector.cend(), reversedStdVector1.begin()); + std::reverse_copy(std::cbegin(stdVector), std::cend(stdVector), std::begin(reversedStdVector1)); std::reverse_copy(range.begin(), range.end(), reversedStdVector2.begin()); std::reverse_copy(range.cbegin(), range.cend(), reversedStdVector3.begin()); @@ -455,11 +457,13 @@ TEST(ShapedImageNeighborhoodRange, CanBeUsedAsExpressionOfRangeBasedForLoop) const itk::Size radius = { { 0, 1 } }; const std::vector> offsets = itk::GenerateRectangularImageNeighborhoodOffsets(radius); - RangeType range{ *image, location, offsets }; + RangeType range{ *image, location, offsets }; + constexpr PixelType reference_value = 42; for (const PixelType pixel : range) { - EXPECT_NE(pixel, 42); + // Initially not set to reference value + EXPECT_NE(pixel, reference_value); } // Note: instead of 'iterator::reference', you may also type 'auto&&', but @@ -471,12 +475,12 @@ TEST(ShapedImageNeighborhoodRange, CanBeUsedAsExpressionOfRangeBasedForLoop) // https://bugs.llvm.org/show_bug.cgi?id=37392 for (RangeType::iterator::reference pixel : range) { - pixel = 42; + pixel = reference_value; } for (const PixelType pixel : range) { - EXPECT_EQ(pixel, 42); + EXPECT_EQ(pixel, reference_value); } } @@ -911,12 +915,14 @@ TEST(ShapedImageNeighborhoodRange, ProvidesReverseIterators) itk::GenerateRectangularImageNeighborhoodOffsets(radius); RangeType range{ *image, location, offsets }; - const unsigned int numberOfNeighborhoodPixels = 3; + constexpr unsigned int numberOfNeighborhoodPixels = 3; + ASSERT_EQ(numberOfNeighborhoodPixels, range.size()); const std::vector stdVector(range.begin(), range.end()); - std::vector reversedStdVector1(numberOfNeighborhoodPixels); - std::vector reversedStdVector2(numberOfNeighborhoodPixels); - std::vector reversedStdVector3(numberOfNeighborhoodPixels); + ASSERT_EQ(stdVector.size(), numberOfNeighborhoodPixels); + std::vector reversedStdVector1(numberOfNeighborhoodPixels); + std::vector reversedStdVector2(numberOfNeighborhoodPixels); + std::vector reversedStdVector3(numberOfNeighborhoodPixels); std::reverse_copy(stdVector.cbegin(), stdVector.cend(), reversedStdVector1.begin()); diff --git a/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx b/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx index c0111d0f0564..48043848d637 100644 --- a/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx +++ b/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx @@ -42,7 +42,7 @@ itkQuadEdgeMeshEulerOperatorSplitEdgeTest(int, char *[]) auto splitEdge = SplitEdge::New(); std::cout << " " << "Test No Mesh Input"; - if (splitEdge->Evaluate((QEType *)1)) + if (splitEdge->Evaluate((QEType *){})) { std::cout << "FAILED." << std::endl; return EXIT_FAILURE; diff --git a/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx b/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx index 39f36cec512a..86a62abc34cf 100644 --- a/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx +++ b/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx @@ -157,7 +157,7 @@ LabelMapContourOverlayImageFilter::Befor srad.Fill(typename RadiusType::SizeValueType{}); for (unsigned int i = 0, j = 0; i < ImageDimension; ++i) { - if (j != static_cast(m_SliceDimension)) + if (j != static_cast(m_SliceDimension) and j < (ImageDimension - 1)) { srad[j] = m_ContourThickness[i]; ++j; diff --git a/Modules/Segmentation/Watersheds/include/itkWatershedSegmenter.hxx b/Modules/Segmentation/Watersheds/include/itkWatershedSegmenter.hxx index 862df33e8ef2..fa334b8824a0 100644 --- a/Modules/Segmentation/Watersheds/include/itkWatershedSegmenter.hxx +++ b/Modules/Segmentation/Watersheds/include/itkWatershedSegmenter.hxx @@ -1192,17 +1192,20 @@ Segmenter::Threshold(InputImageTypePointer destination, while (!dIt.IsAtEnd()) { InputPixelType tmp = sIt.Get(); + GCC_PRAGMA_PUSH + GCC_SUPPRESS_Wfloat_equal if (tmp < threshold) { dIt.Set(threshold); } - GCC_PRAGMA_PUSH - GCC_SUPPRESS_Wfloat_equal else if (tmp == NumericTraits::max()) { dIt.Set(tmp - NumericTraits::OneValue()); } - else { dIt.Set(tmp); } + else + { + dIt.Set(tmp); + } GCC_PRAGMA_POP ++dIt; ++sIt;