Skip to content

headers,test: guard Test/TestSuite placeholders so include order is irrelevant - #608

Open
simonyang08 wants to merge 1 commit into
Snaipe:bleedingfrom
simonyang08:codex/criterion-573-include-order
Open

simonyang08 wants to merge 1 commit into
Snaipe:bleedingfrom
simonyang08:codex/criterion-573-include-order

Conversation

@simonyang08

Copy link
Copy Markdown

Summary

Fixes #573.

When a translation unit includes <criterion/parameterized.h> before <criterion/criterion.h>, internal/parameterized.h transitively pulls in internal/test.h, which provides the real definitions of Test(...) and TestSuite(...). The subsequent include of criterion.h then unconditionally redefines both macros to the placeholder token internal, because the trailing #include "internal/test.h" in criterion.h is a no-op thanks to its include guard. As a result TestSuite(suite); expands to the bare token internal ;, producing the cryptic error described in the issue:

example.c:7:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

Fix

Wrap the placeholder definitions in include/criterion/criterion.h with #ifndef Test / #ifndef TestSuite. The placeholder is now only applied when no real definition has been seen yet (i.e. when criterion.h is included in isolation). When parameterized.h has already supplied the real macros, the placeholders are skipped and the guarded internal/test.h include leaves the real definitions intact.

Why Theory(...) is intentionally left alone: the same placeholder pattern exists in theories.h, but internal/theories.h is the only place that #undefs and re-defines Theory, and it is reachable only through theories.h itself (which always defines the placeholder first) — unlike internal/test.h, which is reachable directly from internal/parameterized.h. So the include-order defect cannot occur for Theory. Happy to sweep it in a follow-up if maintainers prefer consistency.

Regression test

New samples/include-order.c includes parameterized.h first and criterion.h second, then defines a TestSuite and a Test, exercising the broken expansion path at compile time. Registered in samples/meson.build and runs as part of meson test (like the other samples it uses --always-succeed, so it acts as a compile-time regression guard).

Verification

  • Pre-fix: meson compile fails on the new sample with exactly the issue's expected '=', ',', ';', 'asm' or '__attribute__' before '{' token error.
  • Post-fix: meson test 49/49 OK including the new sample; both include orders compile and run.
  • uncrustify -c .uncrustify.cfg clean on changed files.

3 files, +15/-2.

…rrelevant

When <criterion/parameterized.h> is included before <criterion/criterion.h>,
internal/parameterized.h pulls in internal/test.h which already defines
Test and TestSuite with their real expansions. The subsequent include of
criterion.h then unconditionally redefined Test and TestSuite to the
placeholder token 'internal', because the trailing '#include "internal/test.h"'
in criterion.h was a no-op thanks to its include guard. As a result
TestSuite(...) expanded to the bare token 'internal', producing a
cryptic compile error far from the actual cause.

Wrap the placeholder definitions in criterion.h with '#ifndef Test' /
'#ifndef TestSuite' so they only apply when the real definition has not
yet been seen. A new samples/include-order.c sample exercises the
reversed include order to lock in the fix.

Fixes Snaipe#573

Signed-off-by: simonyang08 <ppt5928@gmail.com>
@MrAnno

MrAnno commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

The macros ifdefed out in this PR help all kinds of tools to show a simplified signature for the Criterion primitives (for example, IDE hints).

I'm not entirely sure it's worth fixing this issue as it is documented that <criterion/criterion.h> should come first.

Let me know if you disagree or you have a better idea.

@simonyang08

Copy link
Copy Markdown
Author

Thanks for the review! I believe both concerns can coexist: with the #ifndef guards, the documented order (<criterion/criterion.h> first) behaves exactly as before — Test/TestSuite are still undefined at that point, so the placeholders (and the IDE simplified signatures that rely on them) are still defined. The guards only skip the placeholder when the real definitions were already pulled in by <criterion/parameterized.h>, which is what currently produces the cryptic internal ; expansion from #573.

So the IDE-hint purpose is preserved, and the header just stops producing a confusing error when the include order differs. That said, if you'd rather keep the header order-sensitive and close this as works-as-documented, I'm fine with that too — your call.

@simonyang08
simonyang08 marked this pull request as ready for review October 1, 2026 01:18

This branch has not been deployed

No deployments
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.

Header imports in the wrong order define macros as internal

3 participants