codegraph_c/lib.rs
1//! # codegraph-c
2//!
3//! C parser for CodeGraph - extracts code entities and relationships from C source files.
4//!
5//! ## Features
6//!
7//! - Parse C source files (.c) and header files (.h)
8//! - Extract functions, structs, unions, enums, and typedefs
9//! - Track relationships (includes, function calls)
10//! - Calculate cyclomatic complexity metrics
11//! - **Tolerant parsing mode** for incomplete/kernel code
12//! - **Macro preprocessing** for Linux kernel and system code
13//! - **Layered processing pipeline** for better parsing of kernel code
14//! - **Platform detection** with support for Linux, FreeBSD, Darwin
15//! - Full integration with codegraph-parser-api
16//!
17//! ## Quick Start
18//!
19//! ```rust,no_run
20//! use codegraph_c::CParser;
21//! use codegraph_parser_api::CodeParser;
22//! use codegraph::CodeGraph;
23//! use std::path::Path;
24//!
25//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
26//! let mut graph = CodeGraph::in_memory()?;
27//! let parser = CParser::new();
28//!
29//! let file_info = parser.parse_file(Path::new("main.c"), &mut graph)?;
30//! println!("Parsed {} functions", file_info.functions.len());
31//! # Ok(())
32//! # }
33//! ```
34//!
35//! ## Tolerant Parsing
36//!
37//! For code with syntax errors or missing headers (like kernel code):
38//!
39//! ```rust,no_run
40//! use codegraph_c::{extractor::{extract_with_options, ExtractionOptions}, CParser};
41//! use codegraph_parser_api::ParserConfig;
42//! use std::path::Path;
43//!
44//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
45//! let source = r#"
46//! static __init int my_init(void) { return 0; }
47//! "#;
48//!
49//! // Use kernel-optimized extraction
50//! let options = ExtractionOptions::for_kernel_code();
51//! let result = extract_with_options(source, Path::new("test.c"), &ParserConfig::default(), &options)?;
52//!
53//! println!("Extracted {} functions (partial: {})", result.ir.functions.len(), result.is_partial);
54//! # Ok(())
55//! # }
56//! ```
57//!
58//! ## Layered Pipeline
59//!
60//! For advanced processing with platform-specific optimizations:
61//!
62//! ```rust,no_run
63//! use codegraph_c::pipeline::{Pipeline, PipelineConfig};
64//!
65//! let pipeline = Pipeline::new();
66//! let config = PipelineConfig::for_kernel_code();
67//!
68//! let source = r#"
69//! #include <linux/module.h>
70//! MODULE_LICENSE("GPL");
71//! static __init int my_init(void) { return 0; }
72//! "#;
73//!
74//! let result = pipeline.process(source, &config);
75//! println!("Platform: {} (confidence: {:.0}%)",
76//! result.platform.platform_id,
77//! result.platform.confidence * 100.0);
78//! ```
79
80pub mod extractor;
81mod mapper;
82mod parser_impl;
83pub mod pipeline;
84pub mod platform;
85pub mod preprocessor;
86pub mod visitor;
87
88// Re-export parser-api types for convenience
89pub use codegraph_parser_api::{
90 CodeParser, FileInfo, ParserConfig, ParserError, ParserMetrics, ProjectInfo,
91};
92
93// Export the C parser implementation
94pub use parser_impl::CParser;
95
96// Export key types from submodules
97pub use extractor::{ExtractionOptions, ExtractionResult};
98pub use pipeline::{Pipeline, PipelineConfig, PipelineResult};
99pub use platform::{DetectionResult, PlatformModule, PlatformRegistry};
100pub use preprocessor::{CPreprocessor, MacroInfo, MacroKind};
101pub use visitor::FunctionCall;