Skip to content

Commit 0ff6f35

Browse files
committed
A5-2-2: Refactor query to extract shared components
MISRA C++ Rule 8.2.2 is similar, but not identical.
1 parent afa6c7f commit 0ff6f35

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

cpp/autosar/src/rules/A5-2-2/TraditionalCStyleCastsUsed.ql

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import cpp
1717
import codingstandards.cpp.autosar
1818
import codingstandards.cpp.Macro
19+
import codingstandards.cpp.CStyleCasts
1920

2021
/**
2122
* Gets the macro (if any) that generated the given `CStyleCast`.
@@ -61,18 +62,10 @@ Macro getGeneratedFrom(CStyleCast c) {
6162
* argument type is compatible with a single-argument constructor.
6263
*/
6364

64-
from CStyleCast c, string extraMessage, Locatable l, string supplementary
65+
from ExplicitUserDefinedCStyleCast c, string extraMessage, Locatable l, string supplementary
6566
where
6667
not isExcluded(c, BannedSyntaxPackage::traditionalCStyleCastsUsedQuery()) and
67-
not c.isImplicit() and
68-
not c.getType() instanceof UnknownType and
69-
// For casts in templates that occur on types related to a template parameter, the copy of th
70-
// cast in the uninstantiated template is represented as a `CStyleCast` even if in practice all
71-
// the instantiations represent it as a `ConstructorCall`. To avoid the common false positive case
72-
// of using the functional cast notation to call a constructor we exclude all `CStyleCast`s on
73-
// uninstantiated templates, and instead rely on reporting results within instantiations.
74-
not c.isFromUninstantiatedTemplate(_) and
75-
// Exclude casts created from macro invocations of macros defined by third parties
68+
// Not generated from a library macro
7669
not getGeneratedFrom(c) instanceof LibraryMacro and
7770
// If the cast was generated from a user-provided macro, then report the macro that generated the
7871
// cast, as the macro itself may have generated the cast

cpp/autosar/test/rules/A5-2-2/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <string>
33
#include <utility>
44
int foo() { return 1; }
5-
5+
// A copy of 8.2,2 test.cpp, but with different cases compliant/non-compliant
66
void test_c_style_cast() {
77
double f = 3.14;
88
std::uint32_t n1 = (std::uint32_t)f; // NON_COMPLIANT - C-style cast
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import cpp
2+
import codingstandards.cpp.Macro
3+
4+
/**
5+
* A C-style cast that is explicitly user written and has a known target type.
6+
*/
7+
class ExplicitUserDefinedCStyleCast extends CStyleCast {
8+
ExplicitUserDefinedCStyleCast() {
9+
not this.isImplicit() and
10+
not this.getType() instanceof UnknownType and
11+
// For casts in templates that occur on types related to a template parameter, the copy of th
12+
// cast in the uninstantiated template is represented as a `CStyleCast` even if in practice all
13+
// the instantiations represent it as a `ConstructorCall`. To avoid the common false positive case
14+
// of using the functional cast notation to call a constructor we exclude all `CStyleCast`s on
15+
// uninstantiated templates, and instead rely on reporting results within instantiations.
16+
not this.isFromUninstantiatedTemplate(_)
17+
}
18+
}

0 commit comments

Comments
 (0)