]> gitweb.factorcode.org Git - factor.git/blob - extra/lint/lint.factor
Cleanup lint warnings.
[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 words.alias ;
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 ] [ 2drop ] [ 2array ]
35         [ bitand ]
36         [ . ]
37         [ new ]
38         [ get ]
39         [ t ] [ f ]
40         [ { } ]
41         [ drop t ] [ drop f ] [ 2drop t ] [ 2drop f ]
42         [ cdecl ]
43         [ first ] [ second ] [ third ] [ fourth ]
44         [ ">" write ] [ "/>" write ]
45     }
46
47 ! ! Add definitions
48 H{ } clone lint-definitions set-global
49
50 all-words [
51     dup def>> dup callable?
52     [ lint-definitions get-global set-hash-vector ] [ drop ] if
53 ] each
54
55 ! ! Remove definitions
56
57 ! Remove empty word defs
58 lint-definitions get-global [ drop empty? not ] assoc-filter
59
60 ! Remove constants [ 1 ]
61 [ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
62
63 ! Remove words that are their own definition
64 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
65
66 ! Remove specialized*
67  [ nip [ vocabulary>> "specialized-" head? ] any? not ] assoc-filter
68
69  [ nip [ vocabulary>> "windows.messages" = ] any? not ] assoc-filter
70
71  [ nip [ alias? ] any? not ] assoc-filter
72
73 ! Remove trivial defs
74 [ drop trivial-defs member? not ] assoc-filter
75
76 ! Remove numbers only defs
77 [ drop [ number? ] all? not ] assoc-filter
78
79 ! Remove curry only defs
80 [ drop [ \ curry = ] all? not ] assoc-filter
81
82 ! Remove tag defs
83 [
84     drop {
85             [ length 3 = ]
86             [ first \ tag = ] [ second number? ] [ third \ eq? = ]
87     } 1&& not
88 ] assoc-filter
89
90 [
91     drop {
92         [ [ wrapper? ] deep-any? ]
93         [ [ hashtable? ] deep-any? ]
94     } 1|| not
95 ] assoc-filter
96
97 ! Remove n m shift defs
98 [
99     drop dup length 3 = [
100         [ first2 [ number? ] both? ]
101         [ third \ shift = ] bi and not
102     ] [ drop t ] if
103 ] assoc-filter 
104
105 ! Remove [ n slot ]
106 [
107     drop dup length 2 =
108     [ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
109 ] assoc-filter
110
111 dup manual-substitutions
112
113 [ lint-definitions set-global ] [ keys lint-definitions-keys set-global ] bi
114
115 : find-duplicates ( -- seq )
116     lint-definitions get-global [ nip length 1 > ] assoc-filter ;
117
118 GENERIC: lint ( obj -- seq )
119
120 M: object lint ( obj -- seq ) drop f ;
121
122 : subseq/member? ( subseq/member seq -- ? )
123     { [ start ] [ member? ] } 2|| ;
124
125 M: callable lint ( quot -- seq )
126     [ lint-definitions-keys get-global ] dip '[ _ subseq/member? ] filter ;
127
128 M: word lint ( word -- seq )
129     def>> dup callable? [ lint ] [ drop f ] if ;
130
131 : word-path. ( word -- )
132     [ vocabulary>> ] [ name>> ] bi ":" glue print ;
133
134 : 4bl ( -- ) bl bl bl bl ;
135
136 : (lint.) ( pair -- )
137     first2 [ word-path. ] dip [
138         [ 4bl .  "-----------------------------------" print ]
139         [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
140     ] each nl nl ;
141
142 : lint. ( alist -- ) [ (lint.) ] each ;
143
144 GENERIC: run-lint ( obj -- obj )
145
146 : (trim-self) ( val key -- obj ? )
147     lint-definitions get-global at*
148     [ dupd remove empty? not ] [ drop f ] if ;
149
150 : trim-self ( seq -- newseq )
151     [ [ (trim-self) ] filter ] assoc-map ;
152
153 : filter-symbols ( alist -- alist )
154     [
155         nip first dup lint-definitions get-global at
156         [ first ] bi@ literalize = not
157     ] assoc-filter ;
158
159 M: sequence run-lint ( seq -- seq )
160     [ dup lint ] { } map>assoc trim-self
161     [ second empty? not ] filter filter-symbols ;
162
163 M: word run-lint ( word -- seq ) 1array run-lint ;
164
165 : lint-all ( -- seq ) all-words run-lint dup lint. ;
166
167 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
168
169 : lint-vocabs ( prefix -- seq )
170     [ vocabs ] dip [ head? ] curry filter [ lint-vocab ] map ;
171
172 : lint-word ( word -- seq ) 1array run-lint dup lint. ;