Skip to content

[clang] preserve the static_assert keyword in C++ capture - #2330

Merged
dulmarod merged 1 commit into
meta:mainfrom
VladimirMakaev:pr/static-assert-1125
Dec 2, 2026
Merged

dulmarod merged 1 commit into
meta:mainfrom
VladimirMakaev:pr/static-assert-1125

Conversation

@VladimirMakaev

Copy link
Copy Markdown
Contributor

The force-included variadic macro erased C++ assertions and made directives in their operands
invalid macro arguments. Restrict it to C. The compiler checks C++ assertions and the frontend
continues to skip StaticAssertDecl when producing runtime instructions.

For example, capture used to reject this valid packed-layout check:

static_assert([] {
#pragma pack(push, 1)
struct Packed { char first; int second; };
#pragma pack(pop)
return sizeof(Packed) == sizeof(char) + sizeof(int);
}(), "packed layout");

Legacy C macro behavior is retained.

Limitations: C++ assertions failing under the capture compiler now fail capture of that unit.
Compiler validation adds about 1.56% total instructions (2.96-4.91% on three tested projects).
This cost is accepted for the capture correction.

Test plan

Check raw and captured compiler acceptance/rejection in C++11/14/17/20 and C11, unchanged
statement/block/loop CFGs, and compile-time-lambda versus runtime-null reports. Run the full
regression suite and compare all real-code projects for capture, report and cost changes.

Overlap with #2153

This PR carries a six-line addition to declStmt_trans in infer/src/clang/cTrans.ml that adds
StaticAssertDecl to the existing no-op list alongside NamespaceAliasDecl, TypedefDecl,
TypeAliasDecl, UsingDecl and UsingDirectiveDecl.

That hunk also appears in #2153 ("[clang] translate block-scope enum, function and local label
declarations"), which is currently closed. It is included here because without it this change is
not self-contained: once static_assert is no longer erased in C++, a static_assert inside a
function body becomes a real StaticAssertDecl, declStmt_trans raises unimplemented, and the
frontend discards the whole enclosing procedure. The effect is silent — no report and no
user-visible error — so every C++ function containing a static_assert would simply stop being
analysed.

If #2153 is reopened and lands, the two will conflict trivially on that one list and either side
can be dropped.

@meta-cla meta-cla Bot added the CLA Signed label Nov 25, 2026
@dulmarod

Copy link
Copy Markdown
Contributor

This is adding pretty complex test scripts, I wonder if that's really necessary.

The force-included C++ macro erased assertions and rejected directives in their operands.
Keep the legacy C macro and let Clang validate the C++ keyword. Translate StaticAssertDecl
without runtime instructions so it preserves the enclosing function.

Libc++'s comparator diagnostic can inspect an incomplete key through its generic invocation
trait. Match that diagnostic during capture and check the direct binary call, retaining its
warning attribute and severity.

For example, capture used to reject this valid layout check:

  static_assert([] {
  #pragma pack(push, 1)
    struct Packed { char first; int second; };
  #pragma pack(pop)
    return sizeof(Packed) == sizeof(char) + sizeof(int);
  }(), "packed layout");

Limitations: C++ assertions failing under the capture compiler fail that unit. The accepted
compiler-validation cost is 1.56% total instructions (2.96-4.91% on three tested projects).

## Test plan

Native build and 73 Linux direct suites plus annotations pass. The C++20 frontend fixture
covers directive operands, local assertions and incomplete-key comparators; three controls
fail without the relevant behavior. Additional local compiler/diagnostic checks pass.
The fresh macOS full-suite gate is pending for this revised test tree.
@VladimirMakaev

Copy link
Copy Markdown
Contributor Author

Fair point, they weren't necessary. I removed the shell and Python test scripts and their fixtures. The PR now has a single frontend test, cpp/frontend-20/static_assert_directives.cpp. It covers preprocessor directives inside a static_assert, code after a local assertion, and the comparator capture regression, and it fails without the StaticAssertDecl change.

@dulmarod

dulmarod commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Thank you, I think it's best to avoid making our testing frameworks more complex unnecessarily.

@dulmarod
dulmarod merged commit db44a59 into meta:main Dec 2, 2026
8 checks passed
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.

2 participants