Skip to content

Commit f173072

Browse files
committed
Deviations: Integrate extended code-identifier deviations
This ties in the code-identifier deviation support to the deviations and exclusions libraries.
1 parent 7b2a2e0 commit f173072

File tree

2 files changed

+11
-39
lines changed

2 files changed

+11
-39
lines changed

cpp/common/src/codingstandards/cpp/Exclusions.qll

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,14 @@ predicate isExcluded(Element e, Query query, string reason) {
3535
) and
3636
reason = "Query has an associated deviation record for the element's file."
3737
or
38-
// The element is on the same line as a suppression comment
39-
exists(Comment c |
40-
c = dr.getACodeIdentifierComment() and
41-
query = dr.getQuery()
42-
|
43-
exists(string filepath, int endLine |
44-
// Comment occurs on the same line as the end line of the element
45-
e.getLocation().hasLocationInfo(filepath, _, _, endLine, _) and
46-
c.getLocation().hasLocationInfo(filepath, endLine, _, _, _)
47-
)
48-
) and
49-
reason =
50-
"Query has an associated deviation record with a code identifier that is applied to the element."
38+
// The element is annotated by a code identifier that deviates this rule
39+
exists(CodeIdentifierDeviation deviationInCode |
40+
dr.getQuery() = query and
41+
deviationInCode = dr.getACodeIdentifierDeviation() and
42+
deviationInCode.isElementMatching(e) and
43+
reason =
44+
"Query has an associated deviation record with a code identifier that is applied to the element."
45+
)
5146
)
5247
or
5348
// The effective category of the query is 'Disapplied'.

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import cpp
88
import semmle.code.cpp.XML
99
import codingstandards.cpp.exclusions.RuleMetadata
1010
import codingstandards.cpp.Config
11+
import CodeIdentifierDeviation
1112

1213
predicate applyDeviationsAtQueryLevel() {
1314
not exists(CodingStandardsReportDeviatedAlerts reportDeviatedResults |
@@ -219,32 +220,8 @@ class DeviationRecord extends XmlElement {
219220
else result = getADeviationPermit().getCodeIdentifier()
220221
}
221222

222-
/** Gets a comment which starts or ends with the code identifier comment. */
223-
Comment getACodeIdentifierComment() {
224-
exists(string text |
225-
(
226-
result instanceof CppStyleComment and
227-
// strip the beginning slashes
228-
text = result.getContents().suffix(2).trim()
229-
or
230-
result instanceof CStyleComment and
231-
// strip both the beginning /* and the end */ the comment
232-
exists(string text0 |
233-
text0 = result.getContents().suffix(2) and
234-
text = text0.prefix(text0.length() - 2).trim()
235-
) and
236-
// The /* */ comment must be a single-line comment
237-
not text.matches("%\n%")
238-
) and
239-
(
240-
// Code identifier appears at the start of the comment (modulo whitespace)
241-
text.prefix(getCodeIdentifier().length()) = getCodeIdentifier()
242-
or
243-
// Code identifier appears at the end of the comment (modulo whitespace)
244-
text.suffix(text.length() - getCodeIdentifier().length()) = getCodeIdentifier()
245-
)
246-
)
247-
}
223+
/** Gets a code identifier deviation in code which starts or ends with the code identifier comment. */
224+
CodeIdentifierDeviation getACodeIdentifierDeviation() { this = result.getDeviationRecord() }
248225

249226
/** Gets the `rule-id` specified for this record, if any. */
250227
private string getRawRuleId() { result = getAChild("rule-id").getTextValue() }

0 commit comments

Comments
 (0)