use crate::manifest::{RuleCategory, Severity};
use crate::rules::{CertRule, RuleViolation};
use crate::utility::cert_c::ast_utils::get_node_text;
use std::collections::HashSet;
use tree_sitter::Node;
pub struct Dcl12C;
impl CertRule for Dcl12C {
fn rule_id(&self) -> &'static str {
"DCL12-C"
}
fn description(&self) -> &'static str {
"Implement abstract data types using opaque types"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"DCL12-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
let mut full_struct_defs: Vec<(String, Node)> = Vec::new();
let mut has_extern_functions = false;
let mut structs_used_in_externs: HashSet<String> = HashSet::new();
self.scan_file(
node,
source,
&mut full_struct_defs,
&mut has_extern_functions,
&mut structs_used_in_externs,
);
if has_extern_functions {
for (struct_name, struct_node) in &full_struct_defs {
if structs_used_in_externs.contains(struct_name) {
let start_point = struct_node.start_position();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Low,
message: format!(
"Struct '{}' exposes implementation details in what appears to be a public interface. \
Consider using an opaque type (forward declaration) to hide internal structure.",
struct_name
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(format!(
"Replace full struct definition with forward declaration: 'struct {};' \
Move the full definition to an implementation file.",
struct_name
)),
..Default::default()
});
}
}
}
violations
}
}
impl Dcl12C {
fn scan_file<'a>(
&self,
node: &Node<'a>,
source: &str,
full_struct_defs: &mut Vec<(String, Node<'a>)>,
has_extern_functions: &mut bool,
structs_used_in_externs: &mut HashSet<String>,
) {
if node.kind() == "struct_specifier" {
if let Some(name_node) = node.child_by_field_name("name") {
let struct_name = get_node_text(&name_node, source).to_string();
if node.child_by_field_name("body").is_some() {
full_struct_defs.push((struct_name, *node));
}
}
}
if node.kind() == "declaration" {
let decl_text = get_node_text(node, source);
if decl_text.starts_with("extern ") && decl_text.contains('(') {
*has_extern_functions = true;
self.extract_struct_types_from_decl(node, source, structs_used_in_externs);
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.scan_file(
&child,
source,
full_struct_defs,
has_extern_functions,
structs_used_in_externs,
);
}
}
}
fn extract_struct_types_from_decl(
&self,
node: &Node,
source: &str,
structs_used: &mut HashSet<String>,
) {
if node.kind() == "type_identifier" {
let type_name = get_node_text(node, source).to_string();
structs_used.insert(type_name);
}
if node.kind() == "struct_specifier" {
if let Some(name_node) = node.child_by_field_name("name") {
let struct_name = get_node_text(&name_node, source).to_string();
structs_used.insert(struct_name);
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.extract_struct_types_from_decl(&child, source, structs_used);
}
}
}
}