Skip to main content

GraphBuilder

Struct GraphBuilder 

Source
pub struct GraphBuilder {
    pub cpg: CodePropertyGraph,
    symbol_index: HashMap<String, NodeIndex>,
    function_defs: HashMap<String, Vec<NodeIndex>>,
    call_sites: HashMap<String, Vec<NodeIndex>>,
}
Expand description

Incrementally builds a CodePropertyGraph from parser facts.

Supports ingesting facts from multiple files and merging local graphs (e.g., from parallel parsing) into a single global graph with symbol deduplication. Call Self::resolve_calls after all facts are ingested to wire up call sites to their definitions.

§Example

use icb_graph::builder::GraphBuilder;
use icb_parser::facts::RawNode;
use icb_common::{Language, NodeKind};

let mut builder = GraphBuilder::new();
let facts = vec![RawNode {
    language: Language::Python,
    kind: NodeKind::Function,
    name: Some("main".into()),
    usr: None,
    start_line: 1,
    start_col: 0,
    end_line: 1,
    end_col: 5,
    children: vec![],
    source_file: None,
}];
builder.ingest_file_facts(&facts);
builder.resolve_calls();
assert_eq!(builder.cpg.node_count(), 1);

Fields§

§cpg: CodePropertyGraph§symbol_index: HashMap<String, NodeIndex>§function_defs: HashMap<String, Vec<NodeIndex>>§call_sites: HashMap<String, Vec<NodeIndex>>

Implementations§

Source§

impl GraphBuilder

Source

pub fn new() -> Self

Source

pub fn ingest_file_facts(&mut self, facts: &[RawNode])

Ingest facts from a single file.

This method may be called multiple times (even from different threads after merging). Each call enriches the same graph.

Source

pub fn resolve_calls(&mut self)

Resolve calls: for every call site with a matching function/class definition, add a Edge::Call edge from the call site to the definition.

Source

pub fn merge(&mut self, other: GraphBuilder)

Merge another GraphBuilder into this one.

All nodes from other are transferred to self. Nodes with the same symbolic key (USR or name) that already exist in self are reused, and edges are rewired accordingly. The temporary call/def maps are also merged.

Trait Implementations§

Source§

impl Default for GraphBuilder

Source§

fn default() -> GraphBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.