-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C++: Ignore non-type template parameters when matching signatures in MaD #20899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++: Ignore non-type template parameters when matching signatures in MaD #20899
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enables MaD (Model-as-Data) summaries for C++ functions with non-type template parameters. Previously, functions like template<int N, typename T> void f(const T*, T*) could not have MaD summaries because the non-type parameter N would fail type-checking during signature computation. The fix filters out non-type template parameters when computing signatures, retaining only type template parameters.
Key changes:
- Introduced predicates to filter and count only supported (type) template parameters
- Updated signature computation to use filtered template parameter counts
- Added documentation clarifying that non-type template parameters cannot be specified in MaD models
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll | Added filtering predicates for type-only template parameters; updated signature computation to exclude non-type parameters; clarified documentation about non-type parameter limitations |
| cpp/ql/test/library-tests/dataflow/external-models/test.cpp | Added test case with function using non-type template parameter (callWithNonTypeTemplate<10, int>) |
| cpp/ql/test/library-tests/dataflow/external-models/flow.ext.yml | Added MaD model for callWithNonTypeTemplate<T> to test non-type parameter handling |
| cpp/ql/test/library-tests/dataflow/external-models/sources.expected | Updated expected sources with new test case results |
| cpp/ql/test/library-tests/dataflow/external-models/sinks.expected | Updated expected sinks with new test case results |
| cpp/ql/test/library-tests/dataflow/external-models/flow.expected | Updated expected flow analysis results including new edges and nodes for non-type template test |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Thanks for writing the exact same description as I wrote in the PR, Copilot. I'm sure this will be a great help to reviewers! |
|
I'm not sure I like the approach taken here. In the sense that I'm not sure what template<int N, typename T>
T callWithNonTypeTemplate(const T&);
template<typename T>
T callWithNonTypeTemplate(const T&);
void test_callWithNonTypeTemplate() {
int x = ymlSource();
int y = callWithNonTypeTemplate<10, int>(x);
int z = callWithNonTypeTemplate<int>(x);
}The two versions of |
You cannot currently do that. We could make that possible, but as far as I know we have not had a reason yet to do this. I think this current approach has at least two benefits:
template<typename _U1, typename _U2, typename
enable_if<_PCCFP<_U1, _U2>::template
_MoveConstructiblePair<_U1, _U2>()
&& !_PCCFP<_U1, _U2>::template
_ImplicitlyMoveConvertiblePair<_U1, _U2>(),
bool>::type=false>
explicit constexpr pair(pair<_U1, _U2>&& __p)
: first(std::forward<_U1>(__p.first)),
second(std::forward<_U2>(__p.second)) { }where we currently cannot add a model for this constructor because of the |
|
I discussed this with Jeroen just now, and we agreed to restrict this PR to only ignore non-type template parameters at the end of the template parameter list. That makes it easier to eventually add support for specifying non-type template parameters in MaD syntax in the future, and the only thing we need for the upcoming STL models are hopefully non-type template parameters in tail positions. |
Should be done now in 05737af |
jketema
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
On
mainit's currently not possible to write a MaD summary for the following function:because the first template parameter fails the QL type-check here and thus we fail to compute the expected signature that can be specified in the MaD row's "signature" column.
(The example may look slightly contrived, but it's important for writing summaries for certain STL functions that abuse template parameters to pick various overloads based on constant computations that evaluate to constant Booleans.)
Commit-by-commit review recommended.