There was a problem hiding this comment.
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.
Copilot reviewed 12 out of 13 changed files in this pull request and generated 7 comments.
Show a summary per file| 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.
Sorry, something went wrong.
| * | ||
| * // 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) |
There was a problem hiding this comment.
Typo: "rerefence" should be "reference".
| * FooType rf = f; // does not match (non-rerefence FooTypes) | |
| * FooType rf = f; // does not match (non-reference FooTypes) |
Sorry, something went wrong.
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:
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 ideas?
Change request type
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.