Skip to content

fix(const-param): keep the parameter types of a function used as a callback - #292

Draft
SizzleUnrlsd wants to merge 12 commits into
mainfrom
fix/const-param-callback-signature
Draft

SizzleUnrlsd wants to merge 12 commits into
mainfrom
fix/const-param-callback-signature

Conversation

@SizzleUnrlsd

@SizzleUnrlsd SizzleUnrlsd commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ConstParameterNotModified suggested const for a parameter of a function used as a callback. That function is called through a pointer to function, and the type of the pointer sets the types of its parameters: the suggested type does not compile. This is the callback example of #217; the other examples of that issue are not changed.

Refs #217

Change

  • A function whose address is given away keeps its parameter types: passed to a call, stored, returned, or placed in the initializer of a variable. A direct call and a comparison are not such uses, nor is an alias or an entry of llvm.used. No function is named in the rule.
  • A lambda converted to a pointer to function: what it gives away is a function that the compiler writes in the lambda's class. That function calls the lambda's body, whose parameters keep their types.
  • Across files: in a multi-file run, a function that one file defines and another uses as a callback keeps its types.
  • Not changed: a virtual function. Its address is in the table of its class, where its type follows the function it overrides.

Tests

New, in test/pointer_reference-const_correctness/:

  • callback-signature.c: a C function given to a call, kept in a table, kept in a variable, returned. A function that is only called keeps the suggestion.
  • callback-lambda.cpp: a lambda with the shape of a thread's start routine and one with a typed pointer, each converted to a pointer to function. A lambda that is only called keeps the suggestion.
  • check_const_param_callback_cross_tu (run_test.py): on_value() is suggested when its file is analyzed alone, and not with the file that gives it to a call, whatever the sort order; only_called() is suggested in both.
  • callback-virtual-function.cpp: a virtual function that overrides nothing keeps the suggestion.

No existing test is changed. takes_void(void* p) of advanced-cases.c keeps its suggestion.

The same checks with main's analyzer (0372f35) and with this one (9fcbff7), built on one runner (run 46917587199, on the branch experiment/217-callback-compare):

Check main's analyzer This pull request's
callback-signature.c fails: the four callbacks are reported passes
callback-lambda.cpp fails: the two converted lambdas are reported passes
check_const_param_callback_cross_tu fails: on_value is reported with the file that uses it passes
advanced-cases.c passes passes
callback-virtual-function.cpp passes passes

Reports of main and of this pull request on the same inputs

Same run. Each fixture of this tree is analyzed alone by both analyzers (499 inputs), then main's sources as the Self-analysis step of ci.yml does (55 files, 4 analyzers).

Inputs main's analyzer This pull request's Only with main's Only with this one
The fixtures 677 diagnostics 670 7 0
main's sources 72 68 4 0

No run ends differently with the two analyzers. The eleven diagnostics are all ConstParameterNotModified.Pointer, on a function used as a callback:

  • 4 in callback-signature.c and 2 in callback-lambda.cpp: the new fixtures.
  • 1 in test/escape-stack/indirect-callback-unknown-target-with-safe-candidate.c: safe_cb(char* p), which choose_safe() returns as a cb_t, a pointer to void (char*). That fixture expects nothing about this parameter and is not changed.
  • 4 in src/app/AnalyzerApp.cpp: rawContext, the parameter of the lambda that runParallelWork gives to pthread_create, once per instantiation of the template.

Each of the seven suggestions on the fixtures, applied to its source, fails to compile with clang 20: incompatible function pointer types for the C functions, no matching function for call for the lambdas. The lambda of runParallelWork has the shape of the first one of callback-lambda.cpp. The suggestions that stay compile once applied: only_called, the lambda that is only called, and the virtual function.

CI at 9fcbff7

  • ubuntu-24.04, macos-15 and macos-latest: 2946 of 2946 functional tests, against 2917 on main; both unit-test programs end with their All ... passed. line.
  • Self-analysis of this tree, on Linux: errors=0, warnings=10, infos=58, against 62 on main: the four rawContext suggestions, and nothing new.

Limits

  • A file analyzed alone says nothing of a callback use in another file: the suggestion is still made there.
  • Any use of a function's address as a value counts, even where nothing calls the function back. So does a function passed to a template that would also take the const type, as std::thread does. A suggestion that would compile is then lost.
  • The other examples of ConstParameterNotModified suggests a const type that does not compile #217 are not changed: &p passed to a pointer to pointer, a lambda whose body is never emitted, a reference bound to a reference member.

…llback

A function whose address goes to a call, into memory, out of a function
or into the initializer of a variable is called through a pointer to
function, and the type of that pointer sets the types of its
parameters: const is not suggested for them. A virtual function, held
in the table of its class, is not such a use.

Refs #217
Its address goes to a call, into a table, into a variable or out of a
function. A function that is only called keeps the suggestion.

Refs #217
…lback

A lambda converted to a pointer to function gives away a function that
the compiler writes in its class, and that function calls the lambda's
body: the body's parameters have the types the pointer sets.

Refs #217
One has the shape of a thread's start routine, another takes a typed
pointer. A lambda that is only called keeps the suggestion.

Refs #217
…llbacks

The index of the modules analyzed together lists the external functions
whose address a module gives away, defined there or not. A function
listed there keeps its parameter types.

Refs #217
…her file

A multi-file run gathers the callbacks of every module: one module that
uses a function as a callback is enough to set its types.

Refs #217
…nother

Alone, the file that defines it says nothing of that use, and const is
suggested. With the file that uses it, whatever the sort order, it is
not. A function that the other file only calls keeps the suggestion.

Refs #217
@SizzleUnrlsd SizzleUnrlsd added the bug Something isn't working label Dec 2, 2026
@SizzleUnrlsd SizzleUnrlsd self-assigned this Dec 2, 2026
A returned product gets the signed-overflow warning, which this fixture
is not about: its strict count of warnings failed.

Refs #217
A returned sum gets the signed-overflow warning, which these fixtures
are not about: their strict count of warnings failed.

Refs #217
Its address is in the table of its class, which is not the use of a
callback.

Refs #217
… a worklist

The walk called itself for each constant, which the self-analysis
reports as a recursion. A worklist visits each constant once.

Refs #217
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants