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.
  • Loading branch information
seanm authored and hjmjohnson committed Apr 27, 2024
1 parent 1986b54 commit 5bf59e1
Show file tree
Hide file tree
Showing 19 changed files with 727 additions and 668 deletions.
10 changes: 10 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ Standard: Cpp11
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
- GCC_PRAGMA_PUSH
- GCC_PRAGMA_POP
- GCC_SUPPRESS_Wfloat_equal
- GCC_SUPPRESS_Wformat_nonliteral
- CLANG_PRAGMA_PUSH
- CLANG_PRAGMA_POP
- CLANG_SUPPRESS_Wcpp14_extensions
- INTEL_PRAGMA_WARN_PUSH
- INTEL_PRAGMA_WARN_POP
- INTEL_SUPPRESS_warning_1292
TabWidth: 2
UseTab: Never
...
9 changes: 5 additions & 4 deletions .github/workflows/itk_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Crofton's
Cuadra
Cz
DDataFrameobject
Dz
Dasarathy
Deref
Dij
Expand All @@ -55,6 +54,7 @@ Dxx
Dyx
Dyy
Dyz
Dz
Dzx
Dzy
Dzz
Expand All @@ -74,13 +74,13 @@ Fletch
Freesurfer
Frenet
GEAdw
GPUSuperclass's
Gao
Gibbsian
Gifti
Golby
Graphviz
Greenleaf
Gifti
GPUSuperclass's
Guillen
Haar
Hackathon
Expand All @@ -95,11 +95,11 @@ Hilden
Hiraki
Hirschberg
Hostname
Intelli
IOregion
ITKIOMeshGifti
ITKImageToIplImage
ITKOptimizersv
Intelli
Irfan
Irix
Isocenter
Expand Down Expand Up @@ -740,6 +740,7 @@ ponderation
posn
postcondition
posteriori
pragmas
preallocated
precalculate
precalculating
Expand Down
14 changes: 8 additions & 6 deletions Modules/Core/Common/include/itkConceptChecking.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,11 @@ struct EqualityComparable
void
constraints()
{
CLANG_PRAGMA_PUSH
CLANG_SUPPRESS_Wfloat_equal Detail::RequireBooleanExpression(a == b);
GCC_PRAGMA_PUSH
GCC_SUPPRESS_Wfloat_equal
Detail::RequireBooleanExpression(a == b);
Detail::RequireBooleanExpression(a != b);
CLANG_PRAGMA_POP
GCC_PRAGMA_POP
}

T1 a;
Expand All @@ -331,10 +332,11 @@ struct Comparable
Detail::RequireBooleanExpression(a > b);
Detail::RequireBooleanExpression(a <= b);
Detail::RequireBooleanExpression(a >= b);
CLANG_PRAGMA_PUSH
CLANG_SUPPRESS_Wfloat_equal Detail::RequireBooleanExpression(a == b);
GCC_PRAGMA_PUSH
GCC_SUPPRESS_Wfloat_equal
Detail::RequireBooleanExpression(a == b);
Detail::RequireBooleanExpression(a != b);
CLANG_PRAGMA_POP
GCC_PRAGMA_POP
}

T1 a;
Expand Down
56 changes: 32 additions & 24 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,27 @@ namespace itk
// to be quoted.
#define ITK_PRAGMA(x) _Pragma(#x)

// The clang compiler has many useful non-default compiler warnings
// that tend to have a high false positive rate.
// The following set of defines allows us to suppress false positives
// and still track down suspicious code
// The GCC/Clang compilers have many useful non-default compiler warnings
// that tend to have a high false positive rate or are otherwise not always appropriate.
// The following set of defines allows us to suppress instances of said warnings.

// For GCC and Clang (Clang also identifies itself as GCC, and supports these pragmas):
#if defined(__GNUC__)
# define GCC_PRAGMA_PUSH ITK_PRAGMA(GCC diagnostic push)
# define GCC_PRAGMA_POP ITK_PRAGMA(GCC diagnostic pop)
# define GCC_SUPPRESS_Wfloat_equal ITK_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
# define GCC_SUPPRESS_Wformat_nonliteral ITK_PRAGMA(GCC diagnostic ignored "-Wformat-nonliteral")
#else
# define GCC_PRAGMA_PUSH
# define GCC_PRAGMA_POP
# define GCC_SUPPRESS_Wfloat_equal
# define GCC_SUPPRESS_Wformat_nonliteral
#endif

// For Clang only (and not GCC):
#if defined(__clang__) && defined(__has_warning)
# define CLANG_PRAGMA_PUSH ITK_PRAGMA(clang diagnostic push)
# define CLANG_PRAGMA_POP ITK_PRAGMA(clang diagnostic pop)
# if __has_warning("-Wfloat-equal")
# define CLANG_SUPPRESS_Wfloat_equal ITK_PRAGMA(clang diagnostic ignored "-Wfloat-equal")
# else
# define CLANG_SUPPRESS_Wfloat_equal
# endif
# if __has_warning("-Wc++14-extensions")
# define CLANG_SUPPRESS_Wcpp14_extensions ITK_PRAGMA(clang diagnostic ignored "-Wc++14-extensions")
# else
Expand All @@ -119,7 +128,6 @@ namespace itk
#else
# define CLANG_PRAGMA_PUSH
# define CLANG_PRAGMA_POP
# define CLANG_SUPPRESS_Wfloat_equal
# define CLANG_SUPPRESS_Wcpp14_extensions
#endif

Expand Down Expand Up @@ -887,13 +895,13 @@ compilers.
itkDebugMacro("setting input " #name " to " << _arg); \
const DecoratorType * oldInput = \
itkDynamicCastInDebugMode<const DecoratorType *>(this->ProcessObject::GetInput(#name)); \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
if (oldInput && oldInput->Get() == _arg) \
{ \
return; \
} \
CLANG_PRAGMA_POP \
GCC_PRAGMA_POP \
auto newInput = DecoratorType::New(); \
newInput->Set(_arg); \
this->Set##name##Input(newInput); \
Expand Down Expand Up @@ -991,17 +999,17 @@ compilers.
/** Set built-in type or regular C++ type. Creates member Set"name"() (e.g., SetVisibility()); */
// clang-format off
#define itkSetMacro(name, type) \
virtual void Set##name(type _arg) \
virtual void Set##name(type _arg) \
{ \
itkDebugMacro("setting " #name " to " << _arg); \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
if (this->m_##name != _arg) \
{ \
this->m_##name = std::move(_arg); \
this->m_##name = std::move(_arg); \
this->Modified(); \
} \
CLANG_PRAGMA_POP \
GCC_PRAGMA_POP \
} \
ITK_MACROEND_NOOP_STATEMENT
// clang-format on
Expand Down Expand Up @@ -1089,14 +1097,14 @@ compilers.
{ \
const type temp_extrema = (_arg <= min ? min : (_arg >= max ? max : _arg)); \
itkDebugMacro("setting " << #name " to " << _arg); \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
if (this->m_##name != temp_extrema) \
{ \
this->m_##name = temp_extrema; \
this->Modified(); \
} \
CLANG_PRAGMA_POP \
GCC_PRAGMA_POP \
} \
ITK_MACROEND_NOOP_STATEMENT
// clang-format on
Expand Down Expand Up @@ -1217,13 +1225,13 @@ compilers.
unsigned int i; \
for (i = 0; i < count; ++i) \
{ \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
if (data[i] != this->m_##name[i]) \
{ \
break; \
} \
CLANG_PRAGMA_POP \
GCC_PRAGMA_POP \
} \
if (i < count) \
{ \
Expand Down
7 changes: 4 additions & 3 deletions Modules/Core/Common/include/itkMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,10 @@ template <typename TInput1, typename TInput2>
inline bool
ExactlyEquals(const TInput1 & x1, const TInput2 & x2)
{
CLANG_PRAGMA_PUSH
CLANG_SUPPRESS_Wfloat_equal return x1 == x2;
CLANG_PRAGMA_POP
GCC_PRAGMA_PUSH
GCC_SUPPRESS_Wfloat_equal
return x1 == x2;
GCC_PRAGMA_POP
}

// The NotExactlyEquals function
Expand Down
12 changes: 6 additions & 6 deletions Modules/Core/Common/include/itkMathDetail.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ namespace Detail
////////////////////////////////////////
// Base versions

CLANG_PRAGMA_PUSH
CLANG_SUPPRESS_Wfloat_equal
GCC_PRAGMA_PUSH
GCC_SUPPRESS_Wfloat_equal

template <typename TReturn, typename TInput>
inline TReturn
RoundHalfIntegerToEven_base(TInput x)
template <typename TReturn, typename TInput>
inline TReturn
RoundHalfIntegerToEven_base(TInput x)
{
if (NumericTraits<TInput>::IsNonnegative(x))
{
Expand Down Expand Up @@ -135,7 +135,7 @@ Ceil_base(TInput x)
return (NumericTraits<TInput>::IsNegative(x)) ? r : (x == static_cast<TInput>(r) ? r : r + static_cast<TReturn>(1));
}

CLANG_PRAGMA_POP
GCC_PRAGMA_POP

////////////////////////////////////////
// 32 bits versions
Expand Down
6 changes: 3 additions & 3 deletions Modules/Core/Common/include/itkMultiThreaderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ CLANG_PRAGMA_PUSH
CLANG_SUPPRESS_Wcpp14_extensions
// clang-format on
# ifdef ITK_LEGACY_SILENT
struct ThreadInfoStruct
struct ThreadInfoStruct
# else
struct [[deprecated("Use WorkUnitInfo, ThreadInfoStruct is deprecated since ITK 5.0")]] ThreadInfoStruct
struct [[deprecated("Use WorkUnitInfo, ThreadInfoStruct is deprecated since ITK 5.0")]] ThreadInfoStruct
# endif
// clang-format off
// clang-format off
CLANG_PRAGMA_POP
INTEL_PRAGMA_WARN_POP
// clang-format on
Expand Down
33 changes: 19 additions & 14 deletions Modules/Core/Common/test/itkMathRoundProfileTest1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,25 @@ itkMathRoundTestHelperFunction(double x)
return static_cast<int>(x >= 0. ? x : (itk::Math::ExactlyEquals(x, static_cast<int>(x)) ? x : x - 1.));
}

#define itkRoundMacro(x, y) \
if (x >= 0.5) \
{ \
y = static_cast<int>(x + 0.5); \
} \
else \
{ \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal if ((x + 0.5) == static_cast<int>(x + 0.5)) CLANG_PRAGMA_POP \
{ \
y = static_cast<int>(x + 0.5); \
} \
else { y = static_cast<int>(x - 0.5); } \
} \
#define itkRoundMacro(x, y) \
if (x >= 0.5) \
{ \
y = static_cast<int>(x + 0.5); \
} \
else \
{ \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
if ((x + 0.5) == static_cast<int>(x + 0.5)) \
{ \
y = static_cast<int>(x + 0.5); \
} \
else \
{ \
y = static_cast<int>(x - 0.5); \
} \
GCC_PRAGMA_POP \
} \
ITK_MACROEND_NOOP_STATEMENT

int
Expand Down
7 changes: 4 additions & 3 deletions Modules/Core/Common/test/itkRealTimeIntervalTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
#define CHECK_FOR_VALUE(a, b) \
{ \
double eps = 4.0 * itk::NumericTraits<double>::epsilon(); \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal eps = (b == 0.0) ? eps : itk::Math::abs(b * eps); \
CLANG_PRAGMA_POP \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
eps = (b == 0.0) ? eps : itk::Math::abs(b * eps); \
GCC_PRAGMA_POP \
if (itk::Math::abs(a - b) > eps) \
{ \
std::cerr << "Error in " #a << " expected " << b << " but got " << a << std::endl; \
Expand Down
7 changes: 4 additions & 3 deletions Modules/Core/Common/test/itkRealTimeStampTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
#define CHECK_FOR_VALUE(a, b) \
{ \
double eps = 4.0 * itk::NumericTraits<double>::epsilon(); \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal eps = (b == 0.0) ? eps : itk::Math::abs(b * eps); \
CLANG_PRAGMA_POP \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
eps = (b == 0.0) ? eps : itk::Math::abs(b * eps); \
GCC_PRAGMA_POP \
if (itk::Math::abs(a - b) > eps) \
{ \
std::cerr << "Error in " #a << " expected " << b << " but got " << a << std::endl; \
Expand Down
6 changes: 4 additions & 2 deletions Modules/Core/Common/test/itkVariableLengthVectorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
#include "itkMath.h"

#define ASSERT(cond, text) \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal if (!(cond)) CLANG_PRAGMA_POP \
GCC_PRAGMA_PUSH \
GCC_SUPPRESS_Wfloat_equal \
if (!(cond)) \
{ \
std::cerr << __FILE__ << ':' << __LINE__ << ':' << "Assertion failed: " << #cond << ": " << text << std::endl; \
result = EXIT_FAILURE; \
} \
GCC_PRAGMA_POP \
ITK_MACROEND_NOOP_STATEMENT

int
Expand Down
Loading

0 comments on commit 5bf59e1

Please sign in to comment.