Skip to content

Commit 6e68fb8

Browse files
committed
Deviations: Support an attribute like comment syntax
1 parent e077935 commit 6e68fb8

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
* some range of lines in the file containing the comment based on the annotation. The supported marker annotation
66
* formats are:
77
* - `<code-identifier>` - the deviation applies to results on the current line.
8-
* - `DEVIATION(<code-identifier>)` - same as above.
9-
* - `DEVIATION_NEXT_LINE(<code-identifier>)` - this deviation applies to results on the next line.
10-
* - `DEVIATION_BEGIN(<code-identifier>)` - marks the beginning of a range of lines where the deviation applies.
11-
* - `DEVIATION_END(<code-identifier>)` - marks the end of a range of lines where the deviation applies.
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.
1212
*
1313
* The valid `code-identifier`s are specified in deviation records, which also specify the query whose results are
1414
* suppressed by the deviation.
@@ -53,7 +53,7 @@ private predicate commentMatches(Comment comment, string codeIdentifier) {
5353
/**
5454
* A deviation marker in the code.
5555
*/
56-
abstract class DeviationMarker extends Comment {
56+
abstract class CommentDeviationMarker extends Comment {
5757
DeviationRecord record;
5858

5959
/**
@@ -65,45 +65,50 @@ abstract class DeviationMarker extends Comment {
6565
/**
6666
* A deviation marker for a deviation that applies to the current line.
6767
*/
68-
class DeviationEndOfLineMarker extends DeviationMarker {
68+
class DeviationEndOfLineMarker extends CommentDeviationMarker {
6969
DeviationEndOfLineMarker() {
70-
commentMatches(this, "DEVIATION(" + record.getCodeIdentifier() + ")")
70+
commentMatches(this, "[[codingstandards::deviation(" + record.getCodeIdentifier() + ")]]")
7171
}
7272
}
7373

7474
/**
7575
* A deviation marker for a deviation that applies to the next line.
7676
*/
77-
class DeviationNextLineMarker extends DeviationMarker {
77+
class DeviationNextLineMarker extends CommentDeviationMarker {
7878
DeviationNextLineMarker() {
79-
commentMatches(this, "DEVIATION_NEXT_LINE(" + record.getCodeIdentifier() + ")")
79+
commentMatches(this,
80+
"[[codingstandards::deviation_next_line(" + record.getCodeIdentifier() + ")]]")
8081
}
8182
}
8283

8384
/**
8485
* A deviation marker for a deviation that applies to a range of lines
8586
*/
86-
abstract class DeviationRangeMarker extends DeviationMarker { }
87+
abstract class CommentDeviationRangeMarker extends CommentDeviationMarker { }
8788

8889
/**
8990
* A deviation marker for a deviation that begins on this line.
9091
*/
91-
class DeviationBegin extends DeviationRangeMarker {
92-
DeviationBegin() { commentMatches(this, "DEVIATION_BEGIN(" + record.getCodeIdentifier() + ")") }
92+
class DeviationBegin extends CommentDeviationRangeMarker {
93+
DeviationBegin() {
94+
commentMatches(this, "[[codingstandards::deviation_begin(" + record.getCodeIdentifier() + ")]]")
95+
}
9396
}
9497

9598
/**
9699
* A deviation marker for a deviation that ends on this line.
97100
*/
98-
class DeviationEnd extends DeviationRangeMarker {
99-
DeviationEnd() { commentMatches(this, "DEVIATION_END(" + record.getCodeIdentifier() + ")") }
101+
class DeviationEnd extends CommentDeviationRangeMarker {
102+
DeviationEnd() {
103+
commentMatches(this, "[[codingstandards::deviation_end(" + record.getCodeIdentifier() + ")]]")
104+
}
100105
}
101106

102107
private predicate hasDeviationCommentFileOrdering(
103-
DeviationRecord record, DeviationRangeMarker comment, File file, int index
108+
DeviationRecord record, CommentDeviationRangeMarker comment, File file, int index
104109
) {
105110
comment =
106-
rank[index](DeviationRangeMarker c |
111+
rank[index](CommentDeviationRangeMarker c |
107112
c.getRecord() = record and
108113
file = c.getFile()
109114
|
@@ -115,7 +120,7 @@ private predicate mkBeginStack(DeviationRecord record, File file, BeginStack sta
115120
// Stack is empty at the start
116121
index = 0 and
117122
stack = TEmptyBeginStack() and
118-
exists(DeviationRangeMarker marker |
123+
exists(CommentDeviationRangeMarker marker |
119124
marker.getRecord() = record and marker.getLocation().getFile() = file
120125
)
121126
or

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

Lines changed: 7 additions & 7 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; // DEVIATION(a-0-4-2-deviation) COMPLIANT[DEVIATED]
17-
16+
long double d3; // [[codingstandards::deviation(a-0-4-2-deviation)]]
17+
// COMPLIANT[DEVIATED]
1818
long double d4; // NON_COMPLIANT (A0-4-2)
19-
// DEVIATION_NEXT_LINE(a-0-4-2-deviation)
19+
// [[codingstandards::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-
// DEVIATION_BEGIN(a-0-4-2-deviation)
23+
// [[codingstandards::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-
// DEVIATION_END(a-0-4-2-deviation)
29+
// [[codingstandards::deviation_end(a-0-4-2-deviation)]]
3030
long double d10; // NON_COMPLIANT (A0-4-2)
31-
// DEVIATION_BEGIN(a-0-4-2-deviation)
31+
// [[codingstandards::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-
// DEVIATION_END(a-0-4-2-deviation)
37+
// [[codingstandards::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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ 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 comment marker consisting of a `code-identifier` with some optional annotations. The supported marker annotation formats are:
427427

428428
- `<code-identifier>` - the deviation applies to results on the current line.
429-
- `DEVIATION(<code-identifier>)` - the deviation applies to results on the current line.
430-
- `DEVIATION_NEXT_LINE(<code-identifier>)` - this deviation applies to results on the next line.
429+
- `codingstandards::deviation(<code-identifier>)` - the deviation applies to results on the current line.
430+
- `codingstandards::deviation_next_line(<code-identifier>)` - this deviation applies to results on the next line.
431431
- `DEVIATION_BEGIN(<code-identifier>)` - marks the beginning of a range of lines where the deviation applies.
432432
- `DEVIATION_END(<code-identifier>)` - marks the end of a range of lines where the deviation applies.
433433

@@ -438,32 +438,32 @@ Here are some examples, using the deviation record with the `a-0-4-2-deviation`
438438
long double x2; // a-0-4-2-deviation - COMPLIANT
439439
long double x3; // COMPLIANT - a-0-4-2-deviation
440440
441-
long double x4; // DEVIATION(a-0-4-2-deviation) - COMPLIANT
442-
long double x5; // COMPLIANT - DEVIATION(a-0-4-2-deviation)
441+
long double x4; // [[codingstandards::deviation(a-0-4-2-deviation)]] - COMPLIANT
442+
long double x5; // COMPLIANT - [[codingstandards::deviation(a-0-4-2-deviation)]]
443443
444-
// DEVIATION_NEXT_LINE(a-0-4-2-deviation)
444+
// [[codingstandards::deviation_next_line(a-0-4-2-deviation)]]
445445
long double x6; // COMPLIANT
446446
447-
// DEVIATION_BEGIN(a-0-4-2-deviation)
447+
// [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
448448
long double x7; // COMPLIANT
449-
// DEVIATION_END(a-0-4-2-deviation)
449+
// [[codingstandards::deviation_end(a-0-4-2-deviation)]]
450450
```
451451

452-
`DEVIATION_END` markers will pair with the closest unmatched `DEVIATION_BEGIN` for the same `code-identifier`. Consider this example:
452+
`codingstandards::deviation_end` markers will pair with the closest unmatched `codingstandards::deviation_begin` for the same `code-identifier`. Consider this example:
453453
```cpp
454-
1 | // DEVIATION_BEGIN(a-0-4-2-deviation)
454+
1 | // [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
455455
2 |
456-
3 | // DEVIATION_BEGIN(a-0-4-2-deviation)
456+
3 | // [[codingstandards::deviation_begin(a-0-4-2-deviation)]]
457457
4 |
458-
5 | // DEVIATION_END(a-0-4-2-deviation)
458+
5 | // [[codingstandards::deviation_end(a-0-4-2-deviation)]]
459459
6 |
460-
7 | // DEVIATION_END(a-0-4-2-deviation)
460+
7 | // [[codingstandards::deviation_end(a-0-4-2-deviation)]]
461461
```
462462
Here, Line 1 will pair with Line 7, and Line 3 will pair with Line 8.
463463

464-
A `DEVIATION_END` without a matching `DEVIATION_BEGIN`, or `DEVIATION_BEGIN` without a matching `DEVIATION_END` is invalid and will be ignored.
464+
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.
465465

466-
`DEVIATION_BEGIN` and `DEVIATION_END` markers only apply within a single file. Markers cannot be paired across files, and deviations do not apply to included files.
466+
`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.
467467

468468
##### Deviation permit
469469

0 commit comments

Comments
 (0)