Skip to content

Commit 39ba455

Browse files
committed
A0-1-6: Handle alias templates
alias templates are not properly handled by the extractor, so we provide special case handling that ensures alias template instantiations are correctly associated with alias templates. alias templates are not represented using the same template infrastructure as other templates. Instead, each instantiation is a copy of the template type, with no specified location. We use this to identify templates with at least one instantiation. We assume an instantiated template is used, as the only way it would be instantiated is if it was used.
1 parent e5b4abb commit 39ba455

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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:104:26:104:26 | Y<int> | Type declaration Y<int> is not used. |
7-
| test.cpp:107:29:107:29 | Z | Type declaration Z is not used. |
86
| test.cpp:108:29:108:30 | AA | Type declaration AA is not used. |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ template <class T> class X { // COMPLIANT - template class never instantiated
101101
using custom_type = E<T>; // COMPLIANT - template class never instantiated
102102
};
103103

104-
template <class T> class Y {}; // COMPLIANT[FALSE_POSITIVE] - used in the test case below
104+
template <class T> class Y {}; // COMPLIANT - used in the test case below
105105

106106
// Alias templates
107-
template <typename T> using Z = Y<T>; // COMPLIANT[FALSE_POSITIVE] - used below
107+
template <typename T> using Z = Y<T>; // COMPLIANT - used below
108108
template <typename T> using AA = Y<T>; // NON_COMPLIANT - never instantiated
109109

110110
void test_alias_template() {

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)