Skip to content

Commit 0d83c9d

Browse files
authored
Merge branch 'main' into mbaluda-22-3-test
2 parents d47f7d1 + 03e0340 commit 0d83c9d

10 files changed

+181
-59
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `A0-1-2`
2+
- Addressed false positives where the return values are cast to `void` in C-style or assigned to `std::ignore`.
3+
- `A0-1-4`
4+
- Addressed false positives where the parameters are marked with attribute `[[maybe_unused]]`, or either cast to `void` in C-style or assigned to `std::ignore` in the function body.

cpp/autosar/src/rules/M14-6-1/NameInDependentBase.qll

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,112 @@ import cpp
22
import codingstandards.cpp.autosar
33

44
/**
5-
* Just the reverse of `Class.getABaseClass()`
5+
* Gets a dependent base type of the given template class.
6+
*
7+
* This returns the `TemplateClass` for the base type, rather than the `ClassTemplateInstantiation`,
8+
* as the instantiation does not appear to include any member declarations.
69
*/
7-
Class getParent(Class child) { child.getABaseClass() = result }
10+
TemplateClass getADependentBaseType(TemplateClass t) {
11+
exists(ClassTemplateInstantiation baseType |
12+
baseType = t.getABaseClass() and
13+
// Base type depends on at least one of the template parameters of class t
14+
baseType.getATemplateArgument() = t.getATemplateArgument() and
15+
// Return the template itself
16+
result = baseType.getTemplate()
17+
)
18+
}
819

920
/**
10-
* There is a `MemberFunction` in parent class with same name
11-
* as a `FunctionCall` that exists in a child `MemberFunction`
21+
* Helper predicate that ensures we do not join on function pairs by name early on, as that creates
22+
* a large dataset on big databases with lots of name duplication.
1223
*/
13-
FunctionCall parentMemberFunctionCall(Class child, Class parent) {
14-
exists(MemberFunction parentFunction, Function other |
15-
not other = parentFunction and
16-
parent.getAMember() = parentFunction and
17-
other.getName() = parentFunction.getName() and
18-
result = other.getACallToThisFunction() and
19-
result.getEnclosingFunction() = child.getAMemberFunction()
24+
pragma[nomagic]
25+
private FunctionCall helper_functioncall(
26+
TemplateClass t, TemplateClass dependentBaseType, Function target, string name
27+
) {
28+
dependentBaseType = getADependentBaseType(t) and
29+
// The target of the call is not declared in the dependent base type
30+
not target.getDeclaringType() = dependentBaseType and
31+
result = target.getACallToThisFunction() and
32+
result.getEnclosingFunction() = t.getAMemberFunction() and
33+
name = target.getName()
34+
}
35+
36+
/**
37+
* Gets a function call in `TemplateClass` `t` where the target function name exists in a dependent
38+
* base type and the call is to a function that is not declared in the dependent base type.
39+
*/
40+
FunctionCall getConfusingFunctionCall(
41+
TemplateClass t, string name, Function target, MemberFunction dependentTypeFunction
42+
) {
43+
exists(TemplateClass dependentBaseType |
44+
result = helper_functioncall(t, dependentBaseType, target, name) and
45+
// The dependentTypeFunction is declared on the dependent base type
46+
dependentBaseType.getAMember() = dependentTypeFunction and
47+
// And has the same name as the target of the function call in the child
48+
name = dependentTypeFunction.getName()
2049
)
2150
}
2251

2352
/**
24-
* There is a `MemberFunction` in parent class with same name
25-
* as a `FunctionAccess` that exists in a child `MemberFunction`
53+
* Helper predicate that ensures we do not join on function pairs by name early on, as that creates
54+
* a large dataset on big databases with lots of name duplication.
55+
*/
56+
pragma[nomagic]
57+
private FunctionAccess helper_functionaccess(
58+
TemplateClass t, TemplateClass dependentBaseType, Function target, string name
59+
) {
60+
dependentBaseType = getADependentBaseType(t) and
61+
// The target of the access is not declared in the dependent base type
62+
not target.getDeclaringType() = dependentBaseType and
63+
result = target.getAnAccess() and
64+
result.getEnclosingFunction() = t.getAMemberFunction() and
65+
name = target.getName()
66+
}
67+
68+
/**
69+
* Gets a function access in `TemplateClass` `t` where the target function name exists in a dependent
70+
* base type and the access is to a function declared outside the dependent base type.
2671
*/
27-
FunctionAccess parentMemberFunctionAccess(Class child, Class parent) {
28-
exists(MemberFunction parentFunction, Function other |
29-
not other = parentFunction and
30-
parent.getAMember() = parentFunction and
31-
other.getName() = parentFunction.getName() and
32-
result = other.getAnAccess() and
33-
result.getEnclosingFunction() = child.getAMemberFunction()
72+
FunctionAccess getConfusingFunctionAccess(
73+
TemplateClass t, string name, Function target, MemberFunction dependentTypeFunction
74+
) {
75+
exists(TemplateClass dependentBaseType |
76+
result = helper_functionaccess(t, dependentBaseType, target, name) and
77+
dependentBaseType.getAMember() = dependentTypeFunction and
78+
name = dependentTypeFunction.getName()
3479
)
3580
}
3681

3782
/**
38-
* There is a `MemberVariable` in parent class with same name
39-
* as a `VariableAccess` that exists in a child `MemberFunction`
83+
* Helper predicate that ensures we do not join on variable pairs by name early on, as that creates
84+
* a large dataset on big databases with lots of name duplication.
85+
*/
86+
pragma[nomagic]
87+
private VariableAccess helper_memberaccess(
88+
TemplateClass t, TemplateClass dependentBaseType, Variable target, string name
89+
) {
90+
dependentBaseType = getADependentBaseType(t) and
91+
// The target of the access is not declared in the dependent base type
92+
not target.getDeclaringType() = dependentBaseType and
93+
result = target.getAnAccess() and
94+
result.getEnclosingFunction() = t.getAMemberFunction() and
95+
name = target.getName() and
96+
// The target is not a local variable, which isn't subject to confusion
97+
not target instanceof LocalScopeVariable
98+
}
99+
100+
/**
101+
* Gets a memmber access in `TemplateClass` `t` where the target member name exists in a dependent
102+
* base type and the access is to a variable declared outside the dependent base type.
40103
*/
41-
Access parentMemberAccess(Class child, Class parent) {
42-
exists(MemberVariable parentMember, Variable other |
43-
not other = parentMember and
44-
parent.getAMemberVariable() = parentMember and
45-
other.getName() = parentMember.getName() and
46-
result = other.getAnAccess() and
47-
result.getEnclosingFunction() = child.getAMemberFunction()
104+
VariableAccess getConfusingMemberVariableAccess(
105+
TemplateClass t, string name, Variable target, MemberVariable dependentTypeMemberVariable
106+
) {
107+
exists(TemplateClass dependentBaseType |
108+
result = helper_memberaccess(t, dependentBaseType, target, name) and
109+
dependentBaseType.getAMemberVariable() = dependentTypeMemberVariable and
110+
name = dependentTypeMemberVariable.getName()
48111
)
49112
}
50113

cpp/autosar/src/rules/M14-6-1/NameNotReferredUsingAQualifiedIdOrThis.ql

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,25 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import NameInDependentBase
2020

21-
from Class c, Class p, NameQualifiableElement fn
21+
from
22+
TemplateClass c, NameQualifiableElement fn, string targetName, Element actualTarget,
23+
Element dependentTypeMemberWithSameName
2224
where
2325
not isExcluded(fn, TemplatesPackage::nameNotReferredUsingAQualifiedIdOrThisQuery()) and
2426
not isCustomExcluded(fn) and
25-
p = getParent(c) and
2627
missingNameQualifier(fn) and
2728
(
28-
fn instanceof FunctionAccess and
29-
fn = parentMemberFunctionAccess(c, p)
29+
fn = getConfusingFunctionAccess(c, targetName, actualTarget, dependentTypeMemberWithSameName)
3030
or
31-
fn instanceof FunctionCall and
32-
fn = parentMemberFunctionCall(c, p) and
31+
fn = getConfusingFunctionCall(c, targetName, actualTarget, dependentTypeMemberWithSameName) and
3332
not exists(Expr e | e = fn.(FunctionCall).getQualifier())
3433
or
35-
fn instanceof VariableAccess and
36-
not fn.(VariableAccess).getTarget() instanceof Parameter and
37-
fn = parentMemberAccess(c, p) and
34+
fn =
35+
getConfusingMemberVariableAccess(c, targetName, actualTarget, dependentTypeMemberWithSameName) and
3836
not exists(Expr e | e = fn.(VariableAccess).getQualifier())
3937
) and
4038
not fn.isAffectedByMacro()
41-
select fn, "Use of identifier that also exists in a base class that is not fully qualified."
39+
select fn,
40+
"Use of unqualified identifier " + targetName +
41+
" targets $@ but a member with the name also exists $@.", actualTarget, targetName,
42+
dependentTypeMemberWithSameName, "in the dependent base class"

cpp/autosar/src/rules/M14-6-1/NameNotReferredUsingAQualifiedIdOrThisAudit.ql

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import NameInDependentBase
2020

21-
from Class c, Class p, NameQualifiableElement fn
21+
from
22+
TemplateClass c, NameQualifiableElement fn, string targetName, Element actualTarget,
23+
Element dependentTypeMemberWithSameName
2224
where
2325
not isExcluded(fn, TemplatesPackage::nameNotReferredUsingAQualifiedIdOrThisAuditQuery()) and
2426
not isCustomExcluded(fn) and
25-
p = getParent(c) and
2627
missingNameQualifier(fn) and
2728
(
28-
fn instanceof FunctionAccess and
29-
fn = parentMemberFunctionAccess(c, p)
29+
fn = getConfusingFunctionAccess(c, targetName, actualTarget, dependentTypeMemberWithSameName)
3030
or
31-
fn instanceof FunctionCall and
32-
fn = parentMemberFunctionCall(c, p) and
31+
fn = getConfusingFunctionCall(c, targetName, actualTarget, dependentTypeMemberWithSameName) and
3332
not exists(Expr e | e = fn.(FunctionCall).getQualifier())
3433
or
35-
fn instanceof VariableAccess and
3634
not fn.(VariableAccess).getTarget() instanceof Parameter and
37-
fn = parentMemberAccess(c, p) and
35+
fn =
36+
getConfusingMemberVariableAccess(c, targetName, actualTarget, dependentTypeMemberWithSameName) and
3837
not exists(Expr e | e = fn.(VariableAccess).getQualifier())
3938
)
40-
select fn, "Use of identifier that also exists in a base class that is not fully qualified."
39+
select fn,
40+
"Use of unqualified identifier " + targetName +
41+
" targets $@ but a member with the name also exists $@.", actualTarget, targetName,
42+
dependentTypeMemberWithSameName, "in the dependent base class"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| test.cpp:16:5:16:5 | m | Use of identifier that also exists in a base class that is not fully qualified. |
2-
| test.cpp:17:5:17:5 | call to g | Use of identifier that also exists in a base class that is not fully qualified. |
3-
| test.cpp:19:20:19:20 | g | Use of identifier that also exists in a base class that is not fully qualified. |
1+
| test.cpp:16:5:16:5 | m | Use of unqualified identifier m targets $@ but a member with the name also exists $@. | test.cpp:4:5:4:5 | m | m | test.cpp:10:7:10:7 | m | in the dependent base class |
2+
| test.cpp:17:5:17:5 | call to g | Use of unqualified identifier g targets $@ but a member with the name also exists $@. | test.cpp:2:6:2:6 | g | g | test.cpp:9:8:9:8 | g | in the dependent base class |
3+
| test.cpp:19:20:19:20 | g | Use of unqualified identifier g targets $@ but a member with the name also exists $@. | test.cpp:2:6:2:6 | g | g | test.cpp:9:8:9:8 | g | in the dependent base class |
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| test.cpp:16:5:16:5 | m | Use of identifier that also exists in a base class that is not fully qualified. |
2-
| test.cpp:17:5:17:5 | call to g | Use of identifier that also exists in a base class that is not fully qualified. |
3-
| test.cpp:19:20:19:20 | g | Use of identifier that also exists in a base class that is not fully qualified. |
1+
| test.cpp:16:5:16:5 | m | Use of unqualified identifier m targets $@ but a member with the name also exists $@. | test.cpp:4:5:4:5 | m | m | test.cpp:10:7:10:7 | m | in the dependent base class |
2+
| test.cpp:17:5:17:5 | call to g | Use of unqualified identifier g targets $@ but a member with the name also exists $@. | test.cpp:2:6:2:6 | g | g | test.cpp:9:8:9:8 | g | in the dependent base class |
3+
| test.cpp:19:20:19:20 | g | Use of unqualified identifier g targets $@ but a member with the name also exists $@. | test.cpp:2:6:2:6 | g | g | test.cpp:9:8:9:8 | g | in the dependent base class |

cpp/autosar/test/rules/M14-6-1/test.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,59 @@ template <typename T> class A : B<T> {
2929
typename B<T>::TYPE t2 = 0; // COMPLIANT
3030
g1(); // COMPLIANT, identifier not found in B
3131
}
32+
void m3(int m) {
33+
m = 0; // COMPLIANT, hides member
34+
}
35+
void m4() {
36+
int m = 0;
37+
m = 0; // COMPLIANT, hides member
38+
}
3239
};
3340

3441
void f() {
3542
A<int> a;
3643
a.m1();
3744
a.m2();
45+
a.m3(1);
46+
a.m4();
47+
}
48+
49+
class D {
50+
public:
51+
typedef int TYPE;
52+
void g();
53+
void g(int x);
54+
static void sg();
55+
static void sg(int x);
56+
int m;
57+
};
58+
59+
class C : D {
60+
public:
61+
void m1() {
62+
m = 0; // COMPLIANT - does not apply to non-class templates
63+
g(); // COMPLIANT - does not apply to non-class templates
64+
sg(); // COMPLIANT - does not apply to non-class templates
65+
TYPE t1 = 0; // COMPLIANT - does not apply to non-class templates
66+
// void (*p)() = &g; // NON_COMPILABLE - not valid to take address of member
67+
// function without qualifier
68+
}
69+
};
70+
71+
template <typename T> class E : D {
72+
public:
73+
void m1() {
74+
m = 0; // COMPLIANT - does not apply to non dependent base types
75+
g(); // COMPLIANT - does not apply to non dependent base types
76+
TYPE t1 = 0; // COMPLIANT - does not apply to non dependent base types
77+
// void (*p)() = &g; // NON_COMPILABLE - not valid to take address of member
78+
// function without qualifier
79+
}
80+
};
81+
82+
void f2() {
83+
C c;
84+
c.m1();
85+
E<int> e;
86+
e.m1();
3887
}

scripts/matrix_testing/Config.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ $COMPILER_ARGS = @{
2121
};
2222

2323
"c" = @{
24-
"gcc" = "-fsyntax-only";
25-
"clang" = "-fsyntax-only";
24+
"gcc" = "-fsyntax-only -std=c11";
25+
"clang" = "-fsyntax-only -std=c11";
2626
};
2727

2828
}

scripts/matrix_testing/CreateMatrixTestReport.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ $jobRows = $queriesToCheck | ForEach-Object -ThrottleLimit $NumThreads -Parallel
321321
###########################################################
322322
# Push context
323323
###########################################################
324-
$fileSet = (Get-CompilerSpecificFiles -Configuration $using:Configuration -Language $using:Language -TestDirectory $testDirectory)
324+
$fileSet = (Get-CompilerSpecificFiles -Configuration $using:Configuration -Language $using:Language -TestDirectory $testDirectory -Query $CurrentQueryName)
325325

326326
if($fileSet){
327327
$context = Push-CompilerSpecificFiles -Configuration $using:Configuration -Language $using:Language -FileSet $fileSet

scripts/matrix_testing/Get-CompilerSpecificFiles.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ function Get-CompilerSpecificFiles {
99
$Language,
1010
[Parameter(Mandatory)]
1111
[string]
12-
$TestDirectory
12+
$TestDirectory,
13+
[Parameter(Mandatory)]
14+
[string]
15+
$Query
1316
)
1417
#
1518
# Convention is as follows:
@@ -28,7 +31,7 @@ function Get-CompilerSpecificFiles {
2831
$f
2932
}
3033

31-
foreach($f in (Get-ChildItem -Filter "*.expected.$Configuration" $TestDirectory)){
34+
foreach($f in (Get-ChildItem -Filter "$Query.expected.$Configuration" $TestDirectory)){
3235
Write-Host "Found file $f..."
3336
$f
3437
}

0 commit comments

Comments
 (0)