Skip to content

Conversation

@MichaelRFairhurst
Copy link
Collaborator

Description

There are problems with the APIs on types for handling typedefs, decltypes, and specifiers, etc. Some of them resolve typedefs and some of them don't, and its not clear. Some of them have incorrect documentation. Some of them have bugs (for instance, ArrayType.resolveTypedefs()). These APIs also have a fundamental flaw: they assume that a given type exists in the database. For instance, if type T has a typedef T_t, then const T_t may exist in the database and const T may not.

At one point I decided to make a new API for these methods that was more clear, and I created PossiblySpecified<T>::Type. One of the important points in PossiblySpecified<T> was that it doesn't resolve typedefs, so that the type T can be a typedef type such as uint8_t. However, what if someone makes a typedef of uint8_t, for instance, typedef uint8_t ubyte_t? Well in this case, PossiblySpecified<Uint8Type> won't match ubtyte_t. So clearly we need to resolve some typedefs. But if we try to write a query that uses expr.getType().resolveTypedefs() instanceof Uint8Type, we'll have no results.

Furthermore, we will have a lot of places in our queries where we want to treat T and T& the same.

So we really want an API that:

  • uses clear naming for all of the types of resolution it might perform
  • resolves typedefs/decltypes until it matches something of interest and no further
  • additionally resolves specifiers only when asked to
  • can require certain specifiers to match types (to search for const and/or volatile types) when asked to
  • doesn't presume arbitrary types exist in the database
  • hopefully has some guard rails to encourage the correct behavior, and few trap cases.

This PR introduces a candidate API and I welcome feedback on it.

Instead of expr.getUnderlyingType() instanceof PointerType, you can write expr.getType() instanceof ResolvesTo<PointerType>::Exactly. This should raise some hairs "what do you mean by exactly?" That's because getUnderlyingType() instanceof PointerType() is usually wrong, or at least incomplete. Exactly means it will not match a cv-qualified pointer type. In this case, typically what we want to write is expr.getType() instanceof ResolvesTo<PointerType>::IgnoringSpecifiers.

Other options exist: ResolvesTo<PointerType>::CvConst will match a const pointer or const volatile pointer. ResolvesTo<PointerType>::Ref matches a reference to a pointer type. ResolvesTo<PointerType>::CvConstRef matches a reference to a const pointer (not to be confused with a const reference to a pointer, which doesn't exist!).

Added some additional classes PointerTo<T>::Type, ReferenceOf<T>::Type, that work well with this API, such that you can write ResolvesTo<PointerTo<FooType>::Type>::IgnoringSpecifiers. Also RawConstType, ConstType, NonConstType.

In addition to replacing the prior usages of PossiblySpecified<T>, I also integrated this API into Jeongsoo's recent query, for comparison, and one other random query I found via grep that was using getUnderlyingType.

Comments and feedback and bikeshedding welcome!

These are some of the improvements I'd like to see in it one day myself:

  • Additional features like ResolvesTo<IntType>::Pointer.
  • Some improved support for specifiers+references, not just CvConstRef. This is complicated by CodeQL's handling of nested specified types (it doesn't!).
  • Some support for RefOrNonRef kind of concepts (e.g., I want to match both T and T&).

Additional ideas?

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:
    • INT36-C
    • RULE-22-12
    • RULE-22-13
    • RULE-22-14
    • RULE-9-5-1

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 of shared 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 the style 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 the style 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!)

…ing types.

Expands upon `PossiblySpecified<T>` module, which was nothing but a renaming of
`.stripSpeciifers` that was intended to be clearer (and not resolve typedefs).
However, there is almost never a reason not to resolve _certain_ typedefs.

Assume we are writing a query to detect usages of a typedef such as `uint8_t`.
If we have a candidate type and wish to check if it is a `uint8_t`, we may do so
via `candidate.(TypedefType).hasName("uint8_t")`. However, this will not match
`const uint8_t`. The type methods such as `stripSpecifiers`,
`getUnderlyingType`, can be used but are sometimes vague, and some implicitly
resolve typedefs while others do not, and some even have incorrect
documentation.

Furthermore, if a user has declared `typedef uint8_t uchar_t` or
`typedef const uint8_t uint8_const_t`, then we _must_ resolve typedefs to find
these usages of `uint8_t` in the project. However, `candidate.resolveTypedefs()`
will also unwrap the `uint8_t`. In C++ the problem is worse too, as we would
need to detect cases such as `const uint8_t&`, and decltypes.

This project needs, IMO, a better API for incrementally resolving typedefs,
decltypes, specifiers, and references. This PR adds what is hopefully a decent
starting point for this.

Instead of writing `candidate.resolveTypedefs() instanceof FooType` or
`candidate.stripTopLevelSpecifiers() instanceof FooType` or
`candidate.getUnderlyingType() instanceof FooType`, the `cpp.types.Resolve`
import now allows `candidate instanceof ResolvesTo<FooType>::Exactly` to only
resolve typedefs and decltypes, or `::IgnoringSpecifiers` to resolve typedefs,
decltypes, and unwrap `SpecifiedType`s as well. These types have a `.resolve()`
member predicate to unwrap everything and get the resolved `FooType`.

The API basically intends to define an interterface for how to unwrap type A in
search of a type B. Types are like linked lists, with flags on certain links.
We really just want a means of traversing different kinds of links in different
contexts, sometimes requiring the presence of a certain kind of link, and
sometimes aggregating properties about what links were traversed.

We also want an API that provides some guard rails and funnels people towards
correct usage of itself. Hopefully `::Exactly` comes across as too strict, and
hopefully `CvConst` makes it clear that a type may be `const` or
`const volatile`.

Also added some types that compliment this API well such as
`PointerTo<T>::Type` and `ReferenceOf<T>::Type`.

Also integrated this library into a few example queries so the PR includes
example usage.
Copilot AI review requested due to automatic review settings December 3, 2025 23:38
Copilot finished reviewing on behalf of MichaelRFairhurst December 3, 2025 23:42
Copy link
Contributor

Copilot AI left a 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 introduces a new type resolution library (ResolvesTo, PointerTo, ReferenceOf) to replace the existing PossiblySpecified API. The new library provides clearer semantics for resolving typedefs, decltypes, and cv-qualifiers, and handles edge cases where certain type combinations don't exist in the database. The new API has been integrated into five queries across CERT and MISRA rule sets.

Key Changes

  • New comprehensive type resolution module with classes like ResolvesTo<T>::Exactly, ResolvesTo<T>::IgnoringSpecifiers, ResolvesTo<T>::CvConst, ResolvesTo<T>::Ref, and ResolvesTo<T>::CvConstRef
  • Support for pointer and reference type construction via PointerTo<T>::Type and ReferenceOf<T>::Type
  • Removal of the PossiblySpecified API from Type.qll
  • Migration of five queries to use the new API with improved typedef handling

Reviewed changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
cpp/common/src/codingstandards/cpp/types/Resolve.qll Core type resolution library with parameterized modules for matching types through typedef/decltype chains
cpp/common/src/codingstandards/cpp/types/Specifiers.qll Helper classes for matching const-specified types
cpp/common/src/codingstandards/cpp/types/Type.qll Removes deprecated PossiblySpecified module
cpp/misra/src/rules/RULE-9-5-1/LegacyForStatementsShouldBeSimple.ql Migrated to use ResolvesTo<PointerTo<NonConstType>>::IgnoringSpecifiers and ResolvesTo<ReferenceOf<NonConstType>>::IgnoringSpecifiers
c/misra/src/rules/RULE-22-14/MutexNotInitializedBeforeUse.ql Migrated from PossiblySpecified to ResolvesTo<T>::IgnoringSpecifiers for mutex and condition types
c/misra/src/rules/RULE-22-13/ThreadingObjectWithInvalidStorageDuration.ql Migrated to use ResolvesTo<C11ThreadingObjectType>::IgnoringSpecifiers
c/misra/src/rules/RULE-22-12/NonstandardUseOfThreadingObject.ql Refactored isThreadingObject predicate to use new type resolution
c/cert/src/rules/INT36-C/ConvertingAPointerToIntegerOrIntegerToPointer.ql Comprehensive migration replacing getUnderlyingType() with ResolvesTo<T>::IgnoringSpecifiers for int, pointer, intptr_t, and void pointer types
cpp/common/test/library/codingstandards/cpp/types/Resolve/ResolveTest.ql Test query validating the new type resolution API
cpp/common/test/library/codingstandards/cpp/types/Resolve/test.cpp Comprehensive test cases covering typedefs, decltypes, references, and cv-qualifiers
cpp/common/test/library/codingstandards/cpp/types/options Extractor options for test compilation
change_notes/2025-12-03-type-resolution-tracking-changes.md Change notes documenting query modifications

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

*
* // Examples types that are not `ResolvesTo<FooType>::Ref` types:
* const FooType &cf; // does not match (specified references to FooTypes)
* FooType rf = f; // does not match (non-rerefence FooTypes)
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

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

Typo: "rerefence" should be "reference".

Suggested change
* FooType rf = f; // does not match (non-rerefence FooTypes)
* FooType rf = f; // does not match (non-reference FooTypes)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants