Skip to content

fix GCC PCH Breakage Triggered by fmt Scoped #pragma GCC optimize("0g")#4768

Open
theflashwin wants to merge 1 commit intofmtlib:mainfrom
theflashwin:issue-4757
Open

fix GCC PCH Breakage Triggered by fmt Scoped #pragma GCC optimize("0g")#4768
theflashwin wants to merge 1 commit intofmtlib:mainfrom
theflashwin:issue-4757

Conversation

@theflashwin
Copy link
Copy Markdown

@theflashwin theflashwin commented May 5, 2026

Issue

Issue: #4757

Changes

Added a preprocessor flag FMT_DISABLE_DEBUG_OPTIMIZE_PRAGMA that if set prevents FMT_PRAGMA_GCC(optimize("0g")) flag from being set, preventing the gcc compiler option leak.

Testing

Verified fix works on Fedora 36/44 with gcc 12/16 with the following Dockerfile:

FROM --platform=linux/amd64 fedora:44

RUN dnf install -y gcc-c++ && dnf clean all

WORKDIR /fmt
COPY . .

# create bug repro files
# (include base.h in pch header and some user code in main.cpp that will trigger error)
RUN mkdir -p /repro && \
    printf '#include <fmt/base.h>\n' > /repro/pch.h && \
    printf '#include <emmintrin.h>\nint shift_right(__m128i x) {\n    __m128i result = _mm_srli_si128(x, 2);\n    return _mm_cvtsi128_si32(result);\n}\n' > /repro/main.cpp

# Compiles project twice
# [1/2] - compile code without fix (should print FAILED)
# [2/2] - compile code with fix (should print OK - fix works)
RUN printf '#!/bin/bash\n\
echo "GCC: $(g++ --version | head -1)"\n\
echo ""\n\
echo "[1] Precompiling PCH without fix..."\n\
g++ -msse2 -g -O0 -I/fmt/include -x c++-header /repro/pch.h -o /repro/pch.h.gch\n\
echo "[2] Compiling WITHOUT fix (should fail):"\n\
if g++ -msse2 -g -O0 -I/fmt/include -include /repro/pch.h -c /repro/main.cpp -o /dev/null 2>&1; then\n\
    echo "    OK (bug not triggered)"\n\
else\n\
    echo "    FAILED - bug confirmed"\n\
fi\n\
echo ""\n\
echo "[3] Precompiling PCH WITH fix..."\n\
g++ -msse2 -g -O0 -I/fmt/include -DFMT_DISABLE_DEBUG_OPTIMIZE_PRAGMA -x c++-header /repro/pch.h -o /repro/pch.h.gch\n\
echo "[4] Compiling WITH fix (should succeed):"\n\
if g++ -msse2 -g -O0 -I/fmt/include -DFMT_DISABLE_DEBUG_OPTIMIZE_PRAGMA -include /repro/pch.h -c /repro/main.cpp -o /dev/null 2>&1; then\n\
    echo "    OK - fix works!"\n\
else\n\
    echo "    FAILED - fix did not work"\n\
fi\n\
' > /repro/run.sh && chmod +x /repro/run.sh

CMD ["/repro/run.sh"]

Copy link
Copy Markdown
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Thanks for the PR

Comment thread include/fmt/base.h Outdated
#endif

// Enable minimal optimizations for more compact code in debug mode.
#if !defined(FMT_DISABLE_DEBUG_OPTIMIZE_PRAGMA)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's move this check to the #if below and don't touch push_options/pop_options.

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.

Just updated! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants