Skip to content

Fix GCC -Wpedantic and -Wunused-but-set-variable warnings - #347

Open
Joel Kiptoo (Kiptoo-Deus) wants to merge 2 commits into
microsoft:mainfrom
Kiptoo-Deus:fix-gcc-pedantic-warnings
Open

Joel Kiptoo (Kiptoo-Deus) wants to merge 2 commits into
microsoft:mainfrom
Kiptoo-Deus:fix-gcc-pedantic-warnings

Conversation

@Kiptoo-Deus

Copy link
Copy Markdown

Fixes #342.

GCC reports ISO C++ prohibits anonymous structs [-Wpedantic] for the anonymous structs in the XMFLOAT*, XMINT*, XMUINT* and PackedVector types. Clang's equivalent warnings are already suppressed by the existing #pragma clang diagnostic blocks in DirectXMath.h and DirectXPackedVector.h; this adds matching #pragma GCC diagnostic blocks (guarded by defined(__GNUC__) && !defined(__clang__), so clang is unaffected) inside them.

The anonymous struct in XMMATRIX (_XM_NO_INTRINSICS_ only) is outside those blocks, and clang warns about it too (-Wgnu-anonymous-struct, -Wnested-anon-types), so it gets its own push/pop for both compilers.

In XMMatrixDecompose, only cc of the second XM3RANKDECOMPOSE is used, so aa and bb are marked (void) to fix -Wunused-but-set-variable.

Testing

Built a small program including DirectXMath.h, DirectXPackedVector.h, DirectXCollision.h and DirectXColors.h (system headers first, sal.h from dotnet/runtime, per the README) with -Wall -Wextra -Wpedantic -O2, for C++14, C++17 and C++20, with GCC 15.1 and Apple clang 21 on macOS. It calls XMMatrixDecompose on a known scale/rotation/translation and checks the result.

Warnings from this change's categories (-Wpedantic, -Wunused-but-set-variable), same for all three standards:

GCC before GCC after clang before clang after
arm64 (NEON) 36 0 0 0
x86_64 (SSE) 36 0 0 0
_XM_NO_INTRINSICS_ 37 0 2 0

All builds pass and the program returns the expected result. Anonymous structs in user code after the includes still warn with both compilers, so the suppression does not leak past the headers.

I don't have MSVC available to test with; the change only adds preprocessor blocks that MSVC skips, and the (void) casts.

Not included

GCC also reports -Wstrict-aliasing ("dereferencing type-punned pointer will break strict-aliasing rules"), which is not in this issue: 2 sites in DirectXPackedVector.inl (e.g. reinterpret_cast<float*>(&Result)[0] in XMConvertHalfToFloat) on the x86_64 and no-intrinsics paths, and 24 on the arm64 NEON path (DirectXMathVector.inl, DirectXMathConvert.inl, DirectXMath.h). These are real type-punning rather than style warnings, so I left them out of this PR; I'm happy to open a separate issue or PR if you'd like them addressed, and in which style (memcpy or otherwise).

GCC reports "ISO C++ prohibits anonymous structs" for the anonymous
structs in the XMFLOAT*, XMINT*, XMUINT* and PackedVector types. Clang's
equivalent warnings are already suppressed by the existing
"#pragma clang diagnostic" blocks, so add matching GCC blocks.

The anonymous struct in XMMATRIX (_XM_NO_INTRINSICS_ only) is outside
those blocks and also warns with clang, so wrap it for both compilers.

In XMMatrixDecompose, only cc of the second XM3RANKDECOMPOSE is used,
so mark aa and bb as unused.

Fixes microsoft#342
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread Inc/DirectXMath.h Outdated
Comment on lines +624 to +626
#endif
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use #elif defined(__GNUC__) ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, since clang defines __GNUC__ too, the #elif does the same job with less noise. Changed it everywhere.

@walbourn Chuck Walbourn (walbourn) added the complier Issue related to compiler codegen label Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

complier Issue related to compiler codegen

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler warnings when building with GCC

2 participants