Expand description
§cpg-rs
A Rust library for working with Code Property Graphs (CPGs).
This crate provides a complete set of data structures for representing Code Property Graphs, a language-agnostic intermediate representation for static code analysis. CPGs combine abstract syntax trees, control flow graphs, and data flow graphs into a unified structure that enables sophisticated program analysis.
§Features
- Complete implementation of the CPG specification
- Serialization/deserialization support via serde
- Strongly-typed enums for node types, edge types, and properties
- Support for CPG overlays and diff graphs for incremental analysis
§Usage
The main data structures in this crate are:
Cpg
: The root structure containing nodes and edgesNode
: Represents program entities like methods, variables, and expressionsEdge
: Represents relationships between nodesPropertyValue
: Represents typed property values attached to nodes and edges
CPGs can be created programmatically or deserialized from JSON.
§Examples
§Creating and modifying a CPG
use cpg_rs::{Cpg, Node, Edge, NodeType, EdgeType, NodeProperty, NodePropertyName, PropertyValue, PropertyValueEnum};
use std::fs::File;
use std::io::{BufWriter, BufReader};
// Create a new CPG
let cpg = Cpg {
node: vec![
Node {
key: 1,
r#type: NodeType::Method,
property: vec![
NodeProperty {
name: NodePropertyName::Name,
value: Some(PropertyValue {
value: Some(PropertyValueEnum::StringValue("main".to_string())),
}),
},
],
},
],
edge: vec![],
};
// Serialize to JSON
let file = File::create("cpg.json").unwrap();
let writer = BufWriter::new(file);
serde_json::to_writer_pretty(writer, &cpg).unwrap();
// Deserialize from JSON
let file = File::open("cpg.json").unwrap();
let reader = BufReader::new(file);
let cpg: Cpg = serde_json::from_reader(reader).unwrap();
See the examples directory for more examples:
modify_cpg.rs
: Creating, modifying, and saving a CPGfind_methods.rs
: Finding methods and parameters in a CPGdiff_graph.rs
: Working with diff graphs to represent changes to a CPG
See the documentation for individual types for more details.
Structs§
- Additional
Edge Property - Additional
Node Property - Bool
List - A list of boolean values that can be used as a property value.
- Contained
Refs - Represents references to other nodes in the CPG.
- Cpg
- The root structure of a Code Property Graph (CPG).
- CpgOverlay
- Overlays can be stacked onto each other, therefore their node ids must be globally unique.
- Diff
Graph - Represents a set of changes to be applied to a Code Property Graph.
- Double
List - A list of 64-bit floating point values that can be used as a property value.
- Edge
- Represents an edge in a Code Property Graph.
- Edge
Property - Represents a property of an edge in a Code Property Graph.
- Float
List - A list of 32-bit floating point values that can be used as a property value.
- IntList
- A list of 32-bit integer values that can be used as a property value.
- Long
List - A list of 64-bit integer values that can be used as a property value.
- Node
- Represents a node in a Code Property Graph.
- Node
Property - Represents a property of a node in a Code Property Graph.
- Property
Value - Represents a typed property value that can be attached to nodes and edges in a CPG.
- Remove
Edge - Remove
Edge Property - Remove
Node - Remove
Node Property - String
List - A list of string values that can be used as a property value.
Enums§
- Control
Structure Types - Diff
Graph Entry - Dispatch
Types - Edge
Property Name - Edge
Type - Evaluation
Strategies - Languages
- Modifier
Types - Node
Property Name - Node
Type - Property
Value Enum - Represents the different types of values that a property can have in a CPG.