Skip to content

C++: Implement MaD support for flow through perfect-forwarding functions - #22521

Draft
MathiasVP wants to merge 7 commits into
github:mainfrom
MathiasVP:flow-through-forwards-using-callbacks-without-return-nodes-for-this
Draft

C++: Implement MaD support for flow through perfect-forwarding functions#22521
MathiasVP wants to merge 7 commits into
github:mainfrom
MathiasVP:flow-through-forwards-using-callbacks-without-return-nodes-for-this

Conversation

@MathiasVP

@MathiasVP MathiasVP commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

This PR implements the necessary library changes to support MaD summaries for functions such as vector::emplace_back or make_unique. These functions receive a list of arguments and then forwards them to a constructor call.

@hvitved had a super cool implementation idea. Given:

struct Foo {
  Foo(int);
};

std::vector<Foo> v;

we model a call such as v.emplace_back(42) as:

v.emplace_back(42, &Foo)

and give emplace_back two summaries:

  1. Argument[0] -> Argument[1].Parameter[0]
  2. Argument[1].Parameter[this] -> Argument[this].Element

The first summary states that 42 goes into the 0'th parameter of the Foo constructor, and the second summary states that the this parameter of the constructed object goes into the this argument of v with an Element content.

(A few lines I told in the above paragraph:

  • You cannot take the address of a constructor in C++. But that doesn't mean we cannot use it as an implementation detail!
  • We don't actually use the last argument as the argument position of the synthetic function pointer for the constructor. This PR adds a new argument position which we name forward in MaD. I'm happy to change this name to something else if anyone has any strong opinions about this.)

In order to know which constructor to forward to we need to know what type is being constructed. For example:

std::vector<Foo> v;
v.emplace_back(42); // calls `Foo(42)`
v.emplace(v.begin(), 42); // calls `Foo(42)`
std::make_unique<Foo> p(42); // calls `Foo(42)`

To know which type is constructed we add a new extensible called forwardsModel with rows very much like what we have for MaD summaries. For example, I've added this as row as a test:

["", "Container<T>", True, "emplace<Args>", "(Args &&)", "", "0", "T", "manual"]

this says that a call to Container<T>::emplace(args0, ..., argsN) forwards its arguments to a call to T(args0, ..., argsN). The 0 specifies an offset so we can support cases like v.emplace(v.begin(), 42) where we need to ignore the first argument.

This PR doesn't actually add any non-test MaD summaries. I'll delay that to a future PR.

@github-actions github-actions Bot added the C++ label Sep 7, 2026
@MathiasVP MathiasVP changed the title C++: Implement MaD support for through perfect-forwarding functions C++: Implement MaD support for flow through perfect-forwarding functions Sep 7, 2026
@MathiasVP
MathiasVP force-pushed the flow-through-forwards-using-callbacks-without-return-nodes-for-this branch 2 times, most recently from 8f8225a to 6edcabd Compare September 8, 2026 11:18
@MathiasVP
MathiasVP force-pushed the flow-through-forwards-using-callbacks-without-return-nodes-for-this branch 2 times, most recently from ccad487 to f8d0434 Compare September 8, 2026 11:34
In the upcoming commits we will add a new extensional predicate which
allows us to model that a function forwards it arguments to the
constructor of a given type. This initial commit adds the test YAML
models for this new extensional predicate.
signature to implement the forwardsModel. So instead of recursing on the
number of elements in the signature we will recurse on the number of
elements in the type (or name) columns. For well-formed models this will
be equivalent.
@MathiasVP
MathiasVP force-pushed the flow-through-forwards-using-callbacks-without-return-nodes-for-this branch from f8d0434 to ada9784 Compare September 8, 2026 11:53
@MathiasVP
MathiasVP force-pushed the flow-through-forwards-using-callbacks-without-return-nodes-for-this branch from ada9784 to 232b29e Compare September 8, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant