From 29d0538a020e89596b10a8817e01fff0c9dc263f Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Mon, 3 Jun 2024 15:23:17 +0100 Subject: [PATCH 1/4] A7-1-2: Exclude generated variables and variables in uninstantiated templates. --- change_notes/2024-06-03-constexpr-variable.md | 3 +++ .../src/rules/A7-1-2/VariableMissingConstexpr.ql | 6 +++++- cpp/autosar/test/rules/A7-1-2/test.cpp | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 change_notes/2024-06-03-constexpr-variable.md diff --git a/change_notes/2024-06-03-constexpr-variable.md b/change_notes/2024-06-03-constexpr-variable.md new file mode 100644 index 0000000000..1731e7de5b --- /dev/null +++ b/change_notes/2024-06-03-constexpr-variable.md @@ -0,0 +1,3 @@ + - `A7-1-2` - `VariableMissingConstexpr.ql`: + - Remove false positives for compiler generated variables + - Remove results in uninstantiated templates that cause false positives \ No newline at end of file diff --git a/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql b/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql index 13272c8169..4a97e84493 100644 --- a/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql +++ b/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql @@ -62,5 +62,9 @@ where // Not assigned by a user in a constructor not exists(ConstructorFieldInit cfi | cfi.getTarget() = v and not cfi.isCompilerGenerated()) and // Ignore union members - not v.getDeclaringType() instanceof Union + not v.getDeclaringType() instanceof Union and + // Exclude variables in uninstantiated templates, as they may be incomplete + not v.isFromUninstantiatedTemplate(_) and + // Exclude compiler generated variables, which are not user controllable + not v.isCompilerGenerated() select v, "Variable " + v.getName() + " could be marked 'constexpr'." diff --git a/cpp/autosar/test/rules/A7-1-2/test.cpp b/cpp/autosar/test/rules/A7-1-2/test.cpp index a3b7baea83..210a386543 100644 --- a/cpp/autosar/test/rules/A7-1-2/test.cpp +++ b/cpp/autosar/test/rules/A7-1-2/test.cpp @@ -264,4 +264,16 @@ constexpr void fp_reported_in_466(int p) { // compile time constant int l26 = add4(1, l3); // COMPLIANT - l3 is not compile time constant on all paths +} + +template T* init(T** t) { } + +template T* init() { + T* t = nullptr; // COMPLIANT - initialized below + init(&t); // Init is ignored in uninitialized template + return t; +} + +void test_template_instantiation() { + int* t = init(); } \ No newline at end of file From 648f58350e46af55924f3010bfd28387d99122ce Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Wed, 5 Jun 2024 14:05:39 +0100 Subject: [PATCH 2/4] Address review comments --- change_notes/2024-06-03-constexpr-variable.md | 3 +-- .../src/rules/A7-1-2/VariableMissingConstexpr.ql | 2 +- cpp/autosar/test/rules/A7-1-2/test.cpp | 14 ++++++-------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/change_notes/2024-06-03-constexpr-variable.md b/change_notes/2024-06-03-constexpr-variable.md index 1731e7de5b..1da02b5d9f 100644 --- a/change_notes/2024-06-03-constexpr-variable.md +++ b/change_notes/2024-06-03-constexpr-variable.md @@ -1,3 +1,2 @@ - `A7-1-2` - `VariableMissingConstexpr.ql`: - - Remove false positives for compiler generated variables - - Remove results in uninstantiated templates that cause false positives \ No newline at end of file + - Fixes #607. Remove false positives for compiler generated variables and in uninstantiated templates \ No newline at end of file diff --git a/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql b/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql index 4a97e84493..f0adab07d4 100644 --- a/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql +++ b/cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql @@ -67,4 +67,4 @@ where not v.isFromUninstantiatedTemplate(_) and // Exclude compiler generated variables, which are not user controllable not v.isCompilerGenerated() -select v, "Variable " + v.getName() + " could be marked 'constexpr'." +select v, "Variable '" + v.getName() + "' could be marked 'constexpr'." diff --git a/cpp/autosar/test/rules/A7-1-2/test.cpp b/cpp/autosar/test/rules/A7-1-2/test.cpp index 210a386543..8395f60ff3 100644 --- a/cpp/autosar/test/rules/A7-1-2/test.cpp +++ b/cpp/autosar/test/rules/A7-1-2/test.cpp @@ -266,14 +266,12 @@ constexpr void fp_reported_in_466(int p) { add4(1, l3); // COMPLIANT - l3 is not compile time constant on all paths } -template T* init(T** t) { } +template T *init(T **t) {} -template T* init() { - T* t = nullptr; // COMPLIANT - initialized below - init(&t); // Init is ignored in uninitialized template - return t; +template T *init() { + T *t = nullptr; // COMPLIANT - initialized below + init(&t); // Init is ignored in uninitialized template + return t; } -void test_template_instantiation() { - int* t = init(); -} \ No newline at end of file +void test_template_instantiation() { int *t = init(); } \ No newline at end of file From 766504c922a430451637f232d76996167e83d753 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Wed, 5 Jun 2024 15:05:57 +0100 Subject: [PATCH 3/4] Add expected results file --- .../A7-1-2/VariableMissingConstexpr.expected | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/cpp/autosar/test/rules/A7-1-2/VariableMissingConstexpr.expected b/cpp/autosar/test/rules/A7-1-2/VariableMissingConstexpr.expected index dbf223e0cf..f86faf1a7b 100644 --- a/cpp/autosar/test/rules/A7-1-2/VariableMissingConstexpr.expected +++ b/cpp/autosar/test/rules/A7-1-2/VariableMissingConstexpr.expected @@ -1,23 +1,23 @@ -| test.cpp:4:5:4:6 | g1 | Variable g1 could be marked 'constexpr'. | -| test.cpp:6:5:6:6 | g2 | Variable g2 could be marked 'constexpr'. | -| test.cpp:13:14:13:15 | lc | Variable lc could be marked 'constexpr'. | -| test.cpp:15:14:15:16 | lca | Variable lca could be marked 'constexpr'. | -| test.cpp:23:15:23:17 | lc2 | Variable lc2 could be marked 'constexpr'. | -| test.cpp:25:15:25:18 | lc2a | Variable lc2a could be marked 'constexpr'. | -| test.cpp:41:14:41:15 | l2 | Variable l2 could be marked 'constexpr'. | -| test.cpp:44:16:44:17 | lc | Variable lc could be marked 'constexpr'. | -| test.cpp:45:17:45:19 | lc2 | Variable lc2 could be marked 'constexpr'. | -| test.cpp:55:7:55:8 | m2 | Variable m2 could be marked 'constexpr'. | -| test.cpp:130:7:130:8 | m1 | Variable m1 could be marked 'constexpr'. | -| test.cpp:141:7:141:8 | m1 | Variable m1 could be marked 'constexpr'. | -| test.cpp:221:7:221:8 | l1 | Variable l1 could be marked 'constexpr'. | -| test.cpp:235:7:235:8 | l6 | Variable l6 could be marked 'constexpr'. | -| test.cpp:237:7:237:8 | l8 | Variable l8 could be marked 'constexpr'. | -| test.cpp:240:7:240:9 | l10 | Variable l10 could be marked 'constexpr'. | -| test.cpp:243:7:243:9 | l12 | Variable l12 could be marked 'constexpr'. | -| test.cpp:248:7:248:9 | l15 | Variable l15 could be marked 'constexpr'. | -| test.cpp:250:7:250:9 | l16 | Variable l16 could be marked 'constexpr'. | -| test.cpp:251:7:251:9 | l17 | Variable l17 could be marked 'constexpr'. | -| test.cpp:257:7:257:9 | l21 | Variable l21 could be marked 'constexpr'. | -| test.cpp:262:7:262:9 | l24 | Variable l24 could be marked 'constexpr'. | -| test.cpp:263:7:263:9 | l25 | Variable l25 could be marked 'constexpr'. | +| test.cpp:4:5:4:6 | g1 | Variable 'g1' could be marked 'constexpr'. | +| test.cpp:6:5:6:6 | g2 | Variable 'g2' could be marked 'constexpr'. | +| test.cpp:13:14:13:15 | lc | Variable 'lc' could be marked 'constexpr'. | +| test.cpp:15:14:15:16 | lca | Variable 'lca' could be marked 'constexpr'. | +| test.cpp:23:15:23:17 | lc2 | Variable 'lc2' could be marked 'constexpr'. | +| test.cpp:25:15:25:18 | lc2a | Variable 'lc2a' could be marked 'constexpr'. | +| test.cpp:41:14:41:15 | l2 | Variable 'l2' could be marked 'constexpr'. | +| test.cpp:44:16:44:17 | lc | Variable 'lc' could be marked 'constexpr'. | +| test.cpp:45:17:45:19 | lc2 | Variable 'lc2' could be marked 'constexpr'. | +| test.cpp:55:7:55:8 | m2 | Variable 'm2' could be marked 'constexpr'. | +| test.cpp:130:7:130:8 | m1 | Variable 'm1' could be marked 'constexpr'. | +| test.cpp:141:7:141:8 | m1 | Variable 'm1' could be marked 'constexpr'. | +| test.cpp:221:7:221:8 | l1 | Variable 'l1' could be marked 'constexpr'. | +| test.cpp:235:7:235:8 | l6 | Variable 'l6' could be marked 'constexpr'. | +| test.cpp:237:7:237:8 | l8 | Variable 'l8' could be marked 'constexpr'. | +| test.cpp:240:7:240:9 | l10 | Variable 'l10' could be marked 'constexpr'. | +| test.cpp:243:7:243:9 | l12 | Variable 'l12' could be marked 'constexpr'. | +| test.cpp:248:7:248:9 | l15 | Variable 'l15' could be marked 'constexpr'. | +| test.cpp:250:7:250:9 | l16 | Variable 'l16' could be marked 'constexpr'. | +| test.cpp:251:7:251:9 | l17 | Variable 'l17' could be marked 'constexpr'. | +| test.cpp:257:7:257:9 | l21 | Variable 'l21' could be marked 'constexpr'. | +| test.cpp:262:7:262:9 | l24 | Variable 'l24' could be marked 'constexpr'. | +| test.cpp:263:7:263:9 | l25 | Variable 'l25' could be marked 'constexpr'. | From d8986c951fad9c8f5802f34c0b96e6f230b0124c Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Thu, 6 Jun 2024 11:51:57 +0100 Subject: [PATCH 4/4] Update function constexpr expected results --- .../test/rules/A7-1-2/FunctionMissingConstexpr.expected | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cpp/autosar/test/rules/A7-1-2/FunctionMissingConstexpr.expected b/cpp/autosar/test/rules/A7-1-2/FunctionMissingConstexpr.expected index 666721d0ad..a6de3fd724 100644 --- a/cpp/autosar/test/rules/A7-1-2/FunctionMissingConstexpr.expected +++ b/cpp/autosar/test/rules/A7-1-2/FunctionMissingConstexpr.expected @@ -10,3 +10,7 @@ | test.cpp:162:3:162:26 | VariantMemberInitialized | VariantMemberInitialized function could be marked as 'constexpr'. | | test.cpp:163:3:163:26 | VariantMemberInitialized | VariantMemberInitialized function could be marked as 'constexpr'. | | test.cpp:190:3:190:22 | VariantMemberNotInit | VariantMemberNotInit function could be marked as 'constexpr'. | +| test.cpp:269:26:269:26 | init | init function could be marked as 'constexpr'. | +| test.cpp:269:26:269:29 | init | init function could be marked as 'constexpr'. | +| test.cpp:271:26:271:26 | init | init function could be marked as 'constexpr'. | +| test.cpp:277:6:277:32 | test_template_instantiation | test_template_instantiation function could be marked as 'constexpr'. |