Crate cpg_rs

Source
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 edges
  • Node: Represents program entities like methods, variables, and expressions
  • Edge: Represents relationships between nodes
  • PropertyValue: 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 CPG
  • find_methods.rs: Finding methods and parameters in a CPG
  • diff_graph.rs: Working with diff graphs to represent changes to a CPG

See the documentation for individual types for more details.

Structs§

AdditionalEdgeProperty
AdditionalNodeProperty
BoolList
A list of boolean values that can be used as a property value.
ContainedRefs
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.
DiffGraph
Represents a set of changes to be applied to a Code Property Graph.
DoubleList
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.
EdgeProperty
Represents a property of an edge in a Code Property Graph.
FloatList
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.
LongList
A list of 64-bit integer values that can be used as a property value.
Node
Represents a node in a Code Property Graph.
NodeProperty
Represents a property of a node in a Code Property Graph.
PropertyValue
Represents a typed property value that can be attached to nodes and edges in a CPG.
RemoveEdge
RemoveEdgeProperty
RemoveNode
RemoveNodeProperty
StringList
A list of string values that can be used as a property value.

Enums§

ControlStructureTypes
DiffGraphEntry
DispatchTypes
EdgePropertyName
EdgeType
EvaluationStrategies
Languages
ModifierTypes
NodePropertyName
NodeType
PropertyValueEnum
Represents the different types of values that a property can have in a CPG.