dirge-agent 0.11.1

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
use std::path::Path;

use tree_sitter::{Node, Parser};

use crate::semantic::adapter::LanguageAdapter;
use crate::semantic::common::{node_text, signature_up_to_body};
use crate::semantic::types::{ByteRange, ExtractedFile, Import, ImportKind, Symbol, SymbolKind};

/// Tree-sitter adapter for C.
///
/// C has no real privacy concept beyond `static` (file scope) vs
/// extern linkage. We surface `static` functions/variables as
/// non-exported and everything else as exported.
///
/// `struct` and `enum` become `SymbolKind::Class`; `typedef`
/// becomes `TypeAlias` (unless it wraps a struct, in which case
/// only the inner struct symbol is emitted to avoid duplicates).
/// Headers (`#include`) are extracted as imports.
pub struct CAdapter;

impl CAdapter {
    fn signature(&self, n: Node, s: &[u8]) -> String {
        signature_up_to_body(n, s)
    }

    /// Direct child `storage_class_specifier` with text "static"
    /// indicates file-scope (non-exported). Functions/variables
    /// without `static` have external linkage by default.
    fn is_static(&self, n: Node, s: &[u8]) -> bool {
        for i in 0..n.named_child_count() {
            if let Some(c) = n.named_child(i)
                && c.kind() == "storage_class_specifier"
                && node_text(c, s) == "static"
            {
                return true;
            }
        }
        false
    }

    /// Walks a `function_declarator` to find the underlying
    /// identifier. The declarator can be nested under
    /// `pointer_declarator` (for `int *foo()`-style returns) or
    /// `parenthesized_declarator` (function pointers).
    #[allow(clippy::only_used_in_recursion)]
    fn declarator_name<'a>(&self, n: Node<'a>, s: &'a [u8]) -> Option<String> {
        match n.kind() {
            "identifier" => Some(node_text(n, s).to_string()),
            "function_declarator" | "pointer_declarator" | "parenthesized_declarator" => {
                // First named child that resolves to an identifier wins.
                for i in 0..n.named_child_count() {
                    if let Some(c) = n.named_child(i)
                        && let Some(name) = self.declarator_name(c, s)
                    {
                        return Some(name);
                    }
                }
                None
            }
            _ => None,
        }
    }

    fn handle_function(&self, n: Node, s: &[u8], symbols: &mut Vec<Symbol>) {
        // `function_definition > function_declarator > identifier`.
        let mut name: Option<String> = None;
        for i in 0..n.named_child_count() {
            if let Some(c) = n.named_child(i)
                && c.kind() == "function_declarator"
            {
                name = self.declarator_name(c, s);
                break;
            }
            if let Some(c) = n.named_child(i)
                && c.kind() == "pointer_declarator"
            {
                name = self.declarator_name(c, s);
                if name.is_some() {
                    break;
                }
            }
        }
        let Some(name) = name else { return };
        symbols.push(Symbol {
            kind: SymbolKind::Function,
            is_exported: !self.is_static(n, s),
            name,
            range: ByteRange::from(n),
            signature: self.signature(n, s),
            parent_class: None,
        });
    }

    /// `struct Foo { … }` at top level. Anonymous structs (no
    /// `type_identifier` child) are skipped — there's no useful
    /// symbol name to attach. Forward declarations (`struct Foo;`,
    /// no body) are also skipped: emitting them creates a Class
    /// symbol with no contents that confuses code navigation, and
    /// the real definition elsewhere produces the canonical symbol.
    fn handle_struct(&self, n: Node, s: &[u8], symbols: &mut Vec<Symbol>) {
        let mut name: Option<String> = None;
        let mut has_body = false;
        for i in 0..n.named_child_count() {
            let Some(c) = n.named_child(i) else { continue };
            if c.kind() == "type_identifier" && name.is_none() {
                name = Some(node_text(c, s).to_string());
            }
            if c.kind() == "field_declaration_list" {
                has_body = true;
            }
        }
        if !has_body {
            return;
        }
        let Some(name) = name else { return };
        symbols.push(Symbol {
            kind: SymbolKind::Class,
            is_exported: true,
            name,
            range: ByteRange::from(n),
            signature: node_text(n, s).lines().next().unwrap_or("").to_string(),
            parent_class: None,
        });
    }

    fn handle_enum(&self, n: Node, s: &[u8], symbols: &mut Vec<Symbol>) {
        // Find the enum's tag name + its enumerator list in a single
        // pass so we can emit both the parent enum AND its constants
        // as Variable symbols. Previously the constants (`RED`,
        // `GREEN`, …) were lost; in C they're far more frequently
        // referenced than the enum tag itself.
        let mut name: Option<String> = None;
        let mut enumerator_list: Option<Node> = None;
        for i in 0..n.named_child_count() {
            let Some(c) = n.named_child(i) else { continue };
            match c.kind() {
                "type_identifier" if name.is_none() => {
                    name = Some(node_text(c, s).to_string());
                }
                "enumerator_list" => enumerator_list = Some(c),
                _ => {}
            }
        }
        let parent_name = name.clone();
        if let Some(name) = name {
            symbols.push(Symbol {
                kind: SymbolKind::Class,
                is_exported: true,
                name,
                range: ByteRange::from(n),
                signature: node_text(n, s).lines().next().unwrap_or("").to_string(),
                parent_class: None,
            });
        }
        // Emit each constant as a Variable symbol. Anonymous enums
        // (`enum { A, B };`) still surface their constants — the
        // parent enum just isn't named.
        if let Some(list) = enumerator_list {
            for i in 0..list.named_child_count() {
                if let Some(e) = list.named_child(i)
                    && e.kind() == "enumerator"
                {
                    // Enumerator's first identifier child is the
                    // constant name.
                    for j in 0..e.named_child_count() {
                        if let Some(id) = e.named_child(j)
                            && id.kind() == "identifier"
                        {
                            symbols.push(Symbol {
                                kind: SymbolKind::Variable,
                                is_exported: true,
                                name: node_text(id, s).to_string(),
                                range: ByteRange::from(e),
                                signature: node_text(e, s).to_string(),
                                parent_class: parent_name.clone(),
                            });
                            break;
                        }
                    }
                }
            }
        }
    }

    /// `typedef <type> <alias>;`. If `<type>` is an inline struct
    /// definition, surface only the struct (avoiding a duplicate
    /// alias symbol for the same range). Otherwise emit TypeAlias
    /// keyed on the rightmost identifier.
    fn handle_typedef(&self, n: Node, s: &[u8], symbols: &mut Vec<Symbol>) {
        // Scan for an inner struct_specifier or enum_specifier with a
        // type_identifier — those carry the canonical name.
        for i in 0..n.named_child_count() {
            if let Some(c) = n.named_child(i)
                && (c.kind() == "struct_specifier" || c.kind() == "enum_specifier")
            {
                // Only suppress the alias if the struct has its own
                // name — `typedef struct { ... } Foo;` (anonymous
                // struct) DOES need the alias to be visible.
                let has_inner_name = (0..c.named_child_count()).any(|j| {
                    c.named_child(j)
                        .map(|x| x.kind() == "type_identifier")
                        .unwrap_or(false)
                });
                if has_inner_name {
                    // The inner struct/enum will be handled by its
                    // own match arm in the top-level walk; nothing
                    // more to do here.
                    return;
                }
            }
        }
        // Last identifier in the typedef is the alias name.
        let mut alias: Option<String> = None;
        for i in (0..n.named_child_count()).rev() {
            if let Some(c) = n.named_child(i)
                && c.kind() == "type_identifier"
            {
                alias = Some(node_text(c, s).to_string());
                break;
            }
        }
        let Some(name) = alias else { return };
        symbols.push(Symbol {
            kind: SymbolKind::TypeAlias,
            is_exported: true,
            name,
            range: ByteRange::from(n),
            signature: node_text(n, s).lines().next().unwrap_or("").to_string(),
            parent_class: None,
        });
    }

    fn handle_include(&self, n: Node, s: &[u8], imports: &mut Vec<Import>) {
        // `#include <stdio.h>` → `system_lib_string`.
        // `#include "local.h"` → `string_literal > string_content`.
        for i in 0..n.named_child_count() {
            let Some(c) = n.named_child(i) else { continue };
            match c.kind() {
                "system_lib_string" => {
                    let raw = node_text(c, s);
                    let path = raw
                        .trim_matches(|ch: char| ch == '<' || ch == '>')
                        .to_string();
                    imports.push(Import {
                        names: vec![path.clone()],
                        source: path,
                        kind: ImportKind::Header,
                    });
                    return;
                }
                "string_literal" => {
                    for j in 0..c.named_child_count() {
                        if let Some(sub) = c.named_child(j)
                            && sub.kind() == "string_content"
                        {
                            let path = node_text(sub, s).to_string();
                            imports.push(Import {
                                names: vec![path.clone()],
                                source: path,
                                kind: ImportKind::Header,
                            });
                            return;
                        }
                    }
                }
                _ => {}
            }
        }
    }
}

impl LanguageAdapter for CAdapter {
    fn extensions(&self) -> &[&str] {
        // Header files share C's grammar in tree-sitter-c; C++
        // headers go through tree-sitter-cpp via the cpp adapter.
        &[".c", ".h"]
    }

    fn extract(&self, file_path: &Path, source: &str) -> Result<ExtractedFile, String> {
        let lang: tree_sitter::Language = tree_sitter_c::LANGUAGE.into();
        let mut parser = Parser::new();
        parser
            .set_language(&lang)
            .map_err(|e| format!("Failed to set language: {e}"))?;
        let tree = parser.parse(source, None).ok_or("Failed to parse source")?;
        let root = tree.root_node();
        let bytes = source.as_bytes();

        let mut symbols = Vec::new();
        let mut imports = Vec::new();
        let mut warnings = Vec::new();
        if root.has_error() {
            warnings.push("tree-sitter reported syntax errors".to_string());
        }

        for i in 0..root.named_child_count() {
            let Some(c) = root.named_child(i) else {
                continue;
            };
            match c.kind() {
                "function_definition" => self.handle_function(c, bytes, &mut symbols),
                "struct_specifier" => self.handle_struct(c, bytes, &mut symbols),
                "enum_specifier" => self.handle_enum(c, bytes, &mut symbols),
                "type_definition" => self.handle_typedef(c, bytes, &mut symbols),
                "preproc_include" => self.handle_include(c, bytes, &mut imports),
                _ => {}
            }
        }

        // `typedef struct Foo { ... } AliasName;` puts the struct
        // INSIDE a type_definition. To make `Foo` show up in
        // list_symbols we walk type_definition children for inner
        // struct/enum specifiers.
        for i in 0..root.named_child_count() {
            if let Some(c) = root.named_child(i)
                && c.kind() == "type_definition"
            {
                for j in 0..c.named_child_count() {
                    let Some(inner) = c.named_child(j) else {
                        continue;
                    };
                    match inner.kind() {
                        "struct_specifier" => self.handle_struct(inner, bytes, &mut symbols),
                        "enum_specifier" => self.handle_enum(inner, bytes, &mut symbols),
                        _ => {}
                    }
                }
            }
        }

        let exports: Vec<String> = symbols
            .iter()
            .filter(|s| s.is_exported)
            .map(|s| s.name.clone())
            .collect();

        Ok(ExtractedFile {
            file_path: file_path.to_path_buf(),
            symbols,
            imports,
            exports,
            warnings,
            mtime: std::time::SystemTime::now(),
            size: 0,
            head_hash: 0,
        })
    }

    fn find_callees_in_range(
        &self,
        source: &str,
        _file_path: &Path,
        range: ByteRange,
    ) -> Result<Vec<String>, String> {
        let lang: tree_sitter::Language = tree_sitter_c::LANGUAGE.into();
        // `call_expression > identifier` for `foo()`. Member access
        // `obj.foo()` parses with field_expression; we capture the
        // accessed field too.
        let query_str = r#"
            (call_expression function: (identifier) @callee)
            (call_expression function: (field_expression field: (field_identifier) @callee))
        "#;
        crate::semantic::common::run_callee_query(&lang, query_str, source, range)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn pb(n: &str) -> std::path::PathBuf {
        std::path::PathBuf::from(n)
    }

    #[test]
    fn extracts_function_with_static_visibility() {
        let src =
            "int add(int a, int b) { return a + b; }\nstatic int helper(int x) { return x; }\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        let add = f.symbols.iter().find(|s| s.name == "add").unwrap();
        let helper = f.symbols.iter().find(|s| s.name == "helper").unwrap();
        assert!(matches!(add.kind, SymbolKind::Function));
        assert!(add.is_exported);
        assert!(!helper.is_exported);
    }

    #[test]
    fn extracts_struct_and_enum_as_class() {
        let src = "struct Foo { int n; };\nenum Color { RED, GREEN };\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        let foo = f.symbols.iter().find(|s| s.name == "Foo").unwrap();
        let col = f.symbols.iter().find(|s| s.name == "Color").unwrap();
        assert!(matches!(foo.kind, SymbolKind::Class));
        assert!(matches!(col.kind, SymbolKind::Class));
    }

    #[test]
    fn extracts_typedef_struct_as_inner_struct() {
        // Named inner struct: surface only `Point` (the struct
        // name), not the alias.
        let src = "typedef struct Point { int x; int y; } Point;\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        let points: Vec<_> = f.symbols.iter().filter(|s| s.name == "Point").collect();
        // Inner struct emits one Class symbol; alias is suppressed
        // because the struct has its own name.
        assert_eq!(points.len(), 1);
        assert!(matches!(points[0].kind, SymbolKind::Class));
    }

    #[test]
    fn extracts_anonymous_typedef_as_alias() {
        // Anonymous inner struct: emit the alias (otherwise users
        // have no name for it).
        let src = "typedef struct { int x; } Vec;\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        let vec = f.symbols.iter().find(|s| s.name == "Vec").unwrap();
        assert!(matches!(vec.kind, SymbolKind::TypeAlias));
    }

    #[test]
    fn extracts_includes() {
        let src = "#include <stdio.h>\n#include \"local.h\"\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        assert!(f.imports.iter().any(|i| i.source == "stdio.h"));
        assert!(f.imports.iter().any(|i| i.source == "local.h"));
    }

    #[test]
    fn find_callees_captures_calls() {
        let src = "void run(void) { printf(\"hi\"); helper(); }\nstatic void helper(void) {}\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        let run = f.symbols.iter().find(|s| s.name == "run").unwrap();
        let callees = CAdapter
            .find_callees_in_range(src, &pb("a.c"), run.range)
            .unwrap();
        assert!(callees.contains(&"printf".to_string()));
        assert!(callees.contains(&"helper".to_string()));
    }

    /// Enum constants — `RED`/`GREEN` are individually surfaced as
    /// Variable symbols anchored to the parent enum tag.
    #[test]
    fn extracts_enum_constants_as_variables() {
        let src = "enum Color { RED, GREEN = 5, BLUE };\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        for needed in ["Color", "RED", "GREEN", "BLUE"] {
            assert!(
                f.symbols.iter().any(|s| s.name == needed),
                "missing: {needed}",
            );
        }
        let red = f.symbols.iter().find(|s| s.name == "RED").unwrap();
        assert!(matches!(red.kind, SymbolKind::Variable));
        assert_eq!(red.parent_class.as_deref(), Some("Color"));
    }

    /// Forward `struct` declarations (no body) are skipped to
    /// avoid duplicate symbols with the real definition.
    #[test]
    fn forward_struct_declaration_is_skipped() {
        let src = "struct Foo;\nstruct Foo { int n; };\n";
        let f = CAdapter.extract(&pb("a.c"), src).unwrap();
        let foos: Vec<_> = f.symbols.iter().filter(|s| s.name == "Foo").collect();
        assert_eq!(foos.len(), 1, "only the definition should produce a symbol");
    }
}