-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathGeneratedCode.qll
More file actions
194 lines (170 loc) · 5.89 KB
/
GeneratedCode.qll
File metadata and controls
194 lines (170 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import python
import semmle.python.templates.Templates
/**
* A file that is detected as being generated.
*/
abstract class GeneratedFile extends File {
abstract string getTool();
}
/*
* We distinguish between a "lax" match which just includes "generated by" or similar versus a "strict" match which includes "this file is generated by" or similar
* "lax" matches are taken to indicate generated file if they occur at the top of a file. "strict" matches can occur anywhere.
* There is no formal reason for the above, it just seems to work well in practice.
*/
class GenericGeneratedFile extends GeneratedFile {
GenericGeneratedFile() {
not this instanceof SpecificGeneratedFile and
(
(lax_generated_by(this, _) or lax_generated_from(this, _)) and
dont_modify(this)
or
strict_generated_by(this, _)
or
strict_generated_from(this, _)
or
auto_generated(this)
)
}
override string getTool() { lax_generated_by(this, result) or strict_generated_by(this, result) }
}
pragma[nomagic]
private int minStmtLine(File file) {
result =
min(int line |
line = any(Stmt s | s.getLocation().getFile() = file).getLocation().getStartLine()
)
}
pragma[nomagic]
private predicate isCommentAfterCode(Comment c, File f) {
f = c.getLocation().getFile() and
minStmtLine(f) < c.getLocation().getStartLine()
}
private string comment_or_docstring(File f, boolean before_code) {
exists(Comment c |
c.getLocation().getFile() = f and
result = c.getText()
|
if isCommentAfterCode(c, f) then before_code = false else before_code = true
)
or
exists(Module m | m.getFile() = f |
result = m.getDocString().getText() and
before_code = true
)
}
private predicate lax_generated_by(File f, string tool) {
exists(string comment | comment = comment_or_docstring(f, _) |
tool =
comment
.regexpCapture("(?is).*\\b(?:(?:auto[ -]?)?generated|created automatically) by (?:the )?([-/\\w.]+[-/\\w]).*",
1)
)
}
private predicate lax_generated_from(File f, string src) {
exists(string comment | comment = comment_or_docstring(f, _) |
src =
comment
.regexpCapture("(?is).*\\b((?:auto[ -]?)?generated|created automatically) from ([-/\\w.]+[-/\\w]).*",
1)
)
}
private predicate strict_generated_by(File f, string tool) {
exists(string comment | comment = comment_or_docstring(f, true) |
tool =
comment
.regexpCapture("(?is)# *(?:this +)?(?:(?:code|file) +)?(?:is +)?(?:(?:auto(?:matically)?[ -]?)?generated|created automatically) by (?:the )?([-/\\w.]+[-/\\w]).*",
1)
)
}
private predicate strict_generated_from(File f, string src) {
exists(string comment | comment = comment_or_docstring(f, true) |
src =
comment
.regexpCapture("(?is)# *(?:this +)?(?:(?:code|file) +)?(?:is +)?(?:(?:auto(?:matically)?[ -]?)?generated|created automatically) from ([-/\\w.]+[-/\\w]).*",
1)
)
}
private predicate dont_modify(File f) {
comment_or_docstring(f, _).regexpMatch("(?is).*\\b(Do not|Don't) (edit|modify|make changes)\\b.*")
}
private predicate auto_generated(File f) {
exists(Comment c |
c.getLocation().getFile() = f and
c.getText()
.regexpMatch("(?is)# *this +(code|file) +is +(auto(matically)?[ -]?generated|created automatically).*")
)
}
/**
* A file generated by a template engine
*/
abstract class SpecificGeneratedFile extends GeneratedFile {
/*
* Currently cover Spitfire, Pyxl and Mako.
* Django templates are not compiled to Python.
* Jinja2 templates are compiled direct to bytecode via the ast.
*/
}
/** A file generated by the spitfire templating engine */
class SpitfireGeneratedFile extends SpecificGeneratedFile {
SpitfireGeneratedFile() {
exists(Module m | m.getFile() = this and not m instanceof SpitfireTemplate |
exists(ImportMember template_method, ImportExpr spitfire_runtime_template |
spitfire_runtime_template.getName() = "spitfire.runtime.template" and
template_method.getModule() = spitfire_runtime_template and
template_method.getName() = "template_method"
)
)
}
override string getTool() { result = "spitfire" }
}
/** A file generated by the pyxl templating engine */
class PyxlGeneratedFile extends SpecificGeneratedFile {
PyxlGeneratedFile() { this.getSpecifiedEncoding() = "pyxl" }
override string getTool() { result = "pyxl" }
}
/** A file generated by the mako templating engine */
class MakoGeneratedFile extends SpecificGeneratedFile {
MakoGeneratedFile() {
exists(Module m | m.getFile() = this |
from_mako_import(m) = "runtime" and
from_mako_import(m) = "filters" and
from_mako_import(m) = "cache" and
exists(Assign a, Name n |
a.getScope() = m and a.getATarget() = n and n.getId() = "__M_dict_builtin"
) and
exists(Assign a, Name n |
a.getScope() = m and a.getATarget() = n and n.getId() = "__M_locals_builtin"
) and
exists(Assign a, Name n |
a.getScope() = m and a.getATarget() = n and n.getId() = "_magic_number"
)
)
}
override string getTool() { result = "mako" }
}
string from_mako_import(Module m) {
exists(ImportMember member, ImportExpr mako |
member.getScope() = m and
member.getModule() = mako and
mako.getName() = "mako"
|
result = member.getName()
)
}
/** A file generated by Google's protobuf tool. */
class ProtobufGeneratedFile extends SpecificGeneratedFile {
ProtobufGeneratedFile() {
this.getAbsolutePath().regexpMatch(".*_pb2?.py") and
exists(Module m | m.getFile() = this |
exists(ImportExpr imp | imp.getEnclosingModule() = m |
imp.getImportedModuleName() = "google.net.proto2.python.public"
) and
exists(AssignStmt a, Name n |
a.getEnclosingModule() = m and
a.getATarget() = n and
n.getId() = "DESCRIPTOR"
)
)
}
override string getTool() { result = "protobuf" }
}