Skip to content

Commit 2b859f3

Browse files
committed
Fix false positives in RULE-15-1-4 for class template constructors
Delegating constructors of uninstantiated class templates are not modelled as delegating, so all members were reported as uninitialized. Skip the template and rely on the instantiations.
1 parent bc8a0eb commit 2b859f3

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `RULE-15-1-4` - `NonStaticMemberNotInitBeforeUse.ql`:
2+
- Remove false positives for constructors of uninstantiated class templates, where delegating constructors are not modelled as such. The instantiations are still checked.

‎cpp/misra/src/rules/RULE-15-1-4/NonStaticMemberNotInitBeforeUse.ql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class IncompleteConstructor extends Constructor, IncompleteInitialization {
116116
not getDeclaringType() instanceof Union and
117117
this.getDeclaringType().hasDefinition() and
118118
not this.isDeleted() and
119+
// Constructors of uninstantiated class templates cannot be analysed reliably, because for
120+
// instance a delegating constructor is not modelled as such. The instantiations are checked.
121+
not this.getDeclaringType() instanceof TemplateClass and
119122
// Delegating constructors do not need to initialize members
120123
not any(ConstructorDelegationInit init) = this.getAnInitializer() and
121124
// exclude defaulted move and copy constructors.

‎cpp/misra/test/rules/RULE-15-1-4/NonStaticMemberNotInitBeforeUse.expected‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
| test.cpp:267:18:267:18 | a | Aggregate variable does not initialize non-static data member $@ (and 9 more). | test.cpp:262:43:262:45 | m10 | m10 |
2525
| test.cpp:290:18:290:18 | a | Aggregate variable does not initialize non-static data member $@ (and 1 more). | test.cpp:285:9:285:10 | m1 | m1 |
2626
| test.cpp:290:18:290:18 | a | Aggregate variable does not initialize non-static data member $@ (and 1 more). | test.cpp:286:11:286:12 | m2 | m2 |
27+
| test.cpp:318:3:318:3 | TemplateMemberUninit | Constructor does not initialize non-static data member $@. | test.cpp:322:7:322:8 | m2 | m2 |

‎cpp/misra/test/rules/RULE-15-1-4/test.cpp‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,52 @@ union UnionWithConstructor {
302302
// Union constructors should still be required to initialize one member, but
303303
// this is not implemented.
304304
UnionWithConstructor() {} // NON_COMPLIANT[False negative]
305-
};
305+
};
306+
template <typename T> class TemplateAllInit {
307+
public:
308+
TemplateAllInit(T value) : m1(value), m2(0) {} // COMPLIANT
309+
TemplateAllInit(const TemplateAllInit &) = delete;
310+
311+
private:
312+
T m1;
313+
int m2;
314+
};
315+
316+
template <typename T> class TemplateMemberUninit {
317+
public:
318+
TemplateMemberUninit(T value) : m1(value) {} // NON_COMPLIANT
319+
320+
private:
321+
T m1;
322+
int m2;
323+
};
324+
325+
template <typename T> class TemplateOutOfLineInit {
326+
public:
327+
TemplateOutOfLineInit(T value);
328+
329+
private:
330+
T m1;
331+
int m2;
332+
};
333+
334+
template <typename T>
335+
TemplateOutOfLineInit<T>::TemplateOutOfLineInit(T value)
336+
: m1(value), m2(0) {} // COMPLIANT
337+
338+
template <typename T> class TemplateDelegating {
339+
public:
340+
TemplateDelegating() : TemplateDelegating(0) {} // COMPLIANT
341+
TemplateDelegating(T value) : m1(value), m2(0) {} // COMPLIANT
342+
343+
private:
344+
T m1;
345+
int m2;
346+
};
347+
348+
void instantiateTemplates() {
349+
TemplateAllInit<int> a(1);
350+
TemplateMemberUninit<int> b(1);
351+
TemplateOutOfLineInit<int> c(1);
352+
TemplateDelegating<int> d;
353+
}

0 commit comments

Comments
 (0)