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 Int12C;
impl CertRule for Int12C {
fn rule_id(&self) -> &'static str {
"INT12-C"
}
fn description(&self) -> &'static str {
"Do not make assumptions about the type of a plain int bit-field when used in an expression"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"INT12-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.traverse(node, source, &mut violations);
violations
}
}
impl Int12C {
fn traverse(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "field_declaration" {
self.check_field_declaration(node, source, violations);
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.traverse(&child, source, violations);
}
}
}
fn check_field_declaration(
&self,
field: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
let field_text = get_node_text(field, source);
if !field_text.contains(':') {
return;
}
if !field_text.contains("int") {
return;
}
if field_text.contains("signed") || field_text.contains("unsigned") {
return;
}
if field_text.contains("_t") {
return;
}
let (field_name, line, column) = self.get_field_info(field, source).unwrap_or_else(|| {
(
"unknown".to_string(),
field.start_position().row + 1,
field.start_position().column + 1,
)
});
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
message: format!(
"Bit-field '{}' uses plain 'int' with implementation-defined signedness. \
Use explicit 'signed int' or 'unsigned int' instead.",
field_name
),
severity: self.severity(),
line,
column,
file_path: String::new(),
suggestion: Some(format!(
"Change 'int {}' to 'signed int {}' or 'unsigned int {}'",
field_name, field_name, field_name
)),
requires_manual_review: None,
});
}
fn get_field_info(&self, field: &Node, source: &str) -> Option<(String, usize, usize)> {
for i in 0..field.child_count() {
if let Some(child) = field.child(i) {
if child.kind() == "field_declarator" {
if let Some(identifier) = self.find_identifier(&child, source) {
return Some((
identifier,
field.start_position().row + 1,
field.start_position().column + 1,
));
}
}
}
}
None
}
fn find_identifier(&self, node: &Node, source: &str) -> Option<String> {
if node.kind() == "field_identifier" || node.kind() == "identifier" {
return Some(get_node_text(node, source).to_string());
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if let Some(id) = self.find_identifier(&child, source) {
return Some(id);
}
}
}
None
}
}