|
| 1 | +/** |
| 2 | + * @name Alert suppression using annotations |
| 3 | + * @description Generates information about alert suppressions |
| 4 | + * using 'SuppressWarnings' annotations. |
| 5 | + * @kind alert-suppression |
| 6 | + * @id java/alert-suppression-annotations |
| 7 | + */ |
| 8 | + |
| 9 | +import java |
| 10 | +import Metrics.Internal.Extents |
| 11 | + |
| 12 | +/** Gets the LGTM suppression annotation text in the string `s`, if any. */ |
| 13 | +bindingset[s] |
| 14 | +string getAnnotationText(string s) { |
| 15 | + // match `lgtm[...]` anywhere in the comment |
| 16 | + result = s.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _) |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * An alert suppression annotation. |
| 21 | + */ |
| 22 | +class SuppressionAnnotation extends SuppressWarningsAnnotation { |
| 23 | + string text; |
| 24 | + |
| 25 | + SuppressionAnnotation() { |
| 26 | + text = this.getASuppressedWarningLiteral().getValue() and |
| 27 | + exists(getAnnotationText(text)) |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Gets the text of this suppression annotation. |
| 32 | + */ |
| 33 | + string getText() { result = text } |
| 34 | + |
| 35 | + private Annotation getASiblingAnnotation() { |
| 36 | + result = getAnnotatedElement().(Annotatable).getAnAnnotation() and |
| 37 | + (getAnnotatedElement() instanceof Callable or getAnnotatedElement() instanceof RefType) |
| 38 | + } |
| 39 | + |
| 40 | + private Annotation firstAnnotation() { |
| 41 | + result = min(this.getASiblingAnnotation() as m |
| 42 | + order by |
| 43 | + m.getLocation().getStartLine(), m.getLocation().getStartColumn() |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Holds if this annotation applies to the range from column `startcolumn` of line `startline` |
| 49 | + * to column `endcolumn` of line `endline` in file `filepath`. |
| 50 | + */ |
| 51 | + predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) { |
| 52 | + if firstAnnotation().hasLocationInfo(filepath, _, _, _, _) |
| 53 | + then |
| 54 | + getAnnotatedElement().hasLocationInfo(filepath, _, _, endline, endcolumn) and |
| 55 | + firstAnnotation().hasLocationInfo(filepath, startline, startcolumn, _, _) |
| 56 | + else getAnnotatedElement().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) |
| 57 | + } |
| 58 | + |
| 59 | + /** Gets the scope of this suppression. */ |
| 60 | + SuppressionScope getScope() { this = result.getSuppressionAnnotation() } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * The scope of an alert suppression annotation. |
| 65 | + */ |
| 66 | +class SuppressionScope extends @annotation { |
| 67 | + SuppressionScope() { this instanceof SuppressionAnnotation } |
| 68 | + |
| 69 | + /** Gets a suppression annotation with this scope. */ |
| 70 | + SuppressionAnnotation getSuppressionAnnotation() { result = this } |
| 71 | + |
| 72 | + /** |
| 73 | + * Holds if this element is at the specified location. |
| 74 | + * The location spans column `startcolumn` of line `startline` to |
| 75 | + * column `endcolumn` of line `endline` in file `filepath`. |
| 76 | + * For more information, see |
| 77 | + * [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html). |
| 78 | + */ |
| 79 | + predicate hasLocationInfo( |
| 80 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 81 | + ) { |
| 82 | + this.(SuppressionAnnotation).covers(filepath, startline, startcolumn, endline, endcolumn) |
| 83 | + } |
| 84 | + |
| 85 | + /** Gets a textual representation of this element. */ |
| 86 | + string toString() { result = "suppression range" } |
| 87 | +} |
| 88 | + |
| 89 | +from SuppressionAnnotation c, string text, string annotationText |
| 90 | +where |
| 91 | + text = c.getText() and |
| 92 | + annotationText = getAnnotationText(text) |
| 93 | +select c, // suppression entity |
| 94 | + text, // full text of suppression string |
| 95 | + annotationText, // LGTM suppression annotation text |
| 96 | + c.getScope() // scope of suppression |
0 commit comments