Skip to content

Commit bfba947

Browse files
committed
Clean up code
When it comes to declaration, treat everything as non-compliant, including the ones that are replaceable allocation / non-allocation functions.
1 parent e67b7af commit bfba947

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

cpp/misra/src/rules/RULE-21-6-3/AdvancedMemoryManagementUsed.ql

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,79 @@ import codingstandards.cpp.misra
1818
import codingstandards.cpp.UnintializedMemoryAllocation
1919
import codingstandards.cpp.allocations.CustomOperatorNewDelete
2020

21+
class AdvancedMemoryManagementFunction extends Function {
22+
string description;
23+
24+
AdvancedMemoryManagementFunction() {
25+
this instanceof NonStandardNewOrNewArrayOperator and
26+
description = "a non-replaceable allocation function as operator `new` / `new[]`"
27+
or
28+
this instanceof NonStandardDeleteOrDeleteArrayOperator and
29+
description = "a non-replaceable deallocation function as operator `delete` / `delete[]`"
30+
or
31+
this instanceof UninitializedMemoryManagementFunction and
32+
description = "a function from <memory> that manages uninitialized memory"
33+
}
34+
35+
string describe() { result = description }
36+
}
37+
2138
class NonStandardNewOrNewArrayOperator extends CustomOperatorNewOrDelete {
2239
NonStandardNewOrNewArrayOperator() {
2340
this.getName() in ["operator new", "operator new[]"] and
2441
not this instanceof CustomOperatorNew // `CustomOperatorNew` only detects replaceable allocation functions.
2542
}
2643
}
2744

45+
/**
46+
* A user-provided declaration of `new` / `new[]` / `delete` / `delete[]`.
47+
*
48+
* NOTE: Technically, the rule does not care if the declarations are in user-provided code,
49+
* but for the sake of development, we want to exclude the stubs we index into the database.
50+
*/
51+
class UserDeclaredOperatorNewOrDelete extends FunctionDeclarationEntry {
52+
UserDeclaredOperatorNewOrDelete() {
53+
/* Not in the standard library */
54+
exists(this.getFile().getRelativePath()) and
55+
/* Not in a file called `new`, which is likely to be a stub of the standard library */
56+
not this.getFile().getBaseName() = "new" and
57+
(
58+
this.getName().regexpMatch("operator new(\\[\\])?") or
59+
this.getName().regexpMatch("operator delete(\\[\\])?")
60+
)
61+
}
62+
}
63+
2864
class NonStandardDeleteOrDeleteArrayOperator extends CustomOperatorNewOrDelete {
2965
NonStandardDeleteOrDeleteArrayOperator() {
3066
this.getName() in ["operator delete", "operator delete[]"] and
3167
not this instanceof CustomOperatorDelete // `CustomOperatorDelete` only detects replaceable deallocation functions.
3268
}
3369
}
3470

35-
from Element element
71+
from Element element, string message
3672
where
3773
not isExcluded(element, Memory6Package::advancedMemoryManagementUsedQuery()) and
74+
exists(AdvancedMemoryManagementFunction advancedMemoryManagementFunction |
75+
/* 1. The element is a call to one of the advanced management functions. */
76+
element = advancedMemoryManagementFunction.getACallToThisFunction() and
77+
message =
78+
"This expression is a call to `" + advancedMemoryManagementFunction.getName() + "` which is " +
79+
advancedMemoryManagementFunction.describe() + "."
80+
or
81+
/* 2. The element takes address of the advanced memory management functions. */
82+
element = advancedMemoryManagementFunction.getAnAccess() and
83+
message =
84+
"This expression takes address of `" + advancedMemoryManagementFunction.getName() +
85+
"` which is " + advancedMemoryManagementFunction.describe() + "."
86+
)
87+
or
3888
(
39-
/* The element is a call to one of the function at <memory> that manages uninitialized memory. */
40-
element.(FunctionCall).getTarget() instanceof UninitializedMemoryManagementFunction or
41-
/* The element is an explicit call to a destructor. */
4289
element instanceof VacuousDestructorCall or
43-
element instanceof DestructorCall or
44-
/* The element is a declaration or a definition of operator `new` / `new[]` / `delete` / `delete[]`. */
45-
element instanceof NonStandardNewOrNewArrayOperator or
46-
element instanceof NonStandardDeleteOrDeleteArrayOperator
47-
)
48-
select element, "TODO"
90+
element instanceof DestructorCall
91+
) and
92+
message = "This expression is a call to a destructor."
93+
or
94+
element instanceof UserDeclaredOperatorNewOrDelete and
95+
message = "This is a user-provided declaration of `new` / `new[]` / `delete` / `delete[]`."
96+
select element, message

0 commit comments

Comments
 (0)