JS: Avoid scanning individual comment lines to find generated code markers

Some subclasses of GeneratedCodeMarkerComment regex match against `getLine(_)`.
When evaluated, this results in multiple scans (one per subclass that uses it)
of all comment lines in the database, before regex matching against those lines.

To make these scans smaller, regex match against the entire comment text
without splitting them into lines.
This is achieved using `?m` (multiline) and line boundaries in the regexes.
This commit is contained in:
Aditya Sharad
2021-12-03 19:01:14 -08:00
parent c9a87234ef
commit 6a1aea740f

View File

@@ -49,14 +49,14 @@ private predicate codeGeneratorMarkerComment(Comment c, string tool) {
*/
private class GenericGeneratedCodeMarkerComment extends GeneratedCodeMarkerComment {
GenericGeneratedCodeMarkerComment() {
exists(string line | line = getLine(_) |
exists(string entity, string was, string automatically |
entity = "code|file|class|interface|art[ei]fact|module|script" and
was = "was|is|has been" and
automatically = "automatically |mechanically |auto[- ]?" and
line.regexpMatch("(?i).*\\b(This|The following) (" + entity + ") (" + was + ") (" +
automatically + ")?gener(e?)ated\\b.*")
)
exists(string entity, string was, string automatically |
entity = "code|file|class|interface|art[ei]fact|module|script" and
was = "was|is|has been" and
automatically = "automatically |mechanically |auto[- ]?" and
// Look for this pattern in each line of the comment.
this.getText()
.regexpMatch("(?im)^.*\\b(This|The following) (" + entity + ") (" + was + ") (" +
automatically + ")?gener(e?)ated\\b.*$")
)
}
}
@@ -66,9 +66,14 @@ private class GenericGeneratedCodeMarkerComment extends GeneratedCodeMarkerComme
*/
private class DontModifyMarkerComment extends GeneratedCodeMarkerComment {
DontModifyMarkerComment() {
exists(string line | line = getLine(_) |
line.regexpMatch("(?i).*\\bGenerated by\\b.*\\bDo not edit\\b.*") or
line.regexpMatch("(?i).*\\bAny modifications to this file will be lost\\b.*")
exists(string pattern |
// Look for these patterns in each line of the comment.
this.getText().regexpMatch(pattern) and
pattern =
[
"(?im)^.*\\bGenerated by\\b.*\\bDo not edit\\b.*$",
"(?im)^.*\\bAny modifications to this file will be lost\\b.*$"
]
)
}
}