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
#[cfg(feature = "cpp-ast")]
impl CppAstVisitor {
/// Extracts function declarations (complexity ≤10)
#[allow(clippy::cast_possible_truncation)]
fn extract_function_declarations(&mut self, source: &str) -> Result<(), String> {
// Track namespace context while parsing
let mut namespace_stack: Vec<String> = Vec::new();
let mut brace_depth = 0;
let mut class_depth = 0; // Track if we're inside a class
let mut in_multiline_comment = false; // BUG-009 FIX: Track multiline comment state
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
// BUG-009 FIX: Handle multiline comment state
// Check if we're entering a multiline comment
if trimmed.contains("/*") {
in_multiline_comment = true;
}
// Check if we're exiting a multiline comment
if trimmed.contains("*/") {
in_multiline_comment = false;
continue; // Skip the line with the closing comment
}
// Skip if we're inside a multiline comment
if in_multiline_comment {
continue;
}
// Skip single-line comments and preprocessor directives
if trimmed.starts_with("//") || trimmed.starts_with("/*") || trimmed.starts_with("#") {
continue;
}
// Track namespace declarations
if trimmed.starts_with("namespace ") {
if let Some(ns_name) = self.extract_namespace_name(trimmed) {
namespace_stack.push(ns_name);
}
}
// Track class/struct declarations to avoid extracting methods
if trimmed.starts_with("class ") || trimmed.starts_with("struct ") {
class_depth = brace_depth + 1; // Methods will be at this depth or deeper
}
// Track brace depth
brace_depth += trimmed.chars().filter(|&c| c == '{').count() as i32;
brace_depth -= trimmed.chars().filter(|&c| c == '}').count() as i32;
// Reset class depth when we exit the class
if class_depth > 0 && brace_depth < class_depth {
class_depth = 0;
}
// Check for closing namespace
if trimmed.contains("}")
&& !namespace_stack.is_empty()
&& brace_depth < namespace_stack.len() as i32
{
namespace_stack.pop();
}
// Skip if we're inside a class
if class_depth > 0 && brace_depth >= class_depth {
continue;
}
// Check for function declaration
if self.is_function_declaration(trimmed) && !self.is_class_method(trimmed) {
if let Ok(name) = self.extract_function_name(trimmed) {
// Build qualified name with current namespace
let qualified_name = if !namespace_stack.is_empty() {
format!("{}::{}", namespace_stack.join("::"), name)
} else {
name
};
// Check for function visibility
let visibility = if trimmed.contains("static ") {
"private"
} else {
"public"
}
.to_string();
// Check if function is async (C++20 feature)
let is_async = trimmed.contains("async ") || trimmed.contains("co_await ");
self.items.push(AstItem::Function {
name: qualified_name,
visibility: visibility.clone(),
is_async,
line: line_num + 1,
});
}
}
}
Ok(())
}
/// Extracts class method declarations (complexity ≤10)
#[allow(clippy::cast_possible_truncation)]
fn extract_method_declarations(&mut self, source: &str) -> Result<(), String> {
let mut in_class = false;
let mut current_class_name = String::new();
let mut brace_depth = 0;
let mut visibility = "private".to_string();
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
// Skip comments and preprocessor directives
if trimmed.starts_with("//") || trimmed.starts_with("/*") || trimmed.starts_with("#") {
continue;
}
// Track class declarations
if (trimmed.starts_with("class ") || trimmed.starts_with("struct ")) && !in_class {
let class_type = if trimmed.starts_with("class ") {
"class"
} else {
"struct"
};
if let Some(name) = self.extract_class_name(trimmed) {
// Set default visibility based on type
visibility = if class_type == "class" {
"private"
} else {
"public"
}
.to_string();
current_class_name = self.get_qualified_name(&name);
in_class = true;
}
}
// Track access modifiers within class
if in_class {
if trimmed.starts_with("public:") {
visibility = "public".to_string();
} else if trimmed.starts_with("private:") {
visibility = "private".to_string();
} else if trimmed.starts_with("protected:") {
visibility = "protected".to_string();
}
}
// Check for method declaration within class
if in_class && self.is_function_declaration(trimmed) {
if let Ok(method_name) = self.extract_function_name(trimmed) {
let qualified_name = format!("{}::{}", current_class_name, method_name);
// Check for virtual/static/const modifiers
let is_virtual = trimmed.contains("virtual ");
let is_static = trimmed.contains("static ");
let is_const = trimmed.contains(" const");
// Add more detail to visibility
let method_visibility = if is_virtual {
format!("{}_virtual", visibility)
} else if is_static {
format!("{}_static", visibility)
} else if is_const {
format!("{}_const", visibility)
} else {
visibility.clone()
};
// Check if method is async
let is_async = trimmed.contains("async ") || trimmed.contains("co_await ");
// Use Function for Method
self.items.push(AstItem::Function {
name: qualified_name,
visibility: method_visibility,
is_async,
line: line_num + 1,
});
}
}
// Track brace depth
brace_depth += trimmed.chars().filter(|&c| c == '{').count() as i32;
brace_depth -= trimmed.chars().filter(|&c| c == '}').count() as i32;
// Check if we're exiting a class
if in_class && trimmed.contains("}") && (brace_depth == 0 || trimmed.ends_with("};")) {
in_class = false;
}
}
Ok(())
}
/// Extracts enum declarations (complexity ≤10)
#[allow(clippy::cast_possible_truncation)]
fn extract_enum_declarations(&mut self, source: &str) -> Result<(), String> {
// Track namespace context while parsing
let mut namespace_stack: Vec<String> = Vec::new();
let mut brace_depth = 0;
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
// Track namespace declarations
if trimmed.starts_with("namespace ") {
if let Some(ns_name) = self.extract_namespace_name(trimmed) {
namespace_stack.push(ns_name);
}
}
// Track brace depth
brace_depth += trimmed.chars().filter(|&c| c == '{').count() as i32;
brace_depth -= trimmed.chars().filter(|&c| c == '}').count() as i32;
// Check for closing namespace
if trimmed.contains("}")
&& !namespace_stack.is_empty()
&& brace_depth < namespace_stack.len() as i32
{
namespace_stack.pop();
}
if (trimmed.starts_with("enum ") || trimmed.starts_with("enum class "))
&& trimmed.contains("{")
{
if let Some(name) = self.extract_enum_name(trimmed) {
// Build qualified name with current namespace
let qualified_name = if !namespace_stack.is_empty() {
format!("{}::{}", namespace_stack.join("::"), name)
} else {
name
};
// Count enum variants (simplified)
let variants_count = self.count_enum_variants(source, line_num);
self.items.push(AstItem::Enum {
name: qualified_name,
visibility: "public".to_string(),
variants_count,
line: line_num + 1,
});
}
}
}
Ok(())
}
/// Extracts typedef declarations (complexity ≤10)
fn extract_typedef_declarations(&mut self, source: &str) -> Result<(), String> {
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
// Check for typedef and using declarations
if trimmed.starts_with("typedef ")
|| (trimmed.starts_with("using ") && trimmed.contains("="))
{
if let Some(name) = if trimmed.starts_with("typedef ") {
self.extract_typedef_name(trimmed)
} else {
self.extract_using_name(trimmed)
} {
let qualified_name = self.get_qualified_name(&name);
// Use Struct for TypeAlias
self.items.push(AstItem::Struct {
name: qualified_name,
visibility: "public".to_string(),
fields_count: 0,
derives: Vec::new(),
line: line_num + 1,
});
}
}
}
Ok(())
}
/// Extracts template declarations (complexity ≤10)
fn extract_template_declarations(&mut self, source: &str) -> Result<(), String> {
let mut in_template = false;
let mut template_line = 0;
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
// Check for template declaration start
if trimmed.starts_with("template<") || trimmed.starts_with("template <") {
in_template = true;
template_line = line_num;
continue;
}
// Check what follows the template
if in_template {
in_template = false;
if trimmed.starts_with("class ") || trimmed.starts_with("struct ") {
// Template class/struct
if let Some(name) = self.extract_class_name(trimmed) {
let qualified_name = self.get_qualified_name(&name);
// Use Struct for GenericType
self.items.push(AstItem::Struct {
name: qualified_name,
visibility: "public".to_string(),
fields_count: 0,
derives: Vec::new(),
line: template_line + 1,
});
}
} else if self.is_function_declaration(trimmed) {
// Template function
if let Ok(name) = self.extract_function_name(trimmed) {
let qualified_name = self.get_qualified_name(&name);
self.items.push(AstItem::Function {
name: format!("template::{}", qualified_name),
visibility: "public".to_string(),
is_async: false,
line: template_line + 1,
});
}
}
}
}
Ok(())
}
/// Counts enum variants (complexity ≤10)
#[allow(clippy::cast_possible_truncation)]
fn count_enum_variants(&self, source: &str, enum_start_line: usize) -> usize {
let mut in_enum = false;
let mut brace_depth = 0;
let mut variant_count = 0;
for (i, line) in source.lines().enumerate().skip(enum_start_line) {
let trimmed = line.trim();
// Find the enum start
if i == enum_start_line && trimmed.contains("{") {
in_enum = true;
}
// Track brace depth
brace_depth += trimmed.chars().filter(|&c| c == '{').count() as i32;
brace_depth -= trimmed.chars().filter(|&c| c == '}').count() as i32;
// Count variants
if in_enum && trimmed.contains(",") {
// Each comma represents a variant (except trailing comma)
variant_count += trimmed.chars().filter(|&c| c == ',').count();
}
// Exit when enum is closed
if in_enum && trimmed.contains("}") && brace_depth == 0 {
// Add 1 for the last variant (after the last comma)
variant_count += 1;
break;
}
}
// Ensure at least 1 variant
std::cmp::max(1, variant_count)
}
}