tools: add a checker for missing argument name comments

CODING_STYLE asks for a /* name= */ comment whenever NULL or another
value meaning "unset" is passed to a function. Nothing enforces it.
clang-tidy's bugprone-argument-comment only validates comments that
exist, and its option to demand them for literals never sees NULL
behind its cast in C and flags every size and mode along the way.

Add tools/check-argument-comments.py. It parses each file with
libclang and the flags from compile_commands.json, with every set of
them for a file that is built more than once, takes the parameter
names from the first declaration the caller sees, the one clang-tidy
compares comments against, and reports arguments spelled as a bare
NULL, 0, false or true, in the file and in the in-tree headers it
includes.

The arguments are read from the text of the call. So a literal with a
comment after it is still bare, and a comment at the end of the
previous line belongs to the previous argument. Like clang-tidy, only
a /* name= */ comment names a parameter, and case and leading or
trailing underscores don't matter. A /* name */ that lost its '=' is
reported too.

Calls into libc are skipped, and so are calls produced by macro bodies
since the caller never spelled their arguments. A call written inside
a macro argument is checked, as is a call through a macro that only
renames the function, the shims in src/include/override for example,
and a call through the sym_* pointers of the dlopen() wrappers.
--diff REV limits the report to lines that differ from a revision, and
--fix inserts the comments.

    tools/check-argument-comments.py --diff "$(git merge-base main HEAD)" src

The builtin headers, stddef.h and friends, are located by asking the
clang binary of the same major version. libclang derives their
location from its own, and in the Fedora rawhide tools tree that
comes out as the relative ../lib/clang/22, so every file fails to
parse with 'stddef.h' file not found.

Needs python3-clang and libclang 19 or newer. Older versions return
the file location from clang_getSpellingLocation(), so calls produced
by macro bodies would be checked as if the caller had spelled them.
This commit is contained in:
Christian Brauner committed 2026-11-04 23:19:52 +01:00
1 parent 4343b7c7d5
commit dee2b81067
2 files changed
+1011

No files matched your search

+10
View File
@@ -286,6 +286,16 @@ SPDX-License-Identifier: LGPL-2.1-or-later
```
This guidance should be applied tree-wide, including in test files.
`tools/check-argument-comments.py` enforces it for every bare `NULL`, `0`,
`true` and `false`, not only the ones meaning "unset", passed for a parameter
with a telling name. Single-letter names, `argN`, reserved `__names`, calls
into libc and other code outside the tree, and calls produced by macro bodies
are exempt. Calls through the `sym_*` pointers of the dlopen() wrappers use
the names from the library's header. It also checks that an existing comment
names the right parameter. `meson test -C build --suite=argument-comments`
checks the whole tree. Once `meson compile -C build gensources` has run,
`tools/check-argument-comments.py -p build --diff "$(git merge-base main HEAD)" src`
checks only what a branch changed, and `--fix` inserts the missing comments.
- Please do not introduce new circular dependencies between header files.
Effectively this means that if a.h includes b.h, then b.h cannot include a.h,
+1001
View File
File diff suppressed because it is too large. Load diff