gh-136952: Fix C analyzer handling of static assertions - #156969
Open
ShellWen wants to merge 2 commits into
Open
gh-136952: Fix C analyzer handling of static assertions#156969ShellWen wants to merge 2 commits into
ShellWen wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
🟢 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_assertcompatibility expansions. - Add parser support to skip
_Static_assert/static_assertat 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
force-pushed
the
gh-136952-c-analyzer-static-assert
branch
from
September 6, 2026 08:00
b0b0ae5 to
8110334
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.cto fail when configured with--disable-gil.This change:
_Static_assertandstatic_assertdeclarations at file scopeand inside structs and unions, reusing the existing parenthesis-matching
logic.
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:
python -m test test_tools: 80 tests run, 1 skipped, successful.check-c-globals.pyonPython/gc.c,Python/gc_free_threading.c, andObjects/listobject.c:0 failures.
the preprocessing regression test fails.
git diff --checkpasses.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.
--disable-gilis set #136952