Crate codegraph_c

Crate codegraph_c 

Source
Expand description

§codegraph-c

C parser for CodeGraph - extracts code entities and relationships from C source files.

§Features

  • Parse C source files (.c) and header files (.h)
  • Extract functions, structs, unions, enums, and typedefs
  • Track relationships (includes, function calls)
  • Calculate cyclomatic complexity metrics
  • Tolerant parsing mode for incomplete/kernel code
  • Macro preprocessing for Linux kernel and system code
  • Layered processing pipeline for better parsing of kernel code
  • Platform detection with support for Linux, FreeBSD, Darwin
  • Full integration with codegraph-parser-api

§Quick Start

use codegraph_c::CParser;
use codegraph_parser_api::CodeParser;
use codegraph::CodeGraph;
use std::path::Path;

let mut graph = CodeGraph::in_memory()?;
let parser = CParser::new();

let file_info = parser.parse_file(Path::new("main.c"), &mut graph)?;
println!("Parsed {} functions", file_info.functions.len());

§Tolerant Parsing

For code with syntax errors or missing headers (like kernel code):

use codegraph_c::{extractor::{extract_with_options, ExtractionOptions}, CParser};
use codegraph_parser_api::ParserConfig;
use std::path::Path;

let source = r#"
static __init int my_init(void) { return 0; }
"#;

// Use kernel-optimized extraction
let options = ExtractionOptions::for_kernel_code();
let result = extract_with_options(source, Path::new("test.c"), &ParserConfig::default(), &options)?;

println!("Extracted {} functions (partial: {})", result.ir.functions.len(), result.is_partial);

§Layered Pipeline

For advanced processing with platform-specific optimizations:

use codegraph_c::pipeline::{Pipeline, PipelineConfig};

let pipeline = Pipeline::new();
let config = PipelineConfig::for_kernel_code();

let source = r#"
#include <linux/module.h>
MODULE_LICENSE("GPL");
static __init int my_init(void) { return 0; }
"#;

let result = pipeline.process(source, &config);
println!("Platform: {} (confidence: {:.0}%)",
    result.platform.platform_id,
    result.platform.confidence * 100.0);

Re-exports§

pub use extractor::ExtractionOptions;
pub use extractor::ExtractionResult;
pub use pipeline::Pipeline;
pub use pipeline::PipelineConfig;
pub use pipeline::PipelineResult;
pub use platform::DetectionResult;
pub use platform::PlatformModule;
pub use platform::PlatformRegistry;
pub use preprocessor::CPreprocessor;
pub use preprocessor::MacroInfo;
pub use preprocessor::MacroKind;
pub use visitor::FunctionCall;

Modules§

extractor
AST extraction for C source code
pipeline
Layered processing pipeline for C source code
platform
Platform detection and abstraction for C codebases
preprocessor
C Preprocessor simulation layer
visitor
AST visitor for extracting C entities

Structs§

CParser
C language parser implementing the CodeParser trait
FileInfo
Information about a successfully parsed file
ParserConfig
Configuration for parser behavior
ParserMetrics
Metrics collected during parsing
ProjectInfo
Aggregate information about a parsed project

Enums§

ParserError
Errors that can occur during parsing

Traits§

CodeParser
Core trait that all language parsers must implement