use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils;
use tree_sitter::Node;
pub struct Dcl03C;
impl CertRule for Dcl03C {
fn rule_id(&self) -> &'static str {
"DCL03-C"
}
fn description(&self) -> &'static str {
"Use a static assertion to test the value of a constant expression"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"DCL03-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
if node.kind() == "call_expression" {
if let Some(function) = node.child_by_field_name("function") {
let func_text = ast_utils::get_node_text(&function, source);
if func_text == "assert" {
if let Some(arguments) = node.child_by_field_name("arguments") {
if is_constant_expression(&arguments, source) {
let start_point = node.start_position();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Low,
message: "Runtime assert() used with constant expression; consider using static_assert() instead".to_string(),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"Replace assert() with static_assert() to evaluate at compile time"
.to_string(),
),
..Default::default()
});
}
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
violations.extend(self.check(&child, source));
}
}
violations
}
}
fn is_constant_expression(node: &Node, source: &str) -> bool {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() != "(" && child.kind() != ")" && child.kind() != "," {
return is_constant_expr_recursive(&child, source);
}
}
}
false
}
fn is_constant_expr_recursive(node: &Node, source: &str) -> bool {
match node.kind() {
"sizeof_expression" => true,
"number_literal" => true,
"char_literal" => true,
"binary_expression" => {
let mut has_constant_operands = true;
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if !is_operator(&child, source) {
if !is_constant_expr_recursive(&child, source) {
has_constant_operands = false;
break;
}
}
}
}
has_constant_operands
}
"parenthesized_expression" => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() != "(" && child.kind() != ")" {
return is_constant_expr_recursive(&child, source);
}
}
}
false
}
"type_identifier" | "primitive_type" | "sized_type_specifier" | "struct_specifier" => true,
"field_expression" => {
if let Some(parent) = node.parent() {
if parent.kind() == "sizeof_expression" {
return true;
}
}
false
}
"cast_expression" => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() != "("
&& child.kind() != ")"
&& child.kind() != "type_descriptor"
{
return is_constant_expr_recursive(&child, source);
}
}
}
false
}
"unary_expression" => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if !is_operator(&child, source) {
return is_constant_expr_recursive(&child, source);
}
}
}
false
}
"identifier" => {
let text = ast_utils::get_node_text(node, source);
if text
.chars()
.all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
{
return true; }
false
}
_ => false,
}
}
fn is_operator(node: &Node, source: &str) -> bool {
let text = ast_utils::get_node_text(node, source);
matches!(
text,
"+" | "-"
| "*"
| "/"
| "%"
| "=="
| "!="
| "<"
| ">"
| "<="
| ">="
| "&&"
| "||"
| "&"
| "|"
| "^"
| "<<"
| ">>"
| "!"
| "~"
)
}