From b19874adb61a247a1a9b1dd8912e2cee82f9aec0 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Fri, 16 Feb 2024 14:15:44 -0800 Subject: [PATCH 01/14] Exclude literals that are the result of constexpr variable uses. --- .../A5-1-1/LiteralValueUsedOutsideTypeInit.ql | 3 ++- .../LiteralValueUsedOutsideTypeInit.expected | 4 ++++ cpp/autosar/test/rules/A5-1-1/test.cpp | 24 ++++++++++++++++++- .../src/codingstandards/cpp/Literals.qll | 19 +++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql index a83e3ade5d..233fe5c4a3 100644 --- a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql +++ b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql @@ -51,7 +51,8 @@ where // Aggregate literal not l = any(ArrayOrVectorAggregateLiteral aal).getAnElementExpr(_).getAChild*() and // Ignore x - 1 expressions - not exists(SubExpr se | se.getRightOperand() = l and l.getValue() = "1") + not exists(SubExpr se | se.getRightOperand() = l and l.getValue() = "1") and + not l instanceof CompileTimeComputedIntegralLiteral select l, "Literal value " + getTruncatedLiteralText(l) + " used outside of type initialization " + l.getAPrimaryQlClass() diff --git a/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected b/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected index 3212f14efb..4aecf3bf6c 100644 --- a/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected +++ b/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected @@ -3,3 +3,7 @@ | test.cpp:54:7:54:7 | 1 | Literal value 1 used outside of type initialization Literal | | test.cpp:75:23:75:28 | test | Literal value "test" used outside of type initialization StringLiteral | | test.cpp:76:19:76:28 | not okay | Literal value "not okay" used outside of type initialization StringLiteral | +| test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | +| test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | +| test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | +| test.cpp:108:18:108:18 | 1 | Literal value 1 used outside of type initialization Literal | diff --git a/cpp/autosar/test/rules/A5-1-1/test.cpp b/cpp/autosar/test/rules/A5-1-1/test.cpp index 4c4ad4fb30..58c1ac64f1 100644 --- a/cpp/autosar/test/rules/A5-1-1/test.cpp +++ b/cpp/autosar/test/rules/A5-1-1/test.cpp @@ -80,10 +80,32 @@ void test_not_wrapper_stream(std::ostream &os, const char *str) noexcept { #define MACRO_LOG(test_str) \ do { \ struct test_struct { \ - static const char *get_str() { return static_cast(test_str); } \ + static const char *get_str() { \ + return static_cast(test_str); \ + } \ }; \ } while (false) void f() { MACRO_LOG("test"); // COMPLIANT - exclusion +} + +template struct S1 { static constexpr size_t value(); }; + +template <> struct S1 { + static constexpr size_t value() { return sizeof(int); }; +}; + +constexpr size_t g1 = S1::value(); +constexpr size_t f1() { return sizeof(int); } + +template struct S2 { + T m1[size]; // COMPLIANT + T m2[4]; // NON_COMPLIANT +}; + +void test_fp_reported_in_371() { + struct S2 l1; // COMPLIANT[FALSE_POSITIVE] + struct S2 l2; // COMPLIANT + struct S2 l3; // COMPLIANT } \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/Literals.qll b/cpp/common/src/codingstandards/cpp/Literals.qll index d4e11154fa..e6014d417b 100644 --- a/cpp/common/src/codingstandards/cpp/Literals.qll +++ b/cpp/common/src/codingstandards/cpp/Literals.qll @@ -28,3 +28,22 @@ class Utf16StringLiteral extends StringLiteral { class Utf32StringLiteral extends StringLiteral { Utf32StringLiteral() { this.getValueText().regexpMatch("(?s)\\s*U\".*") } } + +/** + * A literal resulting from the use of a constexpr + * variable, or macro expansion. + */ +class CompileTimeComputedIntegralLiteral extends Literal { + CompileTimeComputedIntegralLiteral() { + this.getUnspecifiedType() instanceof IntegralType and + not this.getUnspecifiedType() instanceof BoolType and + not this.getUnspecifiedType() instanceof CharType and + // In some cases we still type char constants like '.' as int + not this.getValueText().trim().matches("'%'") and + not this.getValueText() + .trim() + .regexpMatch("([0-9][0-9']*|0[xX][0-9a-fA-F']+|0b[01']+)[uU]?([lL]{1,2}|[zZ])?") and + // Exclude class field initializers whose value text equals the initializer expression, e.g., `x(0)` + not any(ConstructorFieldInit cfi).getExpr() = this + } +} From 5f90fae199089c5d4abb325203178e570a78a444 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Fri, 16 Feb 2024 14:23:05 -0800 Subject: [PATCH 02/14] Exclude literals used in class template instantiation --- .../src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql | 5 ++++- .../rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected | 1 - cpp/autosar/test/rules/A5-1-1/test.cpp | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql index 233fe5c4a3..4392821af5 100644 --- a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql +++ b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql @@ -52,7 +52,10 @@ where not l = any(ArrayOrVectorAggregateLiteral aal).getAnElementExpr(_).getAChild*() and // Ignore x - 1 expressions not exists(SubExpr se | se.getRightOperand() = l and l.getValue() = "1") and - not l instanceof CompileTimeComputedIntegralLiteral + not l instanceof CompileTimeComputedIntegralLiteral and + // Exclude literals to instantiate a class template per example in the standard + // where an type of std::array is intialized with size 5. + not l = any(ClassTemplateInstantiation cti).getATemplateArgument() select l, "Literal value " + getTruncatedLiteralText(l) + " used outside of type initialization " + l.getAPrimaryQlClass() diff --git a/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected b/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected index 4aecf3bf6c..99ab3d6c37 100644 --- a/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected +++ b/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected @@ -6,4 +6,3 @@ | test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | | test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | | test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | -| test.cpp:108:18:108:18 | 1 | Literal value 1 used outside of type initialization Literal | diff --git a/cpp/autosar/test/rules/A5-1-1/test.cpp b/cpp/autosar/test/rules/A5-1-1/test.cpp index 58c1ac64f1..4f3b812c2d 100644 --- a/cpp/autosar/test/rules/A5-1-1/test.cpp +++ b/cpp/autosar/test/rules/A5-1-1/test.cpp @@ -105,7 +105,7 @@ template struct S2 { }; void test_fp_reported_in_371() { - struct S2 l1; // COMPLIANT[FALSE_POSITIVE] + struct S2 l1; // COMPLIANT struct S2 l2; // COMPLIANT struct S2 l3; // COMPLIANT } \ No newline at end of file From b79e986f20630cb72534c0e53ecdcb0563f32622 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Fri, 16 Feb 2024 14:26:25 -0800 Subject: [PATCH 03/14] Format alert according to style-guide --- .../A5-1-1/LiteralValueUsedOutsideTypeInit.ql | 4 +--- .../LiteralValueUsedOutsideTypeInit.expected | 16 ++++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql index 4392821af5..ef838e585b 100644 --- a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql +++ b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql @@ -56,6 +56,4 @@ where // Exclude literals to instantiate a class template per example in the standard // where an type of std::array is intialized with size 5. not l = any(ClassTemplateInstantiation cti).getATemplateArgument() -select l, - "Literal value " + getTruncatedLiteralText(l) + " used outside of type initialization " + - l.getAPrimaryQlClass() +select l, "Literal value '" + getTruncatedLiteralText(l) + "' used outside of type initialization." diff --git a/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected b/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected index 99ab3d6c37..22300512fc 100644 --- a/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected +++ b/cpp/autosar/test/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.expected @@ -1,8 +1,8 @@ -| test.cpp:5:9:5:25 | constant string | Literal value "constant string" used outside of type initialization StringLiteral | -| test.cpp:14:23:14:25 | 100 | Literal value 100 used outside of type initialization Literal | -| test.cpp:54:7:54:7 | 1 | Literal value 1 used outside of type initialization Literal | -| test.cpp:75:23:75:28 | test | Literal value "test" used outside of type initialization StringLiteral | -| test.cpp:76:19:76:28 | not okay | Literal value "not okay" used outside of type initialization StringLiteral | -| test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | -| test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | -| test.cpp:104:8:104:8 | 4 | Literal value 4 used outside of type initialization Literal | +| test.cpp:5:9:5:25 | constant string | Literal value '"constant string"' used outside of type initialization. | +| test.cpp:14:23:14:25 | 100 | Literal value '100' used outside of type initialization. | +| test.cpp:54:7:54:7 | 1 | Literal value '1' used outside of type initialization. | +| test.cpp:75:23:75:28 | test | Literal value '"test"' used outside of type initialization. | +| test.cpp:76:19:76:28 | not okay | Literal value '"not okay"' used outside of type initialization. | +| test.cpp:104:8:104:8 | 4 | Literal value '4' used outside of type initialization. | +| test.cpp:104:8:104:8 | 4 | Literal value '4' used outside of type initialization. | +| test.cpp:104:8:104:8 | 4 | Literal value '4' used outside of type initialization. | From 9109b3c29153171c85f93ff1e205a7333635ee9f Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Fri, 16 Feb 2024 14:46:25 -0800 Subject: [PATCH 04/14] Add changenote --- change_notes/2024-02-16-fix-fps-a5-1-1.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 change_notes/2024-02-16-fix-fps-a5-1-1.md diff --git a/change_notes/2024-02-16-fix-fps-a5-1-1.md b/change_notes/2024-02-16-fix-fps-a5-1-1.md new file mode 100644 index 0000000000..6cc792be29 --- /dev/null +++ b/change_notes/2024-02-16-fix-fps-a5-1-1.md @@ -0,0 +1,4 @@ +- `A5-1-1` - `LiteralValueUsedOutsideTypeInit.ql`: + - Address FP reported in #371. Exclude literals generated by uses of constexpr variables. + - Exclude literals used in class template instantiations. + - Update the alert message to adhere to the style-guide. \ No newline at end of file From eb13914e98cba05b0d8505691c00da758ce75234 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 26 Feb 2024 16:22:19 -0800 Subject: [PATCH 05/14] Exclude boolean literals used as non-type template arguments --- .../rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql | 2 +- cpp/common/src/codingstandards/cpp/Literals.qll | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql index ef838e585b..34acb3dd4f 100644 --- a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql +++ b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql @@ -39,7 +39,7 @@ where // Exclude `nullptr` not l.getType() instanceof NullPointerType and // Exclude boolean `true` and `false` - not l.getType() instanceof BoolType and + not l instanceof BoolLiteral and // Exclude empty string not l.getValue() = "" and // Template functions use literals to represent calls which are unknown diff --git a/cpp/common/src/codingstandards/cpp/Literals.qll b/cpp/common/src/codingstandards/cpp/Literals.qll index e6014d417b..8f0dbe775b 100644 --- a/cpp/common/src/codingstandards/cpp/Literals.qll +++ b/cpp/common/src/codingstandards/cpp/Literals.qll @@ -47,3 +47,15 @@ class CompileTimeComputedIntegralLiteral extends Literal { not any(ConstructorFieldInit cfi).getExpr() = this } } + +class BoolLiteral extends Literal { + BoolLiteral() { + this.getType() instanceof BoolType + or + // When used as non-type template arguments, bool literals might + // have been converted to a non-bool type. + this.getValue() = "1" and this.getValueText() = "true" + or + this.getValue() = "0" and this.getValueText() = "false" + } +} From 28ac4c1a2038a72f87f5a14d43412bc8dd4e792b Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 26 Feb 2024 16:32:53 -0800 Subject: [PATCH 06/14] Extend excluded char literals with char16_t and char32_t --- .../A5-1-1/LiteralValueUsedOutsideTypeInit.ql | 3 ++- .../src/codingstandards/cpp/Cpp14Literal.qll | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql index 34acb3dd4f..54c7fea969 100644 --- a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql +++ b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql @@ -18,6 +18,7 @@ import cpp import codingstandards.cpp.autosar import codingstandards.cpp.LoggingOperation import codingstandards.cpp.Literals +import codingstandards.cpp.Cpp14Literal from Literal l where @@ -35,7 +36,7 @@ where // Exclude literal 0 not l.getValue() = "0" and // Exclude character literals - not l instanceof CharLiteral and + not l instanceof Cpp14Literal::CharLiteral and // Exclude `nullptr` not l.getType() instanceof NullPointerType and // Exclude boolean `true` and `false` diff --git a/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll b/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll index c3908008ef..d8e265d4ab 100644 --- a/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll +++ b/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll @@ -82,4 +82,24 @@ module Cpp14Literal { override string getAPrimaryQlClass() { result = "FloatingLiteral" } } + + /** + * A character literal. For example: + * ``` + * char c1 = 'a'; + * char16_t c2 = u'a'; + * char32_t c3 = U'a'; + * wchar_t c4 = L'b'; + * ``` + */ + class CharLiteral extends StandardLibrary::TextLiteral { + CharLiteral() { this.getValueText().regexpMatch("(?s)\\s*(L|u|U)?'.*") } + + override string getAPrimaryQlClass() { result = "CharLiteral" } + + /** + * Gets the character of this literal. For example `L'a'` has character `"a"`. + */ + string getCharacter() { result = this.getValueText().regexpCapture("(?s)\\s*(L|u|U)?'(.*)'", 1) } + } } From 00d36a73b3f909c927fd0bae16b1820ac958618e Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 26 Feb 2024 16:35:23 -0800 Subject: [PATCH 07/14] Exclude bool literals and char literals instead of types Bool literals do not always have a bool type so we exclude them using the bool literal class. We exclude using the char literal class to reduce duplication and increase coverage in case of new char literals. --- cpp/common/src/codingstandards/cpp/Literals.qll | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cpp/common/src/codingstandards/cpp/Literals.qll b/cpp/common/src/codingstandards/cpp/Literals.qll index 8f0dbe775b..192c3c0445 100644 --- a/cpp/common/src/codingstandards/cpp/Literals.qll +++ b/cpp/common/src/codingstandards/cpp/Literals.qll @@ -3,6 +3,7 @@ */ import cpp +import codingstandards.cpp.Cpp14Literal /** Gets `Literal.getValueText()` truncated to at most 20 characters. */ string getTruncatedLiteralText(Literal l) { @@ -36,10 +37,12 @@ class Utf32StringLiteral extends StringLiteral { class CompileTimeComputedIntegralLiteral extends Literal { CompileTimeComputedIntegralLiteral() { this.getUnspecifiedType() instanceof IntegralType and - not this.getUnspecifiedType() instanceof BoolType and - not this.getUnspecifiedType() instanceof CharType and - // In some cases we still type char constants like '.' as int - not this.getValueText().trim().matches("'%'") and + // Exclude bool, whose value text is true or false, but the value itself + // is 1 or 0. + not this instanceof BoolLiteral and + // Exclude character literals, whose value text is the quoted character, but the value + // is the numeric value of the character. + not this instanceof Cpp14Literal::CharLiteral and not this.getValueText() .trim() .regexpMatch("([0-9][0-9']*|0[xX][0-9a-fA-F']+|0b[01']+)[uU]?([lL]{1,2}|[zZ])?") and From 24b4a0038f9203614964513f80c4197ab43270b6 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 26 Feb 2024 16:37:16 -0800 Subject: [PATCH 08/14] Document assumption underlying `CompileTimeComputedIntegralLiteral` --- cpp/common/src/codingstandards/cpp/Literals.qll | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp/common/src/codingstandards/cpp/Literals.qll b/cpp/common/src/codingstandards/cpp/Literals.qll index 192c3c0445..8bc51ebe95 100644 --- a/cpp/common/src/codingstandards/cpp/Literals.qll +++ b/cpp/common/src/codingstandards/cpp/Literals.qll @@ -33,6 +33,8 @@ class Utf32StringLiteral extends StringLiteral { /** * A literal resulting from the use of a constexpr * variable, or macro expansion. + * We rely on the fact that the value text of a literal is equal to the + * `constexpr` variable or macro name. */ class CompileTimeComputedIntegralLiteral extends Literal { CompileTimeComputedIntegralLiteral() { From ba39b6822664be4cf8aaa158a56383f82d5de3fe Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 26 Feb 2024 16:54:04 -0800 Subject: [PATCH 09/14] Exclude literals part of a class aggregate literal --- .../src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql index 54c7fea969..9daea358e7 100644 --- a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql +++ b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql @@ -56,5 +56,6 @@ where not l instanceof CompileTimeComputedIntegralLiteral and // Exclude literals to instantiate a class template per example in the standard // where an type of std::array is intialized with size 5. - not l = any(ClassTemplateInstantiation cti).getATemplateArgument() + not l = any(ClassTemplateInstantiation cti).getATemplateArgument() and + not l = any(ClassAggregateLiteral cal).getAFieldExpr(_) select l, "Literal value '" + getTruncatedLiteralText(l) + "' used outside of type initialization." From 9a9138ab375bb7c03fc5b159ea9a033e8fa94d7c Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 26 Feb 2024 16:55:21 -0800 Subject: [PATCH 10/14] Extend test cases - Add tests with different char literals - Add test case with float literal --- cpp/autosar/test/rules/A5-1-1/test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cpp/autosar/test/rules/A5-1-1/test.cpp b/cpp/autosar/test/rules/A5-1-1/test.cpp index 4f3b812c2d..65e691fd32 100644 --- a/cpp/autosar/test/rules/A5-1-1/test.cpp +++ b/cpp/autosar/test/rules/A5-1-1/test.cpp @@ -104,8 +104,22 @@ template struct S2 { T m2[4]; // NON_COMPLIANT }; +template struct S3 { + static constexpr T value = val; // COMPLIANT; +}; + void test_fp_reported_in_371() { struct S2 l1; // COMPLIANT struct S2 l2; // COMPLIANT struct S2 l3; // COMPLIANT + + S3 l4; // COMPLIANT + S3::value> l5; // COMPLIANT + S3 l6; // COMPLIANT + S3::value> l7; // COMPLIANT + + constexpr float l8 = 3.14159f; +#define delta 0.1f + for (float i = 0.0f; i < l8; i += delta) { // COMPLIANT + } } \ No newline at end of file From e1521ad87cfa458f72c8a942903935910647df45 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 26 Feb 2024 16:58:33 -0800 Subject: [PATCH 11/14] Add comment describing exclusion of compile time float literals --- .../src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql index 9daea358e7..a14681d95b 100644 --- a/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql +++ b/cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql @@ -53,6 +53,10 @@ where not l = any(ArrayOrVectorAggregateLiteral aal).getAnElementExpr(_).getAChild*() and // Ignore x - 1 expressions not exists(SubExpr se | se.getRightOperand() = l and l.getValue() = "1") and + // Exclude compile time computed integral literals as they can appear as integral literals + // when used as non-type template arguments. + // We limit ourselves to integral literals, because floating point literals as non-type + // template arguments are not supported in C++ 14. Those are supported shince C++ 20. not l instanceof CompileTimeComputedIntegralLiteral and // Exclude literals to instantiate a class template per example in the standard // where an type of std::array is intialized with size 5. From 226c9f585e6bcfb2abfc93c09e42d62da5a08267 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Tue, 27 Feb 2024 10:04:35 -0800 Subject: [PATCH 12/14] Update changenote --- change_notes/2024-02-16-fix-fps-a5-1-1.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/change_notes/2024-02-16-fix-fps-a5-1-1.md b/change_notes/2024-02-16-fix-fps-a5-1-1.md index 6cc792be29..11831f70ea 100644 --- a/change_notes/2024-02-16-fix-fps-a5-1-1.md +++ b/change_notes/2024-02-16-fix-fps-a5-1-1.md @@ -1,4 +1,7 @@ - `A5-1-1` - `LiteralValueUsedOutsideTypeInit.ql`: - Address FP reported in #371. Exclude literals generated by uses of constexpr variables. - Exclude literals used in class template instantiations. - - Update the alert message to adhere to the style-guide. \ No newline at end of file + - Update the alert message to adhere to the style-guide. + - Exclude boolean literals used as template arguments. + - Exclude `u` and `U` prefixed char literals. + - Exclude literals part of a class aggregate literal. From 7b4c73abd46518922b65020f8ab1657290b36a0b Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Mon, 4 Mar 2024 14:46:29 -0800 Subject: [PATCH 13/14] Format query module. --- cpp/common/src/codingstandards/cpp/Literals.qll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/common/src/codingstandards/cpp/Literals.qll b/cpp/common/src/codingstandards/cpp/Literals.qll index 8bc51ebe95..38f2fb0e8b 100644 --- a/cpp/common/src/codingstandards/cpp/Literals.qll +++ b/cpp/common/src/codingstandards/cpp/Literals.qll @@ -41,7 +41,7 @@ class CompileTimeComputedIntegralLiteral extends Literal { this.getUnspecifiedType() instanceof IntegralType and // Exclude bool, whose value text is true or false, but the value itself // is 1 or 0. - not this instanceof BoolLiteral and + not this instanceof BoolLiteral and // Exclude character literals, whose value text is the quoted character, but the value // is the numeric value of the character. not this instanceof Cpp14Literal::CharLiteral and From 3b3fca019c08cdbb7a13e9da6f08291b4fff0242 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Tue, 5 Mar 2024 09:37:57 -0800 Subject: [PATCH 14/14] Format module --- cpp/common/src/codingstandards/cpp/Cpp14Literal.qll | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll b/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll index d8e265d4ab..c974ec7eb8 100644 --- a/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll +++ b/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll @@ -100,6 +100,8 @@ module Cpp14Literal { /** * Gets the character of this literal. For example `L'a'` has character `"a"`. */ - string getCharacter() { result = this.getValueText().regexpCapture("(?s)\\s*(L|u|U)?'(.*)'", 1) } + string getCharacter() { + result = this.getValueText().regexpCapture("(?s)\\s*(L|u|U)?'(.*)'", 1) + } } }