Skip to main content

decy_parser/
parser.rs

1//! C parser implementation using clang-sys.
2//!
3//! This module provides the core parsing functionality to convert C source code
4//! into an AST representation using LLVM/Clang bindings.
5
6// Allow non-upper-case globals from clang-sys FFI bindings (CXCursor_* constants)
7#![allow(non_upper_case_globals)]
8
9use crate::diagnostic::{DiagnosticError, Severity};
10use crate::extract::{extract_diagnostics, visit_function};
11use anyhow::{Context, Result};
12use clang_sys::*;
13use std::ffi::CString;
14use std::path::Path;
15use std::process::Command;
16use std::ptr;
17
18/// Discover system include paths from the clang compiler.
19///
20/// This function runs `clang -E -x c - -v` to extract the system include
21/// search paths, enabling parsing of code that uses standard headers.
22fn discover_system_includes() -> Vec<String> {
23    let mut includes = Vec::new();
24
25    // Try to get include paths from clang
26    let output = Command::new("clang")
27        .args(["-E", "-x", "c", "-", "-v"])
28        .stdin(std::process::Stdio::null())
29        .output();
30
31    if let Ok(output) = output {
32        let stderr = String::from_utf8_lossy(&output.stderr);
33        let mut in_include_section = false;
34
35        for line in stderr.lines() {
36            if line.contains("#include <...> search starts here:") {
37                in_include_section = true;
38                continue;
39            }
40            if line.contains("End of search list.") {
41                break;
42            }
43            if in_include_section {
44                let path = line.trim();
45                // Skip framework directories (macOS-specific)
46                if !path.is_empty() && !path.contains("(framework directory)") {
47                    includes.push(path.to_string());
48                }
49            }
50        }
51    }
52
53    // Fallback paths if clang detection fails
54    if includes.is_empty() {
55        // macOS paths
56        if cfg!(target_os = "macos") {
57            includes.extend([
58                "/usr/local/include".to_string(),
59                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include".to_string(),
60            ]);
61        }
62        // Linux paths
63        if cfg!(target_os = "linux") {
64            includes.extend(["/usr/include".to_string(), "/usr/local/include".to_string()]);
65        }
66    }
67
68    includes
69}
70
71/// C parser using clang-sys.
72///
73/// # Examples
74///
75/// ```no_run
76/// use decy_parser::parser::CParser;
77///
78/// let parser = CParser::new()?;
79/// let ast = parser.parse("int main() { return 0; }")?;
80/// assert_eq!(ast.functions().len(), 1);
81/// # Ok::<(), anyhow::Error>(())
82/// ```
83#[derive(Debug)]
84pub struct CParser {
85    index: CXIndex,
86    /// System include paths discovered from the compiler
87    system_includes: Vec<String>,
88}
89
90impl CParser {
91    /// Create a new C parser.
92    ///
93    /// This automatically discovers system include paths from the clang compiler.
94    ///
95    /// # Examples
96    ///
97    /// ```no_run
98    /// use decy_parser::parser::CParser;
99    ///
100    /// let parser = CParser::new()?;
101    /// # Ok::<(), anyhow::Error>(())
102    /// ```
103    pub fn new() -> Result<Self> {
104        // SAFETY: clang_createIndex is safe to call with these parameters
105        let index = unsafe { clang_createIndex(0, 0) };
106        if index.is_null() {
107            anyhow::bail!("Failed to create clang index");
108        }
109
110        // Discover system include paths for standard header support
111        let system_includes = discover_system_includes();
112
113        Ok(Self { index, system_includes })
114    }
115
116    /// Parse C source code into an AST.
117    ///
118    /// # Arguments
119    ///
120    /// * `source` - The C source code to parse
121    ///
122    /// # Returns
123    ///
124    /// * `Ok(Ast)` - The parsed AST
125    /// * `Err(anyhow::Error)` - If parsing fails
126    ///
127    /// # Examples
128    ///
129    /// ```no_run
130    /// use decy_parser::parser::CParser;
131    ///
132    /// let parser = CParser::new()?;
133    /// let ast = parser.parse("int add(int a, int b) { return a + b; }")?;
134    /// # Ok::<(), anyhow::Error>(())
135    /// ```
136    #[allow(clippy::disallowed_methods)] // CString::new with literals cannot fail
137    pub fn parse(&self, source: &str) -> Result<Ast> {
138        let filename = CString::new("input.c").context("Failed to create filename")?;
139        let source_cstr = CString::new(source).context("Failed to convert source to CString")?;
140
141        let mut ast = Ast::new();
142
143        // Handle empty input
144        if source.trim().is_empty() {
145            return Ok(ast);
146        }
147
148        // SAFETY: Creating unsaved file with valid C strings
149        let unsaved_file = CXUnsavedFile {
150            Filename: filename.as_ptr(),
151            Contents: source_cstr.as_ptr(),
152            Length: source.len() as std::os::raw::c_ulong,
153        };
154
155        // Detect language mode from source content
156        // DECY-198/221: Support C++ and CUDA language modes
157        let has_extern_c = source.contains("extern \"C\"");
158        let has_ifdef_guard =
159            source.contains("#ifdef __cplusplus") || source.contains("#if defined(__cplusplus)");
160        // DECY-221: Detect CUDA keywords to enable C++ mode for .cu content
161        let has_cuda_keywords = source.contains("__global__")
162            || source.contains("__device__")
163            || source.contains("__host__");
164        let needs_cpp_mode =
165            (has_extern_c && !has_ifdef_guard) || has_cuda_keywords;
166
167        // Build system include path arguments
168        // We need to keep CStrings alive for the duration of parsing
169        let isystem_flag = CString::new("-isystem").unwrap();
170        let include_cstrings: Vec<CString> =
171            self.system_includes.iter().map(|p| CString::new(p.as_str()).unwrap()).collect();
172
173        // Prepare command line arguments for language mode
174        let cpp_flag = CString::new("-x").unwrap();
175        let cpp_lang = CString::new("c++").unwrap();
176
177        // DECY-194: Add standard C macro definitions that might not be available
178        let define_eof = CString::new("-DEOF=-1").unwrap();
179        let define_null = CString::new("-DNULL=0").unwrap();
180        let define_bufsiz = CString::new("-DBUFSIZ=8192").unwrap();
181
182        // DECY-221: Define CUDA keywords as empty macros so clang in C++ mode
183        // can parse .cu content without CUDA toolkit. The qualifier detection
184        // falls back to name-based heuristics (functions named with _kernel suffix
185        // or detected via annotation attributes).
186        let define_global = CString::new("-D__global__=").unwrap();
187        let define_device = CString::new("-D__device__=").unwrap();
188        let define_host = CString::new("-D__host__=").unwrap();
189        let define_shared = CString::new("-D__shared__=").unwrap();
190
191        // Build the complete args vector
192        let mut args_vec: Vec<*const std::os::raw::c_char> = Vec::new();
193
194        // Add C++ mode flags if needed
195        if needs_cpp_mode {
196            args_vec.push(cpp_flag.as_ptr());
197            args_vec.push(cpp_lang.as_ptr());
198        }
199
200        // DECY-221: Add CUDA macro definitions when CUDA keywords detected
201        if has_cuda_keywords {
202            args_vec.push(define_global.as_ptr());
203            args_vec.push(define_device.as_ptr());
204            args_vec.push(define_host.as_ptr());
205            args_vec.push(define_shared.as_ptr());
206        }
207
208        // Add macro definitions
209        args_vec.push(define_eof.as_ptr());
210        args_vec.push(define_null.as_ptr());
211        args_vec.push(define_bufsiz.as_ptr());
212
213        // Add system include paths
214        for include_path in &include_cstrings {
215            args_vec.push(isystem_flag.as_ptr());
216            args_vec.push(include_path.as_ptr());
217        }
218
219        // SAFETY: Parsing with clang_parseTranslationUnit2
220        // Enable DetailedPreprocessingRecord to capture macro definitions
221        // CXTranslationUnit_DetailedPreprocessingRecord = 1
222        let flags = 1;
223
224        let mut tu = ptr::null_mut();
225        let result = unsafe {
226            clang_parseTranslationUnit2(
227                self.index,
228                filename.as_ptr(),
229                if args_vec.is_empty() { ptr::null() } else { args_vec.as_ptr() },
230                args_vec.len() as std::os::raw::c_int,
231                &unsaved_file as *const CXUnsavedFile as *mut CXUnsavedFile,
232                1,
233                flags,
234                &mut tu,
235            )
236        };
237
238        if result != CXError_Success || tu.is_null() {
239            anyhow::bail!("Failed to parse C source");
240        }
241
242        // SAFETY: Extract structured diagnostics from clang
243        let diagnostics = extract_diagnostics(tu, source, None);
244        if diagnostics.iter().any(|d| d.severity >= Severity::Error) {
245            unsafe { clang_disposeTranslationUnit(tu) };
246            return Err(DiagnosticError::new(diagnostics).into());
247        }
248
249        // SAFETY: Getting cursor from valid translation unit
250        let cursor = unsafe { clang_getTranslationUnitCursor(tu) };
251
252        // Visit children to extract functions
253        let ast_ptr = &mut ast as *mut Ast;
254
255        // SAFETY: Visiting cursor children with callback
256        unsafe {
257            clang_visitChildren(cursor, visit_function, ast_ptr as CXClientData);
258
259            // Clean up
260            clang_disposeTranslationUnit(tu);
261        }
262
263        // DECY-221: Post-parse CUDA qualifier application
264        // Since we use empty macros (__global__= etc.), the qualifier info is lost
265        // during parsing. Recover it by scanning the original source.
266        if has_cuda_keywords {
267            apply_cuda_qualifiers_from_source(&mut ast, source);
268        }
269
270        Ok(ast)
271    }
272
273    /// Parse a C file into an AST.
274    ///
275    /// # Arguments
276    ///
277    /// * `path` - Path to the C file
278    ///
279    /// # Returns
280    ///
281    /// * `Ok(Ast)` - The parsed AST
282    /// * `Err(anyhow::Error)` - If parsing fails
283    ///
284    /// Parse C source code from a file path.
285    ///
286    /// DECY-237: This method allows includes to resolve properly by parsing
287    /// from the actual file path instead of an in-memory string.
288    #[allow(clippy::disallowed_methods)] // CString::new with literals cannot fail
289    pub fn parse_file(&self, path: &Path) -> Result<Ast> {
290        contract_pre_parse!();
291        let source = std::fs::read_to_string(path)
292            .with_context(|| format!("Failed to read file: {}", path.display()))?;
293
294        // Convert the path to an absolute path for clang
295        let abs_path = if path.is_absolute() {
296            path.to_path_buf()
297        } else {
298            std::env::current_dir()?.join(path)
299        };
300
301        let filename =
302            CString::new(abs_path.to_string_lossy().as_bytes()).context("Invalid path")?;
303        let source_cstr = CString::new(source.as_str()).context("Invalid source")?;
304
305        let mut ast = Ast::new();
306
307        // Handle empty input
308        if source.trim().is_empty() {
309            return Ok(ast);
310        }
311
312        // SAFETY: Creating unsaved file with valid C strings
313        let unsaved_file = CXUnsavedFile {
314            Filename: filename.as_ptr(),
315            Contents: source_cstr.as_ptr(),
316            Length: source.len() as std::os::raw::c_ulong,
317        };
318
319        // DECY-198: Detect language mode from file extension and source content
320        // .cu and .cpp/.cc/.cxx files use C++ mode (CUDA mode requires CUDA toolkit)
321        let is_cpp_or_cuda_file = matches!(
322            path.extension().and_then(|s| s.to_str()),
323            Some("cu") | Some("cpp") | Some("cc") | Some("cxx") | Some("hpp")
324        );
325        let has_extern_c = source.contains("extern \"C\"");
326        let has_ifdef_guard =
327            source.contains("#ifdef __cplusplus") || source.contains("#if defined(__cplusplus)");
328        let needs_cpp_mode =
329            is_cpp_or_cuda_file || (has_extern_c && !has_ifdef_guard);
330
331        // Prepare command line arguments for language mode
332        let cpp_flag = CString::new("-x").unwrap();
333        let cpp_lang = CString::new("c++").unwrap();
334
335        // Standard macro definitions
336        let define_eof = CString::new("-DEOF=-1").unwrap();
337        let define_null = CString::new("-DNULL=0").unwrap();
338        let define_bufsiz = CString::new("-DBUFSIZ=8192").unwrap();
339
340        // DECY-237: Add explicit defines for <limits.h> macros
341        let define_char_bit = CString::new("-DCHAR_BIT=8").unwrap();
342        let define_char_min = CString::new("-DCHAR_MIN=-128").unwrap();
343        let define_char_max = CString::new("-DCHAR_MAX=127").unwrap();
344        let define_schar_min = CString::new("-DSCHAR_MIN=-128").unwrap();
345        let define_schar_max = CString::new("-DSCHAR_MAX=127").unwrap();
346        let define_uchar_max = CString::new("-DUCHAR_MAX=255").unwrap();
347        let define_shrt_min = CString::new("-DSHRT_MIN=-32768").unwrap();
348        let define_shrt_max = CString::new("-DSHRT_MAX=32767").unwrap();
349        let define_ushrt_max = CString::new("-DUSHRT_MAX=65535").unwrap();
350        let define_int_min = CString::new("-DINT_MIN=-2147483648").unwrap();
351        let define_int_max = CString::new("-DINT_MAX=2147483647").unwrap();
352        let define_uint_max = CString::new("-DUINT_MAX=4294967295U").unwrap();
353        let define_long_min = CString::new("-DLONG_MIN=-9223372036854775808L").unwrap();
354        let define_long_max = CString::new("-DLONG_MAX=9223372036854775807L").unwrap();
355        let define_ulong_max = CString::new("-DULONG_MAX=18446744073709551615UL").unwrap();
356
357        // <stdlib.h> macros
358        let define_exit_success = CString::new("-DEXIT_SUCCESS=0").unwrap();
359        let define_exit_failure = CString::new("-DEXIT_FAILURE=1").unwrap();
360        let define_rand_max = CString::new("-DRAND_MAX=2147483647").unwrap();
361
362        // System include paths
363        let include_clang = CString::new("-I/usr/lib/llvm-14/lib/clang/14.0.0/include").unwrap();
364        let include_local = CString::new("-I/usr/local/include").unwrap();
365        let include_arch = CString::new("-I/usr/include/x86_64-linux-gnu").unwrap();
366        let include_usr = CString::new("-I/usr/include").unwrap();
367
368        // Build the args vector with all defines
369        let base_defines: Vec<*const std::os::raw::c_char> = vec![
370            define_eof.as_ptr(),
371            define_null.as_ptr(),
372            define_bufsiz.as_ptr(),
373            define_char_bit.as_ptr(),
374            define_char_min.as_ptr(),
375            define_char_max.as_ptr(),
376            define_schar_min.as_ptr(),
377            define_schar_max.as_ptr(),
378            define_uchar_max.as_ptr(),
379            define_shrt_min.as_ptr(),
380            define_shrt_max.as_ptr(),
381            define_ushrt_max.as_ptr(),
382            define_int_min.as_ptr(),
383            define_int_max.as_ptr(),
384            define_uint_max.as_ptr(),
385            define_long_min.as_ptr(),
386            define_long_max.as_ptr(),
387            define_ulong_max.as_ptr(),
388            define_exit_success.as_ptr(),
389            define_exit_failure.as_ptr(),
390            define_rand_max.as_ptr(),
391            include_clang.as_ptr(),
392            include_local.as_ptr(),
393            include_arch.as_ptr(),
394            include_usr.as_ptr(),
395        ];
396
397        let args_vec: Vec<*const std::os::raw::c_char> = if needs_cpp_mode {
398            let mut args = vec![cpp_flag.as_ptr(), cpp_lang.as_ptr()];
399            args.extend(base_defines);
400            args
401        } else {
402            base_defines
403        };
404
405        // Enable DetailedPreprocessingRecord to capture macro definitions
406        let flags = 1;
407
408        let mut tu = ptr::null_mut();
409        let result = unsafe {
410            clang_parseTranslationUnit2(
411                self.index,
412                filename.as_ptr(),
413                if args_vec.is_empty() { ptr::null() } else { args_vec.as_ptr() },
414                args_vec.len() as std::os::raw::c_int,
415                &unsaved_file as *const CXUnsavedFile as *mut CXUnsavedFile,
416                1,
417                flags,
418                &mut tu,
419            )
420        };
421
422        if result != CXError_Success || tu.is_null() {
423            anyhow::bail!("Failed to parse C source file: {}", path.display());
424        }
425
426        // SAFETY: Extract structured diagnostics from clang
427        let file_name = path.to_string_lossy().to_string();
428        let diagnostics = extract_diagnostics(tu, &source, Some(&file_name));
429        if diagnostics.iter().any(|d| d.severity >= Severity::Error) {
430            unsafe { clang_disposeTranslationUnit(tu) };
431            return Err(DiagnosticError::new(diagnostics).into());
432        }
433
434        // Get cursor and visit
435        let cursor = unsafe { clang_getTranslationUnitCursor(tu) };
436        let ast_ptr = &mut ast as *mut Ast;
437
438        unsafe {
439            clang_visitChildren(cursor, visit_function, ast_ptr as CXClientData);
440            clang_disposeTranslationUnit(tu);
441        }
442
443        Ok(ast)
444    }
445}
446
447impl Drop for CParser {
448    fn drop(&mut self) {
449        // SAFETY: Disposing of valid clang index
450        unsafe {
451            clang_disposeIndex(self.index);
452        }
453    }
454}
455
456// Re-export all AST types so external users see them at parser:: level
457pub use crate::ast_types::*;
458
459/// DECY-221: Apply CUDA qualifiers to parsed functions by scanning source text.
460///
461/// When parsing .cu content in C++ mode (with empty CUDA macros), the
462/// `__global__`, `__device__`, and `__host__` qualifiers are stripped.
463/// This function recovers them by matching function names against source lines.
464fn apply_cuda_qualifiers_from_source(ast: &mut Ast, source: &str) {
465    contract_pre_qualifier_preservation!();
466    // Build a map of function_name -> qualifier from source scanning
467    let mut qualifiers: std::collections::HashMap<String, CudaQualifier> =
468        std::collections::HashMap::new();
469
470    for line in source.lines() {
471        let trimmed = line.trim();
472        let qualifier = if trimmed.starts_with("__global__") {
473            Some(CudaQualifier::Global)
474        } else if (trimmed.starts_with("__device__") && trimmed.contains("__host__"))
475            || (trimmed.starts_with("__host__") && trimmed.contains("__device__"))
476        {
477            Some(CudaQualifier::HostDevice)
478        } else if trimmed.starts_with("__device__") {
479            Some(CudaQualifier::Device)
480        } else if trimmed.starts_with("__host__") {
481            Some(CudaQualifier::Host)
482        } else {
483            None
484        };
485
486        if let Some(qual) = qualifier {
487            // Extract function name: after qualifier + return type, before '('
488            let after_qual = trimmed
489                .replace("__global__", "")
490                .replace("__device__", "")
491                .replace("__host__", "");
492            if let Some(paren_pos) = after_qual.find('(') {
493                let before_paren = after_qual[..paren_pos].trim();
494                // Last word before '(' is the function name
495                if let Some(name) = before_paren.split_whitespace().last() {
496                    // Strip pointer/ref decorators
497                    let clean_name = name.trim_start_matches('*').trim_start_matches('&');
498                    qualifiers.insert(clean_name.to_string(), qual);
499                }
500            }
501        }
502    }
503
504    // Apply qualifiers to parsed functions
505    for func in ast.functions_mut() {
506        if let Some(qual) = qualifiers.get(&func.name) {
507            func.cuda_qualifier = Some(*qual);
508        }
509    }
510}
511
512#[cfg(test)]
513#[path = "parser_tests.rs"]
514mod parser_tests;
515
516#[cfg(test)]
517#[path = "pointer_arithmetic_tests.rs"]
518mod pointer_arithmetic_tests;
519
520#[cfg(test)]
521#[path = "break_continue_tests.rs"]
522mod break_continue_tests;
523
524#[cfg(test)]
525#[path = "cpp_cuda_tests.rs"]
526mod cpp_cuda_tests;