Skip to content

Commit eac0acd

Browse files
committed
Refactor NumericLiteral and UserDefinedLiteral classes for improved clarity and functionality
1 parent 767faec commit eac0acd

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ module Cpp14Literal {
1111
abstract class NumericLiteral extends StandardLibrary::Literal {
1212
NumericLiteral() {
1313
// exclude user-defined literals as they define custom suffixes
14-
not exists(StandardLibrary::FunctionCall fc |
15-
this = fc.getArgument(0) and
16-
fc.getTarget() instanceof UserDefinedLiteral
17-
) and
18-
// exclude literals derived from template instantiations
19-
not this.isFromTemplateInstantiation(_)
14+
not this instanceof UserDefinedLiteral
2015
}
2116
}
2217

cpp/common/src/codingstandards/cpp/UserDefinedLiteral.qll

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import cpp
66

7-
class UserDefinedLiteral extends Function {
8-
UserDefinedLiteral() {
7+
class UserDefinedLiteralDeclaration extends Function {
8+
UserDefinedLiteralDeclaration() {
99
// We use the '?' in this regexp because CodeQL CLI 2.4.6 and earlier reported these operators
1010
// using a single ", i.e `operator "`. This has been fixed in 2.5.9 (at the latest), but I
1111
// don't know if upgraded older databases will still have the broken version in. I've therefore
@@ -16,3 +16,12 @@ class UserDefinedLiteral extends Function {
1616
/** Holds if `this` has a compliant suffix. */
1717
predicate hasCompliantSuffix() { this.getName().regexpMatch("operator \"\"?_\\p{Alpha}+") }
1818
}
19+
20+
class UserDefinedLiteral extends Literal {
21+
UserDefinedLiteral() {
22+
exists(FunctionCall fc |
23+
this = fc.getArgument(0) and
24+
fc.getTarget() instanceof UserDefinedLiteralDeclaration
25+
)
26+
}
27+
}

cpp/common/src/codingstandards/cpp/rules/unsignedintegerliteralsnotappropriatelysuffixed/UnsignedIntegerLiteralsNotAppropriatelySuffixed.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ query predicate problems(Cpp14Literal::NumericLiteral nl, string message) {
2626
nl.getType().getUnspecifiedType().(IntegralType).isUnsigned() and
2727
// The literal already has a `u` or `U` suffix.
2828
not nl.getValueText().regexpMatch(".*[lL]*[uU][lL]*") and
29+
// exclude literals derived from template instantiations
30+
not nl.isFromTemplateInstantiation(_) and
2931
message = literalKind + " literal is an unsigned integer but does not include a 'U' suffix."
3032
)
3133
}

0 commit comments

Comments
 (0)