Skip to content

Commit 36a4bdc

Browse files
author
Nikita Kraiouchkine
authored
Merge pull request #37 from lcartey/lcartey/a0-1-6/templates
`A0-1-6`: Do not consider specialized alias templates as unused
2 parents 2319462 + 3062fe9 commit 36a4bdc

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `A0-1-6`: alias templates are now appropriately handled, with alias templates considered used if there exists an instantiation that is used.

cpp/autosar/test/rules/A0-1-6/UnusedTypeDeclarations.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
| test.cpp:10:11:10:11 | D | Type declaration D is not used. |
44
| test.cpp:74:11:74:11 | R | Type declaration R is not used. |
55
| test.cpp:87:12:87:12 | struct <unnamed> | Type declaration struct <unnamed> is not used. |
6+
| test.cpp:108:29:108:30 | AA | Type declaration AA is not used. |

cpp/autosar/test/rules/A0-1-6/test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,12 @@ void test_nested_struct() {
9999

100100
template <class T> class X { // COMPLIANT - template class never instantiated
101101
using custom_type = E<T>; // COMPLIANT - template class never instantiated
102-
};
102+
};
103+
104+
template <class T> class Y {}; // COMPLIANT - used in the test case below
105+
106+
// Alias templates
107+
template <typename T> using Z = Y<T>; // COMPLIANT - used below
108+
template <typename T> using AA = Y<T>; // NON_COMPLIANT - never instantiated
109+
110+
void test_alias_template() { Z<int> v; }

cpp/common/src/codingstandards/cpp/TypeUses.qll

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ Locatable getATypeUse(Type type) {
6666
}
6767

6868
private Locatable getATypeUse_i(Type type) {
69-
// Restrict to uses within the source checkout root
70-
exists(result.getFile().getRelativePath()) and
69+
(
70+
// Restrict to uses within the source checkout root
71+
exists(result.getFile().getRelativePath())
72+
or
73+
// Unless it's an alias template instantiation, as they do not have correct locations (last
74+
// verified in CodeQL CLI 2.7.6)
75+
result instanceof UsingAliasTypedefType and
76+
not exists(result.getLocation())
77+
) and
7178
(
7279
// Used as a variable type
7380
exists(Variable v | result = v |
@@ -143,5 +150,20 @@ private Locatable getATypeUse_i(Type type) {
143150
or
144151
// This is a TemplateClass where one of the specializations is used
145152
type = used.(ClassTemplateSpecialization).getPrimaryTemplate()
153+
or
154+
// Alias templates - alias templates and instantiations are not properly captured by the
155+
// extractor (last verified in CodeQL CLI 2.7.6). The only distinguishing factor is that
156+
// instantiations of alias templates do not have a location.
157+
exists(UsingAliasTypedefType template, UsingAliasTypedefType instantiation |
158+
// Instantiation is a "use" of the template
159+
used = instantiation and
160+
type = template and
161+
// The template has a location
162+
exists(template.getLocation()) and
163+
// The instantiation does not
164+
not exists(instantiation.getLocation()) and
165+
// Template and instantiation both have the same qualified name
166+
template.getQualifiedName() = instantiation.getQualifiedName()
167+
)
146168
)
147169
}

0 commit comments

Comments
 (0)