Skip to content

A0-1-6: Do not consider specialized alias templates as unused #37

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions change_notes/2022-08-01-a0-1-6-alias-templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `A0-1-6`: alias templates are now appropriately handled, with alias templates considered used if there exists an instantiation that is used.
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
| test.cpp:10:11:10:11 | D | Type declaration D is not used. |
| test.cpp:74:11:74:11 | R | Type declaration R is not used. |
| test.cpp:87:12:87:12 | struct <unnamed> | Type declaration struct <unnamed> is not used. |
| test.cpp:108:29:108:30 | AA | Type declaration AA is not used. |
10 changes: 9 additions & 1 deletion cpp/autosar/test/rules/A0-1-6/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,12 @@ void test_nested_struct() {

template <class T> class X { // COMPLIANT - template class never instantiated
using custom_type = E<T>; // COMPLIANT - template class never instantiated
};
};

template <class T> class Y {}; // COMPLIANT - used in the test case below

// Alias templates
template <typename T> using Z = Y<T>; // COMPLIANT - used below
template <typename T> using AA = Y<T>; // NON_COMPLIANT - never instantiated

void test_alias_template() { Z<int> v; }
26 changes: 24 additions & 2 deletions cpp/common/src/codingstandards/cpp/TypeUses.qll
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ Locatable getATypeUse(Type type) {
}

private Locatable getATypeUse_i(Type type) {
// Restrict to uses within the source checkout root
exists(result.getFile().getRelativePath()) and
(
// Restrict to uses within the source checkout root
exists(result.getFile().getRelativePath())
or
// Unless it's an alias template instantiation, as they do not have correct locations (last
// verified in CodeQL CLI 2.7.6)
result instanceof UsingAliasTypedefType and
not exists(result.getLocation())
) and
(
// Used as a variable type
exists(Variable v | result = v |
Expand Down Expand Up @@ -143,5 +150,20 @@ private Locatable getATypeUse_i(Type type) {
or
// This is a TemplateClass where one of the specializations is used
type = used.(ClassTemplateSpecialization).getPrimaryTemplate()
or
// Alias templates - alias templates and instantiations are not properly captured by the
// extractor (last verified in CodeQL CLI 2.7.6). The only distinguishing factor is that
// instantiations of alias templates do not have a location.
exists(UsingAliasTypedefType template, UsingAliasTypedefType instantiation |
// Instantiation is a "use" of the template
used = instantiation and
type = template and
// The template has a location
exists(template.getLocation()) and
// The instantiation does not
not exists(instantiation.getLocation()) and
// Template and instantiation both have the same qualified name
template.getQualifiedName() = instantiation.getQualifiedName()
)
)
}