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 ;
12 CONSTANT: manual-substitutions
14 { -rot [ swap [ swap ] dip ] }
15 { -rot [ swap swapd ] }
16 { rot [ [ swap ] dip swap ] }
17 { rot [ swapd swap ] }
19 { swapd [ [ swap ] dip ] }
23 { 2drop [ drop drop ] }
24 { 3drop [ drop drop drop ] }
27 { >boolean [ f = not ] }
30 CONSTANT: trivial-defs
32 [ ">" write ] [ "/>" write ] [ " " write ]
38 [ 2dup = ] [ 2dup eq? ]
41 [ with each ] [ with map ]
44 [ empty? ] [ empty? not ]
45 [ dup empty? ] [ dup empty? not ]
46 [ 2dup both-fixnums? ]
51 : lintable-word? ( word -- ? )
53 [ vocabulary>> "specialized-" head? ]
54 [ vocabulary>> "windows-messages" = ]
58 : lintable-words ( -- words )
59 all-words [ lintable-word? ] filter ;
61 : ignore-def? ( def -- ? )
67 [ trivial-defs member? ]
69 ! Remove curry only defs
70 [ [ \ curry = ] all? ]
72 ! Remove words with locals
73 [ [ \ load-locals = ] any? ]
75 ! Remove stuff with wrappers
79 [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
81 ! Remove more trival defs
85 [ first2 [ word? ] either? ]
86 [ first2 [ { dip dup over swap drop } member? ] either? ]
90 ! Remove [ V{ } clone ] and related
94 [ first { [ sequence? ] [ assoc? ] } 1|| ]
95 [ second { clone clone-like like assoc-like make } member? ]
99 ! Remove [ foo get ] and related
104 [ second { get get-global , % } member? ]
108 ! Remove [ first second ] and related
112 [ first { first second third } member? ]
113 [ second { first second third } member? ]
117 ! Remove [ [ trivial ] if ] and related
121 [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
122 [ second { if if* unless unless* when when* curry } member? ]
126 ! Remove [ n - ] and related
130 [ first { [ number? ] [ boolean? ] } 1|| ]
131 [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
135 ! Remove [ dup 0 > ] and related
139 [ first { dup over } member? ]
141 [ third { + - / * < <= = >= > } member? ]
145 ! Remove [ drop f f ] and related
149 [ first { drop 2drop 3drop nip 2nip 3nip 4nip } member? ]
150 [ rest-slice [ boolean? ] all? ]
154 ! Remove [ length 1 = ] and related
160 [ third { + - / * < <= = >= > } member? ]
164 ! Remove [ dup length 1 = ] and related
168 [ first { dup over } member? ]
169 [ second \ length = ]
171 [ fourth { + - / * < <= = >= > } member? ]
175 ! Remove numbers/t/f only defs
177 [ { [ number? ] [ boolean? ] } 1|| ] all?
180 ! Remove [ tag n eq? ]
184 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
188 ! Remove [ { foo } declare class-of ]
192 [ first { [ array? ] [ length 1 = ] } 1&& ]
193 [ second \ declare = ]
194 [ third \ class-of = ]
198 ! Remove [ m n shift ]
202 [ first2 [ number? ] both? ] [ third \ shift = ]
206 ! Remove [ layout-of n slot ]
210 [ first \ layout-of = ]
217 : all-callables ( def -- seq )
218 [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
220 : (load-definitions) ( word def hash -- )
221 [ all-callables ] dip '[ _ push-at ] with each ;
223 : load-definitions ( words -- hash )
224 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
226 SYMBOL: lint-definitions
227 SYMBOL: lint-definitions-keys
229 : reload-definitions ( -- )
230 ! Load lintable and non-ignored definitions
231 lintable-words load-definitions
233 ! Remove words that are their own definition
234 [ [ [ def>> ] [ 1quotation ] bi = ] reject ] assoc-map
236 ! Add manual definitions
237 manual-substitutions over '[ _ push-at ] assoc-each
239 ! Set globals to new values
240 [ lint-definitions set-global ]
241 [ keys lint-definitions-keys set-global ] bi ;
243 : find-duplicates ( -- seq )
244 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
246 GENERIC: lint ( obj -- seq )
248 M: object lint ( obj -- seq ) drop f ;
250 M: callable lint ( quot -- seq )
251 [ lint-definitions-keys get-global ] dip '[ _ subseq? ] filter ;
253 M: word lint ( word -- seq/f )
254 def>> [ callable? ] deep-filter [ lint ] map concat ;
256 : word-path. ( word -- )
257 [ vocabulary>> write ":" write ] [ . ] bi ;
259 : 4bl ( -- ) bl bl bl bl ;
261 : (lint.) ( pair -- )
262 first2 [ word-path. ] dip [
263 [ 4bl . "-----------------------------------" print ]
264 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
267 : lint. ( alist -- ) [ (lint.) ] each ;
269 GENERIC: run-lint ( obj -- obj )
271 : (trim-self) ( val key -- obj ? )
272 lint-definitions get-global at*
273 [ dupd remove empty? not ] [ drop f ] if ;
275 : trim-self ( seq -- newseq )
276 [ [ (trim-self) ] filter ] assoc-map ;
278 : filter-symbols ( alist -- alist )
280 nip first dup lint-definitions get-global at
281 [ first ] bi@ literalize = not
284 M: sequence run-lint ( seq -- seq )
285 [ dup lint ] { } map>assoc trim-self
286 [ second empty? ] reject filter-symbols ;
288 M: word run-lint ( word -- seq ) 1array run-lint ;
292 : find-swap/swap ( word -- ? )
293 def>> [ callable? ] deep-filter
296 [ [ \ swap = ] count 2 >= ]
298 { swap } split rest but-last
299 [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
304 : find-redundant-word-props ( -- seq )
307 [ { [ foldable? ] [ flushable? ] } 1|| ]
312 : lint-all ( -- seq )
313 all-words run-lint dup lint. ;
315 : lint-vocab ( vocab -- seq )
316 vocab-words run-lint dup lint. ;
318 : lint-vocabs ( prefix -- seq )
319 [ loaded-vocab-names ] dip [ head? ] curry filter [ lint-vocab ] map ;
321 : lint-word ( word -- seq )
322 1array run-lint dup lint. ;