Skip to content

Commit 08a8b49

Browse files
authored
Merge branch 'main' into mbaluda-next
2 parents 2927415 + 562ad8e commit 08a8b49

9 files changed

Lines changed: 61 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- `RULE-5-13-4`, `M2-13-3` - `UnsignedIntegerLiteralsNotAppropriatelySuffixed.ql`, `MissingUSuffix.ql`:
2+
- Remove FPs in user-defined literals and template instantiations.
3+
- `M2-13-3` - `cpp/autosar/missing-u-suffix`:
4+
- Remove FPs in user-defined literals and template instantiations.
5+

cpp/autosar/src/rules/A13-1-2/UserDefinedLiteralOperatorSuffixViolation.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import cpp
1717
import codingstandards.cpp.autosar
1818
import codingstandards.cpp.UserDefinedLiteral as udl
1919

20-
from udl::UserDefinedLiteral udl
20+
from udl::UserDefinedLiteralDeclaration udl
2121
where
2222
not isExcluded(udl, NamingPackage::userDefinedLiteralOperatorSuffixViolationQuery()) and
2323
not udl.hasCompliantSuffix()

cpp/autosar/src/rules/A13-1-3/UserDefinedLiteralsOperatorsShallNotHaveSideEffects.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import codingstandards.cpp.UserDefinedLiteral as udl
1818
import codingstandards.cpp.SideEffect
1919
import codingstandards.cpp.sideeffect.DefaultEffects
2020

21-
from udl::UserDefinedLiteral udl, SideEffect e
21+
from udl::UserDefinedLiteralDeclaration udl, SideEffect e
2222
where
2323
not isExcluded(udl,
2424
SideEffects2Package::userDefinedLiteralsOperatorsShallNotHaveSideEffectsQuery()) and

cpp/autosar/src/rules/A13-1-3/UserDefinedLiteralsOperatorsShallOnlyPerformConversionOfPassedParameters.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import codingstandards.cpp.autosar
1919
import codingstandards.cpp.UserDefinedLiteral as udl
2020
import codingstandards.cpp.SideEffect
2121

22-
from udl::UserDefinedLiteral udl, Expr retExpr
22+
from udl::UserDefinedLiteralDeclaration udl, Expr retExpr
2323
where
2424
not isExcluded(udl,
2525
SideEffects2Package::userDefinedLiteralsOperatorsShallOnlyPerformConversionOfPassedParametersQuery()) and

cpp/cert/src/rules/DCL51-CPP/UseOfReservedLiteralSuffixIdentifier.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import cpp
2222
import codingstandards.cpp.cert
2323
import codingstandards.cpp.UserDefinedLiteral as udl
2424

25-
from udl::UserDefinedLiteral udl
25+
from udl::UserDefinedLiteralDeclaration udl
2626
where
2727
not isExcluded(udl, NamingPackage::useOfReservedLiteralSuffixIdentifierQuery()) and
2828
not udl.hasCompliantSuffix()

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
*/
66
module Cpp14Literal {
77
private import cpp as StandardLibrary
8+
private import codingstandards.cpp.UserDefinedLiteral
89

910
/** An numeric literal. */
10-
abstract class NumericLiteral extends StandardLibrary::Literal { }
11+
abstract class NumericLiteral extends StandardLibrary::Literal {
12+
NumericLiteral() {
13+
// exclude user-defined literals as they define custom suffixes
14+
not this instanceof UserDefinedLiteral
15+
}
16+
}
1117

1218
/** Convenience for implementing class `UnrecognizedNumericLiteral` */
1319
abstract private class RecognizedNumericLiteral extends StandardLibrary::Literal { }

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

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

55
import cpp
66

7-
class UserDefinedLiteral extends Function {
8-
UserDefinedLiteral() {
7+
/**
8+
* A user defined literal operator is a function that defines the behavior of a user defined literal.
9+
* It is declared using the `operator ""` syntax.
10+
* ```
11+
* constexpr long operator""_km(unsigned long value) {
12+
* ...
13+
* }
14+
* ```
15+
*/
16+
class UserDefinedLiteralDeclaration extends Function {
17+
UserDefinedLiteralDeclaration() {
918
// We use the '?' in this regexp because CodeQL CLI 2.4.6 and earlier reported these operators
1019
// using a single ", i.e `operator "`. This has been fixed in 2.5.9 (at the latest), but I
1120
// don't know if upgraded older databases will still have the broken version in. I've therefore
@@ -16,3 +25,18 @@ class UserDefinedLiteral extends Function {
1625
/** Holds if `this` has a compliant suffix. */
1726
predicate hasCompliantSuffix() { this.getName().regexpMatch("operator \"\"?_\\p{Alpha}+") }
1827
}
28+
29+
/**
30+
* A user defined literal is a literal that is passed as an argument to a call to a user defined literal operator.
31+
* ```
32+
* 1000_km;
33+
* ```
34+
*/
35+
class UserDefinedLiteral extends Literal {
36+
UserDefinedLiteral() {
37+
exists(FunctionCall fc |
38+
this = fc.getArgument(0) and
39+
fc.getTarget() instanceof UserDefinedLiteralDeclaration
40+
)
41+
}
42+
}

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
}

cpp/common/test/rules/unsignedintegerliteralsnotappropriatelysuffixed/test.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,20 @@ void test_macro() {
543543
COMPLIANT_VAL; // COMPLIANT
544544
NON_COMPLIANT_VAL; // NON_COMPLIANT[FALSE_NEGATIVE] - cannot determine suffix
545545
// in macro expansions
546-
}
546+
}
547+
548+
constexpr unsigned long long operator""_km(unsigned long long value) {
549+
return value;
550+
}
551+
552+
void test_user_defined_literal_exclusion() {
553+
0x80000000_km; // COMPLIANT - user-defined literal argument should be excluded
554+
}
555+
556+
template <typename T> unsigned long long instantiated_literal_exclusion() {
557+
return T{}; // COMPLIANT - template instantiation should be excluded
558+
}
559+
560+
void test_instantiated_literal_exclusion() {
561+
instantiated_literal_exclusion<int>();
562+
}

0 commit comments

Comments
 (0)