Skip to content

Commit 6b5ad0d

Browse files
committed
Python: Detect a wider range of generated files.
1 parent d566141 commit 6b5ad0d

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

python/ql/src/semmle/python/filters/GeneratedCode.qll

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ library class GenericGeneratedFile extends GeneratedFile {
2424
(lax_generated_by(this, _) or lax_generated_from(this, _)) and dont_modify(this)
2525
or
2626
strict_generated_by(this, _) or strict_generated_from(this, _)
27+
or
28+
auto_generated(this)
2729
)
2830
}
2931

@@ -78,8 +80,15 @@ private predicate dont_modify(File f) {
7880
comment_or_docstring(f, _).regexpMatch("(?is).*\\b(Do not|Don't) (edit|modify|make changes)\\b.*")
7981
}
8082

83+
private predicate auto_generated(File f) {
84+
exists(Comment c |
85+
c.getLocation().getFile() = f and
86+
c.getText().regexpMatch("(?is)# *this +(code|file) +is +(auto(matically)?[ -]?generated|created automatically).*")
87+
)
88+
}
89+
8190

82-
/**
91+
/**
8392
* A file generated by a template engine
8493
*/
8594
abstract library class SpecificGeneratedFile extends GeneratedFile {

python/ql/test/library-tests/filters/generated/Filter.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| antlr.py | none |
12
| generic.py | tools/idna-data |
23
| sphinx.py | Sphinx |
34
| swig.py | SWIG |

python/ql/test/library-tests/filters/generated/Filter.ql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
import python
33
import semmle.python.filters.GeneratedCode
44

5-
from GeneratedFile f
6-
select f.toString(), f.getTool()
5+
from GeneratedFile f, string tool
6+
where
7+
tool = f.getTool()
8+
or
9+
not exists(f.getTool()) and tool = "none"
10+
select f.toString(), tool
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# $ANTLR 3.0.1 C.g 2010-02-23 09:58:53
2+
3+
from antlr3 import *
4+
from antlr3.compat import set, frozenset
5+
6+
## @file
7+
# The file defines the parser for C source files.
8+
#
9+
# THIS FILE IS AUTO-GENERATED.
10+
#
11+
##

0 commit comments

Comments
 (0)