Add Template
query library
#932
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Add a query library titled
Template
that provides definitions to help reason about templates and human-friendly alert locations.What is this?
This library started its life as part of a new version of query for M-0-12 in MISRA C++. The rule was mistaken as pertaining to template usages, and thus the types here was conceived of and utilized to report usages that were thought of violating the rule. Although the definitions were pulled off the final version of the query, these are expected to be of some use in providing sensible alert locations when the pattern a query aims to find involves templates.
What does it provide?
Templates, when instantiated, become generated code that is only visible after they are processed. This makes tricky when the way the template is instantiated in a particular case has to be reported, and doubly so when there are some other uses of the same template that don't violate the rule. A quick and dirty way is to alert on a location in the uninstantiated template definition, but in practice this makes it tricky to trace back to the client code that instantiates the template in a way that violates the rule. To circumvent this issue, this library provides
TemplateInstantiation.getAUse/0
that finds occurrences of a template call in the client code. TemplateInstantiation is a composition of three cases, class templates, function templates, and variable templates. Therefore, it is natural to explain per case the location this predicate gets us for given template instantiation.Class templates
Let's assume we're expanding M-0-12, which dictates that explicitly signed or unsigned chars should only be assigned numeric values, to catch template instantiations. We may decide to consider cases like below as below as violating the rule:
The first allocation of
c1
does not transgress, as the memberx
of type unsigned char is assigned to a numeral declared as also having typeunsigned char
. However, when we allocatec2
, the unsigned char memberx
gets assigned a char literal'x'
with the typechar
. This can be viewed as violating the rule, and we'd like to report it. But at which location should we report it?We might be tempted to use the member initialization site
x(y)
as the location to report on. However, it is not immediately clear which allocation betweenc1
andc2
causes the assignment. Instead, if we choose to alert on the use ofC<char, 'x'>
type name on top of the assignment, possibly as a clickable location in the alert message, then it becomes much more helpful for the user to identify the problem. This library enables the first part, to get the type name use, withTemplateInstantiation.getAUse/0
.Function templates
The story here is not so different from the class templates'. Consider this example:
Only the second call makes a char literal to be assigned to an unsigned char. Again, it is much more helpful when we provide the call site (
g(x2)
) on top of where the problematic assignment takes place (in the body ofg
).TemplateInstantiation.getAUse/0
gets the function callg(x2)
in the above example.Variable templates
Variable template is the most straightforward one out of the three kinds, and the only case where the predicate does not get a different location. This is because instantiation is the use site in the case of variable templates.
Given the above example, the predicate gets
v2<char>
.Call for discussion
There are some potential use cases of this library that can enhance result reporting for some queries. The reviewer is recommended to think of ones that might benefit from making use of this library.
Change request type
.ql
,.qll
,.qls
or unit tests)Rules with added or modified queries
Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.