Skip to content

Commit 4283652

Browse files
committed
Deviations: Switch to new deviations format
1 parent 1a24541 commit 4283652

File tree

6 files changed

+71
-52
lines changed

6 files changed

+71
-52
lines changed

cpp/common/src/codingstandards/cpp/deviations/CodeIdentifierDeviation.qll

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
/**
2-
* A module for identifying comment markers in code that trigger deviations.
2+
* A module for identifying in code markers in code that trigger deviations.
33
*
4-
* Each comment marker consists of a `code-identifier` with some optional annotations. A deviation will be applied to
4+
* This module supports two different code identifier markers:
5+
* - A C/C++ attribute based syntax
6+
* - A comment-based format
7+
*
8+
* The C/C++ attribute based syntax uses the following format:
9+
* ```
10+
* [[codeql::<standard>_deviation("code-identifier")]]
11+
* ```
12+
* The deviation will be applied to the selected program element, and any syntactically nested children of that program element.
13+
*
14+
* For the comment format the marker consists of a `code-identifier` with some optional annotations. A deviation will be applied to
515
* some range of lines in the file containing the comment based on the annotation. The supported marker annotation
616
* formats are:
717
* - `<code-identifier>` - the deviation applies to results on the current line.
8-
* - `[[codingstandards::deviation(<code-identifier>)]]` - same as above.
9-
* - `[[codingstandards::deviation_next_line(<code-identifier>)]]` - this deviation applies to results on the next line.
10-
* - `[[codingstandards::deviation_begin(<code-identifier>)]]` - marks the beginning of a range of lines where the deviation applies.
11-
* - `[[codingstandards::deviation_end(<code-identifier>)]]` - marks the end of a range of lines where the deviation applies.
18+
* - `codeql::<standard>_deviation(<code-identifier>)` - same as above.
19+
* - `codeql::<standard>_deviation_next_line(<code-identifier>)` - this deviation applies to results on the next line.
20+
* - `codeql::<standard>_deviation_begin(<code-identifier>)` - marks the beginning of a range of lines where the deviation applies.
21+
* - `codeql::<standard>_deviation_end(<code-identifier>)` - marks the end of a range of lines where the deviation applies.
1222
*
1323
* The valid `code-identifier`s are specified in deviation records, which also specify the query whose results are
1424
* suppressed by the deviation.
@@ -23,6 +33,8 @@
2333
import cpp
2434
import Deviations
2535

36+
string supportedStandard() { result = ["misra", "autosar", "cert"] }
37+
2638
/**
2739
* Holds if the given comment contains the code identifier.
2840
*/
@@ -67,7 +79,8 @@ abstract class CommentDeviationMarker extends Comment {
6779
*/
6880
class DeviationEndOfLineMarker extends CommentDeviationMarker {
6981
DeviationEndOfLineMarker() {
70-
commentMatches(this, "[[codingstandards::deviation(" + record.getCodeIdentifier() + ")]]")
82+
commentMatches(this,
83+
"codeql::" + supportedStandard() + "_deviation(" + record.getCodeIdentifier() + ")")
7184
}
7285
}
7386

@@ -77,7 +90,7 @@ class DeviationEndOfLineMarker extends CommentDeviationMarker {
7790
class DeviationNextLineMarker extends CommentDeviationMarker {
7891
DeviationNextLineMarker() {
7992
commentMatches(this,
80-
"[[codingstandards::deviation_next_line(" + record.getCodeIdentifier() + ")]]")
93+
"codeql::" + supportedStandard() + "_deviation_next_line(" + record.getCodeIdentifier() + ")")
8194
}
8295
}
8396

@@ -91,7 +104,8 @@ abstract class CommentDeviationRangeMarker extends CommentDeviationMarker { }
91104
*/
92105
class DeviationBegin extends CommentDeviationRangeMarker {
93106
DeviationBegin() {
94-
commentMatches(this, "[[codingstandards::deviation_begin(" + record.getCodeIdentifier() + ")]]")
107+
commentMatches(this,
108+
"codeql::" + supportedStandard() + "_deviation_begin(" + record.getCodeIdentifier() + ")")
95109
}
96110
}
97111

@@ -100,7 +114,8 @@ class DeviationBegin extends CommentDeviationRangeMarker {
100114
*/
101115
class DeviationEnd extends CommentDeviationRangeMarker {
102116
DeviationEnd() {
103-
commentMatches(this, "[[codingstandards::deviation_end(" + record.getCodeIdentifier() + ")]]")
117+
commentMatches(this,
118+
"codeql::" + supportedStandard() + "_deviation_end(" + record.getCodeIdentifier() + ")")
104119
}
105120
}
106121

@@ -184,7 +199,7 @@ class DeviationAttribute extends StdAttribute {
184199
DeviationRecord record;
185200

186201
DeviationAttribute() {
187-
this.hasQualifiedName("codingstandards", "deviation") and
202+
this.hasQualifiedName("codeql", supportedStandard() + "_deviation") and
188203
// Support multiple argument deviations
189204
"\"" + record.getCodeIdentifier() + "\"" = this.getAnArgument().getValueText()
190205
}

cpp/common/test/deviations/deviations_basic_test/TypeLongDoubleUsed.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
| attribute_syntax.cpp:6:15:6:17 | dd1 | Use of long double type. |
2-
| attribute_syntax.cpp:22:15:22:17 | d10 | Use of long double type. |
3-
| attribute_syntax.cpp:30:15:30:17 | d14 | Use of long double type. |
4-
| attribute_syntax.cpp:34:20:34:22 | d16 | Use of long double type. |
2+
| attribute_syntax.cpp:21:15:21:17 | d10 | Use of long double type. |
3+
| attribute_syntax.cpp:29:15:29:17 | d14 | Use of long double type. |
4+
| attribute_syntax.cpp:33:20:33:22 | d16 | Use of long double type. |
55
| main.cpp:13:15:13:16 | d1 | Use of long double type. |
66
| main.cpp:18:15:18:16 | d4 | Use of long double type. |
77
| main.cpp:21:15:21:16 | d6 | Use of long double type. |

cpp/common/test/deviations/deviations_basic_test/UnusedReturnValue.expected

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
| attribute_syntax.cpp:5:3:5:6 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
2-
| attribute_syntax.cpp:17:5:17:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
3-
| attribute_syntax.cpp:19:5:19:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
4-
| attribute_syntax.cpp:25:5:25:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
5-
| attribute_syntax.cpp:27:5:27:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
6-
| attribute_syntax.cpp:31:3:31:6 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
7-
| attribute_syntax.cpp:42:3:42:6 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
2+
| attribute_syntax.cpp:16:5:16:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
3+
| attribute_syntax.cpp:18:5:18:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
4+
| attribute_syntax.cpp:24:5:24:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
5+
| attribute_syntax.cpp:26:5:26:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
6+
| attribute_syntax.cpp:30:3:30:6 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
7+
| attribute_syntax.cpp:41:3:41:6 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
88
| main.cpp:12:3:12:6 | call to getX | Return value from call to $@ is unused. | main.cpp:8:5:8:8 | getX | getX |
99
| main.cpp:25:3:25:6 | call to getX | Return value from call to $@ is unused. | main.cpp:8:5:8:8 | getX | getX |
1010
| main.cpp:27:3:27:6 | call to getX | Return value from call to $@ is unused. | main.cpp:8:5:8:8 | getX | getX |

cpp/common/test/deviations/deviations_basic_test/attribute_syntax.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ int alt() {
55
getZ(); // NON_COMPLIANT
66
long double dd1; // NON_COMPLIANT (A0-4-2)
77

8-
long double [[codingstandards::deviation(
9-
"a-0-4-2-deviation")]] dd3; // COMPLIANT[DEVIATED]
10-
long double [[codingstandards::deviation("a")]] dd3a; // NON_COMPLIAT
8+
long double [[codeql::autosar_deviation(
9+
"a-0-4-2-deviation")]] dd3; // COMPLIANT[DEVIATED]
1110

12-
[[codingstandards::deviation(
11+
[[codeql::autosar_deviation(
1312
"a-0-4-2-deviation")]] long double dd4; // COMPLIANT[DEVIATED]
1413

15-
[[codingstandards::deviation("a-0-4-2-deviation")]] {
14+
[[codeql::autosar_deviation("a-0-4-2-deviation")]] {
1615
long double d7; // COMPLIANT[DEVIATED]
1716
getZ(); // NON_COMPLIANT (A0-1-2)
1817
long double d8; // COMPLIANT[DEVIATED]
1918
getZ(); // NON_COMPLIANT (A0-1-2)
2019
long double d9; // COMPLIANT[DEVIATED]
2120
}
2221
long double d10; // NON_COMPLIANT (A0-4-2)
23-
[[codingstandards::deviation("a-0-4-2-deviation")]] {
22+
[[codeql::autosar_deviation("a-0-4-2-deviation")]] {
2423
long double d11; // COMPLIANT[DEVIATED]
2524
getZ(); // NON_COMPLIANT (A0-1-2)
2625
long double d12; // COMPLIANT[DEVIATED]
@@ -29,16 +28,18 @@ int alt() {
2928
}
3029
long double d14; // NON_COMPLIANT (A0-4-2)
3130
getZ(); // NON_COMPLIANT (A0-1-2)
32-
[[codingstandards::deviation("a-0-4-2-deviation")]]
31+
[[codeql::autosar_deviation("a-0-4-2-deviation")]]
3332
for (long double d15 = 0.0; true;) {} // COMPLIANT[DEVIATED]
3433
for (long double d16 = 0.0; true;) { // NON_COMPLIANT (A0-4-2)
3534
}
3635
return 0;
3736
}
3837

39-
[[codingstandards::deviation("a-0-4-2-deviation")]]
38+
[[codeql::autosar_deviation("a-0-4-2-deviation")]]
4039
int alt2() {
4140
int x = 0; // COMPLIANT[DEVIATED]
4241
getZ(); // NON_COMPLIANT
4342
long double dd1; // COMPLIANT[DEVIATED]
43+
[[codeql::autosar_deviation(
44+
"a-0-4-2-deviation")]] long double dd2; // COMPLIANT[DEVIATED]
4445
}

cpp/common/test/deviations/deviations_basic_test/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ int main(int argc, char **argv) {
1313
long double d1; // NON_COMPLIANT (A0-4-2)
1414
long double d2; // a-0-4-2-deviation COMPLIANT[DEVIATED]
1515

16-
long double d3; // [[codingstandards::deviation(a-0-4-2-deviation)]]
16+
long double d3; // codeql::autosar_deviation(a-0-4-2-deviation)
1717
// COMPLIANT[DEVIATED]
1818
long double d4; // NON_COMPLIANT (A0-4-2)
19-
// [[codingstandards::deviation_next_line(a-0-4-2-deviation)]]
19+
// codeql::autosar_deviation_next_line(a-0-4-2-deviation)
2020
long double d5; // COMPLIANT[DEVIATED]
2121
long double d6; // NON_COMPLIANT (A0-4-2)
2222

23-
// [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
23+
// codeql::autosar_deviation_begin(a-0-4-2-deviation)
2424
long double d7; // COMPLIANT[DEVIATED]
2525
getX(); // NON_COMPLIANT (A0-1-2)
2626
long double d8; // COMPLIANT[DEVIATED]
2727
getX(); // NON_COMPLIANT (A0-1-2)
2828
long double d9; // COMPLIANT[DEVIATED]
29-
// [[codingstandards::deviation_end(a-0-4-2-deviation)]]
29+
// codeql::autosar_deviation_end(a-0-4-2-deviation)
3030
long double d10; // NON_COMPLIANT (A0-4-2)
31-
// [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
31+
// codeql::autosar_deviation_begin(a-0-4-2-deviation)
3232
long double d11; // COMPLIANT[DEVIATED]
3333
getX(); // NON_COMPLIANT (A0-1-2)
3434
long double d12; // COMPLIANT[DEVIATED]
3535
getX(); // NON_COMPLIANT (A0-1-2)
3636
long double d13; // COMPLIANT[DEVIATED]
37-
// [[codingstandards::deviation_end(a-0-4-2-deviation)]]
37+
// codeql::autosar_deviation_end(a-0-4-2-deviation)
3838
long double d14; // NON_COMPLIANT (A0-4-2)
3939
getX(); // NON_COMPLIANT (A0-1-2)
4040
return 0;

docs/user_manual.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,13 @@ The `process_coding_standards_config.py` has a dependency on the package `pyyaml
426426
A code identifier specified in a deviation record can be applied to certain results in the code by adding a C or C++ attribute of the following format:
427427

428428
```
429-
[[codingstandards::deviation("code-identifier")]]
429+
[[codeql::<standard>_deviation("code-identifier")]]
430430
```
431431

432+
For example `[[codeql::misra_deviation("a1-2-4")]]` would apply a deviation of a rule in a MISRA standard, using the code identifier `a1-2-4`. The supported standard names are `misra`, `autosar` and `cert`.
433+
432434
This attribute may be added to the following program elements:
435+
433436
* Functions
434437
* Statements
435438
* Variables
@@ -440,7 +443,7 @@ Deviation attributes are inherited from parents in the code structure. For examp
440443
Multiple code identifiers may be passed in a single attribute to apply multiple deviations, for example:
441444

442445
```
443-
[[codingstandards::deviation("code-identifier-1", "code-identifier-2")]]
446+
[[codeql::misra_deviation("code-identifier-1", "code-identifier-2")]]
444447
```
445448

446449
Note - considation should be taken to ensure the use of custom attributes for deviations is compatible with your chosen language version, compiler, compiler configuration and coding standard.
@@ -461,10 +464,10 @@ If you cannot satisfy these condition, please use the deviation code identifier
461464
As an alternative to attributes, a code identifier specified in a deviation record can be applied to certain results in the code by adding a comment marker consisting of a `code-identifier` with some optional annotations. The supported marker annotation formats are:
462465

463466
- `<code-identifier>` - the deviation applies to results on the current line.
464-
- `codingstandards::deviation(<code-identifier>)` - the deviation applies to results on the current line.
465-
- `codingstandards::deviation_next_line(<code-identifier>)` - this deviation applies to results on the next line.
466-
- `codingstandards::deviation_begin(<code-identifier>)` - marks the beginning of a range of lines where the deviation applies.
467-
- `codingstandards::deviation_end(<code-identifier>)` - marks the end of a range of lines where the deviation applies.
467+
- `codeql::<standard>_deviation(<code-identifier>)` - the deviation applies to results on the current line.
468+
- `codeql::<standard>_deviation_next_line(<code-identifier>)` - this deviation applies to results on the next line.
469+
- `codeql::<standard>_deviation_begin(<code-identifier>)` - marks the beginning of a range of lines where the deviation applies.
470+
- `codeql::<standard>_deviation_end(<code-identifier>)` - marks the end of a range of lines where the deviation applies.
468471

469472
Here are some examples, using the deviation record with the `a-0-4-2-deviation` code-identifier specified above:
470473
```cpp
@@ -473,32 +476,32 @@ Here are some examples, using the deviation record with the `a-0-4-2-deviation`
473476
long double x2; // a-0-4-2-deviation - COMPLIANT
474477
long double x3; // COMPLIANT - a-0-4-2-deviation
475478
476-
long double x4; // [[codingstandards::deviation(a-0-4-2-deviation)]] - COMPLIANT
477-
long double x5; // COMPLIANT - [[codingstandards::deviation(a-0-4-2-deviation)]]
479+
long double x4; // codeql::<standard>_deviation(a-0-4-2-deviation) - COMPLIANT
480+
long double x5; // COMPLIANT - codeql::<standard>_deviation(a-0-4-2-deviation)
478481
479-
// [[codingstandards::deviation_next_line(a-0-4-2-deviation)]]
482+
// codeql::<standard>_deviation_next_line(a-0-4-2-deviation)
480483
long double x6; // COMPLIANT
481484
482-
// [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
485+
// codeql::<standard>_deviation_begin(a-0-4-2-deviation)
483486
long double x7; // COMPLIANT
484-
// [[codingstandards::deviation_end(a-0-4-2-deviation)]]
487+
// codeql::<standard>_deviation_end(a-0-4-2-deviation)
485488
```
486489

487-
`codingstandards::deviation_end` markers will pair with the closest unmatched `codingstandards::deviation_begin` for the same `code-identifier`. Consider this example:
490+
`codeql::<standard>_deviation_end` markers will pair with the closest unmatched `codeql::<standard>_deviation_begin` for the same `code-identifier`. Consider this example:
488491
```cpp
489-
1 | // [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
492+
1 | // codeql::<standard>_deviation_begin(a-0-4-2-deviation)
490493
2 |
491-
3 | // [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
494+
3 | // codeql::<standard>_deviation_begin(a-0-4-2-deviation)
492495
4 |
493-
5 | // [[codingstandards::deviation_end(a-0-4-2-deviation)]]
496+
5 | // codeql::<standard>_deviation_end(a-0-4-2-deviation)
494497
6 |
495-
7 | // [[codingstandards::deviation_end(a-0-4-2-deviation)]]
498+
7 | // codeql::<standard>_deviation_end(a-0-4-2-deviation)
496499
```
497500
Here, Line 1 will pair with Line 7, and Line 3 will pair with Line 8.
498501

499-
A `codingstandards::deviation_end` without a matching `codingstandards::deviation_begin`, or `codingstandards::deviation_begin` without a matching `codingstandards::deviation_end` is invalid and will be ignored.
502+
A `codeql::<standard>_deviation_end` without a matching `codeql::<standard>_deviation_begin`, or `codeql::<standard>_deviation_begin` without a matching `codeql::<standard>_deviation_end` is invalid and will be ignored.
500503

501-
`codingstandards::deviation_begin` and `codingstandards::deviation_end` markers only apply within a single file. Markers cannot be paired across files, and deviations do not apply to included files.
504+
`codeql::<standard>_deviation_begin` and `ccodeql::<standard>_deviation_end` markers only apply within a single file. Markers cannot be paired across files, and deviations do not apply to included files.
502505

503506
Note: deviation markers cannot be applied to the body of a macro. Please apply the deviation to macro expansion, or use the attribute deviation format.
504507

0 commit comments

Comments
 (0)