Skip to content

gh-136952: Fix C analyzer handling of static assertions - #156969

Open
ShellWen wants to merge 2 commits into
python:mainfrom
ShellWen:gh-136952-c-analyzer-static-assert
Open

gh-136952: Fix C analyzer handling of static assertions#156969
ShellWen wants to merge 2 commits into
python:mainfrom
ShellWen:gh-136952-c-analyzer-static-assert

Conversation

@ShellWen

@ShellWen ShellWen commented Sep 5, 2026

Copy link
Copy Markdown

Fixes #136952.

The C analyzer preprocesses source files in C99 mode. With the headers used
in the reported configuration, static assertions expand into compatibility
declarations that the parser cannot handle. This causes analysis of
Python/gc_free_threading.c to fail when configured with --disable-gil.

This change:

  • Uses C11 preprocessing to avoid the compatibility expansion.
  • Handles _Static_assert and static_assert declarations at file scope
    and inside structs and unions, reusing the existing parenthesis-matching
    logic.
  • Adds focused regression tests to verify that declarations following
    static assertions are still discovered.

The assertions in CPython's C code remain unchanged.

Testing

Validated with a locally built CPython 3.16 free-threading interpreter:

  • Reproduced the reported analysis failure before the fix.
  • Ran python -m test test_tools: 80 tests run, 1 skipped, successful.
  • Ran check-c-globals.py on Python/gc.c,
    Python/gc_free_threading.c, and Objects/listobject.c:
    0 failures.
  • Temporarily restored C99 preprocessing in memory and confirmed that
    the preprocessing regression test fails.
  • git diff --check passes.

The full CPython test suite was not run.

AI assistance

GPT-6 Astra via OpenCode v2 assisted with investigating the issue,
implementing the changes, and preparing the tests and this PR description.

Copilot AI lite review requested due to automatic review settings September 5, 2026 06:43
@python-cla-bot

python-cla-bot Bot commented Sep 5, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The change is tightly scoped to the tooling, directly addresses the reported failure mode, and includes targeted regression tests for both parsing and preprocessing behavior.

Pull request overview

This PR fixes the CPython C analyzer failing to parse free-threading builds where static assertions expand into declarations the parser can’t handle, by switching preprocessing to C11 and teaching the parser to skip _Static_assert / static_assert declarations while still discovering subsequent declarations.

Changes:

  • Preprocess analyzed C sources in C11 mode to avoid C99-era static_assert compatibility expansions.
  • Add parser support to skip _Static_assert / static_assert at file scope and inside struct/union bodies using existing parenthesis-matching logic.
  • Add regression tests covering global-scope, struct-scope, and real-preprocessor scenarios to ensure declarations after static assertions are still found.
File summaries
File Description
Tools/c-analyzer/c_parser/preprocessor/gcc.py Switch preprocessor standard from C99 to C11 to avoid problematic static-assert expansions.
Tools/c-analyzer/c_parser/parser/_common.py Add skip_static_assert() helper that consumes static-assert declarations (including multi-line) and preserves trailing declarations.
Tools/c-analyzer/c_parser/parser/_global.py Apply skip_static_assert() in global parsing so static assertions don’t block discovery of globals.
Tools/c-analyzer/c_parser/parser/_compound_decl_body.py Apply skip_static_assert() in struct/union member parsing so static assertions don’t block field discovery.
Lib/test/test_tools/test_c_analyzer.py Add focused regression tests for parser behavior and GCC preprocessing output.
Misc/NEWS.d/next/Tools-Demos/2026-09-05-00-00-00.gh-issue-136952.Jx7mQp.rst Document the tool fix in NEWS.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@ShellWen
ShellWen force-pushed the gh-136952-c-analyzer-static-assert branch from b0b0ae5 to 8110334 Compare September 6, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The C analyzer seems to fail when --disable-gil is set

2 participants