Skip to content

A2-3-1: Exclude wide string literals and utf8 string literals #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2024-01-17-fix-reported-fp-for-a2-3-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`A2-3-1`: ` cpp/autosar/invalid-character-in-string-literal`
- Fixes #311. Exclude wide string literals and utf8 string literal.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.Literals

bindingset[s]
string getCharOutsideBasicSourceCharSet(string s) {
Expand All @@ -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."
Original file line number Diff line number Diff line change
@@ -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. |
Original file line number Diff line number Diff line change
@@ -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. |
8 changes: 7 additions & 1 deletion cpp/autosar/test/rules/A2-3-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
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;
/*
Invalid character ↦ NON_COMPLIANT
*/

/*
Valid character @ in comments COMPLIANT
*/
16 changes: 16 additions & 0 deletions cpp/common/src/codingstandards/cpp/Literals.qll
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ 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\".*") }
}