use crate::c_cpp;
use crate::external_packages::ResolvedPackage;
use crate::{Export, Import, Language, Symbol, SymbolKind, Visibility, VisibilityMechanism};
use std::path::{Path, PathBuf};
use tree_sitter::Node;
pub struct Cpp;
impl Language for Cpp {
fn name(&self) -> &'static str {
"C++"
}
fn extensions(&self) -> &'static [&'static str] {
&["cpp", "cc", "cxx", "hpp", "hh", "hxx"]
}
fn grammar_name(&self) -> &'static str {
"cpp"
}
fn has_symbols(&self) -> bool {
true
}
fn container_kinds(&self) -> &'static [&'static str] {
&["class_specifier", "struct_specifier"]
}
fn function_kinds(&self) -> &'static [&'static str] {
&["function_definition"]
}
fn type_kinds(&self) -> &'static [&'static str] {
&[
"class_specifier",
"struct_specifier",
"enum_specifier",
"type_definition",
]
}
fn import_kinds(&self) -> &'static [&'static str] {
&["preproc_include"]
}
fn public_symbol_kinds(&self) -> &'static [&'static str] {
&["function_definition", "class_specifier", "struct_specifier"]
}
fn visibility_mechanism(&self) -> VisibilityMechanism {
VisibilityMechanism::HeaderBased }
fn scope_creating_kinds(&self) -> &'static [&'static str] {
&[
"for_statement",
"for_range_loop",
"while_statement",
"compound_statement",
"lambda_expression",
"namespace_definition",
]
}
fn control_flow_kinds(&self) -> &'static [&'static str] {
&[
"if_statement",
"for_statement",
"for_range_loop",
"while_statement",
"do_statement",
"switch_statement",
"return_statement",
"break_statement",
"continue_statement",
"throw_statement",
"goto_statement",
"try_statement",
]
}
fn complexity_nodes(&self) -> &'static [&'static str] {
&[
"if_statement",
"for_statement",
"for_range_loop",
"while_statement",
"do_statement",
"switch_statement",
"case_statement",
"try_statement",
"catch_clause",
"throw_statement",
"&&",
"||",
"conditional_expression",
]
}
fn nesting_nodes(&self) -> &'static [&'static str] {
&[
"if_statement",
"for_statement",
"for_range_loop",
"while_statement",
"do_statement",
"switch_statement",
"try_statement",
"function_definition",
"class_specifier",
"struct_specifier",
"namespace_definition",
"lambda_expression",
]
}
fn signature_suffix(&self) -> &'static str {
" {}"
}
fn extract_function(&self, node: &Node, content: &str, in_container: bool) -> Option<Symbol> {
let declarator = node.child_by_field_name("declarator")?;
let name = find_identifier(&declarator, content)?;
Some(Symbol {
name: name.to_string(),
kind: if in_container {
SymbolKind::Method
} else {
SymbolKind::Function
},
signature: name.to_string(),
docstring: None,
attributes: Vec::new(),
start_line: node.start_position().row + 1,
end_line: node.end_position().row + 1,
visibility: Visibility::Public,
children: Vec::new(),
is_interface_impl: false,
implements: Vec::new(),
})
}
fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
let name = self.node_name(node, content)?;
let kind = if node.kind() == "class_specifier" {
SymbolKind::Class
} else {
SymbolKind::Struct
};
Some(Symbol {
name: name.to_string(),
kind,
signature: format!("{} {}", kind.as_str(), name),
docstring: None,
attributes: Vec::new(),
start_line: node.start_position().row + 1,
end_line: node.end_position().row + 1,
visibility: Visibility::Public,
children: Vec::new(),
is_interface_impl: false,
implements: Vec::new(),
})
}
fn extract_type(&self, node: &Node, content: &str) -> Option<Symbol> {
self.extract_container(node, content)
}
fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
None
}
fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
Vec::new()
}
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 extract_public_symbols(&self, node: &Node, content: &str) -> Vec<Export> {
let kind = match node.kind() {
"function_definition" => SymbolKind::Function,
"class_specifier" => SymbolKind::Class,
"struct_specifier" => SymbolKind::Struct,
_ => return Vec::new(),
};
if let Some(name) = self.node_name(node, content) {
vec![Export {
name: name.to_string(),
kind,
line: node.start_position().row + 1,
}]
} else {
Vec::new()
}
}
fn is_public(&self, _node: &Node, _content: &str) -> bool {
true }
fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
Visibility::Public
}
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 embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
None
}
fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
node.child_by_field_name("body")
}
fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
false
}
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 find_identifier(&declarator, content);
}
None
}
fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
let ext = path.extension()?.to_str()?;
if !["cpp", "cc", "cxx", "hpp", "hh", "hxx", "h"].contains(&ext) {
return None;
}
Some(path.to_string_lossy().to_string())
}
fn module_name_to_paths(&self, module: &str) -> Vec<String> {
vec![module.to_string()]
}
fn is_stdlib_import(&self, include: &str, _project_root: &Path) -> bool {
let stdlib = [
"iostream",
"vector",
"string",
"map",
"set",
"algorithm",
"memory",
"utility",
"functional",
"iterator",
"numeric",
"cstdio",
"cstdlib",
"cstring",
"cmath",
"climits",
];
stdlib.contains(&include)
}
fn find_package_cache(&self, _project_root: &Path) -> Option<PathBuf> {
None
}
fn find_stdlib(&self, _project_root: &Path) -> Option<PathBuf> {
c_cpp::find_cpp_include_paths().into_iter().next()
}
fn package_module_name(&self, entry_name: &str) -> String {
entry_name.to_string()
}
fn discover_packages(&self, source: &crate::PackageSource) -> Vec<(String, PathBuf)> {
self.discover_recursive_packages(&source.path, &source.path)
}
fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
if path.is_file() {
Some(path.to_path_buf())
} else {
None
}
}
fn lang_key(&self) -> &'static str {
"cpp"
}
fn resolve_local_import(
&self,
include: &str,
current_file: &Path,
_project_root: &Path,
) -> Option<PathBuf> {
let header = include
.trim_start_matches('"')
.trim_end_matches('"')
.trim_start_matches('<')
.trim_end_matches('>');
let current_dir = current_file.parent()?;
let relative = current_dir.join(header);
if relative.is_file() {
return Some(relative);
}
if !header.contains('.') {
for ext in &[".h", ".hpp", ".hxx", ".hh"] {
let with_ext = current_dir.join(format!("{}{}", header, ext));
if with_ext.is_file() {
return Some(with_ext);
}
}
}
None
}
fn resolve_external_import(
&self,
include: &str,
_project_root: &Path,
) -> Option<ResolvedPackage> {
let include_paths = c_cpp::find_cpp_include_paths();
c_cpp::resolve_cpp_include(include, &include_paths)
}
fn get_version(&self, _project_root: &Path) -> Option<String> {
c_cpp::get_gcc_version()
}
fn indexable_extensions(&self) -> &'static [&'static str] {
&["cpp", "hpp", "cc", "hh", "cxx", "hxx", "h"]
}
fn package_sources(&self, _project_root: &Path) -> Vec<crate::PackageSource> {
use crate::{PackageSource, PackageSourceKind};
c_cpp::find_cpp_include_paths()
.into_iter()
.map(|path| PackageSource {
name: "includes",
path,
kind: PackageSourceKind::Recursive,
version_specific: false,
})
.collect()
}
fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
use crate::traits::skip_dotfiles;
if skip_dotfiles(name) {
return true;
}
if is_dir && name == "bits" {
return true;
}
if is_dir {
return false;
}
let is_header = name.ends_with(".h")
|| name.ends_with(".hpp")
|| name.ends_with(".hxx")
|| name.ends_with(".hh")
|| (!name.contains('.') && !name.contains('-')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'));
!is_header
}
}
fn find_identifier<'a>(node: &Node, content: &'a str) -> Option<&'a str> {
if node.kind() == "identifier" || node.kind() == "field_identifier" {
return Some(&content[node.byte_range()]);
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(id) = find_identifier(&child, content) {
return Some(id);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::validate_unused_kinds_audit;
#[test]
fn unused_node_kinds_audit() {
#[rustfmt::skip]
let documented_unused: &[&str] = &[
"access_specifier", "base_class_clause", "bitfield_clause", "condition_clause", "declaration", "declaration_list", "default_method_clause", "delete_method_clause", "dependent_type", "destructor_name", "enumerator", "enumerator_list", "field_declaration", "field_declaration_list", "field_expression", "field_identifier", "identifier", "init_statement", "linkage_specification", "module_name", "module_partition", "namespace_identifier", "nested_namespace_specifier", "operator_name", "parameter_declaration", "primitive_type", "pure_virtual_clause", "ref_qualifier", "sized_type_specifier", "statement_identifier", "static_assert_declaration", "storage_class_specifier", "structured_binding_declarator", "type_descriptor", "type_identifier", "type_parameter_declaration", "type_qualifier", "union_specifier", "using_declaration", "variadic_parameter_declaration", "variadic_type_parameter_declaration", "virtual_specifier",
"else_clause", "noexcept",
"alignof_expression", "assignment_expression", "binary_expression", "call_expression", "cast_expression", "co_await_expression", "co_return_statement", "co_yield_statement", "comma_expression", "compound_literal_expression", "delete_expression", "extension_expression", "fold_expression", "generic_expression", "gnu_asm_expression", "new_expression", "offsetof_expression", "parenthesized_expression", "pointer_expression", "reflect_expression", "sizeof_expression", "splice_expression", "subscript_expression", "unary_expression", "update_expression",
"template_declaration", "template_function", "template_method", "template_template_parameter_declaration", "template_type",
"lambda_capture_initializer", "lambda_capture_specifier", "lambda_declarator", "lambda_default_capture", "lambda_specifier",
"abstract_function_declarator", "explicit_function_specifier", "explicit_object_parameter_declaration", "function_declarator", "operator_cast", "optional_parameter_declaration", "optional_type_parameter_declaration", "placeholder_type_specifier", "pointer_type_declarator", "trailing_return_type",
"concept_definition", "requires_clause", "requires_expression", "type_requirement",
"export_declaration", "global_module_fragment_declaration", "import_declaration", "module_declaration", "private_module_fragment_declaration",
"preproc_elif", "preproc_elifdef", "preproc_else", "preproc_function_def", "preproc_if", "preproc_ifdef",
"splice_specifier", "splice_type_specifier",
"alias_declaration", "alignas_qualifier", "attribute_declaration", "attribute_specifier", "attributed_statement", "consteval_block_declaration", "decltype", "expansion_statement", "expression_statement", "friend_declaration", "gnu_asm_qualifier", "labeled_statement", "namespace_alias_definition", "qualified_identifier", "throw_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", ];
validate_unused_kinds_audit(&Cpp, documented_unused)
.expect("C++ unused node kinds audit failed");
}
}