normalize_languages/
devicetree.rs1use crate::{ContainerBody, Import, Language, LanguageSymbols};
4use tree_sitter::Node;
5
6pub struct DeviceTree;
8
9impl Language for DeviceTree {
10 fn name(&self) -> &'static str {
11 "DeviceTree"
12 }
13 fn extensions(&self) -> &'static [&'static str] {
14 &["dts", "dtsi"]
15 }
16 fn grammar_name(&self) -> &'static str {
17 "devicetree"
18 }
19
20 fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
21 Some(self)
22 }
23
24 fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
25 if node.kind() != "preproc_include" {
26 return Vec::new();
27 }
28
29 let text = &content[node.byte_range()];
30 let module = text
31 .split('"')
32 .nth(1)
33 .or_else(|| text.split('<').nth(1).and_then(|s| s.split('>').next()))
34 .map(|s| s.to_string());
35
36 if let Some(module) = module {
37 return vec![Import {
38 module,
39 names: Vec::new(),
40 alias: None,
41 is_wildcard: false,
42 is_relative: text.contains('"'),
43 line: node.start_position().row + 1,
44 }];
45 }
46 Vec::new()
47 }
48
49 fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
50 format!("/include/ \"{}\"", import.module)
52 }
53
54 fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
55 Some(*node)
57 }
58
59 fn analyze_container_body(
60 &self,
61 body_node: &Node,
62 content: &str,
63 inner_indent: &str,
64 ) -> Option<ContainerBody> {
65 crate::body::analyze_brace_body(body_node, content, inner_indent)
67 }
68}
69
70impl LanguageSymbols for DeviceTree {}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use crate::validate_unused_kinds_audit;
76
77 #[test]
78 fn unused_node_kinds_audit() {
79 #[rustfmt::skip]
80 let documented_unused: &[&str] = &[
81 "preproc_if", "preproc_ifdef", "preproc_else", "preproc_elif",
83 "preproc_elifdef", "preproc_function_def",
84 "unary_expression", "binary_expression", "conditional_expression",
86 "parenthesized_expression", "call_expression",
87 "identifier", "omit_if_no_ref",
89 ];
90 validate_unused_kinds_audit(&DeviceTree, documented_unused)
91 .expect("DeviceTree unused node kinds audit failed");
92 }
93}