Skip to content

Commit e3c9d28

Browse files
Add more info to query result strings
1 parent 66d2344 commit e3c9d28

10 files changed

+89
-57
lines changed

c/misra/src/rules/RULE-7-5/IncorrectlySizedIntegerConstantMacroArgument.ql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ where
3737
invoke.getMacro() = macro and
3838
literal = invoke.getExpr() and
3939
(
40-
not matchesSign(macro, invoke.getExpr()) and explanation = "cannot be negative"
40+
not matchesSign(macro, invoke.getExpr()) and explanation = " cannot be negative"
4141
or
42-
not matchesSize(macro, invoke.getExpr()) and explanation = "is too large for the specified type"
42+
not matchesSize(macro, invoke.getExpr()) and
43+
explanation = " is outside of the allowed range " + macro.getRangeString()
4344
)
44-
select invoke.getExpr(), "Integer constant macro value " + explanation
45+
select invoke.getExpr(), "Value provided to integer constant macro " + macro.getName() + explanation

c/misra/src/rules/RULE-7-5/IntegerConstantMacroArgumentUsesSuffix.ql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ import codingstandards.c.misra
1616
import codingstandards.cpp.IntegerConstantMacro
1717
import codingstandards.cpp.Literals
1818

19-
predicate usesSuffix(MacroInvocation invoke) {
20-
invoke.getUnexpandedArgument(0).regexpMatch(".*[uUlL]")
19+
string argumentSuffix(MacroInvocation invoke) {
20+
result = invoke.getUnexpandedArgument(0).regexpCapture(".*[^uUlL]([uUlL]+)$", 1)
2121
}
2222

23-
from MacroInvocation invoke, PossiblyNegativeLiteral argument
23+
from MacroInvocation invoke, PossiblyNegativeLiteral argument, string suffix
2424
where
2525
not isExcluded(invoke, Types2Package::integerConstantMacroArgumentUsesSuffixQuery()) and
2626
invoke.getMacro() instanceof IntegerConstantMacro and
2727
invoke.getExpr() = argument and
28-
usesSuffix(invoke)
29-
select invoke.getExpr(), "Integer constant macro arguments should not have 'u'/'l' suffix."
28+
suffix = argumentSuffix(invoke)
29+
select invoke.getExpr(),
30+
"Value suffix '" + suffix + "' is not allowed on provided argument to integer constant macro " +
31+
invoke.getMacroName() + "."

c/misra/src/rules/RULE-7-5/InvalidIntegerConstantMacroArgument.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ where
3838
invoke.getMacro() = macro and
3939
not invoke.getExpr() instanceof PossiblyNegativeLiteral and
4040
not specialMaxNegative64Exception(macro, invoke.getExpr())
41-
select invoke.getExpr(), "Integer constant macro argument must be an integer literal."
41+
select invoke.getExpr(),
42+
"Argument to integer constant macro " + macro.getName() + " must be an integer literal."

c/misra/src/rules/RULE-7-5/InvalidLiteralForIntegerConstantMacroArgument.ql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ predicate seemsBinaryLiteral(MacroInvocation invoke) {
3737
invoke.getUnexpandedArgument(0).regexpMatch("0[bB][01]+")
3838
}
3939

40+
string explainIncorrectArgument(MacroInvocation invoke) {
41+
if seemsBinaryLiteral(invoke)
42+
then result = "binary literal"
43+
else
44+
exists(PossiblyNegativeLiteral literal |
45+
literal = invoke.getExpr() and
46+
if literal.getBaseLiteral() instanceof Cpp14Literal::FloatingLiteral
47+
then result = "floating point literal"
48+
else result = "invalid literal"
49+
)
50+
}
51+
4052
from MacroInvocation invoke, PossiblyNegativeLiteral literal
4153
where
4254
not isExcluded(invoke, Types2Package::invalidLiteralForIntegerConstantMacroArgumentQuery()) and
@@ -46,4 +58,7 @@ where
4658
not validLiteralType(literal) or
4759
seemsBinaryLiteral(invoke)
4860
)
49-
select literal, "Integer constant macro arguments must be a decimal, octal, or hex integer literal."
61+
select literal,
62+
"Integer constant macro " + invoke.getMacroName() + " used with " +
63+
explainIncorrectArgument(invoke) +
64+
" argument, only decimal, octal, or hex integer literal allowed."
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
| test.c:26:13:26:15 | 256 | Integer constant macro value is too large for the specified type |
2-
| test.c:27:13:27:16 | 256 | Integer constant macro value is too large for the specified type |
3-
| test.c:28:13:28:17 | 256 | Integer constant macro value is too large for the specified type |
4-
| test.c:31:13:31:14 | - ... | Integer constant macro value cannot be negative |
5-
| test.c:32:13:32:15 | - ... | Integer constant macro value cannot be negative |
6-
| test.c:33:13:33:15 | - ... | Integer constant macro value cannot be negative |
7-
| test.c:34:13:34:17 | - ... | Integer constant macro value cannot be negative |
8-
| test.c:55:12:55:14 | 128 | Integer constant macro value is too large for the specified type |
9-
| test.c:56:12:56:15 | 128 | Integer constant macro value is too large for the specified type |
10-
| test.c:57:12:57:15 | 128 | Integer constant macro value is too large for the specified type |
11-
| test.c:61:12:61:15 | - ... | Integer constant macro value is too large for the specified type |
12-
| test.c:62:12:62:16 | - ... | Integer constant macro value is too large for the specified type |
13-
| test.c:63:12:63:16 | - ... | Integer constant macro value is too large for the specified type |
14-
| test.c:76:14:76:18 | 65536 | Integer constant macro value is too large for the specified type |
15-
| test.c:77:14:77:20 | 65536 | Integer constant macro value is too large for the specified type |
16-
| test.c:78:14:78:20 | 65536 | Integer constant macro value is too large for the specified type |
17-
| test.c:91:13:91:17 | 32768 | Integer constant macro value is too large for the specified type |
18-
| test.c:92:13:92:19 | 32768 | Integer constant macro value is too large for the specified type |
19-
| test.c:93:13:93:18 | 32768 | Integer constant macro value is too large for the specified type |
20-
| test.c:97:13:97:18 | - ... | Integer constant macro value is too large for the specified type |
21-
| test.c:98:13:98:20 | - ... | Integer constant macro value is too large for the specified type |
22-
| test.c:99:13:99:19 | - ... | Integer constant macro value is too large for the specified type |
23-
| test.c:109:14:109:24 | 4294967296 | Integer constant macro value is too large for the specified type |
24-
| test.c:110:14:110:25 | 4294967296 | Integer constant macro value is too large for the specified type |
25-
| test.c:120:13:120:22 | 2147483648 | Integer constant macro value is too large for the specified type |
26-
| test.c:121:13:121:22 | 2147483648 | Integer constant macro value is too large for the specified type |
27-
| test.c:124:13:124:23 | - ... | Integer constant macro value is too large for the specified type |
28-
| test.c:125:13:125:23 | - ... | Integer constant macro value is too large for the specified type |
1+
| test.c:13:13:13:16 | - ... | Value provided to integer constant macro UINT8_C cannot be negative |
2+
| test.c:15:13:15:18 | - ... | Value provided to integer constant macro UINT8_C cannot be negative |
3+
| test.c:30:13:30:15 | 256 | Value provided to integer constant macro UINT8_C is outside of the allowed range 0..255 |
4+
| test.c:31:13:31:16 | 256 | Value provided to integer constant macro UINT8_C is outside of the allowed range 0..255 |
5+
| test.c:32:13:32:17 | 256 | Value provided to integer constant macro UINT8_C is outside of the allowed range 0..255 |
6+
| test.c:35:13:35:14 | - ... | Value provided to integer constant macro UINT8_C cannot be negative |
7+
| test.c:36:13:36:15 | - ... | Value provided to integer constant macro UINT8_C cannot be negative |
8+
| test.c:37:13:37:15 | - ... | Value provided to integer constant macro UINT8_C cannot be negative |
9+
| test.c:38:13:38:17 | - ... | Value provided to integer constant macro UINT8_C cannot be negative |
10+
| test.c:59:12:59:14 | 128 | Value provided to integer constant macro INT8_C is outside of the allowed range -128..127 |
11+
| test.c:60:12:60:15 | 128 | Value provided to integer constant macro INT8_C is outside of the allowed range -128..127 |
12+
| test.c:61:12:61:15 | 128 | Value provided to integer constant macro INT8_C is outside of the allowed range -128..127 |
13+
| test.c:65:12:65:15 | - ... | Value provided to integer constant macro INT8_C is outside of the allowed range -128..127 |
14+
| test.c:66:12:66:16 | - ... | Value provided to integer constant macro INT8_C is outside of the allowed range -128..127 |
15+
| test.c:67:12:67:16 | - ... | Value provided to integer constant macro INT8_C is outside of the allowed range -128..127 |
16+
| test.c:80:14:80:18 | 65536 | Value provided to integer constant macro UINT16_C is outside of the allowed range 0..65535 |
17+
| test.c:81:14:81:20 | 65536 | Value provided to integer constant macro UINT16_C is outside of the allowed range 0..65535 |
18+
| test.c:82:14:82:20 | 65536 | Value provided to integer constant macro UINT16_C is outside of the allowed range 0..65535 |
19+
| test.c:95:13:95:17 | 32768 | Value provided to integer constant macro INT16_C is outside of the allowed range -32768..32767 |
20+
| test.c:96:13:96:19 | 32768 | Value provided to integer constant macro INT16_C is outside of the allowed range -32768..32767 |
21+
| test.c:97:13:97:18 | 32768 | Value provided to integer constant macro INT16_C is outside of the allowed range -32768..32767 |
22+
| test.c:101:13:101:18 | - ... | Value provided to integer constant macro INT16_C is outside of the allowed range -32768..32767 |
23+
| test.c:102:13:102:20 | - ... | Value provided to integer constant macro INT16_C is outside of the allowed range -32768..32767 |
24+
| test.c:103:13:103:19 | - ... | Value provided to integer constant macro INT16_C is outside of the allowed range -32768..32767 |
25+
| test.c:113:14:113:24 | 4294967296 | Value provided to integer constant macro UINT32_C is outside of the allowed range 0..4294967295 |
26+
| test.c:114:14:114:25 | 4294967296 | Value provided to integer constant macro UINT32_C is outside of the allowed range 0..4294967295 |
27+
| test.c:124:13:124:22 | 2147483648 | Value provided to integer constant macro INT32_C is outside of the allowed range -2147483648..2147483647 |
28+
| test.c:125:13:125:22 | 2147483648 | Value provided to integer constant macro INT32_C is outside of the allowed range -2147483648..2147483647 |
29+
| test.c:128:13:128:23 | - ... | Value provided to integer constant macro INT32_C is outside of the allowed range -2147483648..2147483647 |
30+
| test.c:129:13:129:23 | - ... | Value provided to integer constant macro INT32_C is outside of the allowed range -2147483648..2147483647 |
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
| test.c:16:13:16:14 | 1 | Integer constant macro arguments should not have 'u'/'l' suffix. |
2-
| test.c:17:13:17:14 | 2 | Integer constant macro arguments should not have 'u'/'l' suffix. |
3-
| test.c:18:13:18:14 | 3 | Integer constant macro arguments should not have 'u'/'l' suffix. |
4-
| test.c:19:13:19:14 | 4 | Integer constant macro arguments should not have 'u'/'l' suffix. |
5-
| test.c:20:13:20:15 | 5 | Integer constant macro arguments should not have 'u'/'l' suffix. |
1+
| test.c:18:13:18:14 | 1 | Value suffix 'u' is not allowed on provided argument to integer constant macro UINT8_C. |
2+
| test.c:19:13:19:14 | 2 | Value suffix 'U' is not allowed on provided argument to integer constant macro UINT8_C. |
3+
| test.c:20:13:20:14 | 3 | Value suffix 'l' is not allowed on provided argument to integer constant macro UINT8_C. |
4+
| test.c:21:13:21:14 | 4 | Value suffix 'L' is not allowed on provided argument to integer constant macro UINT8_C. |
5+
| test.c:22:13:22:15 | 5 | Value suffix 'ul' is not allowed on provided argument to integer constant macro UINT8_C. |
6+
| test.c:23:13:23:15 | 5 | Value suffix 'll' is not allowed on provided argument to integer constant macro UINT8_C. |
7+
| test.c:24:13:24:16 | 5 | Value suffix 'ull' is not allowed on provided argument to integer constant macro UINT8_C. |
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| test.c:37:13:37:17 | ... + ... | Integer constant macro argument must be an integer literal. |
2-
| test.c:38:13:38:18 | access to array | Integer constant macro argument must be an integer literal. |
3-
| test.c:39:13:39:19 | access to array | Integer constant macro argument must be an integer literal. |
4-
| test.c:152:13:152:37 | ... - ... | Integer constant macro argument must be an integer literal. |
5-
| test.c:153:13:153:47 | ... - ... | Integer constant macro argument must be an integer literal. |
1+
| test.c:41:13:41:17 | ... + ... | Argument to integer constant macro UINT8_C must be an integer literal. |
2+
| test.c:42:13:42:18 | access to array | Argument to integer constant macro UINT8_C must be an integer literal. |
3+
| test.c:43:13:43:19 | access to array | Argument to integer constant macro UINT8_C must be an integer literal. |
4+
| test.c:156:13:156:37 | ... - ... | Argument to integer constant macro INT64_C must be an integer literal. |
5+
| test.c:157:13:157:47 | ... - ... | Argument to integer constant macro INT64_C must be an integer literal. |
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
| test.c:12:13:12:15 | 1.0 | Integer constant macro arguments must be a decimal, octal, or hex integer literal. |
2-
| test.c:13:13:13:17 | 7 | Integer constant macro arguments must be a decimal, octal, or hex integer literal. |
1+
| test.c:12:13:12:15 | 1.0 | Integer constant macro UINT8_C used with floating point literal argument, only decimal, octal, or hex integer literal allowed. |
2+
| test.c:13:13:13:16 | - ... | Integer constant macro UINT8_C used with floating point literal argument, only decimal, octal, or hex integer literal allowed. |
3+
| test.c:14:13:14:17 | 7 | Integer constant macro UINT8_C used with binary literal argument, only decimal, octal, or hex integer literal allowed. |

c/misra/test/rules/RULE-7-5/test.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ uint_least8_t g1[] = {
99
UINT8_C(034), // COMPLIANT
1010

1111
// Incorrect literal types
12-
UINT8_C(1.0), // NON-COMPLIANT
13-
UINT8_C(0b111), // NON-COMPLIANT
12+
UINT8_C(1.0), // NON-COMPLIANT
13+
UINT8_C(-1.0), // NON-COMPLIANT
14+
UINT8_C(0b111), // NON-COMPLIANT
15+
UINT8_C(-0b111), // NON-COMPLIANT
1416

1517
// Suffixes disallowed
16-
UINT8_C(1u), // NON-COMPLIANT
17-
UINT8_C(2U), // NON-COMPLIANT
18-
UINT8_C(3l), // NON-COMPLIANT
19-
UINT8_C(4L), // NON-COMPLIANT
20-
UINT8_C(5ul), // NON-COMPLIANT
18+
UINT8_C(1u), // NON-COMPLIANT
19+
UINT8_C(2U), // NON-COMPLIANT
20+
UINT8_C(3l), // NON-COMPLIANT
21+
UINT8_C(4L), // NON-COMPLIANT
22+
UINT8_C(5ul), // NON-COMPLIANT
23+
UINT8_C(5ll), // NON-COMPLIANT
24+
UINT8_C(5ull), // NON-COMPLIANT
2125

2226
// Range tests
2327
UINT8_C(255), // COMPLIANT

cpp/common/src/codingstandards/cpp/IntegerConstantMacro.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ class IntegerConstantMacro extends Macro {
3232
or
3333
signed = false and result = 0
3434
}
35+
36+
string getRangeString() {
37+
result = minValue().toString() + ".." + maxValue().toString()
38+
}
3539
}

0 commit comments

Comments
 (0)