From 79fc3a9f1665a1c8df209ca4e1462af12572368c Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 11:15:59 +0100 Subject: [PATCH 1/6] M3-2-1: Add failing tests cases for variables templates Variable templates cause multiple variables to be generated, causing false positives for M3-2-1. --- ...DeclarationsOfAnObjectShallHaveCompatibleTypes.expected | 6 ++++++ ...ions_of_an_object_shall_have_compatible_types_unit1.cpp | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected b/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected index 8aed8c0473..0faba23f69 100644 --- a/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected +++ b/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected @@ -4,6 +4,12 @@ | 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 | | 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 | | 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 | +| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | +| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | +| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | +| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | +| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | +| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | | 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 | | 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 | | 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 | diff --git a/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp b/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp index 33482c62a2..008f3b41f2 100644 --- a/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp +++ b/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp @@ -17,3 +17,10 @@ int a2; // NON_COMPLIANT int a9[100]; // COMPLIANT int a10[100]; // COMPLIANT int a11[100]; // NON_COMPLIANT - different sizes + +// Variable templates can cause false positives +template constexpr T number_one = T(1); // COMPLIANT + +int test() { return number_one; } + +long test2() { return number_one; } \ No newline at end of file From 6aa9c9084a3ac847f8f9febd29a280ef4215d583 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 11:21:27 +0100 Subject: [PATCH 2/6] M3-2-1: Exclude TemplateVariable decls and decls in instantiations TemplateVariables and variables in template instantiations are excluded because they cause spurious results for this query. --- ...DeclarationsOfAnObjectShallHaveCompatibleTypes.ql | 12 +++++++++++- ...ationsOfAnObjectShallHaveCompatibleTypes.expected | 6 ------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql b/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql index cc6a921a6b..be73f018c1 100644 --- a/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql +++ b/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql @@ -27,6 +27,16 @@ where not areCompatible(decl1.getType(), decl2.getType()) and // Note that normally `VariableDeclarationEntry` includes parameters, which are not covered // by this query. We implicitly exclude them with the `getQualifiedName()` predicate. - decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName() + decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName() and + // The underlying Variable shouldn't be from a TemplateVariable or a template instantiation + not exists(Variable v | + v = decl1.getVariable() + or + v = decl2.getVariable() + | + v instanceof TemplateVariable + or + v.isFromTemplateInstantiation(_) + ) select decl1, "The object $@ is not compatible with re-declaration $@", decl1, decl1.getName(), decl2, decl2.getName() diff --git a/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected b/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected index 0faba23f69..8aed8c0473 100644 --- a/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected +++ b/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected @@ -4,12 +4,6 @@ | 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 | | 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 | | 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 | -| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | -| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | -| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | -| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | -| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | -| test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | The object $@ is not compatible with re-declaration $@ | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:43:22:43 | definition of number_one | number_one | test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp:22:32:22:32 | definition of number_one | number_one | | 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 | | 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 | | 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 | From cda96065400698aff9544713e9912abb1fd3d1ca Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 11:25:38 +0100 Subject: [PATCH 3/6] M3-2-1: Improve message to include types Add the types of the two declarations to help clarify differences. --- ...tionsOfAnObjectShallHaveCompatibleTypes.ql | 6 +++-- ...fAnObjectShallHaveCompatibleTypes.expected | 24 +++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql b/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql index be73f018c1..785509611b 100644 --- a/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql +++ b/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql @@ -38,5 +38,7 @@ where or v.isFromTemplateInstantiation(_) ) -select decl1, "The object $@ is not compatible with re-declaration $@", decl1, decl1.getName(), - decl2, decl2.getName() +select decl1, + "The object $@ of type " + decl1.getType().toString() + + " is not compatible with re-declaration $@ of type " + decl2.getType().toString(), decl1, + decl1.getName(), decl2, decl2.getName() diff --git a/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected b/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected index 8aed8c0473..33f6dec68f 100644 --- a/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected +++ b/cpp/autosar/test/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.expected @@ -1,12 +1,12 @@ -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | -| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | From 53c7483eff3a481b1b9aafbf0c1884b4dd4c06d3 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 11:40:32 +0100 Subject: [PATCH 4/6] M3-2-1: Add failing test case for member variables --- ...of_an_object_shall_have_compatible_types_unit1.cpp | 11 ++++++++++- ...of_an_object_shall_have_compatible_types_unit2.cpp | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp b/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp index 008f3b41f2..ad774c5995 100644 --- a/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp +++ b/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit1.cpp @@ -23,4 +23,13 @@ template constexpr T number_one = T(1); // COMPLIANT int test() { return number_one; } -long test2() { return number_one; } \ No newline at end of file +long test2() { return number_one; } + +template class ClassB { +private: + T mA; // Should be ignored, as not an object + double mB; // Should be ignored, as not an object +}; + +void test3() { ClassB b; } +void test4() { ClassB b; } \ No newline at end of file diff --git a/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp b/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp index 0a2fe7a472..58aefe0281 100644 --- a/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp +++ b/cpp/autosar/test/rules/M3-2-1/test_declarations_of_an_object_shall_have_compatible_types_unit2.cpp @@ -26,3 +26,12 @@ extern long a2; // NON_COMPLIANT extern int a9[100]; // COMPLIANT LI a10[100]; // COMPLIANT extern int a11[101]; // NON_COMPLIANT - different sizes + +template class ClassB { +private: + T mA; // Should be ignored, as not an object + int mB; // Should be ignored, as not an object +}; + +void testb_1() { ClassB b; } +void testb_2() { ClassB b; } \ No newline at end of file From dec4d305d02ea6f12505fe16463873d341e6bf6b Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 11:42:40 +0100 Subject: [PATCH 5/6] M3-2-1: Exclude member variables Exclude member variables and variables from templates or template instantiations, as the rule is only applicable to objects. --- ...ationsOfAnObjectShallHaveCompatibleTypes.ql | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql b/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql index 785509611b..cb970a41f9 100644 --- a/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql +++ b/cpp/autosar/src/rules/M3-2-1/DeclarationsOfAnObjectShallHaveCompatibleTypes.ql @@ -20,6 +20,11 @@ import cpp import codingstandards.cpp.autosar import codingstandards.cpp.Typehelpers +predicate isNonTemplateObjectVariable(GlobalOrNamespaceVariable gv) { + not gv.isFromTemplateInstantiation(_) and + not gv.isFromUninstantiatedTemplate(_) +} + from VariableDeclarationEntry decl1, VariableDeclarationEntry decl2 where not isExcluded(decl1, DeclarationsPackage::declarationsOfAnObjectShallHaveCompatibleTypesQuery()) and @@ -28,16 +33,9 @@ where // Note that normally `VariableDeclarationEntry` includes parameters, which are not covered // by this query. We implicitly exclude them with the `getQualifiedName()` predicate. decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName() and - // The underlying Variable shouldn't be from a TemplateVariable or a template instantiation - not exists(Variable v | - v = decl1.getVariable() - or - v = decl2.getVariable() - | - v instanceof TemplateVariable - or - v.isFromTemplateInstantiation(_) - ) + // Only consider global/namespace variables which aren't templated + isNonTemplateObjectVariable(decl1.getVariable()) and + isNonTemplateObjectVariable(decl2.getVariable()) select decl1, "The object $@ of type " + decl1.getType().toString() + " is not compatible with re-declaration $@ of type " + decl2.getType().toString(), decl1, From b6e9be7f9d856ab4c3f6d2813ed7bd20b253e30f Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 18 Aug 2022 11:45:42 +0100 Subject: [PATCH 6/6] M3-2-1: Add change note for template and member exclusions --- change_notes/2022-09-18-m3-2-1-templates.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 change_notes/2022-09-18-m3-2-1-templates.md diff --git a/change_notes/2022-09-18-m3-2-1-templates.md b/change_notes/2022-09-18-m3-2-1-templates.md new file mode 100644 index 0000000000..b96cb35dca --- /dev/null +++ b/change_notes/2022-09-18-m3-2-1-templates.md @@ -0,0 +1,4 @@ + - `M3-2-1` - `DeclarationsOfAnObjectShallHaveCompatibleTypes.ql` + - Reduced false positives by excluding non-object variables (for example, member variables). + - Reduced false positives by excluding variable templates and template instantiations. + - Improved the reported error message by including the conflicting type names. \ No newline at end of file