Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

AddTemplate query library#932

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
jeongsoolee09 wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromjeongsoolee09/add-template-lib

Conversation

jeongsoolee09
Copy link
Contributor

@jeongsoolee09jeongsoolee09 commentedJul 15, 2025
edited
Loading

Description

Add a query library titledTemplate 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 whenthe 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 providesTemplateInstantiation.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:

template<typename T, T y>classC {public:C() : x(y) {}private:unsignedchar x;}voidf() {  C<unsignedchar,1>c1();  C<char,'x'>c2();}

The first allocation ofc1 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 sitex(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 nameon 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:

template<typename T>voidg(T y) {unsignedchar y = x; }voidf() {unsignedchar x1 =1;char x2 ='x';g(x1);g(x2);}

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 instantiationis the use site in the case of variable templates.

template<typename T> T v1;voidinstantiateTemplateVariables() {    v1<unsignedchar> =1;    v2<char> ='x';}

Given the above example, the predicate getsv2<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

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql,.qll,.qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • rule number here
  • Queries have been modified for the following rules:
    • rule number here

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
Reviewer: Confirm that format ofshared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

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

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with thestyle guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with thestyle guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

Copilot code reviewCopilotAwaiting requested review from CopilotCopilot will automatically review once the pull request is marked ready for review

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@jeongsoolee09

[8]ページ先頭

©2009-2025 Movatter.jp