Skip to content

Commit d1fda78

Browse files
committed
[docs][methods][4/n] add class type example
This demonstrates the current silencing of class types.
1 parent 1d365a5 commit d1fda78

File tree

11 files changed

+268
-8
lines changed

11 files changed

+268
-8
lines changed

check/threshold-1/threshold-1.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
./examples/docs/methods/code_constructs/class/class_bin.ml:6: push_n_times
139139
./examples/docs/methods/code_constructs/class/class_bin.ml:12: clear_stack
140140

141+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
142+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
143+
141144
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:6: push_n_times
142145
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:12: clear_stack
143146

check/threshold-1/threshold-1.ref

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
./examples/docs/methods/code_constructs/class/class_bin.ml:6: push_n_times
144144
./examples/docs/methods/code_constructs/class/class_bin.ml:12: clear_stack
145145

146+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
147+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
148+
146149
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:6: push_n_times
147150
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:12: clear_stack
148151

@@ -977,7 +980,7 @@ Nothing else to report in this section
977980
--------------------------------------------------------------------------------
978981

979982

980-
Total: 840
981-
Success: 835
983+
Total: 842
984+
Success: 837
982985
Failed: 5
983-
Ratio: 99.4047619048%
986+
Ratio: 99.406175772%

check/threshold-3-0.5/threshold-3-0.5.exp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
./examples/docs/methods/code_constructs/class/class_bin.ml:6: push_n_times
139139
./examples/docs/methods/code_constructs/class/class_bin.ml:12: clear_stack
140140

141+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
142+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
143+
141144
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:6: push_n_times
142145
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:12: clear_stack
143146

@@ -350,6 +353,8 @@
350353

351354
./examples/docs/methods/code_constructs/class/class_lib.mli:17: int_stack
352355

356+
./examples/docs/methods/code_constructs/class_type/class_type_lib.mli:17: int_stack
357+
353358
./examples/docs/methods/code_constructs/imm_obj/imm_obj_lib.mli:8: int_stack
354359

355360
./examples/using_dune/preprocessed_lib/preprocessed.mli:2: used

check/threshold-3-0.5/threshold-3-0.5.ref

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
./examples/docs/methods/code_constructs/class/class_bin.ml:6: push_n_times
144144
./examples/docs/methods/code_constructs/class/class_bin.ml:12: clear_stack
145145

146+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
147+
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
148+
146149
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:6: push_n_times
147150
./examples/docs/methods/code_constructs/imm_obj/imm_obj_bin.ml:12: clear_stack
148151

@@ -355,6 +358,8 @@
355358

356359
./examples/docs/methods/code_constructs/class/class_lib.mli:17: int_stack
357360

361+
./examples/docs/methods/code_constructs/class_type/class_type_lib.mli:17: int_stack
362+
358363
./examples/docs/methods/code_constructs/imm_obj/imm_obj_lib.mli:8: int_stack
359364

360365
./examples/using_dune/preprocessed_lib/preprocessed.mli:2: used
@@ -1378,7 +1383,7 @@ Nothing else to report in this section
13781383
--------------------------------------------------------------------------------
13791384

13801385

1381-
Total: 1160
1382-
Success: 1155
1386+
Total: 1163
1387+
Success: 1158
13831388
Failed: 5
1384-
Ratio: 99.5689655172%
1389+
Ratio: 99.5700773861%

docs/methods/METHODS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ from the `.mli` if there is one and the `.ml`.
137137
examples dedicated to specific code constructs :
138138
- [Immediate object](./code_constructs/IMMEDIATE_OBJ.md)
139139
- [Class](./code_constructs/CLASS.md)
140+
- [Class type](./code_constructs/CLASS_TYPE.md)
140141

141142
[TODO]: # (
142-
- [Class type](./code_constructs/CLASS_TYPE.md)
143143
- [Inheritance](./code_constructs/INHERITANCE.md)
144144
)
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
The reference files for this example are in the
2+
[class\_type](../../../examples/docs/methods/code_constructs/class_type) directory.
3+
4+
The reference takes place in `/tmp/docs/methods/code_constructs`, which
5+
is a copy of the [code\_constructs](../../../examples/docs/methods/code_constructs)
6+
directory. Reported locations may differ depending on the location of the source
7+
files.
8+
9+
The compilation command is :
10+
```
11+
make -C class_type build
12+
```
13+
14+
The analysis command is :
15+
```
16+
make -C class_type analyze
17+
```
18+
19+
The compile + analyze command is :
20+
```
21+
make -C class_type
22+
```
23+
24+
> [!IMPORTANT]
25+
> **LIMITATION**
26+
>
27+
> Methods declared in class types definition (different from class signatures)
28+
> are currently ignored by the analyzer.
29+
30+
## First run
31+
32+
Code:
33+
```OCaml
34+
(* class_type_lib.mli *)
35+
class type counter = object
36+
method get : int
37+
method incr : unit
38+
method reset : unit
39+
end
40+
41+
class type int_stack = object
42+
method push : int -> unit
43+
method pop : unit
44+
method peek : int option
45+
method reset : unit
46+
end
47+
48+
val counter : counter
49+
50+
val int_stack : int_stack
51+
```
52+
```OCaml
53+
(* class_type_lib.ml *)
54+
class type counter = object
55+
method get : int
56+
method incr : unit
57+
method reset : unit
58+
end
59+
60+
class type int_stack = object
61+
method push : int -> unit
62+
method pop : unit
63+
method peek : int option
64+
method reset : unit
65+
end
66+
67+
let counter = object
68+
val mutable n = 0
69+
method get = n
70+
method incr = n <- n + 1
71+
method reset = n <- 0
72+
end
73+
74+
let int_stack = object
75+
val mutable l : int list = []
76+
77+
method push x = l <- x::l
78+
79+
method pop =
80+
match l with
81+
| [] -> ()
82+
| _::tl -> l <- tl
83+
84+
method peek =
85+
match l with
86+
| [] -> None
87+
| hd::_ -> Some hd
88+
89+
method reset = l <- []
90+
end
91+
```
92+
```OCaml
93+
(* class_type_bin.ml *)
94+
class type unused_class = object
95+
method unused_method : unit
96+
end
97+
98+
let push_n_times n counter stack =
99+
for i = 1 to n do
100+
stack#push i;
101+
counter#incr;
102+
done
103+
104+
let clear_stack counter stack =
105+
while stack#peek <> None do
106+
stack#pop;
107+
counter#incr;
108+
done
109+
110+
let () =
111+
let open Class_type_lib in
112+
let n = 42 in
113+
counter#reset;
114+
push_n_times n counter int_stack;
115+
let count = counter#get in
116+
assert (count = n);
117+
counter#reset;
118+
clear_stack counter int_stack;
119+
let count' = counter#get in
120+
assert (count' = n)
121+
```
122+
123+
By looking at the code, we could make the same observation as in the
124+
[Class](./CLASS.md) example.
125+
126+
However, because of the current limitation on class types, nothing is expected
127+
to reported.
128+
129+
Code and analyze:
130+
```
131+
$ make -C class_type
132+
make: Entering directory '/tmp/docs/methods/code_construct/class_type'
133+
ocamlopt -bin-annot class_type_lib.mli class_type_lib.ml class_type_bin.ml
134+
dead_code_analyzer --nothing -M all .
135+
Scanning files...
136+
[DONE]
137+
138+
.> UNUSED METHODS:
139+
=================
140+
141+
Nothing else to report in this section
142+
--------------------------------------------------------------------------------
143+
144+
145+
make: Leaving directory '/tmp/docs/methods/code_construct/class_type'
146+
```
147+
148+
Nothing is reported. Our work here is done.

examples/docs/methods/code_constructs/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ all: build
55
build:
66
make -C imm_obj build
77
make -C class build
8+
make -C class_type build
89

910
clean:
1011
rm -f *~ *.cm* *.o *.obj
1112
make -C imm_obj clean
12-
make -C class clean
13+
make -C class_type clean
1314

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SRC:=class_type_lib.mli class_type_lib.ml class_type_bin.ml
2+
3+
all: build analyze
4+
5+
build:
6+
ocamlopt -bin-annot ${SRC}
7+
8+
analyze:
9+
dead_code_analyzer --nothing -M all .
10+
11+
clean:
12+
rm -f *.cm* *.o a.out
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(* class_type_bin.ml *)
2+
class type unused_class = object
3+
method unused_method : unit
4+
end
5+
6+
let push_n_times n counter stack =
7+
for i = 1 to n do
8+
stack#push i;
9+
counter#incr;
10+
done
11+
12+
let clear_stack counter stack =
13+
while stack#peek <> None do
14+
stack#pop;
15+
counter#incr;
16+
done
17+
18+
let () =
19+
let open Class_type_lib in
20+
let n = 42 in
21+
counter#reset;
22+
push_n_times n counter int_stack;
23+
let count = counter#get in
24+
assert (count = n);
25+
counter#reset;
26+
clear_stack counter int_stack;
27+
let count' = counter#get in
28+
assert (count' = n)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(* class_type_lib.ml *)
2+
class type counter = object
3+
method get : int
4+
method incr : unit
5+
method reset : unit
6+
end
7+
8+
class type int_stack = object
9+
method push : int -> unit
10+
method pop : unit
11+
method peek : int option
12+
method reset : unit
13+
end
14+
15+
let counter = object
16+
val mutable n = 0
17+
method get = n
18+
method incr = n <- n + 1
19+
method reset = n <- 0
20+
end
21+
22+
let int_stack = object
23+
val mutable l : int list = []
24+
25+
method push x = l <- x::l
26+
27+
method pop =
28+
match l with
29+
| [] -> ()
30+
| _::tl -> l <- tl
31+
32+
method peek =
33+
match l with
34+
| [] -> None
35+
| hd::_ -> Some hd
36+
37+
method reset = l <- []
38+
end

0 commit comments

Comments
 (0)