@@ -11,6 +11,7 @@ use crate::*;
1111pub struct Stats {
1212 start : usize ,
1313 end : usize ,
14+ unit : bool ,
1415 lines : FxHashSet < usize > ,
1516}
1617
@@ -19,16 +20,23 @@ impl Serialize for Stats {
1920 where
2021 S : Serializer ,
2122 {
22- let mut st = serializer. serialize_struct ( "loc" , 2 ) ?;
23+ let mut st = serializer. serialize_struct ( "loc" , 3 ) ?;
2324 st. serialize_field ( "sloc" , & self . sloc ( ) ) ?;
2425 st. serialize_field ( "lloc" , & self . lloc ( ) ) ?;
26+ st. serialize_field ( "cloc" , & self . cloc ( ) ) ?;
2527 st. end ( )
2628 }
2729}
2830
2931impl fmt:: Display for Stats {
3032 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
31- write ! ( f, "sloc {}, lloc: {}" , self . sloc( ) , self . lloc( ) )
33+ write ! (
34+ f,
35+ "sloc: {}, lloc: {}, cloc: {}" ,
36+ self . sloc( ) ,
37+ self . lloc( ) ,
38+ self . cloc( )
39+ )
3240 }
3341}
3442
@@ -41,40 +49,60 @@ impl Stats {
4149
4250 #[ inline( always) ]
4351 pub fn sloc ( & self ) -> f64 {
44- ( self . end - self . start ) as f64 + 1.
52+ // The if construct is needed to count the line that represents
53+ // the signature of a function in a function space
54+ let sloc = if self . unit {
55+ self . end - self . start
56+ } else {
57+ ( ( self . end - self . start ) + 1 )
58+ } ;
59+ sloc as f64
4560 }
4661
4762 #[ inline( always) ]
4863 pub fn lloc ( & self ) -> f64 {
4964 self . lines . len ( ) as f64
5065 }
66+
67+ #[ inline( always) ]
68+ pub fn cloc ( & self ) -> f64 {
69+ self . sloc ( ) - self . lloc ( )
70+ }
5171}
5272
5373pub trait SourceLoc
5474where
5575 Self : Checker ,
5676{
57- fn compute ( _node : & Node , _code : & [ u8 ] , _stats : & mut Stats , _is_func_space : bool ) { }
77+ fn compute (
78+ _node : & Node ,
79+ _code : & [ u8 ] ,
80+ _stats : & mut Stats ,
81+ _is_func_space : bool ,
82+ _is_unit : bool ,
83+ ) {
84+ }
5885}
5986
6087#[ inline( always) ]
61- fn init ( node : & Node , stats : & mut Stats , is_func_space : bool ) -> usize {
88+ fn init ( node : & Node , stats : & mut Stats , is_func_space : bool , is_unit : bool ) -> usize {
6289 let start = node. start_position ( ) . row ;
6390
6491 if is_func_space {
65- stats. start = start;
66-
6792 let end = node. end_position ( ) . row ;
93+
94+ stats. start = start;
6895 stats. end = end;
96+ stats. unit = is_unit;
6997 }
7098 start
7199}
72100
73101impl SourceLoc for PythonCode {
74- fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool ) {
102+ fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool , is_unit : bool ) {
75103 use Python :: * ;
76104
77- let start = init ( node, stats, is_func_space) ;
105+ let start = init ( node, stats, is_func_space, is_unit ) ;
78106
79107 match node. kind_id ( ) . into ( ) {
80108 Comment | String | DQUOTE | DQUOTE2 | ExpressionStatement | Block => { }
@@ -86,10 +114,10 @@ impl SourceLoc for PythonCode {
86114}
87115
88116impl SourceLoc for MozjsCode {
89- fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool ) {
117+ fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool , is_unit : bool ) {
90118 use Mozjs :: * ;
91119
92- let start = init ( node, stats, is_func_space) ;
120+ let start = init ( node, stats, is_func_space, is_unit ) ;
93121
94122 match node. kind_id ( ) . into ( ) {
95123 Comment | String | DQUOTE | ExpressionStatement | StatementBlock => { }
@@ -101,10 +129,10 @@ impl SourceLoc for MozjsCode {
101129}
102130
103131impl SourceLoc for JavascriptCode {
104- fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool ) {
132+ fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool , is_unit : bool ) {
105133 use Javascript :: * ;
106134
107- let start = init ( node, stats, is_func_space) ;
135+ let start = init ( node, stats, is_func_space, is_unit ) ;
108136
109137 match node. kind_id ( ) . into ( ) {
110138 Comment | String | DQUOTE | ExpressionStatement | StatementBlock => { }
@@ -116,10 +144,10 @@ impl SourceLoc for JavascriptCode {
116144}
117145
118146impl SourceLoc for TypescriptCode {
119- fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool ) {
147+ fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool , is_unit : bool ) {
120148 use Typescript :: * ;
121149
122- let start = init ( node, stats, is_func_space) ;
150+ let start = init ( node, stats, is_func_space, is_unit ) ;
123151
124152 match node. kind_id ( ) . into ( ) {
125153 Comment | String | DQUOTE | ExpressionStatement | StatementBlock => { }
@@ -131,10 +159,10 @@ impl SourceLoc for TypescriptCode {
131159}
132160
133161impl SourceLoc for TsxCode {
134- fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool ) {
162+ fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool , is_unit : bool ) {
135163 use Tsx :: * ;
136164
137- let start = init ( node, stats, is_func_space) ;
165+ let start = init ( node, stats, is_func_space, is_unit ) ;
138166
139167 match node. kind_id ( ) . into ( ) {
140168 Comment | String | DQUOTE | ExpressionStatement | StatementBlock => { }
@@ -146,14 +174,14 @@ impl SourceLoc for TsxCode {
146174}
147175
148176impl SourceLoc for RustCode {
149- fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool ) {
177+ fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool , is_unit : bool ) {
150178 use Rust :: * ;
151179
152- let start = init ( node, stats, is_func_space) ;
180+ let start = init ( node, stats, is_func_space, is_unit ) ;
153181
154182 match node. kind_id ( ) . into ( ) {
155183 LineComment | BlockComment | StringLiteral | RawStringLiteral | ExpressionStatement
156- | Block => { }
184+ | Block | SourceFile => { }
157185 _ => {
158186 stats. lines . insert ( start) ;
159187 }
@@ -162,14 +190,15 @@ impl SourceLoc for RustCode {
162190}
163191
164192impl SourceLoc for CppCode {
165- fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool ) {
193+ fn compute ( node : & Node , _code : & [ u8 ] , stats : & mut Stats , is_func_space : bool , is_unit : bool ) {
166194 use Cpp :: * ;
167195
168- let start = init ( node, stats, is_func_space) ;
196+ let start = init ( node, stats, is_func_space, is_unit ) ;
169197
170198 match node. kind_id ( ) . into ( ) {
171199 Comment | RawStringLiteral | StringLiteral | ExpressionStatement
172- | CompoundStatement | LabeledStatement | DeclarationList | FieldDeclarationList => { }
200+ | CompoundStatement | LabeledStatement | DeclarationList | FieldDeclarationList
201+ | TranslationUnit => { }
173202 _ => {
174203 stats. lines . insert ( start) ;
175204 }
0 commit comments