Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ StatementMacros:
- ITK_CLANG_PRAGMA_POP
- ITK_CLANG_SUPPRESS_Wzero_as_null_pointer_constant
- ITK_CLANG_SUPPRESS_Wduplicate_enum
- ITK_CLANG_SUPPRESS_Wallocator_wrappers
- CLANG_PRAGMA_PUSH
- CLANG_PRAGMA_POP
- CLANG_SUPPRESS_Wfloat_equal
Expand Down
38 changes: 38 additions & 0 deletions Documentation/AI/compiler-cautions.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,41 @@ for C++17, ITK's minimum, and reads the same in every standard.
a check against a newer standard library belongs in a test `.cxx`.
- The `check-no-cxx-standard-tests-in-headers` pre-commit hook rejects
single-line violations only; check backslash continuations by hand.

---

## 17. When a Warning May Be Suppressed

Fix the code first. A suppression is acceptable only when all four hold:

1. **The warning is a confirmed false positive, or the code is not ITK's to
change** (vendored `Modules/ThirdParty/`, a system header). "Too many
warnings" is not a reason. A fix to vendored code goes upstream first; see
[ThirdPartyForkConventions.md](../Maintenance/ThirdPartyForkConventions.md).
2. **The scope is the narrowest that works.** In order of preference:
`ITK_CLANG_PRAGMA_PUSH` / `ITK_CLANG_SUPPRESS_*` / `ITK_CLANG_PRAGMA_POP`
around one definition or one `#include`; a flag on one target; a
`CTEST_CUSTOM_WARNING_EXCEPTION` for one file. A project-wide `-Wno-` flag
in `CMake/ITKSetStandardCompilerFlags.cmake` needs reviewer agreement.
3. **The source says why, in one line**, at the suppression. The commit
message carries the evidence.
4. **The line names what lets it be removed**: an upstream fix, an API
change, or a compiler version.

New `ITK_CLANG_SUPPRESS_<warning>` macros go in `itkMacro.h` behind
`__has_warning`, so compilers without the warning see no unknown option, and
in the `StatementMacros` list of `.clang-format`, so clang-format does not
join the macro to the declaration that follows it.

Worked example: AppleClang's `-Wallocator-wrappers` flags a function whose
name contains "alloc" and that returns a byte-typed pointer (`void *`,
`char *`, `unsigned char *`) obtained from an allocation. In
`ImportImageContainer<…, unsigned char>::AllocateElements` this is a false
positive, since `new TElement[n]` is already typed. Changing the virtual
function's return type to `std::unique_ptr<TElement[]>` would silence it but
breaks every subclass that overrides it, so the definition alone is wrapped
in `ITK_CLANG_SUPPRESS_Wallocator_wrappers`, with the return-type change as
the removal condition. The same warning on vendored Eigen, VNL, and
PocketFFT wrappers, which do erase the type, is left for upstream fixes. A
project-wide `-Wno-allocator-wrappers` was rejected because it would also hide
future instances in ITK code.
4 changes: 4 additions & 0 deletions Modules/Core/Common/include/itkImportImageContainer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ ImportImageContainer<TElementIdentifier, TElement>::SetImportPointer(TElement *
this->Modified();
}

// new TElement[] is already typed; remove when AllocateElements returns std::unique_ptr.
ITK_CLANG_PRAGMA_PUSH
ITK_CLANG_SUPPRESS_Wallocator_wrappers
template <typename TElementIdentifier, typename TElement>
TElement *
ImportImageContainer<TElementIdentifier, TElement>::AllocateElements(ElementIdentifier size,
Expand Down Expand Up @@ -180,6 +183,7 @@ ImportImageContainer<TElementIdentifier, TElement>::AllocateElements(ElementIden
}
return data;
}
ITK_CLANG_PRAGMA_POP

template <typename TElementIdentifier, typename TElement>
void
Expand Down
6 changes: 6 additions & 0 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ namespace itk
# else
# define ITK_CLANG_SUPPRESS_Wduplicate_enum
# endif
# if __has_warning("-Wallocator-wrappers")
# define ITK_CLANG_SUPPRESS_Wallocator_wrappers ITK_PRAGMA(clang diagnostic ignored "-Wallocator-wrappers")
# else
# define ITK_CLANG_SUPPRESS_Wallocator_wrappers
# endif
#else
# define ITK_CLANG_PRAGMA_PUSH
# define ITK_CLANG_PRAGMA_POP
# define ITK_CLANG_SUPPRESS_Wzero_as_null_pointer_constant
# define ITK_CLANG_SUPPRESS_Wduplicate_enum
# define ITK_CLANG_SUPPRESS_Wallocator_wrappers
#endif

// These were not intended as public API, but some code was nevertheless using them.
Expand Down
Loading