Skip to content

Commit 795f67c

Browse files
authored
Merge pull request #525 from rvermeulen/rvermeulen/fix-351
Resolve issue 351
2 parents 9c9df75 + b051766 commit 795f67c

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`A0-4-1` - `FloatingPointImplementationShallComplyWithIeeeStandard.ql`:
2+
- May return more results due to improvements to underlying `getATypeUse`.

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

+42-23
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,74 +82,92 @@ 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
86+
reason = "used as a variable type"
8687
or
8788
// Used a function return type
8889
exists(Function f |
8990
result = f and
9091
not f.isCompilerGenerated() and
9192
not type = f.getDeclaringType()
9293
|
93-
type = f.getType()
94+
type = f.getType() and reason = "used as a function return type"
9495
or
95-
type = f.getATemplateArgument()
96+
type = f.getATemplateArgument() and reason = "used as a function template argument"
9697
)
9798
or
9899
// Used either in a function call as a template argument, or as the declaring type
99100
// of the function
100101
exists(FunctionCall fc | result = fc |
101-
type = fc.getTarget().getDeclaringType()
102+
type = fc.getTarget().getDeclaringType() and reason = "used in call to member function"
102103
or
103-
type = fc.getATemplateArgument()
104+
type = fc.getATemplateArgument() and reason = "used in function call template argument"
104105
)
105106
or
106107
// Aliased in a user typedef
107-
exists(TypedefType t | result = t | type = t.getBaseType())
108+
exists(TypedefType t | result = t | type = t.getBaseType()) and
109+
reason = "aliased in user typedef"
108110
or
109111
// A use in a `FunctionAccess`
110-
exists(FunctionAccess fa | result = fa | type = fa.getTarget().getDeclaringType())
112+
exists(FunctionAccess fa | result = fa | type = fa.getTarget().getDeclaringType()) and
113+
reason = "used in a function accesses"
111114
or
112115
// A use in a `sizeof` expr
113-
exists(SizeofTypeOperator soto | result = soto | type = soto.getTypeOperand())
116+
exists(SizeofTypeOperator soto | result = soto | type = soto.getTypeOperand()) and
117+
reason = "used in a sizeof expr"
114118
or
115119
// A use in a `Cast`
116-
exists(Cast c | c = result | type = c.getType())
120+
exists(Cast c | c = result | type = c.getType()) and
121+
reason = "used in a cast"
117122
or
118123
// Use of the type name in source
119-
exists(TypeName t | t = result | type = t.getType())
124+
exists(TypeName t | t = result | type = t.getType()) and
125+
reason = "used in a typename"
120126
or
121127
// Access of an enum constant
122-
exists(EnumConstantAccess eca | result = eca | type = eca.getTarget().getDeclaringEnum())
128+
exists(EnumConstantAccess eca | result = eca | type = eca.getTarget().getDeclaringEnum()) and
129+
reason = "used in an enum constant access"
123130
or
124131
// Accessing a field on the type
125132
exists(FieldAccess fa |
126133
result = fa and
127134
type = fa.getTarget().getDeclaringType()
128-
)
135+
) and
136+
reason = "used in a field access"
129137
or
130138
// Name qualifiers
131139
exists(NameQualifier nq |
132140
result = nq and
133141
type = nq.getQualifyingElement()
134-
)
142+
) and
143+
reason = "used in name qualifier"
144+
or
145+
// Temporary object creation of type `type`
146+
exists(TemporaryObjectExpr toe | result = toe | type = toe.getType()) and
147+
reason = "used in temporary object expr"
135148
)
136149
or
137150
// Recursive case - used by a used type
138-
exists(Type used | result = getATypeUse_i(used) |
151+
exists(Type used | result = getATypeUse_i(used, _) |
139152
// The `used` class has `type` as a base class
140-
type = used.(DerivedType).getBaseType()
153+
type = used.(DerivedType).getBaseType() and
154+
reason = "used in derived type"
141155
or
142156
// The `used` class has `type` as a template argument
143-
type = used.(Class).getATemplateArgument()
157+
type = used.(Class).getATemplateArgument() and
158+
reason = "used in class template argument"
144159
or
145160
// A used class is derived from the type class
146-
type = used.(Class).getABaseClass()
161+
type = used.(Class).getABaseClass() and
162+
reason = "used in derived class"
147163
or
148164
// This is a TemplateClass where one of the instantiations is used
149-
type.(TemplateClass).getAnInstantiation() = used
165+
type.(TemplateClass).getAnInstantiation() = used and
166+
reason = "used in template class instantiation"
150167
or
151168
// This is a TemplateClass where one of the specializations is used
152-
type = used.(ClassTemplateSpecialization).getPrimaryTemplate()
169+
type = used.(ClassTemplateSpecialization).getPrimaryTemplate() and
170+
reason = "used in template class specialization"
153171
or
154172
// Alias templates - alias templates and instantiations are not properly captured by the
155173
// extractor (last verified in CodeQL CLI 2.7.6). The only distinguishing factor is that
@@ -164,6 +182,7 @@ private Locatable getATypeUse_i(Type type) {
164182
not exists(instantiation.getLocation()) and
165183
// Template and instantiation both have the same qualified name
166184
template.getQualifiedName() = instantiation.getQualifiedName()
167-
)
185+
) and
186+
reason = "used in alias template instantiation"
168187
)
169188
}

cpp/common/test/rules/unusedtypedeclarations/test.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ template <typename T> using Z = Y<T>; // COMPLIANT - used below
111111
template <typename T> using AA = Y<T>; // NON_COMPLIANT - never instantiated
112112

113113
void test_alias_template() { Z<int> v; }
114+
115+
void test_temporary_object_creation() {
116+
auto l1 = [](const auto &p1) noexcept {
117+
class C1 { // COMPLIANT - used in temporary object construction
118+
public:
119+
constexpr static const char *m1() noexcept { return "foo"; }
120+
};
121+
122+
return C1{p1};
123+
};
124+
}

0 commit comments

Comments
 (0)