Skip to content

Commit e03fe0f

Browse files
committed
Add ClassifyFiles.ql
1 parent c2ec640 commit e03fe0f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/** Provides classes for detecting generated code. */
2+
3+
private import ruby
4+
private import codeql.ruby.ast.internal.TreeSitter
5+
6+
/** A source file that contains generated code. */
7+
abstract class GeneratedCodeFile extends RubyFile { }
8+
9+
/** A file contining comments suggesting it contains generated code. */
10+
class GeneratedCommentFile extends GeneratedCodeFile {
11+
GeneratedCommentFile() { this = any(GeneratedCodeComment c).getLocation().getFile() }
12+
}
13+
14+
/** A comment line that indicates generated code. */
15+
abstract class GeneratedCodeComment extends Ruby::Comment { }
16+
17+
/**
18+
* A generic comment line that suggests that the file is generated.
19+
*/
20+
class GenericGeneratedCodeComment extends GeneratedCodeComment {
21+
GenericGeneratedCodeComment() {
22+
exists(string line, string entity, string was, string automatically | line = getValue() |
23+
entity = "file|class|art[ei]fact|module|script" and
24+
was = "was|is|has been" and
25+
automatically = "automatically |mechanically |auto[- ]?" and
26+
line.regexpMatch("(?i).*\\bThis (" + entity + ") (" + was + ") (" + automatically +
27+
")?generated\\b.*")
28+
)
29+
}
30+
}
31+
32+
/** A comment warning against modifications. */
33+
class DontModifyMarkerComment extends GeneratedCodeComment {
34+
DontModifyMarkerComment() {
35+
exists(string line | line = getValue() |
36+
line.regexpMatch("(?i).*\\bGenerated by\\b.*\\bDo not edit\\b.*") or
37+
line.regexpMatch("(?i).*\\bAny modifications to this file will be lost\\b.*")
38+
)
39+
}
40+
}
41+
42+
/** Holds if `file` looks like it contains generated code. */
43+
predicate isGeneratedCode(GeneratedCodeFile file) { any() }

ql/src/filters/ClassifyFiles.ql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @name Classify files
3+
* @description This query produces a list of all files in a database
4+
* that are classified as generated code or test code.
5+
*
6+
* Used by LGTM.
7+
* @kind file-classifier
8+
* @id rb/file-classifier
9+
*/
10+
11+
import ruby
12+
import codeql.ruby.filters.GeneratedCode
13+
14+
predicate classify(File f, string category) {
15+
f instanceof GeneratedCodeFile and category = "generated"
16+
}
17+
18+
from File f, string category
19+
where classify(f, category)
20+
select f, category

0 commit comments

Comments
 (0)