use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils;
use tree_sitter::Node;
pub struct Pre10C;
impl CertRule for Pre10C {
fn rule_id(&self) -> &'static str {
"PRE10-C"
}
fn description(&self) -> &'static str {
"Wrap multistatement macros in a do-while loop"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"PRE10-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Pre10C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "preproc_def" || node.kind() == "preproc_function_def" {
if let Some(value) = node.child_by_field_name("value") {
let value_text = ast_utils::get_node_text(&value, source);
if self.has_multiple_statements(&value_text) {
if !self.is_wrapped_in_do_while(&value_text) {
let macro_name = if let Some(name) = node.child_by_field_name("name") {
ast_utils::get_node_text(&name, source)
} else {
"unknown"
};
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: format!(
"Multistatement macro '{}' is not wrapped in a do-while loop. \
This can cause issues when used in if statements without braces",
macro_name
),
file_path: String::new(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
suggestion: Some(
"Wrap the macro body in: do { /* statements */ } while (0)"
.to_string(),
),
requires_manual_review: None,
});
}
}
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.check_node(&child, source, violations);
}
}
fn has_multiple_statements(&self, text: &str) -> bool {
let cleaned = text.replace("\\\n", " ").replace("\\\r\n", " ");
let semicolon_count = self.count_semicolons_outside_strings(&cleaned);
if semicolon_count > 1 {
return true;
}
if cleaned.contains('{') && cleaned.contains('}') && semicolon_count > 0 {
return true;
}
false
}
fn count_semicolons_outside_strings(&self, text: &str) -> usize {
let mut count = 0;
let mut in_string = false;
let mut in_char = false;
let mut escaped = false;
let chars: Vec<char> = text.chars().collect();
for c in chars {
if escaped {
escaped = false;
continue;
}
if c == '\\' {
escaped = true;
continue;
}
if c == '"' && !in_char {
in_string = !in_string;
} else if c == '\'' && !in_string {
in_char = !in_char;
} else if c == ';' && !in_string && !in_char {
count += 1;
}
}
count
}
fn is_wrapped_in_do_while(&self, text: &str) -> bool {
let raw = text.replace("\\\r\n", " ").replace("\\\n", " ");
let normalized: String = raw.split_whitespace().collect::<Vec<_>>().join(" ");
let starts_with_do = normalized.starts_with("do {") || normalized.starts_with("do{");
if !starts_with_do {
return false;
}
let endings = [
"while ( 0 )",
"while(0)",
"while (0)",
"while( 0 )",
"while ( 0 );",
"while(0);",
"while (0);",
"while( 0 );",
];
endings.iter().any(|e| normalized.ends_with(e))
}
}