use crate::{Import, Language, LanguageSymbols};
use tree_sitter::Node;
pub struct C;
impl Language for C {
fn name(&self) -> &'static str {
"C"
}
fn extensions(&self) -> &'static [&'static str] {
&["c", "h"]
}
fn grammar_name(&self) -> &'static str {
"c"
}
fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
Some(self)
}
fn signature_suffix(&self) -> &'static str {
" {}"
}
fn extract_docstring(&self, node: &Node, content: &str) -> Option<String> {
let mut prev = node.prev_sibling();
while let Some(sibling) = prev {
if sibling.kind() == "comment" {
let text = &content[sibling.byte_range()];
if text.starts_with("/**") {
return Some(clean_block_doc_comment(text));
}
return None;
}
if sibling.kind() != "preproc_def" && sibling.kind() != "preproc_ifdef" {
return None;
}
prev = sibling.prev_sibling();
}
None
}
fn extract_attributes(&self, node: &Node, content: &str) -> Vec<String> {
let mut attrs = Vec::new();
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"attribute_declaration" | "attribute_specifier" | "ms_declspec_modifier" => {
attrs.push(content[child.byte_range()].trim().to_string());
}
_ => {}
}
}
attrs
}
fn build_signature(&self, node: &Node, content: &str) -> String {
match node.kind() {
"function_definition" => {
if let Some(declarator) = node.child_by_field_name("declarator")
&& let Some(name) = C::find_identifier(&declarator, content)
{
return name.to_string();
}
let text = &content[node.byte_range()];
text.lines().next().unwrap_or(text).trim().to_string()
}
"struct_specifier" | "enum_specifier" => {
let name = self.node_name(node, content).unwrap_or("");
let keyword = if node.kind() == "struct_specifier" {
"struct"
} else {
"enum"
};
format!("{} {}", keyword, name)
}
_ => {
let text = &content[node.byte_range()];
text.lines().next().unwrap_or(text).trim().to_string()
}
}
}
fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
if node.kind() != "preproc_include" {
return Vec::new();
}
let line = node.start_position().row + 1;
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "string_literal" || child.kind() == "system_lib_string" {
let text = &content[child.byte_range()];
let module = text
.trim_matches(|c| c == '"' || c == '<' || c == '>')
.to_string();
let is_relative = text.starts_with('"');
return vec![Import {
module,
names: Vec::new(),
alias: None,
is_wildcard: false,
is_relative,
line,
}];
}
}
Vec::new()
}
fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
if import.module.starts_with('<') || import.module.ends_with('>') {
format!("#include {}", import.module)
} else {
format!("#include \"{}\"", import.module)
}
}
fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
let name = symbol.name.as_str();
match symbol.kind {
crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
crate::SymbolKind::Module => name == "tests" || name == "test",
_ => false,
}
}
fn test_file_globs(&self) -> &'static [&'static str] {
&["**/test_*.c", "**/*_test.c", "**/tests/**/*.c"]
}
fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
node.child_by_field_name("body")
}
fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
if let Some(name_node) = node.child_by_field_name("name") {
return Some(&content[name_node.byte_range()]);
}
if let Some(declarator) = node.child_by_field_name("declarator") {
return Self::find_identifier(&declarator, content);
}
None
}
}
impl LanguageSymbols for C {}
impl C {
fn find_identifier<'a>(node: &Node, content: &'a str) -> Option<&'a str> {
if node.kind() == "identifier" {
return Some(&content[node.byte_range()]);
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(id) = Self::find_identifier(&child, content) {
return Some(id);
}
}
None
}
}
fn clean_block_doc_comment(text: &str) -> String {
text.strip_prefix("/**")
.unwrap_or(text)
.strip_suffix("*/")
.unwrap_or(text)
.lines()
.map(|l| l.trim().strip_prefix('*').unwrap_or(l).trim())
.filter(|l| !l.is_empty())
.collect::<Vec<_>>()
.join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::validate_unused_kinds_audit;
#[test]
fn unused_node_kinds_audit() {
#[rustfmt::skip]
let documented_unused: &[&str] = &[
"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",
"if_statement",
"conditional_expression",
"case_statement",
"continue_statement",
"for_statement",
"while_statement",
"return_statement",
"break_statement",
"switch_statement",
"compound_statement",
"do_statement",
"goto_statement",
];
validate_unused_kinds_audit(&C, documented_unused)
.expect("C unused node kinds audit failed");
}
}