Skip to content

Lint C and Objective-C in CI: a clang-tidy ratchet and a clang-format report - #425

Closed
v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:ci-lint-c
Closed

v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:ci-lint-c

Conversation

@v0lumehi

@v0lumehi v0lumehi commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Update (Oct 28): rebased on main 2f1a044. Code that landed since (the capture and replay tests, hle_sdk16.c, t_fpslock.c and others) brought new findings in 11 file/check pairs; the baseline is refreshed with tidy --update and now holds 244 findings in 127 file/check pairs across 69 files. None of them is fixed here; they are for the "work the baselines down" step of #313.

Update: rebased on main 81d53bf. Your fixes for #424 (18d65b3) took four findings out, and the ratchet saw them as four file/check pairs below the baseline (for example hle_hid.c core.BitwiseShift 2 → 0). The baseline is refreshed with tidy --update and now holds 208 findings (213 − 5; one fix removed two).

Result: the first CI run is green in every job. The lint job took 2m54s on xcode-27: LLVM 23.1.4 from the pinned tarball (sha256 checked), compdb found the same 166 sources as locally, and tidy gave 213 findings in 55 files, 107 file/check pairs, matching the baseline exactly (0 above, 0 below). format reported 0 changed C lines, since this PR changes none.

Part of #313 ("Code quality checks in CI": clang-tidy, plus clang-format on changed lines only). Uses the same runner setup as #421 (xcode-27, Xcode 27.1).

Problem

Python has a lint ratchet (#323), but the C and Objective-C host and NVN layer (most of the code) has none. clang's static analyzer finds null dereferences, use-after-free, leaks and uninitialized reads that a review of a 10,000-line file misses.

Change

  • .clang-tidy: checks that find bugs, not style:
    • bugprone-*, the analyzer's core, unix, osx and nullability groups, security.ArrayBound / FloatLoopCounter, misc-redundant-expression, misc-misplaced-const.
    • Each excluded check has its reason on its own line. The big ones: bugprone-signed-bitwise was 1,799 of the first run's 2,464 findings (deliberate bit masking on ints); unchecked atoi / sscanf (133) is deliberate for DM_* switches and profile tables.
  • dm/tools/lint_c.py:
    • compdb builds a compile database from make -n -B dry runs of the host, the tests and the gfx tests (166 sources).
    • tidy runs clang-tidy and compares per-file / per-check counts with dm/tools/lint_c_baseline.tsv: 213 findings in 107 pairs. Counts don't include line numbers, so moving code doesn't change them. Growth fails with the finding annotated on its line; shrinking asks for --update.
    • format runs git clang-format on the lines a PR changes and reports spacing differences in the job summary. Report only, and existing code is never reformatted.
  • .github/workflows/lint-c.yml:
    • runs on xcode-27 / Xcode 27.1, only when C / Objective-C sources, Makefiles or the lint files change;
    • SHA-pinned actions, contents: read;
    • LLVM 23.1.4 from the official release tarball, sha256-checked. Only clang-tidy, clang-format and the clang headers are unpacked (139 MB, 5 s).
  • dm/tools/tests/test_lint_c.py: 19 tests for the pure logic (baseline parsing and comparison, compdb dedupe, the format line filter, the LLVM version matching the baseline's). No LLVM needed.
  • Docs: docs/development.md (Continuous integration), 06-dev-loop.md §7.

What it found

Most findings in the baseline are false positives that depend on invariants the analyzer can't see, or realloc failures that only matter when memory runs out. Four look like real (small) bugs; I filed them together as #424:

  • two time-of-check / time-of-use cases (a controller disconnecting, the frame cap dropping to 0);
  • two unchecked null pointers (one only with DM_NVN_PASSDUMP).

Each fix takes its baseline entry with it.

Checked

Command Result
lint_c.py compdb 166 sources, 0.4 s
lint_c.py tidy -j 8 on main 1963fab 213 findings; 0 file/check pairs above it, 0 below it, 79 s on 8 cores
a probe null dereference added to hle_os.c annotated ::error on its line, exit 1
a baseline count raised by one ::notice … refresh it, exit 0
lint_c.py format over 30 recent commits 97 of 1,248 changed lines flagged (report only)
tool tests (371), test_docs, Python lint ratchet OK, 0 new

Not yet run on GitHub: this PR's own CI run is the first. I'd expect about 5 minutes on the runner's 3 cores.

@v0lumehi
v0lumehi force-pushed the ci-lint-c branch 5 times, most recently from 172342b to ef68571 Compare October 28, 2026 09:07
… report

Add .github/workflows/lint-c.yml and dm/tools/lint_c.py for dm/host and
dm/gfx (roadmap seedds#313). compdb builds a compile database from dry runs of
the Makefiles (host, host tests, gfx tests, texture tests, Metal sample;
166 sources). tidy runs clang-tidy with the bug-finding checks of
.clang-tidy and clang's static analyzer and counts findings per file and
check against dm/tools/lint_c_baseline.tsv: more fail, fewer ask for a
refresh. format runs clang-format (.clang-format) on the changed lines and
reports spacing changes in the job summary without failing.

Checks that mostly flag deliberate idioms are left out with a reason in
.clang-tidy (signed bitwise operations alone were 1,799 of 2,464
findings), CERT aliases are not listed twice, and `if (!strcmp(...))` is
allowed. The baseline holds 213 findings in 107 file/check pairs.

The job runs on xcode-27 with Xcode 27.1 and LLVM 23.1.4's release
archive (sha256-checked; clang-tidy, clang-format and clang's headers
unpacked), in about five minutes. test_lint_c.py covers the baseline and
ratchet, the compile database's dedupe, clang-tidy's output and the
format line filter, and keeps the workflow's LLVM and the baseline's in
step.

Validation: compdb 0.4 s, tidy 73 s at -j8 (213 findings, 0 above or
below the baseline), format 0.1 s; a probe null dereference fails tidy and
a lowered count only prints a notice. 371 tool tests pass, docs test
passes, lint_python reports 0 new.
seedds added a commit that referenced this pull request Oct 30, 2026
…(PR #425)

.clang-tidy runs the bugprone checks and clang's static analyzer (core, unix, osx,
nullability, two security checks) over dm/host and dm/gfx, with each excluded check's
reason on its line. dm/tools/lint_c.py builds a compile database from the Makefiles'
dry runs (196 sources) and counts findings per file and check against
dm/tools/lint_c_baseline.tsv (273 findings in 140 pairs, LLVM 23.1.4, current main).
The lint-c workflow downloads that LLVM release (sha256 pinned) on xcode-27.

The ratchet works like lint_python.py's: a new finding fails (fix it, or // NOLINT a
false positive with a reason), so does a fixed one still listed (tidy --update only
shrinks), and a pull request's baseline may not grow over its base's (--against),
unless LLVM_VERSION was raised (--rebaseline).

Left out of the PR: the clang-format report. .clang-format cannot state the house
style (one statement per line), and a root .clang-format would let editors'
format-on-save and git clang-format rewrite dense lines.

Co-authored-by: v0lumehi <[email protected]>
@seedds

seedds commented Oct 30, 2026

Copy link
Copy Markdown
Owner

Thank you! This landed on main as a345f5b, with you as co-author. Three changes from the PR:

  • The baseline only shrinks, as lint_python.py's does. A new finding fails: fix it, or mark a false positive // NOLINT(<check>) with a reason. A fixed finding still listed also fails until tidy --update lowers the counts, and --update never adds an entry. A pull request's baseline may not grow over its base's (--against, from the workflow). --rebaseline writes a new one, and only with a raised LLVM_VERSION.
  • The clang-format report is left out. .clang-format can't state the house style (it puts each statement on its own line). A root .clang-format would also let editors' format-on-save and git clang-format rewrite dense lines.
  • The baseline was remade on current main with LLVM 23.1.4: 273 findings in 140 file/check pairs, over 196 sources. The two new host findings since your run are false positives: keys_file's read loop at EOF, and guest_thread_set_id's TLS key, which is always set when the thread starts.

Working the baseline down stays with #313.

@seedds seedds closed this Oct 30, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…(PR seedds#425)

.clang-tidy runs the bugprone checks and clang's static analyzer (core, unix, osx,
nullability, two security checks) over dm/host and dm/gfx, with each excluded check's
reason on its line. dm/tools/lint_c.py builds a compile database from the Makefiles'
dry runs (196 sources) and counts findings per file and check against
dm/tools/lint_c_baseline.tsv (273 findings in 140 pairs, LLVM 23.1.4, current main).
The lint-c workflow downloads that LLVM release (sha256 pinned) on xcode-27.

The ratchet works like lint_python.py's: a new finding fails (fix it, or // NOLINT a
false positive with a reason), so does a fixed one still listed (tidy --update only
shrinks), and a pull request's baseline may not grow over its base's (--against),
unless LLVM_VERSION was raised (--rebaseline).

Left out of the PR: the clang-format report. .clang-format cannot state the house
style (one statement per line), and a root .clang-format would let editors'
format-on-save and git clang-format rewrite dense lines.

Co-authored-by: v0lumehi <[email protected]>
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.

2 participants