Skip to content

lane_syntax_check: resolve a bare macro in #if, so app/serve.cpp is actually checked - #2343

Merged
bong-water-water-bong merged 1 commit into
mainfrom
lane/lane-syntax-bare
Nov 13, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
lane/lane-syntax-bare

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

The plant sweep found this the hard way. Planting an unbalanced brace in app/serve.cpp returned rc 0 — not because the check passed, but because the file was in the tool's own not checked bucket:

lane_syntax: not checked app/serve.cpp: unresolved #if condition at line 243

Line 243 is #if defined(ONEBIT_HRX_ALLOW_NO_SVM) && ONEBIT_HRX_ALLOW_NO_SVM, and the filter resolved the defined(...) half but not the bare macro beside it. A bare macro resolves exactly like its defined() twin when the name is in the define set, so it is now evaluated the same way.

before:  343 file(s) balanced,  8 not checked     app/serve.cpp among the 8
after:   346 file(s) balanced,  5 not checked     and the planted brace in serve.cpp gives rc 1

app/serve.cpp is the largest lane file, and #2330's class hit its sibling app/forward_serve.cpp — so it being silently unchecked was the worst place for this hole to hide. The remaining 5 are named, not guessed: laya/scorer.cpp, npu/forward/include/{attn_math,hadamard,q4nx_head_host,quant_i8}.h.

Two arms added and mutation-proven (dropping the bare-macro rule fails 2 arms): a bare macro that is defined takes the live branch; one that is not takes the other.

1/3  doc_section_loss_selftest .....  Passed  0.18 s
2/3  lane_syntax_selftest ..........  Passed  0.07 s
3/3  pr_review_queue_selftest ......  Passed  0.06 s

copyright --check rc 0, ci_targets_check --check rc 0. The --root . count above is from this branch's tree, not from a claim.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 0606df1)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Bare Macro Resolution

The change adds support for resolving bare macros in #if expressions, which was previously unhandled. This ensures that expressions like #if defined(ONEBIT_NPU) && ONEBIT_NPU are evaluated correctly by treating the bare macro ONEBIT_NPU the same as defined(ONEBIT_NPU). This fixes a silent failure where app/serve.cpp was not being checked due to an unresolved bare macro, and now correctly triggers a syntax error when the file has unbalanced braces.

ident = re.fullmatch(r"([A-Za-z_]\w*)", e)
if ident:
    # `#if defined(A) && A` -- the second `A` is a bare macro, resolvable the same way as the first.
    return ident.group(1) in defines
✅ Resolved findings

tools/ci/lane_syntax_check.py:75-78

Bare Macro Resolution

The change adds support for resolving bare macros in #if conditions, which was previously unhandled. This ensures that expressions like #if defined(ONEBIT_NPU) && ONEBIT_NPU are evaluated correctly by treating the bare macro ONEBIT_NPU the same as defined(ONEBIT_NPU). This fixes a silent failure where app/serve.cpp was not being checked due to an unresolved bare macro, and now correctly triggers a syntax error when the file has unbalanced braces.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: READY at e88ebbb

The numbers reproduce and the newly-covered file really does catch a planted brace:

$ python3 tools/ci/lane_syntax_check.py
lane_syntax: 346 file(s) balanced, 5 not checked            rc 0

$ <a planted `void planted() {` appended to app/serve.cpp>
lane_syntax: app/serve.cpp: 591 '{' vs 590 '}' first unclosed '{' at line 3633     rc 1

So app/serve.cpp -- the largest lane file, sibling of the app/forward_serve.cpp that broke main -- went from not checked to covered, and the planted brace is the proof rather than the count. 8 -> 5 "not checked" is three files gained, and the file that mattered is one of them.

The generalisation is the right one: #if defined(A) && A names the same macro twice, and resolving the second occurrence as a bare identifier is what makes the filter able to judge it at all. Two arms pin both directions (a bare macro resolves like its defined() twin, a bare macro that is NOT defined takes the other branch), which is the pair a filter change needs.

One soundness note, and I checked whether it costs anything here before raising it. defined(A) && A and "A is defined" are not the same condition: with A defined to 0 the preprocessor takes the else branch while the filter takes the if branch. This tree has exactly such a macro --

cmake/hrx.cmake:209   set(ONEBIT_HRX_ALLOW_NO_SVM 0)
CMakeLists.txt:823    ONEBIT_HRX_ALLOW_NO_SVM=${ONEBIT_HRX_ALLOW_NO_SVM}
app/serve.cpp:243     #if defined(ONEBIT_HRX_ALLOW_NO_SVM) && ONEBIT_HRX_ALLOW_NO_SVM

-- so on that line the filter and the compiler disagree about which branch is live. Today that costs nothing, and I measured it rather than assuming: the two branches are single constexpr bool definitions, and the surrounding twenty lines are 4 opens and 4 closes either way, so the running count is identical under both interpretations.

Which is why this is a note and not a blocker: the alternative (not checked, which is what app/serve.cpp was) is strictly worse than a branch choice that is brace-neutral here. If the filter grows a #if whose branches differ in braces and a zero-valued macro, the mis-resolution would produce a false "balanced" on the exact class this tool exists to catch. The cheap hardening is to resolve a bare identifier by its value when it is known (#define X 0 in the file, or a -DX=0 from CMake) and keep defined-implies-true only for unknown values -- with an arm of the shape #define A 0 + #if defined(A) && A counting the else branch.

Everything from 9e584a3 stands: the corpus is still 338+ files, the lexer still handles strings/raw strings/comments/char literals (#if 0 skipped, the ONEBIT_NPU branch counted), and not checked remains a named third state rather than a pass.

Not run: the full onebit build (source scan only).

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT at e88ebbb: CHANGES

The bare-macro fix is right and the method that found it is the best thing here — but the exit code still treats "cannot tell" as "fine", which is the exact path the plant went through.

What is excellent: you planted an unbalanced brace in app/serve.cpp and got rc 0. That is the negative control this kind of tool needs, and it is the only way to learn that lane_syntax: not checked app/serve.cpp: unresolved #if condition at line 243 was a pass. _truth() now resolving a bare macro (return ident.group(1) in defines) has arms in both directions — resolved and not-defined — which is the right pair.

What still fails open:

254:  print(f"lane_syntax: {len(bad)} of {checked} file(s) are brace-imbalanced");  return 1
257:  print(f"lane_syntax: not checked {path}: unresolved #if condition at line {line}")
259:  return 2 if unreadable else 0        # ← a skipped file does not affect the result

An unreadable file gets rc 2. A file skipped for an unresolved condition — which is epistemically the same thing, we did not check it — gets rc 0. So a brace error inside any of the 124 sources whose #if this cannot resolve is still invisible, and the next plant that lands on such a file will pass exactly as this one did before the fix.

The change is one line: count skipped alongside unreadable and return 2 when either is non-empty, keeping 1 for an actual imbalance and 0 only for "every file was read and every brace balanced":

return 2 if (unreadable or skipped) else 0

That gives the tool the three outcomes it needs — 0 = checked and fine, 1 = checked and broken, 2 = could not check — and makes the "named as not checked" message a failure rather than a note. Your docstring already says the right thing ("never guessed at: a wrong guess here would either pass a broken file or fail a good one"); the exit code is the part that has to agree with it, because a CI row reads the code, not the prose.

Why I would hold on this rather than readie the bare-macro half: the whole reason the plant returned rc 0 was that skipping is silent. Fixing one condition that caused a skip leaves the mechanism intact — and this tool's job is to be the last word on files nothing else compiles, so "I did not check this" has to sound like a failure.

Once it does, this is a genuinely strong addition: 124 sources across app/ npu/ moe/ laya/ tools/ tests/, brace-checked in CI, with a planted defect proving the check can fail.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY e88ebbb

The fix is right, the self-test covers both branches, and the way it was found is the part worth keeping.

The bug is a false pass, not a false failure: an unbalanced brace in app/serve.cpp returned rc 0 because the file landed in the tool's own not checked bucket — #if defined(ONEBIT_HRX_ALLOW_NO_SVM) && ONEBIT_HRX_ALLOW_NO_SVM, where the filter resolved the defined() half and not the bare macro beside it. A bare macro resolves exactly like its defined() twin when the name is in the define set, so evaluating it the same way is the correct rule, not a workaround.

The two self-test arms are the right pair: "a bare macro resolves like its defined() twin" and "a bare macro that is NOT defined takes the other branch" — so the rule can fail in either direction, rather than only being demonstrated on the case that motivated it.

And the technique that found it is the tool this repo has been missing all night: a plant sweep — insert a defect deliberately and require the checker to catch it. That is what would have caught forward_serve.cpp's brace before main broke, and it is the same discipline as #2247's mutant and the writer.py plant in #2268. If plant sweeps become routine for syntax tools, the "green build that never parsed the file" class largely closes.

One thing I would change, and it is the class rather than this instance: a file in the not checked bucket currently yields rc 0 with a printed line, so unresolvable coverage is silent by exit status. This instance is fixed, but the next unresolvable construct — #elif, #if FOO > 1, a macro used as a value — puts a file back in that bucket and the tool still reports success. Making the bucket loud (rc ≠ 0, or a count in the summary that fails when it changes) would turn "we could not check this file" from something a human reads past into something CI refuses. The plant sweep already proves the tool can tell the difference; the exit status is what tells the next reader.

Not run by me: the sweep itself. The rule, the two arms, and the rc-0-on-not-checked behaviour are what I read.

…ctually checked

The plant sweep found this the hard way. Planting an unbalanced brace in `app/serve.cpp` returned **rc 0** --
not because the check passed, but because the file was in the tool's own "not checked" bucket:

    lane_syntax: not checked app/serve.cpp: unresolved #if condition at line 243

Line 243 is `#if defined(ONEBIT_HRX_ALLOW_NO_SVM) && ONEBIT_HRX_ALLOW_NO_SVM`, and the filter resolved the
`defined(...)` half but not the bare `ONEBIT_HRX_ALLOW_NO_SVM` beside it. A bare macro resolves exactly like its
`defined()` twin when the same name is in the define set, so it is now evaluated the same way:

    before:  343 file(s) balanced,  8 not checked     app/serve.cpp among the 8
    after:   346 file(s) balanced,  5 not checked     and the planted brace in serve.cpp gives rc 1

`app/serve.cpp` is the largest lane file and the one #2330's class hit in its sibling, so it being silently
unchecked was the worst place for this to hide. The remaining 5 are named, not guessed (`laya/scorer.cpp`,
`npu/forward/include/{attn_math,hadamard,q4nx_host_head,quant_i8}.h`).

Two arms added and mutation-proven (dropping the bare-macro rule fails 2 arms): a bare macro that IS defined takes
the live branch, and one that is NOT takes the other. Self-test ok, ctest lane_syntax_selftest Passed 0.03 s,
copyright --check rc 0, ci_targets_check --check rc 0.
@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit 0606df1

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: READY at f0601f1(head 3f9ebd7's sibling) -- the semantics are the C standard's, and the paths that are not are skipped rather than guessed

#if defined(ONEBIT_NPU) && ONEBIT_NPU is the shape this closes, and resolving the second ONEBIT_NPU the same way as the first is exactly right: an identifier that survives macro expansion in a #if and is still undefined evaluates to 0 in C, so "bare macro resolves like its defined() twin" is not a convenience, it is the rule.

Both directions are armed, which is the pair that makes it a check rather than a demo:

  • ("#if defined(ONEBIT_NPU) && ONEBIT_NPU\nint f(){\n#endif\nint g(){ retu...", ...) -- the lane-guarded body is inspected;
  • ("#if defined(ONEBIT_NOT_SET_HERE) && ONEBIT_NOT_SET_HERE\nint f(){\n#endif\n...", ...) -- the other branch is taken, so the checker does not silently inspect code the preprocessor excludes.

And the part I came to test is already right: an expression the resolver cannot handle does not get a guessed branch. active_lines returns filtered is None plus the line number, and the caller puts the file into skipped with (path, unknown):

filtered, unknown = active_lines(text, LANE_DEFINES)
if filtered is None:
    skipped.append((os.path.relpath(path, root), unknown))
    continue

That is the fail-closed shape this whole family of checkers needs -- and the one they keep getting wrong (#2332 SKIPs 77 without an NPU build, the q8-tile bench reported NaN as a kernel fault). A checker that guesses at a conditional is a checker that stops checking the guarded code and says nothing.

Nothing to change. #ifdef/#ifndef are presumably handled by the same table; if a form ever appears that the regex cannot express (#if A > 0, #if !B), the skip-with-line path already covers it honestly.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: READY at 0606df1 -- correcting the head in the verdict above, which I typed as a mangled hash

The verdict above stands in full (bare macro resolves like its defined() twin, both branches armed, and an unresolvable #if skips the file with its line number rather than guessing at a branch). Only the head reference was wrong: this PR's head is 0606df1, not the string I wrote. Recording it so the pin is usable and nobody reads the typo as a different commit.

@bong-water-water-bong
bong-water-water-bong merged commit 373ae10 into main Nov 13, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the lane/lane-syntax-bare branch November 13, 2026 08:39
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.

1 participant