Skip to content

Commit d9da42b

Browse files
authored
Merge branch 'main' into knewbury01/Declarations3
2 parents 68304ab + f793d14 commit d9da42b

13 files changed

+93
-18
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M0-1-9` - `DeadCode.ql`:
2+
- More compiler generated statements are now excluded from being reported as dead code, including compiler generated statements for `static_assert` calls.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A2-10-5` - `IdentifierNameOfANonMemberObjectWithExternalOrInternalLinkageIsReused.ql`
2+
- Reduce false positives by excluding variable template instantiations.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `M3-2-1` - `DeclarationsOfAnObjectShallHaveCompatibleTypes.ql`
2+
- Reduced false positives by excluding non-object variables (for example, member variables).
3+
- Reduced false positives by excluding variable templates and template instantiations.
4+
- Improved the reported error message by including the conflicting type names.

cpp/autosar/src/rules/A2-10-5/IdentifierNameOfANonMemberObjectWithExternalOrInternalLinkageIsReused.ql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ where
3232
not isExcluded(o2,
3333
NamingPackage::identifierNameOfANonMemberObjectWithExternalOrInternalLinkageIsReusedQuery()) and
3434
not o1 = o2 and
35-
o1.getName() = o2.getName()
35+
o1.getName() = o2.getName() and
36+
// Only consider variables from uninstantiated templates, to avoid false positives where o1 and
37+
// o2 are the same object across different template instantiations
38+
not o1.isFromTemplateInstantiation(_) and
39+
not o2.isFromTemplateInstantiation(_)
3640
select o2,
3741
"Identifier name of non-member object $@ reuses the identifier name of non-member object $@.", o2,
3842
o2.getName(), o1, o1.getName()

cpp/autosar/src/rules/M0-1-9/DeadCode.ql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ predicate isDeadStmt(Stmt s) {
5151
// - The initializers for each of the variables are pure
5252
exists(DeclStmt ds |
5353
ds = s and
54-
forall(Declaration d | d = ds.getADeclaration() |
54+
// Use forex so that we don't flag "fake" generated `DeclStmt`s (e.g. those generated by the
55+
// extractor for static_asserts) with no actual declarations
56+
forex(Declaration d | d = ds.getADeclaration() |
5557
exists(LocalScopeVariable v |
5658
d = v and
5759
v.getInitializer().getExpr().isPure() and
@@ -123,5 +125,7 @@ where
123125
// output". We therefore exclude unreachable statements as they are, by definition, not executed.
124126
not s.getBasicBlock() = any(UnreachableBasicBlock ubb).getABasicBlock() and
125127
// Exclude code generated by macros, because the code may be "live" in other instantiations
126-
not s.isAffectedByMacro()
128+
not s.isAffectedByMacro() and
129+
// Exclude compiler generated statements
130+
not s.isCompilerGenerated()
127131
select s, "This statement is dead code."

cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@ import cpp
2020
import codingstandards.cpp.autosar
2121
import codingstandards.cpp.Typehelpers
2222

23+
predicate isNonTemplateObjectVariable(GlobalOrNamespaceVariable gv) {
24+
not gv.isFromTemplateInstantiation(_) and
25+
not gv.isFromUninstantiatedTemplate(_)
26+
}
27+
2328
from VariableDeclarationEntry decl1, VariableDeclarationEntry decl2
2429
where
2530
not isExcluded(decl1, DeclarationsPackage::declarationsOfAnObjectShallHaveCompatibleTypesQuery()) and
2631
not isExcluded(decl2, DeclarationsPackage::declarationsOfAnObjectShallHaveCompatibleTypesQuery()) and
2732
not areCompatible(decl1.getType(), decl2.getType()) and
2833
// Note that normally `VariableDeclarationEntry` includes parameters, which are not covered
2934
// by this query. We implicitly exclude them with the `getQualifiedName()` predicate.
30-
decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName()
31-
select decl1, "The object $@ is not compatible with re-declaration $@", decl1, decl1.getName(),
32-
decl2, decl2.getName()
35+
decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName() and
36+
// Only consider global/namespace variables which aren't templated
37+
isNonTemplateObjectVariable(decl1.getVariable()) and
38+
isNonTemplateObjectVariable(decl2.getVariable())
39+
select decl1,
40+
"The object $@ of type " + decl1.getType().toString() +
41+
" is not compatible with re-declaration $@ of type " + decl2.getType().toString(), decl1,
42+
decl1.getName(), decl2, decl2.getName()
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| test1a.cpp:6:12:6:13 | g3 | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1a.cpp:6:12:6:13 | g3 | g3 | test1b.cpp:7:12:7:13 | g3 | g3 |
2+
| test1a.cpp:17:43:17:43 | number_two | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1a.cpp:17:43:17:43 | number_two | number_two | test1b.cpp:12:43:12:43 | number_two | number_two |
23
| test1b.cpp:7:12:7:13 | g3 | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1b.cpp:7:12:7:13 | g3 | g3 | test1a.cpp:6:12:6:13 | g3 | g3 |
4+
| test1b.cpp:12:43:12:43 | number_two | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1b.cpp:12:43:12:43 | number_two | number_two | test1a.cpp:17:43:17:43 | number_two | number_two |

cpp/autosar/test/rules/A2-10-5/test1a.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ static int g1 = 0;
55
static int g2; // COMPLIANT
66
static int g3 = 1; // NON_COMPLIANT
77
static void f1(){}; // NON_COMPLIANT
8+
9+
// Variable template has multiple declarations: one for the uninstantiated
10+
// template and one for each instantiation
11+
template <class T> constexpr T number_one = T(1); // COMPLIANT
12+
13+
int test() { return number_one<int>; }
14+
15+
long test2() { return number_one<long>; }
16+
17+
template <class T> constexpr T number_two = T(1); // NON_COMPLIANT
18+
19+
int test3() { return number_two<int>; }
20+
21+
long test4() { return number_two<long>; }

cpp/autosar/test/rules/A2-10-5/test1b.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ static int g3 = 0; // NON_COMPLIANT
88
}
99

1010
static void f1() {} // NON_COMPLIANT
11+
12+
template <class T> constexpr T number_two = T(1); // NON_COMPLIANT
13+
14+
int test3() { return number_two<int>; }
15+
16+
long test4() { return number_two<long>; }

cpp/autosar/test/rules/M0-1-9/test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ int test_dead_code(int x) {
7676
} catch (...) { // NON_COMPLIANT
7777
}
7878

79+
static_assert(1); // COMPLIANT
80+
7981
return live5 + live6; // COMPLIANT
8082
}

0 commit comments

Comments
 (0)