Skip to content

Commit 0575818

Browse files
authored
Merge pull request #7884 from rdmarsh2/rdmarsh2/template-implicit-copy-constructor
C++: fix hasImplicitCopyConstructor for templates
2 parents a08246a + 1e2cc4f commit 0575818

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* `hasImplicitCopyConstructor` and `hasImplicitCopyAssignmentOperator` now correctly handle implicitly-deleted operators in templates.

cpp/ql/lib/semmle/code/cpp/Class.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ class Class extends UserType {
251251
not this.implicitCopyConstructorDeleted() and
252252
forall(CopyConstructor cc | cc = this.getAMemberFunction() |
253253
cc.isCompilerGenerated() and not cc.isDeleted()
254+
) and
255+
(
256+
not this instanceof ClassTemplateInstantiation
257+
or
258+
this.(ClassTemplateInstantiation).getTemplate().hasImplicitCopyConstructor()
259+
) and
260+
(
261+
not this instanceof PartialClassTemplateSpecialization
262+
or
263+
this.(PartialClassTemplateSpecialization).getPrimaryTemplate().hasImplicitCopyConstructor()
254264
)
255265
}
256266

@@ -266,6 +276,18 @@ class Class extends UserType {
266276
not this.implicitCopyAssignmentOperatorDeleted() and
267277
forall(CopyAssignmentOperator ca | ca = this.getAMemberFunction() |
268278
ca.isCompilerGenerated() and not ca.isDeleted()
279+
) and
280+
(
281+
not this instanceof ClassTemplateInstantiation
282+
or
283+
this.(ClassTemplateInstantiation).getTemplate().hasImplicitCopyAssignmentOperator()
284+
) and
285+
(
286+
not this instanceof PartialClassTemplateSpecialization
287+
or
288+
this.(PartialClassTemplateSpecialization)
289+
.getPrimaryTemplate()
290+
.hasImplicitCopyAssignmentOperator()
269291
)
270292
}
271293

cpp/ql/test/library-tests/special_members/generated_copy/assign.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| difference::Base | can | does NOT | have implicit copy assignment |
1212
| difference::OnlyAssign | can | does | have implicit copy assignment |
1313
| difference::OnlyCtor | can NOT | does NOT | have implicit copy assignment |
14+
| instantiated_explicit_ctor::Wrapper<int> | can | does | have implicit copy assignment |
1415
| moves::MoveAssign | can NOT | does NOT | have implicit copy assignment |
1516
| moves::MoveCtor | can NOT | does NOT | have implicit copy assignment |
1617
| private_cc::C | can | does NOT | have implicit copy assignment |

cpp/ql/test/library-tests/special_members/generated_copy/copy.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,21 @@ namespace difference {
131131
class OnlyAssign : Base {
132132
};
133133
}
134+
135+
namespace instantiated_explicit_ctor {
136+
template<class T>
137+
class Wrapper {
138+
public:
139+
Wrapper(Wrapper<T> &other) {
140+
m_t = other.m_t;
141+
}
142+
143+
Wrapper() {
144+
m_t = 0;
145+
}
146+
private:
147+
T m_t;
148+
};
149+
150+
Wrapper<int> wrapped_int;
151+
}

cpp/ql/test/library-tests/special_members/generated_copy/ctor.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| difference::Base | can | does NOT | have implicit copy constructor |
1212
| difference::OnlyAssign | can NOT | does NOT | have implicit copy constructor |
1313
| difference::OnlyCtor | can | does | have implicit copy constructor |
14+
| instantiated_explicit_ctor::Wrapper<int> | can | does NOT | have implicit copy constructor |
1415
| moves::MoveAssign | can NOT | does NOT | have implicit copy constructor |
1516
| moves::MoveCtor | can NOT | does NOT | have implicit copy constructor |
1617
| private_cc::C | can | does NOT | have implicit copy constructor |

cpp/ql/test/library-tests/special_members/generated_copy/functions.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,9 @@
8686
| copy.cpp:131:9:131:9 | OnlyAssign | deleted | |
8787
| copy.cpp:131:9:131:9 | operator= | | |
8888
| copy.cpp:131:9:131:9 | operator= | | |
89+
| copy.cpp:137:9:137:9 | operator= | | |
90+
| copy.cpp:139:5:139:11 | Wrapper | | |
91+
| copy.cpp:143:5:143:5 | Wrapper | | |
92+
| copy.cpp:143:5:143:11 | Wrapper | | |
8993
| file://:0:0:0:0 | operator= | | |
9094
| file://:0:0:0:0 | operator= | | |

0 commit comments

Comments
 (0)