Skip to content

Commit 1c4c483

Browse files
authored
Merge pull request #493 from rvermeulen/rvermeulen/fix-issue-#311
A2-3-1: Exclude wide string literals and utf8 string literals
2 parents 8b9f677 + e3c9408 commit 1c4c483

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`A2-3-1`: ` cpp/autosar/invalid-character-in-string-literal`
2+
- Fixes #311. Exclude wide string literals and utf8 string literal.

cpp/autosar/src/rules/A2-3-1/InvalidCharacterInStringLiteral.ql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.Literals
2021

2122
bindingset[s]
2223
string getCharOutsideBasicSourceCharSet(string s) {
@@ -27,6 +28,9 @@ string getCharOutsideBasicSourceCharSet(string s) {
2728
from StringLiteral s, string ch
2829
where
2930
not isExcluded(s, NamingPackage::invalidCharacterInStringLiteralQuery()) and
30-
ch = getCharOutsideBasicSourceCharSet(s.getValueText())
31+
ch = getCharOutsideBasicSourceCharSet(s.getValueText()) and
32+
// wide string and utf8 string literals are exempted.
33+
not s instanceof WideStringLiteral and
34+
not s instanceof Utf8StringLiteral
3135
select s,
3236
"String literal uses the character '" + ch + "' that is outside the language basic character set."
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
| 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. |
2-
| 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. |
2+
| 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. |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.cpp:7:20:7:22 | \u00ce\u00b1 | String literal uses the character '\u03b1' that is outside the language basic character set. |
1+
| test.cpp:7:21:7:23 | \u00ce\u00b1 | String literal uses the character '\u03b1' that is outside the language basic character set. |

cpp/autosar/test/rules/A2-3-1/test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
double α = 2.; // NON_COMPLIANT; U+03b1
55
void *to_𐆅_and_beyond = nullptr; // NON_COMPLIANT; U+10185
66
int l1_\u00A8; // COMPLIANT[FALSE_POSITIVE]
7-
const char *euro = "α"; // NON_COMPLIANT
7+
const char *euro1 = "α"; // NON_COMPLIANT
8+
const wchar_t *euro2 = L"α"; // COMPLIANT
9+
const char *euro3 = u8"α"; // COMPLIANT
810

911
int valid;
1012
/*
1113
Invalid character ↦ NON_COMPLIANT
14+
*/
15+
16+
/*
17+
Valid character @ in comments COMPLIANT
1218
*/

cpp/common/src/codingstandards/cpp/Literals.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,19 @@ string getTruncatedLiteralText(Literal l) {
1212
else result = text
1313
)
1414
}
15+
16+
class WideStringLiteral extends StringLiteral {
17+
WideStringLiteral() { this.getValueText().regexpMatch("(?s)\\s*L\".*") }
18+
}
19+
20+
class Utf8StringLiteral extends StringLiteral {
21+
Utf8StringLiteral() { this.getValueText().regexpMatch("(?s)\\s*u8\".*") }
22+
}
23+
24+
class Utf16StringLiteral extends StringLiteral {
25+
Utf16StringLiteral() { this.getValueText().regexpMatch("(?s)\\s*u\".*") }
26+
}
27+
28+
class Utf32StringLiteral extends StringLiteral {
29+
Utf32StringLiteral() { this.getValueText().regexpMatch("(?s)\\s*U\".*") }
30+
}

0 commit comments

Comments
 (0)