Skip to content

Commit 057e33b

Browse files
Implement audit query
1 parent a31aac3 commit 057e33b

5 files changed

Lines changed: 123 additions & 35 deletions

File tree

cpp/misra/src/rules/RULE-15-0-1/AnalyzableClass.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ private predicate isUsable(MemberFunction f) {
88
private predicate isMemberCustomized(MemberFunction f) {
99
exists(f.getDefinition()) and
1010
not f.isDefaulted() and
11-
not f.isDeleted()
11+
not f.isDeleted() and
12+
not f.isCompilerGenerated()
1213
}
1314

1415
newtype TSpecialMember =

cpp/misra/src/rules/RULE-15-0-1/ImproperlyProvidedSpecialMemberFunctionsAudit.ql

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,33 @@
1717

1818
import cpp
1919
import codingstandards.cpp.misra
20+
import AnalyzableClass
2021

21-
from
22+
string missingKind(Class c) {
23+
not c.getAConstructor() instanceof MoveConstructor and
24+
result = "move constructor"
25+
or
26+
not c.getAMemberFunction() instanceof MoveAssignmentOperator and
27+
result = "move assignment operator"
28+
or
29+
not c.getAConstructor() instanceof CopyConstructor and
30+
result = "copy constructor"
31+
or
32+
not c.getAMemberFunction() instanceof CopyAssignmentOperator and
33+
result = "copy assignment operator"
34+
or
35+
not c.getAMemberFunction() instanceof Destructor and
36+
result = "destructor"
37+
}
38+
39+
string missingKinds(Class c) { result = concat(missingKind(c), " and ") }
40+
41+
from Class c, string kinds
2242
where
23-
not isExcluded(x, Classes3Package::improperlyProvidedSpecialMemberFunctionsAuditQuery()) and
24-
select
43+
not isExcluded(c, Classes3Package::improperlyProvidedSpecialMemberFunctionsAuditQuery()) and
44+
not c instanceof AnalyzableClass and
45+
not c.isPod() and
46+
kinds = missingKinds(c)
47+
select c,
48+
"Class '" + c.getName() + "' is not analyzable because the " + kinds +
49+
" are not present in the CodeQL database."
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
| test.cpp:69:11:69:12 | C2 | Class 'C2' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
2-
| test.cpp:70:11:70:12 | C3 | Class 'C3' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
3-
| test.cpp:72:11:72:12 | C5 | Class 'C5' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
4-
| test.cpp:74:11:74:12 | C7 | Class 'C7' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
5-
| test.cpp:76:11:76:12 | C9 | Class 'C9' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
6-
| test.cpp:77:11:77:13 | C10 | Class 'C10' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
7-
| test.cpp:78:11:78:13 | C11 | Class 'C11' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
8-
| test.cpp:79:11:79:13 | C12 | Class 'C12' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
9-
| test.cpp:80:11:80:13 | C13 | Class 'C13' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
10-
| test.cpp:81:11:81:13 | C14 | Class 'C14' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
11-
| test.cpp:82:11:82:13 | C15 | Class 'C15' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
12-
| test.cpp:100:7:100:21 | PrivateCopyCtor | Class 'PrivateCopyCtor' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
13-
| test.cpp:113:7:113:21 | PrivateMoveCtor | Class 'PrivateMoveCtor' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
14-
| test.cpp:126:7:126:23 | PrivateCopyAssign | Class 'PrivateCopyAssign' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
15-
| test.cpp:139:7:139:23 | PrivateMoveAssign | Class 'PrivateMoveAssign' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
16-
| test.cpp:179:7:179:45 | CustomizedCopyCtorDefaultedNonCompliant | Class 'CustomizedCopyCtorDefaultedNonCompliant' has customized copy constructor, but does not customize the destructor. |
17-
| test.cpp:186:7:186:43 | CustomizedCopyCtorDeletedNonCompliant | Class 'CustomizedCopyCtorDeletedNonCompliant' has customized copy constructor, but does not customize the destructor. |
18-
| test.cpp:192:7:192:36 | CustomizedMoveCtorNonCompliant | Class 'CustomizedMoveCtorNonCompliant' has customized move constructor, but does not customize the destructor. |
19-
| test.cpp:198:7:198:38 | CustomizedCopyAssignNonCompliant | Class 'CustomizedCopyAssignNonCompliant' has customized copy assignment operator, but does not customize the destructor. |
20-
| test.cpp:204:7:204:38 | CustomizedMoveAssignNonCompliant | Class 'CustomizedMoveAssignNonCompliant' has customized copy constructor, move assignment operator, move constructor, but does not customize the destructor. |
21-
| test.cpp:211:7:211:39 | MoveOnlyNotCustomizedNonCompliant | Class 'MoveOnlyNotCustomizedNonCompliant' has customized the destructor, but does not customize the move constructor. |
22-
| test.cpp:218:7:218:49 | MoveOnlyAssignableNotCustomizedNonCompliant | Class 'MoveOnlyAssignableNotCustomizedNonCompliant' has customized the destructor, but does not customize the move assignment operator. |
23-
| test.cpp:218:7:218:49 | MoveOnlyAssignableNotCustomizedNonCompliant | Class 'MoveOnlyAssignableNotCustomizedNonCompliant' has customized the destructor, but does not customize the move constructor. |
24-
| test.cpp:249:7:249:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the copy assignment operator. |
25-
| test.cpp:249:7:249:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the copy constructor. |
26-
| test.cpp:249:7:249:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the move assignment operator. |
27-
| test.cpp:249:7:249:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the move constructor. |
28-
| test.cpp:268:7:268:33 | UnmovableBaseNonvirtualDtor | Class 'UnmovableBaseNonvirtualDtor' violates inheritance requirements for special member functions. |
29-
| test.cpp:289:7:289:33 | UnmovablePrivateVirtualDtor | Class 'UnmovablePrivateVirtualDtor' violates inheritance requirements for special member functions. |
30-
| test.cpp:316:7:316:30 | BaseVirtualProtectedDtor | Class 'BaseVirtualProtectedDtor' violates inheritance requirements for special member functions. |
1+
| test.cpp:71:11:71:12 | C2 | Class 'C2' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
2+
| test.cpp:72:11:72:12 | C3 | Class 'C3' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
3+
| test.cpp:74:11:74:12 | C5 | Class 'C5' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
4+
| test.cpp:76:11:76:12 | C7 | Class 'C7' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
5+
| test.cpp:78:11:78:12 | C9 | Class 'C9' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
6+
| test.cpp:79:11:79:13 | C10 | Class 'C10' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
7+
| test.cpp:80:11:80:13 | C11 | Class 'C11' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
8+
| test.cpp:81:11:81:13 | C12 | Class 'C12' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
9+
| test.cpp:82:11:82:13 | C13 | Class 'C13' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
10+
| test.cpp:83:11:83:13 | C14 | Class 'C14' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
11+
| test.cpp:84:11:84:13 | C15 | Class 'C15' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
12+
| test.cpp:102:7:102:21 | PrivateCopyCtor | Class 'PrivateCopyCtor' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
13+
| test.cpp:115:7:115:21 | PrivateMoveCtor | Class 'PrivateMoveCtor' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
14+
| test.cpp:128:7:128:23 | PrivateCopyAssign | Class 'PrivateCopyAssign' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
15+
| test.cpp:141:7:141:23 | PrivateMoveAssign | Class 'PrivateMoveAssign' does not fall into a valid category (isUnmovable, move-only, or copy-enabled). |
16+
| test.cpp:181:7:181:45 | CustomizedCopyCtorDefaultedNonCompliant | Class 'CustomizedCopyCtorDefaultedNonCompliant' has customized copy constructor, but does not customize the destructor. |
17+
| test.cpp:188:7:188:43 | CustomizedCopyCtorDeletedNonCompliant | Class 'CustomizedCopyCtorDeletedNonCompliant' has customized copy constructor, but does not customize the destructor. |
18+
| test.cpp:194:7:194:36 | CustomizedMoveCtorNonCompliant | Class 'CustomizedMoveCtorNonCompliant' has customized move constructor, but does not customize the destructor. |
19+
| test.cpp:200:7:200:38 | CustomizedCopyAssignNonCompliant | Class 'CustomizedCopyAssignNonCompliant' has customized copy assignment operator, but does not customize the destructor. |
20+
| test.cpp:206:7:206:38 | CustomizedMoveAssignNonCompliant | Class 'CustomizedMoveAssignNonCompliant' has customized copy constructor, move assignment operator, move constructor, but does not customize the destructor. |
21+
| test.cpp:213:7:213:39 | MoveOnlyNotCustomizedNonCompliant | Class 'MoveOnlyNotCustomizedNonCompliant' has customized the destructor, but does not customize the move constructor. |
22+
| test.cpp:220:7:220:49 | MoveOnlyAssignableNotCustomizedNonCompliant | Class 'MoveOnlyAssignableNotCustomizedNonCompliant' has customized the destructor, but does not customize the move assignment operator. |
23+
| test.cpp:220:7:220:49 | MoveOnlyAssignableNotCustomizedNonCompliant | Class 'MoveOnlyAssignableNotCustomizedNonCompliant' has customized the destructor, but does not customize the move constructor. |
24+
| test.cpp:251:7:251:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the copy assignment operator. |
25+
| test.cpp:251:7:251:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the copy constructor. |
26+
| test.cpp:251:7:251:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the move assignment operator. |
27+
| test.cpp:251:7:251:43 | CopyEnabledCustomizedDtorNonCompliant | Class 'CopyEnabledCustomizedDtorNonCompliant' has customized the destructor, but does not customize the move constructor. |
28+
| test.cpp:270:7:270:33 | UnmovableBaseNonvirtualDtor | Class 'UnmovableBaseNonvirtualDtor' violates inheritance requirements for special member functions. |
29+
| test.cpp:291:7:291:33 | UnmovablePrivateVirtualDtor | Class 'UnmovablePrivateVirtualDtor' violates inheritance requirements for special member functions. |
30+
| test.cpp:318:7:318:30 | BaseVirtualProtectedDtor | Class 'BaseVirtualProtectedDtor' violates inheritance requirements for special member functions. |
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
No expected results have yet been specified
1+
| test.cpp:276:7:276:32 | UnmovableNonvirtualDerived | Class 'UnmovableNonvirtualDerived' is not analyzable because the destructor and move assignment operator and move constructor are not present in the CodeQL database. |
2+
| test.cpp:288:7:288:39 | UnmovableDerivedPublicVirtualDtor | Class 'UnmovableDerivedPublicVirtualDtor' is not analyzable because the move assignment operator and move constructor are not present in the CodeQL database. |
3+
| test.cpp:302:7:302:40 | UnmovablePrivateVirtualDtorDerived | Class 'UnmovablePrivateVirtualDtorDerived' is not analyzable because the move assignment operator and move constructor are not present in the CodeQL database. |
4+
| test.cpp:316:7:316:26 | ProtectedDtorDerived | Class 'ProtectedDtorDerived' is not analyzable because the destructor and move assignment operator are not present in the CodeQL database. |
5+
| test.cpp:329:7:329:33 | VirtualProtectedDtorDerived | Class 'VirtualProtectedDtorDerived' is not analyzable because the move assignment operator are not present in the CodeQL database. |
6+
| test.cpp:342:8:342:19 | TrivialClass | Class 'TrivialClass' is not analyzable because the destructor and move assignment operator and move constructor are not present in the CodeQL database. |
7+
| test.cpp:348:7:348:21 | NonTrivialClass | Class 'NonTrivialClass' is not analyzable because the move assignment operator and move constructor are not present in the CodeQL database. |
8+
| test.cpp:362:7:362:14 | CopyOnly | Class 'CopyOnly' is not analyzable because the destructor are not present in the CodeQL database. |

cpp/misra/test/rules/RULE-15-0-1/test.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <utility>
2+
13
namespace helpers {
24
void f();
35
}
@@ -329,3 +331,56 @@ class VirtualProtectedDtorDerived : public BaseVirtualProtectedDtor {};
329331
} // namespace additional_requirements
330332

331333
} // namespace fully_specified
334+
335+
namespace audit_results {
336+
337+
struct PodClass { // COMPLIANT - we know PODs are OK.
338+
int x;
339+
int y;
340+
};
341+
342+
struct TrivialClass { // NON_COMPLIANT - audit result
343+
int x;
344+
int y;
345+
COPY_CTOR(TrivialClass) = default;
346+
};
347+
348+
class NonTrivialClass { // NON_COMPLIANT - audit result
349+
int x;
350+
int y;
351+
352+
public:
353+
COPY_CTOR(NonTrivialClass) {
354+
x = 1;
355+
y = 2;
356+
}
357+
~NonTrivialClass() { x = 1; }
358+
};
359+
360+
// This class is not a valid category but hard to analyze in the general case.
361+
// This should not be reported as a violation except by the audit query.
362+
class CopyOnly { // NON_COMPLIANT - audit result
363+
COPY_CTOR(CopyOnly) = default;
364+
MOVE_CTOR(CopyOnly) = delete;
365+
MOVE_ASSIGN(CopyOnly) = delete;
366+
};
367+
368+
// this class should not appear in the audit results, because we have all
369+
// members in the database even though they aren't all explicitly declared
370+
class OdrUsedMoveEnabled { // COMPLIANT
371+
NonTrivialClass x;
372+
373+
public:
374+
// copy operations are generated as deleted
375+
MOVE_CTOR(OdrUsedMoveEnabled) = default;
376+
MOVE_ASSIGN(OdrUsedMoveEnabled) = default;
377+
// destructor is generated as defaulted since it's non trivial and ODR-used.
378+
};
379+
380+
void f(OdrUsedMoveEnabled o) {
381+
// This function ensures the non-trivial destructor is ODR-used.
382+
OdrUsedMoveEnabled o3 = std::move(o);
383+
o3 = std::move(o);
384+
}
385+
386+
} // namespace audit_results

0 commit comments

Comments
 (0)