]> gitweb.factorcode.org Git - factor.git/blob - extra/lint/lint.factor
Don't report aliases as having a duplicate word definition as the word they alias...
[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 ] [ 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  [ nip [ alias? ] any? not ] assoc-filter
71
72 ! Remove trivial defs
73 [ drop trivial-defs member? not ] assoc-filter
74
75 ! Remove numbers only defs
76 [ drop [ number? ] all? not ] assoc-filter
77
78 ! Remove curry only defs
79 [ drop [ \ curry = ] all? not ] assoc-filter
80
81 ! Remove tag defs
82 [
83     drop {
84             [ length 3 = ]
85             [ first \ tag = ] [ second number? ] [ third \ eq? = ]
86     } 1&& not
87 ] assoc-filter
88
89 [
90     drop {
91         [ [ wrapper? ] deep-any? ]
92         [ [ hashtable? ] deep-any? ]
93     } 1|| not
94 ] assoc-filter
95
96 ! Remove n m shift defs
97 [
98     drop dup length 3 = [
99         [ first2 [ number? ] both? ]
100         [ third \ shift = ] bi and not
101     ] [ drop t ] if
102 ] assoc-filter 
103
104 ! Remove [ n slot ]
105 [
106     drop dup length 2 =
107     [ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
108 ] assoc-filter
109
110 dup manual-substitutions
111
112 [ lint-definitions set-global ] [ keys lint-definitions-keys set-global ] bi
113
114 : find-duplicates ( -- seq )
115     lint-definitions get-global [ nip length 1 > ] assoc-filter ;
116
117 GENERIC: lint ( obj -- seq )
118
119 M: object lint ( obj -- seq ) drop f ;
120
121 : subseq/member? ( subseq/member seq -- ? )
122     { [ start ] [ member? ] } 2|| ;
123
124 M: callable lint ( quot -- seq )
125     [ lint-definitions-keys get-global ] dip '[ _ subseq/member? ] filter ;
126
127 M: word lint ( word -- seq )
128     def>> dup callable? [ lint ] [ drop f ] if ;
129
130 : word-path. ( word -- )
131     [ vocabulary>> ] [ name>> ] bi ":" glue print ;
132
133 : 4bl ( -- ) bl bl bl bl ;
134
135 : (lint.) ( pair -- )
136     first2 [ word-path. ] dip [
137         [ 4bl .  "-----------------------------------" print ]
138         [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
139     ] each nl nl ;
140
141 : lint. ( alist -- ) [ (lint.) ] each ;
142
143 GENERIC: run-lint ( obj -- obj )
144
145 : (trim-self) ( val key -- obj ? )
146     lint-definitions get-global at*
147     [ dupd remove empty? not ] [ drop f ] if ;
148
149 : trim-self ( seq -- newseq )
150     [ [ (trim-self) ] filter ] assoc-map ;
151
152 : filter-symbols ( alist -- alist )
153     [
154         nip first dup lint-definitions get-global at
155         [ first ] bi@ literalize = not
156     ] assoc-filter ;
157
158 M: sequence run-lint ( seq -- seq )
159     [ dup lint ] { } map>assoc trim-self
160     [ second empty? not ] filter filter-symbols ;
161
162 M: word run-lint ( word -- seq ) 1array run-lint ;
163
164 : lint-all ( -- seq ) all-words run-lint dup lint. ;
165
166 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
167
168 : lint-word ( word -- seq ) 1array run-lint dup lint. ;