From 72a45394b2905314f5cad18257cbf7028dae341a Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Wed, 18 Mar 2020 10:05:19 -0500 Subject: [PATCH] COMP: Fix extra semi-colon compiler warning ITK demands that macros end with a semi-colon so when the macro is expanded to nothing, there is an exra ; that is complained about. https://stackoverflow.com/questions/28777660/practical-differences-between-do-while-0-and-void0-in-macr/28777865 https://stackoverflow.com/questions/1306611/how-do-i-implement-no-op-macro-or-template-in-c Define new ITK_STATEMENT_NOOP for consistent re-use in functions. pragma-push/pop operations can not have ';' at end, so protect with "// clang-format [off|on]" syntax to ensure consistent formatting across differnet configuration options. --- Modules/Core/Common/include/itkMacro.h | 58 ++++++++++++++----- .../itkScalarImageToRunLengthMatrixFilter.hxx | 19 +++--- .../itkLandmarkBasedTransformInitializer.hxx | 17 +++--- 3 files changed, 63 insertions(+), 31 deletions(-) diff --git a/Modules/Core/Common/include/itkMacro.h b/Modules/Core/Common/include/itkMacro.h index 180cc48590c..7897bd3a448 100644 --- a/Modules/Core/Common/include/itkMacro.h +++ b/Modules/Core/Common/include/itkMacro.h @@ -65,6 +65,13 @@ namespace itk * avoiding compile-time warnings. */ #define itkNotUsed(x) +/** The `do {} while(0)` idiom is commonly employed for + * allowing a macro to be used anywhere that a statement + * is expected, and to enforce consistent use of ; after + * macro */ +// clang-format off +# define ITK_NOOP_STATEMENT do {} while(0) +// clang-format on // Define ITK_PRAGMA macro. // @@ -386,12 +393,13 @@ 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 /** This macro is used to print debug (or other information). They are * also used to catch errors, etc. Example usage looks like: * itkDebugMacro(<< "this is debug info" << this->SomeVariable); */ #if defined(NDEBUG) -# define itkDebugMacro(x) -# define itkDebugStatement(x) +# define itkDebugMacro(x) ITK_NOOP_STATEMENT +# define itkDebugStatement(x) ITK_NOOP_STATEMENT #else # define itkDebugMacro(x) \ do \ @@ -425,8 +433,6 @@ OutputWindowDisplayDebugText(const char *); } \ } while (0) -// The itkDebugStatement is to be used ot protect code that is only -// used in the itkDebugMacro #define itkWarningStatement(x) x #if defined(ITK_CPP_FUNCTION) @@ -598,10 +604,10 @@ OutputWindowDisplayDebugText(const char *); // that are conditionally compiled only when ITK_LEGACY_REMOVE // is off. #if defined(ITK_LEGACY_SILENT) -# define itkLegacyBodyMacro(method, version) -# define itkLegacyReplaceBodyMacro(method, version, replace) -# define itkGenericLegacyBodyMacro(method, version) -# define itkGenericLegacyReplaceBodyMacro(method, version, replace) +# define itkLegacyBodyMacro(method, version) ITK_NOOP_STATEMENT +# define itkLegacyReplaceBodyMacro(method, version, replace) ITK_NOOP_STATEMENT +# define itkGenericLegacyBodyMacro(method, version) ITK_NOOP_STATEMENT +# define itkGenericLegacyReplaceBodyMacro(method, version, replace) ITK_NOOP_STATEMENT #else # define itkLegacyBodyMacro(method, version) \ itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.") @@ -734,7 +740,7 @@ compilers. #ifndef NDEBUG # define itkAssertInDebugAndIgnoreInReleaseMacro(X) assert(X) #else -# define itkAssertInDebugAndIgnoreInReleaseMacro(X) +# define itkAssertInDebugAndIgnoreInReleaseMacro(X) ITK_NOOP_STATEMENT #endif @@ -781,6 +787,7 @@ compilers. return itkDynamicCastInDebugMode(this->ProcessObject::GetInput(#name)); \ } +// clang-format off /** Set a decorated input. This defines the Set"name"() and a Set"name"Input() method */ #define itkSetDecoratedInputMacro(name, type) \ virtual void Set##name##Input(const SimpleDataObjectDecorator * _arg) \ @@ -800,11 +807,17 @@ compilers. const DecoratorType * oldInput = \ itkDynamicCastInDebugMode(this->ProcessObject::GetInput(#name)); \ CLANG_PRAGMA_PUSH \ - CLANG_SUPPRESS_Wfloat_equal if (oldInput && oldInput->Get() == _arg) CLANG_PRAGMA_POP { return; } \ + CLANG_SUPPRESS_Wfloat_equal \ + if (oldInput && oldInput->Get() == _arg) \ + { \ + return; \ + } \ + CLANG_PRAGMA_POP \ typename DecoratorType::Pointer newInput = DecoratorType::New(); \ newInput->Set(_arg); \ this->Set##name##Input(newInput); \ } +// clang-format on /** Set a decorated input. This defines the Set"name"() and Set"name"Input() method */ #define itkGetDecoratedInputMacro(name, type) \ @@ -889,19 +902,21 @@ compilers. itkSetDecoratedObjectInputMacro(name, type) itkGetDecoratedObjectInputMacro(name, type) /** Set built-in type. Creates member Set"name"() (e.g., SetVisibility()); */ +// clang-format off #define itkSetMacro(name, type) \ virtual void Set##name(const type _arg) \ { \ itkDebugMacro("setting " #name " to " << _arg); \ CLANG_PRAGMA_PUSH \ - CLANG_SUPPRESS_Wfloat_equal if (this->m_##name != _arg) \ + CLANG_SUPPRESS_Wfloat_equal \ + if (this->m_##name != _arg) \ { \ this->m_##name = _arg; \ this->Modified(); \ } \ CLANG_PRAGMA_POP \ } - +// clang-format on /** Get built-in type. Creates member Get"name"() (e.g., GetVisibility()); */ #define itkGetMacro(name, type) \ virtual type Get##name() { return this->m_##name; } @@ -970,6 +985,7 @@ compilers. #define itkGetStringMacro(name) \ virtual const char * Get##name() const { return this->m_##name.c_str(); } +// clang-format off /** Set built-in type where value is constrained between min/max limits. * Create member Set"name"() (e.q., SetRadius()). \#defines are * convenience for clamping open-ended values. */ @@ -979,14 +995,17 @@ compilers. const type temp_extrema = (_arg < min ? min : (_arg > max ? max : _arg)); \ itkDebugMacro("setting " << #name " to " << _arg); \ CLANG_PRAGMA_PUSH \ - CLANG_SUPPRESS_Wfloat_equal if (this->m_##name != temp_extrema) \ + CLANG_SUPPRESS_Wfloat_equal \ + if (this->m_##name != temp_extrema) \ { \ this->m_##name = temp_extrema; \ this->Modified(); \ } \ CLANG_PRAGMA_POP \ } +// clang-format on +// clang-format off // NOTE: warning: comparing floating point with == or != is unsafe [-Wfloat-equal] /** Set pointer to object; uses Object reference counting methodology. * Creates method Set"name"() (e.g., SetPoints()). Note that using @@ -997,13 +1016,15 @@ compilers. { \ itkDebugMacro("setting " << #name " to " << _arg); \ CLANG_PRAGMA_PUSH \ - CLANG_SUPPRESS_Wfloat_equal if (this->m_##name != _arg) \ + CLANG_SUPPRESS_Wfloat_equal \ + if (this->m_##name != _arg) \ { \ this->m_##name = _arg; \ this->Modified(); \ } \ CLANG_PRAGMA_POP \ } +// clang-format on /** Get a smart pointer to an object. Creates the member * Get"name"() (e.g., GetPoints()). @@ -1090,6 +1111,7 @@ compilers. virtual void name##On() { this->Set##name(true); } \ virtual void name##Off() { this->Set##name(false); } +// clang-format off /** General set vector macro creates a single method that copies specified * number of values into object. * Examples: void SetColor(c,3) */ @@ -1100,7 +1122,12 @@ compilers. for (i = 0; i < count; i++) \ { \ CLANG_PRAGMA_PUSH \ - CLANG_SUPPRESS_Wfloat_equal if (data[i] != this->m_##name[i]) CLANG_PRAGMA_POP { break; } \ + CLANG_SUPPRESS_Wfloat_equal \ + if (data[i] != this->m_##name[i]) \ + { \ + break; \ + } \ + CLANG_PRAGMA_POP \ } \ if (i < count) \ { \ @@ -1111,6 +1138,7 @@ compilers. } \ } \ } +// clang-format on /** Get vector macro. Returns pointer to type (i.e., array of type). * This is for efficiency. */ diff --git a/Modules/Numerics/Statistics/include/itkScalarImageToRunLengthMatrixFilter.hxx b/Modules/Numerics/Statistics/include/itkScalarImageToRunLengthMatrixFilter.hxx index 6eee0a3b348..a68f0623428 100644 --- a/Modules/Numerics/Statistics/include/itkScalarImageToRunLengthMatrixFilter.hxx +++ b/Modules/Numerics/Statistics/include/itkScalarImageToRunLengthMatrixFilter.hxx @@ -256,15 +256,16 @@ ScalarImageToRunLengthMatrixFilter::Ge output->GetIndex(run, hIndex); output->IncreaseFrequencyOfIndex(hIndex, 1); - itkDebugStatement(typename HistogramType::IndexType tempMeasurementIndex;) - itkDebugStatement(output->GetIndex(run, tempMeasurementIndex);) itkDebugMacro( - "centerIndex<->index: " << static_cast(centerPixelIntensity) << "@" << centerIndex << "<->" - << static_cast(pixelIntensity) << "@" << index << ", Bin# " - << tempMeasurementIndex << ", Measurement: (" << run[0] << ", " << run[1] << ")" - << ", Center bin [" << this->GetOutput()->GetBinMinFromValue(0, run[0]) << "," - << this->GetOutput()->GetBinMaxFromValue(0, run[0]) << "]" - << "~[" << this->GetOutput()->GetBinMinFromValue(1, run[1]) << "," - << this->GetOutput()->GetBinMaxFromValue(1, run[1]) << "]" << std::endl); + itkDebugStatement(typename HistogramType::IndexType tempMeasurementIndex); + itkDebugStatement(output->GetIndex(run, tempMeasurementIndex)); + itkDebugMacro("centerIndex<->index: " << static_cast(centerPixelIntensity) << "@" << centerIndex << "<->" + << static_cast(pixelIntensity) << "@" << index << ", Bin# " + << tempMeasurementIndex << ", Measurement: (" << run[0] << ", " << run[1] + << ")" + << ", Center bin [" << this->GetOutput()->GetBinMinFromValue(0, run[0]) + << "," << this->GetOutput()->GetBinMaxFromValue(0, run[0]) << "]" + << "~[" << this->GetOutput()->GetBinMinFromValue(1, run[1]) << "," + << this->GetOutput()->GetBinMaxFromValue(1, run[1]) << "]" << std::endl); } } } diff --git a/Modules/Registration/Common/include/itkLandmarkBasedTransformInitializer.hxx b/Modules/Registration/Common/include/itkLandmarkBasedTransformInitializer.hxx index 7cca8267d45..da654f40865 100644 --- a/Modules/Registration/Common/include/itkLandmarkBasedTransformInitializer.hxx +++ b/Modules/Registration/Common/include/itkLandmarkBasedTransformInitializer.hxx @@ -440,10 +440,10 @@ LandmarkBasedTransformInitializer::Intern fixedCentered.Fill(0.0); movingCentered.Fill(0.0); - itkDebugStatement(int ii = 0;) + itkDebugStatement(int ii = 0); - // Computations are relative to the Center of Rotation. - while (movingItr != m_MovingLandmarks.end()) + // Computations are relative to the Center of Rotation. + while (movingItr != m_MovingLandmarks.end()) { for (unsigned int i = 0; i < ImageDimension; i++) { @@ -460,7 +460,8 @@ LandmarkBasedTransformInitializer::Intern } } - itkDebugStatement(++ii;) itkDebugMacro(<< "f_" << ii << " = " << fixedCentered); + itkDebugStatement(++ii); + itkDebugMacro(<< "f_" << ii << " = " << fixedCentered); itkDebugMacro(<< "m_" << ii << " = " << movingCentered); ++movingItr; @@ -613,8 +614,9 @@ LandmarkBasedTransformInitializer::Intern fixedCentered.Fill(0.0); movingCentered.Fill(0.0); - itkDebugStatement(int ii = 0;) double s_dot = 0; - double s_cross = 0; + itkDebugStatement(int ii = 0); + double s_dot = 0; + double s_cross = 0; // Computations are relative to the Center of Rotation. while (movingItr != m_MovingLandmarks.end()) { @@ -626,7 +628,8 @@ LandmarkBasedTransformInitializer::Intern s_dot += (movingCentered[0] * fixedCentered[0]) + (movingCentered[1] * fixedCentered[1]); s_cross += (movingCentered[1] * fixedCentered[0]) - (movingCentered[0] * fixedCentered[1]); - itkDebugStatement(++ii;) itkDebugMacro(<< "f_" << ii << " = " << fixedCentered); + itkDebugStatement(++ii); + itkDebugMacro(<< "f_" << ii << " = " << fixedCentered); itkDebugMacro(<< "m_" << ii << " = " << movingCentered); ++movingItr;