Skip to main content
ClaudeWave
Skill85 repo starsupdated 3mo ago

control-flow-abstraction-generator

Generate abstract Control Flow Graph (CFG) representations of programs showing loops, branches, and function calls for static analysis or verification. Use when users need to: (1) Visualize program control flow structure, (2) Generate CFGs for static analysis tools, (3) Create control flow abstractions for formal verification, (4) Analyze program paths and reachability, (5) Document program structure. Supports both function-level (intraprocedural) and program-level (interprocedural) analysis with multiple output formats (textual, DOT/Graphviz, JSON).

Install in Claude Code
Copy
git clone --depth 1 https://github.com/ArabelaTso/Skills-4-SE /tmp/control-flow-abstraction-generator && cp -r /tmp/control-flow-abstraction-generator/skills/control-flow-abstraction-generator ~/.claude/skills/control-flow-abstraction-generator
Then start a new Claude Code session; the skill loads automatically.

SKILL.md

# Control Flow Abstraction Generator

Generate abstract Control Flow Graph representations of programs.

## Overview

This skill analyzes program code and generates Control Flow Graphs (CFGs) that abstract the program's control flow structure. CFGs show how execution flows through the program via nodes (statements, conditions) and edges (control transfers), making them suitable for static analysis, formal verification, and program understanding.

## How to Use

Provide:
1. **Program code**: Function or program to analyze
2. **Analysis scope**: Function-level or program-level
3. **Output format** (optional): Textual, DOT, JSON, or multiple

The skill will generate:
- CFG with nodes and edges
- Node types (entry, exit, statement, condition, merge)
- Edge types (sequential, true/false branches, back edges, calls)
- Optional: DOT format for visualization, JSON for tool integration

## CFG Generation Workflow

### Step 1: Parse Program Structure

Identify program constructs:
- **Sequential statements**: Assignments, expressions, declarations
- **Conditional statements**: if-then-else, switch-case
- **Loop statements**: while, for, do-while
- **Function calls**: Direct calls, recursive calls
- **Control transfers**: break, continue, return, goto
- **Exception handling**: try-catch-finally

### Step 2: Create CFG Nodes

Generate nodes for each construct:

**Entry Node**: Function/program start
- Label: `ENTRY` or function name
- Type: `entry`
- Successors: First statement

**Exit Node**: Function/program end
- Label: `EXIT` or `return`
- Type: `exit`
- Predecessors: All return points

**Statement Node**: Regular statement
- Label: Statement text or line number
- Type: `statement`
- Represents: Assignment, call, expression

**Condition Node**: Branch decision
- Label: Boolean expression
- Type: `condition`
- Successors: True branch, false branch

**Merge Node**: Branch join point
- Label: `MERGE` or empty
- Type: `merge`
- Predecessors: Multiple branches

### Step 3: Create CFG Edges

Connect nodes with appropriate edges:

**Sequential Edge**: Normal flow
- From: Statement/node
- To: Next statement/node
- Label: None or `→`

**True Edge**: Condition true branch
- From: Condition node
- To: True branch first statement
- Label: `T` or `true`

**False Edge**: Condition false branch
- From: Condition node
- To: False branch first statement
- Label: `F` or `false`

**Back Edge**: Loop iteration
- From: Loop body end
- To: Loop header
- Label: `↶` or `back`

**Call Edge**: Function invocation (interprocedural)
- From: Call site
- To: Called function entry
- Label: `⇒` or `call`

**Return Edge**: Function return (interprocedural)
- From: Called function exit
- To: Call site return point
- Label: `⇐` or `return`

### Step 4: Handle Special Constructs

**Loops**: Create back edges from body to header

**Break**: Create edge from break statement to loop exit

**Continue**: Create back edge from continue to loop header

**Return**: Create edge from return to EXIT node

**Exceptions**: Create exception edges from try block to catch handlers

### Step 5: Generate Output

Produce CFG in requested format(s):
- Textual representation
- DOT format for Graphviz
- JSON for tool integration

## Example: Simple Conditional

**Code**:
```python
def max_value(x, y):
    if x > y:
        result = x
    else:
        result = y
    return result
```

**CFG (Textual)**:
```
Node 1 (ENTRY):
  Label: max_value
  Successors: [2]

Node 2 (x > y):
  Type: condition
  Predecessors: [1]
  Successors: [3 (true), 4 (false)]

Node 3 (result = x):
  Type: statement
  Predecessors: [2]
  Successors: [5]

Node 4 (result = y):
  Type: statement
  Predecessors: [2]
  Successors: [5]

Node 5 (MERGE):
  Type: merge
  Predecessors: [3, 4]
  Successors: [6]

Node 6 (return result):
  Type: statement
  Predecessors: [5]
  Successors: [7]

Node 7 (EXIT):
  Predecessors: [6]
```

**CFG (Visual)**:
```
    ENTRY
      ↓
   [x > y]
    ↓   ↓
   T↓   ↓F
    ↓   ↓
[result=x] [result=y]
    ↓       ↓
    └→MERGE←┘
        ↓
  [return result]
        ↓
      EXIT
```

**CFG (DOT)**:
```dot
digraph CFG {
  node [shape=box];

  n1 [label="ENTRY", shape=ellipse];
  n2 [label="x > y", shape=diamond];
  n3 [label="result = x"];
  n4 [label="result = y"];
  n5 [label="MERGE", shape=circle];
  n6 [label="return result"];
  n7 [label="EXIT", shape=ellipse];

  n1 -> n2;
  n2 -> n3 [label="T", color=green];
  n2 -> n4 [label="F", color=red];
  n3 -> n5;
  n4 -> n5;
  n5 -> n6;
  n6 -> n7;
}
```

## Example: While Loop

**Code**:
```python
def sum_to_n(n):
    sum = 0
    i = 0
    while i < n:
        sum += i
        i += 1
    return sum
```

**CFG (Visual)**:
```
    ENTRY
      ↓
   [sum = 0]
      ↓
   [i = 0]
      ↓
      ┌─────────┐
      ↓         ↑
   [i < n]      ↑ (back edge)
    ↓   ↓       ↑
   T↓   ↓F      ↑
    ↓   ↓       ↑
[sum += i]      ↑
      ↓         ↑
  [i += 1]──────┘
      ↓F
[return sum]
      ↓
    EXIT
```

**Key Features**:
- Loop header: `[i < n]`
- Back edge: From `[i += 1]` to `[i < n]`
- Exit edge: False branch from condition to return

**CFG (Textual)**:
```
Node 1 (ENTRY)
  → Node 2

Node 2 (sum = 0)
  → Node 3

Node 3 (i = 0)
  → Node 4

Node 4 (i < n) [LOOP HEADER]
  →T Node 5
  →F Node 7

Node 5 (sum += i)
  → Node 6

Node 6 (i += 1)
  → Node 4 [BACK EDGE]

Node 7 (return sum)
  → Node 8

Node 8 (EXIT)
```

## Example: Nested Control Flow

**Code**:
```python
def process(arr, threshold):
    result = []
    for item in arr:
        if item > threshold:
            result.append(item * 2)
        else:
            if item < 0:
                continue
            result.append(item)
    return result
```

**CFG (Visual)**:
```
ENTRY
  ↓
[result = []]
  ↓
[i = 0]
  ↓
  ┌──────────────────────┐
  ↓                      ↑
[i < len(arr)]           ↑
  ↓T                     ↑
[item = arr[i]]          ↑
  ↓                      ↑
[item > threshold]       ↑
  ↓T            ↓F       ↑
[result.append  [item<0] ↑
 (item*2)]
abstract-domain-explorerSkill

Applies abstract interpretation using different abstract domains (intervals, octagons, polyhedra, sign, congruence) to statically analyze program variables and infer invariants, value ranges, and relationships. Use when analyzing program properties, inferring loop invariants, detecting potential errors, or understanding variable relationships through static analysis.

abstract-invariant-generatorSkill

Uses abstract interpretation to automatically infer loop invariants, function preconditions, and postconditions for formal verification. Generates invariants that capture program behavior and support correctness proofs in Dafny, Isabelle, Coq, and other verification systems. Use when adding formal specifications to code, generating verification conditions, inferring contracts for functions, or discovering loop invariants for proofs.

abstract-state-analyzerSkill

Performs abstract interpretation over source code to infer possible program states, variable ranges, and data properties without executing the program. Reports potential runtime errors including out-of-bounds accesses, null dereferences, type inconsistencies, division by zero, and integer overflows. Use when analyzing code for potential runtime errors, performing static analysis, checking safety properties, or verifying program behavior without execution.

abstract-trace-summarizerSkill

Performs abstract interpretation to produce summarized execution traces and high-level program behavior representations. Highlights key control flow paths, variable relationships, loop invariants, function summaries, and potential runtime states using abstract domains (intervals, signs, nullness, etc.). Use when analyzing program behavior, understanding execution paths, computing loop invariants, tracking variable ranges, detecting potential runtime errors, or generating program summaries without concrete execution.

acsl-annotation-assistantSkill

Create ACSL (ANSI/ISO C Specification Language) formal annotations for C/C++ programs. Use this skill when working with formal verification, adding function contracts (requires/ensures), loop invariants, assertions, memory safety annotations, or any ACSL specifications. Supports Frama-C verification and generates comprehensive formal specifications for C/C++ code.

agent-browserSkill

CLI-based browser automation with persistent page state using ref-based element interaction. Use when users ask to navigate websites, interact with web pages, fill forms, take screenshots, test web applications, or extract information from web pages.

ambiguity-detectorSkill

Detects and analyzes ambiguous language in software requirements and user stories. Use when reviewing requirements documents, user stories, specifications, or any software requirement text to identify vague quantifiers, unclear scope, undefined terms, missing edge cases, subjective language, and incomplete specifications. Provides detailed analysis with clarifying questions and suggested improvements.

api-design-assistantSkill

Design and review APIs with suggestions for endpoints, parameters, return types, and best practices. Use when designing new APIs from requirements, reviewing existing API designs, generating API documentation, or getting implementation guidance. Supports REST APIs with focus on endpoint structure, request/response schemas, authentication, pagination, filtering, versioning, and OpenAPI specifications. Triggers when users ask to design, review, document, or improve APIs.