]> gitweb.factorcode.org Git - factor.git/blob - extra/lint/lint.factor
Revive the lint tool
[factor.git] / extra / lint / lint.factor
1 ! Copyright (C) 2007, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs combinators.short-circuit
4 fry hashtables io kernel math namespaces prettyprint quotations
5 sequences sequences.deep shuffle slots.private vectors vocabs
6 words xml.data ;
7 IN: lint
8
9 SYMBOL: lint-definitions
10 SYMBOL: lint-definitions-keys
11
12 : set-hash-vector ( val key hash -- )
13     2dup at -rot [ ?push ] 2dip set-at ;
14
15 : manual-substitutions ( hash -- )
16     {
17         { -rot [ swap [ swap ] dip ] }
18         { -rot [ swap swapd ] }
19         { rot [ [ swap ] dip swap ] }
20         { rot [ swapd swap ] }
21         { over [ dup swap ] }
22         { tuck [ dup -rot ] }
23         { swapd [ [ swap ] dip ] }
24         { 2nip [ nip nip ] }
25         { 2drop [ drop drop ] }
26         { 3drop [ drop drop drop ] }
27         { pop* [ pop drop ] }
28         { when [ [ ] if ] }
29         { >boolean [ f = not ] }
30     } swap '[ first2 _ set-hash-vector ] each ;
31
32 CONSTANT: trivial-defs
33     {
34         [ drop ] [ 2array ]
35         [ bitand ]
36         [ . ]
37         [ get ]
38         [ t ] [ f ]
39         [ { } ]
40         [ drop f ] [ 2drop ] [ 2drop t ]
41         [ cdecl ]
42         [ first ] [ second ] [ third ] [ fourth ]
43         [ ">" write ] [ "/>" write ]
44     }
45
46 ! ! Add definitions
47 H{ } clone lint-definitions set-global
48
49 all-words [
50     dup def>> dup callable?
51     [ lint-definitions get-global set-hash-vector ] [ drop ] if
52 ] each
53
54 ! ! Remove definitions
55
56 ! Remove empty word defs
57 lint-definitions get-global [ drop empty? not ] assoc-filter
58
59 ! Remove constants [ 1 ]
60 [ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
61
62 ! Remove words that are their own definition
63 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
64
65 ! Remove specialized*
66  [ nip [ vocabulary>> "specialized-" head? ] any? not ] assoc-filter
67
68  [ nip [ vocabulary>> "windows.messages" = ] any? not ] assoc-filter
69
70 ! Remove trivial defs
71 [ drop trivial-defs member? not ] assoc-filter
72
73 ! Remove numbers only defs
74 [ drop [ number? ] all? not ] assoc-filter
75
76 ! Remove curry only defs
77 [ drop [ \ curry = ] all? not ] assoc-filter
78
79 ! Remove tag defs
80 [
81     drop {
82             [ length 3 = ]
83             [ first \ tag = ] [ second number? ] [ third \ eq? = ]
84     } 1&& not
85 ] assoc-filter
86
87 [
88     drop {
89         [ [ wrapper? ] deep-any? ]
90         [ [ hashtable? ] deep-any? ]
91     } 1|| not
92 ] assoc-filter
93
94 ! Remove n m shift defs
95 [
96     drop dup length 3 = [
97         [ first2 [ number? ] both? ]
98         [ third \ shift = ] bi and not
99     ] [ drop t ] if
100 ] assoc-filter 
101
102 ! Remove [ n slot ]
103 [
104     drop dup length 2 =
105     [ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
106 ] assoc-filter
107
108 dup manual-substitutions
109
110 [ lint-definitions set-global ] [ keys lint-definitions-keys set-global ] bi
111
112 : find-duplicates ( -- seq )
113     lint-definitions get-global [ nip length 1 > ] assoc-filter ;
114
115 GENERIC: lint ( obj -- seq )
116
117 M: object lint ( obj -- seq ) drop f ;
118
119 : subseq/member? ( subseq/member seq -- ? )
120     { [ start ] [ member? ] } 2|| ;
121
122 M: callable lint ( quot -- seq )
123     [ lint-definitions-keys get-global ] dip '[ _ subseq/member? ] filter ;
124
125 M: word lint ( word -- seq )
126     def>> dup callable? [ lint ] [ drop f ] if ;
127
128 : word-path. ( word -- )
129     [ vocabulary>> ] [ name>> ] bi ":" glue print ;
130
131 : 4bl ( -- ) bl bl bl bl ;
132
133 : (lint.) ( pair -- )
134     first2 [ word-path. ] dip [
135         [ 4bl .  "-----------------------------------" print ]
136         [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
137     ] each nl nl ;
138
139 : lint. ( alist -- ) [ (lint.) ] each ;
140
141 GENERIC: run-lint ( obj -- obj )
142
143 : (trim-self) ( val key -- obj ? )
144     lint-definitions get-global at*
145     [ dupd remove empty? not ] [ drop f ] if ;
146
147 : trim-self ( seq -- newseq )
148     [ [ (trim-self) ] filter ] assoc-map ;
149
150 : filter-symbols ( alist -- alist )
151     [
152         nip first dup lint-definitions get-global at
153         [ first ] bi@ literalize = not
154     ] assoc-filter ;
155
156 M: sequence run-lint ( seq -- seq )
157     [ dup lint ] { } map>assoc trim-self
158     [ second empty? not ] filter filter-symbols ;
159
160 M: word run-lint ( word -- seq ) 1array run-lint ;
161
162 : lint-all ( -- seq ) all-words run-lint dup lint. ;
163
164 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
165
166 : lint-word ( word -- seq ) 1array run-lint dup lint. ;