From 013d1e2a07f7b90551fcc6b9fa29c31f5ec09933 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Wed, 17 Jan 2024 16:11:41 -0800 Subject: [PATCH 1/5] Exclude wide string literals and utf8 string literals --- .../2024-01-17-fix-reported-fp-for-a2-3-1.md | 2 ++ .../A2-3-1/InvalidCharacterInStringLiteral.ql | 6 ++++- .../InvalidCharacterInStringLiteral.expected | 2 +- cpp/autosar/test/rules/A2-3-1/test.cpp | 4 +++- .../src/codingstandards/cpp/Literals.qll | 24 +++++++++++++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md diff --git a/change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md b/change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md new file mode 100644 index 0000000000..c819549e16 --- /dev/null +++ b/change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md @@ -0,0 +1,2 @@ +`A2-3-1`: ` cpp/autosar/invalid-character-in-string-literal` + - Exclude wide string literals and utf8 string literal. \ No newline at end of file diff --git a/cpp/autosar/src/rules/A2-3-1/InvalidCharacterInStringLiteral.ql b/cpp/autosar/src/rules/A2-3-1/InvalidCharacterInStringLiteral.ql index 93109bcd30..4f215d7d9c 100644 --- a/cpp/autosar/src/rules/A2-3-1/InvalidCharacterInStringLiteral.ql +++ b/cpp/autosar/src/rules/A2-3-1/InvalidCharacterInStringLiteral.ql @@ -17,6 +17,7 @@ import cpp import codingstandards.cpp.autosar +import codingstandards.cpp.Literals bindingset[s] string getCharOutsideBasicSourceCharSet(string s) { @@ -27,6 +28,9 @@ string getCharOutsideBasicSourceCharSet(string s) { from StringLiteral s, string ch where not isExcluded(s, NamingPackage::invalidCharacterInStringLiteralQuery()) and - ch = getCharOutsideBasicSourceCharSet(s.getValueText()) + ch = getCharOutsideBasicSourceCharSet(s.getValueText()) and + // wide string and utf8 string literals are exempted. + not s instanceof WideStringLiteral and + not s instanceof Utf8StringLiteral select s, "String literal uses the character '" + ch + "' that is outside the language basic character set." diff --git a/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInStringLiteral.expected b/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInStringLiteral.expected index 3ad38685ba..fe21bce430 100644 --- a/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInStringLiteral.expected +++ b/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInStringLiteral.expected @@ -1 +1 @@ -| test.cpp:7:20:7:22 | \u00ce\u00b1 | String literal uses the character '\u03b1' that is outside the language basic character set. | +| test.cpp:7:21:7:23 | \u00ce\u00b1 | String literal uses the character '\u03b1' that is outside the language basic character set. | diff --git a/cpp/autosar/test/rules/A2-3-1/test.cpp b/cpp/autosar/test/rules/A2-3-1/test.cpp index 5d1550f292..9ba0bbd5ce 100644 --- a/cpp/autosar/test/rules/A2-3-1/test.cpp +++ b/cpp/autosar/test/rules/A2-3-1/test.cpp @@ -4,7 +4,9 @@ double α = 2.; // NON_COMPLIANT; U+03b1 void *to_𐆅_and_beyond = nullptr; // NON_COMPLIANT; U+10185 int l1_\u00A8; // COMPLIANT[FALSE_POSITIVE] -const char *euro = "α"; // NON_COMPLIANT +const char *euro1 = "α"; // NON_COMPLIANT +const wchar_t *euro2 = L"α"; // COMPLIANT +const char *euro3 = u8"α"; // COMPLIANT int valid; /* diff --git a/cpp/common/src/codingstandards/cpp/Literals.qll b/cpp/common/src/codingstandards/cpp/Literals.qll index 0a6a40aa19..a772940154 100644 --- a/cpp/common/src/codingstandards/cpp/Literals.qll +++ b/cpp/common/src/codingstandards/cpp/Literals.qll @@ -12,3 +12,27 @@ string getTruncatedLiteralText(Literal l) { else result = text ) } + +class WideStringLiteral extends StringLiteral { + WideStringLiteral() { + this.getValueText().regexpMatch("(?s)\\s*L\".*") + } +} + +class Utf8StringLiteral extends StringLiteral { + Utf8StringLiteral() { + this.getValueText().regexpMatch("(?s)\\s*u8\".*") + } +} + +class Utf16StringLiteral extends StringLiteral { + Utf16StringLiteral() { + this.getValueText().regexpMatch("(?s)\\s*u\".*") + } +} + +class Utf32StringLiteral extends StringLiteral { + Utf32StringLiteral() { + this.getValueText().regexpMatch("(?s)\\s*U\".*") + } +} \ No newline at end of file From c6ab9c79192038d1c8eea6e02f0bcb1d1d5d0b2b Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 18 Jan 2024 14:52:56 -0800 Subject: [PATCH 2/5] Fix formatting --- .../src/codingstandards/cpp/Literals.qll | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/cpp/common/src/codingstandards/cpp/Literals.qll b/cpp/common/src/codingstandards/cpp/Literals.qll index a772940154..d4e11154fa 100644 --- a/cpp/common/src/codingstandards/cpp/Literals.qll +++ b/cpp/common/src/codingstandards/cpp/Literals.qll @@ -14,25 +14,17 @@ string getTruncatedLiteralText(Literal l) { } class WideStringLiteral extends StringLiteral { - WideStringLiteral() { - this.getValueText().regexpMatch("(?s)\\s*L\".*") - } + WideStringLiteral() { this.getValueText().regexpMatch("(?s)\\s*L\".*") } } class Utf8StringLiteral extends StringLiteral { - Utf8StringLiteral() { - this.getValueText().regexpMatch("(?s)\\s*u8\".*") - } + Utf8StringLiteral() { this.getValueText().regexpMatch("(?s)\\s*u8\".*") } } class Utf16StringLiteral extends StringLiteral { - Utf16StringLiteral() { - this.getValueText().regexpMatch("(?s)\\s*u\".*") - } + Utf16StringLiteral() { this.getValueText().regexpMatch("(?s)\\s*u\".*") } } class Utf32StringLiteral extends StringLiteral { - Utf32StringLiteral() { - this.getValueText().regexpMatch("(?s)\\s*U\".*") - } -} \ No newline at end of file + Utf32StringLiteral() { this.getValueText().regexpMatch("(?s)\\s*U\".*") } +} From ba94a630c996e21393e873e7ea48e919b1a77248 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 18 Jan 2024 15:36:36 -0800 Subject: [PATCH 3/5] Extend test case --- cpp/autosar/test/rules/A2-3-1/test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cpp/autosar/test/rules/A2-3-1/test.cpp b/cpp/autosar/test/rules/A2-3-1/test.cpp index 9ba0bbd5ce..cc8b1b53ac 100644 --- a/cpp/autosar/test/rules/A2-3-1/test.cpp +++ b/cpp/autosar/test/rules/A2-3-1/test.cpp @@ -11,4 +11,8 @@ const char *euro3 = u8"α"; // COMPLIANT int valid; /* Invalid character ↦ NON_COMPLIANT +*/ + +/* +Valid character @ in comments COMPLIANT */ \ No newline at end of file From 934829bdbbde72592e628a9ecec0abb8802a7cb2 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 18 Jan 2024 15:37:12 -0800 Subject: [PATCH 4/5] Accept expected test output --- .../test/rules/A2-3-1/InvalidCharacterInComment.expected | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInComment.expected b/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInComment.expected index b5fd4c77cc..4df213e5c2 100644 --- a/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInComment.expected +++ b/cpp/autosar/test/rules/A2-3-1/InvalidCharacterInComment.expected @@ -1,2 +1,2 @@ | test.cpp:3:1:3:37 | // Invalid character \u00ce\u00b1 NON_COMPLIANT | Comment uses the character '\u00ce\u00b1' that is outside the language basic character set. | -| test.cpp:10:1:12:2 | /*\nInvalid character \u00e2\u0086\u00a6 NON_COMPLIANT\n*/ | Comment uses the character '\u00e2\u0086\u00a6' that is outside the language basic character set. | +| test.cpp:12:1:14:2 | /*\nInvalid character \u00e2\u0086\u00a6 NON_COMPLIANT\n*/ | Comment uses the character '\u00e2\u0086\u00a6' that is outside the language basic character set. | From e3c9408c3e097b5a94ebc1283f8c85e6e1d52df1 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Tue, 23 Jan 2024 15:18:49 -0800 Subject: [PATCH 5/5] Add reference to fixed issue --- change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md b/change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md index c819549e16..0ac0580506 100644 --- a/change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md +++ b/change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md @@ -1,2 +1,2 @@ `A2-3-1`: ` cpp/autosar/invalid-character-in-string-literal` - - Exclude wide string literals and utf8 string literal. \ No newline at end of file + - Fixes #311. Exclude wide string literals and utf8 string literal. \ No newline at end of file