sloc-languages 1.5.63

Source line analysis tool with CLI, web UI, HTML/PDF reports, and CI/CD integration
Documentation
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Nima Shafie <nimzshafie@gmail.com>

//! Style-guide analysis for C, C++, and Objective-C.
//! Guides: LLVM, Google, Mozilla, Microsoft, WebKit.

use super::common::*;

/// Brace placement.
#[derive(Clone, Copy, PartialEq, Eq)]
enum BraceStyle {
    Attach,
    Allman,
    Mixed,
    Unknown,
}

/// Pointer/reference declarator alignment.
#[derive(Clone, Copy, PartialEq, Eq)]
enum PointerStyle {
    WithType,
    WithName,
    Mixed,
    Unknown,
}

pub fn analyze(text: &str) -> StyleAnalysis {
    let lines: Vec<&str> = text.lines().collect();
    let mut tabs = 0u32;
    let mut sp2 = 0u32;
    let mut sp4 = 0u32;
    let mut allman = 0u32;
    let mut attach = 0u32;
    let mut ptr_type = 0u32;
    let mut ptr_name = 0u32;
    let mut space_paren = 0u32;
    let mut nospace_paren = 0u32;
    let mut pragma_once = false;
    let mut total = 0u32;

    let over80 = count_over(&lines, 80);
    let over100 = count_over(&lines, 100);
    let over120 = count_over(&lines, 120);
    let max_len = lines.iter().map(|l| l.len() as u32).max().unwrap_or(0);

    for line in &lines {
        total += 1;
        let trimmed = line.trim();
        if trimmed == "#pragma once" {
            pragma_once = true;
        }
        scan_indent(line, &mut tabs, &mut sp2, &mut sp4);
        scan_braces(trimmed, &mut allman, &mut attach);
        scan_paren(trimmed, &mut space_paren, &mut nospace_paren);
        scan_ptr(trimmed, &mut ptr_type, &mut ptr_name);
    }

    let indent = classify_indent(tabs, sp2, sp4);
    let brace = classify_brace(allman, attach);
    let ptr = classify_ptr(ptr_type, ptr_name);

    let guides = score_guides(
        indent,
        brace,
        ptr,
        over80,
        over100,
        total,
        space_paren,
        nospace_paren,
    );
    let (dominant, dominant_pct) = top_guide(&guides);

    let mut signals = vec![
        StyleSignal {
            name: "Brace Style".into(),
            value: brace_display(brace).into(),
        },
        StyleSignal {
            name: "Pointer Style".into(),
            value: ptr_display(ptr).into(),
        },
        StyleSignal {
            name: "Space Before Paren".into(),
            value: paren_display(space_paren, nospace_paren).into(),
        },
    ];
    if pragma_once {
        signals.push(StyleSignal {
            name: "Include Guard".into(),
            value: "#pragma once".into(),
        });
    }

    StyleAnalysis {
        language_family: "C / C++".into(),
        indent_style: indent,
        tab_indented_lines: tabs,
        space2_indented_lines: sp2,
        space4_indented_lines: sp4,
        lines_over_80: over80,
        lines_over_100: over100,
        lines_over_120: over120,
        max_line_length: max_len,
        total_lines: total,
        signals,
        guide_scores: guides,
        dominant_guide: dominant,
        dominant_score_pct: dominant_pct,
    }
}

fn scan_braces(trimmed: &str, allman: &mut u32, attach: &mut u32) {
    if trimmed == "{" {
        *allman += 1;
        return;
    }
    if trimmed.ends_with(" {") || trimmed.ends_with("\t{") {
        let head = trimmed[..trimmed.len() - 2].trim_end();
        if !head.is_empty() && is_block_head(head) {
            *attach += 1;
        }
    }
}

fn is_block_head(head: &str) -> bool {
    if head.ends_with(')')
        || head.ends_with("else")
        || head.ends_with("try")
        || head.ends_with("do")
        || head.ends_with("noexcept")
        || head.ends_with("const")
        || head.ends_with("override")
    {
        return true;
    }
    for kw in &["class ", "struct ", "enum ", "namespace ", "extern "] {
        if head.contains(kw) {
            return true;
        }
    }
    false
}

fn scan_paren(trimmed: &str, with_sp: &mut u32, no_sp: &mut u32) {
    static W: &[&str] = &[
        "if (",
        "} else if (",
        "while (",
        "for (",
        "switch (",
        "catch (",
    ];
    static N: &[&str] = &["if(", "while(", "for(", "switch(", "catch("];
    if W.iter().any(|kw| trimmed.contains(kw)) {
        *with_sp += 1;
    }
    if N.iter().any(|kw| trimmed.contains(kw)) {
        *no_sp += 1;
    }
}

fn scan_ptr(trimmed: &str, with_type: &mut u32, with_name: &mut u32) {
    if trimmed.starts_with("//")
        || trimmed.starts_with('*')
        || trimmed.starts_with("/*")
        || trimmed.starts_with('#')
    {
        return;
    }
    let bytes = trimmed.as_bytes();
    let len = bytes.len();
    let mut i = 0;
    let mut in_str = false;
    let mut in_char = false;
    while i < len {
        let b = bytes[i];
        if b == b'"' && !in_char && (i == 0 || bytes[i - 1] != b'\\') {
            in_str = !in_str;
        }
        if b == b'\'' && !in_str && (i == 0 || bytes[i - 1] != b'\\') {
            in_char = !in_char;
        }
        if in_str || in_char {
            i += 1;
            continue;
        }
        if b == b'*' || b == b'&' {
            if i + 1 < len && (bytes[i + 1] == b'*' || bytes[i + 1] == b'&') {
                i += 2;
                continue;
            }
            if i + 1 < len && (bytes[i + 1] == b'=' || bytes[i + 1] == b'/' || bytes[i + 1] == b'>')
            {
                i += 2;
                continue;
            }
            if i > 0 && (bytes[i - 1] == b'=' || bytes[i - 1] == b'/' || bytes[i - 1] == b'-') {
                i += 1;
                continue;
            }
            let pre_word = i > 0 && (bytes[i - 1].is_ascii_alphanumeric() || bytes[i - 1] == b'_');
            let pre_space = i > 0 && bytes[i - 1] == b' ';
            let post_word =
                i + 1 < len && (bytes[i + 1].is_ascii_alphanumeric() || bytes[i + 1] == b'_');
            let post_space = i + 1 < len && bytes[i + 1] == b' ';
            if pre_word && (post_word || post_space) {
                *with_type += 1;
            } else if pre_space && post_word {
                *with_name += 1;
            }
        }
        i += 1;
    }
}

fn classify_brace(allman: u32, attach: u32) -> BraceStyle {
    let t = allman + attach;
    if t == 0 {
        return BraceStyle::Unknown;
    }
    let a = allman as f32 / t as f32;
    let k = attach as f32 / t as f32;
    if a >= 0.65 {
        BraceStyle::Allman
    } else if k >= 0.65 {
        BraceStyle::Attach
    } else {
        BraceStyle::Mixed
    }
}

fn classify_ptr(with_type: u32, with_name: u32) -> PointerStyle {
    let t = with_type + with_name;
    if t == 0 {
        return PointerStyle::Unknown;
    }
    let tp = with_type as f32 / t as f32;
    let np = with_name as f32 / t as f32;
    if tp >= 0.65 {
        PointerStyle::WithType
    } else if np >= 0.65 {
        PointerStyle::WithName
    } else {
        PointerStyle::Mixed
    }
}

fn brace_display(s: BraceStyle) -> &'static str {
    match s {
        BraceStyle::Attach => "K&R / Attach",
        BraceStyle::Allman => "Allman",
        BraceStyle::Mixed => "Mixed",
        BraceStyle::Unknown => "\u{2014}",
    }
}

fn ptr_display(s: PointerStyle) -> &'static str {
    match s {
        PointerStyle::WithType => "Type* var",
        PointerStyle::WithName => "Type *var",
        PointerStyle::Mixed => "Mixed",
        PointerStyle::Unknown => "\u{2014}",
    }
}

fn paren_display(with_sp: u32, no_sp: u32) -> &'static str {
    let t = with_sp + no_sp;
    if t == 0 {
        return "\u{2014}";
    }
    if with_sp as f32 / t as f32 >= 0.70 {
        "space before '('"
    } else {
        "no space before '('"
    }
}

fn score_attach(b: BraceStyle) -> f32 {
    match b {
        BraceStyle::Attach => 1.0,
        BraceStyle::Mixed => 0.40,
        BraceStyle::Allman => 0.05,
        BraceStyle::Unknown => 0.50,
    }
}

fn score_allman(b: BraceStyle) -> f32 {
    match b {
        BraceStyle::Allman => 1.0,
        BraceStyle::Mixed => 0.40,
        BraceStyle::Attach => 0.05,
        BraceStyle::Unknown => 0.50,
    }
}

fn score_ptr_type(p: PointerStyle) -> f32 {
    match p {
        PointerStyle::WithType => 1.0,
        PointerStyle::Mixed => 0.40,
        PointerStyle::Unknown => 0.50,
        _ => 0.05,
    }
}

fn score_ptr_name(p: PointerStyle) -> f32 {
    match p {
        PointerStyle::WithName => 1.0,
        PointerStyle::Mixed => 0.40,
        PointerStyle::Unknown => 0.50,
        _ => 0.05,
    }
}

fn score_sp(with: u32, no: u32) -> f32 {
    let t = with + no;
    if t == 0 {
        0.50
    } else {
        with as f32 / t as f32
    }
}

fn top_guide(scores: &[StyleGuideScore]) -> (String, u8) {
    scores
        .iter()
        .max_by_key(|s| s.score_pct)
        .map(|s| (s.name.clone(), s.score_pct))
        .unwrap_or_else(|| ("Unknown".into(), 0))
}

#[allow(clippy::too_many_arguments)]
fn score_guides(
    ind: IndentStyle,
    brace: BraceStyle,
    ptr: PointerStyle,
    over80: u32,
    over100: u32,
    total: u32,
    sp: u32,
    no_sp: u32,
) -> Vec<StyleGuideScore> {
    let l80 = score_line80(over80, total);
    let l100 = score_line100(over100, total);
    let att = score_attach(brace);
    let all = score_allman(brace);
    let pt = score_ptr_type(ptr);
    let pn = score_ptr_name(ptr);
    let spc = score_sp(sp, no_sp);

    let llvm = weighted_score(&[
        (0.28, score_indent_2(ind)),
        (0.20, l80),
        (0.24, att),
        (0.15, pn),
        (0.13, spc),
    ]);
    let google = weighted_score(&[
        (0.25, score_indent_2(ind)),
        (0.20, l80),
        (0.25, att),
        (0.18, pt),
        (0.12, spc),
    ]);
    let moz_brace = match brace {
        BraceStyle::Attach => 0.60,
        BraceStyle::Allman => 0.45,
        BraceStyle::Mixed => 0.80,
        BraceStyle::Unknown => 0.50,
    };
    let mozilla = weighted_score(&[
        (0.28, score_indent_4(ind)),
        (0.20, l80),
        (0.22, moz_brace),
        (0.18, pt),
        (0.12, spc),
    ]);
    let microsoft = weighted_score(&[
        (0.32, score_indent_4(ind)),
        (0.36, all),
        (0.16, l100),
        (0.16, pn),
    ]);
    let webkit = weighted_score(&[
        (0.28, score_indent_4(ind)),
        (0.20, l80),
        (0.24, att),
        (0.16, pt),
        (0.12, spc),
    ]);

    vec![
        StyleGuideScore {
            name: "LLVM".into(),
            description: "2-space | 80-col | K&R | *var".into(),
            score_pct: llvm,
        },
        StyleGuideScore {
            name: "Google".into(),
            description: "2-space | 80-col | K&R | Type*".into(),
            score_pct: google,
        },
        StyleGuideScore {
            name: "Mozilla".into(),
            description: "4-space | 80-col | mixed braces | Type*".into(),
            score_pct: mozilla,
        },
        StyleGuideScore {
            name: "Microsoft".into(),
            description: "4-space | Allman | 100-col | *var".into(),
            score_pct: microsoft,
        },
        StyleGuideScore {
            name: "WebKit".into(),
            description: "4-space | 80-col | K&R | Type*".into(),
            score_pct: webkit,
        },
    ]
}