Skip to content

Commit d268839

Browse files
Merge pull request #946 from github/lcartey/cpp-conversions2
Implement `Conversions2` rule package
2 parents f5f41f5 + 29b2406 commit d268839

File tree

43 files changed

+1251
-44
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1251
-44
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- `M5-2-2` - `PointerToAVirtualBaseClassCastToAPointer.ql`:
2+
- Report casts where the from or to types are typedefs to virtual base classes or derived classes.
3+
- Report casts to a reference type which is a derived type.
4+
- Report casts where the base class is the parent of a virtual base class.
5+
- The alert message has been updated to refer to the virtual base class derivation.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `RULE-1-2`, `RULE-23-3`, `RULE-23-5`, `RULE-23-6`:
2+
- Results that occur in nested macro invocations are now reported in the macro that defines the contravening code, rather than the macro which is first expanded.
3+
- Results the occur in arguments to macro invocations are now reported in at the macro invocation site, instead of the macro definition site.

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/src/rules/M5-2-2/PointerToAVirtualBaseClassCastToAPointer.ql

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
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+
{
22+
PointerToAVirtualBaseClassCastToAPointerQuery() {
23+
this = PointersPackage::pointerToAVirtualBaseClassCastToAPointerQuery()
24+
}
25+
}

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

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

Whitespace-only changes.

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

cpp/common/src/codingstandards/cpp/AlertReporting.qll

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,28 @@ module MacroUnwrapper<ResultType ResultElement> {
1414
* Gets a macro invocation that applies to the result element.
1515
*/
1616
private MacroInvocation getAMacroInvocation(ResultElement re) {
17-
result.getAnExpandedElement() = re
17+
result.getAnAffectedElement() = re
18+
}
19+
20+
private MacroInvocation getASubsumedMacroInvocation(ResultElement re) {
21+
result = getAMacroInvocation(re) and
22+
// Only report cases where the element is not located at the macro expansion site
23+
// This means we'll report results in macro arguments in the macro argument
24+
// location, not within the macro itself.
25+
//
26+
// Do not join start column values.
27+
pragma[only_bind_out](result.getLocation().getStartColumn()) =
28+
pragma[only_bind_out](re.getLocation().getStartColumn())
1829
}
1930

2031
/**
2132
* Gets the primary macro invocation that generated the result element.
33+
*
34+
* Does not hold for cases where the result element is located at a macro argument site.
2235
*/
2336
MacroInvocation getPrimaryMacroInvocation(ResultElement re) {
2437
exists(MacroInvocation mi |
25-
mi = getAMacroInvocation(re) and
38+
mi = getASubsumedMacroInvocation(re) and
2639
// No other more specific macro that expands to element
2740
not exists(MacroInvocation otherMi |
2841
otherMi = getAMacroInvocation(re) and otherMi.getParentInvocation() = mi
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)