decy-parser 2.2.0

C AST parsing using clang-sys for Decy transpiler
Documentation
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! C parser implementation using clang-sys.
//!
//! This module provides the core parsing functionality to convert C source code
//! into an AST representation using LLVM/Clang bindings.

// Allow non-upper-case globals from clang-sys FFI bindings (CXCursor_* constants)
#![allow(non_upper_case_globals)]

use crate::diagnostic::{DiagnosticError, Severity};
use crate::extract::{extract_diagnostics, visit_function};
use anyhow::{Context, Result};
use clang_sys::*;
use std::ffi::CString;
use std::path::Path;
use std::process::Command;
use std::ptr;

/// Discover system include paths from the clang compiler.
///
/// This function runs `clang -E -x c - -v` to extract the system include
/// search paths, enabling parsing of code that uses standard headers.
fn discover_system_includes() -> Vec<String> {
    let mut includes = Vec::new();

    // Try to get include paths from clang
    let output = Command::new("clang")
        .args(["-E", "-x", "c", "-", "-v"])
        .stdin(std::process::Stdio::null())
        .output();

    if let Ok(output) = output {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let mut in_include_section = false;

        for line in stderr.lines() {
            if line.contains("#include <...> search starts here:") {
                in_include_section = true;
                continue;
            }
            if line.contains("End of search list.") {
                break;
            }
            if in_include_section {
                let path = line.trim();
                // Skip framework directories (macOS-specific)
                if !path.is_empty() && !path.contains("(framework directory)") {
                    includes.push(path.to_string());
                }
            }
        }
    }

    // Fallback paths if clang detection fails
    if includes.is_empty() {
        // macOS paths
        if cfg!(target_os = "macos") {
            includes.extend([
                "/usr/local/include".to_string(),
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include".to_string(),
            ]);
        }
        // Linux paths
        if cfg!(target_os = "linux") {
            includes.extend(["/usr/include".to_string(), "/usr/local/include".to_string()]);
        }
    }

    includes
}

/// C parser using clang-sys.
///
/// # Examples
///
/// ```no_run
/// use decy_parser::parser::CParser;
///
/// let parser = CParser::new()?;
/// let ast = parser.parse("int main() { return 0; }")?;
/// assert_eq!(ast.functions().len(), 1);
/// # Ok::<(), anyhow::Error>(())
/// ```
#[derive(Debug)]
pub struct CParser {
    index: CXIndex,
    /// System include paths discovered from the compiler
    system_includes: Vec<String>,
}

impl CParser {
    /// Create a new C parser.
    ///
    /// This automatically discovers system include paths from the clang compiler.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use decy_parser::parser::CParser;
    ///
    /// let parser = CParser::new()?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn new() -> Result<Self> {
        // SAFETY: clang_createIndex is safe to call with these parameters
        let index = unsafe { clang_createIndex(0, 0) };
        if index.is_null() {
            anyhow::bail!("Failed to create clang index");
        }

        // Discover system include paths for standard header support
        let system_includes = discover_system_includes();

        Ok(Self { index, system_includes })
    }

    /// Parse C source code into an AST.
    ///
    /// # Arguments
    ///
    /// * `source` - The C source code to parse
    ///
    /// # Returns
    ///
    /// * `Ok(Ast)` - The parsed AST
    /// * `Err(anyhow::Error)` - If parsing fails
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use decy_parser::parser::CParser;
    ///
    /// let parser = CParser::new()?;
    /// let ast = parser.parse("int add(int a, int b) { return a + b; }")?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    #[allow(clippy::disallowed_methods)] // CString::new with literals cannot fail
    pub fn parse(&self, source: &str) -> Result<Ast> {
        let filename = CString::new("input.c").context("Failed to create filename")?;
        let source_cstr = CString::new(source).context("Failed to convert source to CString")?;

        let mut ast = Ast::new();

        // Handle empty input
        if source.trim().is_empty() {
            return Ok(ast);
        }

        // SAFETY: Creating unsaved file with valid C strings
        let unsaved_file = CXUnsavedFile {
            Filename: filename.as_ptr(),
            Contents: source_cstr.as_ptr(),
            Length: source.len() as std::os::raw::c_ulong,
        };

        // Detect language mode from source content
        // DECY-198/221: Support C++ and CUDA language modes
        let has_extern_c = source.contains("extern \"C\"");
        let has_ifdef_guard =
            source.contains("#ifdef __cplusplus") || source.contains("#if defined(__cplusplus)");
        // DECY-221: Detect CUDA keywords to enable C++ mode for .cu content
        let has_cuda_keywords = source.contains("__global__")
            || source.contains("__device__")
            || source.contains("__host__");
        let needs_cpp_mode =
            (has_extern_c && !has_ifdef_guard) || has_cuda_keywords;

        // Build system include path arguments
        // We need to keep CStrings alive for the duration of parsing
        let isystem_flag = CString::new("-isystem").unwrap();
        let include_cstrings: Vec<CString> =
            self.system_includes.iter().map(|p| CString::new(p.as_str()).unwrap()).collect();

        // Prepare command line arguments for language mode
        let cpp_flag = CString::new("-x").unwrap();
        let cpp_lang = CString::new("c++").unwrap();

        // DECY-194: Add standard C macro definitions that might not be available
        let define_eof = CString::new("-DEOF=-1").unwrap();
        let define_null = CString::new("-DNULL=0").unwrap();
        let define_bufsiz = CString::new("-DBUFSIZ=8192").unwrap();

        // DECY-221: Define CUDA keywords as empty macros so clang in C++ mode
        // can parse .cu content without CUDA toolkit. The qualifier detection
        // falls back to name-based heuristics (functions named with _kernel suffix
        // or detected via annotation attributes).
        let define_global = CString::new("-D__global__=").unwrap();
        let define_device = CString::new("-D__device__=").unwrap();
        let define_host = CString::new("-D__host__=").unwrap();
        let define_shared = CString::new("-D__shared__=").unwrap();

        // Build the complete args vector
        let mut args_vec: Vec<*const std::os::raw::c_char> = Vec::new();

        // Add C++ mode flags if needed
        if needs_cpp_mode {
            args_vec.push(cpp_flag.as_ptr());
            args_vec.push(cpp_lang.as_ptr());
        }

        // DECY-221: Add CUDA macro definitions when CUDA keywords detected
        if has_cuda_keywords {
            args_vec.push(define_global.as_ptr());
            args_vec.push(define_device.as_ptr());
            args_vec.push(define_host.as_ptr());
            args_vec.push(define_shared.as_ptr());
        }

        // Add macro definitions
        args_vec.push(define_eof.as_ptr());
        args_vec.push(define_null.as_ptr());
        args_vec.push(define_bufsiz.as_ptr());

        // Add system include paths
        for include_path in &include_cstrings {
            args_vec.push(isystem_flag.as_ptr());
            args_vec.push(include_path.as_ptr());
        }

        // SAFETY: Parsing with clang_parseTranslationUnit2
        // Enable DetailedPreprocessingRecord to capture macro definitions
        // CXTranslationUnit_DetailedPreprocessingRecord = 1
        let flags = 1;

        let mut tu = ptr::null_mut();
        let result = unsafe {
            clang_parseTranslationUnit2(
                self.index,
                filename.as_ptr(),
                if args_vec.is_empty() { ptr::null() } else { args_vec.as_ptr() },
                args_vec.len() as std::os::raw::c_int,
                &unsaved_file as *const CXUnsavedFile as *mut CXUnsavedFile,
                1,
                flags,
                &mut tu,
            )
        };

        if result != CXError_Success || tu.is_null() {
            anyhow::bail!("Failed to parse C source");
        }

        // SAFETY: Extract structured diagnostics from clang
        let diagnostics = extract_diagnostics(tu, source, None);
        if diagnostics.iter().any(|d| d.severity >= Severity::Error) {
            unsafe { clang_disposeTranslationUnit(tu) };
            return Err(DiagnosticError::new(diagnostics).into());
        }

        // SAFETY: Getting cursor from valid translation unit
        let cursor = unsafe { clang_getTranslationUnitCursor(tu) };

        // Visit children to extract functions
        let ast_ptr = &mut ast as *mut Ast;

        // SAFETY: Visiting cursor children with callback
        unsafe {
            clang_visitChildren(cursor, visit_function, ast_ptr as CXClientData);

            // Clean up
            clang_disposeTranslationUnit(tu);
        }

        // DECY-221: Post-parse CUDA qualifier application
        // Since we use empty macros (__global__= etc.), the qualifier info is lost
        // during parsing. Recover it by scanning the original source.
        if has_cuda_keywords {
            apply_cuda_qualifiers_from_source(&mut ast, source);
        }

        Ok(ast)
    }

    /// Parse a C file into an AST.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the C file
    ///
    /// # Returns
    ///
    /// * `Ok(Ast)` - The parsed AST
    /// * `Err(anyhow::Error)` - If parsing fails
    ///
    /// Parse C source code from a file path.
    ///
    /// DECY-237: This method allows includes to resolve properly by parsing
    /// from the actual file path instead of an in-memory string.
    #[allow(clippy::disallowed_methods)] // CString::new with literals cannot fail
    pub fn parse_file(&self, path: &Path) -> Result<Ast> {
        contract_pre_parse!();
        let source = std::fs::read_to_string(path)
            .with_context(|| format!("Failed to read file: {}", path.display()))?;

        // Convert the path to an absolute path for clang
        let abs_path = if path.is_absolute() {
            path.to_path_buf()
        } else {
            std::env::current_dir()?.join(path)
        };

        let filename =
            CString::new(abs_path.to_string_lossy().as_bytes()).context("Invalid path")?;
        let source_cstr = CString::new(source.as_str()).context("Invalid source")?;

        let mut ast = Ast::new();

        // Handle empty input
        if source.trim().is_empty() {
            return Ok(ast);
        }

        // SAFETY: Creating unsaved file with valid C strings
        let unsaved_file = CXUnsavedFile {
            Filename: filename.as_ptr(),
            Contents: source_cstr.as_ptr(),
            Length: source.len() as std::os::raw::c_ulong,
        };

        // DECY-198: Detect language mode from file extension and source content
        // .cu and .cpp/.cc/.cxx files use C++ mode (CUDA mode requires CUDA toolkit)
        let is_cpp_or_cuda_file = matches!(
            path.extension().and_then(|s| s.to_str()),
            Some("cu") | Some("cpp") | Some("cc") | Some("cxx") | Some("hpp")
        );
        let has_extern_c = source.contains("extern \"C\"");
        let has_ifdef_guard =
            source.contains("#ifdef __cplusplus") || source.contains("#if defined(__cplusplus)");
        let needs_cpp_mode =
            is_cpp_or_cuda_file || (has_extern_c && !has_ifdef_guard);

        // Prepare command line arguments for language mode
        let cpp_flag = CString::new("-x").unwrap();
        let cpp_lang = CString::new("c++").unwrap();

        // Standard macro definitions
        let define_eof = CString::new("-DEOF=-1").unwrap();
        let define_null = CString::new("-DNULL=0").unwrap();
        let define_bufsiz = CString::new("-DBUFSIZ=8192").unwrap();

        // DECY-237: Add explicit defines for <limits.h> macros
        let define_char_bit = CString::new("-DCHAR_BIT=8").unwrap();
        let define_char_min = CString::new("-DCHAR_MIN=-128").unwrap();
        let define_char_max = CString::new("-DCHAR_MAX=127").unwrap();
        let define_schar_min = CString::new("-DSCHAR_MIN=-128").unwrap();
        let define_schar_max = CString::new("-DSCHAR_MAX=127").unwrap();
        let define_uchar_max = CString::new("-DUCHAR_MAX=255").unwrap();
        let define_shrt_min = CString::new("-DSHRT_MIN=-32768").unwrap();
        let define_shrt_max = CString::new("-DSHRT_MAX=32767").unwrap();
        let define_ushrt_max = CString::new("-DUSHRT_MAX=65535").unwrap();
        let define_int_min = CString::new("-DINT_MIN=-2147483648").unwrap();
        let define_int_max = CString::new("-DINT_MAX=2147483647").unwrap();
        let define_uint_max = CString::new("-DUINT_MAX=4294967295U").unwrap();
        let define_long_min = CString::new("-DLONG_MIN=-9223372036854775808L").unwrap();
        let define_long_max = CString::new("-DLONG_MAX=9223372036854775807L").unwrap();
        let define_ulong_max = CString::new("-DULONG_MAX=18446744073709551615UL").unwrap();

        // <stdlib.h> macros
        let define_exit_success = CString::new("-DEXIT_SUCCESS=0").unwrap();
        let define_exit_failure = CString::new("-DEXIT_FAILURE=1").unwrap();
        let define_rand_max = CString::new("-DRAND_MAX=2147483647").unwrap();

        // System include paths
        let include_clang = CString::new("-I/usr/lib/llvm-14/lib/clang/14.0.0/include").unwrap();
        let include_local = CString::new("-I/usr/local/include").unwrap();
        let include_arch = CString::new("-I/usr/include/x86_64-linux-gnu").unwrap();
        let include_usr = CString::new("-I/usr/include").unwrap();

        // Build the args vector with all defines
        let base_defines: Vec<*const std::os::raw::c_char> = vec![
            define_eof.as_ptr(),
            define_null.as_ptr(),
            define_bufsiz.as_ptr(),
            define_char_bit.as_ptr(),
            define_char_min.as_ptr(),
            define_char_max.as_ptr(),
            define_schar_min.as_ptr(),
            define_schar_max.as_ptr(),
            define_uchar_max.as_ptr(),
            define_shrt_min.as_ptr(),
            define_shrt_max.as_ptr(),
            define_ushrt_max.as_ptr(),
            define_int_min.as_ptr(),
            define_int_max.as_ptr(),
            define_uint_max.as_ptr(),
            define_long_min.as_ptr(),
            define_long_max.as_ptr(),
            define_ulong_max.as_ptr(),
            define_exit_success.as_ptr(),
            define_exit_failure.as_ptr(),
            define_rand_max.as_ptr(),
            include_clang.as_ptr(),
            include_local.as_ptr(),
            include_arch.as_ptr(),
            include_usr.as_ptr(),
        ];

        let args_vec: Vec<*const std::os::raw::c_char> = if needs_cpp_mode {
            let mut args = vec![cpp_flag.as_ptr(), cpp_lang.as_ptr()];
            args.extend(base_defines);
            args
        } else {
            base_defines
        };

        // Enable DetailedPreprocessingRecord to capture macro definitions
        let flags = 1;

        let mut tu = ptr::null_mut();
        let result = unsafe {
            clang_parseTranslationUnit2(
                self.index,
                filename.as_ptr(),
                if args_vec.is_empty() { ptr::null() } else { args_vec.as_ptr() },
                args_vec.len() as std::os::raw::c_int,
                &unsaved_file as *const CXUnsavedFile as *mut CXUnsavedFile,
                1,
                flags,
                &mut tu,
            )
        };

        if result != CXError_Success || tu.is_null() {
            anyhow::bail!("Failed to parse C source file: {}", path.display());
        }

        // SAFETY: Extract structured diagnostics from clang
        let file_name = path.to_string_lossy().to_string();
        let diagnostics = extract_diagnostics(tu, &source, Some(&file_name));
        if diagnostics.iter().any(|d| d.severity >= Severity::Error) {
            unsafe { clang_disposeTranslationUnit(tu) };
            return Err(DiagnosticError::new(diagnostics).into());
        }

        // Get cursor and visit
        let cursor = unsafe { clang_getTranslationUnitCursor(tu) };
        let ast_ptr = &mut ast as *mut Ast;

        unsafe {
            clang_visitChildren(cursor, visit_function, ast_ptr as CXClientData);
            clang_disposeTranslationUnit(tu);
        }

        Ok(ast)
    }
}

impl Drop for CParser {
    fn drop(&mut self) {
        // SAFETY: Disposing of valid clang index
        unsafe {
            clang_disposeIndex(self.index);
        }
    }
}

// Re-export all AST types so external users see them at parser:: level
pub use crate::ast_types::*;

/// DECY-221: Apply CUDA qualifiers to parsed functions by scanning source text.
///
/// When parsing .cu content in C++ mode (with empty CUDA macros), the
/// `__global__`, `__device__`, and `__host__` qualifiers are stripped.
/// This function recovers them by matching function names against source lines.
fn apply_cuda_qualifiers_from_source(ast: &mut Ast, source: &str) {
    contract_pre_qualifier_preservation!();
    // Build a map of function_name -> qualifier from source scanning
    let mut qualifiers: std::collections::HashMap<String, CudaQualifier> =
        std::collections::HashMap::new();

    for line in source.lines() {
        let trimmed = line.trim();
        let qualifier = if trimmed.starts_with("__global__") {
            Some(CudaQualifier::Global)
        } else if (trimmed.starts_with("__device__") && trimmed.contains("__host__"))
            || (trimmed.starts_with("__host__") && trimmed.contains("__device__"))
        {
            Some(CudaQualifier::HostDevice)
        } else if trimmed.starts_with("__device__") {
            Some(CudaQualifier::Device)
        } else if trimmed.starts_with("__host__") {
            Some(CudaQualifier::Host)
        } else {
            None
        };

        if let Some(qual) = qualifier {
            // Extract function name: after qualifier + return type, before '('
            let after_qual = trimmed
                .replace("__global__", "")
                .replace("__device__", "")
                .replace("__host__", "");
            if let Some(paren_pos) = after_qual.find('(') {
                let before_paren = after_qual[..paren_pos].trim();
                // Last word before '(' is the function name
                if let Some(name) = before_paren.split_whitespace().last() {
                    // Strip pointer/ref decorators
                    let clean_name = name.trim_start_matches('*').trim_start_matches('&');
                    qualifiers.insert(clean_name.to_string(), qual);
                }
            }
        }
    }

    // Apply qualifiers to parsed functions
    for func in ast.functions_mut() {
        if let Some(qual) = qualifiers.get(&func.name) {
            func.cuda_qualifier = Some(*qual);
        }
    }
}

#[cfg(test)]
#[path = "parser_tests.rs"]
mod parser_tests;

#[cfg(test)]
#[path = "pointer_arithmetic_tests.rs"]
mod pointer_arithmetic_tests;

#[cfg(test)]
#[path = "break_continue_tests.rs"]
mod break_continue_tests;

#[cfg(test)]
#[path = "cpp_cuda_tests.rs"]
mod cpp_cuda_tests;