Skip to content

Commit c25018f

Browse files
committed
Re-add DeviationsSuppression.ql
This is required for our reporting scripts. Changes implemented to support reporting of locations and messages for new deviation formats.
1 parent 1e4011f commit c25018f

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

Diff for: cpp/common/src/codingstandards/cpp/deviations/CodeIdentifierDeviation.qll

+44-5
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,20 @@ class DeviationAttribute extends StdAttribute {
226226

227227
DeviationRecord getADeviationRecord() { result = record }
228228

229-
pragma[nomagic]
230-
Element getASuppressedElement() {
229+
/** Gets the element to which this attribute was applied. */
230+
Element getPrimarySuppressedElement() {
231231
result.(Type).getAnAttribute() = this
232232
or
233233
result.(Stmt).getAnAttribute() = this
234234
or
235235
result.(Variable).getAnAttribute() = this
236236
or
237237
result.(Function).getAnAttribute() = this
238+
}
239+
240+
pragma[nomagic]
241+
Element getASuppressedElement() {
242+
result = this.getPrimarySuppressedElement()
238243
or
239244
result.(Expr).getEnclosingStmt() = this.getASuppressedElement()
240245
or
@@ -336,26 +341,60 @@ class CodeIdentifierDeviation extends TCodeIndentifierDeviation {
336341
)
337342
}
338343

344+
predicate hasLocationInfo(
345+
string filepath, int suppressedLine, int suppressedColumn, int endline, int endcolumn
346+
) {
347+
exists(Comment commentMarker |
348+
this = TSingleLineDeviation(_, commentMarker, filepath, suppressedLine) and
349+
suppressedColumn = 1 and
350+
endline = suppressedLine
351+
|
352+
if commentMarker instanceof DeviationEndOfLineMarker
353+
then endcolumn = commentMarker.(DeviationEndOfLineMarker).getLocation().getEndColumn()
354+
else
355+
// Find the last column for a location on the next line
356+
endcolumn =
357+
max(Location l |
358+
l.hasLocationInfo(filepath, _, _, _, _) and
359+
l.getEndLine() = suppressedLine
360+
|
361+
l.getEndColumn()
362+
)
363+
)
364+
or
365+
this = TMultiLineDeviation(_, _, _, filepath, suppressedLine, endline) and
366+
suppressedColumn = 1 and
367+
endcolumn = 1
368+
or
369+
exists(DeviationAttribute attribute |
370+
this = TCodeIdentifierDeviation(_, attribute) and
371+
attribute
372+
.getPrimarySuppressedElement()
373+
.getLocation()
374+
.hasLocationInfo(filepath, suppressedLine, suppressedColumn, endline, endcolumn)
375+
)
376+
}
377+
339378
string toString() {
340379
exists(string filepath |
341380
exists(int suppressedLine |
342381
this = TSingleLineDeviation(_, _, filepath, suppressedLine) and
343382
result =
344-
"Deviation record " + getADeviationRecord() + " applied to " + filepath + " Line " +
383+
"Deviation of " + getADeviationRecord().getQuery() + " applied to " + filepath + " Line " +
345384
suppressedLine
346385
)
347386
or
348387
exists(int suppressedStartLine, int suppressedEndLine |
349388
this = TMultiLineDeviation(_, _, _, filepath, suppressedStartLine, suppressedEndLine) and
350389
result =
351-
"Deviation record " + getADeviationRecord() + " applied to " + filepath + " Line" +
390+
"Deviation of " + getADeviationRecord().getQuery() + " applied to " + filepath + " Line " +
352391
suppressedStartLine + ":" + suppressedEndLine
353392
)
354393
)
355394
or
356395
exists(DeviationAttribute attribute |
357396
this = TCodeIdentifierDeviation(_, attribute) and
358-
result = "Deviation record " + getADeviationRecord() + " applied to " + attribute
397+
result = "Deviation of " + getADeviationRecord().getQuery() + " applied to " + attribute
359398
)
360399
}
361400
}

Diff for: cpp/common/src/codingstandards/cpp/deviations/DeviationsSuppression.ql

+15-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ newtype TDeviationScope =
3838
file.getRelativePath().prefix(deviationPath.length()) = deviationPath
3939
)
4040
} or
41-
TDeviationRecordCommentScope(DeviationRecord dr, Comment c) { c = dr.getACodeIdentifierComment() }
41+
TDeviationRecordCodeIdentiferDeviationScope(DeviationRecord dr, CodeIdentifierDeviation c) {
42+
c = dr.getACodeIdentifierDeviation()
43+
}
4244

4345
/** A deviation scope. */
4446
class DeviationScope extends TDeviationScope {
@@ -91,10 +93,16 @@ class DeviationRecordFileScope extends DeviationScope, TDeviationRecordFileScope
9193
* A deviation scope derived from a comment corresponding to a "code-identifier" entry for a
9294
* `DeviationRecord`.
9395
*/
94-
class DeviationRecordCommentScope extends DeviationScope, TDeviationRecordCommentScope {
95-
private DeviationRecord getDeviationRecord() { this = TDeviationRecordCommentScope(result, _) }
96+
class DeviationRecordCommentScope extends DeviationScope,
97+
TDeviationRecordCodeIdentiferDeviationScope
98+
{
99+
private DeviationRecord getDeviationRecord() {
100+
this = TDeviationRecordCodeIdentiferDeviationScope(result, _)
101+
}
96102

97-
private Comment getComment() { this = TDeviationRecordCommentScope(_, result) }
103+
private CodeIdentifierDeviation getCodeIdentifierDeviation() {
104+
this = TDeviationRecordCodeIdentiferDeviationScope(_, result)
105+
}
98106

99107
override Locatable getDeviationDefinitionLocation() { result = getDeviationRecord() }
100108

@@ -103,14 +111,11 @@ class DeviationRecordCommentScope extends DeviationScope, TDeviationRecordCommen
103111
override predicate hasLocationInfo(
104112
string filepath, int startline, int startcolumn, int endline, int endcolumn
105113
) {
106-
getComment().getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and
107-
startcolumn = 1
114+
getCodeIdentifierDeviation()
115+
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
108116
}
109117

110-
override string toString() {
111-
result =
112-
"Deviation of " + getDeviationRecord().getQuery() + " for comment " + getComment() + "."
113-
}
118+
override string toString() { result = getCodeIdentifierDeviation().toString() }
114119
}
115120

116121
from DeviationScope deviationScope

Diff for: cpp/common/test/deviations/deviations_report_deviated/DeviationsSuppression.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| file://:0:0:0:0 | (no string representation) | // lgtm[cpp/autosar/type-long-double-used] | lgtm[cpp/autosar/type-long-double-used] | main.cpp:12:1:12:58 | Deviation of cpp/autosar/type-long-double-used for comment // a-0-4-2-deviation COMPLIANT[DEVIATED]. |
1+
| file://:0:0:0:0 | (no string representation) | // lgtm[cpp/autosar/type-long-double-used] | lgtm[cpp/autosar/type-long-double-used] | main.cpp:12:1:12:58 | Deviation of cpp/autosar/type-long-double-used applied to main.cpp Line 12 |
22
| file://:0:0:0:0 | (no string representation) | // lgtm[cpp/autosar/unused-return-value] | lgtm[cpp/autosar/unused-return-value] | nested/nested2/test2.h:1:1:6:1 | Deviation of cpp/autosar/unused-return-value for nested/nested2/test2.h. |
33
| file://:0:0:0:0 | (no string representation) | // lgtm[cpp/autosar/useless-assignment] | lgtm[cpp/autosar/useless-assignment] | coding-standards.xml:1:1:17:19 | Deviation of cpp/autosar/useless-assignment for coding-standards.xml. |
44
| file://:0:0:0:0 | (no string representation) | // lgtm[cpp/autosar/useless-assignment] | lgtm[cpp/autosar/useless-assignment] | main.cpp:1:1:14:1 | Deviation of cpp/autosar/useless-assignment for main.cpp. |

0 commit comments

Comments
 (0)