summaryrefslogtreecommitdiff
path: root/tix/tools/tixverify.tcl
blob: 3dec025a5fae6114f7b591f04d5a3eed99a4421f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#! /bin/sh
# the next line restarts using tclsh7.5 \
exec tclsh7.5 "$0" "$@"

#
# tixverify.tcl
#
#	Parses some files in the Tix distribution (.tcl scripts, Makefile.in,
#	etc) and detect potential errors.
#
#

#----------------------------------------------------------------------
#			 AUX ROUTINES
#----------------------------------------------------------------------

proc Usage {} {
    global info

    puts "Usage: \[test ... --\] files ..."
    puts "available tests:"
    set maxLen 0
    foreach name [lsort [array names info]] {
	if {$maxLen < [string length $name]} {
	    set maxLen [string length $name]
	}
    }
    foreach name [lsort [array names info]] {
	puts "  [format %-$maxLen\s $name] : $info($name)"
    }
}

proc ReadFile {file} {
    set data {}
    set fd [open $file {RDONLY}]
    while {![eof $fd]} {
	append data [gets $fd]\n
    }
    close $fd
    return $data
}

# returns a list of all the procedures in the $file
#
#
proc ProcParser {file} {
    global procs
    set procs {}

    set interp [interp create]

    foreach cmd [$interp eval info commands] {
	if {$cmd == "rename"} {
	    continue
	}
	if {$cmd == "proc"} {
	    continue
	}
	if {$cmd == "unknown"} {
	    continue
	}
	if {$cmd == "source"} {
	    continue
	}
	if {$cmd == "info"} {
	    continue
	}
	if {$cmd == "set"} {
	    continue
	}
	$interp eval [list rename $cmd {}]
    }

    $interp eval rename source __source
    $interp eval rename info   __info
    $interp eval rename rename __rename
    $interp alias unknown unknown_sub
    $interp alias proc proc_sub

    proc unknown_sub {args} {
	#puts "Ignoring toplevel command $args"
    }

    proc proc_sub {name arg body} {
	global procs
	lappend procs [list $name $arg $body]
    }

    $interp eval __source $file
    interp delete $interp 

    return $procs
}

#----------------------------------------------------------------------
#		       THE TESTS
#----------------------------------------------------------------------

#----------------------------------------------------------------------
#
set info(make) "the .o targets in the Makefiles"

proc Verify:make {files} {
    # $files is not used
    #
    set list {}
    set list_s {}
    set appPWD [pwd]
    set dir [file dirname [info script]]
    cd $dir
    cd ..
    set dir [pwd]
    cd $appPWD
    puts "checking the makefiles $dir/*/Makefile.in"
    foreach file [glob $dir/*/Makefile.in] {
	set data [ReadFile $file]
	if [regexp {tixClass[.]o} $data] {
	    lappend list $file

	    foreach line [split $data \n] {
		if [regexp {tix[A-z]*[.]o} $line target] {
		    regsub _s $target "" target
		    set targets($target) 1
		}
	    }
	}
	if [regexp {tixClass_s[.]o} $data] {
	    lappend list_s $file
	}
    }

    foreach file $list {
	set data [ReadFile $file]
	set filename "$file:\n"
	foreach target [lsort [array names targets]] {
	    if {![regexp $target $data]} {
		puts -nonewline $filename
		puts \t$target
		set filename ""
	    }
	}
	if {[lsearch $list_s $file] != -1} {
	    foreach target [lsort [array names targets]] {
		if {$target == "tixAppInit.o"} {
		    continue
		}
		regsub {[.]o} $target _s.o target
		if {![regexp $target $data]} {
		    puts -nonewline $filename
		    puts \t$target
		    set filename ""
		}
	    }
	}
    }
}

#----------------------------------------------------------------------
#
set info(strcmp)  "ungarded string comparisons"

proc Verify:strcmp {files} {
    catch {
	set lines ""
	set lines [eval exec egrep -n [list {if.*[!=]=.*[^x]\$}] $files]
    }

    foreach line [split $lines \n] {
	if [regexp {[x]\$} $line] {
	    continue
	}
	puts $line
    }
}

#----------------------------------------------------------------------
#
set info(strcmp1) "improperly guarded string comparisons"

proc Verify:strcmp1 {files} {
    catch {
	set lines ""
	set lines [eval exec egrep -n [list {if.*[!=]=.*[x]\$}] $files]
    }

    foreach line [split $lines \n] {
       if ![regexp {[\"]x[^\"]*[\"][ ]*[!=]=[ ]*[\"]x[^\"]*[\"]} $line stuff] {
	    puts $line
	    continue
	}
    }
}

#----------------------------------------------------------------------
#
set info(bool) "unverified boolean options (missing tixVerifyBoolean)"

proc Verify:bool {files} {
    set boolOpts {
	-disablecallback
	-dropdown
	-editable
	-fancy
	-history
	-prunehistory
	-disablecallback
	-showhidden
	-takefocus
	-allowzero
	-radio
	-dynamicgeometry
	-ignoreinvoke
    }

    puts "checking the following options: \{$boolOpts\}"

    set rexp ""
    set prefix ""

    foreach opt $boolOpts {
	append rexp "$prefix\(\{$opt\[\ \].*\[^n\]\}\)"
	set prefix "|"
    }

    catch {
	puts [eval exec egrep -n [list $rexp] $files]
    }
}

#----------------------------------------------------------------------
#
set info(classname) "misspelled class name"

proc Verify:classname {files} {
    foreach file $files {
	set data [exec cat $file]
	if [regexp "(tixWidgetClass|tixClass)\[^\{\]*\{" $data def] {
	    regsub (tixWidgetClass|tixClass) $def "" def
	    regsub \{ $def "" def
	    set def [string trim $def]

	    set inFile "in file $file:\n"

	    foreach line [split $data \n] {
		if {[regexp "^proc.*:" $line] && ![regexp $def: $line]} {
		    puts -nonewline $inFile
		    puts "    $line"
		    set inFile ""
		}
	    }
	}
    }
}

#----------------------------------------------------------------------
#
set info(chain)  "improperly chained methods"

proc Verify:chain {files} {
    set baseClass(InitWidgetRec)   tixPrimitive
    set baseClass(ConstructWidget) tixPrimitive
    set baseClass(SetBindings)     tixPrimitive
    set baseClass(Destructor)      tixPrimitive
    set mustChain [array names baseClass]

    foreach file $files {
	foreach proc [ProcParser $file] {
	    set name [lindex $proc 0]
	    set args [lindex $proc 1]
	    set body [lindex $proc 2]

	    set class  [lindex [split $name :] 0]
	    set method [lindex [split $name :] 1]

	    foreach p $mustChain {
		if {$baseClass($p) == $class} {
		    continue
		}
		if [regexp :$p $name] {
		    if {![string match "*tixChainMethod \$w $p*" $body]} {
			puts $name
		    }
		}
	    }
	}
    }
}

#----------------------------------------------------------------------
#	Main program
#----------------------------------------------------------------------
if {$argv == {}} {
    Usage
    exit 0
}

set idx [lsearch $argv "--"]
if {$idx > 0 } {
    set tests [lrange $argv 0 [expr $idx-1]]
} else {
    set tests [lsort [array names info]]
}

if {$idx != -1} {
    set files [lrange $argv [expr $idx+1] end]
} else {
    set files $argv
}


foreach test $tests {
    if {![info exists info($test)]} {
	puts "Error: \"$test\" is not a test"
	Usage
	exit 1
    }
}

foreach test $tests {
    puts "Executing test \"$test\": Checking $info($test)"
    Verify:$test $files
    puts --------OK-----------
}