1use crate::{Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6pub struct C;
8
9impl Language for C {
10 fn name(&self) -> &'static str {
11 "C"
12 }
13 fn extensions(&self) -> &'static [&'static str] {
14 &["c", "h"]
15 }
16 fn grammar_name(&self) -> &'static str {
17 "c"
18 }
19
20 fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
21 Some(self)
22 }
23
24 fn signature_suffix(&self) -> &'static str {
25 " {}"
26 }
27
28 fn extract_docstring(&self, node: &Node, content: &str) -> Option<String> {
29 let mut prev = node.prev_sibling();
30 while let Some(sibling) = prev {
31 if sibling.kind() == "comment" {
32 let text = &content[sibling.byte_range()];
33 if text.starts_with("/**") {
34 return Some(clean_block_doc_comment(text));
35 }
36 return None;
37 }
38 if sibling.kind() != "preproc_def" && sibling.kind() != "preproc_ifdef" {
40 return None;
41 }
42 prev = sibling.prev_sibling();
43 }
44 None
45 }
46
47 fn extract_attributes(&self, node: &Node, content: &str) -> Vec<String> {
48 let mut attrs = Vec::new();
49 let mut cursor = node.walk();
50 for child in node.children(&mut cursor) {
51 match child.kind() {
52 "attribute_declaration" | "attribute_specifier" | "ms_declspec_modifier" => {
53 attrs.push(content[child.byte_range()].trim().to_string());
54 }
55 _ => {}
56 }
57 }
58 attrs
59 }
60
61 fn build_signature(&self, node: &Node, content: &str) -> String {
62 match node.kind() {
63 "function_definition" => {
64 if let Some(declarator) = node.child_by_field_name("declarator")
65 && let Some(name) = C::find_identifier(&declarator, content)
66 {
67 return name.to_string();
68 }
69 let text = &content[node.byte_range()];
70 text.lines().next().unwrap_or(text).trim().to_string()
71 }
72 "struct_specifier" | "enum_specifier" => {
73 let name = self.node_name(node, content).unwrap_or("");
74 let keyword = if node.kind() == "struct_specifier" {
75 "struct"
76 } else {
77 "enum"
78 };
79 format!("{} {}", keyword, name)
80 }
81 _ => {
82 let text = &content[node.byte_range()];
83 text.lines().next().unwrap_or(text).trim().to_string()
84 }
85 }
86 }
87
88 fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
89 if node.kind() != "preproc_include" {
90 return Vec::new();
91 }
92
93 let line = node.start_position().row + 1;
94 let mut cursor = node.walk();
95 for child in node.children(&mut cursor) {
96 if child.kind() == "string_literal" || child.kind() == "system_lib_string" {
97 let text = &content[child.byte_range()];
98 let module = text
99 .trim_matches(|c| c == '"' || c == '<' || c == '>')
100 .to_string();
101 let is_relative = text.starts_with('"');
102 return vec![Import {
103 module,
104 names: Vec::new(),
105 alias: None,
106 is_wildcard: false,
107 is_relative,
108 line,
109 }];
110 }
111 }
112 Vec::new()
113 }
114
115 fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
116 if import.module.starts_with('<') || import.module.ends_with('>') {
118 format!("#include {}", import.module)
119 } else {
120 format!("#include \"{}\"", import.module)
121 }
122 }
123
124 fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
125 let name = symbol.name.as_str();
126 match symbol.kind {
127 crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
128 crate::SymbolKind::Module => name == "tests" || name == "test",
129 _ => false,
130 }
131 }
132
133 fn test_file_globs(&self) -> &'static [&'static str] {
134 &["**/test_*.c", "**/*_test.c", "**/tests/**/*.c"]
135 }
136
137 fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
138 node.child_by_field_name("body")
139 }
140
141 fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
142 if let Some(name_node) = node.child_by_field_name("name") {
144 return Some(&content[name_node.byte_range()]);
145 }
146 if let Some(declarator) = node.child_by_field_name("declarator") {
148 return Self::find_identifier(&declarator, content);
149 }
150 None
151 }
152}
153
154impl LanguageSymbols for C {}
155
156impl C {
157 fn find_identifier<'a>(node: &Node, content: &'a str) -> Option<&'a str> {
158 if node.kind() == "identifier" {
159 return Some(&content[node.byte_range()]);
160 }
161 let mut cursor = node.walk();
162 for child in node.children(&mut cursor) {
163 if let Some(id) = Self::find_identifier(&child, content) {
164 return Some(id);
165 }
166 }
167 None
168 }
169}
170
171fn clean_block_doc_comment(text: &str) -> String {
172 text.strip_prefix("/**")
173 .unwrap_or(text)
174 .strip_suffix("*/")
175 .unwrap_or(text)
176 .lines()
177 .map(|l| l.trim().strip_prefix('*').unwrap_or(l).trim())
178 .filter(|l| !l.is_empty())
179 .collect::<Vec<_>>()
180 .join(" ")
181}
182
183#[cfg(test)]
184mod tests {
185 use super::*;
186 use crate::validate_unused_kinds_audit;
187
188 #[test]
191 fn unused_node_kinds_audit() {
192 #[rustfmt::skip]
193 let documented_unused: &[&str] = &[
194 "bitfield_clause", "declaration_list", "enumerator", "enumerator_list", "field_declaration", "field_declaration_list", "field_expression", "field_identifier", "identifier", "linkage_specification", "parameter_declaration", "primitive_type", "sized_type_specifier", "statement_identifier", "storage_class_specifier", "type_descriptor", "type_identifier", "type_qualifier", "union_specifier", "else_clause", "alignof_expression", "assignment_expression", "binary_expression", "call_expression", "cast_expression", "comma_expression", "compound_literal_expression", "extension_expression", "generic_expression", "gnu_asm_expression", "offsetof_expression", "parenthesized_expression","pointer_expression", "sizeof_expression", "subscript_expression", "unary_expression", "update_expression", "abstract_function_declarator", "preproc_elif", "preproc_elifdef", "preproc_else", "preproc_function_def", "preproc_if", "preproc_ifdef", "alignas_qualifier", "attribute_declaration", "attribute_specifier", "attributed_statement", "expression_statement", "gnu_asm_qualifier", "labeled_statement", "macro_type_specifier", "ms_based_modifier", "ms_call_modifier", "ms_declspec_modifier", "ms_pointer_modifier", "ms_restrict_modifier", "ms_signed_ptr_modifier", "ms_unaligned_ptr_modifier", "ms_unsigned_ptr_modifier", "seh_except_clause", "seh_finally_clause", "seh_leave_statement", "seh_try_statement", "function_definition",
275 "if_statement",
276 "conditional_expression",
277 "case_statement",
278 "continue_statement",
279 "for_statement",
280 "while_statement",
281 "return_statement",
282 "break_statement",
283 "switch_statement",
284 "compound_statement",
285 "do_statement",
286 "goto_statement",
287 ];
288
289 validate_unused_kinds_audit(&C, documented_unused)
290 .expect("C unused node kinds audit failed");
291 }
292}