Skip to content

Commit 28ac4c1

Browse files
committed
Extend excluded char literals with char16_t and char32_t
1 parent eb13914 commit 28ac4c1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import codingstandards.cpp.LoggingOperation
2020
import codingstandards.cpp.Literals
21+
import codingstandards.cpp.Cpp14Literal
2122

2223
from Literal l
2324
where
@@ -35,7 +36,7 @@ where
3536
// Exclude literal 0
3637
not l.getValue() = "0" and
3738
// Exclude character literals
38-
not l instanceof CharLiteral and
39+
not l instanceof Cpp14Literal::CharLiteral and
3940
// Exclude `nullptr`
4041
not l.getType() instanceof NullPointerType and
4142
// Exclude boolean `true` and `false`

cpp/common/src/codingstandards/cpp/Cpp14Literal.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,24 @@ module Cpp14Literal {
8282

8383
override string getAPrimaryQlClass() { result = "FloatingLiteral" }
8484
}
85+
86+
/**
87+
* A character literal. For example:
88+
* ```
89+
* char c1 = 'a';
90+
* char16_t c2 = u'a';
91+
* char32_t c3 = U'a';
92+
* wchar_t c4 = L'b';
93+
* ```
94+
*/
95+
class CharLiteral extends StandardLibrary::TextLiteral {
96+
CharLiteral() { this.getValueText().regexpMatch("(?s)\\s*(L|u|U)?'.*") }
97+
98+
override string getAPrimaryQlClass() { result = "CharLiteral" }
99+
100+
/**
101+
* Gets the character of this literal. For example `L'a'` has character `"a"`.
102+
*/
103+
string getCharacter() { result = this.getValueText().regexpCapture("(?s)\\s*(L|u|U)?'(.*)'", 1) }
104+
}
85105
}

0 commit comments

Comments
 (0)