Skip to content

Commit c401e20

Browse files
authored
Merge pull request #1209 from castler/fix-rule-5-13-4-unkown-suffix-in-macro-expension
Rule 5.13.4: Remove false positives in macro expansions
2 parents ee8a78d + 5103cb2 commit c401e20

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `RULE-5-13-4`, `M2-13-3` - `UnsignedIntegerLiteralsNotAppropriatelySuffixed.ql`, `MissingUSuffix.ql`:
2+
- Remove false positives for literals in macro expansions (e.g. `0U`), as the suffix information is lost in this case. This is consistent with `RULE-7-2`.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ 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 generated by macro expansions, because the suffix information is lost in
30+
// this case, so can cause false positives.
31+
not nl.isInMacroExpansion() and
2932
// exclude literals derived from template instantiations
3033
not nl.isFromTemplateInstantiation(_) and
3134
message = literalKind + " literal is an unsigned integer but does not include a 'U' suffix."

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,11 @@ template <typename T> unsigned long long instantiated_literal_exclusion() {
560560
void test_instantiated_literal_exclusion() {
561561
instantiated_literal_exclusion<int>();
562562
}
563+
564+
#define TEST_MACRO(value) (value)
565+
566+
void test_literals_in_macro_argument() {
567+
TEST_MACRO(1U == 0U); // COMPLIANT
568+
TEST_MACRO(020000000000); // NON_COMPLIANT[FALSE_NEGATIVE] - cannot determine
569+
// suffix in macro expansions
570+
}

0 commit comments

Comments
 (0)