Skip to content

Commit

Permalink
COMP: Suppress some Wformat-nonliteral warnings
Browse files Browse the repository at this point in the history
Introduced GCC_PRAGMA_PUSH macro and friends, similiar to existing CLANG_PRAGMA_PUSH and friends, but that applies to both GCC and Clang, which share many warning flags.

Added macros to suppess -Wformat-nonliteral warnings. Used them in a few places where the warnings are impossible to fix with ITK's current API.

Made -Wfloat-equal apply to GCC now, in addition to Clang.

Also added these macros as StatementMacros in the .clang-format file, so that clang-format formats them more nicely.  Redid some formatting.

Remove warnings about using c++14 features

We now require c++14, so do not warn if c++14 features
are used.

Avoid gcc13.2 compiler out-of-bounds index warnings

The gcc 13.2 compiler warns about potential index out
of bounds errors in cases where the index is not explicitly
requested is not explicitly tested.

Avoid using index operator when index would be
out of known compile time bounds.
  • Loading branch information
seanm authored and hjmjohnson committed Apr 27, 2024
1 parent 6e75e99 commit a8246c3
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 35 deletions.
15 changes: 7 additions & 8 deletions Modules/Core/Common/include/itkExtractImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ ExtractImageFilter<TInputImage, TOutputImage>::SetExtractionRegion(InputImageReg
"InputImageDimension must be greater than OutputImageDimension");
m_ExtractionRegion = extractRegion;

unsigned int nonzeroSizeCount = 0;
InputImageSizeType inputSize = extractRegion.GetSize();
OutputImageSizeType outputSize;
outputSize.Fill(0);
Expand All @@ -73,12 +72,16 @@ ExtractImageFilter<TInputImage, TOutputImage>::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;
}
}
Expand Down Expand Up @@ -116,11 +119,7 @@ ExtractImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
outputPtr->SetLargestPossibleRegion(m_OutputImageRegion);

// Set the output spacing and origin
const ImageBase<InputImageDimension> * phyData;

phyData = dynamic_cast<const ImageBase<InputImageDimension> *>(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
Expand Down
19 changes: 14 additions & 5 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,20 @@ namespace itk
# define itkExposeEnumValue(name) static_cast<int>(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(),
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions Modules/Core/Common/include/itkMultiThreaderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 10 additions & 0 deletions Modules/Core/Common/include/itkNeighborhood.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -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];
}

Expand Down
5 changes: 5 additions & 0 deletions Modules/Core/Common/include/itkNeighborhoodOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class ITK_TEMPLATE_EXPORT NeighborhoodOperator : public Neighborhood<TPixel, VDi
void
SetDirection(const unsigned long direction)
{
if (direction >= VDimension)
{
itkExceptionMacro(<< " Can not set direction " << direction << " greater than dimensionality of neighborhood "
<< VDimension);
}
m_Direction = direction;
}

Expand Down
32 changes: 19 additions & 13 deletions Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<PixelType>;
enum
{
Expand All @@ -366,14 +366,16 @@ TEST(ShapedImageNeighborhoodRange, IteratorsCanBePassedToStdReverseCopy)
itk::ShapedImageNeighborhoodRange<ImageType> range{ *image, location, offsets };

const unsigned int numberOfNeighborhoodPixels = 3;
ASSERT_EQ(numberOfNeighborhoodPixels, range.size());

const std::vector<PixelType> stdVector(range.begin(), range.end());
std::vector<PixelType> reversedStdVector1(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector2(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector3(numberOfNeighborhoodPixels);
ASSERT_EQ(stdVector.size(), numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector1(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector2(numberOfNeighborhoodPixels);
std::vector<PixelType> 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());

Expand Down Expand Up @@ -455,11 +457,13 @@ TEST(ShapedImageNeighborhoodRange, CanBeUsedAsExpressionOfRangeBasedForLoop)
const itk::Size<ImageType::ImageDimension> radius = { { 0, 1 } };
const std::vector<itk::Offset<ImageType::ImageDimension>> 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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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<PixelType> stdVector(range.begin(), range.end());
std::vector<PixelType> reversedStdVector1(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector2(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector3(numberOfNeighborhoodPixels);
ASSERT_EQ(stdVector.size(), numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector1(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector2(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector3(numberOfNeighborhoodPixels);

std::reverse_copy(stdVector.cbegin(), stdVector.cend(), reversedStdVector1.begin());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ LabelMapContourOverlayImageFilter<TLabelMap, TFeatureImage, TOutputImage>::Befor
srad.Fill(typename RadiusType::SizeValueType{});
for (unsigned int i = 0, j = 0; i < ImageDimension; ++i)
{
if (j != static_cast<unsigned int>(m_SliceDimension))
if (j != static_cast<unsigned int>(m_SliceDimension) and j < (ImageDimension - 1))
{
srad[j] = m_ContourThickness[i];
++j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,17 +1192,20 @@ Segmenter<TInputImage>::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<InputPixelType>::max())
{
dIt.Set(tmp - NumericTraits<InputPixelType>::OneValue());
}
else { dIt.Set(tmp); }
else
{
dIt.Set(tmp);
}
GCC_PRAGMA_POP
++dIt;
++sIt;
Expand Down

0 comments on commit a8246c3

Please sign in to comment.