Skip to content

Commit 92a3d5e

Browse files
committed
Add reason to type use for debug purposes
1 parent a65a8b8 commit 92a3d5e

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

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

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private TypedefType getAnEquivalentTypeDef(TypedefType type) {
3535
* is from within the function signature or field declaration of the type itself.
3636
*/
3737
Locatable getATypeUse(Type type) {
38-
result = getATypeUse_i(type)
38+
result = getATypeUse_i(type, _)
3939
or
4040
// Identify `TypeMention`s of typedef types, where the underlying type is used.
4141
//
@@ -61,11 +61,11 @@ Locatable getATypeUse(Type type) {
6161
tm.getMentionedType() = typedefType
6262
|
6363
exists(tm.getFile().getRelativePath()) and
64-
exists(getATypeUse_i(typedefType.getUnderlyingType()))
64+
exists(getATypeUse_i(typedefType.getUnderlyingType(), _))
6565
)
6666
}
6767

68-
private Locatable getATypeUse_i(Type type) {
68+
private Locatable getATypeUse_i(Type type, string reason) {
6969
(
7070
// Restrict to uses within the source checkout root
7171
exists(result.getFile().getRelativePath())
@@ -82,77 +82,86 @@ private Locatable getATypeUse_i(Type type) {
8282
// Ignore self referential variables and parameters
8383
not v.getDeclaringType().refersTo(type) and
8484
not type = v.(Parameter).getFunction().getDeclaringType()
85-
)
85+
) and reason = "used as a variable type"
8686
or
8787
// Used a function return type
8888
exists(Function f |
8989
result = f and
9090
not f.isCompilerGenerated() and
9191
not type = f.getDeclaringType()
9292
|
93-
type = f.getType()
93+
type = f.getType() and reason = "used as a function return type"
9494
or
95-
type = f.getATemplateArgument()
96-
)
97-
or
95+
type = f.getATemplateArgument() and reason = "used as a function template argument"
96+
) or
9897
// Used either in a function call as a template argument, or as the declaring type
9998
// of the function
10099
exists(FunctionCall fc | result = fc |
101-
type = fc.getTarget().getDeclaringType()
100+
type = fc.getTarget().getDeclaringType() and reason = "used in call to member function"
102101
or
103-
type = fc.getATemplateArgument()
102+
type = fc.getATemplateArgument() and reason = "used in function call template argument"
104103
)
105104
or
106105
// Aliased in a user typedef
107-
exists(TypedefType t | result = t | type = t.getBaseType())
106+
exists(TypedefType t | result = t | type = t.getBaseType()) and
107+
reason = "aliased in user typedef"
108108
or
109109
// A use in a `FunctionAccess`
110-
exists(FunctionAccess fa | result = fa | type = fa.getTarget().getDeclaringType())
110+
exists(FunctionAccess fa | result = fa | type = fa.getTarget().getDeclaringType()) and
111+
reason = "used in a function accesses"
111112
or
112113
// A use in a `sizeof` expr
113-
exists(SizeofTypeOperator soto | result = soto | type = soto.getTypeOperand())
114+
exists(SizeofTypeOperator soto | result = soto | type = soto.getTypeOperand()) and
115+
reason = "used in a sizeof expr"
114116
or
115117
// A use in a `Cast`
116-
exists(Cast c | c = result | type = c.getType())
118+
exists(Cast c | c = result | type = c.getType()) and reason = "used in a cast"
117119
or
118120
// Use of the type name in source
119-
exists(TypeName t | t = result | type = t.getType())
121+
exists(TypeName t | t = result | type = t.getType()) and reason = "used in a typename"
120122
or
121123
// Access of an enum constant
122-
exists(EnumConstantAccess eca | result = eca | type = eca.getTarget().getDeclaringEnum())
124+
exists(EnumConstantAccess eca | result = eca | type = eca.getTarget().getDeclaringEnum()) and
125+
reason = "used in an enum constant access"
123126
or
124127
// Accessing a field on the type
125128
exists(FieldAccess fa |
126129
result = fa and
127130
type = fa.getTarget().getDeclaringType()
128-
)
131+
) and reason = "used in a field access"
129132
or
130133
// Name qualifiers
131134
exists(NameQualifier nq |
132135
result = nq and
133136
type = nq.getQualifyingElement()
134-
)
137+
) and reason = "used in name qualifier"
135138
// Temporary object creation of type `type`
136139
or
137-
exists(TemporaryObjectExpr toe | result = toe | type = toe.getType())
140+
exists(TemporaryObjectExpr toe | result = toe | type = toe.getType()) and
141+
reason = "used in temporary object expr"
138142
)
139143
or
140144
// Recursive case - used by a used type
141-
exists(Type used | result = getATypeUse_i(used) |
145+
exists(Type used | result = getATypeUse_i(used, _) |
142146
// The `used` class has `type` as a base class
143-
type = used.(DerivedType).getBaseType()
147+
type = used.(DerivedType).getBaseType() and
148+
reason = "used in derived type"
144149
or
145150
// The `used` class has `type` as a template argument
146-
type = used.(Class).getATemplateArgument()
151+
type = used.(Class).getATemplateArgument() and
152+
reason = "used in class template argument"
147153
or
148154
// A used class is derived from the type class
149-
type = used.(Class).getABaseClass()
155+
type = used.(Class).getABaseClass() and
156+
reason = "used in derived class"
150157
or
151158
// This is a TemplateClass where one of the instantiations is used
152-
type.(TemplateClass).getAnInstantiation() = used
159+
type.(TemplateClass).getAnInstantiation() = used and
160+
reason = "used in template class instantiation"
153161
or
154162
// This is a TemplateClass where one of the specializations is used
155163
type = used.(ClassTemplateSpecialization).getPrimaryTemplate()
164+
and reason = "used in template class specialization"
156165
or
157166
// Alias templates - alias templates and instantiations are not properly captured by the
158167
// extractor (last verified in CodeQL CLI 2.7.6). The only distinguishing factor is that
@@ -167,6 +176,6 @@ private Locatable getATypeUse_i(Type type) {
167176
not exists(instantiation.getLocation()) and
168177
// Template and instantiation both have the same qualified name
169178
template.getQualifiedName() = instantiation.getQualifiedName()
170-
)
179+
) and reason = "used in alias template instantiation"
171180
)
172181
}

0 commit comments

Comments
 (0)