use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use tree_sitter::Node;
pub struct Dcl05C;
impl CertRule for Dcl05C {
fn rule_id(&self) -> &'static str {
"DCL05-C"
}
fn description(&self) -> &'static str {
"Use typedefs of non-pointer types only"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"DCL05-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
let mut pointer_typedefs = std::collections::HashSet::new();
collect_pointer_typedefs(node, source, &mut pointer_typedefs);
check_typedef_declarations(node, source, &mut violations);
check_external_pointer_typedef_usage(node, source, &mut violations, &pointer_typedefs);
check_complex_function_pointers(node, source, &mut violations);
violations
}
}
fn collect_pointer_typedefs(
node: &Node,
source: &str,
pointer_typedefs: &mut std::collections::HashSet<String>,
) {
if node.kind() == "type_definition" {
if contains_pointer_declarator(node) {
if let Some(typedef_name) = extract_typedef_name(node, source) {
pointer_typedefs.insert(typedef_name);
}
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
collect_pointer_typedefs(&child, source, pointer_typedefs);
}
}
fn check_typedef_declarations(node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "type_definition" {
if is_pointer_typedef(node, source) {
if is_const_pointer_typedef(node, source) {
return;
}
let typedef_name =
extract_typedef_name(node, source).unwrap_or_else(|| "unknown".to_string());
violations.push(RuleViolation {
rule_id: "DCL05-C".to_string(),
file_path: "".to_string(),
message: format!(
"Typedef '{}' defines a pointer type, which can cause confusion with const-qualification",
typedef_name
),
line: node.start_position().row + 1,
column: node.start_position().column,
severity: Severity::Medium,
suggestion: Some("Use typedef of non-pointer type and declare pointers explicitly at point of use".to_string()),
requires_manual_review: Some(false),
});
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
check_typedef_declarations(&child, source, violations);
}
}
fn is_pointer_typedef(node: &Node, _source: &str) -> bool {
contains_pointer_declarator(node)
}
fn contains_pointer_declarator(node: &Node) -> bool {
if node.kind() == "pointer_declarator" {
return true;
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if contains_pointer_declarator(&child) {
return true;
}
}
false
}
fn extract_typedef_name(node: &Node, source: &str) -> Option<String> {
find_type_identifier(node, source)
}
fn find_type_identifier(node: &Node, source: &str) -> Option<String> {
if node.kind() == "type_identifier" {
return Some(get_node_text(node, source).to_string());
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(id) = find_type_identifier(&child, source) {
return Some(id);
}
}
None
}
fn is_const_pointer_typedef(node: &Node, source: &str) -> bool {
let typedef_text = get_node_text(node, source);
let has_const = typedef_text.contains("const");
if !has_const {
return false;
}
if let Some(const_pos) = typedef_text.find("const") {
if let Some(star_pos) = typedef_text.find('*') {
return const_pos < star_pos;
}
}
false
}
fn check_external_pointer_typedef_usage(
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
defined_typedefs: &std::collections::HashSet<String>,
) {
if node.kind() == "parameter_declaration" || node.kind() == "declaration" {
if let Some(type_id_node) = find_first_type_identifier_node(node) {
let type_name = get_node_text(&type_id_node, source);
if is_likely_external_pointer_typedef(&type_name)
&& !defined_typedefs.contains(type_name)
{
violations.push(RuleViolation {
rule_id: "DCL05-C".to_string(),
file_path: "".to_string(),
message: format!(
"Usage of external pointer typedef '{}' (likely from header). \
Pointer typedefs can cause confusion with const-qualification",
type_name
),
line: type_id_node.start_position().row + 1,
column: type_id_node.start_position().column,
severity: Severity::Medium,
suggestion: Some(
"Avoid using pointer typedefs from external headers, or use const-qualified versions".to_string()
),
requires_manual_review: Some(false),
});
}
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
check_external_pointer_typedef_usage(&child, source, violations, defined_typedefs);
}
}
#[allow(clippy::manual_find)]
fn find_first_type_identifier_node<'a>(node: &'a Node) -> Option<Node<'a>> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "type_identifier" {
return Some(child);
}
}
None
}
fn is_likely_external_pointer_typedef(type_name: &str) -> bool {
if type_name.starts_with("LP") && type_name.len() > 2 {
return !type_name.starts_with("LPC");
}
if type_name.starts_with('P')
&& type_name.len() > 1
&& type_name.chars().nth(1).unwrap().is_uppercase()
{
return true;
}
if type_name.ends_with("PTR") || type_name.ends_with("Ptr") {
return true;
}
false
}
fn check_complex_function_pointers(node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "function_declarator" || node.kind() == "declaration" {
let text = get_node_text(node, source);
if is_complex_function_pointer_syntax(&text) {
violations.push(RuleViolation {
rule_id: "DCL05-C".to_string(),
file_path: "".to_string(),
message: "Complex function pointer declaration should use typedef for clarity"
.to_string(),
line: node.start_position().row + 1,
column: node.start_position().column,
severity: Severity::Medium,
suggestion: Some(
"Use typedef to simplify complex function pointer declarations".to_string(),
),
requires_manual_review: Some(false),
});
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
check_complex_function_pointers(&child, source, violations);
}
}
fn is_complex_function_pointer_syntax(text: &str) -> bool {
let has_func_ptr_return = text.contains("(*") && text.matches('(').count() >= 3;
let has_unnamed_func_ptr = text.contains("(*)(");
has_func_ptr_return || has_unnamed_func_ptr
}