Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deviations on next line and multiple lines #807

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f3a6c3f
Deviations: remove no longer required query
lcartey Dec 2, 2024
7b2a2e0
Add library for more complex code identifier deviations
lcartey Dec 3, 2024
f173072
Deviations: Integrate extended code-identifier deviations
lcartey Dec 3, 2024
e36828d
Deviations: Add test cases for new code-identifier deviations
lcartey Dec 3, 2024
e077935
Update user manual for new code identifier deviations
lcartey Dec 3, 2024
6e68fb8
Deviations: Support an attribute like comment syntax
lcartey Jan 23, 2025
1a24541
Deviations: Support C/C++ attributes
lcartey Jan 28, 2025
4283652
Deviations: Switch to new deviations format
lcartey Feb 10, 2025
c65f635
Remove deviation suppression query tests
lcartey Feb 10, 2025
f56fa0f
Detect invalid deviation markers
lcartey Feb 12, 2025
bf62d9a
Merge branch 'main' into lcartey/extend-deviations
lcartey Feb 12, 2025
5a6d7ca
Deviations: Support attribute inheritence
lcartey Feb 13, 2025
e38e416
Reformatting test file
lcartey Feb 13, 2025
9f35565
Add change note, update manual.
lcartey Feb 13, 2025
d2f8670
Reformat file
lcartey Feb 13, 2025
0de32d3
Update expected results
lcartey Feb 13, 2025
029537d
Fix typos
lcartey Feb 18, 2025
3f1997b
Deviations: Highlight invalid starts
lcartey Feb 18, 2025
665a7d1
Improve documentation
lcartey Feb 18, 2025
8437c7c
Deviation: Refactor begin/end for clarity.
lcartey Feb 18, 2025
20669c7
Deviation: Clarification in user manual.
lcartey Feb 18, 2025
9e35e59
Deviations: use getADeviationRecord
lcartey Feb 18, 2025
b273d0f
Deviations: Update expected tests
lcartey Feb 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Deviations: Integrate extended code-identifier deviations
This ties in the code-identifier deviation support to the deviations
and exclusions libraries.
  • Loading branch information
lcartey committed Dec 3, 2024
commit f1730722ed98a0c7df7b27b755a43d27beb97302
21 changes: 8 additions & 13 deletions cpp/common/src/codingstandards/cpp/Exclusions.qll
Original file line number Diff line number Diff line change
@@ -35,19 +35,14 @@ predicate isExcluded(Element e, Query query, string reason) {
) and
reason = "Query has an associated deviation record for the element's file."
or
// The element is on the same line as a suppression comment
exists(Comment c |
c = dr.getACodeIdentifierComment() and
query = dr.getQuery()
|
exists(string filepath, int endLine |
// Comment occurs on the same line as the end line of the element
e.getLocation().hasLocationInfo(filepath, _, _, endLine, _) and
c.getLocation().hasLocationInfo(filepath, endLine, _, _, _)
)
) and
reason =
"Query has an associated deviation record with a code identifier that is applied to the element."
// The element is annotated by a code identifier that deviates this rule
exists(CodeIdentifierDeviation deviationInCode |
dr.getQuery() = query and
deviationInCode = dr.getACodeIdentifierDeviation() and
deviationInCode.isElementMatching(e) and
reason =
"Query has an associated deviation record with a code identifier that is applied to the element."
)
)
or
// The effective category of the query is 'Disapplied'.
29 changes: 3 additions & 26 deletions cpp/common/src/codingstandards/cpp/deviations/Deviations.qll
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import cpp
import semmle.code.cpp.XML
import codingstandards.cpp.exclusions.RuleMetadata
import codingstandards.cpp.Config
import CodeIdentifierDeviation

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

/** Gets a comment which starts or ends with the code identifier comment. */
Comment getACodeIdentifierComment() {
exists(string text |
(
result instanceof CppStyleComment and
// strip the beginning slashes
text = result.getContents().suffix(2).trim()
or
result instanceof CStyleComment and
// strip both the beginning /* and the end */ the comment
exists(string text0 |
text0 = result.getContents().suffix(2) and
text = text0.prefix(text0.length() - 2).trim()
) and
// The /* */ comment must be a single-line comment
not text.matches("%\n%")
) and
(
// Code identifier appears at the start of the comment (modulo whitespace)
text.prefix(getCodeIdentifier().length()) = getCodeIdentifier()
or
// Code identifier appears at the end of the comment (modulo whitespace)
text.suffix(text.length() - getCodeIdentifier().length()) = getCodeIdentifier()
)
)
}
/** Gets a code identifier deviation in code which starts or ends with the code identifier comment. */
CodeIdentifierDeviation getACodeIdentifierDeviation() { this = result.getDeviationRecord() }

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