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

Implement "Statements" package#938

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 merge35 commits intomain
base:main
Choose a base branch
Loading
fromjeongsoolee09/MISRA-C++-2023-Statements

Conversation

jeongsoolee09
Copy link
Contributor

@jeongsoolee09jeongsoolee09 commentedAug 1, 2025
edited
Loading

Description

This PR implements theStatements package.

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-9-4-2
    • RULE-9-5-1
    • RULE-9-5-2
  • 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!)

Copy link
Collaborator

@MichaelRFairhurstMichaelRFairhurst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is really coming along and looking really good!!

* to a non-const reference variable (thus constituting a `T` -> `&T` conversion.), i.e.
* initialization and assignment.
*/
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Simple comment formatting, unnecessary split

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Good call. The intention was to split the documentation and the meta-level comment (explaining how this predicate came to be). But like you said it can be disconnected easily, so I'll merge the meta-level comment into the docstring first.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Addressed inc8c0770.

predicate loopVariableAssignedToNonConstPointerOrReferenceType(
ForStmt forLoop, VariableAccess loopVariableAccessInCondition
) {
exists(Expr assignmentRhs, DerivedType targetType |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Likely want to test that this works for aint * const x:

voidf(int *const x) {    (*x)++;}intmain() {for (int i =0; i <10; ++i) {f(&i);        std::cout << i << std::endl;    }}

I believe what will happen is thatint * const x will be aDerivedType of typeSpecifiedType with a const specifier. ASpecifiedType is notinstanceof PointerType orinstanceof ReferenceType and so this predicate will not hold, even though the value ofi is modifiable withinf.

You may also have problems with typedefs, such astypedef int *int_ptr_t for the same reason.

The solution here I believe will be to call.getUnderlyingType(). Another option frequently used for this is.stripSpecifiers(). Each of these will remove the const and resolve the typedef. I think.stripSpecifiers() may remove the const inconst int*, though, which would make it unsuitable here.

Copy link
ContributorAuthor

@jeongsoolee09jeongsoolee09Sep 24, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You're right; the predicate does not catch this example. 🤔 I guess a clever use of one or more ofisDeeplyConst, orisDeeplyConstBelow will do the trick.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Forgetting to handle typedefs or meaningless consts is a very common bug. But you'll (mostly) get in the habit soon enough of always calling one of these four member predicates on theTypes you handle in your queries:

  • getUnderlyingType()
  • resolveTypedefs()
  • stripSpecifiers()
  • stripTopLevelSpecifiers()

Each one does subtly different things.

In this case, I believe the fix is to do:

exists(...,TypetargetType,DerivedTypestrippedType|    isAssignment(assignmentRhs, targetType, _)and    strippedType=targetType.stripTopLevelSpecifiers()    not strippedType.getBaseType().isConst() and(      strippedType instanceofPointerTypeor      strippedTypeinstanceofReferenceType)

The documentation forstripTopLevelSpecifiers says:

Get this type after any top-level specifiers and typedefs have been stripped.

For example, starting withconst i64* const, this predicate will returnconst i64*.

which is actually wrong, as it ignores the fact thati64 is aTypeDefType, so itactually will result inconst long long*. Which is what you want!

The TLDR of the other options:

  • getUnderlyingType() -- resolvesTypdefTypes andDeclTypes, but won't drop the outer specifer inconst i64* const. Stops at the first non-TypedefType/non-DeclType.
  • stripType() -- resolves all typedefs and decltypes and removes allconst/volatile specifiers recursively all the way down the type chain -- not what you want.
  • resolveTypedefs -- resolves all typedefs and decltypes all the way down the type chain without removingconst orvolatile specifiers. That would handle typedefs but notint const *.

Note that these predicates can have no result. Only a limited set of types are in the database, and these operations just assume that the type you want is one of those types.resolveTypedefs is also bugged and doesn't recurse intoArrayType.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thank you for the detailed breakdown of the related predicates. What I want to express here is definitely "The type we getafter we strip all the typedefs and the specifiers is const". I've come to believestripTopLevelSpecifiers is the one I should use, and swapped the portion with your suggestion.

I also patched an equivalent part inloopVariablePassedAsArgumentToNonConstReferenceParameter, in7d5f08b.

targetType instanceof ReferenceType
)
|
assignmentRhs.getEnclosingStmt().getParent*() = forLoop.getStmt() and
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You may want to addnot assignmentRhs.isInUnevaluatedContext() for safety.

That would prevent reporting cases likesizeof(g(&i)) ordecltype(g(&i)).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Feel free to resolve this.

I mostly wanted to point this out because this is a common trap case in coding standards query writing. In C/C++ you can write any expression inside of asizeof() check, as well as other exprs likedecltype()/alignof()/constexpr()/requires()`, and these aren't evaluated.

So if you're looking at a rule that says something like, "Never passnullptr intostd::some_function(x)", then that's the kind of rule where we may want to be careful thatsizeof(std::some_function(nullptr)) isn't flagged, because it won't actually executestd::some_function().

In this case, I've convinced myself this isn't something we have to worry about in this case. Hopefully I'm not wrong about that! :)

A really complicated example with TLDR, no need to worry about this.

Here's the most reasonable example that Copilot and I came up with:

template<typename IndexType, typename Observer>void process_with_observer(std::vector<int>& data) {    Observer observer;        for (IndexType i = 0; i < data.size(); ++i) {        // Determine observer's interface at compile time        using observer_result = decltype(observer(&i));                if constexpr (std::is_void_v<observer_result>) {            observer(&i);            data[i] = default_transform(data[i]);        } else {            auto metadata = observer(&i);            data[i] = complex_transform(data[i], metadata);        }    }}

This code isalmost reasonable. It would allow you to customize the loop behavior by writing a class with an overloaded() operator......but it's really really strange, and the only "problem" we'd have analyzing this code is that it would flag all three sites toobserver(&i) when we would "ideally?" only flag two.

So yeah. No need to worry about it :)

loopCounterType = forLoopCondition.getLoopCounter().getType() and
loopBoundType = forLoopCondition.getLoopBound().getType()
|
loopCounterType.getSize() < loopBoundType.getSize()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Two missed cases here:

  • Mixing signed/unsigned types, they may have the same size but they'll hold different ranges.
  • The type and runtime value may lead to different conclusions.

I think you may be able to get away withupperBound(loopCounter) < upperBound(loopBound). That would handle signedness, constants (likex < 10ull), and dynamic ranges (likeunsigned long long bound = 10; ... x < bound).

jeongsoolee09 reacted with thumbs up emojijeongsoolee09 reacted with eyes emoji
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Also almost forgot

Another trap case is that when doingupperBound(e) /lowerBound(e) you usually wantupperBound(e.getFullyConverted()). Because conversions one will change the bound.

* variable that is passed as reference to a non-const reference parameter of a function,
* constituting a `T` -> `&T` conversion.
*/
predicate loopVariablePassedAsArgumentToNonConstReferenceParameter(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

A thought on simplifying these names.

Maybe instead of naming themloopVariablePassedAs... you can rename them topassedAsNonConstReference/passedAsNonConstPointer and remove theForStmt argument.

Then at the call sites you can change

loopVariablePassedAsArgumentToNonConstReferenceParameter(loop, va)// becomesexists(VariableAccess other |  passedAsNonConstReference(other) and  other.getVariable() = loop.getBound().(VariableAccess).getVariable() and  other.getEnclosingStmt().getParent*() = loop.getStmt())

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure this is a good idea because

  • TLoopCounterIsTakenNonConstAddress and friends have to have an identical body, and repeating these lines three times will increase the verbosity.
  • The predicatesloopVariablePassedAs... and the other one are highly specialized to this query alone, so it doesn't hurt much to keep them amalgamated (if that's a word!) and not break them down.

Copy link
Collaborator

@MichaelRFairhurstMichaelRFairhurstOct 1, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I wouldn't worry about this as extra verbosity because the extra code is easy to read. In general, I'd worry more about clear/concise naming.

If verbosity is an issue you can add helper methods toLegacyForLoopCondition

class LegacyForLoopCondition {  ...  predicate isBoundAccess(VariableAccess va) {    va.getVariable() = getBound().(VariableAccess).getVariable() and    other.getEnclosingStmt().getParent*() = loop.getStmt()  }  ...}...  exists(VariableAccess other    | passedAsNonConstReference(other) and loopCondition.isBoundAccess(other))

You can definitely keep it as is if you prefer, and you may think of a better way to abstract the duplicated code than theisBoundAccess() example here.

Definitely your call!

We are interested if the underlying *data* can bemutated, not the pointer itself. Also, the surfacetype may be a typedef, so resolve that as well.
Both `TLoopBoundIsMutatedVariableAccess` and `TLoopStepIsMutatedVariableAccess`transitively rely on `valueToUpdate`, which overapproximates by looking at thetypes alone. Therefore we'd like to drop the confidence slightly in reportingthe expression where the expression *might* have been changed.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@MichaelRFairhurstMichaelRFairhurstAwaiting requested review from MichaelRFairhurst

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

@jeongsoolee09jeongsoolee09

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@jeongsoolee09@MichaelRFairhurst

[8]ページ先頭

©2009-2025 Movatter.jp