-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathrmtmps.ml
843 lines (721 loc) · 25 KB
/
rmtmps.ml
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
(*
*
* Copyright (c) 2001-2002,
* George C. Necula <[email protected]>
* Scott McPeak <[email protected]>
* Wes Weimer <[email protected]>
* Ben Liblit <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*)
(* rmtmps.ml *)
(* implementation for rmtmps.mli *)
open Pretty
open Cil
module H = Hashtbl
module E = Errormsg
module U = Util
(* Set on the command-line: *)
let keepUnused = ref false
let rmUnusedInlines = ref false
let trace = Trace.trace "rmtmps"
(***********************************************************************
*
* Clearing of "referenced" bits
*
*)
let clearReferencedBits file =
let considerGlobal global =
match global with
| GType (info, _) ->
trace (dprintf "clearing mark: %a\n" d_shortglobal global);
info.treferenced <- false
| GEnumTag (info, _)
| GEnumTagDecl (info, _) ->
trace (dprintf "clearing mark: %a\n" d_shortglobal global);
info.ereferenced <- false
| GCompTag (info, _)
| GCompTagDecl (info, _) ->
trace (dprintf "clearing mark: %a\n" d_shortglobal global);
info.creferenced <- false
| GVar ({vname = name} as info, _, _)
| GVarDecl ({vname = name} as info, _) ->
trace (dprintf "clearing mark: %a\n" d_shortglobal global);
info.vreferenced <- false
| GFun ({svar = info} as func, _) ->
trace (dprintf "clearing mark: %a\n" d_shortglobal global);
info.vreferenced <- false;
let clearMark local =
trace (dprintf "clearing mark: local %s\n" local.vname);
local.vreferenced <- false
in
List.iter clearMark func.slocals
| _ ->
()
in
iterGlobals file considerGlobal
(***********************************************************************
*
* Scanning and categorization of pragmas
*
*)
(* collections of names of things to keep *)
type collection = (string, unit) H.t
type keepers = {
typedefs : collection;
enums : collection;
structs : collection;
unions : collection;
defines : collection;
}
(* rapid transfer of control when we find a malformed pragma *)
exception Bad_pragma
let ccureddeepcopystring = "ccureddeepcopy"
(* Save this length so we don't recompute it each time. *)
let ccureddeepcopystring_length = String.length ccureddeepcopystring
(* CIL and CCured define several pragmas which prevent removal of
* various global symbols. Here we scan for those pragmas and build
* up collections of the corresponding symbols' names.
*)
let categorizePragmas file =
(* names of things which should be retained *)
let keepers = {
typedefs = H.create 0;
enums = H.create 0;
structs = H.create 0;
unions = H.create 0;
defines = H.create 1
} in
(* populate these name collections in light of each pragma *)
let considerPragma =
let badPragma location pragma =
ignore (warnLoc location "Invalid argument to pragma %s" pragma)
in
function
| GPragma (Attr ("cilnoremove" as directive, args), location) ->
(* a very flexible pragma: can retain typedefs, enums,
* structs, unions, or globals (functions or variables) *)
begin
let processArg arg =
try
match arg with
| AStr specifier ->
(* isolate and categorize one symbol name *)
let collection, name =
(* Two words denotes a typedef, enum, struct, or
* union, as in "type foo" or "enum bar". A
* single word denotes a global function or
* variable. *)
let whitespace = Str.regexp "[ \t]+" in
let words = Str.split whitespace specifier in
match words with
| ["type"; name] ->
keepers.typedefs, name
| ["enum"; name] ->
keepers.enums, name
| ["struct"; name] ->
keepers.structs, name
| ["union"; name] ->
keepers.unions, name
| [name] ->
keepers.defines, name
| _ ->
raise Bad_pragma
in
H.add collection name ()
| _ ->
raise Bad_pragma
with Bad_pragma ->
badPragma location directive
in
List.iter processArg args
end
| GVarDecl (v, _) -> begin
(* Look for alias attributes, e.g. Linux modules *)
match filterAttributes "alias" v.vattr with
[] -> () (* ordinary prototype. *)
| [Attr("alias", [AStr othername])] ->
H.add keepers.defines othername ()
| _ -> E.s (error "Bad alias attribute at %a" d_loc !currentLoc)
end
(*** Begin CCured-specific checks: ***)
(* these pragmas indirectly require that we keep the function named in
-- the first arguments of boxmodelof and ccuredwrapperof, and
-- the third argument of ccureddeepcopy*. *)
| GPragma (Attr("ccuredwrapper" as directive, attribute :: _), location) ->
begin
match attribute with
| AStr name ->
H.add keepers.defines name ()
| _ ->
badPragma location directive
end
| GPragma (Attr("ccuredvararg", funcname :: (ASizeOf t) :: _), location) ->
begin
match t with
| TComp(c,_) when c.cstruct -> (* struct *)
H.add keepers.structs c.cname ()
| TComp(c,_) -> (* union *)
H.add keepers.unions c.cname ()
| TNamed(ti,_) ->
H.add keepers.typedefs ti.tname ()
| TEnum(ei, _) ->
H.add keepers.enums ei.ename ()
| _ ->
()
end
| GPragma (Attr(directive, _ :: _ :: attribute :: _), location)
when String.length directive > ccureddeepcopystring_length
&& (Str.first_chars directive ccureddeepcopystring_length)
= ccureddeepcopystring ->
begin
match attribute with
| AStr name ->
H.add keepers.defines name ()
| _ ->
badPragma location directive
end
(** end CCured-specific stuff **)
| _ ->
()
in
iterGlobals file considerPragma;
keepers
(***********************************************************************
*
* Function body elimination from pragmas
*
*)
(* When performing global slicing, any functions not explicitly marked
* as pragma roots are reduced to mere declarations. This leaves one
* with a reduced source file that still compiles to object code, but
* which contains the bodies of only explicitly retained functions.
*)
let amputateFunctionBodies keptGlobals file =
let considerGlobal = function
| GFun ({svar = {vname = name} as info}, location)
when not (H.mem keptGlobals name) ->
trace (dprintf "slicing: reducing to prototype: function %s\n" name);
GVarDecl (info, location)
| other ->
other
in
mapGlobals file considerGlobal
(***********************************************************************
*
* Root collection from pragmas
*
*)
let isPragmaRoot keepers = function
| GType ({tname = name}, _) ->
H.mem keepers.typedefs name
| GEnumTag ({ename = name}, _)
| GEnumTagDecl ({ename = name}, _) ->
H.mem keepers.enums name
| GCompTag ({cname = name; cstruct = structure}, _)
| GCompTagDecl ({cname = name; cstruct = structure}, _) ->
let collection = if structure then keepers.structs else keepers.unions in
H.mem collection name
| GVar ({vname = name; vattr = attrs}, _, _)
| GVarDecl ({vname = name; vattr = attrs}, _)
| GFun ({svar = {vname = name; vattr = attrs}}, _) ->
H.mem keepers.defines name ||
hasAttribute "used" attrs
| _ ->
false
(***********************************************************************
*
* Common root collecting utilities
*
*)
let traceRoot reason global =
trace (dprintf "root (%s): %a@!" reason d_shortglobal global);
true
let traceNonRoot reason global =
trace (dprintf "non-root (%s): %a@!" reason d_shortglobal global);
false
let hasExportingAttribute funvar =
let rec isExportingAttribute = function
| Attr ("constructor", []) -> true
| Attr ("destructor", []) -> true
| _ -> false
in
List.exists isExportingAttribute funvar.vattr
(***********************************************************************
*
* Root collection from external linkage
*
*)
(* Exported roots are those global symbols which are visible to the
* linker and dynamic loader. For variables, this consists of
* anything that is not "static". For functions, this consists of:
*
* - functions bearing a "constructor" or "destructor" attribute
* - functions declared extern but not inline
* - functions declared neither inline nor static
*
* gcc incorrectly (according to C99) makes inline functions visible to
* the linker. So we can only remove inline functions on MSVC.
*)
let isExportedRoot global =
let result, reason = match global with
| GVar ({vstorage = Static}, _, _) ->
false, "static variable"
| GVar _ ->
true, "non-static variable"
| GFun ({svar = v}, _) -> begin
if hasExportingAttribute v then
true, "constructor or destructor function"
else if v.vstorage = Static then
false, "static function"
else if v.vinline && v.vstorage != Extern
&& (!msvcMode || !rmUnusedInlines) then
false, "inline function"
else
true, "other function"
end
| GVarDecl(v,_) when hasAttribute "alias" v.vattr ->
true, "has GCC alias attribute"
| _ ->
false, "neither function nor variable"
in
trace (dprintf "isExportedRoot %a -> %b, %s@!"
d_shortglobal global result reason);
result
(***********************************************************************
*
* Root collection for complete programs
*
*)
(* Exported roots are "main()" and functions bearing a "constructor"
* or "destructor" attribute. These are the only things which must be
* retained in a complete program.
*)
let isCompleteProgramRoot global =
let result = match global with
| GFun ({svar = {vname = "main"; vstorage = vstorage}}, _) ->
vstorage <> Static
| GFun (fundec, _)
when hasExportingAttribute fundec.svar ->
true
| _ ->
false
in
trace (dprintf "complete program root -> %b for %a@!" result d_shortglobal global);
result
(***********************************************************************
*
* Transitive reachability closure from roots
*
*)
(* This visitor recursively marks all reachable types and variables as used. *)
class markReachableVisitor
((globalMap: (string, Cil.global) H.t),
(currentFunc: fundec option ref)) = object (self)
inherit nopCilVisitor
method vglob = function
| GType (typeinfo, _) ->
typeinfo.treferenced <- true;
DoChildren
| GCompTag (compinfo, _)
| GCompTagDecl (compinfo, _) ->
compinfo.creferenced <- true;
DoChildren
| GEnumTag (enuminfo, _)
| GEnumTagDecl (enuminfo, _) ->
enuminfo.ereferenced <- true;
DoChildren
| GVar (varinfo, _, _)
| GVarDecl (varinfo, _)
| GFun ({svar = varinfo}, _) ->
varinfo.vreferenced <- true;
DoChildren
| _ ->
SkipChildren
method vinst = function
Asm (_, tmpls, _, _, _, _) when !msvcMode ->
(* If we have inline assembly on MSVC, we cannot tell which locals
* are referenced. Keep thsem all *)
(match !currentFunc with
Some fd ->
List.iter (fun v ->
let vre = Str.regexp_string (Str.quote v.vname) in
if List.exists (fun tmp ->
try ignore (Str.search_forward vre tmp 0); true
with Not_found -> false)
tmpls
then
v.vreferenced <- true) fd.slocals
| _ -> assert false);
DoChildren
| _ -> DoChildren
method vvrbl v =
if not v.vreferenced then
begin
let name = v.vname in
if v.vglob then
trace (dprintf "marking transitive use: global %s\n" name)
else
trace (dprintf "marking transitive use: local %s\n" name);
(* If this is a global, we need to keep everything used in its
* definition and declarations. *)
if v.vglob then
begin
trace (dprintf "descending: global %s\n" name);
let descend global =
ignore (visitCilGlobal (self :> cilVisitor) global)
in
let globals = Hashtbl.find_all globalMap name in
List.iter descend globals
end
else
v.vreferenced <- true;
end;
SkipChildren
method vexpr (e: exp) =
match e with
Const (CEnum (_, _, ei)) -> ei.ereferenced <- true;
DoChildren
| _ -> DoChildren
method vtype typ =
let old : bool =
let visitAttrs attrs =
ignore (visitCilAttributes (self :> cilVisitor) attrs)
in
let visitType typ =
ignore (visitCilType (self :> cilVisitor) typ)
in
match typ with
| TEnum(e, attrs) ->
let old = e.ereferenced in
if not old then
begin
trace (dprintf "marking transitive use: enum %s\n" e.ename);
e.ereferenced <- true;
visitAttrs attrs;
visitAttrs e.eattr
end;
old
| TComp(c, attrs) ->
let old = c.creferenced in
if not old then
begin
trace (dprintf "marking transitive use: compound %s\n" c.cname);
c.creferenced <- true;
(* to recurse, we must ask explicitly *)
let recurse f = visitType f.ftype in
List.iter recurse c.cfields;
visitAttrs attrs;
visitAttrs c.cattr
end;
old
| TNamed(ti, attrs) ->
let old = ti.treferenced in
if not old then
begin
trace (dprintf "marking transitive use: typedef %s\n" ti.tname);
ti.treferenced <- true;
(* recurse deeper into the type referred-to by the typedef *)
(* to recurse, we must ask explicitly *)
visitType ti.ttype;
visitAttrs attrs
end;
old
| _ ->
(* for anything else, just look inside it *)
false
in
if old then
SkipChildren
else
DoChildren
end
let markReachable file isRoot =
(* build a mapping from global names back to their definitions &
* declarations *)
let globalMap = Hashtbl.create 137 in
let considerGlobal global =
match global with
| GFun ({svar = info}, _)
| GVar (info, _, _)
| GVarDecl (info, _) ->
Hashtbl.add globalMap info.vname global
| _ ->
()
in
iterGlobals file considerGlobal;
let currentFunc = ref None in
(* mark everything reachable from the global roots *)
let visitor = new markReachableVisitor (globalMap, currentFunc) in
let visitIfRoot global =
if isRoot global then
begin
trace (dprintf "traversing root global: %a\n" d_shortglobal global);
(match global with
GFun(fd, _) -> currentFunc := Some fd
| _ -> currentFunc := None);
ignore (visitCilGlobal visitor global)
end
else
trace (dprintf "skipping non-root global: %a\n" d_shortglobal global)
in
iterGlobals file visitIfRoot
(**********************************************************************
*
* Marking and removing of unused labels
*
**********************************************************************)
(* We keep only one label, preferably one that was not introduced by CIL.
* Scan a list of labels and return the data for the label that should be
* kept, and the remaining filtered list of labels. After this cleanup,
* every statement's labels will be either a single 'Default' or any
* number of 'Case's, in either case possibly preceded by a single 'Label'. *)
let labelsToKeep (ll: label list) : (string * location * bool) * label list =
let rec loop (sofar: string * location * bool) = function
[] -> sofar, []
| l :: rest ->
let newlabel, keepl =
match l with
| CaseRange _ | Case _ | Default _ -> sofar, true
| Label (ln, lloc, isorig) -> begin
match isorig, sofar with
| false, ("", _, _) ->
(* keep this one only if we have no label so far *)
(ln, lloc, isorig), false
| false, _ -> sofar, false
| true, (_, _, false) ->
(* this is an original label; prefer it to temporary or
* missing labels *)
(ln, lloc, isorig), false
| true, _ -> sofar, false
end
in
let newlabel', rest' = loop newlabel rest in
newlabel', (if keepl then l :: rest' else rest')
in
let sofar, labels = loop ("", locUnknown, false) ll in
try
(* If there is a 'default' label, remove all 'case' labels, as they are unnecessary *)
let default = List.find (function Default _ -> true | _ -> false) labels
in sofar, [ default ]
with Not_found ->
sofar, labels
(* Remove some trivial gotos, typically inserted at the end of for loops,
* because they are not printed by CIL which might yield an unused label
* warning. See test/small1/warnings-unused-label.c for a regression test. *)
class removeUnusedGoto = object(self)
inherit nopCilVisitor
method private pStmtNext (next: stmt) (s: stmt) = match s.skind with
(* Else-if: don't call visitCilStmt, recurse manually instead *)
| If(_,t,{ bstmts=[{skind=If _} as elsif]; battrs=[] },_) ->
ignore(visitCilBlock (self:>cilVisitor) t);
self#pStmtNext next elsif
| If(_,_,({bstmts=[{skind=Goto(gref,_);labels=[]}];
battrs=[]} as b),_)
| If(_,({bstmts=[{skind=Goto(gref,_);labels=[]}];
battrs=[]} as b),_,_)
when !gref == next ->
b.bstmts <- [];
ignore(visitCilStmt (self:>cilVisitor) s)
| _ -> ignore(visitCilStmt (self:>cilVisitor) s)
method vblock blk =
let rec dofirst = function
[] -> ()
| [x] -> self#pStmtNext invalidStmt x
| x :: rest -> dorest x rest
and dorest prev = function
[] -> self#pStmtNext invalidStmt prev
| x :: rest ->
self#pStmtNext x prev;
dorest x rest
in
dofirst blk.bstmts;
SkipChildren
(* No need to go into expressions or instructions *)
method vexpr _ = SkipChildren
method vinst _ = SkipChildren
method vtype _ = SkipChildren
end
class markUsedLabels (labelMap: (string, unit) H.t) = object
inherit nopCilVisitor
method vstmt (s: stmt) =
match s.skind with
Goto (dest, _) ->
let (ln, _, _), _ = labelsToKeep !dest.labels in
if ln = "" then
E.s (E.bug "rmtmps: destination of statement does not have labels");
(* Mark it as used *)
H.replace labelMap ln ();
DoChildren
| _ -> DoChildren
method vexpr e = match e with
| AddrOfLabel dest ->
let (ln, _, _), _ = labelsToKeep !dest.labels in
if ln = "" then
E.s (E.bug "rmtmps: destination of address of label does not have labels");
(* Mark it as used *)
H.replace labelMap ln ();
SkipChildren
| _ -> DoChildren
end
class removeUnusedLabels (labelMap: (string, unit) H.t) = object
inherit nopCilVisitor
method vstmt (s: stmt) =
let (ln, lloc, lorig), lrest = labelsToKeep s.labels in
(* Check our desired invariants for labels: 'lrest' must be either a
single 'Default' or only 'Case's. It is okay for 'lrest' to be
empty, because a 'Label' can exist on its own, independent of
switch statement labels, and the 'for_all' accepts this case. *)
assert (match lrest with
[ Default _ ] -> true
| _ -> List.for_all (function Case _ | CaseRange _ -> true | _ -> false) lrest);
s.labels <-
(if ln <> "" && H.mem labelMap ln then (* We had labels *)
(Label(ln, lloc, lorig) :: lrest)
else
lrest);
DoChildren
(* No need to go into expressions or instructions *)
method vexpr _ = SkipChildren
method vinst _ = SkipChildren
method vtype _ = SkipChildren
end
(***********************************************************************
*
* Removal of unused symbols
*
*)
(* regular expression matching names of uninteresting locals *)
let uninteresting =
let names = [
(* Cil.makeTempVar *)
"__cil_tmp";
(* sm: I don't know where it comes from but these show up all over. *)
(* this doesn't seem to do what I wanted.. *)
"iter";
(* various macros in glibc's <bits/string2.h> *)
"__result";
"__s"; "__s1"; "__s2";
"__s1_len"; "__s2_len";
"__retval"; "__len";
(* various macros in glibc's <ctype.h> *)
"__c"; "__res";
(* We remove the __malloc variables *)
] in
(* optional alpha renaming *)
let alpha = "\\(___[0-9]+\\)?" in
let pattern = "\\(" ^ (String.concat "\\|" names) ^ "\\)" ^ alpha ^ "$" in
Str.regexp pattern
let removeUnmarked file =
let removedLocals = ref [] in
let filterGlobal global =
match global with
(* unused global types, variables, and functions are simply removed *)
| GType ({treferenced = false}, _)
| GCompTag ({creferenced = false}, _)
| GCompTagDecl ({creferenced = false}, _)
| GEnumTag ({ereferenced = false}, _)
| GEnumTagDecl ({ereferenced = false}, _)
| GVar ({vreferenced = false}, _, _)
| GVarDecl ({vreferenced = false}, _)
| GFun ({svar = {vreferenced = false}}, _) ->
trace (dprintf "removing global: %a\n" d_shortglobal global);
false
(* retained functions may wish to discard some unused locals *)
| GFun (func, _) ->
let rec filterLocal local =
if not local.vreferenced then
begin
(* along the way, record the interesting locals that were removed *)
let name = local.vname in
trace (dprintf "removing local: %s\n" name);
if not (Str.string_match uninteresting name 0) then
removedLocals := (func.svar.vname ^ "::" ^ name) :: !removedLocals;
end;
local.vreferenced
in
func.slocals <- List.filter filterLocal func.slocals;
(* We also want to remove unused labels. We do it all here, including
* marking the used labels *)
let usedLabels:(string, unit) H.t = H.create 13 in
ignore (visitCilFunction (new removeUnusedGoto) func);
(* scan the function, not only the body, since there might be
* AddrOfLabel in initializers *)
ignore (visitCilFunction (new markUsedLabels usedLabels) func);
(* And now we scan again and we remove them *)
ignore (visitCilBlock (new removeUnusedLabels usedLabels) func.sbody);
true
(* all other globals are retained *)
| _ ->
trace (dprintf "keeping global: %a\n" d_shortglobal global);
true
in
file.globals <- List.filter filterGlobal file.globals;
!removedLocals
(***********************************************************************
*
* Exported interface
*
*)
type rootsFilter = global -> bool
let isDefaultRoot = isExportedRoot
let rec removeUnusedTemps ?(isRoot : rootsFilter = isDefaultRoot) file =
if !keepUnused || Trace.traceActive "disableTmpRemoval" then
Trace.trace "disableTmpRemoval" (dprintf "temp removal disabled\n")
else
begin
if !E.verboseFlag then
ignore (E.log "Removing unused temporaries\n" );
if Trace.traceActive "printCilTree" then
dumpFile defaultCilPrinter stdout "stdout" file;
(* digest any pragmas that would create additional roots *)
let keepers = categorizePragmas file in
(* if slicing, remove the bodies of non-kept functions *)
if !Cilutil.sliceGlobal then
amputateFunctionBodies keepers.defines file;
(* build up the root set *)
let isRoot global =
isPragmaRoot keepers global ||
isRoot global
in
(* mark everything reachable from the global roots *)
clearReferencedBits file;
markReachable file isRoot;
(* take out the trash *)
let removedLocals = removeUnmarked file in
(* print which original source variables were removed *)
if false && removedLocals != [] then
let count = List.length removedLocals in
if count > 2000 then
ignore (E.warn "%d unused local variables removed" count)
else
ignore (E.warn "%d unused local variables removed:@!%a"
count (docList ~sep:(chr ',' ++ break) text) removedLocals)
end