]> gitweb.factorcode.org Git - factor.git/blob - extra/lint/lint.factor
core: Rename iota to <iota> so we can have TUPLE: iota ... ; instead of TUPLE: iota...
[factor.git] / extra / lint / lint.factor
1 ! Copyright (C) 2007, 2008, 2011 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.tuple.private
4 combinators.short-circuit continuations fry io kernel
5 kernel.private locals.backend make math math.private namespaces
6 prettyprint quotations sequences sequences.deep slots.private
7 splitting stack-checker vocabs words words.alias ;
8 IN: lint
9
10 <PRIVATE
11
12 CONSTANT: manual-substitutions
13     H{
14         { -rot [ swap [ swap ] dip ] }
15         { -rot [ swap swapd ] }
16         { rot [ [ swap ] dip swap ] }
17         { rot [ swapd swap ] }
18         { over [ dup swap ] }
19         { swapd [ [ swap ] dip ] }
20         { 2nip [ nip nip ] }
21         { 2drop [ drop drop ] }
22         { 3drop [ drop drop drop ] }
23         { pop* [ pop drop ] }
24         { when [ [ ] if ] }
25         { >boolean [ f = not ] }
26     }
27
28 CONSTANT: trivial-defs
29     {
30         [ ">" write ] [ "/>" write ] [ " " write ]
31         [ 0 or + ]
32         [ dup length <iota> ]
33         [ 0 swap copy ]
34         [ dup length ]
35         [ 0 swap ]
36         [ 2dup = ] [ 2dup eq? ]
37         [ = not ] [ eq? not ]
38         [ boa throw ]
39         [ with each ] [ with map ]
40         [ curry filter ]
41         [ compose compose ]
42         [ empty? ] [ empty? not ]
43         [ dup empty? ] [ dup empty? not ]
44         [ 2dup both-fixnums? ]
45         [ [ drop ] prepose ]
46         [ 1 0 ? ]
47     }
48
49 : lintable-word? ( word -- ? )
50     {
51         [ vocabulary>> "specialized-" head? ]
52         [ vocabulary>> "windows-messages" = ]
53         [ alias? ]
54     } 1|| not ;
55
56 : lintable-words ( -- words )
57     all-words [ lintable-word? ] filter ;
58
59 : ignore-def? ( def -- ? )
60     {
61         ! Remove small defs
62         [ length 1 <= ]
63
64         ! Remove trivial defs
65         [ trivial-defs member? ]
66
67         ! Remove curry only defs
68         [ [ \ curry = ] all? ]
69
70         ! Remove words with locals
71         [ [ \ load-locals = ] any? ]
72
73         ! Remove stuff with wrappers
74         [ [ wrapper? ] any? ]
75
76         ! Remove trivial math
77         [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
78
79         ! Remove more trival defs
80         [
81             {
82                 [ length 2 = ]
83                 [ first2 [ word? ] either? ]
84                 [ first2 [ { dip dup over swap drop } member? ] either? ]
85             } 1&&
86         ]
87
88         ! Remove [ V{ } clone ] and related
89         [
90             {
91                 [ length 2 = ]
92                 [ first { [ sequence? ] [ assoc? ] } 1|| ]
93                 [ second { clone clone-like like assoc-like make } member? ]
94             } 1&&
95         ]
96
97         ! Remove [ foo get ] and related
98         [
99             {
100                 [ length 2 = ]
101                 [ first word? ]
102                 [ second { get get-global , % } member? ]
103             } 1&&
104         ]
105
106         ! Remove [ first second ] and related
107         [
108             {
109                 [ length 2 = ]
110                 [ first { first second third } member? ]
111                 [ second { first second third } member? ]
112             } 1&&
113         ]
114
115         ! Remove [ [ trivial ] if ] and related
116         [
117             {
118                 [ length 2 = ]
119                 [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
120                 [ second { if if* unless unless* when when* curry } member? ]
121             } 1&&
122         ]
123
124         ! Remove [ n - ] and related
125         [
126             {
127                 [ length 2 = ]
128                 [ first { [ number? ] [ boolean? ] } 1|| ]
129                 [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
130             } 1&&
131         ]
132
133         ! Remove [ dup 0 > ] and related
134         [
135             {
136                 [ length 3 = ]
137                 [ first { dup over } member? ]
138                 [ second number? ]
139                 [ third { + - / * < <= = >= > } member? ]
140             } 1&&
141         ]
142
143         ! Remove [ drop f f ] and related
144         [
145             {
146                 [ length 4 <= ]
147                 [ first { drop 2drop 3drop nip 2nip } member? ]
148                 [ rest-slice [ boolean? ] all? ]
149             } 1&&
150         ]
151
152         ! Remove [ length 1 = ] and related
153         [
154             {
155                 [ length 3 = ]
156                 [ first \ length = ]
157                 [ second number? ]
158                 [ third { + - / * < <= = >= > } member? ]
159             } 1&&
160         ]
161
162         ! Remove [ dup length 1 = ] and related
163         [
164             {
165                 [ length 4 = ]
166                 [ first { dup over } member? ]
167                 [ second \ length = ]
168                 [ third number? ]
169                 [ fourth { + - / * < <= = >= > } member? ]
170             } 1&&
171         ]
172
173         ! Remove numbers/t/f only defs
174         [
175             [ { [ number? ] [ boolean? ] } 1|| ] all?
176         ]
177
178         ! Remove [ tag n eq? ]
179         [
180             {
181                 [ length 3 = ]
182                 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
183             } 1&&
184         ]
185
186         ! Remove [ { foo } declare class-of ]
187         [
188             {
189                 [ length 3 = ]
190                 [ first { [ array? ] [ length 1 = ] } 1&& ]
191                 [ second \ declare = ]
192                 [ third \ class-of = ]
193             } 1&&
194         ]
195
196         ! Remove [ m n shift ]
197         [
198             {
199                 [ length 3 = ]
200                 [ first2 [ number? ] both? ] [ third \ shift = ]
201             } 1&&
202         ]
203
204         ! Remove [ layout-of n slot ]
205         [
206             {
207                 [ length 3 = ]
208                 [ first \ layout-of = ]
209                 [ second number? ]
210                 [ third \ slot = ]
211             } 1&&
212         ]
213     } 1|| ;
214
215 : all-callables ( def -- seq )
216     [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
217
218 : (load-definitions) ( word def hash -- )
219     [ all-callables ] dip '[ _ push-at ] with each ;
220
221 : load-definitions ( words -- hash )
222     H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
223
224 SYMBOL: lint-definitions
225 SYMBOL: lint-definitions-keys
226
227 : reload-definitions ( -- )
228     ! Load lintable and non-ignored definitions
229     lintable-words load-definitions
230
231     ! Remove words that are their own definition
232     [ [ [ def>> ] [ 1quotation ] bi = ] reject ] assoc-map
233
234     ! Add manual definitions
235     manual-substitutions over '[ _ push-at ] assoc-each
236
237     ! Set globals to new values
238     [ lint-definitions set-global ]
239     [ keys lint-definitions-keys set-global ] bi ;
240
241 : find-duplicates ( -- seq )
242     lint-definitions get-global [ nip length 1 > ] assoc-filter ;
243
244 GENERIC: lint ( obj -- seq )
245
246 M: object lint ( obj -- seq ) drop f ;
247
248 M: callable lint ( quot -- seq )
249     [ lint-definitions-keys get-global ] dip '[ _ subseq? ] filter ;
250
251 M: word lint ( word -- seq/f )
252     def>> [ callable? ] deep-filter [ lint ] map concat ;
253
254 : word-path. ( word -- )
255     [ vocabulary>> write ":" write ] [ . ] bi ;
256
257 : 4bl ( -- ) bl bl bl bl ;
258
259 : (lint.) ( pair -- )
260     first2 [ word-path. ] dip [
261         [ 4bl .  "-----------------------------------" print ]
262         [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
263     ] each nl ;
264
265 : lint. ( alist -- ) [ (lint.) ] each ;
266
267 GENERIC: run-lint ( obj -- obj )
268
269 : (trim-self) ( val key -- obj ? )
270     lint-definitions get-global at*
271     [ dupd remove empty? not ] [ drop f ] if ;
272
273 : trim-self ( seq -- newseq )
274     [ [ (trim-self) ] filter ] assoc-map ;
275
276 : filter-symbols ( alist -- alist )
277     [
278         nip first dup lint-definitions get-global at
279         [ first ] bi@ literalize = not
280     ] assoc-filter ;
281
282 M: sequence run-lint ( seq -- seq )
283     [ dup lint ] { } map>assoc trim-self
284     [ second empty? ] reject filter-symbols ;
285
286 M: word run-lint ( word -- seq ) 1array run-lint ;
287
288 PRIVATE>
289
290 : find-swap/swap ( word -- ? )
291     def>> [ callable? ] deep-filter
292     [
293         {
294             [ [ \ swap = ] count 2 >= ]
295             [
296                 { swap } split rest but-last
297                 [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
298             ]
299         } 1&&
300     ] any? ;
301
302 : find-redundant-word-props ( -- seq )
303     all-words [
304         {
305             [ { [ foldable? ] [ flushable? ] } 1|| ]
306             [ inline? ]
307         } 1&&
308     ] filter ;
309
310 : lint-all ( -- seq )
311     all-words run-lint dup lint. ;
312
313 : lint-vocab ( vocab -- seq )
314     vocab-words run-lint dup lint. ;
315
316 : lint-vocabs ( prefix -- seq )
317     [ loaded-vocab-names ] dip [ head? ] curry filter [ lint-vocab ] map ;
318
319 : lint-word ( word -- seq )
320     1array run-lint dup lint. ;
321
322 reload-definitions