Skip to content

Commit 7b6cc90

Browse files
committed
M14-6-1: Restrict to templates with dependent base
The query didn't explicitly enforce this condition, and instead relied on the fact that NameQualifiableElement's without any qualifier would only point to the "wrong" element within templates. However, there are some edge cases (static member functions with overrides) where non-class templates could be flagged. This commit restricts the output to the set of template classes with dependent base types, therefore avoiding the sort of false positives seen above. In addition, this improves performance for this query because the set of template classes with dependent base types is much much much smaller than the overall set of base types.
1 parent fbc9cee commit 7b6cc90

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ 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
/**
1021
* There is a `MemberFunction` in parent class with same name

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

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

21-
from Class c, Class p, NameQualifiableElement fn
21+
from TemplateClass c, TemplateClass dependentBaseType, NameQualifiableElement fn
2222
where
2323
not isExcluded(fn, TemplatesPackage::nameNotReferredUsingAQualifiedIdOrThisQuery()) and
2424
not isCustomExcluded(fn) and
25-
p = getParent(c) and
25+
dependentBaseType = getADependentBaseType(c) and
2626
missingNameQualifier(fn) and
2727
(
2828
fn instanceof FunctionAccess and
29-
fn = parentMemberFunctionAccess(c, p)
29+
fn = parentMemberFunctionAccess(c, dependentBaseType)
3030
or
3131
fn instanceof FunctionCall and
32-
fn = parentMemberFunctionCall(c, p) and
32+
fn = parentMemberFunctionCall(c, dependentBaseType) and
3333
not exists(Expr e | e = fn.(FunctionCall).getQualifier())
3434
or
3535
fn instanceof VariableAccess and
3636
not fn.(VariableAccess).getTarget() instanceof Parameter and
37-
fn = parentMemberAccess(c, p) and
37+
fn = parentMemberAccess(c, dependentBaseType) and
3838
not exists(Expr e | e = fn.(VariableAccess).getQualifier())
3939
) and
4040
not fn.isAffectedByMacro()

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

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

21-
from Class c, Class p, NameQualifiableElement fn
21+
from TemplateClass c, TemplateClass dependentBaseType, NameQualifiableElement fn
2222
where
2323
not isExcluded(fn, TemplatesPackage::nameNotReferredUsingAQualifiedIdOrThisAuditQuery()) and
2424
not isCustomExcluded(fn) and
25-
p = getParent(c) and
25+
dependentBaseType = getADependentBaseType(c) and
2626
missingNameQualifier(fn) and
2727
(
2828
fn instanceof FunctionAccess and
29-
fn = parentMemberFunctionAccess(c, p)
29+
fn = parentMemberFunctionAccess(c, dependentBaseType)
3030
or
3131
fn instanceof FunctionCall and
32-
fn = parentMemberFunctionCall(c, p) and
32+
fn = parentMemberFunctionCall(c, dependentBaseType) and
3333
not exists(Expr e | e = fn.(FunctionCall).getQualifier())
3434
or
3535
fn instanceof VariableAccess and
3636
not fn.(VariableAccess).getTarget() instanceof Parameter and
37-
fn = parentMemberAccess(c, p) and
37+
fn = parentMemberAccess(c, dependentBaseType) and
3838
not exists(Expr e | e = fn.(VariableAccess).getQualifier())
3939
)
4040
select fn, "Use of identifier that also exists in a base class that is not fully qualified."

0 commit comments

Comments
 (0)