1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 BISSELL Homecare, Inc.
//! DCL40-C: Do not create incompatible declarations of the same function or object
//!
//! This rule detects violations where a function or object is declared multiple
//! times with incompatible types. This causes undefined behavior.
//!
//! CERT C reference:
//! https://wiki.sei.cmu.edu/confluence/display/c/DCL40-C.+Do+not+create+incompatible+declarations+of+the+same+function+or+object
use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use std::cell::RefCell;
use std::collections::HashMap;
use tree_sitter::Node;
#[derive(Debug)]
pub struct Dcl40C {
// Track function declarations: name -> (return_type, param_types)
function_decls: RefCell<HashMap<String, (String, Vec<String>)>>,
// Track object declarations: name -> type
object_decls: RefCell<HashMap<String, String>>,
}
impl Dcl40C {
pub fn new() -> Self {
Dcl40C {
function_decls: RefCell::new(HashMap::new()),
object_decls: RefCell::new(HashMap::new()),
}
}
/// Get function name from declarator
fn get_function_name(&self, node: &Node, source: &str) -> Option<String> {
match node.kind() {
"identifier" => Some(get_node_text(node, source).to_string()),
"function_declarator" => {
if let Some(declarator) = node.child_by_field_name("declarator") {
self.get_function_name(&declarator, source)
} else {
None
}
}
"pointer_declarator" => {
if let Some(declarator) = node.child_by_field_name("declarator") {
self.get_function_name(&declarator, source)
} else {
None
}
}
_ => None,
}
}
/// Get return type from declaration
fn get_return_type(&self, node: &Node, source: &str) -> String {
if let Some(type_node) = node.child_by_field_name("type") {
get_node_text(&type_node, source).to_string()
} else {
"int".to_string() // Default in old C
}
}
/// Check if a declarator is a function declarator
fn is_function_declarator(&self, node: &Node) -> bool {
match node.kind() {
"function_declarator" => true,
"pointer_declarator" => {
if let Some(declarator) = node.child_by_field_name("declarator") {
self.is_function_declarator(&declarator)
} else {
false
}
}
_ => false,
}
}
/// Get parameter types from function declarator
fn get_param_types(&self, node: &Node, source: &str) -> Vec<String> {
let mut params = Vec::new();
if node.kind() == "function_declarator" {
if let Some(parameters) = node.child_by_field_name("parameters") {
let mut cursor = parameters.walk();
for child in parameters.children(&mut cursor) {
if child.kind() == "parameter_declaration" {
if let Some(type_node) = child.child_by_field_name("type") {
params.push(get_node_text(&type_node, source).to_string());
}
} else if child.kind() == "variadic_parameter" {
params.push("...".to_string());
}
}
}
} else if node.kind() == "pointer_declarator" {
if let Some(declarator) = node.child_by_field_name("declarator") {
return self.get_param_types(&declarator, source);
}
}
params
}
/// Check function declarations for incompatibilities
fn check_function_declaration(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
if let Some(declarator) = node.child_by_field_name("declarator") {
if self.is_function_declarator(&declarator) {
if let Some(name) = self.get_function_name(&declarator, source) {
let return_type = self.get_return_type(node, source);
let param_types = self.get_param_types(&declarator, source);
let mut function_decls = self.function_decls.borrow_mut();
if let Some((prev_return_type, prev_param_types)) = function_decls.get(&name) {
// Check for incompatible return types
if *prev_return_type != return_type {
violations.push(RuleViolation {
rule_id: "DCL40-C".to_string(),
severity: Severity::High,
line: node.start_position().row + 1,
column: node.start_position().column + 1,
message: format!(
"Incompatible declarations of function '{}': return types '{}' and '{}'",
name, prev_return_type, return_type
),
file_path: String::new(),
suggestion: Some("Ensure all declarations of the same function have identical signatures".to_string()),
requires_manual_review: Some(false),
});
}
// Check for incompatible parameter types
else if prev_param_types.len() != param_types.len()
|| prev_param_types
.iter()
.zip(¶m_types)
.any(|(a, b)| a != b)
{
violations.push(RuleViolation {
rule_id: "DCL40-C".to_string(),
severity: Severity::High,
line: node.start_position().row + 1,
column: node.start_position().column + 1,
message: format!(
"Incompatible declarations of function '{}': parameter types differ",
name
),
file_path: String::new(),
suggestion: Some("Ensure all declarations of the same function have identical signatures".to_string()),
requires_manual_review: Some(false),
});
}
} else {
// First declaration - store it
function_decls.insert(name, (return_type, param_types));
}
}
}
}
}
/// Check object declarations for incompatibilities
fn check_object_declaration(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
if let Some(declarator) = node.child_by_field_name("declarator") {
// Skip function declarators - handled separately
if self.is_function_declarator(&declarator) {
return;
}
// Get variable name
if let Some(name) = self.get_variable_name(&declarator, source) {
// Get type information
let type_info = self.get_object_type(node, &declarator, source);
let mut object_decls = self.object_decls.borrow_mut();
if let Some(prev_type) = object_decls.get(&name) {
// Check for incompatible types
if prev_type != &type_info {
violations.push(RuleViolation {
rule_id: "DCL40-C".to_string(),
severity: Severity::High,
line: node.start_position().row + 1,
column: node.start_position().column + 1,
message: format!(
"Incompatible declarations of object '{}': types '{}' and '{}'",
name, prev_type, type_info
),
file_path: String::new(),
suggestion: Some(
"Ensure all declarations of the same object have identical types"
.to_string(),
),
requires_manual_review: Some(false),
});
}
} else {
// First declaration - store it
object_decls.insert(name, type_info);
}
}
}
}
/// Get variable name from declarator
fn get_variable_name(&self, declarator: &Node, source: &str) -> Option<String> {
match declarator.kind() {
"identifier" => Some(get_node_text(declarator, source).to_string()),
"pointer_declarator" | "array_declarator" | "init_declarator" => {
if let Some(inner) = declarator.child_by_field_name("declarator") {
self.get_variable_name(&inner, source)
} else {
// Try to find identifier child
for i in 0..declarator.child_count() {
if let Some(child) = declarator.child(i) {
if child.kind() == "identifier" {
return Some(get_node_text(&child, source).to_string());
}
if let Some(name) = self.get_variable_name(&child, source) {
return Some(name);
}
}
}
None
}
}
_ => {
// Search for identifier in children
for i in 0..declarator.child_count() {
if let Some(child) = declarator.child(i) {
if child.kind() == "identifier" {
return Some(get_node_text(&child, source).to_string());
}
}
}
None
}
}
}
/// Get object type as a normalized string
fn get_object_type(&self, decl_node: &Node, declarator: &Node, source: &str) -> String {
let base_type = self.get_return_type(decl_node, source);
let declarator_type = self.get_declarator_type(declarator, source);
// Normalize: treat int[] and int* as incompatible for extern declarations
format!("{}{}", base_type, declarator_type)
}
/// Get type modifier from declarator (*, [], etc.)
fn get_declarator_type(&self, declarator: &Node, source: &str) -> String {
match declarator.kind() {
"pointer_declarator" => {
let inner = if let Some(inner) = declarator.child_by_field_name("declarator") {
self.get_declarator_type(&inner, source)
} else {
String::new()
};
format!("*{}", inner)
}
"array_declarator" => {
let inner = if let Some(inner) = declarator.child_by_field_name("declarator") {
self.get_declarator_type(&inner, source)
} else {
String::new()
};
format!("{}[]", inner)
}
"init_declarator" => {
if let Some(inner) = declarator.child_by_field_name("declarator") {
self.get_declarator_type(&inner, source)
} else {
String::new()
}
}
_ => String::new(),
}
}
/// Check declarations — only at file scope (direct children of translation_unit
/// or preproc_* blocks). Declarations inside function bodies are local variables
/// and cannot conflict with file-scope declarations in the DCL40-C sense.
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
match node.kind() {
"translation_unit" => {
// Only process direct children at file scope
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"declaration" => {
self.check_function_declaration(&child, source, violations);
self.check_object_declaration(&child, source, violations);
}
"function_definition" => {
self.check_function_declaration(&child, source, violations);
}
kind if kind.starts_with("preproc_") => {
// Recurse into preprocessor blocks at file scope
self.check_node(&child, source, violations);
}
_ => {}
}
}
}
kind if kind.starts_with("preproc_") => {
// Process direct children of preprocessor blocks
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"declaration" => {
self.check_function_declaration(&child, source, violations);
self.check_object_declaration(&child, source, violations);
}
"function_definition" => {
self.check_function_declaration(&child, source, violations);
}
kind if kind.starts_with("preproc_") => {
self.check_node(&child, source, violations);
}
_ => {}
}
}
}
_ => {}
}
}
}
impl Default for Dcl40C {
fn default() -> Self {
Self::new()
}
}
impl CertRule for Dcl40C {
fn rule_id(&self) -> &'static str {
"DCL40-C"
}
fn description(&self) -> &'static str {
"Do not create incompatible declarations of the same function or object"
}
fn severity(&self) -> Severity {
Severity::High
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"DCL40-C"
}
fn check(&self, root_node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
// Clear previous state
self.function_decls.borrow_mut().clear();
self.object_decls.borrow_mut().clear();
// Check the tree
self.check_node(root_node, source, &mut violations);
violations
}
}