Skip to content

Commit be6d8a7

Browse files
authored
Merge pull request #65 from lcartey/lcartey/m3-2-1-variable-templates
`M3-2-1`: Exclude non-object and template variables
2 parents 263edcb + 8087408 commit be6d8a7

5 files changed

+54
-15
lines changed
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/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()
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:4:6:4:7 | definition of a4 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:4:6:4:7 | definition of a4 | a4 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:11:12:11:13 | declaration of a4 | a4 |
2-
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:5:5:5:6 | definition of a5 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:5:5:5:6 | definition of a5 | a5 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:13:13:13:14 | declaration of a5 | a5 |
3-
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:6:6:6:7 | definition of a6 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:6:6:6:7 | definition of a6 | a6 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:17:1:17:3 | declaration of a6 | a6 |
4-
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:7:5:7:6 | definition of a7 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:7:5:7:6 | definition of a7 | a7 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:19:11:19:12 | declaration of a7 | a7 |
5-
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:14:5:14:6 | definition of a2 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:14:5:14:6 | definition of a2 | a2 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:23:13:23:14 | declaration of a2 | a2 |
6-
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:19:5:19:7 | definition of a11 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:19:5:19:7 | definition of a11 | a11 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:28:12:28:14 | declaration of a11 | a11 |
7-
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:11:12:11:13 | declaration of a4 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:11:12:11:13 | declaration of a4 | a4 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:4:6:4:7 | definition of a4 | a4 |
8-
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:13:13:13:14 | declaration of a5 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:13:13:13:14 | declaration of a5 | a5 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:5:5:5:6 | definition of a5 | a5 |
9-
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:17:1:17:3 | declaration of a6 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:17:1:17:3 | declaration of a6 | a6 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:6:6:6:7 | definition of a6 | a6 |
10-
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:19:11:19:12 | declaration of a7 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:19:11:19:12 | declaration of a7 | a7 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:7:5:7:6 | definition of a7 | a7 |
11-
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:23:13:23:14 | declaration of a2 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:23:13:23:14 | declaration of a2 | a2 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:14:5:14:6 | definition of a2 | a2 |
12-
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:28:12:28:14 | declaration of a11 | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:28:12:28:14 | declaration of a11 | a11 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:19:5:19:7 | definition of a11 | a11 |
1+
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:4:6:4:7 | definition of a4 | The object $@ of type long is not compatible with re-declaration $@ of type int | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:4:6:4:7 | definition of a4 | a4 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:11:12:11:13 | declaration of a4 | a4 |
2+
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:5:5:5:6 | definition of a5 | The object $@ of type int is not compatible with re-declaration $@ of type long | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:5:5:5:6 | definition of a5 | a5 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:13:13:13:14 | declaration of a5 | a5 |
3+
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:6:6:6:7 | definition of a6 | The object $@ of type long is not compatible with re-declaration $@ of type int | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:6:6:6:7 | definition of a6 | a6 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:17:1:17:3 | declaration of a6 | a6 |
4+
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:7:5:7:6 | definition of a7 | The object $@ of type int is not compatible with re-declaration $@ of type LL | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:7:5:7:6 | definition of a7 | a7 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:19:11:19:12 | declaration of a7 | a7 |
5+
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:14:5:14:6 | definition of a2 | The object $@ of type int is not compatible with re-declaration $@ of type long | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:14:5:14:6 | definition of a2 | a2 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:23:13:23:14 | declaration of a2 | a2 |
6+
| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:19:5:19:7 | definition of a11 | The object $@ of type int[100] is not compatible with re-declaration $@ of type int[101] | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:19:5:19:7 | definition of a11 | a11 | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:28:12:28:14 | declaration of a11 | a11 |
7+
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:11:12:11:13 | declaration of a4 | The object $@ of type int is not compatible with re-declaration $@ of type long | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:11:12:11:13 | declaration of a4 | a4 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:4:6:4:7 | definition of a4 | a4 |
8+
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:13:13:13:14 | declaration of a5 | The object $@ of type long is not compatible with re-declaration $@ of type int | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:13:13:13:14 | declaration of a5 | a5 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:5:5:5:6 | definition of a5 | a5 |
9+
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:17:1:17:3 | declaration of a6 | The object $@ of type int is not compatible with re-declaration $@ of type long | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:17:1:17:3 | declaration of a6 | a6 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:6:6:6:7 | definition of a6 | a6 |
10+
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:19:11:19:12 | declaration of a7 | The object $@ of type LL is not compatible with re-declaration $@ of type int | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:19:11:19:12 | declaration of a7 | a7 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:7:5:7:6 | definition of a7 | a7 |
11+
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:23:13:23:14 | declaration of a2 | The object $@ of type long is not compatible with re-declaration $@ of type int | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:23:13:23:14 | declaration of a2 | a2 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:14:5:14:6 | definition of a2 | a2 |
12+
| test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:28:12:28:14 | declaration of a11 | The object $@ of type int[101] is not compatible with re-declaration $@ of type int[100] | test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp:28:12:28:14 | declaration of a11 | a11 | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:19:5:19:7 | definition of a11 | a11 |

cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,19 @@ int a2; // NON_COMPLIANT
1717
int a9[100]; // COMPLIANT
1818
int a10[100]; // COMPLIANT
1919
int a11[100]; // NON_COMPLIANT - different sizes
20+
21+
// Variable templates can cause false positives
22+
template <class T> constexpr T number_one = T(1); // COMPLIANT
23+
24+
int test() { return number_one<int>; }
25+
26+
long test2() { return number_one<long>; }
27+
28+
template <class T> class ClassB {
29+
private:
30+
T mA; // Should be ignored, as not an object
31+
double mB; // Should be ignored, as not an object
32+
};
33+
34+
void test3() { ClassB<int> b; }
35+
void test4() { ClassB<long> b; }

cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ extern long a2; // NON_COMPLIANT
2626
extern int a9[100]; // COMPLIANT
2727
LI a10[100]; // COMPLIANT
2828
extern int a11[101]; // NON_COMPLIANT - different sizes
29+
30+
template <class T> class ClassB {
31+
private:
32+
T mA; // Should be ignored, as not an object
33+
int mB; // Should be ignored, as not an object
34+
};
35+
36+
void testb_1() { ClassB<int> b; }
37+
void testb_2() { ClassB<long> b; }

0 commit comments

Comments
 (0)