sqry_lang_cpp/relations/
queries.rs1use tree_sitter::{Language, Query, QueryError};
9
10pub struct CppQueries {
12 pub classes: Query,
14 pub functions: Query,
16 pub calls: Query,
18}
19
20impl CppQueries {
21 pub fn new(language: &Language) -> Result<Self, QueryError> {
27 Ok(Self {
28 classes: Query::new(language, CLASS_QUERY)?,
29 functions: Query::new(language, FUNCTION_QUERY)?,
30 calls: Query::new(language, CALL_QUERY)?,
31 })
32 }
33}
34
35const CLASS_QUERY: &str = r"
49; Class specifiers: class Foo { ... };
50(class_specifier
51 name: (type_identifier) @class.name) @class
52
53; Struct specifiers: struct Foo { ... };
54(struct_specifier
55 name: (type_identifier) @class.name) @class
56
57; Class specifier inside template declaration
58(template_declaration
59 (class_specifier
60 name: (type_identifier) @class.name) @class) @template_class
61
62; Struct specifier inside template declaration
63(template_declaration
64 (struct_specifier
65 name: (type_identifier) @class.name) @class) @template_struct
66";
67
68const FUNCTION_QUERY: &str = r"
81; Function definitions (free functions and methods)
82(function_definition
83 declarator: (function_declarator
84 declarator: [(identifier) (field_identifier)] @func.name)) @func
85
86; Methods declared inside classes/structs without inline bodies
87(field_declaration
88 declarator: (function_declarator
89 declarator: [(field_identifier) (identifier)] @func.name)) @func
90
91; Template function declarations
92(template_declaration
93 (function_definition
94 declarator: (function_declarator
95 declarator: (identifier) @func.name)) @func) @template_func
96";
97
98const CALL_QUERY: &str = r"
110[
111 ; Direct function calls: foo()
112 (call_expression
113 function: (identifier) @call.name) @call
114
115 ; Qualified/scoped function calls: std::move()
116 (call_expression
117 function: (qualified_identifier) @call.name) @call
118
119 ; Member function calls: object.method()
120 (call_expression
121 function: (field_expression
122 field: [(field_identifier) (identifier)] @call.method)) @call
123]
124";
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 #[test]
131 fn test_queries_compile() {
132 let language = tree_sitter_cpp::LANGUAGE.into();
133 let queries = CppQueries::new(&language);
134 assert!(queries.is_ok(), "Cpp queries should compile successfully");
135 }
136
137 #[test]
138 fn test_class_query_compiles() {
139 let language = tree_sitter_cpp::LANGUAGE.into();
140 let query = Query::new(&language, CLASS_QUERY);
141 assert!(query.is_ok(), "CLASS_QUERY should compile");
142 }
143
144 #[test]
145 fn test_function_query_compiles() {
146 let language = tree_sitter_cpp::LANGUAGE.into();
147 let query = Query::new(&language, FUNCTION_QUERY);
148 assert!(query.is_ok(), "FUNCTION_QUERY should compile");
149 }
150
151 #[test]
152 fn test_call_query_compiles() {
153 let language = tree_sitter_cpp::LANGUAGE.into();
154 let query = Query::new(&language, CALL_QUERY);
155 assert!(query.is_ok(), "CALL_QUERY should compile");
156 }
157}