Skip to content

Commit 67bb9d3

Browse files
First fully working draft
1 parent 5532f13 commit 67bb9d3

13 files changed

+200
-60
lines changed

cpp/common/src/codingstandards/cpp/exclusions/cpp/Toolchain3.qll

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype Toolchain3Query =
77
TRedeclarationOfStaticConstexprDataMemberAuditQuery() or
8+
TImplicitDeclarationOfCopyConstructorQuery() or
89
TImplicitDeclarationOfCopyConstructorAuditQuery() or
910
TNoexceptSpecifierThrowAuditQuery() or
1011
TUseOfDeprecatedCHeadersQuery() or
1112
TUseOfDeprecatedStrStreamClassQuery() or
1213
TUseOfUncaughtExceptionQuery() or
13-
TUseOfWeakResultTypesAuditQuery() or
1414
TUseOfDeprecatedFunctionBinderTypedefMemberAuditQuery() or
1515
TUseOfDeprecatedUnaryOrBinaryNegateQuery() or
1616
TUseOfDeprecatedAllocatorVoidQuery() or
@@ -31,6 +31,15 @@ predicate isToolchain3QueryMetadata(Query query, string queryId, string ruleId,
3131
ruleId = "RULE-4-1-2" and
3232
category = "advisory"
3333
or
34+
query =
35+
// `Query` instance for the `implicitDeclarationOfCopyConstructor` query
36+
Toolchain3Package::implicitDeclarationOfCopyConstructorQuery() and
37+
queryId =
38+
// `@id` for the `implicitDeclarationOfCopyConstructor` query
39+
"cpp/misra/implicit-declaration-of-copy-constructor" and
40+
ruleId = "RULE-4-1-2" and
41+
category = "advisory"
42+
or
3443
query =
3544
// `Query` instance for the `implicitDeclarationOfCopyConstructorAudit` query
3645
Toolchain3Package::implicitDeclarationOfCopyConstructorAuditQuery() and
@@ -76,15 +85,6 @@ predicate isToolchain3QueryMetadata(Query query, string queryId, string ruleId,
7685
ruleId = "RULE-4-1-2" and
7786
category = "advisory"
7887
or
79-
query =
80-
// `Query` instance for the `useOfWeakResultTypesAudit` query
81-
Toolchain3Package::useOfWeakResultTypesAuditQuery() and
82-
queryId =
83-
// `@id` for the `useOfWeakResultTypesAudit` query
84-
"cpp/misra/use-of-weak-result-types-audit" and
85-
ruleId = "RULE-4-1-2" and
86-
category = "advisory"
87-
or
8888
query =
8989
// `Query` instance for the `useOfDeprecatedFunctionBinderTypedefMemberAudit` query
9090
Toolchain3Package::useOfDeprecatedFunctionBinderTypedefMemberAuditQuery() and
@@ -175,6 +175,13 @@ module Toolchain3Package {
175175
TQueryCPP(TToolchain3PackageQuery(TRedeclarationOfStaticConstexprDataMemberAuditQuery()))
176176
}
177177

178+
Query implicitDeclarationOfCopyConstructorQuery() {
179+
//autogenerate `Query` type
180+
result =
181+
// `Query` type for `implicitDeclarationOfCopyConstructor` query
182+
TQueryCPP(TToolchain3PackageQuery(TImplicitDeclarationOfCopyConstructorQuery()))
183+
}
184+
178185
Query implicitDeclarationOfCopyConstructorAuditQuery() {
179186
//autogenerate `Query` type
180187
result =
@@ -210,13 +217,6 @@ module Toolchain3Package {
210217
TQueryCPP(TToolchain3PackageQuery(TUseOfUncaughtExceptionQuery()))
211218
}
212219

213-
Query useOfWeakResultTypesAuditQuery() {
214-
//autogenerate `Query` type
215-
result =
216-
// `Query` type for `useOfWeakResultTypesAudit` query
217-
TQueryCPP(TToolchain3PackageQuery(TUseOfWeakResultTypesAuditQuery()))
218-
}
219-
220220
Query useOfDeprecatedFunctionBinderTypedefMemberAuditQuery() {
221221
//autogenerate `Query` type
222222
result =

cpp/common/src/codingstandards/cpp/types/ImplicitSpecialMemberFunctions.qll

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ predicate mustNotBeImplicitlyDefined(Function mf) {
1717
)
1818
}
1919

20-
predicate mustBeUserDeclared(Function mf) {
21-
exists(FunctionDeclarationEntry fde | fde.getFunction() = mf and not fde.isImplicit())
22-
}
20+
predicate mustBeUserDeclared(Function mf) { not mf.isCompilerGenerated() }
2321

2422
predicate mustNotBeUserDeclared(Function mf) {
2523
// use forex: at least one declaration must exist, and all declarations must be implicit
26-
forex(FunctionDeclarationEntry fde | fde.getFunction() = mf | fde.isImplicit())
24+
mf.isCompilerGenerated()
2725
}
2826

2927
private signature class SpecialMember extends Function;
@@ -125,12 +123,18 @@ private class CMayHaveIdefCA =
125123
*
126124
* In our case, `ClassesWhere<...>` performs steps 7 and 8 together via `NotMatching`.
127125
*/
128-
private class CMayHaveUdecCC = ClassesWhere<mustNotBeUserDeclared/1, CopyConstructor>::NotMatching;
129-
130-
private class CMayHaveUdecCA =
131-
ClassesWhere<mustNotBeUserDeclared/1, CopyAssignmentOperator>::NotMatching;
132-
133-
private class CMayHaveUdecD = ClassesWhere<mustNotBeUserDeclared/1, Destructor>::NotMatching;
126+
//private class CMayHaveUdecCC = ClassesWhere<mustNotBeUserDeclared/1, CopyConstructor>::NotMatching;
127+
//
128+
//private class CMayHaveUdecCA =
129+
// ClassesWhere<mustNotBeUserDeclared/1, CopyAssignmentOperator>::NotMatching;
130+
//
131+
//private class CMayHaveUdecD = ClassesWhere<mustBeUserDeclared/1, Destructor>::Matching;
132+
// These are actually 100% known. If there is no dtor, it is not user declared.
133+
private class CMayHaveUdecCC = CMustHaveUdecCC;
134+
135+
private class CMayHaveUdecCA = CMustHaveUdecCA;
136+
137+
private class CMayHaveUdecD = CMustHaveUdecD;
134138

135139
/* - Step 9: All C' may be deprecated where C' in C_mayHave{IDEF CC} and (C' in C_mayHave{UDEC CA} or C' in C_mayHave{UDEC D}) */
136140
class MayHaveDeprecatedCopyConstructor extends CMayHaveIdefCC {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @id cpp/misra/implicit-declaration-of-copy-constructor
3+
* @name RULE-4-1-2: Implicit declaration of copy constructors is a deprecated language feature should not be used
4+
* @description Deprecated language features such as implicit declarations of copy constructors are
5+
* only supported for backwards compatibility; these are considered bad practice, or
6+
* have been superceded by better alternatives.
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity warning
10+
* @tags external/misra/id/rule-4-1-2
11+
* scope/single-translation-unit
12+
* maintainability
13+
* external/misra/enforcement/decidable
14+
* external/misra/obligation/advisory
15+
*/
16+
17+
import cpp
18+
import codingstandards.cpp.misra
19+
import cpp
20+
import codingstandards.cpp.misra
21+
import codingstandards.cpp.types.ImplicitSpecialMemberFunctions
22+
23+
from Class c, string specialMemberName
24+
where
25+
not isExcluded(c, Toolchain3Package::implicitDeclarationOfCopyConstructorAuditQuery()) and
26+
(
27+
c instanceof MustHaveDeprecatedCopyConstructor and
28+
specialMemberName = "copy constructor"
29+
or
30+
c instanceof MustHaveDeprecatedCopyAssignmentOperator and
31+
specialMemberName = "copy assignment operator"
32+
)
33+
select c, "Class '" + c.getName() + "' has a deprecated implicit " + specialMemberName + "."

cpp/misra/src/rules/RULE-4-1-2/ImplicitDeclarationOfCopyConstructorAudit.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ where
2424
not isExcluded(c, Toolchain3Package::implicitDeclarationOfCopyConstructorAuditQuery()) and
2525
(
2626
c instanceof MayHaveDeprecatedCopyConstructor and
27+
not c instanceof MustHaveDeprecatedCopyConstructor and
2728
specialMemberName = "copy constructor"
2829
or
2930
c instanceof MayHaveDeprecatedCopyAssignmentOperator and
31+
not c instanceof MustHaveDeprecatedCopyAssignmentOperator and
3032
specialMemberName = "copy assignment operator"
3133
)
32-
select c, "Class '" + c.getName() + "' may have a deprecated " + specialMemberName + "."
34+
select c, "Class '" + c.getName() + "' may have a deprecated implicit " + specialMemberName + "."

cpp/misra/src/rules/RULE-4-1-2/RedeclarationOfStaticConstexprDataMemberAudit.ql

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@
1818
import cpp
1919
import codingstandards.cpp.misra
2020

21-
from
21+
from Variable v, Initializer i
2222
where
23-
not isExcluded(x, Toolchain3Package::redeclarationOfStaticConstexprDataMemberAuditQuery()) and
24-
select
23+
not isExcluded(v, Toolchain3Package::redeclarationOfStaticConstexprDataMemberAuditQuery()) and
24+
v.isStatic() and
25+
i.getDeclaration() = v and
26+
v.isConstexpr() and
27+
(
28+
// The initializer location is in the class, and the varibale location is outside.
29+
// Detect if they're different files:
30+
not v.getLocation().getFile() = i.getLocation().getFile()
31+
or
32+
// Or if the variable is declared after the initializer:
33+
i.getLocation().getEndLine() < v.getLocation().getEndLine()
34+
)
35+
select v,
36+
"Static constexpr data member '" + v.getName() +
37+
"' is redeclared, which is a deprecated language feature."

cpp/misra/src/rules/RULE-4-1-2/UseOfWeakResultTypesAudit.ql

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
| test.cpp:167:7:167:8 | C1 | Class 'C1' has a deprecated implicit copy assignment operator. |
2+
| test.cpp:167:7:167:8 | C1 | Class 'C1' has a deprecated implicit copy constructor. |
3+
| test.cpp:174:7:174:8 | C2 | Class 'C2' has a deprecated implicit copy assignment operator. |
4+
| test.cpp:181:7:181:8 | C3 | Class 'C3' has a deprecated implicit copy constructor. |
5+
| test.cpp:188:7:188:8 | C4 | Class 'C4' has a deprecated implicit copy assignment operator. |
6+
| test.cpp:188:7:188:8 | C4 | Class 'C4' has a deprecated implicit copy constructor. |
7+
| test.cpp:196:7:196:8 | C5 | Class 'C5' has a deprecated implicit copy assignment operator. |
8+
| test.cpp:196:7:196:8 | C5 | Class 'C5' has a deprecated implicit copy constructor. |
9+
| test.cpp:214:7:214:8 | C7 | Class 'C7' has a deprecated implicit copy assignment operator. |
10+
| test.cpp:214:7:214:8 | C7 | Class 'C7' has a deprecated implicit copy constructor. |
11+
| test.cpp:223:7:223:8 | C8 | Class 'C8' has a deprecated implicit copy assignment operator. |
12+
| test.cpp:223:7:223:8 | C8 | Class 'C8' has a deprecated implicit copy constructor. |
13+
| test.cpp:231:7:231:8 | C9 | Class 'C9' has a deprecated implicit copy assignment operator. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-4-1-2/ImplicitDeclarationOfCopyConstructor.ql
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
No expected results have yet been specified
1+
| test.cpp:231:7:231:8 | C9 | Class 'C9' may have a deprecated implicit copy constructor. |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
No expected results have yet been specified
1+
| test.cpp:255:15:255:21 | m2 | Static constexpr data member 'm2' is redeclared, which is a deprecated language feature. |

0 commit comments

Comments
 (0)