Skip to content

Commit ce43bb9

Browse files
committed
M5-2-2: Make into shared query
This shared implementation will be used for implementing MISRA C++ 2023 8.2.1.
1 parent 05cfc2b commit ce43bb9

File tree

7 files changed

+38
-12
lines changed

7 files changed

+38
-12
lines changed

cpp/autosar/src/rules/M5-2-2/PointerToAVirtualBaseClassCastToAPointer.ql

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515

1616
import cpp
1717
import codingstandards.cpp.autosar
18+
import codingstandards.cpp.rules.pointertoavirtualbaseclasscasttoapointer.PointerToAVirtualBaseClassCastToAPointer
1819

19-
from Cast cast, VirtualBaseClass castFrom, Class castTo
20-
where
21-
not isExcluded(cast, PointersPackage::pointerToAVirtualBaseClassCastToAPointerQuery()) and
22-
not cast instanceof DynamicCast and
23-
castFrom = cast.getExpr().getType().(PointerType).getBaseType() and
24-
cast.getType().(PointerType).getBaseType() = castTo and
25-
castTo = castFrom.getADerivedClass+()
26-
select cast,
27-
"A pointer to virtual base class $@ is not cast to a pointer of derived class $@ using a dynamic_cast.",
28-
castFrom, castFrom.getName(), castTo, castTo.getName()
20+
class PointerToAVirtualBaseClassCastToAPointerQuery extends PointerToAVirtualBaseClassCastToAPointerSharedQuery {
21+
PointerToAVirtualBaseClassCastToAPointerQuery() {
22+
this = PointersPackage::pointerToAVirtualBaseClassCastToAPointerQuery()
23+
}
24+
}

cpp/autosar/test/rules/M5-2-2/PointerToAVirtualBaseClassCastToAPointer.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Provides a library with a `problems` predicate for the following issue:
3+
* A pointer to a virtual base class shall only be cast to a pointer to a derived class
4+
* by means of dynamic_cast, otherwise the cast has undefined behavior.
5+
*/
6+
7+
import cpp
8+
import codingstandards.cpp.Customizations
9+
import codingstandards.cpp.Exclusions
10+
11+
abstract class PointerToAVirtualBaseClassCastToAPointerSharedQuery extends Query { }
12+
13+
Query getQuery() { result instanceof PointerToAVirtualBaseClassCastToAPointerSharedQuery }
14+
15+
query predicate problems(Cast cast, string message) {
16+
exists(VirtualBaseClass castFrom, Class castTo |
17+
not isExcluded(cast, getQuery()) and
18+
not cast instanceof DynamicCast and
19+
castFrom = cast.getExpr().getType().(PointerType).getBaseType() and
20+
cast.getType().(PointerType).getBaseType() = castTo and
21+
castTo = castFrom.getADerivedClass+() and
22+
message =
23+
"A pointer to virtual base class " + castFrom.getName() +
24+
" is not cast to a pointer of derived class " + castTo.getName() + " using a dynamic_cast."
25+
)
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.pointertoavirtualbaseclasscasttoapointer.PointerToAVirtualBaseClassCastToAPointer
3+
4+
class TestFileQuery extends PointerToAVirtualBaseClassCastToAPointerSharedQuery, TestQuery { }

rule_packages/cpp/Pointers.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@
306306
"short_name": "PointerToAVirtualBaseClassCastToAPointer",
307307
"tags": [
308308
"correctness"
309-
]
309+
],
310+
"shared_implementation_short_name": "PointerToAVirtualBaseClassCastToAPointer"
310311
}
311312
],
312313
"title": "A pointer to a virtual base class shall only be cast to a pointer to a derived class by means of dynamic_cast."

0 commit comments

Comments
 (0)